wined3d: Add fog and tex types to the ffp fragment description.
[wine/gsoc_dplay.git] / dlls / fusion / assembly.c
blob240c47cdc34ef49cbbc3fb2251395581b45302fd
1 /*
2 * assembly parser
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
21 #include <stdarg.h>
22 #include <stdio.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "winver.h"
28 #include "wincrypt.h"
29 #include "dbghelp.h"
30 #include "ole2.h"
31 #include "fusion.h"
32 #include "corhdr.h"
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
49 INT rows;
50 DWORD offset;
51 } CLRTABLE;
53 struct tagASSEMBLY
55 LPSTR path;
57 HANDLE hfile;
58 HANDLE hmap;
59 BYTE *data;
61 IMAGE_NT_HEADERS32 *nthdr;
62 IMAGE_COR20_HEADER *corhdr;
64 METADATAHDR *metadatahdr;
66 METADATATABLESHDR *tableshdr;
67 DWORD numtables;
68 DWORD *numrows;
69 CLRTABLE tables[MAX_CLR_TABLES];
71 DWORD stringsz;
72 DWORD guidsz;
73 DWORD blobsz;
75 BYTE *strings;
76 BYTE *blobs;
79 static LPSTR strdupWtoA(LPCWSTR str)
81 LPSTR ret = NULL;
82 DWORD len;
84 if (!str)
85 return ret;
87 len = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);
88 ret = HeapAlloc(GetProcessHeap(), 0, len);
89 if (ret)
90 WideCharToMultiByte(CP_ACP, 0, str, -1, ret, len, NULL, NULL);
92 return ret;
95 static DWORD rva_to_offset(IMAGE_NT_HEADERS *nthdrs, DWORD rva)
97 DWORD offset = rva, limit;
98 IMAGE_SECTION_HEADER *img;
99 WORD i;
101 img = IMAGE_FIRST_SECTION(nthdrs);
103 if (rva < img->PointerToRawData)
104 return rva;
106 for (i = 0; i < nthdrs->FileHeader.NumberOfSections; i++)
108 if (img[i].SizeOfRawData)
109 limit = img[i].SizeOfRawData;
110 else
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;
122 return offset;
126 return 0;
129 static BYTE *GetData(BYTE *pData, ULONG *pLength)
131 if ((*pData & 0x80) == 0x00)
133 *pLength = (*pData & 0x7f);
134 return pData + 1;
137 if ((*pData & 0xC0) == 0x80)
139 *pLength = ((*pData & 0x3f) << 8 | *(pData + 1));
140 return pData + 2;
143 if ((*pData & 0xE0) == 0xC0)
145 *pLength = ((*pData & 0x1f) << 24 | *(pData + 1) << 16 |
146 *(pData + 2) << 8 | *(pData + 3));
147 return pData + 4;
150 *pLength = (ULONG)-1;
151 return 0;
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)
167 DWORD size;
168 INT tables;
170 switch (TokenFromTable(index))
172 case mdtModule:
174 size = sizeof(MODULETABLE) + (assembly->stringsz - sizeof(WORD)) +
175 2 * (assembly->guidsz - sizeof(WORD));
176 break;
178 case mdtTypeRef:
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;
188 break;
190 case mdtTypeDef:
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;
204 break;
206 case mdtFieldDef:
208 size = sizeof(FIELDTABLE) + (assembly->stringsz - sizeof(WORD)) +
209 (assembly->blobsz - sizeof(WORD));
210 break;
212 case mdtMethodDef:
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;
219 break;
221 case mdtParamDef:
223 size = sizeof(PARAMTABLE) + (assembly->stringsz - sizeof(WORD));
224 break;
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;
235 break;
237 case mdtMemberRef:
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;
249 break;
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;
260 break;
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;
290 break;
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;
300 break;
302 case mdtPermission:
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;
311 break;
313 case 0x0F000000: /* FIXME */
315 size = sizeof(CLASSLAYOUTTABLE);
316 size += (assembly->tables[TableFromToken(mdtTypeDef)].rows >
317 MAX_TABLES_WORD) ? sizeof(WORD) : 0;
318 break;
320 case 0x10000000: /* FIXME */
322 size = sizeof(FIELDLAYOUTTABLE);
323 size += (assembly->tables[TableFromToken(mdtFieldDef)].rows >
324 MAX_TABLES_WORD) ? sizeof(WORD) : 0;
325 break;
327 case mdtSignature:
329 size = sizeof(STANDALONESIGTABLE) + (assembly->blobsz - sizeof(WORD));
330 break;
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;
339 break;
341 case mdtEvent:
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;
350 break;
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;
359 break;
361 case mdtProperty:
363 size = sizeof(PROPERTYTABLE) + (assembly->stringsz - sizeof(WORD)) +
364 (assembly->blobsz - sizeof(WORD));
365 break;
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;
378 break;
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;
391 break;
393 case mdtModuleRef:
395 size = sizeof(MODULEREFTABLE) + (assembly->stringsz - sizeof(WORD));
396 break;
398 case mdtTypeSpec:
400 size = sizeof(TYPESPECTABLE) + (assembly->blobsz - sizeof(WORD));
401 break;
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;
414 break;
416 case 0x1D000000: /* FIXME */
418 size = sizeof(FIELDRVATABLE);
419 size += (assembly->tables[TableFromToken(mdtFieldDef)].rows >
420 MAX_TABLES_WORD) ? sizeof(WORD) : 0;
421 break;
423 case mdtAssembly:
425 size = sizeof(ASSEMBLYTABLE) + 2 * (assembly->stringsz - sizeof(WORD)) +
426 (assembly->blobsz - sizeof(WORD));
427 break;
429 case 0x20000001: /* FIXME */
431 size = sizeof(ASSEMBLYPROCESSORTABLE);
432 break;
434 case 0x22000000: /* FIXME */
436 size = sizeof(ASSEMBLYOSTABLE);
437 break;
439 case mdtAssemblyRef:
441 size = sizeof(ASSEMBLYREFTABLE) + 2 * (assembly->stringsz - sizeof(WORD)) +
442 2 * (assembly->blobsz - sizeof(WORD));
443 break;
445 case 0x24000000: /* FIXME */
447 size = sizeof(ASSEMBLYREFPROCESSORTABLE);
448 size += (assembly->tables[TableFromToken(mdtAssemblyRef)].rows >
449 MAX_TABLES_WORD) ? sizeof(WORD) : 0;
450 break;
452 case 0x25000000: /* FIXME */
454 size = sizeof(ASSEMBLYREFOSTABLE);
455 size += (assembly->tables[TableFromToken(mdtAssemblyRef)].rows >
456 MAX_TABLES_WORD) ? sizeof(WORD) : 0;
457 break;
459 case mdtFile:
461 size = sizeof(FILETABLE) + (assembly->stringsz - sizeof(WORD)) +
462 (assembly->blobsz - sizeof(WORD));
463 break;
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;
473 break;
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;
483 break;
485 case 0x29000000: /* FIXME */
487 size = sizeof(NESTEDCLASSTABLE);
488 size += (assembly->tables[TableFromToken(mdtTypeDef)].rows >
489 MAX_TABLES_WORD) ? 2 * sizeof(WORD) : 0;
490 break;
492 default:
493 return 0;
496 return size;
499 static HRESULT parse_clr_tables(ASSEMBLY *assembly, ULONG offset)
501 DWORD i, previ, offidx;
502 ULONG currofs;
504 currofs = offset;
505 assembly->tableshdr = (METADATATABLESHDR *)assembly_data_offset(assembly, currofs);
506 if (!assembly->tableshdr)
507 return E_FAIL;
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)
519 return E_FAIL;
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;
537 offidx = 0;
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];
544 offidx++;
548 previ = 0;
549 offidx = 1;
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;
557 offidx++;
558 previ = i;
562 return S_OK;
565 static HRESULT parse_metadata_header(ASSEMBLY *assembly, DWORD *hdrsz)
567 METADATAHDR *metadatahdr;
568 BYTE *ptr, *dest;
569 DWORD size, ofs;
570 ULONG rva;
572 rva = assembly->corhdr->MetaData.VirtualAddress;
573 ptr = ImageRvaToVa(assembly->nthdr, assembly->data, rva, NULL);
574 if (!ptr)
575 return E_FAIL;
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;
595 return S_OK;
598 static HRESULT parse_clr_metadata(ASSEMBLY *assembly)
600 METADATASTREAMHDR *streamhdr;
601 ULONG rva, i, ofs;
602 LPSTR stream;
603 HRESULT hr;
604 DWORD hdrsz;
605 BYTE *ptr;
607 hr = parse_metadata_header(assembly, &hdrsz);
608 if (FAILED(hr))
609 return hr;
611 rva = assembly->corhdr->MetaData.VirtualAddress;
612 ptr = ImageRvaToVa(assembly->nthdr, assembly->data, rva + hdrsz, NULL);
613 if (!ptr)
614 return E_FAIL;
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);
622 stream = (LPSTR)ptr;
624 if (!lstrcmpA(stream, "#~"))
626 hr = parse_clr_tables(assembly, ofs);
627 if (FAILED(hr))
628 return hr;
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);
636 while (!*ptr) ptr++;
639 return S_OK;
642 static HRESULT parse_pe_header(ASSEMBLY *assembly)
644 IMAGE_DATA_DIRECTORY *datadirs;
646 assembly->nthdr = ImageNtHeader(assembly->data);
647 if (!assembly->nthdr)
648 return E_FAIL;
650 datadirs = assembly->nthdr->OptionalHeader.DataDirectory;
651 if (!datadirs)
652 return E_FAIL;
654 if (!datadirs[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress ||
655 !datadirs[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size)
657 return E_FAIL;
660 assembly->corhdr = ImageRvaToVa(assembly->nthdr, assembly->data,
661 datadirs[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress, NULL);
662 if (!assembly->corhdr)
663 return E_FAIL;
665 return S_OK;
668 HRESULT assembly_create(ASSEMBLY **out, LPCWSTR file)
670 ASSEMBLY *assembly;
671 HRESULT hr;
673 *out = NULL;
675 assembly = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ASSEMBLY));
676 if (!assembly)
677 return E_OUTOFMEMORY;
679 assembly->path = strdupWtoA(file);
680 if (!assembly->path)
682 hr = E_OUTOFMEMORY;
683 goto failed;
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());
691 goto failed;
694 assembly->hmap = CreateFileMappingW(assembly->hfile, NULL, PAGE_READONLY,
695 0, 0, NULL);
696 if (!assembly->hmap)
698 hr = HRESULT_FROM_WIN32(GetLastError());
699 goto failed;
702 assembly->data = MapViewOfFile(assembly->hmap, FILE_MAP_READ, 0, 0, 0);
703 if (!assembly->data)
705 hr = HRESULT_FROM_WIN32(GetLastError());
706 goto failed;
709 hr = parse_pe_header(assembly);
710 if (FAILED(hr)) goto failed;
712 hr = parse_clr_metadata(assembly);
713 if (FAILED(hr)) goto failed;
715 *out = assembly;
716 return S_OK;
718 failed:
719 assembly_release(assembly);
720 return hr;
723 HRESULT assembly_release(ASSEMBLY *assembly)
725 if (!assembly)
726 return S_OK;
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);
735 return S_OK;
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);
742 if (cpy)
743 strcpy(cpy, str);
744 return cpy;
747 HRESULT assembly_get_name(ASSEMBLY *assembly, LPSTR *name)
749 BYTE *ptr;
750 ULONG offset;
751 DWORD stridx;
753 offset = assembly->tables[TableFromToken(mdtAssembly)].offset;
754 if (offset == -1)
755 return E_FAIL;
757 ptr = assembly_data_offset(assembly, offset);
758 if (!ptr)
759 return E_FAIL;
761 ptr += FIELD_OFFSET(ASSEMBLYTABLE, PublicKey) + assembly->blobsz;
762 if (assembly->stringsz == sizeof(DWORD))
763 stridx = *((DWORD *)ptr);
764 else
765 stridx = *((WORD *)ptr);
767 *name = assembly_dup_str(assembly, stridx);
768 if (!*name)
769 return E_OUTOFMEMORY;
771 return S_OK;
774 HRESULT assembly_get_path(ASSEMBLY *assembly, LPSTR *path)
776 LPSTR cpy = HeapAlloc(GetProcessHeap(), 0, strlen(assembly->path)+1);
777 *path = cpy;
778 if (cpy)
779 strcpy(cpy, assembly->path);
780 else
781 return E_OUTOFMEMORY;
783 return S_OK;
786 HRESULT assembly_get_version(ASSEMBLY *assembly, LPSTR *version)
788 LPSTR verdata;
789 VS_FIXEDFILEINFO *ffi;
790 HRESULT hr = S_OK;
791 DWORD size;
793 size = GetFileVersionInfoSizeA(assembly->path, NULL);
794 if (!size)
795 return HRESULT_FROM_WIN32(GetLastError());
797 verdata = HeapAlloc(GetProcessHeap(), 0, size);
798 if (!verdata)
799 return E_OUTOFMEMORY;
801 if (!GetFileVersionInfoA(assembly->path, 0, size, verdata))
803 hr = HRESULT_FROM_WIN32(GetLastError());
804 goto done;
807 if (!VerQueryValueA(verdata, "\\", (LPVOID *)&ffi, &size))
809 hr = HRESULT_FROM_WIN32(GetLastError());
810 goto done;
813 *version = HeapAlloc(GetProcessHeap(), 0, MAX_PATH);
814 if (!*version)
816 hr = E_OUTOFMEMORY;
817 goto done;
820 sprintf(*version, "%d.%d.%d.%d", HIWORD(ffi->dwFileVersionMS),
821 LOWORD(ffi->dwFileVersionMS), HIWORD(ffi->dwFileVersionLS),
822 LOWORD(ffi->dwFileVersionLS));
824 done:
825 HeapFree(GetProcessHeap(), 0, verdata);
826 return hr;
829 HRESULT assembly_get_architecture(ASSEMBLY *assembly, DWORD fixme)
831 /* FIXME */
832 return S_OK;
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)
842 int i;
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;
863 BYTE *hashdata;
864 HCRYPTPROV crypt;
865 HCRYPTHASH hash;
866 BYTE *pubkey;
867 BYTE tokbytes[BYTES_PER_TOKEN];
868 HRESULT hr = E_FAIL;
869 LPSTR tok;
871 *token = NULL;
873 offset = assembly->tables[TableFromToken(mdtAssembly)].offset;
874 if (offset == -1)
875 return E_FAIL;
877 asmtbl = (ASSEMBLYTABLE *)assembly_data_offset(assembly, offset);
878 if (!asmtbl)
879 return E_FAIL;
881 pubkey = assembly_get_blob(assembly, asmtbl->PublicKey, &size);
883 if (!CryptAcquireContextA(&crypt, NULL, NULL, PROV_RSA_FULL,
884 CRYPT_VERIFYCONTEXT))
885 return E_FAIL;
887 if (!CryptCreateHash(crypt, CALG_SHA1, 0, 0, &hash))
888 return E_FAIL;
890 if (!CryptHashData(hash, pubkey, size, 0))
891 return E_FAIL;
893 size = 0;
894 if (!CryptGetHashParam(hash, HP_HASHVAL, NULL, &size, 0))
895 return E_FAIL;
897 hashdata = HeapAlloc(GetProcessHeap(), 0, size);
898 if (!hashdata)
900 hr = E_OUTOFMEMORY;
901 goto done;
904 if (!CryptGetHashParam(hash, HP_HASHVAL, hashdata, &size, 0))
905 goto done;
907 for (i = size - 1; i >= size - 8; i--)
908 tokbytes[size - i - 1] = hashdata[i];
910 tok = HeapAlloc(GetProcessHeap(), 0, TOKEN_LENGTH);
911 if (!tok)
913 hr = E_OUTOFMEMORY;
914 goto done;
917 bytes_to_str(tokbytes, BYTES_PER_TOKEN, tok);
918 tok[TOKEN_LENGTH - 1] = '\0';
920 *token = tok;
921 hr = S_OK;
923 done:
924 HeapFree(GetProcessHeap(), 0, hashdata);
925 CryptDestroyHash(hash);
926 CryptReleaseContext(crypt, 0);
928 return hr;