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.

277 lines
8.2 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996.
  5. //
  6. // File: utility.cxx
  7. //
  8. // Contents: Helpful functions
  9. //
  10. // Classes:
  11. //
  12. // Functions: == and != for STAT_CHUNK, StrToGuid, HexStrToULONG
  13. // StrToPropspec, GuidToPwc
  14. //
  15. // Coupling:
  16. //
  17. // Notes:
  18. //
  19. // History: 9-24-1996 ericne Created
  20. //
  21. //----------------------------------------------------------------------------
  22. #include "pch.cxx"
  23. #include "utility.hxx"
  24. #include "mydebug.hxx"
  25. //+---------------------------------------------------------------------------
  26. //
  27. // Function: FreePropVariant
  28. //
  29. // Synopsis: Frees the memory associated with the PROPVARIANT struct
  30. //
  31. // Arguments: [pPropValue] -- pointer to a PROPVARIANT
  32. //
  33. // Returns: VOID
  34. //
  35. // History: 10-14-1996 ericne Created
  36. //
  37. // Notes:
  38. //
  39. //----------------------------------------------------------------------------
  40. void FreePropVariant( PROPVARIANT *& pPropValue )
  41. {
  42. HRESULT hResult = PropVariantClear( pPropValue );
  43. _ASSERT( S_OK == hResult );
  44. CoTaskMemFree( pPropValue );
  45. } //FreePropVariant
  46. //+---------------------------------------------------------------------------
  47. //
  48. // Function: StrToGuid
  49. //
  50. // Synopsis: Converts a precisely formatted string into a GUID
  51. //
  52. // Arguments: [szString] -- the string containing the guid information
  53. // [pGuid] -- a pointer to the guid
  54. //
  55. // Returns: TRUE if successful, FALSE if some error occurs
  56. //
  57. // History: 9-23-1996 ericne Created
  58. //
  59. // Notes: This functions does extensive error checking to make
  60. // sure it has a correctly formatted guid string.
  61. //
  62. //----------------------------------------------------------------------------
  63. BOOL StrToGuid( LPCTSTR szString, GUID *pGuid )
  64. {
  65. // Here is a sample guid string:
  66. // AA568EEC-E0E5-11CF-8FDA-00AA00A14F93
  67. int iIndex = 0;
  68. int iCounter = 0;
  69. ULONG ulTemp = 0;
  70. // Make sure the input string is formatted correctly
  71. if( _T('-') != szString[8] || _T('-') != szString[13] ||
  72. _T('-') != szString[18] || _T('-') != szString[23] ||
  73. _T('\0') != szString[36] )
  74. {
  75. printf( "Unable to parse guid %s\r\n", szString );
  76. return( FALSE );
  77. }
  78. // Fill in the first 3 guid fields:
  79. if( ! HexStrToULONG( &szString[0], 8, &ulTemp ) )
  80. return( FALSE );
  81. pGuid->Data1 = ulTemp;
  82. if( ! HexStrToULONG( &szString[9], 4, &ulTemp ) )
  83. return( FALSE );
  84. pGuid->Data2 = ( USHORT )ulTemp;
  85. if( ! HexStrToULONG( &szString[14], 4, &ulTemp ) )
  86. return( FALSE );
  87. pGuid->Data3 = ( USHORT )ulTemp;
  88. // Fill in the first two characters in the 4th guid field
  89. if( ! HexStrToULONG( &szString[19], 2, &ulTemp ) )
  90. return( FALSE );
  91. pGuid->Data4[0] = (unsigned char)ulTemp;
  92. if( ! HexStrToULONG( &szString[21], 2, &ulTemp ) )
  93. return( FALSE );
  94. pGuid->Data4[1] = (unsigned char)ulTemp;
  95. // Fill in the rest of the 4th guid field:
  96. for( iCounter = 24, iIndex = 2;
  97. ( iCounter < 36 ) && ( iIndex < 8 );
  98. iCounter += 2, ++iIndex )
  99. {
  100. if( ! HexStrToULONG( &szString[iCounter], 2, &ulTemp ) )
  101. return( FALSE );
  102. pGuid->Data4[iIndex] = (unsigned char)ulTemp;
  103. }
  104. return( TRUE );
  105. } //StrToGuid
  106. //+---------------------------------------------------------------------------
  107. //
  108. // Function: HexStrToULONG
  109. //
  110. // Synopsis: Uses strtoul to convert a string of hex digits to an
  111. // unsigned long.
  112. //
  113. // Arguments: [szHexString] -- The string to convert
  114. // [uiStopOffset] -- The number of digits to consider
  115. // [pUlong] -- a pointer to the ULONG to fill in
  116. //
  117. // Returns: TRUE if successful, FALSE if the string cannot be converted
  118. //
  119. // History: 9-23-1996 ericne Created
  120. //
  121. // Notes: The main purpose of this function is to catch possible user
  122. // typos of the form '-fff', where the leading hyphen could be
  123. // misinterpreted by strtoul as a minus sign.
  124. //
  125. //----------------------------------------------------------------------------
  126. BOOL HexStrToULONG( LPCTSTR szHexString, UINT uiStopOffset, ULONG *pUlong )
  127. {
  128. LPTSTR szStopString = NULL;
  129. ULONG ulTemp = 0;
  130. TCHAR szStringToChange[ MAX_LINE_SIZE ];
  131. if( MAX_LINE_SIZE <= uiStopOffset )
  132. {
  133. printf( "ERROR in HexStrToULONG. uiStopOffset too large\r\n" );
  134. return( FALSE );
  135. }
  136. // copy the part of the string to be converted into its own buffer
  137. _tcsncpy( szStringToChange, szHexString, uiStopOffset );
  138. szStringToChange[ uiStopOffset ] = '\0';
  139. // First, make sure the leading character is not a '-'
  140. if( _T('-') == *szStringToChange )
  141. {
  142. printf( "Parsing error: Unable to convert %s to unsigned long\r\n",
  143. szStringToChange );
  144. return( FALSE );
  145. }
  146. // Convert the string to an unsigned long
  147. ulTemp = _tcstoul( szStringToChange, &szStopString, 16 );
  148. // Make sure the stop offset is correct
  149. if( szStopString != szStringToChange + uiStopOffset )
  150. {
  151. printf( "Parsing error: Unable to convert %s to unsigned long\r\n",
  152. szStringToChange );
  153. return( FALSE );
  154. }
  155. (*pUlong) = ulTemp;
  156. return( TRUE );
  157. } //HexStrToULONG
  158. //+---------------------------------------------------------------------------
  159. //
  160. // Function: StrToPropspec
  161. //
  162. // Synopsis: converts a string to a PROPSPEC structure
  163. //
  164. // Arguments: [szString] -- the ANSI string
  165. // [pPropspec] -- a pointer to the PROPSPEC structure
  166. //
  167. // Returns: TRUE if successful, FALSE if an error occurs.
  168. //
  169. // History: 9-23-1996 ericne Created
  170. //
  171. // Notes:
  172. //
  173. //----------------------------------------------------------------------------
  174. BOOL StrToPropspec( LPCTSTR szString, PROPSPEC *pPropspec )
  175. {
  176. UINT uiStringLength = _tcslen( szString );
  177. ULONG ulTemp = 0;
  178. // If " is the first and last char in the PropSpec buffer,
  179. // treat it like a string.
  180. if( _T('\"') == szString[0] && _T('\"') == szString[ uiStringLength - 1 ] )
  181. {
  182. pPropspec->ulKind = PRSPEC_LPWSTR;
  183. // Allocate an array of wide characters:
  184. pPropspec->lpwstr = NEW WCHAR[ uiStringLength - 1 ];
  185. // Write the string into the buffer not including leading or trailing
  186. // double-quotes
  187. _snwprintf( pPropspec->lpwstr, uiStringLength - 2, L"%s", &szString[1]);
  188. pPropspec->lpwstr[ uiStringLength - 2 ] = _T('\0');
  189. }
  190. // Otherwise, treat it like a propid
  191. else
  192. {
  193. pPropspec->ulKind = PRSPEC_PROPID;
  194. // Convert the propid string into a propid
  195. if( ! HexStrToULONG( szString, uiStringLength, &ulTemp ) )
  196. return( FALSE );
  197. pPropspec->propid = ulTemp;
  198. }
  199. return( TRUE );
  200. } //StrToPropspec
  201. //+---------------------------------------------------------------------------
  202. //
  203. // Function: GetStringFromCLSID
  204. //
  205. // Synopsis: Converts a clsid (a.k.a. guid) to a string
  206. //
  207. // Arguments: [refclsid] --
  208. // [szString] --
  209. // [count] --
  210. //
  211. // Returns:
  212. //
  213. // History: 2-04-1997 ericne Created
  214. //
  215. // Notes:
  216. //
  217. //----------------------------------------------------------------------------
  218. LPTSTR GetStringFromCLSID( REFCLSID refclsid, LPTSTR szString, size_t count)
  219. {
  220. _sntprintf( szString, count,
  221. _T("%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X"),
  222. refclsid.Data1,
  223. refclsid.Data2,
  224. refclsid.Data3,
  225. refclsid.Data4[0],
  226. refclsid.Data4[1],
  227. refclsid.Data4[2],
  228. refclsid.Data4[3],
  229. refclsid.Data4[4],
  230. refclsid.Data4[5],
  231. refclsid.Data4[6],
  232. refclsid.Data4[7] );
  233. return( szString );
  234. } //GetStringFromCLSID