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.

317 lines
7.7 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. wanarp2\guid.c
  5. Abstract:
  6. Cut-n-Paste of rtl\guid.c but without UNICODE_STRINGs and non paged
  7. Revision History:
  8. AmritanR Created
  9. --*/
  10. #define __FILE_SIG__ GUID_SIG
  11. #include "inc.h"
  12. #pragma hdrstop
  13. #define GUID_FORMAT_W L"{%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}"
  14. int
  15. swprintf(wchar_t *buffer, const wchar_t *format, ... );
  16. int
  17. __cdecl
  18. ScanHexFormat(
  19. IN const WCHAR* pwszBuffer,
  20. IN ULONG ulCharCount,
  21. IN const WCHAR* pwszFormat,
  22. ...
  23. )
  24. /*++
  25. Routine Description:
  26. Scans a source Buffer and places values from that buffer into the parameters
  27. as specified by Format.
  28. Arguments:
  29. pwszBuffer Source buffer which is to be scanned.
  30. ulCharCount Maximum length in characters for which Buffer is searched.
  31. This implies that Buffer need not be UNICODE_NULL terminated.
  32. Format Format string which defines both the acceptable string form as
  33. contained in pwszBuffer
  34. Return Value:
  35. Returns the number of parameters filled if the end of the Buffer is reached,
  36. else -1 on an error.
  37. --*/
  38. {
  39. va_list ArgList;
  40. int iFormatItems;
  41. va_start(ArgList, pwszFormat);
  42. //
  43. // Count of number of parameters filled
  44. //
  45. iFormatItems = 0;
  46. while(TRUE)
  47. {
  48. switch (*pwszFormat)
  49. {
  50. case UNICODE_NULL:
  51. {
  52. //
  53. // end of string
  54. //
  55. return (*pwszBuffer && ulCharCount) ? -1 : iFormatItems;
  56. }
  57. case L'%':
  58. {
  59. //
  60. // Format specifier
  61. //
  62. pwszFormat++;
  63. if (*pwszFormat != L'%')
  64. {
  65. ULONG ulNumber;
  66. int iWidth;
  67. int iLong;
  68. PVOID pvPointer;
  69. //
  70. // So it isnt a %%
  71. //
  72. iLong = 0;
  73. iWidth = 0;
  74. while(TRUE)
  75. {
  76. if((*pwszFormat >= L'0') &&
  77. (*pwszFormat <= L'9'))
  78. {
  79. iWidth = iWidth * 10 + *pwszFormat - '0';
  80. }
  81. else
  82. {
  83. if(*pwszFormat == L'l')
  84. {
  85. iLong++;
  86. }
  87. else
  88. {
  89. if((*pwszFormat == L'X') ||
  90. (*pwszFormat == L'x'))
  91. {
  92. break;
  93. }
  94. }
  95. }
  96. //
  97. // Move to the next specifier
  98. //
  99. pwszFormat++;
  100. }
  101. pwszFormat++;
  102. for(ulNumber = 0; iWidth--; pwszBuffer++, ulCharCount--)
  103. {
  104. if(!ulCharCount)
  105. {
  106. return -1;
  107. }
  108. ulNumber *= 16;
  109. if((*pwszBuffer >= L'0') &&
  110. (*pwszBuffer <= L'9'))
  111. {
  112. ulNumber += (*pwszBuffer - L'0');
  113. }
  114. else
  115. {
  116. if((*pwszBuffer >= L'a') &&
  117. (*pwszBuffer <= L'f'))
  118. {
  119. ulNumber += (*pwszBuffer - L'a' + 10);
  120. }
  121. else
  122. {
  123. if((*pwszBuffer >= L'A') &&
  124. (*pwszBuffer <= L'F'))
  125. {
  126. ulNumber += (*pwszBuffer - L'A' + 10);
  127. }
  128. else
  129. {
  130. return -1;
  131. }
  132. }
  133. }
  134. }
  135. pvPointer = va_arg(ArgList, PVOID);
  136. if(iLong)
  137. {
  138. *(PULONG)pvPointer = ulNumber;
  139. }
  140. else
  141. {
  142. *(PUSHORT)pvPointer = (USHORT)ulNumber;
  143. }
  144. iFormatItems++;
  145. break;
  146. }
  147. //
  148. // NO BREAK
  149. //
  150. }
  151. default:
  152. {
  153. if (!ulCharCount || (*pwszBuffer != *pwszFormat))
  154. {
  155. return -1;
  156. }
  157. pwszBuffer++;
  158. ulCharCount--;
  159. pwszFormat++;
  160. break;
  161. }
  162. }
  163. }
  164. }
  165. INT
  166. ConvertGuidToString(
  167. IN GUID *pGuid,
  168. OUT PWCHAR pwszBuffer
  169. )
  170. /*++
  171. Routine Description:
  172. Constructs the standard string version of a GUID, in the form:
  173. "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}".
  174. Arguments:
  175. pGuid Contains the GUID to translate.
  176. pwszBuffer Space for storing the string. Must be >= 39 * sizeof(WCHAR)
  177. Return Value:
  178. --*/
  179. {
  180. return swprintf(pwszBuffer,
  181. GUID_FORMAT_W,
  182. pGuid->Data1,
  183. pGuid->Data2,
  184. pGuid->Data3,
  185. pGuid->Data4[0],
  186. pGuid->Data4[1],
  187. pGuid->Data4[2],
  188. pGuid->Data4[3],
  189. pGuid->Data4[4],
  190. pGuid->Data4[5],
  191. pGuid->Data4[6],
  192. pGuid->Data4[7]);
  193. }
  194. NTSTATUS
  195. ConvertStringToGuid(
  196. IN PWCHAR pwszGuid,
  197. IN ULONG ulStringLen,
  198. OUT GUID *pGuid
  199. )
  200. /*++
  201. Routine Description:
  202. Retrieves a the binary format of a textual GUID presented in the standard
  203. string version of a GUID: "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}".
  204. Arguments:
  205. GuidString -
  206. Place from which to retrieve the textual form of the GUID.
  207. Guid -
  208. Place in which to put the binary form of the GUID.
  209. Return Value:
  210. Returns STATUS_SUCCESS if the buffer contained a valid GUID, else
  211. STATUS_INVALID_PARAMETER if the string was invalid.
  212. --*/
  213. {
  214. USHORT Data4[8];
  215. int Count;
  216. if (ScanHexFormat(pwszGuid,
  217. ulStringLen/sizeof(WCHAR),
  218. GUID_FORMAT_W,
  219. &pGuid->Data1,
  220. &pGuid->Data2,
  221. &pGuid->Data3,
  222. &Data4[0],
  223. &Data4[1],
  224. &Data4[2],
  225. &Data4[3],
  226. &Data4[4],
  227. &Data4[5],
  228. &Data4[6],
  229. &Data4[7]) == -1)
  230. {
  231. return STATUS_INVALID_PARAMETER;
  232. }
  233. for(Count = 0; Count < sizeof(Data4)/sizeof(Data4[0]); Count++)
  234. {
  235. pGuid->Data4[Count] = (UCHAR)Data4[Count];
  236. }
  237. return STATUS_SUCCESS;
  238. }