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.

206 lines
4.4 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. SdbGUIDFromString(
  42. IN LPCWSTR lpszGuid,
  43. OUT GUID* pGuid
  44. )
  45. /*++
  46. Return: TRUE on success, FALSE otherwise.
  47. Desc: Converts a string to a GUID.
  48. --*/
  49. {
  50. UNICODE_STRING ustrGuid;
  51. NTSTATUS status;
  52. RtlInitUnicodeString(&ustrGuid, lpszGuid);
  53. status = RtlGUIDFromString(&ustrGuid, pGuid);
  54. return NT_SUCCESS(status);
  55. }
  56. #else // we do the same thing for both WIN32A and WIN32U
  57. BOOL
  58. SdbGUIDFromString(
  59. LPCTSTR lpszGuid,
  60. GUID* pGuid)
  61. {
  62. int nFields;
  63. DWORD rgData4[8];
  64. DWORD dwData2;
  65. DWORD dwData3;
  66. INT i;
  67. nFields = _stscanf(lpszGuid, g_szGuidFormat,
  68. &pGuid->Data1, // type : long
  69. &dwData2, // type : short
  70. &dwData3, // type : short
  71. &rgData4[0],// type : short all the way to the bottom
  72. &rgData4[1],
  73. &rgData4[2],
  74. &rgData4[3],
  75. &rgData4[4],
  76. &rgData4[5],
  77. &rgData4[6],
  78. &rgData4[7]);
  79. if (nFields == 11) {
  80. pGuid->Data2 = (USHORT)dwData2;
  81. pGuid->Data3 = (USHORT)dwData3;
  82. for (i = 0; i < 8; ++i) {
  83. pGuid->Data4[i] = (BYTE)rgData4[i];
  84. }
  85. }
  86. return (nFields == 11);
  87. }
  88. #endif
  89. #ifndef WIN32A_MODE
  90. //
  91. // Private Functions used internally
  92. //
  93. NTSTATUS
  94. SdbpGUIDToUnicodeString(
  95. IN GUID* pGuid,
  96. OUT PUNICODE_STRING pUnicodeString
  97. )
  98. /*++
  99. Return: BUGBUG: ?
  100. Desc: BUGBUG: ?
  101. --*/
  102. {
  103. pUnicodeString->Length = GUID_STRING_SIZE * sizeof(WCHAR);
  104. pUnicodeString->MaximumLength = pUnicodeString->Length + sizeof(UNICODE_NULL);
  105. pUnicodeString->Buffer = SdbAlloc(pUnicodeString->MaximumLength);
  106. if (pUnicodeString->Buffer == NULL) {
  107. DBGPRINT((sdlError,
  108. "SdbpGUIDToUnicodeString",
  109. "Failed to allocate %ld bytes for GUID\n",
  110. (DWORD)pUnicodeString->MaximumLength));
  111. return STATUS_NO_MEMORY;
  112. }
  113. swprintf(pUnicodeString->Buffer, g_szGuidFormat,
  114. pGuid->Data1,
  115. pGuid->Data2,
  116. pGuid->Data3,
  117. pGuid->Data4[0],
  118. pGuid->Data4[1],
  119. pGuid->Data4[2],
  120. pGuid->Data4[3],
  121. pGuid->Data4[4],
  122. pGuid->Data4[5],
  123. pGuid->Data4[6],
  124. pGuid->Data4[7]);
  125. return STATUS_SUCCESS;
  126. }
  127. VOID
  128. SdbpFreeUnicodeString(
  129. PUNICODE_STRING pUnicodeString
  130. )
  131. /*++
  132. Return: BUGBUG: ?
  133. Desc: BUGBUG: ?
  134. --*/
  135. {
  136. if (pUnicodeString->Buffer != NULL) {
  137. SdbFree(pUnicodeString->Buffer);
  138. RtlZeroMemory(pUnicodeString, sizeof(*pUnicodeString));
  139. }
  140. }
  141. #endif // WIN32A_MODE
  142. BOOL
  143. SDBAPI
  144. SdbGUIDToString(
  145. IN GUID* pGuid,
  146. OUT LPTSTR pszGuid
  147. )
  148. {
  149. _stprintf(pszGuid, g_szGuidFormat,
  150. pGuid->Data1,
  151. pGuid->Data2,
  152. pGuid->Data3,
  153. pGuid->Data4[0],
  154. pGuid->Data4[1],
  155. pGuid->Data4[2],
  156. pGuid->Data4[3],
  157. pGuid->Data4[4],
  158. pGuid->Data4[5],
  159. pGuid->Data4[6],
  160. pGuid->Data4[7]);
  161. return TRUE;
  162. }