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
31 #include "mscoree_private.h"
33 #include "wine/debug.h"
34 #include "wine/unicode.h"
54 typedef struct tagCLRTABLE
68 IMAGE_NT_HEADERS *nthdr;
69 IMAGE_COR20_HEADER *corhdr;
71 METADATAHDR *metadatahdr;
74 static inline LPWSTR strdupW(LPCWSTR src)
81 dest = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(src) + 1) * sizeof(WCHAR));
88 static HRESULT parse_metadata_header(ASSEMBLY *assembly, DWORD *hdrsz)
90 METADATAHDR *metadatahdr;
95 rva = assembly->corhdr->MetaData.VirtualAddress;
96 ptr = ImageRvaToVa(assembly->nthdr, assembly->data, rva, NULL);
100 metadatahdr = (METADATAHDR *)ptr;
102 assembly->metadatahdr = HeapAlloc(GetProcessHeap(), 0, sizeof(METADATAHDR));
103 if (!assembly->metadatahdr)
104 return E_OUTOFMEMORY;
106 size = FIELD_OFFSET(METADATAHDR, Version);
107 memcpy(assembly->metadatahdr, metadatahdr, size);
109 assembly->metadatahdr->Version = (LPSTR)&metadatahdr->Version;
111 ofs = FIELD_OFFSET(METADATAHDR, Flags);
112 ptr += FIELD_OFFSET(METADATAHDR, Version) + metadatahdr->VersionLength + 1;
113 dest = (BYTE *)assembly->metadatahdr + ofs;
114 memcpy(dest, ptr, sizeof(METADATAHDR) - ofs);
116 *hdrsz = sizeof(METADATAHDR) - sizeof(LPSTR) + metadatahdr->VersionLength + 1;
121 static HRESULT parse_clr_metadata(ASSEMBLY *assembly)
126 hr = parse_metadata_header(assembly, &hdrsz);
133 static HRESULT parse_pe_header(ASSEMBLY *assembly)
135 IMAGE_DATA_DIRECTORY *datadirs;
137 assembly->nthdr = ImageNtHeader(assembly->data);
138 if (!assembly->nthdr)
141 if (assembly->nthdr->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
143 IMAGE_OPTIONAL_HEADER64 *opthdr =
144 (IMAGE_OPTIONAL_HEADER64 *)&assembly->nthdr->OptionalHeader;
145 datadirs = opthdr->DataDirectory;
149 IMAGE_OPTIONAL_HEADER32 *opthdr =
150 (IMAGE_OPTIONAL_HEADER32 *)&assembly->nthdr->OptionalHeader;
151 datadirs = opthdr->DataDirectory;
157 if (!datadirs[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress ||
158 !datadirs[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size)
163 assembly->corhdr = ImageRvaToVa(assembly->nthdr, assembly->data,
164 datadirs[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress, NULL);
165 if (!assembly->corhdr)
171 HRESULT assembly_create(ASSEMBLY **out, LPCWSTR file)
178 assembly = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ASSEMBLY));
180 return E_OUTOFMEMORY;
182 assembly->path = strdupW(file);
189 assembly->hfile = CreateFileW(file, GENERIC_READ, FILE_SHARE_READ,
190 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
191 if (assembly->hfile == INVALID_HANDLE_VALUE)
193 hr = HRESULT_FROM_WIN32(GetLastError());
197 assembly->hmap = CreateFileMappingW(assembly->hfile, NULL, PAGE_READONLY,
201 hr = HRESULT_FROM_WIN32(GetLastError());
205 assembly->data = MapViewOfFile(assembly->hmap, FILE_MAP_READ, 0, 0, 0);
208 hr = HRESULT_FROM_WIN32(GetLastError());
212 hr = parse_pe_header(assembly);
213 if (FAILED(hr)) goto failed;
215 hr = parse_clr_metadata(assembly);
216 if (FAILED(hr)) goto failed;
222 assembly_release(assembly);
226 HRESULT assembly_release(ASSEMBLY *assembly)
231 HeapFree(GetProcessHeap(), 0, assembly->metadatahdr);
232 HeapFree(GetProcessHeap(), 0, assembly->path);
233 UnmapViewOfFile(assembly->data);
234 CloseHandle(assembly->hmap);
235 CloseHandle(assembly->hfile);
236 HeapFree(GetProcessHeap(), 0, assembly);
241 HRESULT assembly_get_runtime_version(ASSEMBLY *assembly, LPSTR *version)
243 *version = assembly->metadatahdr->Version;