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.

312 lines
7.3 KiB

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