ReactOS  0.4.14-dev-342-gdc047f9
assembly.c File Reference
#include "mscoree_private.h"
#include <winver.h>
#include <dbghelp.h>
Include dependency graph for assembly.c:

Go to the source code of this file.

Classes

struct  METADATAHDR
 
struct  METADATASTREAMHDR
 
struct  tagCLRTABLE
 
struct  tagASSEMBLY
 

Typedefs

typedef struct tagCLRTABLE CLRTABLE
 

Functions

static LPWSTR strdupW (LPCWSTR src)
 
static voidassembly_rva_to_va (ASSEMBLY *assembly, ULONG rva)
 
static ULONG assembly_datadir_get_data (ASSEMBLY *assembly, IMAGE_DATA_DIRECTORY *datadir, void **data)
 
static HRESULT parse_metadata_header (ASSEMBLY *assembly, DWORD *hdrsz)
 
static HRESULT parse_clr_metadata (ASSEMBLY *assembly)
 
static HRESULT parse_pe_header (ASSEMBLY *assembly)
 
static HRESULT parse_headers (ASSEMBLY *assembly)
 
HRESULT assembly_create (ASSEMBLY **out, LPCWSTR file)
 
HRESULT assembly_from_hmodule (ASSEMBLY **out, HMODULE hmodule)
 
HRESULT assembly_release (ASSEMBLY *assembly)
 
HRESULT assembly_get_runtime_version (ASSEMBLY *assembly, LPSTR *version)
 
HRESULT assembly_get_vtable_fixups (ASSEMBLY *assembly, VTableFixup **fixups, DWORD *count)
 

Typedef Documentation

◆ CLRTABLE

Function Documentation

◆ assembly_create()

HRESULT assembly_create ( ASSEMBLY **  out,
LPCWSTR  file 
)

Definition at line 191 of file assembly.c.

192 {
194  HRESULT hr;
195 
196  *out = NULL;
197 
199  if (!assembly)
200  return E_OUTOFMEMORY;
201 
202  assembly->is_mapped_file = 1;
203 
204  assembly->path = strdupW(file);
205  if (!assembly->path)
206  {
207  hr = E_OUTOFMEMORY;
208  goto failed;
209  }
210 
213  if (assembly->hfile == INVALID_HANDLE_VALUE)
214  {
216  goto failed;
217  }
218 
220  0, 0, NULL);
221  if (!assembly->hmap)
222  {
224  goto failed;
225  }
226 
227  assembly->data = MapViewOfFile(assembly->hmap, FILE_MAP_READ, 0, 0, 0);
228  if (!assembly->data)
229  {
231  goto failed;
232  }
233 
235  if (FAILED(hr)) goto failed;
236 
237  *out = assembly;
238  return S_OK;
239 
240 failed:
242  return hr;
243 }
#define HRESULT_FROM_WIN32(x)
Definition: winerror.h:92
#define MapViewOfFile
Definition: compat.h:410
HRESULT hr
Definition: shlfolder.c:183
#define INVALID_HANDLE_VALUE
Definition: compat.h:399
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
static struct _tagASSEMBLY assembly
#define FILE_SHARE_READ
Definition: compat.h:125
#define E_OUTOFMEMORY
Definition: ddrawi.h:100
smooth NULL
Definition: ftsmooth.c:416
#define FILE_MAP_READ
Definition: compat.h:435
#define OPEN_EXISTING
Definition: compat.h:434
static HRESULT parse_headers(ASSEMBLY *assembly)
Definition: assembly.c:179
#define GetProcessHeap()
Definition: compat.h:403
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
LONG HRESULT
Definition: typedefs.h:77
#define CreateFileMappingW(a, b, c, d, e, f)
Definition: compat.h:409
static FILE * out
Definition: regtests2xml.c:44
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:126
#define GENERIC_READ
Definition: compat.h:124
HRESULT assembly_release(ASSEMBLY *assembly)
Definition: assembly.c:694
#define S_OK
Definition: intsafe.h:59
#define PAGE_READONLY
Definition: compat.h:127
#define HEAP_ZERO_MEMORY
Definition: compat.h:123
#define CreateFileW
Definition: compat.h:408
static LPWSTR strdupW(LPCWSTR src)
Definition: assembly.c:67
Definition: fci.c:126

◆ assembly_datadir_get_data()

static ULONG assembly_datadir_get_data ( ASSEMBLY assembly,
IMAGE_DATA_DIRECTORY datadir,
void **  data 
)
static

Definition at line 89 of file assembly.c.

91 {
92  if (!datadir->VirtualAddress || !datadir->Size)
93  {
94  *data = NULL;
95  return 0;
96  }
97  else
98  {
100  return datadir->Size;
101  }
102 }
smooth NULL
Definition: ftsmooth.c:416
static void * assembly_rva_to_va(ASSEMBLY *assembly, ULONG rva)
Definition: assembly.c:81
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950

Referenced by assembly_get_vtable_fixups(), and parse_pe_header().

◆ assembly_from_hmodule()

HRESULT assembly_from_hmodule ( ASSEMBLY **  out,
HMODULE  hmodule 
)

Definition at line 245 of file assembly.c.

246 {
248  HRESULT hr;
249 
250  *out = NULL;
251 
253  if (!assembly)
254  return E_OUTOFMEMORY;
255 
256  assembly->is_mapped_file = 0;
257 
258  assembly->data = (BYTE*)hmodule;
259 
261  if (SUCCEEDED(hr))
262  *out = assembly;
263  else
265 
266  return hr;
267 }
HRESULT hr
Definition: shlfolder.c:183
static struct _tagASSEMBLY assembly
#define E_OUTOFMEMORY
Definition: ddrawi.h:100
static HMODULE hmodule
Definition: rasapi.c:29
smooth NULL
Definition: ftsmooth.c:416
static HRESULT parse_headers(ASSEMBLY *assembly)
Definition: assembly.c:179
#define GetProcessHeap()
Definition: compat.h:403
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
LONG HRESULT
Definition: typedefs.h:77
static FILE * out
Definition: regtests2xml.c:44
unsigned char BYTE
Definition: mem.h:68
HRESULT assembly_release(ASSEMBLY *assembly)
Definition: assembly.c:694
#define HEAP_ZERO_MEMORY
Definition: compat.h:123
#define SUCCEEDED(hr)
Definition: intsafe.h:57

Referenced by FixupVTable().

◆ assembly_get_runtime_version()

HRESULT assembly_get_runtime_version ( ASSEMBLY assembly,
LPSTR version 
)

Definition at line 287 of file assembly.c.

288 {
289  *version = assembly->metadatahdr->Version;
290 
291  return S_OK;
292 }
static const WCHAR version[]
Definition: asmname.c:66
#define S_OK
Definition: intsafe.h:59

◆ assembly_get_vtable_fixups()

HRESULT assembly_get_vtable_fixups ( ASSEMBLY assembly,
VTableFixup **  fixups,
DWORD count 
)

Definition at line 294 of file assembly.c.

295 {
296  ULONG size;
297 
298  size = assembly_datadir_get_data(assembly, &assembly->corhdr->VTableFixups, (void**)fixups);
299  *count = size / sizeof(VTableFixup);
300 
301  return S_OK;
302 }
struct _VTableFixup VTableFixup
GLuint GLuint GLsizei count
Definition: gl.h:1545
GLsizeiptr size
Definition: glext.h:5919
#define S_OK
Definition: intsafe.h:59
static ULONG assembly_datadir_get_data(ASSEMBLY *assembly, IMAGE_DATA_DIRECTORY *datadir, void **data)
Definition: assembly.c:89
unsigned int ULONG
Definition: retypes.h:1

Referenced by FixupVTable().

◆ assembly_release()

HRESULT assembly_release ( ASSEMBLY assembly)

Definition at line 269 of file assembly.c.

270 {
271  if (!assembly)
272  return S_OK;
273 
274  if (assembly->is_mapped_file)
275  {
276  UnmapViewOfFile(assembly->data);
277  CloseHandle(assembly->hmap);
278  CloseHandle(assembly->hfile);
279  }
280  HeapFree(GetProcessHeap(), 0, assembly->metadatahdr);
281  HeapFree(GetProcessHeap(), 0, assembly->path);
283 
284  return S_OK;
285 }
#define CloseHandle
Definition: compat.h:406
#define GetProcessHeap()
Definition: compat.h:403
#define S_OK
Definition: intsafe.h:59
#define UnmapViewOfFile
Definition: compat.h:411
#define HeapFree(x, y, z)
Definition: compat.h:402

◆ assembly_rva_to_va()

static void* assembly_rva_to_va ( ASSEMBLY assembly,
ULONG  rva 
)
static

Definition at line 81 of file assembly.c.

82 {
83  if (assembly->is_mapped_file)
84  return ImageRvaToVa(assembly->nthdr, assembly->data, rva, NULL);
85  else
86  return assembly->data + rva;
87 }
smooth NULL
Definition: ftsmooth.c:416
PVOID WINAPI ImageRvaToVa(_In_ PIMAGE_NT_HEADERS, _In_ PVOID, _In_ ULONG, _In_opt_ PIMAGE_SECTION_HEADER *)

Referenced by assembly_datadir_get_data(), and parse_metadata_header().

◆ parse_clr_metadata()

static HRESULT parse_clr_metadata ( ASSEMBLY assembly)
static

Definition at line 137 of file assembly.c.

138 {
139  HRESULT hr;
140  DWORD hdrsz;
141 
142  hr = parse_metadata_header(assembly, &hdrsz);
143  if (FAILED(hr))
144  return hr;
145 
146  return S_OK;
147 }
HRESULT hr
Definition: shlfolder.c:183
static HRESULT parse_metadata_header(ASSEMBLY *assembly, DWORD *hdrsz)
Definition: assembly.c:104
LONG HRESULT
Definition: typedefs.h:77
unsigned long DWORD
Definition: ntddk_ex.h:95
#define S_OK
Definition: intsafe.h:59

Referenced by parse_headers().

◆ parse_headers()

static HRESULT parse_headers ( ASSEMBLY assembly)
static

Definition at line 179 of file assembly.c.

180 {
181  HRESULT hr;
182 
184 
185  if (SUCCEEDED(hr))
187 
188  return hr;
189 }
HRESULT hr
Definition: shlfolder.c:183
LONG HRESULT
Definition: typedefs.h:77
static HRESULT parse_clr_metadata(ASSEMBLY *assembly)
Definition: assembly.c:137
#define SUCCEEDED(hr)
Definition: intsafe.h:57
static HRESULT parse_pe_header(ASSEMBLY *assembly)
Definition: assembly.c:149

Referenced by assembly_create(), and assembly_from_hmodule().

◆ parse_metadata_header()

static HRESULT parse_metadata_header ( ASSEMBLY assembly,
DWORD hdrsz 
)
static

Definition at line 104 of file assembly.c.

105 {
106  METADATAHDR *metadatahdr;
107  BYTE *ptr, *dest;
108  DWORD size, ofs;
109  ULONG rva;
110 
111  rva = assembly->corhdr->MetaData.VirtualAddress;
113  if (!ptr)
114  return E_FAIL;
115 
116  metadatahdr = (METADATAHDR *)ptr;
117 
118  assembly->metadatahdr = HeapAlloc(GetProcessHeap(), 0, sizeof(METADATAHDR));
119  if (!assembly->metadatahdr)
120  return E_OUTOFMEMORY;
121 
123  memcpy(assembly->metadatahdr, metadatahdr, size);
124 
125  assembly->metadatahdr->Version = (LPSTR)&metadatahdr->Version;
126 
128  ptr += FIELD_OFFSET(METADATAHDR, Version) + metadatahdr->VersionLength + 1;
129  dest = (BYTE *)assembly->metadatahdr + ofs;
130  memcpy(dest, ptr, sizeof(METADATAHDR) - ofs);
131 
132  *hdrsz = sizeof(METADATAHDR) - sizeof(LPSTR) + metadatahdr->VersionLength + 1;
133 
134  return S_OK;
135 }
IN PVOID IN PVOID IN USHORT Version
Definition: pci.h:359
char * LPSTR
Definition: xmlstorage.h:182
#define E_FAIL
Definition: ddrawi.h:102
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170
#define E_OUTOFMEMORY
Definition: ddrawi.h:100
ULONG VersionLength
Definition: fusionpriv.h:48
static PVOID ptr
Definition: dispmode.c:27
static void * assembly_rva_to_va(ASSEMBLY *assembly, ULONG rva)
Definition: assembly.c:81
GLsizeiptr size
Definition: glext.h:5919
#define GetProcessHeap()
Definition: compat.h:403
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
unsigned long DWORD
Definition: ntddk_ex.h:95
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
unsigned char BYTE
Definition: mem.h:68
#define S_OK
Definition: intsafe.h:59
#define FIELD_OFFSET(t, f)
Definition: typedefs.h:254
unsigned int ULONG
Definition: retypes.h:1
LPSTR Version
Definition: fusionpriv.h:49
static char * dest
Definition: rtl.c:135

Referenced by parse_clr_metadata().

◆ parse_pe_header()

static HRESULT parse_pe_header ( ASSEMBLY assembly)
static

Definition at line 149 of file assembly.c.

150 {
151  IMAGE_DATA_DIRECTORY *datadirs;
152 
153  assembly->nthdr = ImageNtHeader(assembly->data);
154  if (!assembly->nthdr)
155  return E_FAIL;
156 
157  if (assembly->nthdr->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
158  {
159  IMAGE_OPTIONAL_HEADER64 *opthdr =
160  (IMAGE_OPTIONAL_HEADER64 *)&assembly->nthdr->OptionalHeader;
161  datadirs = opthdr->DataDirectory;
162  }
163  else
164  {
165  IMAGE_OPTIONAL_HEADER32 *opthdr =
166  (IMAGE_OPTIONAL_HEADER32 *)&assembly->nthdr->OptionalHeader;
167  datadirs = opthdr->DataDirectory;
168  }
169 
170  if (!datadirs)
171  return E_FAIL;
172 
174  return E_FAIL;
175 
176  return S_OK;
177 }
IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES]
Definition: ntddk_ex.h:178
#define IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
Definition: ntimage.h:489
#define E_FAIL
Definition: ddrawi.h:102
#define IMAGE_NT_OPTIONAL_HDR64_MAGIC
Definition: ntimage.h:377
#define S_OK
Definition: intsafe.h:59
static ULONG assembly_datadir_get_data(ASSEMBLY *assembly, IMAGE_DATA_DIRECTORY *datadir, void **data)
Definition: assembly.c:89
IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES]
Definition: ntimage.h:370
PIMAGE_NT_HEADERS WINAPI ImageNtHeader(_In_ PVOID)

Referenced by parse_headers().

◆ strdupW()

static LPWSTR strdupW ( LPCWSTR  src)
inlinestatic

Definition at line 67 of file assembly.c.

68 {
69  LPWSTR dest;
70 
71  if (!src)
72  return NULL;
73 
74  dest = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(src) + 1) * sizeof(WCHAR));
75  if (dest)
76  lstrcpyW(dest, src);
77 
78  return dest;
79 }
#define lstrlenW
Definition: compat.h:415
smooth NULL
Definition: ftsmooth.c:416
#define GetProcessHeap()
Definition: compat.h:403
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
__wchar_t WCHAR
Definition: xmlstorage.h:180
GLenum src
Definition: glext.h:6340
#define lstrcpyW
Definition: compat.h:414
static char * dest
Definition: rtl.c:135
WCHAR * LPWSTR
Definition: xmlstorage.h:184

Referenced by assembly_create(), and msi_load_assembly().