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.

444 lines
10 KiB

  1. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2. Microsoft Windows, Copyright (C) Microsoft Corporation, 2000.
  3. File: Attributes.cpp
  4. Content: Implementation of CAttributes.
  5. History: 11-15-99 dsie created
  6. ------------------------------------------------------------------------------*/
  7. #include "StdAfx.h"
  8. #include "CAPICOM.h"
  9. #include "Attributes.h"
  10. ////////////////////////////////////////////////////////////////////////////////
  11. //
  12. // Exported functions.
  13. //
  14. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  15. Function : CreateAttributesObject
  16. Synopsis : Create and initialize an IAttributes collection object.
  17. Parameter: CRYPT_ATTRIBUTES * pAttrbibutes - Pointer to attributes to be
  18. added to the collection object.
  19. IAttributes ** ppIAttributes - Pointer to pointer to IAttributes
  20. to receive the interface pointer.
  21. Remark :
  22. ------------------------------------------------------------------------------*/
  23. HRESULT CreateAttributesObject (CRYPT_ATTRIBUTES * pAttributes,
  24. IAttributes ** ppIAttributes)
  25. {
  26. HRESULT hr = S_OK;
  27. CComObject<CAttributes> * pCAttributes = NULL;
  28. DebugTrace("Entering CreateAttributesObject().\n");
  29. //
  30. // Sanity check.
  31. //
  32. ATLASSERT(pAttributes);
  33. ATLASSERT(ppIAttributes);
  34. try
  35. {
  36. //
  37. // Create the object. Note that the ref count will still be 0
  38. // after the object is created.
  39. //
  40. if (FAILED(hr = CComObject<CAttributes>::CreateInstance(&pCAttributes)))
  41. {
  42. DebugTrace("Error [%#x]: CComObject<CAttributes>::CreateInstance() failed.\n", hr);
  43. goto ErrorExit;
  44. }
  45. //
  46. // Initialize object.
  47. //
  48. if (FAILED(hr = pCAttributes->Init(pAttributes)))
  49. {
  50. DebugTrace("Error [%#x]: pCAttributes->Init() failed.\n", hr);
  51. goto ErrorExit;
  52. }
  53. //
  54. // Return IAttributes pointer to caller.
  55. //
  56. if (FAILED(hr = pCAttributes->QueryInterface(ppIAttributes)))
  57. {
  58. DebugTrace("Error [%#x]: pCAttributes->QueryInterface() failed.\n", hr);
  59. goto ErrorExit;
  60. }
  61. }
  62. catch(...)
  63. {
  64. hr = E_POINTER;
  65. DebugTrace("Exception: invalid parameter.\n");
  66. goto ErrorExit;
  67. }
  68. CommonExit:
  69. DebugTrace("Leaving CreateAttributesObject().\n");
  70. return hr;
  71. ErrorExit:
  72. //
  73. // Sanity check.
  74. //
  75. ATLASSERT(FAILED(hr));
  76. //
  77. // Free resource.
  78. //
  79. if (pCAttributes)
  80. {
  81. delete pCAttributes;
  82. }
  83. goto CommonExit;
  84. }
  85. ////////////////////////////////////////////////////////////////////////////////
  86. //
  87. // CAttributes
  88. //
  89. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  90. Function : CAttributes::Add
  91. Synopsis : Add an Attribute to the collection.
  92. Parameter: IAttribute * pVal - Attribute to be added.
  93. Remark :
  94. ------------------------------------------------------------------------------*/
  95. STDMETHODIMP CAttributes::Add (IAttribute * pVal)
  96. {
  97. HRESULT hr = S_OK;
  98. char szIndex[33];
  99. CComBSTR bstrIndex;
  100. DebugTrace("Entering CAttributes::Add().\n");
  101. try
  102. {
  103. //
  104. // Lock access to this object.
  105. //
  106. m_Lock.Lock();
  107. //
  108. // Check parameters.
  109. //
  110. if (NULL == pVal)
  111. {
  112. hr = E_INVALIDARG;
  113. DebugTrace("Error [%#x]: Parameter pVal is NULL.\n", hr);
  114. goto ErrorExit;
  115. }
  116. //
  117. // Make sure we have a valid attribute object.
  118. //
  119. if (FAILED(hr = ::AttributeIsValid(pVal)))
  120. {
  121. DebugTrace("Error [%#x]: AttributeIsValid() failed.\n", hr);
  122. goto ErrorExit;
  123. }
  124. //
  125. // Make sure we still have room to add.
  126. //
  127. if ((m_dwNextIndex + 1) > m_coll.max_size())
  128. {
  129. hr = CAPICOM_E_OUT_OF_RESOURCE;
  130. DebugTrace("Error [%#x]: Maximum entries (%#x) reached for Attributes collection.\n",
  131. hr, m_coll.size() + 1);
  132. goto ErrorExit;
  133. }
  134. //
  135. // BSTR index of numeric value.
  136. //
  137. wsprintfA(szIndex, "%#08x", ++m_dwNextIndex);
  138. if (!(bstrIndex = szIndex))
  139. {
  140. hr = E_OUTOFMEMORY;
  141. DebugTrace("Error [%#x]: bstrIndex = szIndex failed.\n", hr);
  142. goto ErrorExit;
  143. }
  144. //
  145. // Now add object to collection map.
  146. //
  147. // Note that the overloaded = operator for CComPtr will
  148. // automatically AddRef to the object. Also, when the CComPtr
  149. // is deleted (happens when the Remove or map destructor is called),
  150. // the CComPtr destructor will automatically Release the object.
  151. //
  152. m_coll[bstrIndex] = pVal;
  153. }
  154. catch(...)
  155. {
  156. hr = E_POINTER;
  157. DebugTrace("Exception: invalid parameter.\n");
  158. goto ErrorExit;
  159. }
  160. UnlockExit:
  161. //
  162. // Unlock access to this object.
  163. //
  164. m_Lock.Unlock();
  165. DebugTrace("Leaving CAttributes::Add().\n");
  166. return hr;
  167. ErrorExit:
  168. //
  169. // Sanity check.
  170. //
  171. ATLASSERT(FAILED(hr));
  172. ReportError(hr);
  173. goto UnlockExit;
  174. }
  175. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  176. Function : CAttributes::Remove
  177. Synopsis : Remove a Attribute from the collection.
  178. Parameter: long Index - Attribute index (1-based).
  179. Remark :
  180. ------------------------------------------------------------------------------*/
  181. STDMETHODIMP CAttributes::Remove (long Index)
  182. {
  183. HRESULT hr = S_OK;
  184. AttributeMap::iterator iter;
  185. DebugTrace("Entering CAttributes::Remove().\n");
  186. try
  187. {
  188. //
  189. // Lock access to this object.
  190. //
  191. m_Lock.Lock();
  192. //
  193. // Make sure parameter is valid.
  194. //
  195. if (Index < 1 || (DWORD) Index > m_coll.size())
  196. {
  197. hr = E_INVALIDARG;
  198. DebugTrace("Error [%#x]: Index %d is out of range.\n", hr, Index);
  199. goto ErrorExit;
  200. }
  201. //
  202. // Find object in map.
  203. //
  204. Index--;
  205. iter = m_coll.begin();
  206. while (iter != m_coll.end() && Index > 0)
  207. {
  208. iter++;
  209. Index--;
  210. }
  211. //
  212. // This should not happen.
  213. //
  214. if (iter == m_coll.end())
  215. {
  216. hr = CAPICOM_E_INTERNAL;
  217. DebugTrace("Error [%#x]: iterator went pass end of map.\n", hr);
  218. goto ErrorExit;
  219. }
  220. //
  221. // Now remove object in map.
  222. //
  223. m_coll.erase(iter);
  224. }
  225. catch(...)
  226. {
  227. hr = E_POINTER;
  228. DebugTrace("Exception: invalid parameter.\n");
  229. goto ErrorExit;
  230. }
  231. UnlockExit:
  232. //
  233. // Unlock access to this object.
  234. //
  235. m_Lock.Unlock();
  236. DebugTrace("Leaving CAttributes::Remove().\n");
  237. return hr;
  238. ErrorExit:
  239. //
  240. // Sanity check.
  241. //
  242. ATLASSERT(FAILED(hr));
  243. ReportError(hr);
  244. goto UnlockExit;
  245. }
  246. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  247. Function : CAttributes::Clear
  248. Synopsis : Remove all attributes from the collection.
  249. Parameter: None.
  250. Remark :
  251. ------------------------------------------------------------------------------*/
  252. STDMETHODIMP CAttributes::Clear (void)
  253. {
  254. HRESULT hr = S_OK;
  255. DebugTrace("Entering CAttributes::Clear().\n");
  256. //
  257. // Lock access to this object.
  258. //
  259. m_Lock.Lock();
  260. //
  261. // Clear it.
  262. //
  263. m_coll.clear();
  264. //
  265. // Unlock access to this object.
  266. //
  267. m_Lock.Unlock();
  268. DebugTrace("Leaving CAttributes::Clear().\n");
  269. return hr;
  270. }
  271. ////////////////////////////////////////////////////////////////////////////////
  272. //
  273. // Non COM functions.
  274. //
  275. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  276. Function : CAttributes::Init
  277. Synopsis : Initialize the attributes collection object by adding all
  278. individual attribute object to the collection.
  279. Parameter: CRYPT_ATTRIBUTES * pAttributes - Attribute to be added.
  280. Remark :
  281. ------------------------------------------------------------------------------*/
  282. STDMETHODIMP CAttributes::Init (CRYPT_ATTRIBUTES * pAttributes)
  283. {
  284. HRESULT hr = S_OK;
  285. DebugTrace("Entering CAttributes::Init().\n");
  286. //
  287. // Sanity check.
  288. //
  289. ATLASSERT(pAttributes);
  290. //
  291. // Initialize.
  292. //
  293. m_dwNextIndex = 0;
  294. //
  295. // Create the IAttribute object for each of the supported attribute.
  296. //
  297. for (DWORD cAttr= 0; cAttr < pAttributes->cAttr; cAttr++)
  298. {
  299. CComPtr<IAttribute> pIAttribute = NULL;
  300. //
  301. // Add only supported attribute.
  302. //
  303. if (::AttributeIsSupported(pAttributes->rgAttr[cAttr].pszObjId))
  304. {
  305. if (FAILED(hr = ::CreateAttributeObject(&pAttributes->rgAttr[cAttr], &pIAttribute)))
  306. {
  307. DebugTrace("Error [%#x]: CreateAttributeObject() failed.\n", hr);
  308. goto ErrorExit;
  309. }
  310. if (FAILED(hr = Add(pIAttribute)))
  311. {
  312. DebugTrace("Error [%#x]: CAttributes::Add() failed.\n", hr);
  313. goto ErrorExit;
  314. }
  315. }
  316. }
  317. CommonExit:
  318. DebugTrace("Leaving CAttributes::Init().\n");
  319. return hr;
  320. ErrorExit:
  321. //
  322. // Sanity check.
  323. //
  324. ATLASSERT(FAILED(hr));
  325. //
  326. // Free resource.
  327. //
  328. m_coll.clear();
  329. goto CommonExit;
  330. }