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 | // asmman.hpp - header file for manifest-related ILASM functions |
6 | // |
7 | |
8 | #ifndef ASMMAN_HPP |
9 | #define ASMMAN_HPP |
10 | |
11 | #include "strongname.h" |
12 | #include "specstrings.h" |
13 | |
14 | struct AsmManFile |
15 | { |
16 | char* szName; |
17 | mdToken tkTok; |
18 | DWORD dwAttr; |
19 | BinStr* pHash; |
20 | BOOL m_fNew; |
21 | CustomDescrList m_CustomDescrList; |
22 | AsmManFile() |
23 | { |
24 | szName = NULL; |
25 | pHash = NULL; |
26 | m_fNew = TRUE; |
27 | } |
28 | ~AsmManFile() |
29 | { |
30 | if(szName) delete szName; |
31 | if(pHash) delete pHash; |
32 | } |
33 | int ComparedTo(AsmManFile* pX){ return strcmp(szName,pX->szName); } |
34 | }; |
35 | //typedef SORTEDARRAY<AsmManFile> AsmManFileList; |
36 | typedef FIFO<AsmManFile> AsmManFileList; |
37 | |
38 | struct AsmManAssembly |
39 | { |
40 | BOOL isRef; |
41 | BOOL isAutodetect; |
42 | char* szName; |
43 | char* szAlias; |
44 | DWORD dwAlias; |
45 | mdToken tkTok; |
46 | DWORD dwAttr; |
47 | BinStr* pPublicKey; |
48 | BinStr* pPublicKeyToken; |
49 | ULONG ulHashAlgorithm; |
50 | BinStr* pHashBlob; |
51 | BinStr* pLocale; |
52 | BOOL m_fNew; |
53 | // Security attributes |
54 | PermissionDecl* m_pPermissions; |
55 | PermissionSetDecl* m_pPermissionSets; |
56 | CustomDescrList m_CustomDescrList; |
57 | USHORT usVerMajor; |
58 | USHORT usVerMinor; |
59 | USHORT usBuild; |
60 | USHORT usRevision; |
61 | AsmManAssembly() |
62 | { |
63 | /* |
64 | usVerMajor = usVerMinor = usBuild = usRevision = 0xFFFF; |
65 | szName = szAlias = NULL; |
66 | dwAlias = dwAttr = 0; |
67 | tkTok = 0; |
68 | pPublicKey = pPublicKeyToken =pHashBlob = pLocale = NULL; |
69 | ulHashAlgorithm = 0; |
70 | m_fNew = TRUE; |
71 | isAutodetect = isRef = FALSE; |
72 | */ |
73 | } |
74 | ~AsmManAssembly() |
75 | { |
76 | if(szAlias && (szAlias != szName)) delete [] szAlias; |
77 | if(szName) delete [] szName; |
78 | if(pPublicKey) delete pPublicKey; |
79 | if(pPublicKeyToken) delete pPublicKeyToken; |
80 | if(pHashBlob) delete pHashBlob; |
81 | if(pLocale) delete pLocale; |
82 | } |
83 | int ComparedTo(AsmManAssembly* pX){ return strcmp(szAlias,pX->szAlias); } |
84 | }; |
85 | //typedef SORTEDARRAY<AsmManAssembly> AsmManAssemblyList; |
86 | typedef FIFO<AsmManAssembly> AsmManAssemblyList; |
87 | |
88 | struct AsmManComType |
89 | { |
90 | char* szName; |
91 | mdToken tkTok; |
92 | mdToken tkImpl; |
93 | DWORD dwAttr; |
94 | char* szFileName; |
95 | char* szAsmRefName; |
96 | char* szComTypeName; |
97 | mdToken tkClass; |
98 | BOOL m_fNew; |
99 | CustomDescrList m_CustomDescrList; |
100 | AsmManComType() |
101 | { |
102 | szName = szFileName = szAsmRefName = szComTypeName = NULL; |
103 | m_fNew = TRUE; |
104 | tkImpl = 0; |
105 | }; |
106 | ~AsmManComType() |
107 | { |
108 | if(szName) delete szName; |
109 | if(szFileName) delete szFileName; |
110 | }; |
111 | int ComparedTo(AsmManComType* pX){ return strcmp(szName,pX->szName); }; |
112 | }; |
113 | //typedef SORTEDARRAY<AsmManComType> AsmManComTypeList; |
114 | typedef FIFO<AsmManComType> AsmManComTypeList; |
115 | |
116 | |
117 | struct AsmManRes |
118 | { |
119 | char* szName; |
120 | char* szAlias; |
121 | mdToken tkTok; |
122 | DWORD dwAttr; |
123 | char* szFileName; |
124 | ULONG ulOffset; |
125 | BOOL m_fNew; |
126 | CustomDescrList m_CustomDescrList; |
127 | char* szAsmRefName; |
128 | AsmManRes() { szName = szAlias = szAsmRefName = szFileName = NULL; ulOffset = 0; tkTok = 0; dwAttr = 0; m_fNew = TRUE; }; |
129 | ~AsmManRes() |
130 | { |
131 | if(szAlias && (szAlias != szName)) delete szAlias; |
132 | if(szName) delete szName; |
133 | if(szFileName) delete szFileName; |
134 | if(szAsmRefName) delete szAsmRefName; |
135 | } |
136 | }; |
137 | typedef FIFO<AsmManRes> AsmManResList; |
138 | |
139 | struct AsmManModRef |
140 | { |
141 | char* szName; |
142 | mdToken tkTok; |
143 | BOOL m_fNew; |
144 | AsmManModRef() {szName = NULL; tkTok = 0; m_fNew = TRUE; }; |
145 | ~AsmManModRef() { if(szName) delete szName; }; |
146 | }; |
147 | typedef FIFO<AsmManModRef> AsmManModRefList; |
148 | |
149 | struct AsmManStrongName |
150 | { |
151 | enum AllocationState |
152 | { |
153 | NotAllocated = 0, |
154 | AllocatedBySNApi, |
155 | AllocatedByNew |
156 | }; |
157 | |
158 | BYTE *m_pbSignatureKey; |
159 | DWORD m_cbSignatureKey; |
160 | BYTE *m_pbPublicKey; |
161 | DWORD m_cbPublicKey; |
162 | BYTE *m_pbPrivateKey; |
163 | DWORD m_cbPrivateKey; |
164 | WCHAR *m_wzKeyContainer; |
165 | BOOL m_fFullSign; |
166 | |
167 | // Where has the memory pointed to by m_pbPublicKey been taken from: |
168 | AllocationState m_dwPublicKeyAllocated; |
169 | |
170 | AsmManStrongName() { ZeroMemory(this, sizeof(*this)); } |
171 | ~AsmManStrongName() |
172 | { |
173 | if (m_dwPublicKeyAllocated == AllocatedByNew) |
174 | delete [] m_pbPublicKey; |
175 | |
176 | if (m_pbPrivateKey) |
177 | delete [] m_pbPrivateKey; |
178 | |
179 | if (m_pbSignatureKey) |
180 | delete [] m_pbSignatureKey; |
181 | } |
182 | }; |
183 | |
184 | class ErrorReporter; |
185 | |
186 | class AsmMan |
187 | { |
188 | AsmManFileList m_FileLst; |
189 | AsmManComTypeList m_ComTypeLst; |
190 | AsmManResList m_ManResLst; |
191 | AsmManModRefList m_ModRefLst; |
192 | |
193 | AsmManComType* m_pCurComType; |
194 | AsmManRes* m_pCurManRes; |
195 | ErrorReporter* report; |
196 | void* m_pAssembler; |
197 | |
198 | AsmManFile* GetFileByName(__in __nullterminated char* szFileName); |
199 | AsmManAssembly* GetAsmRefByName(__in __nullterminated const char* szAsmRefName); |
200 | AsmManComType* GetComTypeByName(__in_opt __nullterminated char* szComTypeName, |
201 | __in_opt __nullterminated char* szComEnclosingTypeName = NULL); |
202 | mdToken GetComTypeTokByName(__in_opt __nullterminated char* szComTypeName, |
203 | __in_opt __nullterminated char* szComEnclosingTypeName = NULL); |
204 | |
205 | IMetaDataEmit* m_pEmitter; |
206 | |
207 | public: |
208 | IMetaDataAssemblyEmit* m_pAsmEmitter; |
209 | AsmManAssemblyList m_AsmRefLst; |
210 | AsmManAssembly* m_pAssembly; |
211 | AsmManAssembly* m_pCurAsmRef; |
212 | char* m_szScopeName; |
213 | BinStr* m_pGUID; |
214 | AsmManStrongName m_sStrongName; |
215 | // Embedded manifest resources paraphernalia: |
216 | WCHAR* m_wzMResName[MAX_MANIFEST_RESOURCES]; |
217 | DWORD m_dwMResSize[MAX_MANIFEST_RESOURCES]; |
218 | BOOL m_fMResNew[MAX_MANIFEST_RESOURCES]; |
219 | DWORD m_dwMResNum; |
220 | DWORD m_dwMResSizeTotal; |
221 | AsmMan() { m_pAssembly = NULL; m_szScopeName = NULL; m_pGUID = NULL; m_pAsmEmitter = NULL; |
222 | memset(m_wzMResName,0,sizeof(m_wzMResName)); |
223 | memset(m_dwMResSize,0,sizeof(m_dwMResSize)); |
224 | m_dwMResNum = m_dwMResSizeTotal = 0; }; |
225 | AsmMan(void* pAsm) { m_pAssembly = NULL; m_szScopeName = NULL; m_pGUID = NULL; m_pAssembler = pAsm; m_pAsmEmitter = NULL; |
226 | memset(m_wzMResName,0,sizeof(m_wzMResName)); |
227 | memset(m_dwMResSize,0,sizeof(m_dwMResSize)); |
228 | m_dwMResNum = m_dwMResSizeTotal = 0; }; |
229 | AsmMan(ErrorReporter* rpt) { m_pAssembly = NULL; m_szScopeName = NULL; m_pGUID = NULL; report = rpt; m_pAsmEmitter = NULL; |
230 | memset(m_wzMResName,0,sizeof(m_wzMResName)); |
231 | memset(m_dwMResSize,0,sizeof(m_dwMResSize)); |
232 | m_dwMResNum = m_dwMResSizeTotal = 0; }; |
233 | ~AsmMan() |
234 | { |
235 | if(m_pAssembly) delete m_pAssembly; |
236 | if(m_szScopeName) delete m_szScopeName; |
237 | if(m_pGUID) delete m_pGUID; |
238 | }; |
239 | void SetErrorReporter(ErrorReporter* rpt) { report = rpt; }; |
240 | HRESULT EmitManifest(void); |
241 | |
242 | void SetEmitter( IMetaDataEmit* pEmitter) { m_pEmitter = pEmitter; }; |
243 | |
244 | void SetModuleName(__inout_opt __nullterminated char* szName); |
245 | |
246 | void AddFile(__in __nullterminated char* szName, DWORD dwAttr, BinStr* pHashBlob); |
247 | void EmitFiles(); |
248 | void EmitDebuggableAttribute(mdToken tkOwner); |
249 | |
250 | void StartAssembly(__in __nullterminated char* szName, __in_opt __nullterminated char* szAlias, DWORD dwAttr, BOOL isRef); |
251 | void EndAssembly(); |
252 | void EmitAssemblyRefs(); |
253 | void EmitAssembly(); |
254 | void SetAssemblyPublicKey(BinStr* pPublicKey); |
255 | void SetAssemblyPublicKeyToken(BinStr* pPublicKeyToken); |
256 | void SetAssemblyHashAlg(ULONG ulAlgID); |
257 | void SetAssemblyVer(USHORT usMajor, USHORT usMinor, USHORT usBuild, USHORT usRevision); |
258 | void SetAssemblyLocale(BinStr* pLocale, BOOL bConvertToUnicode); |
259 | void SetAssemblyHashBlob(BinStr* pHashBlob); |
260 | void SetAssemblyAutodetect(); |
261 | |
262 | void StartComType(__in __nullterminated char* szName, DWORD dwAttr); |
263 | void EndComType(); |
264 | void SetComTypeFile(__in __nullterminated char* szFileName); |
265 | void SetComTypeAsmRef(__in __nullterminated char* szAsmRefName); |
266 | void SetComTypeComType(__in __nullterminated char* szComTypeName); |
267 | BOOL SetComTypeImplementationTok(mdToken tk); |
268 | BOOL SetComTypeClassTok(mdToken tkClass); |
269 | |
270 | void StartManifestRes(__in __nullterminated char* szName, __in __nullterminated char* szAlias, DWORD dwAttr); |
271 | void EndManifestRes(); |
272 | void SetManifestResFile(__in __nullterminated char* szFileName, ULONG ulOffset); |
273 | void SetManifestResAsmRef(__in __nullterminated char* szAsmRefName); |
274 | |
275 | AsmManAssembly* GetAsmRefByAsmName(__in __nullterminated const char* szAsmName); |
276 | |
277 | mdToken GetFileTokByName(__in __nullterminated char* szFileName); |
278 | mdToken GetAsmRefTokByName(__in __nullterminated const char* szAsmRefName); |
279 | mdToken GetAsmTokByName(__in __nullterminated const char* szAsmName) |
280 | { return (m_pAssembly && (strcmp(m_pAssembly->szName,szAsmName)==0)) ? m_pAssembly->tkTok : 0; }; |
281 | |
282 | mdToken GetModuleRefTokByName(__in __nullterminated char* szName) |
283 | { |
284 | if(szName && *szName) |
285 | { |
286 | AsmManModRef* pMR; |
287 | for(unsigned i=0; (pMR=m_ModRefLst.PEEK(i)); i++) |
288 | { |
289 | if(!strcmp(szName, pMR->szName)) return pMR->tkTok; |
290 | } |
291 | } |
292 | return 0; |
293 | }; |
294 | |
295 | }; |
296 | |
297 | #endif |
298 | |