日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

詳解C語(yǔ)言?xún)?nèi)核字符串拷貝與比較_C 語(yǔ)言

作者:lyshark ? 更新時(shí)間: 2022-11-18 編程語(yǔ)言

在上一篇文章《驅(qū)動(dòng)開(kāi)發(fā):內(nèi)核字符串轉(zhuǎn)換方法》中簡(jiǎn)單介紹了內(nèi)核是如何使用字符串以及字符串之間的轉(zhuǎn)換方法,本章將繼續(xù)探索字符串的拷貝與比較,與應(yīng)用層不同內(nèi)核字符串拷貝與比較也需要使用內(nèi)核專(zhuān)用的API函數(shù),字符串的拷貝往往伴隨有內(nèi)核內(nèi)存分配,我們將首先簡(jiǎn)單介紹內(nèi)核如何分配堆空間,然后再以此為契機(jī)簡(jiǎn)介字符串的拷貝與比較。

首先內(nèi)核中的堆棧分配可以使用ExAllocatePool()這個(gè)內(nèi)核函數(shù)實(shí)現(xiàn),此外還可以使用ExAllocatePoolWithTag()函數(shù),兩者的區(qū)別是,第一個(gè)函數(shù)可以直接分配內(nèi)存,第二個(gè)函數(shù)在分配時(shí)需要指定一個(gè)標(biāo)簽,此外內(nèi)核屬性常用的有兩種NonPagedPool用于分配非分頁(yè)內(nèi)存,而PagePool則用于分配分頁(yè)內(nèi)存,在開(kāi)發(fā)中推薦使用非分頁(yè)內(nèi)存,因?yàn)榉猪?yè)內(nèi)存數(shù)量有限。

內(nèi)存分配使用ExAllocatePool函數(shù),內(nèi)存拷貝可使用RtlCopyMemory函數(shù),需要注意該函數(shù)其實(shí)是對(duì)Memcpy函數(shù)的包裝。

#include <ntifs.h>

VOID UnDriver(PDRIVER_OBJECT driver)
{
	DbgPrint("驅(qū)動(dòng)已卸載 \n");
}

// PowerBy: LyShark
NTSTATUS DriverEntry(IN PDRIVER_OBJECT Driver, PUNICODE_STRING RegistryPath)
{
	UNICODE_STRING uncode_buffer = { 0 };

	DbgPrint("hello lyshark \n");

	wchar_t * wchar_string = L"hello lyshark";

	// 設(shè)置最大長(zhǎng)度
	uncode_buffer.MaximumLength = 1024;

	// 分配內(nèi)存空間
	uncode_buffer.Buffer = (PWSTR)ExAllocatePool(PagedPool, 1024);

	// 設(shè)置字符長(zhǎng)度 因?yàn)槭菍捵址?,所以是字符長(zhǎng)度的 2 倍
	uncode_buffer.Length = wcslen(wchar_string) * 2;

	// 保證緩沖區(qū)足夠大,否則程序終止
	ASSERT(uncode_buffer.MaximumLength >= uncode_buffer.Length);

	// 將 wchar_string 中的字符串拷貝到 uncode_buffer.Buffer
	RtlCopyMemory(uncode_buffer.Buffer, wchar_string, uncode_buffer.Length);

	// 設(shè)置字符串長(zhǎng)度 并輸出
	uncode_buffer.Length = wcslen(wchar_string) * 2;
	DbgPrint("輸出字符串: %wZ \n", uncode_buffer);

	// 釋放堆空間
	ExFreePool(uncode_buffer.Buffer);
	uncode_buffer.Buffer = NULL;
	uncode_buffer.Length = uncode_buffer.MaximumLength = 0;

	DbgPrint("驅(qū)動(dòng)已加載 \n");
	Driver->DriverUnload = UnDriver;
	return STATUS_SUCCESS;
}

代碼輸出效果:

實(shí)現(xiàn)空間分配

字符串結(jié)構(gòu)UNICODE_STRING可以定義數(shù)組,空間的分配也可以循環(huán)進(jìn)行,例如我們分配十個(gè)字符串結(jié)構(gòu),并輸出結(jié)構(gòu)內(nèi)的參數(shù)。

#include <ntifs.h>

VOID UnDriver(PDRIVER_OBJECT driver)
{
	DbgPrint("驅(qū)動(dòng)已卸載 \n");
}

// PowerBy: LyShark
NTSTATUS DriverEntry(IN PDRIVER_OBJECT Driver, PUNICODE_STRING RegistryPath)
{
	UNICODE_STRING uncode_buffer[10] = { 0 };
	wchar_t * wchar_string = L"hello lyshark";

	DbgPrint("hello lyshark \n");

	int size = sizeof(uncode_buffer) / sizeof(uncode_buffer[0]);
	DbgPrint("數(shù)組長(zhǎng)度: %d \n", size);

	for (int x = 0; x < size; x++)
	{
		// 分配空間
		uncode_buffer[x].Buffer = (PWSTR)ExAllocatePool(PagedPool, 1024);

		// 設(shè)置長(zhǎng)度
		uncode_buffer[x].MaximumLength = 1024;
		uncode_buffer[x].Length = wcslen(wchar_string) * sizeof(WCHAR);
		ASSERT(uncode_buffer[x].MaximumLength >= uncode_buffer[x].Length);

		// 拷貝字符串并輸出
		RtlCopyMemory(uncode_buffer[x].Buffer, wchar_string, uncode_buffer[x].Length);
		uncode_buffer[x].Length = wcslen(wchar_string) * sizeof(WCHAR);
		DbgPrint("循環(huán): %d 輸出字符串: %wZ \n", x, uncode_buffer[x]);

		// 釋放內(nèi)存
		ExFreePool(uncode_buffer[x].Buffer);
		uncode_buffer[x].Buffer = NULL;
		uncode_buffer[x].Length = uncode_buffer[x].MaximumLength = 0;
	}

	DbgPrint("驅(qū)動(dòng)加載成功 \n");
	Driver->DriverUnload = UnDriver;
	return STATUS_SUCCESS;
}

代碼輸出效果:

實(shí)現(xiàn)字符串拷貝

此處可以直接使用RtlCopyMemory函數(shù)直接對(duì)內(nèi)存操作,也可以調(diào)用內(nèi)核提供的RtlCopyUnicodeString函數(shù)來(lái)實(shí)現(xiàn),具體代碼如下。

#include <ntifs.h>

VOID UnDriver(PDRIVER_OBJECT driver)
{
	DbgPrint("驅(qū)動(dòng)已卸載 \n");
}

// PowerBy: LyShark
NTSTATUS DriverEntry(IN PDRIVER_OBJECT Driver, PUNICODE_STRING RegistryPath)
{
	DbgPrint("hello lyshark \n");

	UNICODE_STRING uncode_buffer_source = { 0 };
	UNICODE_STRING uncode_buffer_target = { 0 };

	// 該函數(shù)可用于初始化字符串
	RtlInitUnicodeString(&uncode_buffer_source, L"hello lyshark");

	// 初始化target字符串,分配空間
	uncode_buffer_target.Buffer = (PWSTR)ExAllocatePool(PagedPool, 1024);
	uncode_buffer_target.MaximumLength = 1024;

	// 將source中的內(nèi)容拷貝到target中
	RtlCopyUnicodeString(&uncode_buffer_target, &uncode_buffer_source);

	// 輸出結(jié)果
	DbgPrint("source = %wZ \n", &uncode_buffer_source);
	DbgPrint("target = %wZ \n", &uncode_buffer_target);

	// 釋放空間 source 無(wú)需銷(xiāo)毀
	// 如果強(qiáng)制釋放掉source則會(huì)導(dǎo)致系統(tǒng)藍(lán)屏,因?yàn)閟ource是在棧上的
	RtlFreeUnicodeString(&uncode_buffer_target);

	DbgPrint("驅(qū)動(dòng)加載成功 \n");

	Driver->DriverUnload = UnDriver;
	return STATUS_SUCCESS;
}

代碼輸出效果:

實(shí)現(xiàn)字符串比較

如果需要比較兩個(gè)UNICODE_STRING字符串結(jié)構(gòu)體是否相等,那么可以使用RtlEqualUnicodeString這個(gè)內(nèi)核函數(shù)實(shí)現(xiàn),該函數(shù)第三個(gè)參數(shù)是返回值類(lèi)型,如果是TRUE則默認(rèn)返回真,否則返回假,具體代碼如下。

#include <ntifs.h>

VOID UnDriver(PDRIVER_OBJECT driver)
{
	DbgPrint("驅(qū)動(dòng)已卸載 \n");
}

// PowerBy: LyShark
NTSTATUS DriverEntry(IN PDRIVER_OBJECT Driver, PUNICODE_STRING RegistryPath)
{
	DbgPrint("hello lyshark \n");

	UNICODE_STRING uncode_buffer_source = { 0 };
	UNICODE_STRING uncode_buffer_target = { 0 };

	// 該函數(shù)可用于初始化字符串
	RtlInitUnicodeString(&uncode_buffer_source, L"hello lyshark");
	RtlInitUnicodeString(&uncode_buffer_target, L"hello lyshark");

	// 比較字符串是否相等
	if (RtlEqualUnicodeString(&uncode_buffer_source, &uncode_buffer_target, TRUE))
	{
		DbgPrint("字符串相等 \n");
	}
	else
	{
		DbgPrint("字符串不相等 \n");
	}

	DbgPrint("驅(qū)動(dòng)加載成功 \n");

	Driver->DriverUnload = UnDriver;
	return STATUS_SUCCESS;
}

代碼輸出效果:

有時(shí)在字符串比較時(shí)需要統(tǒng)一字符串格式,例如全部變大寫(xiě)以后在做比較等,此時(shí)可以使用RtlUpcaseUnicodeString函數(shù)將小寫(xiě)字符串為大寫(xiě),然后在做比較,代碼如下。

#include <ntifs.h>

VOID UnDriver(PDRIVER_OBJECT driver)
{
	DbgPrint("驅(qū)動(dòng)已卸載 \n");
}

// PowerBy: LyShark
NTSTATUS DriverEntry(IN PDRIVER_OBJECT Driver, PUNICODE_STRING RegistryPath)
{
	DbgPrint("hello lyshark \n");

	UNICODE_STRING uncode_buffer_source = { 0 };
	UNICODE_STRING uncode_buffer_target = { 0 };

	// 該函數(shù)可用于初始化字符串
	RtlInitUnicodeString(&uncode_buffer_source, L"hello lyshark");
	RtlInitUnicodeString(&uncode_buffer_target, L"HELLO LYSHARK");

	// 字符串小寫(xiě)變大寫(xiě)
	RtlUpcaseUnicodeString(&uncode_buffer_target, &uncode_buffer_source, TRUE);
	DbgPrint("小寫(xiě)輸出: %wZ \n", &uncode_buffer_source);
	DbgPrint("變大寫(xiě)輸出: %wZ \n", &uncode_buffer_target);

	// 銷(xiāo)毀字符串
	RtlFreeUnicodeString(&uncode_buffer_target);

	DbgPrint("驅(qū)動(dòng)加載成功 \n");

	Driver->DriverUnload = UnDriver;
	return STATUS_SUCCESS;
}

代碼輸出效果:

原文鏈接:https://www.cnblogs.com/LyShark/p/16740467.html

欄目分類(lèi)
最近更新