Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

318 lines
6.1 KiB

  1. /*++
  2. Copyright (c) 1992 Microsoft Corporation
  3. Module Name:
  4. cmname.c
  5. Abstract:
  6. Provides routines for handling name comparisons and converting to/from the registry
  7. compressed name format.
  8. Author:
  9. John Vert (jvert) 28-Oct-1993
  10. Revision History:
  11. --*/
  12. #include "cmp.h"
  13. #ifdef ALLOC_PRAGMA
  14. #pragma alloc_text(PAGE,CmpNameSize)
  15. #pragma alloc_text(PAGE,CmpCopyName)
  16. #pragma alloc_text(PAGE,CmpCompressedNameSize)
  17. #pragma alloc_text(PAGE,CmpCopyCompressedName)
  18. #pragma alloc_text(PAGE,CmpCompareCompressedName)
  19. #pragma alloc_text(PAGE,CmpCompareUnicodeString)
  20. #endif
  21. USHORT
  22. CmpNameSize(
  23. IN PHHIVE Hive,
  24. IN PUNICODE_STRING Name
  25. )
  26. /*++
  27. Routine Description:
  28. Determines the space needed to store a given string in the registry. May apply
  29. any relevant compression to compute the length, but the compression used is
  30. guaranteed to be the same as CmpCopyName.
  31. Arguments:
  32. Hive - supplies the hive control structure (for version checking)
  33. Name - Supplies the unicode string to be copied into the registry.
  34. Return Value:
  35. The number of bytes of storage required to store this name.
  36. --*/
  37. {
  38. ULONG i;
  39. if (Hive->Version == 1) {
  40. return(Name->Length);
  41. }
  42. for (i=0;i<Name->Length/sizeof(WCHAR);i++) {
  43. if ((USHORT)Name->Buffer[i] > (UCHAR)-1) {
  44. return(Name->Length);
  45. }
  46. }
  47. return(Name->Length / sizeof(WCHAR));
  48. }
  49. USHORT
  50. CmpCopyName(
  51. IN PHHIVE Hive,
  52. IN PWCHAR Destination,
  53. IN PUNICODE_STRING Source
  54. )
  55. /*++
  56. Routine Description:
  57. Copies the given unicode name into the registry, applying any relevant compression
  58. at the same time.
  59. Arguments:
  60. Hive - supplies the hive control structure (For version checking)
  61. Destination - Supplies the destination of the given string.
  62. Source - Supplies the unicode string to copy into the registry.
  63. Return Value:
  64. Number of bytes of storage copied
  65. --*/
  66. {
  67. ULONG i;
  68. if (Hive->Version==1) {
  69. RtlCopyMemory(Destination,Source->Buffer, Source->Length);
  70. return(Source->Length);
  71. }
  72. for (i=0;i<Source->Length/sizeof(WCHAR);i++) {
  73. if ((USHORT)Source->Buffer[i] > (UCHAR)-1) {
  74. RtlCopyMemory(Destination,Source->Buffer, Source->Length);
  75. return(Source->Length);
  76. }
  77. ((PUCHAR)Destination)[i] = (UCHAR)(Source->Buffer[i]);
  78. }
  79. return(Source->Length / sizeof(WCHAR));
  80. }
  81. USHORT
  82. CmpCompressedNameSize(
  83. IN PWCHAR Name,
  84. IN ULONG Length
  85. )
  86. /*++
  87. Routine Description:
  88. Computes the length of the unicode string that the given compressed name
  89. expands into.
  90. Arguments:
  91. Name - Supplies the compressed name.
  92. Length - Supplies the length in bytes of the compressed name
  93. Return Value:
  94. The number of bytes of storage required to hold the Unicode expanded name.
  95. --*/
  96. {
  97. return((USHORT)Length*sizeof(WCHAR));
  98. }
  99. VOID
  100. CmpCopyCompressedName(
  101. IN PWCHAR Destination,
  102. IN ULONG DestinationLength,
  103. IN PWCHAR Source,
  104. IN ULONG SourceLength
  105. )
  106. /*++
  107. Routine Description:
  108. Copies a compressed name from the registry and expands it to Unicode.
  109. Arguments:
  110. Destination - Supplies the destination Unicode buffer
  111. DestinationLength - Supplies the max length of the destination buffer in bytes
  112. Source - Supplies the compressed string.
  113. SourceLength - Supplies the length of the compressed string in bytes
  114. Return Value:
  115. None.
  116. --*/
  117. {
  118. ULONG i;
  119. ULONG Chars;
  120. Chars = (DestinationLength/sizeof(WCHAR) < SourceLength)
  121. ? DestinationLength/sizeof(WCHAR)
  122. : SourceLength;
  123. for (i=0;i<Chars;i++) {
  124. Destination[i] = (WCHAR)(((PUCHAR)Source)[i]);
  125. }
  126. }
  127. LONG
  128. CmpCompareCompressedName(
  129. IN PUNICODE_STRING SearchName,
  130. IN PWCHAR CompressedName,
  131. IN ULONG NameLength,
  132. IN ULONG CompareFlags
  133. )
  134. /*++
  135. Routine Description:
  136. Compares a compressed registry string to a Unicode string. Does a case-insensitive
  137. comparison.
  138. Arguments:
  139. SearchName - Supplies the Unicode string to be compared
  140. CompressedName - Supplies the compressed string to be compared
  141. NameLength - Supplies the length of the compressed string
  142. Return Value:
  143. 0 = SearchName == CompressedName (of Cell)
  144. < 0 = SearchName < CompressedName
  145. > 0 = SearchName > CompressedName
  146. --*/
  147. {
  148. WCHAR *s1;
  149. UCHAR *s2;
  150. USHORT n1, n2;
  151. WCHAR c1;
  152. WCHAR c2;
  153. LONG cDiff;
  154. s1 = SearchName->Buffer;
  155. s2 = (UCHAR *)CompressedName;
  156. n1 = (USHORT )(SearchName->Length / sizeof(WCHAR));
  157. n2 = (USHORT )(NameLength);
  158. while (n1 && n2) {
  159. c1 = *s1++;
  160. c2 = (WCHAR)(*s2++);
  161. c1 = (CompareFlags&CMP_SOURCE_UP)?c1:RtlUpcaseUnicodeChar(c1);
  162. c2 = (CompareFlags&CMP_DEST_UP)?c2:RtlUpcaseUnicodeChar(c2);
  163. if ((cDiff = ((LONG)c1 - (LONG)c2)) != 0) {
  164. return( cDiff );
  165. }
  166. n1--;
  167. n2--;
  168. }
  169. return( n1 - n2 );
  170. }
  171. LONG
  172. CmpCompareUnicodeString(
  173. IN PUNICODE_STRING SourceName,
  174. IN PUNICODE_STRING DestName,
  175. IN ULONG CompareFlags
  176. )
  177. /*++
  178. Routine Description:
  179. Compares 2 unicode strings; Case insensitive comparison.
  180. Uses flags to avoid UpCasing strings again.
  181. Arguments:
  182. SourceName - Supplies the Unicode string to be compared
  183. DestName - Supplies the compressed string to be compared
  184. CompareFlags - Supplies the flags to control comparison (see cmp.h)
  185. Return Value:
  186. 0 = SearchName == CompressedName (of Cell)
  187. < 0 = SearchName < CompressedName
  188. > 0 = SearchName > CompressedName
  189. --*/
  190. {
  191. WCHAR *s1, *s2;
  192. USHORT n1, n2;
  193. WCHAR c1, c2;
  194. LONG cDiff;
  195. s1 = SourceName->Buffer;
  196. s2 = DestName->Buffer;
  197. n1 = (USHORT )(SourceName->Length / sizeof(WCHAR));
  198. n2 = (USHORT )(DestName->Length / sizeof(WCHAR));
  199. while (n1 && n2) {
  200. c1 = *s1++;
  201. c2 = *s2++;
  202. c1 = (CompareFlags&CMP_SOURCE_UP)?c1:RtlUpcaseUnicodeChar(c1);
  203. c2 = (CompareFlags&CMP_DEST_UP)?c2:RtlUpcaseUnicodeChar(c2);
  204. if ((cDiff = ((LONG)c1 - (LONG)c2)) != 0) {
  205. return( cDiff );
  206. }
  207. n1--;
  208. n2--;
  209. }
  210. return( n1 - n2 );
  211. }