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.

212 lines
4.3 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1996 - 1999
  6. //
  7. // File: guid.cpp
  8. //
  9. // Contents: Microsoft Internet Security Guid functions
  10. //
  11. // Functions: wstr2guid
  12. // guid2wstr
  13. //
  14. // History: 05-Sep-1997 pberkman created
  15. //
  16. //--------------------------------------------------------------------------
  17. #include "windows.h"
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <assert.h>
  21. #include <malloc.h>
  22. #include "crtem.h"
  23. #include "unicode.h"
  24. static const BYTE GuidMap[] = { 3, 2, 1, 0, '-', 5, 4, '-', 7, 6, '-',
  25. 8, 9, '-', 10, 11, 12, 13, 14, 15 };
  26. static const WCHAR wszDigits[] = L"0123456789ABCDEF";
  27. static BOOL HexStringToDword(LPCWSTR FAR& pwsz, DWORD FAR& Value,
  28. int cDigits, WCHAR chDelim)
  29. {
  30. int Count;
  31. Value = 0;
  32. for (Count = 0; Count < cDigits; Count++, pwsz++)
  33. {
  34. if (*pwsz >= '0' && *pwsz <= '9')
  35. {
  36. Value = (Value << 4) + *pwsz - '0';
  37. }
  38. else if (*pwsz >= 'A' && *pwsz <= 'F')
  39. {
  40. Value = (Value << 4) + *pwsz - 'A' + 10;
  41. }
  42. else if (*pwsz >= 'a' && *pwsz <= 'f')
  43. {
  44. Value = (Value << 4) + *pwsz - 'a' + 10;
  45. }
  46. else
  47. {
  48. return(FALSE);
  49. }
  50. }
  51. if (chDelim != 0)
  52. {
  53. return *pwsz++ == chDelim;
  54. }
  55. else
  56. {
  57. return(TRUE);
  58. }
  59. }
  60. BOOL WINAPI wstr2guid(const WCHAR *pwszIn, GUID *pgOut)
  61. {
  62. WCHAR wsz[9];
  63. if (!(pwszIn) || !(pgOut))
  64. {
  65. SetLastError(ERROR_INVALID_PARAMETER);
  66. return(FALSE);
  67. }
  68. if (*pwszIn != '{') // only support v2 guids!
  69. {
  70. SetLastError(ERROR_INVALID_PARAMETER);
  71. return(FALSE);
  72. }
  73. if (wcslen(pwszIn) != 38)
  74. {
  75. SetLastError(ERROR_INVALID_PARAMETER);
  76. return(FALSE);
  77. }
  78. WCHAR *pwsz;
  79. pwsz = (WCHAR *)&pwszIn[1]; // pass the first {
  80. memset(pgOut, 0x00, sizeof(GUID));
  81. DWORD dw;
  82. if (!(HexStringToDword(pwsz, pgOut->Data1, sizeof(DWORD)*2, '-')))
  83. {
  84. return(FALSE);
  85. }
  86. if (!(HexStringToDword(pwsz, dw, sizeof(WORD)*2, '-')))
  87. {
  88. return(FALSE);
  89. }
  90. pgOut->Data2 = (WORD)dw;
  91. if (!(HexStringToDword(pwsz, dw, sizeof(WORD)*2, '-')))
  92. {
  93. return(FALSE);
  94. }
  95. pgOut->Data3 = (WORD)dw;
  96. if (!(HexStringToDword(pwsz, dw, sizeof(BYTE)*2, 0)))
  97. {
  98. return(FALSE);
  99. }
  100. pgOut->Data4[0] = (BYTE)dw;
  101. if (!(HexStringToDword(pwsz, dw, sizeof(BYTE)*2, '-')))
  102. {
  103. return(FALSE);
  104. }
  105. pgOut->Data4[1] = (BYTE)dw;
  106. if (!(HexStringToDword(pwsz, dw, sizeof(BYTE)*2, 0)))
  107. {
  108. return(FALSE);
  109. }
  110. pgOut->Data4[2] = (BYTE)dw;
  111. if (!(HexStringToDword(pwsz, dw, sizeof(BYTE)*2, 0)))
  112. {
  113. return(FALSE);
  114. }
  115. pgOut->Data4[3] = (BYTE)dw;
  116. if (!(HexStringToDword(pwsz, dw, sizeof(BYTE)*2, 0)))
  117. {
  118. return(FALSE);
  119. }
  120. pgOut->Data4[4] = (BYTE)dw;
  121. if (!(HexStringToDword(pwsz, dw, sizeof(BYTE)*2, 0)))
  122. {
  123. return(FALSE);
  124. }
  125. pgOut->Data4[5] = (BYTE)dw;
  126. if (!(HexStringToDword(pwsz, dw, sizeof(BYTE)*2, 0)))
  127. {
  128. return(FALSE);
  129. }
  130. pgOut->Data4[6] = (BYTE)dw;
  131. if (!(HexStringToDword(pwsz, dw, sizeof(BYTE)*2, 0)))
  132. {
  133. return(FALSE);
  134. }
  135. pgOut->Data4[7] = (BYTE)dw;
  136. return(TRUE);
  137. }
  138. BOOL WINAPI guid2wstr(const GUID *pgIn, WCHAR *pwszOut)
  139. {
  140. if (!(pwszOut) || !(pgIn))
  141. {
  142. SetLastError(ERROR_INVALID_PARAMETER);
  143. return(FALSE);
  144. }
  145. const BYTE *pBytes;
  146. int i;
  147. LPWSTR p;
  148. p = pwszOut;
  149. pBytes = (const BYTE *)pgIn;
  150. *p++ = L'{';
  151. for (i = 0; i < sizeof(GuidMap); i++)
  152. {
  153. if (GuidMap[i] == '-')
  154. {
  155. *p++ = L'-';
  156. }
  157. else
  158. {
  159. *p++ = wszDigits[ (pBytes[GuidMap[i]] & 0xF0) >> 4 ];
  160. *p++ = wszDigits[ (pBytes[GuidMap[i]] & 0x0F) ];
  161. }
  162. }
  163. *p++ = L'}';
  164. *p = L'\0';
  165. return(TRUE);
  166. }