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.

511 lines
18 KiB

  1. #if !defined(_FUSION_ID_IDP_H_INCLUDED_)
  2. #define _FUSION_ID_IDP_H_INCLUDED_
  3. /*++
  4. Copyright (c) Microsoft Corporation
  5. Module Name:
  6. idp.h
  7. Abstract:
  8. private definitions for assembly identity
  9. Author:
  10. Michael Grier (MGrier) 7/27/2000
  11. Revision History:
  12. --*/
  13. #pragma once
  14. #include "debmacro.h"
  15. #include "fusiontrace.h"
  16. #include "fusionhashstring.h"
  17. #include "fusionheap.h"
  18. #include "util.h"
  19. #include <sxstypes.h>
  20. #include <sxsapi.h>
  21. //
  22. // Power of two to which to round the number of allocated attribute
  23. // pointers.
  24. //
  25. #define ROUNDING_FACTOR_BITS (3)
  26. #define WILDCARD_CHAR '*'
  27. //
  28. // Note! Do not change this algorithm lightly. Encoded identities stored in the
  29. // filesystem contain hashes using it. Actually, just do not change it.
  30. //
  31. #define HASH_ALGORITHM HASH_STRING_ALGORITHM_X65599
  32. typedef struct _ASSEMBLY_IDENTITY_NAMESPACE {
  33. ULONG Hash;
  34. DWORD Flags;
  35. SIZE_T NamespaceCch;
  36. const WCHAR *Namespace;
  37. } ASSEMBLY_IDENTITY_NAMESPACE, *PASSEMBLY_IDENTITY_NAMESPACE;
  38. typedef const ASSEMBLY_IDENTITY_NAMESPACE *PCASSEMBLY_IDENTITY_NAMESPACE;
  39. //
  40. // Internal-use ASSEMBLY_IDENTITY_ATTRIBUTE struct that
  41. // also contains the hash of the attribute definition.
  42. //
  43. typedef struct _INTERNAL_ASSEMBLY_IDENTITY_ATTRIBUTE {
  44. // NOTE!!! It is very important that the Attribute member appear first in this struct;
  45. // there are several places in the code that make this assumption. If it is not true,
  46. // the code will break!
  47. // Note also that the Attribute's namespace string is actually allocated in common
  48. // for all attributes with the same namespace.
  49. ASSEMBLY_IDENTITY_ATTRIBUTE Attribute;
  50. PCASSEMBLY_IDENTITY_NAMESPACE Namespace;
  51. ULONG NamespaceAndNameHash;
  52. ULONG WholeAttributeHash;
  53. ULONG RefCount;
  54. } INTERNAL_ASSEMBLY_IDENTITY_ATTRIBUTE, *PINTERNAL_ASSEMBLY_IDENTITY_ATTRIBUTE;
  55. C_ASSERT(FIELD_OFFSET(INTERNAL_ASSEMBLY_IDENTITY_ATTRIBUTE, Attribute) == 0);
  56. typedef const INTERNAL_ASSEMBLY_IDENTITY_ATTRIBUTE *PCINTERNAL_ASSEMBLY_IDENTITY_ATTRIBUTE;
  57. #define ASSEMBLY_IDENTITY_INTERNAL_FLAG_ATTRIBUTE_POINTERS_IN_SEPARATE_ALLOCATION (0x00000001)
  58. #define ASSEMBLY_IDENTITY_INTERNAL_FLAG_SINGLE_ALLOCATION_FOR_EVERYTHING (0x00000002)
  59. #define ASSEMBLY_IDENTITY_INTERNAL_FLAG_NAMESPACE_POINTERS_IN_SEPARATE_ALLOCATION (0x00000004)
  60. //
  61. // Revelation of the ASSEMBLY_IDENTITY struct:
  62. //
  63. typedef struct _ASSEMBLY_IDENTITY {
  64. DWORD Flags;
  65. ULONG InternalFlags;
  66. ULONG Type;
  67. ULONG Hash;
  68. ULONG AttributeCount;
  69. ULONG AttributeArraySize; // preallocated a little larger so that we don't have to keep growing
  70. ULONG NamespaceCount;
  71. ULONG NamespaceArraySize;
  72. BOOL HashDirty;
  73. PCINTERNAL_ASSEMBLY_IDENTITY_ATTRIBUTE *AttributePointerArray;
  74. PCASSEMBLY_IDENTITY_NAMESPACE *NamespacePointerArray;
  75. } ASSEMBLY_IDENTITY;
  76. //
  77. // Header for encoded/serialized assembly identities:
  78. //
  79. #define ENCODED_ASSEMBLY_IDENTITY_HEADER_MAGIC ((ULONG) 'dIAE')
  80. //
  81. // Encoded assembly identity layout:
  82. //
  83. // ENCODED_ASSEMBLY_IDENTITY_HEADER
  84. // <AttributeCount hashes of the attributes, sorted by the hash value>
  85. // <NamespaceCount ENCODED_ASSEMBLY_IDENTITY_NAMESPACE_HEADER headers, each
  86. // followed by the unicode namespace value>
  87. // <AttributeCount ENCODED_ASSEMBLY_IDENTITY_ATTRIBUTE_HEADER headers, each
  88. // followed by the unicode attribute name and value>
  89. //
  90. //
  91. // e.g.
  92. //
  93. // <begin ENCODED_ASSEMBLY_IDENTITY_HEADER>
  94. // 00000000: 00000038 HeaderSize == sizeof(ENCODED_ASSEMBLY_IDENTITY_HEADER)
  95. // 00000004: 'EAId' Magic (ENCODED_ASSEMBLY_IDENTITY_HEADER_MAGIC)
  96. // 00000008: 0000014C TotalSize
  97. // 0000000C: 00000000 Flags
  98. // 00000010: 00000001 Type (1 = ASSEMBLY_IDENTITY_TYPE_DEFINITION)
  99. // 00000014: 00000000 EncodingFlags
  100. // 00000018: 00000001 HashAlgorithm (1 = HASH_STRING_ALGORITHM_X65599)
  101. // 0000001C: ???????? Logical hash value of entire identity based on hash algorithm
  102. // (algorithm described in more detail below...)
  103. // 00000020: 00000003 AttributeCount
  104. // 00000024: 00000002 NamespaceCount
  105. // 00000028: 00000000 ReservedMustBeZero1
  106. // 0000002C: 00000000 ReservedMustBeZero2
  107. // 00000030: 00000000 00000000 ReservedMustBeZero3
  108. // 00000038: 00000000 00000000 ReservedMustBeZero4
  109. // <end ENCODED_ASSEMBLY_IDENTITY_HEADER>
  110. // <begin sorted attribute hash list>
  111. // 00000040: xxxxxxxx hash of attribute #1
  112. // 00000044: yyyyyyyy hash of attribute #0 - note that yyyyyyyy >= xxxxxxxx
  113. // 00000048: zzzzzzzz hash of attribute #2 - note that zzzzzzzz >= yyyyyyyy
  114. // <end sorted attribute hash list>
  115. // <begin namespace length list>
  116. // 0000004C: 00000015 length (in Unicode chars) of namespace #1 - "http://www.amazon.com" - 21 chars = 0x00000015
  117. // 00000050: 00000018 length (in Unicode chars) of namespace #2 - "http://www.microsoft.com" - 24 chars = 0x00000018
  118. // <end namespace length list>
  119. // <begin attribute headers>
  120. // <begin ENCODED_ASSEMBLY_IDENTITY_ATTRIBUTE_HEADER>
  121. // 00000054: 00000001 NamespaceIndex: 1 (http://www.amazon.com)
  122. // 00000058: 00000004 Name length ("name" - 4 chars = 0x00000004)
  123. // 0000005C: 00000006 Value length ("foobar" - 6 chars = 0x00000006)
  124. // <end ENCODED_ASSEMBLY_IDENTITY_ATTRIBUTE_HEADER>
  125. // <begin ENCODED_ASSEMBLY_IDENTITY_ATTRIBUTE_HEADER>
  126. // 00000060: 00000002 NamespaceIndex: 2 (http://www.microsoft.com)
  127. // 00000064: 00000004 Name length ("guid" - 4 chars = 0x00000004)
  128. // 00000068: 00000026 Value length ("{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}" - 38 chars = 0x00000026)
  129. // <end ENCODED_ASSEMBLY_IDENTITY_ATTRIBUTE_HEADER>
  130. // <begin ENCODED_ASSEMBLY_IDENTITY_ATTRIBUTE_HEADER>
  131. // 0000006C: 00000002 NamespaceIndex: 2 (http://www.microsoft.com)
  132. // 00000070: 00000004 Name length ("type" - 4 chars = 0x00000004)
  133. // 00000074: 00000005 Value length ("win32" - 5 chars = 0x00000005)
  134. // <end ENCODED_ASSEMBLY_IDENTITY_ATTRIBUTE_HEADER>
  135. // <end attribute headers>
  136. // <begin namespace strings>
  137. // 00000078: "http://www.amazon.com"
  138. // 000000A2: "http://www.microsoft.com"
  139. // <end namespace strings>
  140. // <begin attribute values - names and values for each attribute in series>
  141. // 000000D2: "name"
  142. // 000000DA: "foobar"
  143. // 000000E6: "guid"
  144. // 000000EE: "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}"
  145. // 0000013A: "type"
  146. // 00000142: "win32"
  147. // <end attribute values>
  148. // 0000014C:
  149. //
  150. // Computing the whole identity hash:
  151. //
  152. // The hash of the entire encoded identity is not the hash of the binary form, but
  153. // rather is a combination of the hashes for the various components.
  154. //
  155. // For any Unicode character string, its hash is computed according to HashAlgorithm.
  156. // Currently this must be HASH_STRING_ALGORITHM_X65599 which is a multiply-and-
  157. // accumulate algorithm, implemented essentially as follows:
  158. //
  159. // HashValue = 0;
  160. // for (i=0; i<Chars; i++)
  161. // HashValue = (HashValue * 65599) + OptionalToUpper(String[i]);
  162. //
  163. // Note that the characters are converted to upper case. This is somewhat in
  164. // conflict with the Unicode recommendation to convert to lower case for case
  165. // insensitive operations, but it is what the rest of the Windows NT system
  166. // does, so consistency matters more than doing the "right thing".
  167. //
  168. // Note also that no trailing null characters are included in the hash. This
  169. // is significant because of the fact that applying the loop to another character
  170. // even though its value is zero will significantly change the hash value.
  171. //
  172. // Namespaces and attribute names are case sensitive, derived from the fact
  173. // that they appear in case sensitive contexts in the real world. This is
  174. // unfortunate, but simpler in many ways.
  175. //
  176. // Assembly identity attributes are composed of a triple of:
  177. // - Namespace URI (e.g. http://www.microsoft.com/schemas/side-by-side)
  178. // - Name (e.g. "publicKey")
  179. // - Value (case insensitive Unicode string)
  180. //
  181. // The hash of an attribute is computed by computing the hash of the three
  182. // strings, and then combining them as:
  183. //
  184. // AttributeHashValue = (((NamespaceHash * 65599) + NameHash) * 65599) + ValueHash
  185. //
  186. // Now, sort the attributes based first on namespace, then on name then on
  187. // value (case sensitive, case sensitive and case insensitive respectively),
  188. // and combine their hashes as follows:
  189. //
  190. // IdentityHash = 0;
  191. // for (i=0; i<AttributeCount; i++)
  192. // IdentityHash = (IdentityHash * 65599) + AttributeHashes[i];
  193. //
  194. // IdentityHash is the value stored in the encoded header.
  195. //
  196. // The attribute hash array stored in the encoded data is the attribute
  197. // hashes as described above. The interesting thing is that they are stored
  198. // in order of ascending hash value, not in the canonical ordering for
  199. // attributes.
  200. //
  201. // This is because a common scenario is to find an identity which has a
  202. // superset of a given identity. While the actual attributes have to
  203. // be consulted to verify that the candidate is a true subset, non-
  204. // matches can be very quickly found by sorting both lists of hash
  205. // values and first looping over the smaller reference list, then
  206. // in a single pass walking the larger definition list. Attributes present
  207. // in one but not in the other will be immediately noticable due to
  208. // the missing hashes.
  209. //
  210. // As always with hashes, just because an encoded identity contains a
  211. // superset of the hash values in your candidate assembly reference,
  212. // it does not mean that the actual values appear and you must perform
  213. // real character string comparisons to verify containment.
  214. //
  215. #include <pshpack4.h>
  216. typedef struct _ENCODED_ASSEMBLY_IDENTITY_HEADER {
  217. ULONG HeaderSize; // bytes just in the header
  218. ULONG Magic;
  219. ULONG TotalSize; // bytes for the whole encoded thing
  220. DWORD Flags; // as defined for assembly identity flags
  221. ULONG Type; // type of identity - def, ref or wildcard
  222. ULONG EncodingFlags; // flags describing the encoding itself
  223. ULONG HashAlgorithm; // Algorithm ID for the hashes stored in the identity
  224. ULONG Hash; // Hash value of the entire identity
  225. ULONG AttributeCount; // number of attributes
  226. ULONG NamespaceCount; // number of distinct namespaces
  227. ULONG ReservedMustBeZero1;
  228. ULONG ReservedMustBeZero2;
  229. ULONGLONG ReservedMustBeZero3;
  230. ULONGLONG ReservedMustBeZero4;
  231. } ENCODED_ASSEMBLY_IDENTITY_HEADER, *PENCODED_ASSEMBLY_IDENTITY_HEADER;
  232. typedef const ENCODED_ASSEMBLY_IDENTITY_HEADER *PCENCODED_ASSEMBLY_IDENTITY_HEADER;
  233. typedef struct _ENCODED_ASSEMBLY_IDENTITY_ATTRIBUTE_HEADER {
  234. ULONG NamespaceIndex; // number of the namespace for this attribute
  235. ULONG NameCch; // size in Unicode characters of the name immediately following the
  236. // namespace
  237. ULONG ValueCch; // size in Unicode characters of the value immediately following the
  238. // name.
  239. } ENCODED_ASSEMBLY_IDENTITY_ATTRIBUTE_HEADER, *PENCODED_ASSEMBLY_IDENTITY_ATTRIBUTE_HEADER;
  240. typedef const ENCODED_ASSEMBLY_IDENTITY_ATTRIBUTE_HEADER *PCENCODED_ASSEMBLY_IDENTITY_ATTRIBUTE_HEADER;
  241. #include <poppack.h>
  242. #define SXSP_VALIDATE_ASSEMBLY_IDENTITY_FLAGS_MAY_BE_NULL (0x00000001)
  243. BOOL
  244. SxspValidateAssemblyIdentity(
  245. IN DWORD Flags,
  246. IN PCASSEMBLY_IDENTITY AssemblyIdentity
  247. );
  248. BOOL
  249. SxspValidateAssemblyIdentityAttributeNamespace(
  250. IN DWORD Flags,
  251. IN const WCHAR *Namespace,
  252. IN SIZE_T NamespaceCch
  253. );
  254. BOOL
  255. SxspValidateAssemblyIdentityAttributeName(
  256. IN DWORD Flags,
  257. IN const WCHAR *Name,
  258. IN SIZE_T NameCch
  259. );
  260. #define SXSP_VALIDATE_ASSEMBLY_IDENTITY_ATTRIBUTE_VALUE_FLAG_WILDCARDS_PERMITTED (0x00000001)
  261. BOOL
  262. SxspValidateAssemblyIdentityAttributeValue(
  263. IN DWORD Flags,
  264. IN const WCHAR *Value,
  265. IN SIZE_T ValueCch
  266. );
  267. BOOL
  268. SxspComputeInternalAssemblyIdentityAttributeBytesRequired(
  269. IN DWORD Flags,
  270. IN const WCHAR *Name,
  271. IN SIZE_T NameCch,
  272. IN const WCHAR *Value,
  273. IN SIZE_T ValueCch,
  274. OUT SIZE_T *BytesRequiredOut
  275. );
  276. BOOL
  277. SxspComputeAssemblyIdentityAttributeBytesRequired(
  278. IN DWORD Flags,
  279. IN PCASSEMBLY_IDENTITY_ATTRIBUTE Source,
  280. OUT SIZE_T *BytesRequiredOut
  281. );
  282. #define SXSP_FIND_ASSEMBLY_IDENTITY_NAMESPACE_IN_ARRAY_FLAG_ADD_IF_NOT_FOUND (0x00000001)
  283. BOOL
  284. SxspFindAssemblyIdentityNamespaceInArray(
  285. IN DWORD Flags,
  286. IN OUT PCASSEMBLY_IDENTITY_NAMESPACE **NamespacePointerArrayPtr,
  287. IN OUT ULONG *NamespaceArraySizePtr,
  288. IN OUT ULONG *NamespaceCountPtr,
  289. IN const WCHAR *Namespace,
  290. IN SIZE_T NamespaceCch,
  291. OUT PCASSEMBLY_IDENTITY_NAMESPACE *NamespaceOut
  292. );
  293. #define SXSP_FIND_ASSEMBLY_IDENTITY_NAMESPACE_FLAG_ADD_IF_NOT_FOUND (0x00000001)
  294. BOOL
  295. SxspFindAssemblyIdentityNamespace(
  296. IN DWORD Flags,
  297. IN struct _ASSEMBLY_IDENTITY* AssemblyIdentity,
  298. IN const WCHAR *Namespace,
  299. IN SIZE_T NamespaceCch,
  300. OUT PCASSEMBLY_IDENTITY_NAMESPACE *NamespaceOut
  301. );
  302. BOOL
  303. SxspAllocateAssemblyIdentityNamespace(
  304. IN DWORD Flags,
  305. IN const WCHAR *NamespaceString,
  306. IN SIZE_T NamespaceCch,
  307. IN ULONG NamespaceHash,
  308. OUT PCASSEMBLY_IDENTITY_NAMESPACE *NamespaceOut
  309. );
  310. VOID
  311. SxspDeallocateAssemblyIdentityNamespace(
  312. IN PCASSEMBLY_IDENTITY_NAMESPACE Namespace
  313. );
  314. void
  315. SxspPopulateInternalAssemblyIdentityAttribute(
  316. IN DWORD Flags,
  317. IN PCASSEMBLY_IDENTITY_NAMESPACE Namespace,
  318. IN const WCHAR *Name,
  319. IN SIZE_T NameCch,
  320. IN const WCHAR *Value,
  321. IN SIZE_T ValueCch,
  322. OUT PINTERNAL_ASSEMBLY_IDENTITY_ATTRIBUTE Destination
  323. );
  324. BOOL
  325. SxspAllocateInternalAssemblyIdentityAttribute(
  326. IN DWORD Flags,
  327. IN PCASSEMBLY_IDENTITY_NAMESPACE Namespace,
  328. IN const WCHAR *Name,
  329. IN SIZE_T NameCch,
  330. IN const WCHAR *Value,
  331. IN SIZE_T ValueCch,
  332. OUT PCINTERNAL_ASSEMBLY_IDENTITY_ATTRIBUTE *Destination
  333. );
  334. VOID
  335. SxspCleanUpAssemblyIdentityNamespaceIfNotReferenced(
  336. IN DWORD Flags,
  337. IN struct _ASSEMBLY_IDENTITY* AssemblyIdentity,
  338. IN PCASSEMBLY_IDENTITY_NAMESPACE Namespace
  339. );
  340. VOID
  341. SxspDeallocateInternalAssemblyIdentityAttribute(
  342. PINTERNAL_ASSEMBLY_IDENTITY_ATTRIBUTE Attribute
  343. );
  344. int
  345. __cdecl
  346. SxspCompareInternalAttributesForQsort(
  347. const void *elem1,
  348. const void *elem2
  349. );
  350. int
  351. __cdecl
  352. SxspCompareULONGsForQsort(
  353. const void *elem1,
  354. const void *elem2
  355. );
  356. BOOL
  357. SxspCompareAssemblyIdentityAttributeLists(
  358. DWORD Flags,
  359. ULONG AttributeCount,
  360. PCINTERNAL_ASSEMBLY_IDENTITY_ATTRIBUTE *List1,
  361. PCINTERNAL_ASSEMBLY_IDENTITY_ATTRIBUTE *List2,
  362. ULONG *ComparisonResultOut
  363. );
  364. BOOL
  365. SxspHashInternalAssemblyIdentityAttributes(
  366. DWORD Flags,
  367. ULONG Count,
  368. PCINTERNAL_ASSEMBLY_IDENTITY_ATTRIBUTE *Attributes,
  369. ULONG *HashOut
  370. );
  371. BOOL
  372. SxspCopyInternalAssemblyIdentityAttributeOut(
  373. DWORD Flags,
  374. PCINTERNAL_ASSEMBLY_IDENTITY_ATTRIBUTE Attribute,
  375. SIZE_T BufferSize,
  376. PASSEMBLY_IDENTITY_ATTRIBUTE DestinationBuffer,
  377. SIZE_T *BytesCopiedOrRequired
  378. );
  379. BOOL
  380. SxspIsInternalAssemblyIdentityAttribute(
  381. IN DWORD Flags,
  382. IN PCINTERNAL_ASSEMBLY_IDENTITY_ATTRIBUTE Attribute,
  383. IN const WCHAR *Namespace,
  384. IN SIZE_T NamespaceCch,
  385. IN const WCHAR *Name,
  386. IN SIZE_T NameCch,
  387. OUT BOOL *EqualsOut
  388. );
  389. #define SXSP_COMPUTE_INTERNAL_ASSEMBLY_IDENTITY_ATTRIBUTE_ENCODED_TEXTUAL_SIZE_FLAG_VALUE_ONLY (0x00000001)
  390. #define SXSP_COMPUTE_INTERNAL_ASSEMBLY_IDENTITY_ATTRIBUTE_ENCODED_TEXTUAL_SIZE_FLAG_OMIT_QUOTES (0x00000002)
  391. BOOL
  392. SxspComputeInternalAssemblyIdentityAttributeEncodedTextualSize(
  393. IN DWORD Flags,
  394. IN PCINTERNAL_ASSEMBLY_IDENTITY_ATTRIBUTE Attribute,
  395. OUT SIZE_T *BytesOut
  396. );
  397. #define SXSP_LOCATE_INTERNAL_ASSEMBLY_IDENTITY_ATTRIBUTE_FLAG_MATCH_NAMESPACE (0x00000001)
  398. #define SXSP_LOCATE_INTERNAL_ASSEMBLY_IDENTITY_ATTRIBUTE_FLAG_MATCH_NAME (0x00000002)
  399. #define SXSP_LOCATE_INTERNAL_ASSEMBLY_IDENTITY_ATTRIBUTE_FLAG_MATCH_VALUE (0x00000004)
  400. PCINTERNAL_ASSEMBLY_IDENTITY_ATTRIBUTE
  401. SxspLocateInternalAssemblyIdentityAttribute(
  402. IN DWORD Flags,
  403. IN PCASSEMBLY_IDENTITY AssemblyIdentity,
  404. IN PCASSEMBLY_IDENTITY_ATTRIBUTE Attribute,
  405. OUT ULONG *LastIndexSearched OPTIONAL
  406. );
  407. SIZE_T
  408. __fastcall
  409. SxspComputeQuotedStringSize(
  410. IN const WCHAR *StringIn,
  411. IN SIZE_T Cch
  412. );
  413. VOID
  414. SxspDbgPrintInternalAssemblyIdentityAttribute(
  415. DWORD dwflags,
  416. PCINTERNAL_ASSEMBLY_IDENTITY_ATTRIBUTE Attribute
  417. );
  418. VOID
  419. SxspDbgPrintInternalAssemblyIdentityAttributes(
  420. DWORD dwflags,
  421. ULONG AttributeCount,
  422. PCINTERNAL_ASSEMBLY_IDENTITY_ATTRIBUTE const *Attributes
  423. );
  424. VOID
  425. SxspDbgPrintAssemblyIdentityAttribute(
  426. DWORD dwflags,
  427. PCASSEMBLY_IDENTITY_ATTRIBUTE Attribute
  428. );
  429. VOID
  430. SxspDbgPrintAssemblyIdentityAttributes(
  431. DWORD dwflags,
  432. ULONG AttributeCount,
  433. PCASSEMBLY_IDENTITY_ATTRIBUTE const *Attributes
  434. );
  435. BOOL
  436. SxspEnsureAssemblyIdentityHashIsUpToDate(
  437. DWORD dwFlags,
  438. PCASSEMBLY_IDENTITY AssemblyIdentity
  439. );
  440. #endif