Leaked source code of windows server 2003
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.

351 lines
6.0 KiB

  1. //
  2. // REGMISC.C
  3. //
  4. // Copyright (C) Microsoft Corporation, 1995-1996
  5. //
  6. #include "pch.h"
  7. #include "mbstring.h"
  8. // We MUST calculate the hash consistently between the 16-bit and 32-bit
  9. // versions of the registry.
  10. #define ToUpperHash(ch) ((int)(((ch>='a')&&(ch<='z'))?(ch-'a'+'A'):ch))
  11. #if 0
  12. //
  13. // RgChecksum
  14. //
  15. DWORD
  16. INTERNAL
  17. RgChecksum(
  18. LPVOID lpBuffer,
  19. UINT ByteCount
  20. )
  21. {
  22. LPDWORD lpdwBuffer;
  23. DWORD Checksum;
  24. lpdwBuffer = (LPDWORD) lpBuffer;
  25. ByteCount >>= 2;
  26. Checksum = 0;
  27. while (ByteCount) {
  28. Checksum += *lpdwBuffer++;
  29. ByteCount--;
  30. }
  31. return Checksum;
  32. }
  33. #endif
  34. //
  35. // RgHashString
  36. //
  37. // Simple hash computation of a counted string. All characters less than 0x80
  38. // 0x80 and all DBCS characters are added up.
  39. //
  40. // We MUST calculate the hash consistently between the 16-bit and
  41. // 32-bit versions of the registry. We will ignore all extended
  42. // characters because we cannot uppercase the character in 16-bit
  43. // mode.
  44. //
  45. DWORD
  46. INTERNAL
  47. RgHashString(
  48. LPCSTR lpString,
  49. UINT Length
  50. )
  51. {
  52. DWORD Hash;
  53. UINT Byte;
  54. Hash = 0;
  55. while (Length) {
  56. Byte = *((LPBYTE) lpString)++;
  57. if (IsDBCSLeadByte((BYTE) Byte)) {
  58. Hash += Byte;
  59. Length--;
  60. Hash += *lpString++; // Note that this is a signed char!
  61. }
  62. else if (Byte < 0x80)
  63. Hash += ToUpperHash(Byte);
  64. Length--;
  65. }
  66. return Hash;
  67. }
  68. //
  69. // RgAtoW
  70. // Convert an ascii string to a WORD
  71. //
  72. WORD
  73. INTERNAL
  74. RgAtoW(
  75. LPCSTR lpDec
  76. )
  77. {
  78. WORD Dec;
  79. Dec = 0;
  80. while (*lpDec >= '0' && *lpDec <= '9') {
  81. Dec *= 10;
  82. Dec += *lpDec - '0';
  83. lpDec++;
  84. }
  85. return Dec;
  86. }
  87. //
  88. // RgWtoA
  89. // Convert a WORD to an ascii string
  90. //
  91. VOID
  92. INTERNAL
  93. RgWtoA(
  94. WORD Dec,
  95. LPSTR lpDec
  96. )
  97. {
  98. WORD Divisor;
  99. WORD Digit;
  100. BOOL fSignificant = FALSE;
  101. Divisor = 10000;
  102. if (Dec) {
  103. while (Divisor) {
  104. Digit = Dec / Divisor;
  105. Dec -= Digit * Divisor;
  106. if (Digit)
  107. fSignificant = TRUE;
  108. if (fSignificant)
  109. *lpDec++ = '0' + Digit;
  110. Divisor /= 10;
  111. }
  112. }
  113. else {
  114. *lpDec++ = '0';
  115. }
  116. *lpDec = '\0';
  117. }
  118. //
  119. // RgStrCmpNI
  120. //
  121. int
  122. INTERNAL
  123. RgStrCmpNI(
  124. LPCSTR lpString1,
  125. LPCSTR lpString2,
  126. UINT Length
  127. )
  128. {
  129. int Difference;
  130. while (Length) {
  131. if (IsDBCSLeadByte(*lpString1)) {
  132. Difference = _mbctoupper (_mbsnextc (lpString1)) - _mbctoupper (_mbsnextc (lpString2));
  133. if (Difference != 0)
  134. return Difference;
  135. lpString1+=2;
  136. lpString2+=2;
  137. if (Length < 2) {
  138. break;
  139. }
  140. Length -=2;
  141. }
  142. else {
  143. if ((Difference = (int) ToUpper(*lpString1) -
  144. (int) ToUpper(*lpString2)) != 0)
  145. return Difference;
  146. lpString1++;
  147. lpString2++;
  148. Length--;
  149. }
  150. }
  151. return 0;
  152. }
  153. //
  154. // RgCopyFileBytes
  155. //
  156. // Copies the specified number of bytes from the source to the destination
  157. // starting at the specified offsets in each file.
  158. //
  159. int
  160. INTERNAL
  161. RgCopyFileBytes(
  162. HFILE hSourceFile,
  163. LONG SourceOffset,
  164. HFILE hDestinationFile,
  165. LONG DestinationOffset,
  166. DWORD cbSize
  167. )
  168. {
  169. int ErrorCode;
  170. LPVOID lpWorkBuffer;
  171. UINT cbBytesThisPass;
  172. ASSERT(hSourceFile != HFILE_ERROR);
  173. ASSERT(hDestinationFile != HFILE_ERROR);
  174. ErrorCode = ERROR_REGISTRY_IO_FAILED; // Assume this error code
  175. lpWorkBuffer = RgLockWorkBuffer();
  176. if (!RgSeekFile(hSourceFile, SourceOffset))
  177. goto ErrorUnlockWorkBuffer;
  178. if (!RgSeekFile(hDestinationFile, DestinationOffset))
  179. goto ErrorUnlockWorkBuffer;
  180. while (cbSize) {
  181. cbBytesThisPass = (UINT) ((DWORD) min(cbSize, SIZEOF_WORK_BUFFER));
  182. if (!RgReadFile(hSourceFile, lpWorkBuffer, cbBytesThisPass)) {
  183. TRAP();
  184. goto ErrorUnlockWorkBuffer;
  185. }
  186. RgYield();
  187. if (!RgWriteFile(hDestinationFile, lpWorkBuffer, cbBytesThisPass)) {
  188. TRAP();
  189. goto ErrorUnlockWorkBuffer;
  190. }
  191. RgYield();
  192. cbSize -= cbBytesThisPass;
  193. }
  194. ErrorCode = ERROR_SUCCESS;
  195. ErrorUnlockWorkBuffer:
  196. RgUnlockWorkBuffer(lpWorkBuffer);
  197. return ErrorCode;
  198. }
  199. #ifdef WANT_HIVE_SUPPORT
  200. //
  201. // RgGenerateAltFileName
  202. //
  203. BOOL
  204. INTERNAL
  205. RgGenerateAltFileName(
  206. LPCSTR lpFileName,
  207. LPSTR lpAltFileName,
  208. char ExtensionChar
  209. )
  210. {
  211. LPSTR lpString;
  212. StrCpy(lpAltFileName, lpFileName);
  213. lpString = lpAltFileName + StrLen(lpAltFileName) - 3;
  214. *lpString++ = '~';
  215. *lpString++ = '~';
  216. *lpString = ExtensionChar;
  217. return TRUE;
  218. }
  219. #endif
  220. #ifdef VXD
  221. #pragma VxD_RARE_CODE_SEG
  222. //
  223. // RgCopyFile
  224. //
  225. int
  226. INTERNAL
  227. RgCopyFile(
  228. LPCSTR lpSourceFile,
  229. LPCSTR lpDestinationFile
  230. )
  231. {
  232. int ErrorCode;
  233. HFILE hSourceFile;
  234. HFILE hDestinationFile;
  235. DWORD FileSize;
  236. ErrorCode = ERROR_REGISTRY_IO_FAILED; // Assume this error code
  237. if ((hSourceFile = RgOpenFile(lpSourceFile, OF_READ)) != HFILE_ERROR) {
  238. if ((FileSize = RgGetFileSize(hSourceFile)) != (DWORD) -1) {
  239. if ((hDestinationFile = RgCreateFile(lpDestinationFile)) !=
  240. HFILE_ERROR) {
  241. ErrorCode = RgCopyFileBytes(hSourceFile, 0, hDestinationFile, 0,
  242. FileSize);
  243. RgCloseFile(hDestinationFile);
  244. if (ErrorCode != ERROR_SUCCESS)
  245. RgDeleteFile(lpDestinationFile);
  246. }
  247. }
  248. RgCloseFile(hSourceFile);
  249. }
  250. return ErrorCode;
  251. }
  252. #endif // VXD