1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4// ============================================================
5//
6// BaseAssemblySpec.h
7//
8
9
10//
11// Declares the BaseAssemblySpec class
12//
13// ============================================================
14
15#ifndef __BASE_ASSEMBLY_SPEC_H__
16#define __BASE_ASSEMBLY_SPEC_H__
17
18class StackingAllocator;
19
20// a class representing assembly name in Loader
21class BaseAssemblySpec
22{
23protected:
24 AssemblyMetaDataInternal m_context;
25 LPCSTR m_pAssemblyName;
26 PBYTE m_pbPublicKeyOrToken;
27 DWORD m_cbPublicKeyOrToken;
28 DWORD m_dwFlags; // CorAssemblyFlags
29 LPCWSTR m_wszCodeBase; // URL to the code
30 LPCSTR m_szWinRtTypeNamespace;
31 LPCSTR m_szWinRtTypeClassName;
32 ICLRPrivBinder *m_pHostBinder;
33 int m_ownedFlags;
34 ICLRPrivBinder *m_pBindingContext;
35
36public:
37 enum
38 {
39 NAME_OWNED = 0x01,
40 PUBLIC_KEY_OR_TOKEN_OWNED = 0x02,
41 CODE_BASE_OWNED = 0x04,
42 LOCALE_OWNED = 0x08,
43 CODEBASE_OWNED = 0x10,
44 WINRT_TYPE_NAME_OWNED = 0x20,
45 // Set if ParseName() returned illegal textual identity.
46 // Cannot process the string any further.
47 BAD_NAME_OWNED = 0x40,
48 ALL_OWNED = 0xFF,
49 };
50
51 BaseAssemblySpec();
52 ~BaseAssemblySpec();
53
54 HRESULT Init(LPCSTR pAssemblyName,
55 const AssemblyMetaDataInternal* pContext,
56 const BYTE * pbPublicKeyOrToken, DWORD cbPublicKeyOrToken,
57 DWORD dwFlags);
58
59 HRESULT Init(mdToken tkAssemblyRef, IMDInternalImport *pImport);
60 HRESULT Init(mdAssembly tkAssemblyRef, IMetaDataAssemblyImport* pImport);
61 HRESULT Init(LPCSTR pAssemblyDisplayName);
62
63 HRESULT Init(IAssemblyName *pName);
64
65 // Note that this method does not clone the fields!
66 VOID CopyFrom(const BaseAssemblySpec *pSpec);
67
68 VOID CloneFields(int flags=ALL_OWNED);
69 VOID CloneFieldsToLoaderHeap(int flags, LoaderHeap *pHeap, AllocMemTracker *pamTracker);
70 VOID CloneFieldsToStackingAllocator(StackingAllocator* alloc);
71
72 inline void SetBindingContext(ICLRPrivBinder *pBindingContext)
73 {
74 LIMITED_METHOD_CONTRACT;
75
76 m_pBindingContext = pBindingContext;
77 }
78
79 inline ICLRPrivBinder* GetBindingContext()
80 {
81 LIMITED_METHOD_CONTRACT;
82
83 return m_pBindingContext;
84 }
85
86 BOOL IsAssemblySpecForMscorlib();
87
88 HRESULT ParseName();
89 DWORD Hash();
90
91 LPCSTR GetName() const;
92 inline void GetName(SString & ssName) const { WRAPPER_NO_CONTRACT; ssName.SetUTF8(GetName()); }
93
94 void SetName(LPCSTR szName);
95 void SetName(SString const & ssName);
96
97 LPCWSTR GetCodeBase();
98 void SetCodeBase(LPCWSTR szCodeBase);
99
100 VOID SetCulture(LPCSTR szCulture);
101
102 VOID ConvertPublicKeyToToken();
103
104 void SetContext(ASSEMBLYMETADATA* assemblyData);
105
106 inline AssemblyMetaDataInternal *GetContext() { LIMITED_METHOD_CONTRACT; return &m_context; }
107 inline AssemblyMetaDataInternal const *GetContext() const { LIMITED_METHOD_CONTRACT; return &m_context; }
108
109 BOOL IsStrongNamed() const;
110 BOOL HasPublicKey() const;
111 BOOL HasPublicKeyToken() const;
112 BOOL IsMscorlibSatellite();
113 BOOL IsMscorlibDebugSatellite();
114 BOOL IsMscorlib();
115
116 //
117 // Windows Runtime functions that could not be refactored out to AssemblySpec
118 //
119 inline LPCSTR GetWinRtTypeNamespace() const
120 {
121 LIMITED_METHOD_CONTRACT;
122 return m_szWinRtTypeNamespace;
123 }
124 inline LPCSTR GetWinRtTypeClassName() const
125 {
126 LIMITED_METHOD_CONTRACT;
127 return m_szWinRtTypeClassName;
128 }
129
130 //****************************************************************************************
131 //
132 // Creates an IAssemblyName object representing this AssemblySpec.
133 //
134 // fMustBeBindable - if set to TRUE, the resulting IAssemblyName may contain internal
135 // encodings needed to make an identity bindable (this is the case
136 // for WinRT assemblies: a representative type name is encoded as
137 // part of the assembly simple name). Be careful to ensure that
138 // encoded identities are not exposed to customers.
139 HRESULT CreateFusionName(
140 IAssemblyName **ppName,
141 BOOL fIncludeCodeBase = TRUE, /* Used by fusion only */
142 BOOL fMustBeBindable = FALSE) const;
143
144 inline BOOL IsContentType_WindowsRuntime() const
145 {
146 LIMITED_METHOD_CONTRACT;
147#ifdef FEATURE_COMINTEROP
148 return IsAfContentType_WindowsRuntime(m_dwFlags);
149#else
150 return FALSE;
151#endif
152 }
153
154 void GetEncodedName(SString & ssEncodedName) const;
155
156 // Returns true if this object uniquely identifies a single assembly;
157 // false otherwise. This will return false for Windows Runtime assemblies,
158 // as WinRT assembly names do not represent an identity. This method
159 // does not take into account additional attributes such as type namespace
160 // and name.
161 inline BOOL HasUniqueIdentity() const
162 {
163 STATIC_CONTRACT_LIMITED_METHOD;
164 return !IsContentType_WindowsRuntime();
165 }
166
167 enum CompareExFlags
168 {
169 ASC_Default = 0x00, // Default comparison policy.
170 ASC_DefinitionEquality = 0x01, // Will not treat non-bindable content types as equivalent.
171 };
172
173 BOOL CompareEx(BaseAssemblySpec *pSpec, DWORD dwCompareFlags = ASC_Default);
174 static int CompareStrings(LPCUTF8 string1, LPCUTF8 string2);
175 static BOOL RefMatchesDef(const BaseAssemblySpec* pRef, const BaseAssemblySpec* pDef);
176 static BOOL VerifyBindingString(LPCWSTR pwStr);
177
178 void GetFileOrDisplayName(DWORD flags, SString &result) const;
179
180 inline void GetPublicKey(
181 PBYTE * ppbPublicKey,
182 DWORD * pcbPublicKey) const
183 {
184 LIMITED_METHOD_CONTRACT;
185 PRECONDITION(HasPublicKey());
186 if (ppbPublicKey != nullptr)
187 {
188 *ppbPublicKey = m_pbPublicKeyOrToken;
189 }
190 if (pcbPublicKey != nullptr)
191 {
192 *pcbPublicKey = m_cbPublicKeyOrToken;
193 }
194 }
195
196 inline void GetPublicKeyToken(
197 PBYTE * ppbPublicKeyToken,
198 DWORD * pcbPublicKeyToken) const
199 {
200 LIMITED_METHOD_CONTRACT;
201 PRECONDITION(HasPublicKeyToken());
202 if (ppbPublicKeyToken != nullptr)
203 {
204 *ppbPublicKeyToken = m_pbPublicKeyOrToken;
205 }
206 if (pcbPublicKeyToken != nullptr)
207 {
208 *pcbPublicKeyToken = m_cbPublicKeyOrToken;
209 }
210 }
211
212 inline BOOL IsRetargetable() const
213 {
214 LIMITED_METHOD_CONTRACT;
215 return IsAfRetargetable(m_dwFlags);
216 }
217
218
219protected:
220 static BOOL CompareRefToDef(const BaseAssemblySpec *pRef, const BaseAssemblySpec *pDef);
221};
222
223#endif // __BASE_ASSEMBLY_SPEC_H__
224