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.

330 lines
9.7 KiB

  1. //#--------------------------------------------------------------
  2. //
  3. // File: convertvsa.cpp
  4. //
  5. // Synopsis: Implementation of SdoProfile class methods
  6. // which are used to convert a SDO VSA attribute
  7. // to an IAS VSA attribute
  8. //
  9. // History: 06/16/98 MKarki Created
  10. //
  11. // Copyright (C) 1997-98 Microsoft Corporation
  12. // All rights reserved.
  13. //
  14. //----------------------------------------------------------------
  15. #include "stdafx.h"
  16. #include <iasattr.h>
  17. #include <winsock2.h>
  18. #include "sdoprofile.h"
  19. //
  20. // here the VSA format types as enum
  21. //
  22. typedef enum _rfc_format_
  23. {
  24. NONRFC_HEX,
  25. RFC_STRING,
  26. RFC_DEC,
  27. RFC_HEx
  28. } VSAFORMAT;
  29. //
  30. // these are constants which give the size of the VSA fields in the
  31. // SDO
  32. //
  33. const DWORD VSA_FORMAT_SIZE = 2;
  34. const DWORD VENDOR_ID_SIZE = 8;
  35. const DWORD VENDOR_TYPE_SIZE = 2;
  36. const DWORD VENDOR_LENGTH_SIZE = 2;
  37. const DWORD MAX_ATTRIBUTE_LENGTH = 253;
  38. const DWORD MAX_ASCII = 127;
  39. //++--------------------------------------------------------------
  40. //
  41. // Function: VSAAttributeFromVariant
  42. //
  43. // Synopsis: This is the private method of class
  44. // CSdoProfile. It is used to convert a
  45. // VSA SDO attribute to a VSA IAS attribute
  46. //
  47. // Arguments:
  48. // [in] VARIANT*
  49. // [in] IASTYPE
  50. //
  51. // Returns: PIASATTRIBUTE
  52. //
  53. //
  54. // History: MKarki Created 05/21/98
  55. //
  56. //----------------------------------------------------------------
  57. PIASATTRIBUTE
  58. CSdoProfile::VSAAttributeFromVariant (
  59. VARIANT *pVariant,
  60. IASTYPE iasType
  61. )
  62. {
  63. BOOL bStatus = FALSE;
  64. PBYTE pBuffer = NULL;
  65. WCHAR wstrTemp [MAX_ATTRIBUTE_LENGTH +1];
  66. PBYTE pHexString = NULL;
  67. DWORD dwFilledBuffer = 0;
  68. PDWORD pdwValue = NULL;
  69. PIASATTRIBUTE pIasAttribute = NULL;
  70. _ASSERT (NULL != pVariant);
  71. //
  72. // if this is a VSA attribute than the type can only be
  73. // IASTYPE_OCTET_STRING
  74. //
  75. _ASSERT (IASTYPE_OCTET_STRING == iasType);
  76. __try
  77. {
  78. //
  79. // Allocate an attribute to hold the result.
  80. //
  81. DWORD dwRetVal = ::IASAttributeAlloc (1, &pIasAttribute);
  82. if ( 0 != dwRetVal )
  83. __leave;
  84. //
  85. // Allocate the memory for the IAS attribute
  86. //
  87. PBYTE pBuffer = reinterpret_cast <PBYTE>
  88. (::CoTaskMemAlloc(MAX_ATTRIBUTE_LENGTH));
  89. if (NULL == pBuffer)
  90. {
  91. IASTracePrintf("Error in Profile VSAAttributeFromVariant() - CoTaskMemAlloc() failed...");
  92. __leave;
  93. }
  94. PWCHAR pStrStart = V_BSTR (pVariant);
  95. //
  96. // get the size of the string passed in
  97. //
  98. DWORD dwVsaLength = wcslen (pStrStart);
  99. _ASSERT (
  100. (dwVsaLength >= VSA_FORMAT_SIZE) &&
  101. (dwVsaLength <= MAX_ATTRIBUTE_LENGTH)
  102. );
  103. // get the VSA format stored as the first 2 characters of the
  104. // string
  105. //
  106. lstrcpynW (wstrTemp, pStrStart, VSA_FORMAT_SIZE +1);
  107. DWORD dwFormat = wcstoul (wstrTemp, NULL, 16);
  108. pStrStart += VSA_FORMAT_SIZE;
  109. // get the vendor ID now
  110. //
  111. PDWORD pdwVendorId = reinterpret_cast <PDWORD> (pBuffer);
  112. lstrcpyn (wstrTemp, pStrStart, VENDOR_ID_SIZE +1);
  113. *pdwVendorId = htonl (wcstoul (wstrTemp, NULL, 16));
  114. pStrStart += VENDOR_ID_SIZE;
  115. dwFilledBuffer += sizeof (DWORD);
  116. // take different action depending upon the VSA format
  117. //
  118. if (NONRFC_HEX == dwFormat)
  119. {
  120. // Non RFC Format
  121. //
  122. _ASSERT (dwVsaLength > (VSA_FORMAT_SIZE + VENDOR_ID_SIZE));
  123. // go through the characters in string and convert to hex
  124. //
  125. for (
  126. dwVsaLength -= (VSA_FORMAT_SIZE + VENDOR_ID_SIZE),
  127. pHexString = pBuffer + dwFilledBuffer;
  128. ((dwFilledBuffer < MAX_ATTRIBUTE_LENGTH) &&
  129. (dwVsaLength > 0));
  130. pStrStart += 2, dwVsaLength -= 2,
  131. pHexString++, dwFilledBuffer++
  132. )
  133. {
  134. //TODO - change use of wcstoul to convert string to hex
  135. //
  136. lstrcpynW (wstrTemp, pStrStart, 3);
  137. *pHexString = wcstoul (wstrTemp, NULL, 16);
  138. }
  139. }
  140. else
  141. {
  142. //
  143. // RFC compliant format
  144. //
  145. _ASSERT (dwVsaLength >
  146. (VSA_FORMAT_SIZE + VENDOR_ID_SIZE +
  147. VENDOR_TYPE_SIZE + VENDOR_LENGTH_SIZE)
  148. );
  149. //
  150. // get vendor Type
  151. //
  152. PBYTE pVendorType = reinterpret_cast <PBYTE>
  153. (pBuffer + dwFilledBuffer);
  154. lstrcpyn (wstrTemp, pStrStart, VENDOR_TYPE_SIZE + 1);
  155. *pVendorType = wcstoul (wstrTemp, NULL, 16);
  156. pStrStart += VENDOR_TYPE_SIZE;
  157. dwFilledBuffer += sizeof (BYTE);
  158. //
  159. // get vendor Length
  160. //
  161. PBYTE pVendorLength = reinterpret_cast <PBYTE>
  162. (pBuffer +dwFilledBuffer);
  163. lstrcpyn (wstrTemp, pStrStart, VENDOR_LENGTH_SIZE +1);
  164. *pVendorLength = wcstoul (wstrTemp, NULL, 16);
  165. pStrStart += VENDOR_LENGTH_SIZE;
  166. dwFilledBuffer += sizeof (BYTE);
  167. //
  168. // now we do processing specific to each specific RFC
  169. // compliant VSA type
  170. //
  171. switch (dwFormat)
  172. {
  173. case RFC_STRING: // RFC compliant string format
  174. dwFilledBuffer += wcstombs (
  175. reinterpret_cast <PCHAR>
  176. (pBuffer + dwFilledBuffer),
  177. pStrStart,
  178. MAX_ATTRIBUTE_LENGTH - (dwFilledBuffer + 1)
  179. );
  180. break;
  181. case 2: // RFC compliant decimal format
  182. //
  183. // get decimal value
  184. //
  185. pdwValue = reinterpret_cast <PDWORD>
  186. (pBuffer + dwFilledBuffer);
  187. *pdwValue = htonl (wcstoul (pStrStart, NULL, 16));
  188. dwFilledBuffer += sizeof (DWORD);
  189. break;
  190. case 3: // RFC compliant HEX format
  191. //
  192. // go through the characters in string and convert to hex
  193. //
  194. dwVsaLength -= (VSA_FORMAT_SIZE + VENDOR_ID_SIZE +
  195. VENDOR_TYPE_SIZE + VENDOR_LENGTH_SIZE);
  196. pHexString = reinterpret_cast <PBYTE>
  197. (pBuffer + dwFilledBuffer);
  198. //
  199. // convert the Hex values into bytes
  200. //
  201. for (;
  202. ((dwFilledBuffer < MAX_ATTRIBUTE_LENGTH) &&
  203. (dwVsaLength > 0));
  204. pStrStart += 2, dwVsaLength -= 2,
  205. pHexString++, dwFilledBuffer++
  206. )
  207. {
  208. lstrcpynW (wstrTemp, pStrStart, 3);
  209. *pHexString = wcstoul (wstrTemp, NULL, 16);
  210. }
  211. break;
  212. default:
  213. //
  214. // error
  215. //
  216. IASTracePrintf("Error in SDO Profile - VSAAttributeFromVariant() - Unknown VSA type...");
  217. __leave;
  218. break;
  219. }
  220. }
  221. //
  222. // now set values in the IASAttribute
  223. //
  224. pIasAttribute->Value.OctetString.dwLength = dwFilledBuffer;
  225. pIasAttribute->Value.OctetString.lpValue = pBuffer;
  226. pIasAttribute->Value.itType = iasType;
  227. //
  228. // succefully converted VSA to IASAttribute
  229. //
  230. bStatus = TRUE;
  231. }
  232. __finally
  233. {
  234. if (FALSE == bStatus)
  235. {
  236. //
  237. // free the resources allocated
  238. //
  239. if (NULL != pBuffer)
  240. {
  241. ::CoTaskMemFree (pBuffer);
  242. pBuffer = NULL;
  243. }
  244. if (NULL != pIasAttribute)
  245. {
  246. ::IASAttributeRelease (pIasAttribute);
  247. pIasAttribute = NULL;
  248. }
  249. }
  250. }
  251. return (pIasAttribute);
  252. } // end of CSdoProfile::VSAAttributeFromVariant method
  253. #if 0
  254. //++--------------------------------------------------------------
  255. //
  256. // Function: ConvertToInt
  257. //
  258. // Synopsis: This is the private method of CSdoProfile
  259. // used to convert a WCHAR to its actual value
  260. //
  261. // Arguments:
  262. // [in] WCHAR
  263. //
  264. // Returns: BYTE
  265. //
  266. // History: MKarki Created 05/21/98
  267. //
  268. //----------------------------------------------------------------
  269. BYTE
  270. CSdoProfile::ConvertToInt (
  271. WCHAR inChar
  272. )
  273. {
  274. static const BYTE ConversionArray [MAX_ASCII] =
  275. {
  276. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0 - 9 */
  277. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 - 19 */
  278. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 20 - 29 */
  279. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 30 - 39 */
  280. 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, /* 40 - 49 */
  281. 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, /* 50 - 59 */
  282. 0, 0, 0, 0, 0, 10, 11, 12, 13, 14,/* 60 - 69 */
  283. 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 70 - 79 */
  284. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 - 89 */
  285. 0, 0, 0, 0, 0, 0, 0, 10, 11, 12, /* 90 - 99 */
  286. 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, /* 100- 109*/
  287. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 110- 119*/
  288. 0, 0, 0, 0, 0, 0, 0 /* 120- 126*/
  289. };
  290. _ASSERT (inChar < MAX_ASCII);
  291. return (ConversionArray[inChar]);
  292. }
  293. #endif