4 * Copyright 2008 James Hawkins
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
34 #include "fusionpriv.h"
35 #include "wine/debug.h"
36 #include "wine/unicode.h"
38 #define TableFromToken(tk) (TypeFromToken(tk) >> 24)
39 #define TokenFromTable(idx) (idx << 24)
41 #define MAX_CLR_TABLES 64
43 #define MD_STRINGS_BIT 0x1
44 #define MD_GUIDS_BIT 0x2
45 #define MD_BLOBS_BIT 0x4
47 typedef struct tagCLRTABLE
61 IMAGE_NT_HEADERS32
*nthdr
;
62 IMAGE_COR20_HEADER
*corhdr
;
64 METADATAHDR
*metadatahdr
;
66 METADATATABLESHDR
*tableshdr
;
69 CLRTABLE tables
[MAX_CLR_TABLES
];
79 static LPSTR
strdupWtoA(LPCWSTR str
)
87 len
= WideCharToMultiByte(CP_ACP
, 0, str
, -1, NULL
, 0, NULL
, NULL
);
88 ret
= HeapAlloc(GetProcessHeap(), 0, len
);
90 WideCharToMultiByte(CP_ACP
, 0, str
, -1, ret
, len
, NULL
, NULL
);
95 static DWORD
rva_to_offset(IMAGE_NT_HEADERS
*nthdrs
, DWORD rva
)
97 DWORD offset
= rva
, limit
;
98 IMAGE_SECTION_HEADER
*img
;
101 img
= IMAGE_FIRST_SECTION(nthdrs
);
103 if (rva
< img
->PointerToRawData
)
106 for (i
= 0; i
< nthdrs
->FileHeader
.NumberOfSections
; i
++)
108 if (img
[i
].SizeOfRawData
)
109 limit
= img
[i
].SizeOfRawData
;
111 limit
= img
[i
].Misc
.VirtualSize
;
113 if (rva
>= img
[i
].VirtualAddress
&&
114 rva
< (img
[i
].VirtualAddress
+ limit
))
116 if (img
[i
].PointerToRawData
!= 0)
118 offset
-= img
[i
].VirtualAddress
;
119 offset
+= img
[i
].PointerToRawData
;
129 static BYTE
*GetData(BYTE
*pData
, ULONG
*pLength
)
131 if ((*pData
& 0x80) == 0x00)
133 *pLength
= (*pData
& 0x7f);
137 if ((*pData
& 0xC0) == 0x80)
139 *pLength
= ((*pData
& 0x3f) << 8 | *(pData
+ 1));
143 if ((*pData
& 0xE0) == 0xC0)
145 *pLength
= ((*pData
& 0x1f) << 24 | *(pData
+ 1) << 16 |
146 *(pData
+ 2) << 8 | *(pData
+ 3));
150 *pLength
= (ULONG
)-1;
154 static VOID
*assembly_data_offset(ASSEMBLY
*assembly
, ULONG offset
)
156 return (VOID
*)&assembly
->data
[offset
];
159 #define MAX_TABLES_WORD 0xFFFF
160 #define MAX_TABLES_1BIT_ENCODE 32767
161 #define MAX_TABLES_2BIT_ENCODE 16383
162 #define MAX_TABLES_3BIT_ENCODE 8191
163 #define MAX_TABLES_5BIT_ENCODE 2047
165 static inline ULONG
get_table_size(ASSEMBLY
*assembly
, DWORD index
)
170 switch (TokenFromTable(index
))
174 size
= sizeof(MODULETABLE
) + (assembly
->stringsz
- sizeof(WORD
)) +
175 2 * (assembly
->guidsz
- sizeof(WORD
));
180 size
= sizeof(TYPEREFTABLE
) + 2 * (assembly
->stringsz
- sizeof(WORD
));
182 /* ResolutionScope:ResolutionScope */
183 tables
= max(assembly
->tables
[TableFromToken(mdtModule
)].rows
,
184 assembly
->tables
[TableFromToken(mdtModuleRef
)].rows
);
185 tables
= max(tables
, assembly
->tables
[TableFromToken(mdtAssemblyRef
)].rows
);
186 tables
= max(tables
, assembly
->tables
[TableFromToken(mdtTypeRef
)].rows
);
187 size
+= (tables
> MAX_TABLES_2BIT_ENCODE
) ? sizeof(WORD
) : 0;
192 size
= sizeof(TYPEDEFTABLE
) + 2 * (assembly
->stringsz
- sizeof(WORD
));
194 /* Extends:TypeDefOrRef */
195 tables
= max(assembly
->tables
[TableFromToken(mdtTypeDef
)].rows
,
196 assembly
->tables
[TableFromToken(mdtTypeRef
)].rows
);
197 tables
= max(tables
, assembly
->tables
[TableFromToken(mdtTypeSpec
)].rows
);
198 size
+= (tables
> MAX_TABLES_2BIT_ENCODE
) ? sizeof(WORD
) : 0;
200 size
+= (assembly
->tables
[TableFromToken(mdtFieldDef
)].rows
>
201 MAX_TABLES_WORD
) ? sizeof(WORD
) : 0;
202 size
+= (assembly
->tables
[TableFromToken(mdtMethodDef
)].rows
>
203 MAX_TABLES_WORD
) ? sizeof(WORD
) : 0;
208 size
= sizeof(FIELDTABLE
) + (assembly
->stringsz
- sizeof(WORD
)) +
209 (assembly
->blobsz
- sizeof(WORD
));
214 size
= sizeof(METHODDEFTABLE
) + (assembly
->stringsz
- sizeof(WORD
)) +
215 (assembly
->blobsz
- sizeof(WORD
));
217 size
+= (assembly
->tables
[TableFromToken(mdtParamDef
)].rows
>
218 MAX_TABLES_WORD
) ? sizeof(WORD
) : 0;
223 size
= sizeof(PARAMTABLE
) + (assembly
->stringsz
- sizeof(WORD
));
226 case mdtInterfaceImpl
:
228 size
= sizeof(INTERFACEIMPLTABLE
);
230 /* Interface:TypeDefOrRef */
231 tables
= max(assembly
->tables
[TableFromToken(mdtTypeDef
)].rows
,
232 assembly
->tables
[TableFromToken(mdtTypeRef
)].rows
);
233 tables
= max(tables
, assembly
->tables
[TableFromToken(mdtTypeSpec
)].rows
);
234 size
+= (tables
> MAX_TABLES_2BIT_ENCODE
) ? sizeof(WORD
) : 0;
239 size
= sizeof(MEMBERREFTABLE
) + (assembly
->stringsz
- sizeof(WORD
)) +
240 (assembly
->blobsz
- sizeof(WORD
));
242 /* Class:MemberRefParent */
243 tables
= max(assembly
->tables
[TableFromToken(mdtTypeRef
)].rows
,
244 assembly
->tables
[TableFromToken(mdtModuleRef
)].rows
);
245 tables
= max(tables
, assembly
->tables
[TableFromToken(mdtMethodDef
)].rows
);
246 tables
= max(tables
, assembly
->tables
[TableFromToken(mdtTypeSpec
)].rows
);
247 tables
= max(tables
, assembly
->tables
[TableFromToken(mdtTypeDef
)].rows
);
248 size
+= (tables
> MAX_TABLES_3BIT_ENCODE
) ? sizeof(WORD
) : 0;
251 case 0x0B000000: /* FIXME */
253 size
= sizeof(CONSTANTTABLE
) + (assembly
->blobsz
- sizeof(WORD
));
255 /* Parent:HasConstant */
256 tables
= max(assembly
->tables
[TableFromToken(mdtParamDef
)].rows
,
257 assembly
->tables
[TableFromToken(mdtFieldDef
)].rows
);
258 tables
= max(tables
, assembly
->tables
[TableFromToken(mdtProperty
)].rows
);
259 size
+= (tables
> MAX_TABLES_2BIT_ENCODE
) ? sizeof(WORD
) : 0;
262 case mdtCustomAttribute
:
264 size
= sizeof(CUSTOMATTRIBUTETABLE
) + (assembly
->blobsz
- sizeof(WORD
));
266 /* Parent:HasCustomAttribute */
267 tables
= max(assembly
->tables
[TableFromToken(mdtMethodDef
)].rows
,
268 assembly
->tables
[TableFromToken(mdtFieldDef
)].rows
);
269 tables
= max(tables
, assembly
->tables
[TableFromToken(mdtTypeRef
)].rows
);
270 tables
= max(tables
, assembly
->tables
[TableFromToken(mdtTypeDef
)].rows
);
271 tables
= max(tables
, assembly
->tables
[TableFromToken(mdtParamDef
)].rows
);
272 tables
= max(tables
, assembly
->tables
[TableFromToken(mdtInterfaceImpl
)].rows
);
273 tables
= max(tables
, assembly
->tables
[TableFromToken(mdtMemberRef
)].rows
);
274 tables
= max(tables
, assembly
->tables
[TableFromToken(mdtPermission
)].rows
);
275 tables
= max(tables
, assembly
->tables
[TableFromToken(mdtProperty
)].rows
);
276 tables
= max(tables
, assembly
->tables
[TableFromToken(mdtEvent
)].rows
);
277 tables
= max(tables
, assembly
->tables
[TableFromToken(mdtSignature
)].rows
);
278 tables
= max(tables
, assembly
->tables
[TableFromToken(mdtModuleRef
)].rows
);
279 tables
= max(tables
, assembly
->tables
[TableFromToken(mdtTypeSpec
)].rows
);
280 tables
= max(tables
, assembly
->tables
[TableFromToken(mdtAssembly
)].rows
);
281 tables
= max(tables
, assembly
->tables
[TableFromToken(mdtFile
)].rows
);
282 tables
= max(tables
, assembly
->tables
[TableFromToken(mdtExportedType
)].rows
);
283 tables
= max(tables
, assembly
->tables
[TableFromToken(mdtManifestResource
)].rows
);
284 size
+= (tables
> MAX_TABLES_5BIT_ENCODE
) ? sizeof(WORD
) : 0;
286 /* Type:CustomAttributeType */
287 tables
= max(assembly
->tables
[TableFromToken(mdtMethodDef
)].rows
,
288 assembly
->tables
[TableFromToken(mdtMemberRef
)].rows
);
289 size
+= (tables
> MAX_TABLES_3BIT_ENCODE
) ? sizeof(WORD
) : 0;
292 case 0x0D000000: /* FIXME */
294 size
= sizeof(FIELDMARSHALTABLE
) + (assembly
->blobsz
- sizeof(WORD
));
296 /* Parent:HasFieldMarshal */
297 tables
= max(assembly
->tables
[TableFromToken(mdtFieldDef
)].rows
,
298 assembly
->tables
[TableFromToken(mdtParamDef
)].rows
);
299 size
+= (tables
> MAX_TABLES_1BIT_ENCODE
) ? sizeof(WORD
) : 0;
304 size
= sizeof(DECLSECURITYTABLE
) + (assembly
->blobsz
- sizeof(WORD
));
306 /* Parent:HasDeclSecurity */
307 tables
= max(assembly
->tables
[TableFromToken(mdtTypeDef
)].rows
,
308 assembly
->tables
[TableFromToken(mdtMethodDef
)].rows
);
309 tables
= max(tables
, assembly
->tables
[TableFromToken(mdtAssembly
)].rows
);
310 size
+= (tables
> MAX_TABLES_2BIT_ENCODE
) ? sizeof(WORD
) : 0;
313 case 0x0F000000: /* FIXME */
315 size
= sizeof(CLASSLAYOUTTABLE
);
316 size
+= (assembly
->tables
[TableFromToken(mdtTypeDef
)].rows
>
317 MAX_TABLES_WORD
) ? sizeof(WORD
) : 0;
320 case 0x10000000: /* FIXME */
322 size
= sizeof(FIELDLAYOUTTABLE
);
323 size
+= (assembly
->tables
[TableFromToken(mdtFieldDef
)].rows
>
324 MAX_TABLES_WORD
) ? sizeof(WORD
) : 0;
329 size
= sizeof(STANDALONESIGTABLE
) + (assembly
->blobsz
- sizeof(WORD
));
332 case 0x12000000: /* FIXME */
334 size
= sizeof(EVENTMAPTABLE
);
335 size
+= (assembly
->tables
[TableFromToken(mdtTypeDef
)].rows
>
336 MAX_TABLES_WORD
) ? sizeof(WORD
) : 0;
337 size
+= (assembly
->tables
[TableFromToken(mdtEvent
)].rows
>
338 MAX_TABLES_WORD
) ? sizeof(WORD
) : 0;
343 size
= sizeof(EVENTTABLE
) + (assembly
->stringsz
- sizeof(WORD
));
345 /* EventType:TypeDefOrRef */
346 tables
= max(assembly
->tables
[TableFromToken(mdtTypeDef
)].rows
,
347 assembly
->tables
[TableFromToken(mdtTypeRef
)].rows
);
348 tables
= max(tables
, assembly
->tables
[TableFromToken(mdtTypeSpec
)].rows
);
349 size
+= (tables
> MAX_TABLES_2BIT_ENCODE
) ? sizeof(WORD
) : 0;
352 case 0x15000000:/* FIXME */
354 size
= sizeof(PROPERTYMAPTABLE
);
355 size
+= (assembly
->tables
[TableFromToken(mdtTypeDef
)].rows
>
356 MAX_TABLES_WORD
) ? sizeof(WORD
) : 0;
357 size
+= (assembly
->tables
[TableFromToken(mdtProperty
)].rows
>
358 MAX_TABLES_WORD
) ? sizeof(WORD
) : 0;
363 size
= sizeof(PROPERTYTABLE
) + (assembly
->stringsz
- sizeof(WORD
)) +
364 (assembly
->blobsz
- sizeof(WORD
));
367 case 0x18000000: /* FIXME */
369 size
= sizeof(METHODSEMANTICSTABLE
);
371 /* Association:HasSemantics */
372 tables
= max(assembly
->tables
[TableFromToken(mdtEvent
)].rows
,
373 assembly
->tables
[TableFromToken(mdtProperty
)].rows
);
374 size
+= (tables
> MAX_TABLES_1BIT_ENCODE
) ? sizeof(WORD
) : 0;
376 size
+= (assembly
->tables
[TableFromToken(mdtMethodDef
)].rows
>
377 MAX_TABLES_WORD
) ? sizeof(WORD
) : 0;
380 case 0x19000000: /* FIXME */
382 size
= sizeof(METHODIMPLTABLE
);
384 /* MethodBody:MethodDefOrRef, MethodDeclaration:MethodDefOrRef */
385 tables
= max(assembly
->tables
[TableFromToken(mdtMethodDef
)].rows
,
386 assembly
->tables
[TableFromToken(mdtMemberRef
)].rows
);
387 size
+= (tables
> MAX_TABLES_1BIT_ENCODE
) ? 2 * sizeof(WORD
) : 0;
389 size
+= (assembly
->tables
[TableFromToken(mdtTypeDef
)].rows
>
390 MAX_TABLES_WORD
) ? sizeof(WORD
) : 0;
395 size
= sizeof(MODULEREFTABLE
) + (assembly
->stringsz
- sizeof(WORD
));
400 size
= sizeof(TYPESPECTABLE
) + (assembly
->blobsz
- sizeof(WORD
));
403 case 0x1C000000: /* FIXME */
405 size
= sizeof(IMPLMAPTABLE
) + (assembly
->stringsz
- sizeof(WORD
));
407 /* MemberForwarded:MemberForwarded */
408 tables
= max(assembly
->tables
[TableFromToken(mdtFieldDef
)].rows
,
409 assembly
->tables
[TableFromToken(mdtMethodDef
)].rows
);
410 size
+= (tables
> MAX_TABLES_1BIT_ENCODE
) ? sizeof(WORD
) : 0;
412 size
+= (assembly
->tables
[TableFromToken(mdtModuleRef
)].rows
>
413 MAX_TABLES_WORD
) ? sizeof(WORD
) : 0;
416 case 0x1D000000: /* FIXME */
418 size
= sizeof(FIELDRVATABLE
);
419 size
+= (assembly
->tables
[TableFromToken(mdtFieldDef
)].rows
>
420 MAX_TABLES_WORD
) ? sizeof(WORD
) : 0;
425 size
= sizeof(ASSEMBLYTABLE
) + 2 * (assembly
->stringsz
- sizeof(WORD
)) +
426 (assembly
->blobsz
- sizeof(WORD
));
429 case 0x20000001: /* FIXME */
431 size
= sizeof(ASSEMBLYPROCESSORTABLE
);
434 case 0x22000000: /* FIXME */
436 size
= sizeof(ASSEMBLYOSTABLE
);
441 size
= sizeof(ASSEMBLYREFTABLE
) + 2 * (assembly
->stringsz
- sizeof(WORD
)) +
442 2 * (assembly
->blobsz
- sizeof(WORD
));
445 case 0x24000000: /* FIXME */
447 size
= sizeof(ASSEMBLYREFPROCESSORTABLE
);
448 size
+= (assembly
->tables
[TableFromToken(mdtAssemblyRef
)].rows
>
449 MAX_TABLES_WORD
) ? sizeof(WORD
) : 0;
452 case 0x25000000: /* FIXME */
454 size
= sizeof(ASSEMBLYREFOSTABLE
);
455 size
+= (assembly
->tables
[TableFromToken(mdtAssemblyRef
)].rows
>
456 MAX_TABLES_WORD
) ? sizeof(WORD
) : 0;
461 size
= sizeof(FILETABLE
) + (assembly
->stringsz
- sizeof(WORD
)) +
462 (assembly
->blobsz
- sizeof(WORD
));
465 case mdtExportedType
:
467 size
= sizeof(EXPORTEDTYPETABLE
) + 2 * (assembly
->stringsz
- sizeof(WORD
));
469 /* Implementation:Implementation */
470 tables
= max(assembly
->tables
[TableFromToken(mdtFile
)].rows
,
471 assembly
->tables
[TableFromToken(mdtMethodDef
)].rows
);
472 size
+= (tables
> MAX_TABLES_2BIT_ENCODE
) ? sizeof(WORD
) : 0;
475 case mdtManifestResource
:
477 size
= sizeof(MANIFESTRESTABLE
) + (assembly
->stringsz
- sizeof(WORD
));
479 /* Implementation:Implementation */
480 tables
= max(assembly
->tables
[TableFromToken(mdtFile
)].rows
,
481 assembly
->tables
[TableFromToken(mdtAssemblyRef
)].rows
);
482 size
+= (tables
> MAX_TABLES_2BIT_ENCODE
) ? sizeof(WORD
) : 0;
485 case 0x29000000: /* FIXME */
487 size
= sizeof(NESTEDCLASSTABLE
);
488 size
+= (assembly
->tables
[TableFromToken(mdtTypeDef
)].rows
>
489 MAX_TABLES_WORD
) ? 2 * sizeof(WORD
) : 0;
499 static HRESULT
parse_clr_tables(ASSEMBLY
*assembly
, ULONG offset
)
501 DWORD i
, previ
, offidx
;
505 assembly
->tableshdr
= (METADATATABLESHDR
*)assembly_data_offset(assembly
, currofs
);
506 if (!assembly
->tableshdr
)
509 assembly
->stringsz
= (assembly
->tableshdr
->HeapOffsetSizes
& MD_STRINGS_BIT
) ?
510 sizeof(DWORD
) : sizeof(WORD
);
511 assembly
->guidsz
= (assembly
->tableshdr
->HeapOffsetSizes
& MD_GUIDS_BIT
) ?
512 sizeof(DWORD
) : sizeof(WORD
);
513 assembly
->blobsz
= (assembly
->tableshdr
->HeapOffsetSizes
& MD_BLOBS_BIT
) ?
514 sizeof(DWORD
) : sizeof(WORD
);
516 currofs
+= sizeof(METADATATABLESHDR
);
517 assembly
->numrows
= (DWORD
*)assembly_data_offset(assembly
, currofs
);
518 if (!assembly
->numrows
)
521 assembly
->numtables
= 0;
522 for (i
= 0; i
< MAX_CLR_TABLES
; i
++)
524 if ((i
< 32 && (assembly
->tableshdr
->MaskValid
.u
.LowPart
>> i
) & 1) ||
525 (i
>= 32 && (assembly
->tableshdr
->MaskValid
.u
.HighPart
>> i
) & 1))
527 assembly
->numtables
++;
531 currofs
+= assembly
->numtables
* sizeof(DWORD
);
532 memset(assembly
->tables
, -1, MAX_CLR_TABLES
* sizeof(CLRTABLE
));
534 if (assembly
->tableshdr
->MaskValid
.u
.LowPart
& 1)
535 assembly
->tables
[0].offset
= currofs
;
538 for (i
= 0; i
< MAX_CLR_TABLES
; i
++)
540 if ((i
< 32 && (assembly
->tableshdr
->MaskValid
.u
.LowPart
>> i
) & 1) ||
541 (i
>= 32 && (assembly
->tableshdr
->MaskValid
.u
.HighPart
>> i
) & 1))
543 assembly
->tables
[i
].rows
= assembly
->numrows
[offidx
];
550 for (i
= 1; i
< MAX_CLR_TABLES
; i
++)
552 if ((i
< 32 && (assembly
->tableshdr
->MaskValid
.u
.LowPart
>> i
) & 1) ||
553 (i
>= 32 && (assembly
->tableshdr
->MaskValid
.u
.HighPart
>> i
) & 1))
555 currofs
+= get_table_size(assembly
, previ
) * assembly
->numrows
[offidx
- 1];
556 assembly
->tables
[i
].offset
= currofs
;
565 static HRESULT
parse_metadata_header(ASSEMBLY
*assembly
, DWORD
*hdrsz
)
567 METADATAHDR
*metadatahdr
;
572 rva
= assembly
->corhdr
->MetaData
.VirtualAddress
;
573 ptr
= ImageRvaToVa(assembly
->nthdr
, assembly
->data
, rva
, NULL
);
577 metadatahdr
= (METADATAHDR
*)ptr
;
579 assembly
->metadatahdr
= HeapAlloc(GetProcessHeap(), 0, sizeof(METADATAHDR
));
580 if (!assembly
->metadatahdr
)
581 return E_OUTOFMEMORY
;
583 size
= FIELD_OFFSET(METADATAHDR
, Version
);
584 memcpy(assembly
->metadatahdr
, metadatahdr
, size
);
586 /* we don't care about the version string */
588 ofs
= FIELD_OFFSET(METADATAHDR
, Flags
);
589 ptr
+= FIELD_OFFSET(METADATAHDR
, Version
) + metadatahdr
->VersionLength
+ 1;
590 dest
= (BYTE
*)assembly
->metadatahdr
+ ofs
;
591 memcpy(dest
, ptr
, sizeof(METADATAHDR
) - ofs
);
593 *hdrsz
= sizeof(METADATAHDR
) - sizeof(LPSTR
) + metadatahdr
->VersionLength
+ 1;
598 static HRESULT
parse_clr_metadata(ASSEMBLY
*assembly
)
600 METADATASTREAMHDR
*streamhdr
;
607 hr
= parse_metadata_header(assembly
, &hdrsz
);
611 rva
= assembly
->corhdr
->MetaData
.VirtualAddress
;
612 ptr
= ImageRvaToVa(assembly
->nthdr
, assembly
->data
, rva
+ hdrsz
, NULL
);
616 for (i
= 0; i
< assembly
->metadatahdr
->Streams
; i
++)
618 streamhdr
= (METADATASTREAMHDR
*)ptr
;
619 ofs
= rva_to_offset(assembly
->nthdr
, rva
+ streamhdr
->Offset
);
621 ptr
+= sizeof(METADATASTREAMHDR
);
624 if (!lstrcmpA(stream
, "#~"))
626 hr
= parse_clr_tables(assembly
, ofs
);
630 else if (!lstrcmpA(stream
, "#Strings") || !lstrcmpA(stream
, "Strings"))
631 assembly
->strings
= (BYTE
*)assembly_data_offset(assembly
, ofs
);
632 else if (!lstrcmpA(stream
, "#Blob") || !lstrcmpA(stream
, "Blob"))
633 assembly
->blobs
= (BYTE
*)assembly_data_offset(assembly
, ofs
);
635 ptr
+= lstrlenA(stream
);
642 static HRESULT
parse_pe_header(ASSEMBLY
*assembly
)
644 IMAGE_DATA_DIRECTORY
*datadirs
;
646 assembly
->nthdr
= ImageNtHeader(assembly
->data
);
647 if (!assembly
->nthdr
)
650 datadirs
= assembly
->nthdr
->OptionalHeader
.DataDirectory
;
654 if (!datadirs
[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
].VirtualAddress
||
655 !datadirs
[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
].Size
)
660 assembly
->corhdr
= ImageRvaToVa(assembly
->nthdr
, assembly
->data
,
661 datadirs
[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
].VirtualAddress
, NULL
);
662 if (!assembly
->corhdr
)
668 HRESULT
assembly_create(ASSEMBLY
**out
, LPCWSTR file
)
675 assembly
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(ASSEMBLY
));
677 return E_OUTOFMEMORY
;
679 assembly
->path
= strdupWtoA(file
);
686 assembly
->hfile
= CreateFileW(file
, GENERIC_READ
, FILE_SHARE_READ
,
687 NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
688 if (assembly
->hfile
== INVALID_HANDLE_VALUE
)
690 hr
= HRESULT_FROM_WIN32(GetLastError());
694 assembly
->hmap
= CreateFileMappingW(assembly
->hfile
, NULL
, PAGE_READONLY
,
698 hr
= HRESULT_FROM_WIN32(GetLastError());
702 assembly
->data
= MapViewOfFile(assembly
->hmap
, FILE_MAP_READ
, 0, 0, 0);
705 hr
= HRESULT_FROM_WIN32(GetLastError());
709 hr
= parse_pe_header(assembly
);
710 if (FAILED(hr
)) goto failed
;
712 hr
= parse_clr_metadata(assembly
);
713 if (FAILED(hr
)) goto failed
;
719 assembly_release(assembly
);
723 HRESULT
assembly_release(ASSEMBLY
*assembly
)
728 HeapFree(GetProcessHeap(), 0, assembly
->metadatahdr
);
729 HeapFree(GetProcessHeap(), 0, assembly
->path
);
730 UnmapViewOfFile(assembly
->data
);
731 CloseHandle(assembly
->hmap
);
732 CloseHandle(assembly
->hfile
);
733 HeapFree(GetProcessHeap(), 0, assembly
);
738 static LPSTR
assembly_dup_str(ASSEMBLY
*assembly
, DWORD index
)
740 LPSTR str
= (LPSTR
)&assembly
->strings
[index
];
741 LPSTR cpy
= HeapAlloc(GetProcessHeap(), 0, strlen(str
)+1);
747 HRESULT
assembly_get_name(ASSEMBLY
*assembly
, LPSTR
*name
)
753 offset
= assembly
->tables
[TableFromToken(mdtAssembly
)].offset
;
757 ptr
= assembly_data_offset(assembly
, offset
);
761 ptr
+= FIELD_OFFSET(ASSEMBLYTABLE
, PublicKey
) + assembly
->blobsz
;
762 if (assembly
->stringsz
== sizeof(DWORD
))
763 stridx
= *((DWORD
*)ptr
);
765 stridx
= *((WORD
*)ptr
);
767 *name
= assembly_dup_str(assembly
, stridx
);
769 return E_OUTOFMEMORY
;
774 HRESULT
assembly_get_path(ASSEMBLY
*assembly
, LPSTR
*path
)
776 LPSTR cpy
= HeapAlloc(GetProcessHeap(), 0, strlen(assembly
->path
)+1);
779 strcpy(cpy
, assembly
->path
);
781 return E_OUTOFMEMORY
;
786 HRESULT
assembly_get_version(ASSEMBLY
*assembly
, LPSTR
*version
)
789 VS_FIXEDFILEINFO
*ffi
;
793 size
= GetFileVersionInfoSizeA(assembly
->path
, NULL
);
795 return HRESULT_FROM_WIN32(GetLastError());
797 verdata
= HeapAlloc(GetProcessHeap(), 0, size
);
799 return E_OUTOFMEMORY
;
801 if (!GetFileVersionInfoA(assembly
->path
, 0, size
, verdata
))
803 hr
= HRESULT_FROM_WIN32(GetLastError());
807 if (!VerQueryValueA(verdata
, "\\", (LPVOID
*)&ffi
, &size
))
809 hr
= HRESULT_FROM_WIN32(GetLastError());
813 *version
= HeapAlloc(GetProcessHeap(), 0, MAX_PATH
);
820 sprintf(*version
, "%d.%d.%d.%d", HIWORD(ffi
->dwFileVersionMS
),
821 LOWORD(ffi
->dwFileVersionMS
), HIWORD(ffi
->dwFileVersionLS
),
822 LOWORD(ffi
->dwFileVersionLS
));
825 HeapFree(GetProcessHeap(), 0, verdata
);
829 HRESULT
assembly_get_architecture(ASSEMBLY
*assembly
, DWORD fixme
)
835 static BYTE
*assembly_get_blob(ASSEMBLY
*assembly
, WORD index
, ULONG
*size
)
837 return GetData(&assembly
->blobs
[index
], size
);
840 static void bytes_to_str(BYTE
*bytes
, DWORD len
, LPSTR str
)
844 static const char hexval
[16] = {
845 '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'
848 for(i
= 0; i
< len
; i
++)
850 str
[i
* 2] = hexval
[((bytes
[i
] >> 4) & 0xF)];
851 str
[i
* 2 + 1] = hexval
[(bytes
[i
]) & 0x0F];
855 #define BYTES_PER_TOKEN 8
856 #define CHARS_PER_BYTE 2
857 #define TOKEN_LENGTH (BYTES_PER_TOKEN * CHARS_PER_BYTE + 1)
859 HRESULT
assembly_get_pubkey_token(ASSEMBLY
*assembly
, LPSTR
*token
)
861 ASSEMBLYTABLE
*asmtbl
;
862 ULONG i
, offset
, size
;
867 BYTE tokbytes
[BYTES_PER_TOKEN
];
873 offset
= assembly
->tables
[TableFromToken(mdtAssembly
)].offset
;
877 asmtbl
= (ASSEMBLYTABLE
*)assembly_data_offset(assembly
, offset
);
881 pubkey
= assembly_get_blob(assembly
, asmtbl
->PublicKey
, &size
);
883 if (!CryptAcquireContextA(&crypt
, NULL
, NULL
, PROV_RSA_FULL
,
884 CRYPT_VERIFYCONTEXT
))
887 if (!CryptCreateHash(crypt
, CALG_SHA1
, 0, 0, &hash
))
890 if (!CryptHashData(hash
, pubkey
, size
, 0))
894 if (!CryptGetHashParam(hash
, HP_HASHVAL
, NULL
, &size
, 0))
897 hashdata
= HeapAlloc(GetProcessHeap(), 0, size
);
904 if (!CryptGetHashParam(hash
, HP_HASHVAL
, hashdata
, &size
, 0))
907 for (i
= size
- 1; i
>= size
- 8; i
--)
908 tokbytes
[size
- i
- 1] = hashdata
[i
];
910 tok
= HeapAlloc(GetProcessHeap(), 0, TOKEN_LENGTH
);
917 bytes_to_str(tokbytes
, BYTES_PER_TOKEN
, tok
);
918 tok
[TOKEN_LENGTH
- 1] = '\0';
924 HeapFree(GetProcessHeap(), 0, hashdata
);
925 CryptDestroyHash(hash
);
926 CryptReleaseContext(crypt
, 0);