看流星社区

 找回密码
 注册账号
查看: 2272|回复: 0

驱动内核模式下的字符串操作

[复制链接]

该用户从未签到

发表于 2016-9-21 15:44:12 | 显示全部楼层 |阅读模式

1)ASCII字符串和宽字符串   

在应用程序中使用两种字符:
a) char型字符串,负责记录ANSI字符集,它是指向一个char数组的指针,每个char型变量大小是一个字节,字符串是以0标志字符串结束的;
b) wchar_t型的宽字符串,负责描述unicode字符集,它是指向一个wchar_t数组的指针,wchar_t字符大小为两个字节,字符串以0标志字符串结束。   
例:
ANSI字符构造如下:   char *str1 = "ASCE";   
UNICODE字符构造如下:wchar_t *str2 = L"ASCE";   
注:在构造字符串时使用关键字“L”,编译器会自动生成所需要的宽字符   
  
在驱动开发中,DDK将char和wchar_t替换成CHAR和WCHAR。
驱动程序中使用KdPrint宏打印ASCII字符串和宽字符串:   
例:
  1. CHAR *string1 = "ASCE";
  2. KdPrint(("%s\n", string1)); //注意是小写%s

  3. WCHAR *string2 = L"ASCE";
  4. KdPrint(("%S\n", string2));
复制代码

   
2)ANSI_STRING字符串与UNICODE_STRING字符串  
  
DDK不鼓励程序员使用C语言的字符串,主要是因为标准C字符串处理函数容易导致缓冲区溢出等错误。应该使用DDK自定义的字符串:
这个结构对ASCII字符串进行封装:  
  1. typedef struct _STRING {
  2.   USHORT Length; //字符的长度,单位是字节
  3.   USHORT MaximumLength; //整个字符串缓冲区的最大长度
  4.   PCHAR Buffer; //缓冲区的指针
  5. } ANSI_STRING, *PANSI_STRING;
复制代码


标准C字符串中,如果缓冲区长度是N,则只能容纳N-1个字符的字符串,最后一个字节存储NULL;
而在STRING字符串中,缓冲区大小是MaximumLength,最大字符串长度可以是MaximumLength,而不是MaximumLength-1。   


DDK将宽字符串封装成UNICODE_STRING数据结构:     
  1. typedef struct _UNICODE_STRING {
  2.   USHORT Length; //字符的长度,单位是字节。如果是N个字符,那么Length等于N的2倍
  3.   USHORT MaximumLength; //整个字符串缓冲区的最大长度,单位是字节
  4.   PWSTR Buffer; //缓冲区的指针
  5. } UNICODE_STRING, *PUNICODE_STRING;
复制代码

与ANSI_STRING一样,UNICODE_STRING字符串不是以NULL为结束标志的。   


打印这两种字符串方法如下:   
  1. ANSI_STRING ansiString;
  2. //此处略去对ansiString的初始化

  3. KdPrint(("%Z\n", &ansiString)); //注意是%Z

  4. UNICODE_STRING uniString;
  5. //此处略去对uniString的初始化

  6. KdPrint(("%wZ\n", &uniString));
复制代码

  
   
3)字符的初始化和销毁   
  
ANSI_STRING字符串和UNICODE_STRING字符串使用前需要进行初始化,有两种方法构造这个数据结构:   
  
(1)使用DDK提供的函数:   

初始化ANSI_STRING字符串:
  
  1. VOID RtlInitAnsiString(
  2.   __out PANSI_STRING DestinationString, //要初始化的ANSI_STRING字符串
  3.   __in_opt PCSZ SourceString //字符串的内容
  4. );
复制代码

  
初始化UNICODE_STRING字符串:
  1. VOID RtlInitUnicodeString(
  2.   __out PUNICODE_STRING DestinationString, //要初始化的UNICODE_STRING字符串
  3.   __in_opt PCWSTR SourceString //字符串的内容
  4. );
复制代码

  
这种初始化的优点是操作简单,用完后不用清理内存。但有一个问题,就是如果修改SourceString,同时会导致DestinationString字符发生变化:
  1. ANSI_STRING ansiString;
  2. CHAR *string = "asce";

  3. //初始化ANSI_STRING字符串
  4. RtlInitAnsiString(&ansiString, string);
  5. KdPrint(("ansiString: %Z\n", &ansiString));
  6.   
  7. //改变string
  8. string[0] = 'a';
  9. string[1] = 's';
  10. string[2] = 'c';
  11. string[3] = 'e';
  12.   
  13. //改变string的同时ansiString也改变了
  14. KdPrint(("ansiString: %Z\n", &ansiString));
复制代码

  
  
(2)程序员自己申请内存,并初始化内存,当不用字符串时,需要回收字符串占用的内存:   
  1. #define BUFFER_SIZE 1024
  2.   
  3. UNICODE_STRING UnicodeString = {0};
  4.   
  5. //设置缓冲区大小
  6. UnicodeString.MaximumLength = BUFFER_SIZE;
  7.   
  8. //分配内存
  9. UnicodeString.Buffer = (PWSTR)ExAllocatePool(PagedPool, BUFFER_SIZE);
  10. WCHAR *wideString = L"ASCE";

  11. //设置字符串长度,因为是宽字符,所以是字符长度的倍
  12. UnicodeString.Length = 2*wcslen(wideString);
  13.   
  14. //保证缓冲区足够大,否则程序终止
  15. ASSERT(UnicodeString.MaximumLength >= UnicodeString.Length);
  16.   
  17. //内存复制
  18. RtlCopyString(UnicodeString.Buffer, wideString, UnicodeString.Length);

  19. KdPrint(("UnicodeString: %wZ\n", &UnicodeString));

  20. //清理内存
  21. ExFreePool(UnicodeString.Buffer);

  22. UnicodeString.Buffer = NULL;

  23. UnicodeString.Length = UnicodeString.MaximumLength = 0;
复制代码

  
对于最后一步清理内存,DDK给出了简化函数,分别是RtlFreeAnsiString和RtlFreeUnicodeString,这两个函数内部调用了ExFreePool去回收内存的。
  
4)字符串复制   
  
DDK提供针对ANSI_STRING字符串和UNICODE_STRING字符串的复制字符串函数:   
  
  1. VOID RtlCopyString(
  2.   __out PSTRING DestinationString, //目的字符串
  3.   __in_opt const STRING *SourceString //源字符串
  4. );
  5.   
  6. VOID RtlCopyUnicodeString(
  7.   __inout PUNICODE_STRING DestinationString, //目的字符串
  8.   __in_opt PCUNICODE_STRING SourceString //源字符串
  9. );
复制代码

  
下面代码说明了RtlCopyUnicodeString函数的使用:   
  1. #define BUFFER_SIZE 1024
  2.   
  3. //初始化UnicodeString1
  4. UNICODE_STRING UnicodeString1;
  5. RtlInitUnicodeString(&UnicodeString1, L"ASCE");

  6. //初始化UnicodeString2
  7. UNICODE_STRING UnicodeString2 = {0};
  8. UnicodeString2.Buffer = (PWSTR)ExAllocatePool(PagedPool, BUFFER_SIZE);
  9. UnicodeString2.MaximumLength = BUFFER_SIZE;

  10. //将初始化UnicodeString1复制到UnicodeString2
  11. RtlCopyUnicodeString(&UnicodeString2, &UnicodeString1);

  12. //分别显示UnicodeString1和UnicodeString2
  13. KdPrint(("UnicodeString1: %wZ\n", &UnicodeString1));
  14. KdPrint(("UnicodeString2: %wZ\n", &UnicodeString2));

  15. //销毁UnicodeString2,注意:UnicodeString1不用销毁
  16. RtlFreeUnicodeString(&UnicodeString2);
复制代码

  
5)字符串比较   
  
DDK提供了对ANSI_STRING字符串和UNICODE_STRING字符串的相关比较函数:   
  1. LONG RtlCompareString(
  2.   __in const STRING *String1, //要比较的第一个字符串
  3.   __in const STRING *String2, //要比较的第二个字符串
  4.   __in BOOLEAN CaseInSensitive //是否对大小写敏感
  5. );
  6.   
  7. LONG RtlCompareUnicodeString(
  8.   __in PCUNICODE_STRING String1, //要比较的第一个字符串
  9.   __in PCUNICODE_STRING String2, //要比较的第二个字符串
  10.   __in BOOLEAN CaseInSensitive //是否对大小写敏感
  11. );
复制代码

  
DDK同时提供了RtlEqualString和RtlEqualUnicodeString函数,返回为非零代表相等,零代表不相等:   
  1. BOOLEAN RtlEqualString(
  2.   __in const STRING *String1,
  3.   __in const STRING *String2,
  4.   __in BOOLEAN CaseInSensitive
  5. );
  6.   
  7. BOOLEAN RtlEqualUnicodeString(
  8.   __in PCUNICODE_STRING String1,
  9.   __in PCUNICODE_STRING String2,
  10.   __in BOOLEAN CaseInSensitive
  11. );
复制代码

  
函数实例:   
  1. //初始化UnicodeString1
  2. UNICODE_STRING UnicodeString1;
  3. RtlInitUnicodeString(&UnicodeString1, L"ASCE");

  4. //初始化UnicodeString2
  5. UNICODE_STRING UnicodeString2;
  6. RtlnitUnicodeString(&UnicodeString2, L"ASCE BOY");

  7. //判断字符串是否相等
  8. if(RtlEqualUnicodeString(&UnicodeString1, &UnicodeString2, TRUE))
  9. {
  10.          KdPrint(("UnicodeString1 and UnicodeString2 are equal\n"));
  11. } else {
  12.          KdPrint(("UnicodeString1 and UnicodeString2 are not euqal\n"));
  13. }
复制代码

  
6)字符串转化成大写   
  
DDK提供的将ANSI_STRING字符串和UNICODE_STRING字符串转换成大写的函数如下:   
  1. VOID RtlUpperString(   
  2.   __inout  PSTRING DestinationString, //目的字符串   
  3.   __in     const STRING *SourceString //源字符串   
  4. );   
  5.   
  6. NTSTATUS RtlUpcaseUnicodeString(   
  7.   __inout  PUNICODE_STRING DestinationString, //目的字符串   
  8.   __in     PCUNICODE_STRING SourceString, //源字符串   
  9.   __in     BOOLEAN AllocateDestinationString //是否为目的字符串分配内存,   
  10. //目的字符串和源字符串可以是同一个字符串   
  11. );   
复制代码

  
实例代码:   
  1. //初始化UnicodeString1   
  2. UNICODE_STRING UnicodeString;   
  3. RtlInitUnicodeString(&UnicodeString, L"ASCE BOY");   
  4.   
  5. //变化前   
  6. KdPrint(("UnicodeString: %wZ\n", &UnicodeString));   
  7.   
  8. //转化成大写   
  9. RtlUpcaseUnicodeString(&UnicodeString, &UnicodeString, FALSE);   
  10.   
  11. //变化后   
  12. KdPrint(("UnicodeString: %wZ\n", &UnicodeString));   
复制代码

  
   
7)字符串与整型数字相互转换   
  
将UNICODE_STRING字符串转换成整数:   
  1. NTSTATUS RtlUnicodeStringToInteger(   
  2.   __in      PCUNICODE_STRING String, //需要转换的字符串   
  3.   __in_opt  ULONG Base, //转换的数的进制(如2、8、10、16)   
  4.   __out     PULONG Value //需要转换的数字   
  5. );   
复制代码

  
   
  
将整数转换成UNICODE_STRING字符串:   
  1. NTSTATUS RtlIntegerToUnicodeString(   
  2.   __in      ULONG Value, //需要转换的数字   
  3.   __in_opt  ULONG Base, //转换的数的进制(2、8、10、16)   
  4.   __inout   PUNICODE_STRING String //需要转换的字符串   
  5. );
复制代码

  
实例代码如下:   
  1. #define BUFFER_SIZE 1024   
  2.   
  3. //字符串转换成数字   
  4. UNICODE_STRING UnicodeString;   
  5. RtlInitUnicodeString(&UnicodeString, L"-100");   
  6. ULONG lNumber;   
  7. NTSTATUS nStatus = RtlUnicodeStringToInteger(&UnicodeString, 10, &lNumber);   
  8. if(NT_SUCCESS(nStatus))   
  9. {   
  10.          KdPrint(("Conver to integer successfully\n"));   
  11.          KdPrint(("Result : %d\n", lNumber));   
  12. }   
  13. else  
  14. {   
  15.          KdPrint(("Conver to integer failed\n"));   
  16. }   

  17. //将数字转换成字符串   
  18. UNICODE_STRING UnicodeStringSec = {0};   
  19. UnicodeStringSec.Buffer = (PWSTR)ExAllocatePool(PagedPool, BUFFER_SIZE);   
  20. UnicodeStringSec.MaximumLength = BUFFER_SIZE;   
  21.   
  22. nStatus = RtlIntegerToUnicodeString(200, 10, &UnicodeStringSec);   

  23. if(NT_SUCCESS(nStatus))   
  24. {   
  25.          KdPrint(("Cover to string successfully\n"));   
  26.          KdPrint(("Result : %wZ\n", &UnicodeStringSec));   
  27. }   
  28. else  
  29. {   
  30.          KdPrint(("Conver to string failed\n"));   
  31. }   

  32. //销毁UnicodeStringSec,注意:UnicodeString不用销毁   
  33. RtlFreeUnicodeString(&UnicodeStringSec);   
复制代码

  
   
8)ANSI_STRING字符串与UNICODE_STRING字符串的转换   
  
将UNICODE_STRING字符串转换成ANSI_STRING字符串:   
  
NTSTATUS RtlUnicodeStringToAnsiString(   
  __inout  PANSI_STRING DestinationString, //需要被转换的字符串   
  __in     PCUNICODE_STRING SourceString, //需要转换的源字符串   
  __in     BOOLEAN AllocateDestinationString //是否需要对被转换字符串分配内存   
);
  
将ANSI_STRING字符串转换成UNICODE_STRING字符串:   
  
NTSTATUS RtlAnsiStringToUnicodeString(   
  __inout  PUNICODE_STRING DestinationString, //需要被转换的字符串   
  __in     PCANSI_STRING SourceString, //需要转换的源字符串   
  __in     BOOLEAN AllocateDestinationString //是否需要对被转换字符串分配内存   
);

实例代码如下:   
  1. //将UNICODE_STRING字符串转换成ANSI_STRING字符串   
  2. UNICODE_STRING UnicodeString;   
  3. RtlInitUnicodeString(&UnicodeString, L"ASCE BOY");   
  4. ANSI_STRING AnsiString;   
  5. NTSTATUS nStatus = RtlUnicodeStringToAnsiString(&AnsiString,  &UnicodeString, TRUE);   
  6. if(NT_SUCCESS(nStatus))   
  7. {   
  8.          KdPrint(("Conver successfully\n"));   
  9.          KdPrint(("Result:%Z\n", &AnsiString));   
  10. }   
  11. else  
  12. {   
  13.          KdPrint(("Conver failed\n"));   
  14. }   
  15. //销毁AnsiString   
  16. RtlFreeAnsiString(&AnsiString);   
  17.   
  18.    
  19. //将ANSI_STRING字符串转换成UNICODE_STRING字符串   
  20. ANSI_STRING AnsiStringSec;   
  21. RtlInitString(&AnsiStringSec, "ASCE BOY");   
  22. UNICODE_STRING UnicodeStringSec;   
  23. nStatus = RtlAnsiStringToUnicodeString(&UnicodeStringSec,&AnsiStringSec, TRUE);   
  24. if(NT_SUCCESS(nStatus))   
  25. {   
  26.          KdPrint(("Conver successfully\n"));   
  27.          KdPrint(("Result: %wZ\n", &UnicodeStringSec));   
  28. }   
  29. else  
  30. {   
  31.          KdPrint(("Conver failed\n"));   
  32. }   

  33. //销毁UnicodeStringSec   
  34. RtlFreeUnicodeString(&UnicodeStringSec);  
复制代码

点击按钮快速添加回复内容: 支持 高兴 激动 给力 加油 苦寻 生气 回帖 路过 感恩
您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

小黑屋|手机版|Archiver|看流星社区 |网站地图

GMT+8, 2024-3-19 18:01

Powered by Kanliuxing X3.4

© 2010-2019 kanliuxing.com

快速回复 返回顶部 返回列表