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.

532 lines
13 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation
  3. Module Name:
  4. assemblyreference.cpp
  5. Abstract:
  6. Class the contains all the attributes of an assembly reference.
  7. Author:
  8. Michael J. Grier (MGrier) 10-May-2000
  9. Revision History:
  10. xiaoyuw 09/2000 revise the code using Assembly Identity
  11. A couple of APIs in this class are kind of out of date, such as GetXXX, SetXXX, XXXSpecified : they
  12. are not called at all.
  13. --*/
  14. #include "stdinc.h"
  15. #include <windows.h>
  16. #include "sxsapi.h"
  17. #include "sxsp.h"
  18. #include "assemblyreference.h"
  19. #include "sxsid.h"
  20. #include "sxsidp.h"
  21. #include "fusionparser.h"
  22. #include "fusionheap.h"
  23. #include "sxsexceptionhandling.h"
  24. VOID
  25. CAssemblyReference::Construct()
  26. {
  27. m_pAssemblyIdentity = NULL;
  28. }
  29. VOID
  30. CAssemblyReference::Destroy()
  31. {
  32. CSxsPreserveLastError ple;
  33. ::SxsDestroyAssemblyIdentity(m_pAssemblyIdentity);
  34. m_pAssemblyIdentity = NULL;
  35. ple.Restore();
  36. }
  37. BOOL
  38. CAssemblyReference::Initialize()
  39. {
  40. FN_PROLOG_WIN32
  41. INTERNAL_ERROR_CHECK(m_pAssemblyIdentity == NULL);
  42. IFW32FALSE_EXIT(::SxsCreateAssemblyIdentity(0, ASSEMBLY_IDENTITY_TYPE_REFERENCE, &m_pAssemblyIdentity, 0, NULL));
  43. FN_EPILOG
  44. }
  45. BOOL
  46. CAssemblyReference::Initialize(
  47. PCASSEMBLY_IDENTITY Identity
  48. )
  49. {
  50. FN_PROLOG_WIN32
  51. PARAMETER_CHECK(Identity != NULL);
  52. INTERNAL_ERROR_CHECK(m_pAssemblyIdentity == NULL);
  53. IFW32FALSE_EXIT(::SxsDuplicateAssemblyIdentity(0, Identity, &m_pAssemblyIdentity));
  54. FN_EPILOG
  55. }
  56. BOOL
  57. CAssemblyReference::Initialize(
  58. const CAssemblyReference &r
  59. )
  60. {
  61. FN_PROLOG_WIN32
  62. INTERNAL_ERROR_CHECK(m_pAssemblyIdentity == NULL);
  63. INTERNAL_ERROR_CHECK(r.m_pAssemblyIdentity != NULL);
  64. IFW32FALSE_EXIT(
  65. ::SxsDuplicateAssemblyIdentity(
  66. 0, // DWORD Flags,
  67. r.m_pAssemblyIdentity, // PCASSEMBLY_IDENTITY Source,
  68. &m_pAssemblyIdentity)); // PASSEMBLY_IDENTITY *Destination
  69. FN_EPILOG
  70. }
  71. BOOL
  72. CAssemblyReference::Hash(
  73. ULONG &rulPseudoKey
  74. ) const
  75. {
  76. FN_PROLOG_WIN32
  77. INTERNAL_ERROR_CHECK(m_pAssemblyIdentity != NULL);
  78. IFW32FALSE_EXIT(
  79. ::SxsHashAssemblyIdentity(
  80. 0, // DWORD dwFlags,
  81. m_pAssemblyIdentity, // ASSEMBLY_IDENTITY pAssemblyIdentity,
  82. &rulPseudoKey)); // ULONG & rfulPseudoKey
  83. FN_EPILOG
  84. }
  85. BOOL
  86. CAssemblyReference::SetAssemblyName(
  87. PCWSTR AssemblyNameValue,
  88. SIZE_T AssemblyNameValueCch
  89. )
  90. {
  91. FN_PROLOG_WIN32
  92. PARAMETER_CHECK(AssemblyNameValue != NULL);
  93. PARAMETER_CHECK(AssemblyNameValueCch != 0);
  94. INTERNAL_ERROR_CHECK(m_pAssemblyIdentity != NULL);
  95. IFW32FALSE_EXIT(
  96. ::SxspSetAssemblyIdentityAttributeValue(
  97. SXSP_SET_ASSEMBLY_IDENTITY_ATTRIBUTE_VALUE_FLAG_OVERWRITE_EXISTING,
  98. m_pAssemblyIdentity,
  99. &s_IdentityAttribute_name,
  100. AssemblyNameValue,
  101. AssemblyNameValueCch));
  102. FN_EPILOG
  103. }
  104. // if m_pAssemblyIdentity is NULL, return TRUE with Cch == 0
  105. BOOL
  106. CAssemblyReference::GetAssemblyName(
  107. PCWSTR *pAssemblyName,
  108. SIZE_T *Cch
  109. ) const
  110. {
  111. FN_PROLOG_WIN32
  112. SIZE_T CchTemp = 0;
  113. if (Cch != NULL)
  114. *Cch = 0;
  115. if (pAssemblyName != NULL)
  116. *pAssemblyName = NULL;
  117. INTERNAL_ERROR_CHECK(m_pAssemblyIdentity != NULL);
  118. IFW32FALSE_EXIT(
  119. ::SxspGetAssemblyIdentityAttributeValue(
  120. SXSP_GET_ASSEMBLY_IDENTITY_ATTRIBUTE_VALUE_FLAG_NOT_FOUND_RETURNS_NULL,
  121. m_pAssemblyIdentity,
  122. &s_IdentityAttribute_name,
  123. pAssemblyName,
  124. &CchTemp));
  125. if (Cch != NULL)
  126. *Cch = CchTemp;
  127. FN_EPILOG
  128. }
  129. BOOL
  130. CAssemblyReference::TakeValue(
  131. CAssemblyReference &r
  132. )
  133. {
  134. FN_PROLOG_WIN32
  135. INTERNAL_ERROR_CHECK(r.m_pAssemblyIdentity != NULL);
  136. if (m_pAssemblyIdentity != NULL)
  137. {
  138. SxsDestroyAssemblyIdentity(m_pAssemblyIdentity);
  139. m_pAssemblyIdentity = NULL;
  140. }
  141. //
  142. // ISSUE-2002/05/04-jonwis : I'm not convinced that "take value" should have "copy"
  143. // semantics... Isn't that what Assign is for? "Take value" to me means "steal
  144. // their copy, don't allocate more memory."
  145. //
  146. IFW32FALSE_EXIT(::SxsDuplicateAssemblyIdentity(0, r.m_pAssemblyIdentity, &m_pAssemblyIdentity));
  147. FN_EPILOG
  148. }
  149. BOOL CAssemblyReference::ClearAssemblyName()
  150. {
  151. FN_PROLOG_WIN32
  152. INTERNAL_ERROR_CHECK(m_pAssemblyIdentity != NULL);
  153. IFW32FALSE_EXIT(::SxspRemoveAssemblyIdentityAttribute(0, m_pAssemblyIdentity, &s_IdentityAttribute_name));
  154. FN_EPILOG
  155. }
  156. BOOL
  157. CAssemblyReference::GetLanguage(
  158. PCWSTR &rString,
  159. SIZE_T &rCch
  160. ) const
  161. {
  162. FN_PROLOG_WIN32
  163. IFW32FALSE_EXIT(
  164. ::SxspGetAssemblyIdentityAttributeValue(
  165. SXSP_GET_ASSEMBLY_IDENTITY_ATTRIBUTE_VALUE_FLAG_NOT_FOUND_RETURNS_NULL,
  166. m_pAssemblyIdentity,
  167. &s_IdentityAttribute_language,
  168. &rString,
  169. &rCch));
  170. FN_EPILOG
  171. }
  172. BOOL
  173. CAssemblyReference::SetLanguage(
  174. const CBaseStringBuffer &rbuff
  175. )
  176. {
  177. FN_PROLOG_WIN32
  178. INTERNAL_ERROR_CHECK(m_pAssemblyIdentity != NULL);
  179. IFW32FALSE_EXIT(
  180. ::SxspSetAssemblyIdentityAttributeValue(
  181. SXSP_SET_ASSEMBLY_IDENTITY_ATTRIBUTE_VALUE_FLAG_OVERWRITE_EXISTING,
  182. m_pAssemblyIdentity,
  183. &s_IdentityAttribute_language,
  184. rbuff,
  185. rbuff.Cch()));
  186. FN_EPILOG
  187. }
  188. BOOL
  189. CAssemblyReference::SetLanguage(
  190. PCWSTR String,
  191. SIZE_T Cch
  192. )
  193. {
  194. FN_PROLOG_WIN32
  195. INTERNAL_ERROR_CHECK(m_pAssemblyIdentity != NULL);
  196. PARAMETER_CHECK_INTERNAL((Cch == 0) || (String != NULL));
  197. IFW32FALSE_EXIT(
  198. ::SxspSetAssemblyIdentityAttributeValue(
  199. SXSP_SET_ASSEMBLY_IDENTITY_ATTRIBUTE_VALUE_FLAG_OVERWRITE_EXISTING,
  200. m_pAssemblyIdentity,
  201. &s_IdentityAttribute_language,
  202. String,
  203. Cch));
  204. FN_EPILOG
  205. }
  206. BOOL
  207. CAssemblyReference::ClearLanguage()
  208. {
  209. FN_PROLOG_WIN32
  210. INTERNAL_ERROR_CHECK(m_pAssemblyIdentity != NULL);
  211. IFW32FALSE_EXIT(::SxspRemoveAssemblyIdentityAttribute(SXSP_REMOVE_ASSEMBLY_IDENTITY_ATTRIBUTE_FLAG_NOT_FOUND_SUCCEEDS, m_pAssemblyIdentity, &s_IdentityAttribute_language));
  212. FN_EPILOG
  213. }
  214. BOOL
  215. CAssemblyReference::IsLanguageWildcarded(
  216. bool &rfWildcarded
  217. ) const
  218. {
  219. FN_PROLOG_WIN32
  220. const WCHAR *Value = NULL;
  221. SIZE_T Cch = 0;
  222. rfWildcarded = false;
  223. INTERNAL_ERROR_CHECK(m_pAssemblyIdentity != NULL);
  224. IFW32FALSE_EXIT(::SxspGetAssemblyIdentityAttributeValue(SXSP_GET_ASSEMBLY_IDENTITY_ATTRIBUTE_VALUE_FLAG_NOT_FOUND_RETURNS_NULL, m_pAssemblyIdentity, &s_IdentityAttribute_language, &Value, &Cch));
  225. if (Cch == 1)
  226. {
  227. INTERNAL_ERROR_CHECK(Value != NULL);
  228. if (Value[0] == L'*')
  229. rfWildcarded = true;
  230. }
  231. FN_EPILOG
  232. }
  233. BOOL
  234. CAssemblyReference::IsProcessorArchitectureWildcarded(
  235. bool &rfWildcarded
  236. ) const
  237. {
  238. BOOL fSuccess = FALSE;
  239. FN_TRACE_WIN32(fSuccess);
  240. const WCHAR *Value = NULL;
  241. SIZE_T Cch = 0;
  242. rfWildcarded = false;
  243. INTERNAL_ERROR_CHECK(m_pAssemblyIdentity != NULL);
  244. IFW32FALSE_EXIT(::SxspGetAssemblyIdentityAttributeValue(SXSP_GET_ASSEMBLY_IDENTITY_ATTRIBUTE_VALUE_FLAG_NOT_FOUND_RETURNS_NULL, m_pAssemblyIdentity, &s_IdentityAttribute_processorArchitecture, &Value, &Cch));
  245. if (Cch == 1)
  246. {
  247. INTERNAL_ERROR_CHECK(Value != NULL);
  248. if (Value[0] == L'*')
  249. rfWildcarded = true;
  250. }
  251. fSuccess = TRUE;
  252. Exit:
  253. return fSuccess;
  254. }
  255. BOOL
  256. CAssemblyReference::IsProcessorArchitectureX86(
  257. bool &rfX86
  258. ) const
  259. {
  260. FN_PROLOG_WIN32
  261. const WCHAR *Value = NULL;
  262. SIZE_T Cch = 0;
  263. rfX86 = false;
  264. INTERNAL_ERROR_CHECK(m_pAssemblyIdentity != NULL);
  265. IFW32FALSE_EXIT(::SxspGetAssemblyIdentityAttributeValue(SXSP_GET_ASSEMBLY_IDENTITY_ATTRIBUTE_VALUE_FLAG_NOT_FOUND_RETURNS_NULL, m_pAssemblyIdentity, &s_IdentityAttribute_processorArchitecture, &Value, &Cch));
  266. if (Cch == 3)
  267. {
  268. INTERNAL_ERROR_CHECK(Value != NULL);
  269. if (((Value[0] == L'x') || (Value[0] == L'X')) &&
  270. (Value[1] == L'8') &&
  271. (Value[2] == L'6'))
  272. rfX86 = true;
  273. }
  274. FN_EPILOG
  275. }
  276. BOOL
  277. CAssemblyReference::GetProcessorArchitecture(
  278. PCWSTR &rString,
  279. SIZE_T &rCch
  280. ) const
  281. {
  282. FN_PROLOG_WIN32
  283. IFW32FALSE_EXIT(
  284. ::SxspGetAssemblyIdentityAttributeValue(
  285. SXSP_GET_ASSEMBLY_IDENTITY_ATTRIBUTE_VALUE_FLAG_NOT_FOUND_RETURNS_NULL,
  286. m_pAssemblyIdentity,
  287. &s_IdentityAttribute_processorArchitecture,
  288. &rString,
  289. &rCch));
  290. FN_EPILOG
  291. }
  292. BOOL
  293. CAssemblyReference::SetProcessorArchitecture(
  294. const WCHAR *String,
  295. SIZE_T Cch
  296. )
  297. {
  298. BOOL fSuccess = FALSE;
  299. FN_TRACE_WIN32(fSuccess);
  300. PARAMETER_CHECK((String != NULL) || (Cch == 0));
  301. INTERNAL_ERROR_CHECK(m_pAssemblyIdentity != NULL);
  302. IFW32FALSE_EXIT(
  303. ::SxspSetAssemblyIdentityAttributeValue(
  304. SXSP_SET_ASSEMBLY_IDENTITY_ATTRIBUTE_VALUE_FLAG_OVERWRITE_EXISTING,
  305. m_pAssemblyIdentity,
  306. &s_IdentityAttribute_processorArchitecture,
  307. String,
  308. Cch));
  309. fSuccess = TRUE;
  310. Exit:
  311. return fSuccess;
  312. }
  313. BOOL
  314. CAssemblyReference::SetProcessorArchitecture(
  315. IN const CBaseStringBuffer &rbuffProcessorArchitecture
  316. )
  317. {
  318. return this->SetProcessorArchitecture(rbuffProcessorArchitecture, rbuffProcessorArchitecture.Cch());
  319. }
  320. BOOL
  321. CAssemblyReference::Assign(
  322. const CAssemblyReference &r
  323. )
  324. {
  325. BOOL fSuccess = FALSE;
  326. FN_TRACE_WIN32(fSuccess);
  327. PASSEMBLY_IDENTITY IdentityCopy = NULL;
  328. //INTERNAL_ERROR_CHECK(m_pAssemblyIdentity != NULL);
  329. INTERNAL_ERROR_CHECK(r.m_pAssemblyIdentity != NULL);
  330. IFW32FALSE_EXIT(::SxsDuplicateAssemblyIdentity(0, r.m_pAssemblyIdentity, &IdentityCopy));
  331. ::SxsDestroyAssemblyIdentity(m_pAssemblyIdentity);
  332. m_pAssemblyIdentity = IdentityCopy;
  333. // NTRAID#NTBUG9 - 571856 - 2002/03/26 - xiaoyuw
  334. // set NULL to pointer after it is taken over
  335. // IdentityCopy = NULL;
  336. fSuccess = TRUE;
  337. Exit:
  338. return fSuccess;
  339. }
  340. //dupilicate the input parameter
  341. BOOL
  342. CAssemblyReference::SetAssemblyIdentity(
  343. PCASSEMBLY_IDENTITY pAssemblyIdentitySource
  344. )
  345. {
  346. BOOL fSuccess = FALSE;
  347. FN_TRACE_WIN32(fSuccess);
  348. PASSEMBLY_IDENTITY TempAssemblyIdentity = NULL;
  349. PARAMETER_CHECK(pAssemblyIdentitySource != NULL);
  350. INTERNAL_ERROR_CHECK(m_pAssemblyIdentity != NULL); // you should have initialized to start with...
  351. IFW32FALSE_EXIT(::SxsDuplicateAssemblyIdentity(
  352. 0,
  353. pAssemblyIdentitySource,
  354. &TempAssemblyIdentity));
  355. ::SxsDestroyAssemblyIdentity(m_pAssemblyIdentity);
  356. m_pAssemblyIdentity = TempAssemblyIdentity;
  357. // NTRAID#NTBUG9 - 571856 - 2002/03/26 - xiaoyuw
  358. // set NULL to pointer after it is taken over
  359. // TempAssemblyIdentity = NULL;
  360. fSuccess = TRUE;
  361. Exit:
  362. return fSuccess;
  363. }
  364. BOOL
  365. CAssemblyReference::GetPublicKeyToken(
  366. CBaseStringBuffer *pbuffPublicKeyToken,
  367. BOOL &rfHasPublicKeyToken
  368. ) const
  369. {
  370. FN_PROLOG_WIN32
  371. PCWSTR wchString = NULL;
  372. SIZE_T cchString = NULL;
  373. rfHasPublicKeyToken = FALSE;
  374. if (pbuffPublicKeyToken != NULL)
  375. pbuffPublicKeyToken->Clear();
  376. IFW32FALSE_EXIT(
  377. ::SxspGetAssemblyIdentityAttributeValue(
  378. SXSP_GET_ASSEMBLY_IDENTITY_ATTRIBUTE_VALUE_FLAG_NOT_FOUND_RETURNS_NULL,
  379. m_pAssemblyIdentity,
  380. &s_IdentityAttribute_publicKeyToken,
  381. &wchString,
  382. &cchString));
  383. if (cchString != 0)
  384. {
  385. rfHasPublicKeyToken = TRUE;
  386. if (pbuffPublicKeyToken != NULL)
  387. IFW32FALSE_EXIT(pbuffPublicKeyToken->Win32Assign(wchString, cchString));
  388. }
  389. FN_EPILOG
  390. }
  391. BOOL CAssemblyReference::SetPublicKeyToken(
  392. const CBaseStringBuffer &rbuffPublicKeyToken
  393. )
  394. {
  395. return this->SetPublicKeyToken(rbuffPublicKeyToken, rbuffPublicKeyToken.Cch());
  396. }
  397. BOOL CAssemblyReference::SetPublicKeyToken(
  398. PCWSTR pszPublicKeyToken,
  399. SIZE_T cchPublicKeyToken
  400. )
  401. {
  402. BOOL bSuccess = FALSE;
  403. FN_TRACE_WIN32( bSuccess );
  404. PARAMETER_CHECK( (pszPublicKeyToken != NULL ) || ( cchPublicKeyToken == 0 ) );
  405. INTERNAL_ERROR_CHECK(m_pAssemblyIdentity != NULL);
  406. IFW32FALSE_EXIT(
  407. ::SxspSetAssemblyIdentityAttributeValue(
  408. SXSP_SET_ASSEMBLY_IDENTITY_ATTRIBUTE_VALUE_FLAG_OVERWRITE_EXISTING,
  409. m_pAssemblyIdentity,
  410. &s_IdentityAttribute_publicKeyToken,
  411. pszPublicKeyToken,
  412. cchPublicKeyToken));
  413. bSuccess = TRUE;
  414. Exit:
  415. return bSuccess;
  416. }