mapistub: Forward MAPISendMailW.
[wine/multimedia.git] / dlls / mscoree / assembly.c
blob07723f9edb7da048babefe3a7607d917f4b4ec0e
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 "dbghelp.h"
29 #include "ole2.h"
30 #include "mscoree.h"
31 #include "corhdr.h"
32 #include "metahost.h"
33 #include "cordebug.h"
34 #include "wine/list.h"
35 #include "mscoree_private.h"
37 #include "wine/debug.h"
38 #include "wine/unicode.h"
40 typedef struct
42 ULONG Signature;
43 USHORT MajorVersion;
44 USHORT MinorVersion;
45 ULONG Reserved;
46 ULONG VersionLength;
47 LPSTR Version;
48 BYTE Flags;
49 WORD Streams;
50 } METADATAHDR;
52 typedef struct
54 DWORD Offset;
55 DWORD Size;
56 } METADATASTREAMHDR;
58 typedef struct tagCLRTABLE
60 INT rows;
61 DWORD offset;
62 } CLRTABLE;
64 struct tagASSEMBLY
66 LPWSTR path;
68 HANDLE hfile;
69 HANDLE hmap;
70 BYTE *data;
72 IMAGE_NT_HEADERS *nthdr;
73 IMAGE_COR20_HEADER *corhdr;
75 METADATAHDR *metadatahdr;
78 static inline LPWSTR strdupW(LPCWSTR src)
80 LPWSTR dest;
82 if (!src)
83 return NULL;
85 dest = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(src) + 1) * sizeof(WCHAR));
86 if (dest)
87 lstrcpyW(dest, src);
89 return dest;
92 static HRESULT parse_metadata_header(ASSEMBLY *assembly, DWORD *hdrsz)
94 METADATAHDR *metadatahdr;
95 BYTE *ptr, *dest;
96 DWORD size, ofs;
97 ULONG rva;
99 rva = assembly->corhdr->MetaData.VirtualAddress;
100 ptr = ImageRvaToVa(assembly->nthdr, assembly->data, rva, NULL);
101 if (!ptr)
102 return E_FAIL;
104 metadatahdr = (METADATAHDR *)ptr;
106 assembly->metadatahdr = HeapAlloc(GetProcessHeap(), 0, sizeof(METADATAHDR));
107 if (!assembly->metadatahdr)
108 return E_OUTOFMEMORY;
110 size = FIELD_OFFSET(METADATAHDR, Version);
111 memcpy(assembly->metadatahdr, metadatahdr, size);
113 assembly->metadatahdr->Version = (LPSTR)&metadatahdr->Version;
115 ofs = FIELD_OFFSET(METADATAHDR, Flags);
116 ptr += FIELD_OFFSET(METADATAHDR, Version) + metadatahdr->VersionLength + 1;
117 dest = (BYTE *)assembly->metadatahdr + ofs;
118 memcpy(dest, ptr, sizeof(METADATAHDR) - ofs);
120 *hdrsz = sizeof(METADATAHDR) - sizeof(LPSTR) + metadatahdr->VersionLength + 1;
122 return S_OK;
125 static HRESULT parse_clr_metadata(ASSEMBLY *assembly)
127 HRESULT hr;
128 DWORD hdrsz;
130 hr = parse_metadata_header(assembly, &hdrsz);
131 if (FAILED(hr))
132 return hr;
134 return S_OK;
137 static HRESULT parse_pe_header(ASSEMBLY *assembly)
139 IMAGE_DATA_DIRECTORY *datadirs;
141 assembly->nthdr = ImageNtHeader(assembly->data);
142 if (!assembly->nthdr)
143 return E_FAIL;
145 if (assembly->nthdr->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
147 IMAGE_OPTIONAL_HEADER64 *opthdr =
148 (IMAGE_OPTIONAL_HEADER64 *)&assembly->nthdr->OptionalHeader;
149 datadirs = opthdr->DataDirectory;
151 else
153 IMAGE_OPTIONAL_HEADER32 *opthdr =
154 (IMAGE_OPTIONAL_HEADER32 *)&assembly->nthdr->OptionalHeader;
155 datadirs = opthdr->DataDirectory;
158 if (!datadirs)
159 return E_FAIL;
161 if (!datadirs[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress ||
162 !datadirs[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size)
164 return E_FAIL;
167 assembly->corhdr = ImageRvaToVa(assembly->nthdr, assembly->data,
168 datadirs[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress, NULL);
169 if (!assembly->corhdr)
170 return E_FAIL;
172 return S_OK;
175 HRESULT assembly_create(ASSEMBLY **out, LPCWSTR file)
177 ASSEMBLY *assembly;
178 HRESULT hr;
180 *out = NULL;
182 assembly = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ASSEMBLY));
183 if (!assembly)
184 return E_OUTOFMEMORY;
186 assembly->path = strdupW(file);
187 if (!assembly->path)
189 hr = E_OUTOFMEMORY;
190 goto failed;
193 assembly->hfile = CreateFileW(file, GENERIC_READ, FILE_SHARE_READ,
194 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
195 if (assembly->hfile == INVALID_HANDLE_VALUE)
197 hr = HRESULT_FROM_WIN32(GetLastError());
198 goto failed;
201 assembly->hmap = CreateFileMappingW(assembly->hfile, NULL, PAGE_READONLY,
202 0, 0, NULL);
203 if (!assembly->hmap)
205 hr = HRESULT_FROM_WIN32(GetLastError());
206 goto failed;
209 assembly->data = MapViewOfFile(assembly->hmap, FILE_MAP_READ, 0, 0, 0);
210 if (!assembly->data)
212 hr = HRESULT_FROM_WIN32(GetLastError());
213 goto failed;
216 hr = parse_pe_header(assembly);
217 if (FAILED(hr)) goto failed;
219 hr = parse_clr_metadata(assembly);
220 if (FAILED(hr)) goto failed;
222 *out = assembly;
223 return S_OK;
225 failed:
226 assembly_release(assembly);
227 return hr;
230 HRESULT assembly_release(ASSEMBLY *assembly)
232 if (!assembly)
233 return S_OK;
235 HeapFree(GetProcessHeap(), 0, assembly->metadatahdr);
236 HeapFree(GetProcessHeap(), 0, assembly->path);
237 UnmapViewOfFile(assembly->data);
238 CloseHandle(assembly->hmap);
239 CloseHandle(assembly->hfile);
240 HeapFree(GetProcessHeap(), 0, assembly);
242 return S_OK;
245 HRESULT assembly_get_runtime_version(ASSEMBLY *assembly, LPSTR *version)
247 *version = assembly->metadatahdr->Version;
249 return S_OK;