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.

811 lines
21 KiB

  1. #include <precomp.h>
  2. //
  3. // misc.c
  4. //
  5. // Author: Tom McGuire (tommcg) 2/97 - 12/97
  6. //
  7. // Copyright (C) Microsoft, 1997-1998.
  8. //
  9. // MICROSOFT CONFIDENTIAL
  10. //
  11. typedef struct _SUBALLOCATOR SUBALLOCATOR, *PSUBALLOCATOR;
  12. struct _SUBALLOCATOR {
  13. PVOID VirtualListTerminator;
  14. PVOID *VirtualList;
  15. PCHAR NextAvailable;
  16. PCHAR LastAvailable;
  17. ULONG GrowSize;
  18. };
  19. const ULONG gCrcTable32[ 256 ] = {
  20. 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F,
  21. 0xE963A535, 0x9E6495A3, 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988,
  22. 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, 0x1DB71064, 0x6AB020F2,
  23. 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
  24. 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9,
  25. 0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172,
  26. 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 0x35B5A8FA, 0x42B2986C,
  27. 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59,
  28. 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423,
  29. 0xCFBA9599, 0xB8BDA50F, 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924,
  30. 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, 0x76DC4190, 0x01DB7106,
  31. 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433,
  32. 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D,
  33. 0x91646C97, 0xE6635C01, 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E,
  34. 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x65B0D9C6, 0x12B7E950,
  35. 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65,
  36. 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7,
  37. 0xA4D1C46D, 0xD3D6F4FB, 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0,
  38. 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, 0x5005713C, 0x270241AA,
  39. 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
  40. 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81,
  41. 0xB7BD5C3B, 0xC0BA6CAD, 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A,
  42. 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, 0xE3630B12, 0x94643B84,
  43. 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1,
  44. 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB,
  45. 0x196C3671, 0x6E6B06E7, 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC,
  46. 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 0xD6D6A3E8, 0xA1D1937E,
  47. 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
  48. 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55,
  49. 0x316E8EEF, 0x4669BE79, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236,
  50. 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, 0xC5BA3BBE, 0xB2BD0B28,
  51. 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D,
  52. 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F,
  53. 0x72076785, 0x05005713, 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38,
  54. 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 0x86D3D2D4, 0xF1D4E242,
  55. 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777,
  56. 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69,
  57. 0x616BFFD3, 0x166CCF45, 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2,
  58. 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, 0xAED16A4A, 0xD9D65ADC,
  59. 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
  60. 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693,
  61. 0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94,
  62. 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D };
  63. #if defined( DEBUG ) || defined( DBG ) || defined( TESTCODE )
  64. BOOL
  65. Assert(
  66. LPCSTR szText,
  67. LPCSTR szFile,
  68. DWORD dwLine
  69. )
  70. {
  71. CHAR Buffer[ 512 ];
  72. wsprintf( Buffer, "ASSERT( %s ) FAILED, %s (%d)\n", szText, szFile, dwLine );
  73. OutputDebugString( Buffer );
  74. DebugBreak();
  75. return FALSE;
  76. }
  77. #endif
  78. #ifdef DONTCOMPILE // Not currently being used
  79. VOID
  80. InitializeCrc32Table(
  81. VOID
  82. )
  83. {
  84. ULONG i, j, Value;
  85. for ( i = 0; i < 256; i++ ) {
  86. for ( Value = i, j = 8; j > 0; j-- ) {
  87. if ( Value & 1 ) {
  88. Value = ( Value >> 1 ) ^ 0xEDB88320;
  89. }
  90. else {
  91. Value >>= 1;
  92. }
  93. }
  94. gCrcTable32[ i ] = Value;
  95. }
  96. }
  97. #endif // DONTCOMPILE
  98. #ifdef _M_IX86
  99. #pragma warning( disable: 4035 ) // no return value
  100. #endif
  101. ULONG
  102. Crc32(
  103. IN ULONG InitialCrc,
  104. IN PVOID Buffer,
  105. IN ULONG ByteCount
  106. )
  107. {
  108. #ifdef DONTCOMPILE // Not currently being used
  109. //
  110. // First determine if the CRC table has been initialized by checking
  111. // that the last value in it is nonzero. Believe it or not, this is
  112. // thread safe because two threads could initialize the table at the
  113. // same time with no harm, and the last value to be initialized in the
  114. // table is used to determine if the table has been initialized. On
  115. // all hardware platforms (including Alpha) it is safe to assume that
  116. // an aligned DWORD written to memory by one processor will be seen by
  117. // the other processor(s) as either containing the value previously
  118. // contained in that memory location, or the new written value, but not
  119. // some weird unpredictable value.
  120. //
  121. if ( gCrcTable32[ 255 ] == 0 ) {
  122. InitializeCrc32Table();
  123. }
  124. #endif // DONTCOMPILE
  125. #ifdef _M_IX86
  126. __asm {
  127. mov ecx, ByteCount ; number of bytes in buffer
  128. xor ebx, ebx ; ebx (bl) will be our table index
  129. mov esi, Buffer ; buffer pointer
  130. test ecx, ecx ; test for zero length buffer
  131. mov eax, InitialCrc ; CRC-32 value
  132. jnz short loopentry ; if non-zero buffer, start loop
  133. jmp short exitfunc ; else exit (crc already in eax)
  134. looptop: shr eax, 8 ; (crc>>8) (U1)
  135. mov edx, gCrcTable32[ebx*4] ; fetch Table[ index ] (V1)
  136. xor eax, edx ; crc=(crc>>8)^Table[index] (U1)
  137. loopentry: mov bl, [esi] ; fetch next *buffer (V1)
  138. inc esi ; buffer++ (U1)
  139. xor bl, al ; index=(byte)crc^*buffer (V1)
  140. dec ecx ; adjust counter (U1)
  141. jnz short looptop ; loop while nBytes (V1)
  142. shr eax, 8 ; remaining math on last byte
  143. xor eax, gCrcTable32[ebx*4] ; eax returns new crc value
  144. exitfunc:
  145. }
  146. #else // ! _M_IX86
  147. {
  148. ULONG Value = InitialCrc;
  149. ULONG Count = ByteCount;
  150. PUCHAR p = Buffer;
  151. while ( Count-- ) {
  152. Value = ( Value >> 8 ) ^ gCrcTable32[ (UCHAR)( *p++ ^ Value ) ];
  153. }
  154. return Value;
  155. }
  156. #endif // ! _M_IX86
  157. }
  158. #ifdef _M_IX86
  159. #pragma warning( default: 4035 ) // no return value
  160. #endif
  161. BOOL
  162. SafeCompleteCrc32(
  163. IN PVOID Buffer,
  164. IN ULONG ByteCount,
  165. OUT PULONG CrcValue
  166. )
  167. {
  168. BOOL Success = TRUE;
  169. __try {
  170. *CrcValue = Crc32( 0xFFFFFFFF, Buffer, ByteCount ) ^ 0xFFFFFFFF;
  171. }
  172. __except( EXCEPTION_EXECUTE_HANDLER ) {
  173. SetLastError( GetExceptionCode() );
  174. Success = FALSE;
  175. }
  176. return Success;
  177. }
  178. BOOL
  179. SafeCompleteMD5(
  180. IN PVOID Buffer,
  181. IN ULONG ByteCount,
  182. OUT PMD5_HASH MD5Value
  183. )
  184. {
  185. BOOL Success = TRUE;
  186. __try {
  187. ComputeCompleteMD5( Buffer, ByteCount, MD5Value );
  188. }
  189. __except( EXCEPTION_EXECUTE_HANDLER ) {
  190. SetLastError( GetExceptionCode() );
  191. Success = FALSE;
  192. }
  193. return Success;
  194. }
  195. BOOL
  196. MyMapViewOfFileA(
  197. IN LPCSTR FileName,
  198. OUT ULONG *FileSize,
  199. OUT HANDLE *FileHandle,
  200. OUT PVOID *MapBase
  201. )
  202. {
  203. HANDLE InternalFileHandle;
  204. BOOL Success;
  205. InternalFileHandle = CreateFileA(
  206. FileName,
  207. GENERIC_READ,
  208. FILE_SHARE_READ | FILE_SHARE_WRITE,
  209. NULL,
  210. OPEN_EXISTING,
  211. FILE_FLAG_SEQUENTIAL_SCAN,
  212. NULL
  213. );
  214. if ( InternalFileHandle != INVALID_HANDLE_VALUE ) {
  215. Success = MyMapViewOfFileByHandle(
  216. InternalFileHandle,
  217. FileSize,
  218. MapBase
  219. );
  220. if ( Success ) {
  221. *FileHandle = InternalFileHandle;
  222. return TRUE;
  223. }
  224. CloseHandle( InternalFileHandle );
  225. }
  226. return FALSE;
  227. }
  228. BOOL
  229. MyMapViewOfFileByHandle(
  230. IN HANDLE FileHandle,
  231. OUT ULONG *FileSize,
  232. OUT PVOID *MapBase
  233. )
  234. {
  235. ULONG InternalFileSize;
  236. ULONG InternalFileSizeHigh;
  237. HANDLE InternalMapHandle;
  238. PVOID InternalMapBase;
  239. InternalFileSize = GetFileSize( FileHandle, &InternalFileSizeHigh );
  240. if ( InternalFileSizeHigh != 0 ) {
  241. SetLastError( ERROR_OUTOFMEMORY );
  242. return FALSE;
  243. }
  244. if ( InternalFileSize == 0 ) {
  245. *MapBase = NULL;
  246. *FileSize = 0;
  247. return TRUE;
  248. }
  249. if ( InternalFileSize != 0xFFFFFFFF ) {
  250. InternalMapHandle = CreateFileMapping(
  251. FileHandle,
  252. NULL,
  253. PAGE_WRITECOPY,
  254. 0,
  255. 0,
  256. NULL
  257. );
  258. if ( InternalMapHandle != NULL ) {
  259. InternalMapBase = MapViewOfFile(
  260. InternalMapHandle,
  261. FILE_MAP_COPY,
  262. 0,
  263. 0,
  264. 0
  265. );
  266. CloseHandle( InternalMapHandle );
  267. if ( InternalMapBase != NULL ) {
  268. *MapBase = InternalMapBase;
  269. *FileSize = InternalFileSize;
  270. return TRUE;
  271. }
  272. }
  273. }
  274. return FALSE;
  275. }
  276. BOOL
  277. MyCreateMappedFileA(
  278. IN LPCSTR FileName,
  279. IN ULONG InitialSize,
  280. OUT HANDLE *FileHandle,
  281. OUT PVOID *MapBase
  282. )
  283. {
  284. HANDLE InternalFileHandle;
  285. BOOL Success;
  286. InternalFileHandle = CreateFileA(
  287. FileName,
  288. GENERIC_READ | GENERIC_WRITE,
  289. FILE_SHARE_READ,
  290. NULL,
  291. CREATE_ALWAYS,
  292. FILE_ATTRIBUTE_NORMAL,
  293. NULL
  294. );
  295. if ( InternalFileHandle != INVALID_HANDLE_VALUE ) {
  296. Success = MyCreateMappedFileByHandle(
  297. InternalFileHandle,
  298. InitialSize,
  299. MapBase
  300. );
  301. if ( Success ) {
  302. *FileHandle = InternalFileHandle;
  303. return TRUE;
  304. }
  305. CloseHandle( InternalFileHandle );
  306. }
  307. return FALSE;
  308. }
  309. BOOL
  310. MyCreateMappedFileByHandle(
  311. IN HANDLE FileHandle,
  312. IN ULONG InitialSize,
  313. OUT PVOID *MapBase
  314. )
  315. {
  316. HANDLE InternalMapHandle;
  317. PVOID InternalMapBase;
  318. InternalMapHandle = CreateFileMapping(
  319. FileHandle,
  320. NULL,
  321. PAGE_READWRITE,
  322. 0,
  323. InitialSize,
  324. NULL
  325. );
  326. if ( InternalMapHandle != NULL ) {
  327. InternalMapBase = MapViewOfFile(
  328. InternalMapHandle,
  329. FILE_MAP_WRITE,
  330. 0,
  331. 0,
  332. 0
  333. );
  334. CloseHandle( InternalMapHandle );
  335. if ( InternalMapBase != NULL ) {
  336. *MapBase = InternalMapBase;
  337. return TRUE;
  338. }
  339. }
  340. return FALSE;
  341. }
  342. VOID
  343. MyUnmapCreatedMappedFile(
  344. IN HANDLE FileHandle,
  345. IN PVOID MapBase,
  346. IN ULONG FileSize,
  347. IN PFILETIME FileTime OPTIONAL
  348. )
  349. {
  350. FlushViewOfFile( MapBase, 0 );
  351. UnmapViewOfFile( MapBase );
  352. SetFilePointer( FileHandle, (LONG) FileSize, NULL, FILE_BEGIN );
  353. SetEndOfFile( FileHandle );
  354. SetFileTime( FileHandle, NULL, NULL, FileTime );
  355. }
  356. PVOID
  357. __fastcall
  358. MyVirtualAlloc(
  359. ULONG Size
  360. )
  361. {
  362. return VirtualAlloc( NULL, Size, MEM_COMMIT, PAGE_READWRITE );
  363. }
  364. VOID
  365. __fastcall
  366. MyVirtualFree(
  367. PVOID Allocation
  368. )
  369. {
  370. VirtualFree( Allocation, 0, MEM_RELEASE );
  371. }
  372. HANDLE
  373. CreateSubAllocator(
  374. IN ULONG InitialCommitSize,
  375. IN ULONG GrowthCommitSize
  376. )
  377. {
  378. PSUBALLOCATOR SubAllocator;
  379. ULONG InitialSize;
  380. ULONG GrowthSize;
  381. InitialSize = ROUNDUP2( InitialCommitSize, MINIMUM_VM_ALLOCATION );
  382. GrowthSize = ROUNDUP2( GrowthCommitSize, MINIMUM_VM_ALLOCATION );
  383. SubAllocator = MyVirtualAlloc( InitialSize );
  384. //
  385. // If can't allocate entire initial size, back off to minimum size.
  386. // Very large initial requests sometimes cannot be allocated simply
  387. // because there is not enough contiguous address space.
  388. //
  389. if (( SubAllocator == NULL ) && ( InitialSize > GrowthSize )) {
  390. InitialSize = GrowthSize;
  391. SubAllocator = MyVirtualAlloc( InitialSize );
  392. }
  393. if (( SubAllocator == NULL ) && ( InitialSize > MINIMUM_VM_ALLOCATION )) {
  394. InitialSize = MINIMUM_VM_ALLOCATION;
  395. SubAllocator = MyVirtualAlloc( InitialSize );
  396. }
  397. if ( SubAllocator != NULL ) {
  398. SubAllocator->NextAvailable = (PCHAR)SubAllocator + ROUNDUP2( sizeof( SUBALLOCATOR ), SUBALLOCATOR_ALIGNMENT );
  399. SubAllocator->LastAvailable = (PCHAR)SubAllocator + InitialSize;
  400. SubAllocator->VirtualList = (PVOID*)SubAllocator;
  401. SubAllocator->GrowSize = GrowthSize;
  402. }
  403. return (HANDLE) SubAllocator;
  404. }
  405. PVOID
  406. __fastcall
  407. SubAllocate(
  408. IN HANDLE hAllocator,
  409. IN ULONG Size
  410. )
  411. {
  412. PSUBALLOCATOR SubAllocator = (PSUBALLOCATOR) hAllocator;
  413. PCHAR NewVirtual;
  414. PCHAR Allocation;
  415. ULONG AllocSize;
  416. ULONG Available;
  417. ULONG GrowSize;
  418. ASSERT( Size < (ULONG)( ~(( SUBALLOCATOR_ALIGNMENT * 2 ) - 1 )));
  419. AllocSize = ROUNDUP2( Size, SUBALLOCATOR_ALIGNMENT );
  420. Available = (ULONG)( SubAllocator->LastAvailable - SubAllocator->NextAvailable );
  421. if ( AllocSize <= Available ) {
  422. Allocation = SubAllocator->NextAvailable;
  423. SubAllocator->NextAvailable = Allocation + AllocSize;
  424. return Allocation;
  425. }
  426. //
  427. // Insufficient VM, so grow it. Make sure we grow it enough to satisfy
  428. // the allocation request in case the request is larger than the grow
  429. // size specified in CreateSubAllocator.
  430. //
  431. #ifdef TESTCODE
  432. printf( "\nGrowing VM suballocater\n" );
  433. #endif
  434. GrowSize = SubAllocator->GrowSize;
  435. if ( GrowSize < ( AllocSize + SUBALLOCATOR_ALIGNMENT )) {
  436. GrowSize = ROUNDUP2(( AllocSize + SUBALLOCATOR_ALIGNMENT ), MINIMUM_VM_ALLOCATION );
  437. }
  438. NewVirtual = MyVirtualAlloc( GrowSize );
  439. //
  440. // If failed to alloc GrowSize VM, and the allocation could be satisfied
  441. // with a minimum VM allocation, try allocating minimum VM to satisfy
  442. // this request.
  443. //
  444. if (( NewVirtual == NULL ) && ( AllocSize <= ( MINIMUM_VM_ALLOCATION - SUBALLOCATOR_ALIGNMENT ))) {
  445. GrowSize = MINIMUM_VM_ALLOCATION;
  446. NewVirtual = MyVirtualAlloc( GrowSize );
  447. }
  448. if ( NewVirtual != NULL ) {
  449. //
  450. // Set LastAvailable to end of new VM block.
  451. //
  452. SubAllocator->LastAvailable = NewVirtual + GrowSize;
  453. //
  454. // Link new VM into list of VM allocations.
  455. //
  456. *(PVOID*)NewVirtual = SubAllocator->VirtualList;
  457. SubAllocator->VirtualList = (PVOID*)NewVirtual;
  458. //
  459. // Requested allocation comes next.
  460. //
  461. Allocation = NewVirtual + SUBALLOCATOR_ALIGNMENT;
  462. //
  463. // Then set the NextAvailable for what's remaining.
  464. //
  465. SubAllocator->NextAvailable = Allocation + AllocSize;
  466. //
  467. // And return the allocation.
  468. //
  469. return Allocation;
  470. }
  471. //
  472. // Could not allocate enough VM to satisfy request.
  473. //
  474. return NULL;
  475. }
  476. VOID
  477. DestroySubAllocator(
  478. IN HANDLE hAllocator
  479. )
  480. {
  481. PSUBALLOCATOR SubAllocator = (PSUBALLOCATOR) hAllocator;
  482. PVOID VirtualBlock = SubAllocator->VirtualList;
  483. PVOID NextVirtualBlock;
  484. do {
  485. NextVirtualBlock = *(PVOID*)VirtualBlock;
  486. MyVirtualFree( VirtualBlock );
  487. VirtualBlock = NextVirtualBlock;
  488. }
  489. while ( VirtualBlock != NULL );
  490. }
  491. LPSTR
  492. __fastcall
  493. MySubAllocStrDup(
  494. IN HANDLE SubAllocator,
  495. IN LPCSTR String
  496. )
  497. {
  498. ULONG Length = (ULONG)strlen( String );
  499. LPSTR Buffer = SubAllocate( SubAllocator, Length + 1 );
  500. if ( Buffer ) {
  501. memcpy( Buffer, String, Length ); // no need to copy NULL terminator
  502. }
  503. return Buffer;
  504. }
  505. LPSTR
  506. MySubAllocStrDupAndCat(
  507. IN HANDLE SubAllocator,
  508. IN LPCSTR String1,
  509. IN LPCSTR String2,
  510. IN CHAR Separator
  511. )
  512. {
  513. ULONG Length1 = (ULONG)strlen( String1 );
  514. ULONG Length2 = (ULONG)strlen( String2 );
  515. LPSTR Buffer = SubAllocate( SubAllocator, Length1 + Length2 + 2 );
  516. if ( Buffer ) {
  517. memcpy( Buffer, String1, Length1 );
  518. if (( Separator != 0 ) && ( Length1 > 0 ) && ( Buffer[ Length1 - 1 ] != Separator )) {
  519. Buffer[ Length1++ ] = Separator;
  520. }
  521. memcpy( Buffer + Length1, String2, Length2 ); // no need to terminate
  522. }
  523. return Buffer;
  524. }
  525. VOID
  526. MyLowercase(
  527. IN OUT LPSTR String
  528. )
  529. {
  530. LPSTR p;
  531. for ( p = String; *p; p++ ) {
  532. if (( *p >= 'A' ) && ( *p <= 'Z' )) {
  533. *p |= 0x20;
  534. }
  535. }
  536. }
  537. #ifdef DONTCOMPILE // not used currently
  538. DWORD MyProcessHeap;
  539. PVOID
  540. MyHeapAllocZero(
  541. IN ULONG Size
  542. )
  543. {
  544. PVOID Allocation;
  545. if ( MyProcessHeap == NULL ) {
  546. MyProcessHeap = GetProcessHeap();
  547. }
  548. Allocation = HeapAlloc( MyProcessHeap, HEAP_ZERO_MEMORY, Size );
  549. if ( Allocation == NULL ) {
  550. SetLastError( ERROR_OUTOFMEMORY );
  551. }
  552. return Allocation;
  553. }
  554. VOID
  555. MyHeapFree(
  556. IN PVOID Allocation
  557. )
  558. {
  559. HeapFree( MyProcessHeap, 0, Allocation );
  560. }
  561. #endif // DONTCOMPILE
  562. ULONG
  563. HashName(
  564. IN LPCSTR Name
  565. )
  566. {
  567. ULONG Length = (ULONG)strlen( Name );
  568. ULONG Hash = ~Length;
  569. while ( Length-- ) {
  570. Hash = _rotl( Hash, 3 ) ^ *Name++;
  571. }
  572. return Hash;
  573. }
  574. ULONG
  575. HashNameCaseInsensitive(
  576. IN LPCSTR Name
  577. )
  578. {
  579. ULONG Length = (ULONG)strlen( Name );
  580. ULONG Hash = ~Length;
  581. while ( Length-- ) {
  582. Hash = _rotl( Hash, 3 ) ^ ( *Name++ & 0xDF ); // mask case bit
  583. }
  584. return Hash;
  585. }
  586. UCHAR
  587. __inline
  588. LowNibbleToHexChar(
  589. ULONG Value
  590. )
  591. {
  592. return "0123456789abcdef"[ Value & 0x0000000F ];
  593. }
  594. VOID
  595. DwordToHexString(
  596. IN DWORD Value,
  597. OUT LPSTR Buffer // writes exactly 9 bytes including terminator
  598. )
  599. {
  600. ULONG i;
  601. Buffer[ 8 ] = 0;
  602. i = 8;
  603. do {
  604. Buffer[ --i ] = LowNibbleToHexChar( Value );
  605. Value >>= 4;
  606. }
  607. while ( i != 0 );
  608. }
  609. BOOL
  610. HashToHexString(
  611. IN PMD5_HASH HashValue,
  612. OUT LPSTR Buffer // must be at least 33 bytes
  613. )
  614. {
  615. ULONG i;
  616. for ( i = 0; i < sizeof( MD5_HASH ); i++ ) {
  617. *Buffer++ = LowNibbleToHexChar( HashValue->Byte[ i ] >> 4 );
  618. *Buffer++ = LowNibbleToHexChar( HashValue->Byte[ i ] );
  619. }
  620. *Buffer = 0;
  621. return TRUE;
  622. }