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.

253 lines
5.7 KiB

  1. /*++
  2. Copyright (c) 1989-2000 Microsoft Corporation
  3. Module Name:
  4. guid.c
  5. Abstract:
  6. GUID-related utilities that run on win9x and nt4 as
  7. well as win2k and whistler
  8. Author:
  9. vadimb created sometime in 2001
  10. Revision History:
  11. --*/
  12. #include "sdbp.h"
  13. #include "initguid.h"
  14. #if defined(KERNEL_MODE) && defined(ALLOC_DATA_PRAGMA)
  15. #pragma data_seg()
  16. #endif // KERNEL_MODE && ALLOC_DATA_PRAGMA
  17. const TCHAR g_szGuidFormat[] = TEXT("{%08lx-%04hx-%04hx-%02hx%02hx-%02hx%02hx%02hx%02hx%02hx%02hx}");
  18. #if defined(KERNEL_MODE) && defined(ALLOC_PRAGMA)
  19. #pragma alloc_text(PAGE, SdbpGUIDToUnicodeString)
  20. #pragma alloc_text(PAGE, SdbpFreeUnicodeString)
  21. #pragma alloc_text(PAGE, SdbGUIDToString)
  22. #endif // KERNEL_MODE && ALLOC_PRAGMA
  23. //
  24. // GUID string buffer size (in chars) not including the term null char
  25. //
  26. #define GUID_STRING_SIZE 38
  27. DEFINE_GUID(STATIC_NULL_GUID, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, \
  28. 0x0, 0x0, 0x0, 0x0);
  29. BOOL
  30. SDBAPI
  31. SdbIsNullGUID(
  32. IN GUID* pGuid
  33. )
  34. {
  35. return pGuid == NULL ||
  36. RtlEqualMemory(pGuid, &STATIC_NULL_GUID, sizeof(*pGuid));
  37. }
  38. #if defined(NT_MODE) || defined(KERNEL_MODE)
  39. BOOL
  40. SDBAPI
  41. SdbGUIDFromStringN(
  42. IN LPCWSTR pszGuid,
  43. IN size_t Length,
  44. OUT GUID* pGuid
  45. )
  46. {
  47. UNICODE_STRING ustrGuid;
  48. NTSTATUS Status;
  49. ustrGuid.Length =
  50. ustrGuid.MaximumLength = (USHORT)Length;
  51. ustrGuid.Buffer = (LPWSTR)pszGuid;
  52. Status = RtlGUIDFromString(&ustrGuid, pGuid);
  53. return NT_SUCCESS(Status);
  54. }
  55. BOOL
  56. SDBAPI
  57. SdbGUIDFromString(
  58. IN LPCWSTR lpszGuid,
  59. OUT GUID* pGuid
  60. )
  61. /*++
  62. Return: TRUE on success, FALSE otherwise.
  63. Desc: Converts a string to a GUID.
  64. --*/
  65. {
  66. UNICODE_STRING ustrGuid;
  67. NTSTATUS status;
  68. RtlInitUnicodeString(&ustrGuid, lpszGuid);
  69. status = RtlGUIDFromString(&ustrGuid, pGuid);
  70. return NT_SUCCESS(status);
  71. }
  72. #else // we do the same thing for both WIN32A and WIN32U
  73. BOOL
  74. SDBAPI
  75. SdbGUIDFromStringN(
  76. IN LPCTSTR pszGuid,
  77. IN size_t Length,
  78. OUT GUID* pGuid
  79. )
  80. {
  81. TCHAR szGuid[64]; // more than enough
  82. StringCchCopyN(szGuid, CHARCOUNT(szGuid), pszGuid, Length);
  83. return SdbGUIDFromString(szGuid, pGuid);
  84. }
  85. BOOL
  86. SdbGUIDFromString(
  87. LPCTSTR lpszGuid,
  88. GUID* pGuid)
  89. {
  90. int nFields;
  91. DWORD rgData4[8];
  92. DWORD dwData2;
  93. DWORD dwData3;
  94. INT i;
  95. nFields = _stscanf(lpszGuid, g_szGuidFormat,
  96. &pGuid->Data1, // type : long
  97. &dwData2, // type : short
  98. &dwData3, // type : short
  99. &rgData4[0],// type : short all the way to the bottom
  100. &rgData4[1],
  101. &rgData4[2],
  102. &rgData4[3],
  103. &rgData4[4],
  104. &rgData4[5],
  105. &rgData4[6],
  106. &rgData4[7]);
  107. if (nFields == 11) {
  108. pGuid->Data2 = (USHORT)dwData2;
  109. pGuid->Data3 = (USHORT)dwData3;
  110. for (i = 0; i < 8; ++i) {
  111. pGuid->Data4[i] = (BYTE)rgData4[i];
  112. }
  113. }
  114. return (nFields == 11);
  115. }
  116. #endif
  117. #ifndef WIN32A_MODE
  118. //
  119. // Private Functions used internally
  120. //
  121. NTSTATUS
  122. SdbpGUIDToUnicodeString(
  123. IN GUID* pGuid,
  124. OUT PUNICODE_STRING pUnicodeString
  125. )
  126. /*++
  127. Return: BUGBUG: ?
  128. Desc: BUGBUG: ?
  129. --*/
  130. {
  131. pUnicodeString->Length = GUID_STRING_SIZE * sizeof(WCHAR);
  132. pUnicodeString->MaximumLength = pUnicodeString->Length + sizeof(UNICODE_NULL);
  133. pUnicodeString->Buffer = SdbAlloc(pUnicodeString->MaximumLength);
  134. if (pUnicodeString->Buffer == NULL) {
  135. DBGPRINT((sdlError,
  136. "SdbpGUIDToUnicodeString",
  137. "Failed to allocate %ld bytes for GUID\n",
  138. (DWORD)pUnicodeString->MaximumLength));
  139. return STATUS_NO_MEMORY;
  140. }
  141. StringCchPrintf(pUnicodeString->Buffer,
  142. pUnicodeString->MaximumLength / sizeof(WCHAR),
  143. g_szGuidFormat,
  144. pGuid->Data1,
  145. pGuid->Data2,
  146. pGuid->Data3,
  147. pGuid->Data4[0],
  148. pGuid->Data4[1],
  149. pGuid->Data4[2],
  150. pGuid->Data4[3],
  151. pGuid->Data4[4],
  152. pGuid->Data4[5],
  153. pGuid->Data4[6],
  154. pGuid->Data4[7]);
  155. return STATUS_SUCCESS;
  156. }
  157. VOID
  158. SdbpFreeUnicodeString(
  159. PUNICODE_STRING pUnicodeString
  160. )
  161. /*++
  162. Return: BUGBUG: ?
  163. Desc: BUGBUG: ?
  164. --*/
  165. {
  166. if (pUnicodeString->Buffer != NULL) {
  167. SdbFree(pUnicodeString->Buffer);
  168. RtlZeroMemory(pUnicodeString, sizeof(*pUnicodeString));
  169. }
  170. }
  171. #endif // WIN32A_MODE
  172. BOOL
  173. SDBAPI
  174. SdbGUIDToString(
  175. IN GUID* pGuid,
  176. OUT LPTSTR pszGuid,
  177. IN DWORD cchSize
  178. )
  179. {
  180. HRESULT hr;
  181. hr = StringCchPrintf(pszGuid,
  182. cchSize,
  183. g_szGuidFormat,
  184. pGuid->Data1,
  185. pGuid->Data2,
  186. pGuid->Data3,
  187. pGuid->Data4[0],
  188. pGuid->Data4[1],
  189. pGuid->Data4[2],
  190. pGuid->Data4[3],
  191. pGuid->Data4[4],
  192. pGuid->Data4[5],
  193. pGuid->Data4[6],
  194. pGuid->Data4[7]);
  195. if (FAILED(hr)) {
  196. return FALSE;
  197. } else {
  198. return TRUE;
  199. }
  200. }