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