詳解C語言內核字符串拷貝與比較
在上一篇文章《驅動開發(fā):內核字符串轉換方法》中簡單介紹了內核是如何使用字符串以及字符串之間的轉換方法,本章將繼續(xù)探索字符串的拷貝與比較,與應用層不同內核字符串拷貝與比較也需要使用內核專用的API函數(shù),字符串的拷貝往往伴隨有內核內存分配,我們將首先簡單介紹內核如何分配堆空間,然后再以此為契機簡介字符串的拷貝與比較。
首先內核中的堆棧分配可以使用ExAllocatePool()
這個內核函數(shù)實現(xiàn),此外還可以使用ExAllocatePoolWithTag()
函數(shù),兩者的區(qū)別是,第一個函數(shù)可以直接分配內存,第二個函數(shù)在分配時需要指定一個標簽,此外內核屬性常用的有兩種NonPagedPool
用于分配非分頁內存,而PagePool
則用于分配分頁內存,在開發(fā)中推薦使用非分頁內存,因為分頁內存數(shù)量有限。
內存分配使用ExAllocatePool
函數(shù),內存拷貝可使用RtlCopyMemory
函數(shù),需要注意該函數(shù)其實是對Memcpy
函數(shù)的包裝。
#include <ntifs.h> VOID UnDriver(PDRIVER_OBJECT driver) { DbgPrint("驅動已卸載 \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"; // 設置最大長度 uncode_buffer.MaximumLength = 1024; // 分配內存空間 uncode_buffer.Buffer = (PWSTR)ExAllocatePool(PagedPool, 1024); // 設置字符長度 因為是寬字符,所以是字符長度的 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); // 設置字符串長度 并輸出 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("驅動已加載 \n"); Driver->DriverUnload = UnDriver; return STATUS_SUCCESS; }
代碼輸出效果:
實現(xiàn)空間分配
字符串結構UNICODE_STRING
可以定義數(shù)組,空間的分配也可以循環(huán)進行,例如我們分配十個字符串結構,并輸出結構內的參數(shù)。
#include <ntifs.h> VOID UnDriver(PDRIVER_OBJECT driver) { DbgPrint("驅動已卸載 \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ù)組長度: %d \n", size); for (int x = 0; x < size; x++) { // 分配空間 uncode_buffer[x].Buffer = (PWSTR)ExAllocatePool(PagedPool, 1024); // 設置長度 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]); // 釋放內存 ExFreePool(uncode_buffer[x].Buffer); uncode_buffer[x].Buffer = NULL; uncode_buffer[x].Length = uncode_buffer[x].MaximumLength = 0; } DbgPrint("驅動加載成功 \n"); Driver->DriverUnload = UnDriver; return STATUS_SUCCESS; }
代碼輸出效果:
實現(xiàn)字符串拷貝
此處可以直接使用RtlCopyMemory
函數(shù)直接對內存操作,也可以調用內核提供的RtlCopyUnicodeString
函數(shù)來實現(xiàn),具體代碼如下。
#include <ntifs.h> VOID UnDriver(PDRIVER_OBJECT driver) { DbgPrint("驅動已卸載 \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中的內容拷貝到target中 RtlCopyUnicodeString(&uncode_buffer_target, &uncode_buffer_source); // 輸出結果 DbgPrint("source = %wZ \n", &uncode_buffer_source); DbgPrint("target = %wZ \n", &uncode_buffer_target); // 釋放空間 source 無需銷毀 // 如果強制釋放掉source則會導致系統(tǒng)藍屏,因為source是在棧上的 RtlFreeUnicodeString(&uncode_buffer_target); DbgPrint("驅動加載成功 \n"); Driver->DriverUnload = UnDriver; return STATUS_SUCCESS; }
代碼輸出效果:
實現(xiàn)字符串比較
如果需要比較兩個UNICODE_STRING
字符串結構體是否相等,那么可以使用RtlEqualUnicodeString
這個內核函數(shù)實現(xiàn),該函數(shù)第三個參數(shù)是返回值類型,如果是TRUE則默認返回真,否則返回假,具體代碼如下。
#include <ntifs.h> VOID UnDriver(PDRIVER_OBJECT driver) { DbgPrint("驅動已卸載 \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("驅動加載成功 \n"); Driver->DriverUnload = UnDriver; return STATUS_SUCCESS; }
代碼輸出效果:
有時在字符串比較時需要統(tǒng)一字符串格式,例如全部變大寫以后在做比較等,此時可以使用RtlUpcaseUnicodeString
函數(shù)將小寫字符串為大寫,然后在做比較,代碼如下。
#include <ntifs.h> VOID UnDriver(PDRIVER_OBJECT driver) { DbgPrint("驅動已卸載 \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"); // 字符串小寫變大寫 RtlUpcaseUnicodeString(&uncode_buffer_target, &uncode_buffer_source, TRUE); DbgPrint("小寫輸出: %wZ \n", &uncode_buffer_source); DbgPrint("變大寫輸出: %wZ \n", &uncode_buffer_target); // 銷毀字符串 RtlFreeUnicodeString(&uncode_buffer_target); DbgPrint("驅動加載成功 \n"); Driver->DriverUnload = UnDriver; return STATUS_SUCCESS; }
代碼輸出效果:
以上就是詳解C語言內核字符串拷貝與比較的詳細內容,更多關于C語言內核字符串拷貝 比較的資料請關注腳本之家其它相關文章!
相關文章
C語言修煉之路數(shù)據(jù)類型悟正法 解析存儲定風魔下篇
使用編程語言進行編程時,需要用到各種變量來存儲各種信息。變量保留的是它所存儲的值的內存位置。這意味著,當您創(chuàng)建一個變量時,就會在內存中保留一些空間。您可能需要存儲各種數(shù)據(jù)類型的信息,操作系統(tǒng)會根據(jù)變量的數(shù)據(jù)類型,來分配內存和決定在保留內存中存儲什么2022-02-02C語言實現(xiàn)大數(shù)值金額大寫轉換的方法詳解
這篇文章主要為大家詳細介紹了如何利用C語言實現(xiàn)大數(shù)值金額大寫轉換的功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起了解一下2023-03-03C語言數(shù)據(jù)結構與算法時間空間復雜度基礎實踐
這篇文章主要為大家介紹了C語言數(shù)據(jù)結構與算法中時間空間復雜度的基礎實踐,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-02-02