wbemprox: Add support for enumerating class methods.
[wine/multimedia.git] / dlls / mscoree / assembly.c
blob13e7f03e4daa1e620fb5b969f1718c8c45f18946
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 int is_mapped_file;
68 /* mapped files */
69 LPWSTR path;
70 HANDLE hfile;
71 HANDLE hmap;
73 BYTE *data;
75 IMAGE_NT_HEADERS *nthdr;
76 IMAGE_COR20_HEADER *corhdr;
78 METADATAHDR *metadatahdr;
81 static inline LPWSTR strdupW(LPCWSTR src)
83 LPWSTR dest;
85 if (!src)
86 return NULL;
88 dest = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(src) + 1) * sizeof(WCHAR));
89 if (dest)
90 lstrcpyW(dest, src);
92 return dest;
95 static void* assembly_rva_to_va(ASSEMBLY *assembly, ULONG rva)
97 if (assembly->is_mapped_file)
98 return ImageRvaToVa(assembly->nthdr, assembly->data, rva, NULL);
99 else
100 return assembly->data + rva;
103 static ULONG assembly_datadir_get_data(ASSEMBLY *assembly,
104 IMAGE_DATA_DIRECTORY *datadir, void **data)
106 if (!datadir->VirtualAddress || !datadir->Size)
108 *data = NULL;
109 return 0;
111 else
113 *data = assembly_rva_to_va(assembly, datadir->VirtualAddress);
114 return datadir->Size;
118 static HRESULT parse_metadata_header(ASSEMBLY *assembly, DWORD *hdrsz)
120 METADATAHDR *metadatahdr;
121 BYTE *ptr, *dest;
122 DWORD size, ofs;
123 ULONG rva;
125 rva = assembly->corhdr->MetaData.VirtualAddress;
126 ptr = assembly_rva_to_va(assembly, rva);
127 if (!ptr)
128 return E_FAIL;
130 metadatahdr = (METADATAHDR *)ptr;
132 assembly->metadatahdr = HeapAlloc(GetProcessHeap(), 0, sizeof(METADATAHDR));
133 if (!assembly->metadatahdr)
134 return E_OUTOFMEMORY;
136 size = FIELD_OFFSET(METADATAHDR, Version);
137 memcpy(assembly->metadatahdr, metadatahdr, size);
139 assembly->metadatahdr->Version = (LPSTR)&metadatahdr->Version;
141 ofs = FIELD_OFFSET(METADATAHDR, Flags);
142 ptr += FIELD_OFFSET(METADATAHDR, Version) + metadatahdr->VersionLength + 1;
143 dest = (BYTE *)assembly->metadatahdr + ofs;
144 memcpy(dest, ptr, sizeof(METADATAHDR) - ofs);
146 *hdrsz = sizeof(METADATAHDR) - sizeof(LPSTR) + metadatahdr->VersionLength + 1;
148 return S_OK;
151 static HRESULT parse_clr_metadata(ASSEMBLY *assembly)
153 HRESULT hr;
154 DWORD hdrsz;
156 hr = parse_metadata_header(assembly, &hdrsz);
157 if (FAILED(hr))
158 return hr;
160 return S_OK;
163 static HRESULT parse_pe_header(ASSEMBLY *assembly)
165 IMAGE_DATA_DIRECTORY *datadirs;
167 assembly->nthdr = ImageNtHeader(assembly->data);
168 if (!assembly->nthdr)
169 return E_FAIL;
171 if (assembly->nthdr->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
173 IMAGE_OPTIONAL_HEADER64 *opthdr =
174 (IMAGE_OPTIONAL_HEADER64 *)&assembly->nthdr->OptionalHeader;
175 datadirs = opthdr->DataDirectory;
177 else
179 IMAGE_OPTIONAL_HEADER32 *opthdr =
180 (IMAGE_OPTIONAL_HEADER32 *)&assembly->nthdr->OptionalHeader;
181 datadirs = opthdr->DataDirectory;
184 if (!datadirs)
185 return E_FAIL;
187 if (!assembly_datadir_get_data(assembly, &datadirs[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR], (void**)&assembly->corhdr))
188 return E_FAIL;
190 return S_OK;
193 static HRESULT parse_headers(ASSEMBLY *assembly)
195 HRESULT hr;
197 hr = parse_pe_header(assembly);
199 if (SUCCEEDED(hr))
200 hr = parse_clr_metadata(assembly);
202 return hr;
205 HRESULT assembly_create(ASSEMBLY **out, LPCWSTR file)
207 ASSEMBLY *assembly;
208 HRESULT hr;
210 *out = NULL;
212 assembly = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ASSEMBLY));
213 if (!assembly)
214 return E_OUTOFMEMORY;
216 assembly->is_mapped_file = 1;
218 assembly->path = strdupW(file);
219 if (!assembly->path)
221 hr = E_OUTOFMEMORY;
222 goto failed;
225 assembly->hfile = CreateFileW(file, GENERIC_READ, FILE_SHARE_READ,
226 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
227 if (assembly->hfile == INVALID_HANDLE_VALUE)
229 hr = HRESULT_FROM_WIN32(GetLastError());
230 goto failed;
233 assembly->hmap = CreateFileMappingW(assembly->hfile, NULL, PAGE_READONLY,
234 0, 0, NULL);
235 if (!assembly->hmap)
237 hr = HRESULT_FROM_WIN32(GetLastError());
238 goto failed;
241 assembly->data = MapViewOfFile(assembly->hmap, FILE_MAP_READ, 0, 0, 0);
242 if (!assembly->data)
244 hr = HRESULT_FROM_WIN32(GetLastError());
245 goto failed;
248 hr = parse_headers(assembly);
249 if (FAILED(hr)) goto failed;
251 *out = assembly;
252 return S_OK;
254 failed:
255 assembly_release(assembly);
256 return hr;
259 HRESULT assembly_from_hmodule(ASSEMBLY **out, HMODULE hmodule)
261 ASSEMBLY *assembly;
262 HRESULT hr;
264 *out = NULL;
266 assembly = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ASSEMBLY));
267 if (!assembly)
268 return E_OUTOFMEMORY;
270 assembly->is_mapped_file = 0;
272 assembly->data = (BYTE*)hmodule;
274 hr = parse_headers(assembly);
275 if (SUCCEEDED(hr))
276 *out = assembly;
277 else
278 assembly_release(assembly);
280 return hr;
283 HRESULT assembly_release(ASSEMBLY *assembly)
285 if (!assembly)
286 return S_OK;
288 if (assembly->is_mapped_file)
290 UnmapViewOfFile(assembly->data);
291 CloseHandle(assembly->hmap);
292 CloseHandle(assembly->hfile);
294 HeapFree(GetProcessHeap(), 0, assembly->metadatahdr);
295 HeapFree(GetProcessHeap(), 0, assembly->path);
296 HeapFree(GetProcessHeap(), 0, assembly);
298 return S_OK;
301 HRESULT assembly_get_runtime_version(ASSEMBLY *assembly, LPSTR *version)
303 *version = assembly->metadatahdr->Version;
305 return S_OK;
308 HRESULT assembly_get_vtable_fixups(ASSEMBLY *assembly, VTableFixup **fixups, DWORD *count)
310 ULONG size;
312 size = assembly_datadir_get_data(assembly, &assembly->corhdr->VTableFixups, (void**)fixups);
313 *count = size / sizeof(VTableFixup);
315 return S_OK;