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.

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