mscoree: Implement ICLRMetaHost_GetVersionFromFile.
[wine] / dlls / mscoree / assembly.c
1 /*
2  * assembly parser
3  *
4  * Copyright 2008 James Hawkins
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
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 "corhdr.h"
31 #include "mscoree_private.h"
32
33 #include "wine/debug.h"
34 #include "wine/unicode.h"
35
36 typedef struct
37 {
38     ULONG Signature;
39     USHORT MajorVersion;
40     USHORT MinorVersion;
41     ULONG Reserved;
42     ULONG VersionLength;
43     LPSTR Version;
44     BYTE Flags;
45     WORD Streams;
46 } METADATAHDR;
47
48 typedef struct
49 {
50     DWORD Offset;
51     DWORD Size;
52 } METADATASTREAMHDR;
53
54 typedef struct tagCLRTABLE
55 {
56     INT rows;
57     DWORD offset;
58 } CLRTABLE;
59
60 struct tagASSEMBLY
61 {
62     LPWSTR path;
63
64     HANDLE hfile;
65     HANDLE hmap;
66     BYTE *data;
67
68     IMAGE_NT_HEADERS *nthdr;
69     IMAGE_COR20_HEADER *corhdr;
70
71     METADATAHDR *metadatahdr;
72 };
73
74 static inline LPWSTR strdupW(LPCWSTR src)
75 {
76     LPWSTR dest;
77
78     if (!src)
79         return NULL;
80
81     dest = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(src) + 1) * sizeof(WCHAR));
82     if (dest)
83         lstrcpyW(dest, src);
84
85     return dest;
86 }
87
88 static HRESULT parse_metadata_header(ASSEMBLY *assembly, DWORD *hdrsz)
89 {
90     METADATAHDR *metadatahdr;
91     BYTE *ptr, *dest;
92     DWORD size, ofs;
93     ULONG rva;
94
95     rva = assembly->corhdr->MetaData.VirtualAddress;
96     ptr = ImageRvaToVa(assembly->nthdr, assembly->data, rva, NULL);
97     if (!ptr)
98         return E_FAIL;
99
100     metadatahdr = (METADATAHDR *)ptr;
101
102     assembly->metadatahdr = HeapAlloc(GetProcessHeap(), 0, sizeof(METADATAHDR));
103     if (!assembly->metadatahdr)
104         return E_OUTOFMEMORY;
105
106     size = FIELD_OFFSET(METADATAHDR, Version);
107     memcpy(assembly->metadatahdr, metadatahdr, size);
108
109     assembly->metadatahdr->Version = (LPSTR)&metadatahdr->Version;
110
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);
115
116     *hdrsz = sizeof(METADATAHDR) - sizeof(LPSTR) + metadatahdr->VersionLength + 1;
117
118     return S_OK;
119 }
120
121 static HRESULT parse_clr_metadata(ASSEMBLY *assembly)
122 {
123     HRESULT hr;
124     DWORD hdrsz;
125
126     hr = parse_metadata_header(assembly, &hdrsz);
127     if (FAILED(hr))
128         return hr;
129
130     return S_OK;
131 }
132
133 static HRESULT parse_pe_header(ASSEMBLY *assembly)
134 {
135     IMAGE_DATA_DIRECTORY *datadirs;
136
137     assembly->nthdr = ImageNtHeader(assembly->data);
138     if (!assembly->nthdr)
139         return E_FAIL;
140
141     if (assembly->nthdr->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
142     {
143         IMAGE_OPTIONAL_HEADER64 *opthdr =
144                 (IMAGE_OPTIONAL_HEADER64 *)&assembly->nthdr->OptionalHeader;
145         datadirs = opthdr->DataDirectory;
146     }
147     else
148     {
149         IMAGE_OPTIONAL_HEADER32 *opthdr =
150                 (IMAGE_OPTIONAL_HEADER32 *)&assembly->nthdr->OptionalHeader;
151         datadirs = opthdr->DataDirectory;
152     }
153
154     if (!datadirs)
155         return E_FAIL;
156
157     if (!datadirs[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress ||
158         !datadirs[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size)
159     {
160         return E_FAIL;
161     }
162
163     assembly->corhdr = ImageRvaToVa(assembly->nthdr, assembly->data,
164         datadirs[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress, NULL);
165     if (!assembly->corhdr)
166         return E_FAIL;
167
168     return S_OK;
169 }
170
171 HRESULT assembly_create(ASSEMBLY **out, LPCWSTR file)
172 {
173     ASSEMBLY *assembly;
174     HRESULT hr;
175
176     *out = NULL;
177
178     assembly = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ASSEMBLY));
179     if (!assembly)
180         return E_OUTOFMEMORY;
181
182     assembly->path = strdupW(file);
183     if (!assembly->path)
184     {
185         hr = E_OUTOFMEMORY;
186         goto failed;
187     }
188
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)
192     {
193         hr = HRESULT_FROM_WIN32(GetLastError());
194         goto failed;
195     }
196
197     assembly->hmap = CreateFileMappingW(assembly->hfile, NULL, PAGE_READONLY,
198                                         0, 0, NULL);
199     if (!assembly->hmap)
200     {
201         hr = HRESULT_FROM_WIN32(GetLastError());
202         goto failed;
203     }
204
205     assembly->data = MapViewOfFile(assembly->hmap, FILE_MAP_READ, 0, 0, 0);
206     if (!assembly->data)
207     {
208         hr = HRESULT_FROM_WIN32(GetLastError());
209         goto failed;
210     }
211
212     hr = parse_pe_header(assembly);
213     if (FAILED(hr)) goto failed;
214
215     hr = parse_clr_metadata(assembly);
216     if (FAILED(hr)) goto failed;
217
218     *out = assembly;
219     return S_OK;
220
221 failed:
222     assembly_release(assembly);
223     return hr;
224 }
225
226 HRESULT assembly_release(ASSEMBLY *assembly)
227 {
228     if (!assembly)
229         return S_OK;
230
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);
237
238     return S_OK;
239 }
240
241 HRESULT assembly_get_runtime_version(ASSEMBLY *assembly, LPSTR *version)
242 {
243     *version = assembly->metadatahdr->Version;
244
245     return S_OK;
246 }