2006-03-21 11:30:48 +01:00
|
|
|
/*
|
|
|
|
* Creation of Wine fake dlls for apps that access the dll file directly.
|
|
|
|
*
|
2011-08-25 15:33:41 +02:00
|
|
|
* Copyright 2006, 2011 Alexandre Julliard
|
2006-03-21 11:30:48 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2006-03-21 11:30:48 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
2009-08-18 11:38:22 +02:00
|
|
|
#include <fcntl.h>
|
2019-07-04 15:41:03 +02:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2006-03-21 11:30:48 +01:00
|
|
|
|
2011-08-02 13:47:43 +02:00
|
|
|
#define COBJMACROS
|
|
|
|
#define ATL_INITGUID
|
2015-03-18 19:01:26 +01:00
|
|
|
|
2006-03-21 11:30:48 +01:00
|
|
|
#include "ntstatus.h"
|
|
|
|
#define WIN32_NO_STATUS
|
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
|
|
|
#include "winuser.h"
|
|
|
|
#include "winnt.h"
|
|
|
|
#include "winternl.h"
|
|
|
|
#include "wine/debug.h"
|
2011-08-02 13:47:43 +02:00
|
|
|
#include "ole2.h"
|
|
|
|
#include "atliface.h"
|
2006-03-21 11:30:48 +01:00
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(setupapi);
|
|
|
|
|
2019-04-22 11:40:41 +02:00
|
|
|
static const char builtin_signature[] = "Wine builtin DLL";
|
2006-03-21 11:30:48 +01:00
|
|
|
static const char fakedll_signature[] = "Wine placeholder DLL";
|
|
|
|
|
|
|
|
static const unsigned int file_alignment = 512;
|
|
|
|
static const unsigned int section_alignment = 4096;
|
2009-10-15 13:53:14 +02:00
|
|
|
static const unsigned int max_dll_name_len = 64;
|
2006-03-21 11:30:48 +01:00
|
|
|
|
2009-10-15 12:20:14 +02:00
|
|
|
static void *file_buffer;
|
2011-08-02 12:58:26 +02:00
|
|
|
static SIZE_T file_buffer_size;
|
2009-10-15 13:31:24 +02:00
|
|
|
static unsigned int handled_count;
|
|
|
|
static unsigned int handled_total;
|
2019-07-04 15:41:03 +02:00
|
|
|
static WCHAR **handled_dlls;
|
2011-08-02 13:47:43 +02:00
|
|
|
static IRegistrar *registrar;
|
2009-10-15 12:20:14 +02:00
|
|
|
|
2006-03-21 11:30:48 +01:00
|
|
|
struct dll_info
|
|
|
|
{
|
|
|
|
HANDLE handle;
|
|
|
|
IMAGE_NT_HEADERS *nt;
|
|
|
|
DWORD file_pos;
|
|
|
|
DWORD mem_pos;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define ALIGN(size,align) (((size) + (align) - 1) & ~((align) - 1))
|
|
|
|
|
|
|
|
/* contents of the dll sections */
|
|
|
|
|
|
|
|
static const BYTE dll_code_section[] = { 0x31, 0xc0, /* xor %eax,%eax */
|
|
|
|
0xc2, 0x0c, 0x00 }; /* ret $12 */
|
|
|
|
|
|
|
|
static const BYTE exe_code_section[] = { 0xb8, 0x01, 0x00, 0x00, 0x00, /* movl $1,%eax */
|
|
|
|
0xc2, 0x04, 0x00 }; /* ret $4 */
|
|
|
|
|
|
|
|
static const IMAGE_BASE_RELOCATION reloc_section; /* empty relocs */
|
|
|
|
|
|
|
|
|
|
|
|
/* wrapper for WriteFile */
|
|
|
|
static inline BOOL xwrite( struct dll_info *info, const void *data, DWORD size, DWORD offset )
|
|
|
|
{
|
|
|
|
DWORD res;
|
|
|
|
|
|
|
|
return (SetFilePointer( info->handle, offset, NULL, FILE_BEGIN ) != INVALID_SET_FILE_POINTER &&
|
|
|
|
WriteFile( info->handle, data, size, &res, NULL ) &&
|
|
|
|
res == size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add a new section to the dll NT header */
|
|
|
|
static void add_section( struct dll_info *info, const char *name, DWORD size, DWORD flags )
|
|
|
|
{
|
|
|
|
IMAGE_SECTION_HEADER *sec = (IMAGE_SECTION_HEADER *)(info->nt + 1);
|
|
|
|
|
|
|
|
sec += info->nt->FileHeader.NumberOfSections;
|
2009-01-28 10:24:55 +01:00
|
|
|
memcpy( sec->Name, name, min( strlen(name), sizeof(sec->Name)) );
|
2006-03-21 11:30:48 +01:00
|
|
|
sec->Misc.VirtualSize = ALIGN( size, section_alignment );
|
|
|
|
sec->VirtualAddress = info->mem_pos;
|
|
|
|
sec->SizeOfRawData = size;
|
|
|
|
sec->PointerToRawData = info->file_pos;
|
|
|
|
sec->Characteristics = flags;
|
|
|
|
info->file_pos += ALIGN( size, file_alignment );
|
|
|
|
info->mem_pos += ALIGN( size, section_alignment );
|
|
|
|
info->nt->FileHeader.NumberOfSections++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add a data directory to the dll NT header */
|
|
|
|
static inline void add_directory( struct dll_info *info, unsigned int idx, DWORD rva, DWORD size )
|
|
|
|
{
|
|
|
|
info->nt->OptionalHeader.DataDirectory[idx].VirtualAddress = rva;
|
|
|
|
info->nt->OptionalHeader.DataDirectory[idx].Size = size;
|
|
|
|
}
|
|
|
|
|
2009-10-15 13:31:24 +02:00
|
|
|
/* add a dll to the list of dll that have been taken care of */
|
|
|
|
static BOOL add_handled_dll( const WCHAR *name )
|
|
|
|
{
|
|
|
|
int i, min, max, pos, res;
|
|
|
|
|
|
|
|
min = 0;
|
|
|
|
max = handled_count - 1;
|
|
|
|
while (min <= max)
|
|
|
|
{
|
|
|
|
pos = (min + max) / 2;
|
2019-07-04 15:41:03 +02:00
|
|
|
res = wcscmp( handled_dlls[pos], name );
|
|
|
|
if (!res) return FALSE; /* already in the list */
|
2009-10-15 13:31:24 +02:00
|
|
|
if (res < 0) min = pos + 1;
|
|
|
|
else max = pos - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (handled_count >= handled_total)
|
|
|
|
{
|
2019-07-04 15:41:03 +02:00
|
|
|
WCHAR **new_dlls;
|
2009-10-15 13:31:24 +02:00
|
|
|
unsigned int new_count = max( 64, handled_total * 2 );
|
|
|
|
|
|
|
|
if (handled_dlls) new_dlls = HeapReAlloc( GetProcessHeap(), 0, handled_dlls,
|
|
|
|
new_count * sizeof(*handled_dlls) );
|
|
|
|
else new_dlls = HeapAlloc( GetProcessHeap(), 0, new_count * sizeof(*handled_dlls) );
|
2019-07-04 15:41:03 +02:00
|
|
|
if (!new_dlls) return FALSE;
|
2009-10-15 13:31:24 +02:00
|
|
|
handled_dlls = new_dlls;
|
|
|
|
handled_total = new_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = handled_count; i > min; i--) handled_dlls[i] = handled_dlls[i - 1];
|
2019-07-04 15:41:03 +02:00
|
|
|
handled_dlls[i] = wcsdup( name );
|
2009-10-15 13:31:24 +02:00
|
|
|
handled_count++;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-05-18 09:29:04 +02:00
|
|
|
static int is_valid_ptr( const void *data, SIZE_T size, const void *ptr, SIZE_T ptr_size )
|
|
|
|
{
|
|
|
|
if (ptr < data) return 0;
|
|
|
|
if ((char *)ptr - (char *)data >= size) return 0;
|
|
|
|
return (size - ((char *)ptr - (char *)data) >= ptr_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* extract the 16-bit NE dll from a PE builtin */
|
|
|
|
static void extract_16bit_image( IMAGE_NT_HEADERS *nt, void **data, SIZE_T *size )
|
|
|
|
{
|
|
|
|
DWORD exp_size, *size_ptr;
|
|
|
|
IMAGE_DOS_HEADER *dos;
|
|
|
|
IMAGE_EXPORT_DIRECTORY *exports;
|
|
|
|
IMAGE_SECTION_HEADER *section = NULL;
|
|
|
|
WORD *ordinals;
|
|
|
|
DWORD *names, *functions;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
exports = RtlImageDirectoryEntryToData( *data, FALSE, IMAGE_DIRECTORY_ENTRY_EXPORT, &exp_size );
|
|
|
|
if (!is_valid_ptr( *data, *size, exports, exp_size )) return;
|
|
|
|
ordinals = RtlImageRvaToVa( nt, *data, exports->AddressOfNameOrdinals, §ion );
|
|
|
|
names = RtlImageRvaToVa( nt, *data, exports->AddressOfNames, §ion );
|
|
|
|
functions = RtlImageRvaToVa( nt, *data, exports->AddressOfFunctions, §ion );
|
|
|
|
if (!is_valid_ptr( *data, *size, ordinals, exports->NumberOfNames * sizeof(*ordinals) )) return;
|
|
|
|
if (!is_valid_ptr( *data, *size, names, exports->NumberOfNames * sizeof(*names) )) return;
|
|
|
|
|
|
|
|
for (i = 0; i < exports->NumberOfNames; i++)
|
|
|
|
{
|
|
|
|
char *ename = RtlImageRvaToVa( nt, *data, names[i], §ion );
|
|
|
|
if (strcmp( ename, "__wine_spec_dos_header" )) continue;
|
|
|
|
if (ordinals[i] >= exports->NumberOfFunctions) return;
|
|
|
|
if (!is_valid_ptr( *data, *size, functions, sizeof(*functions) )) return;
|
|
|
|
if (!functions[ordinals[i]]) return;
|
|
|
|
dos = RtlImageRvaToVa( nt, *data, functions[ordinals[i]], NULL );
|
|
|
|
if (!is_valid_ptr( *data, *size, dos, sizeof(*dos) )) return;
|
|
|
|
if (dos->e_magic != IMAGE_DOS_SIGNATURE) return;
|
|
|
|
size_ptr = (DWORD *)dos->e_res2;
|
|
|
|
*size = min( *size_ptr, *size - ((const char *)dos - (const char *)*data) );
|
|
|
|
*size_ptr = 0;
|
|
|
|
*data = dos;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-15 12:20:14 +02:00
|
|
|
/* read in the contents of a file into the global file buffer */
|
|
|
|
/* return 1 on success, 0 on nonexistent file, -1 on other error */
|
2019-07-04 15:41:03 +02:00
|
|
|
static int read_file( const WCHAR *name, void **data, SIZE_T *size, BOOL expect_builtin )
|
2009-10-15 12:20:14 +02:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
int fd, ret = -1;
|
2009-10-15 13:25:07 +02:00
|
|
|
size_t header_size;
|
|
|
|
IMAGE_DOS_HEADER *dos;
|
|
|
|
IMAGE_NT_HEADERS *nt;
|
2019-04-22 11:40:41 +02:00
|
|
|
const char *signature = expect_builtin ? builtin_signature : fakedll_signature;
|
|
|
|
const size_t min_size = sizeof(*dos) + 32 +
|
2009-10-15 13:25:07 +02:00
|
|
|
FIELD_OFFSET( IMAGE_NT_HEADERS, OptionalHeader.MajorLinkerVersion );
|
2009-10-15 12:20:14 +02:00
|
|
|
|
2019-07-04 15:41:03 +02:00
|
|
|
if ((fd = _wopen( name, O_RDONLY | O_BINARY )) == -1) return 0;
|
2009-10-15 12:20:14 +02:00
|
|
|
if (fstat( fd, &st ) == -1) goto done;
|
|
|
|
*size = st.st_size;
|
2011-08-02 12:58:26 +02:00
|
|
|
if (!file_buffer || st.st_size > file_buffer_size)
|
2009-10-15 12:20:14 +02:00
|
|
|
{
|
2011-08-02 12:58:26 +02:00
|
|
|
VirtualFree( file_buffer, 0, MEM_RELEASE );
|
|
|
|
file_buffer = NULL;
|
|
|
|
file_buffer_size = st.st_size;
|
|
|
|
if (NtAllocateVirtualMemory( GetCurrentProcess(), &file_buffer, 0, &file_buffer_size,
|
|
|
|
MEM_COMMIT, PAGE_READWRITE )) goto done;
|
2009-10-15 12:20:14 +02:00
|
|
|
}
|
|
|
|
|
2009-10-15 13:25:07 +02:00
|
|
|
/* check for valid fake dll file */
|
|
|
|
|
|
|
|
if (st.st_size < min_size) goto done;
|
|
|
|
header_size = min( st.st_size, 4096 );
|
2019-07-04 15:41:03 +02:00
|
|
|
if (read( fd, file_buffer, header_size ) != header_size) goto done;
|
2011-08-02 12:58:26 +02:00
|
|
|
dos = file_buffer;
|
2009-10-15 13:25:07 +02:00
|
|
|
if (dos->e_magic != IMAGE_DOS_SIGNATURE) goto done;
|
2019-04-22 11:40:41 +02:00
|
|
|
if (dos->e_lfanew < strlen(signature) + 1) goto done;
|
|
|
|
if (memcmp( dos + 1, signature, strlen(signature) + 1 )) goto done;
|
2009-10-15 13:25:07 +02:00
|
|
|
if (dos->e_lfanew + FIELD_OFFSET(IMAGE_NT_HEADERS,OptionalHeader.MajorLinkerVersion) > header_size)
|
|
|
|
goto done;
|
2011-08-02 12:58:26 +02:00
|
|
|
nt = (IMAGE_NT_HEADERS *)((char *)file_buffer + dos->e_lfanew);
|
2009-10-15 13:25:07 +02:00
|
|
|
if (nt->Signature == IMAGE_NT_SIGNATURE && nt->OptionalHeader.Magic != IMAGE_NT_OPTIONAL_HDR_MAGIC)
|
|
|
|
{
|
|
|
|
/* wrong 32/64 type, pretend it doesn't exist */
|
|
|
|
ret = 0;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (st.st_size == header_size ||
|
2019-07-04 15:41:03 +02:00
|
|
|
read( fd, (char *)file_buffer + header_size,
|
|
|
|
st.st_size - header_size ) == st.st_size - header_size)
|
2009-10-15 12:20:14 +02:00
|
|
|
{
|
2011-08-02 12:58:26 +02:00
|
|
|
*data = file_buffer;
|
2020-02-11 09:00:21 +01:00
|
|
|
if (lstrlenW(name) > 2 && !wcscmp( name + lstrlenW(name) - 2, L"16" ))
|
2019-05-18 09:29:04 +02:00
|
|
|
extract_16bit_image( nt, data, size );
|
2009-10-15 12:20:14 +02:00
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
close( fd );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-10-14 17:07:32 +02:00
|
|
|
/* build a complete fake dll from scratch */
|
2013-05-20 12:48:09 +02:00
|
|
|
static BOOL build_fake_dll( HANDLE file, const WCHAR *name )
|
2006-03-21 11:30:48 +01:00
|
|
|
{
|
|
|
|
IMAGE_DOS_HEADER *dos;
|
|
|
|
IMAGE_NT_HEADERS *nt;
|
|
|
|
struct dll_info info;
|
2013-05-20 12:48:09 +02:00
|
|
|
const WCHAR *ext;
|
2006-03-21 11:30:48 +01:00
|
|
|
BYTE *buffer;
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
DWORD lfanew = (sizeof(*dos) + sizeof(fakedll_signature) + 15) & ~15;
|
|
|
|
DWORD size, header_size = lfanew + sizeof(*nt);
|
|
|
|
|
|
|
|
info.handle = file;
|
|
|
|
buffer = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, header_size + 8 * sizeof(IMAGE_SECTION_HEADER) );
|
|
|
|
|
|
|
|
dos = (IMAGE_DOS_HEADER *)buffer;
|
|
|
|
dos->e_magic = IMAGE_DOS_SIGNATURE;
|
|
|
|
dos->e_cblp = sizeof(*dos);
|
|
|
|
dos->e_cp = 1;
|
|
|
|
dos->e_cparhdr = lfanew / 16;
|
|
|
|
dos->e_minalloc = 0;
|
|
|
|
dos->e_maxalloc = 0xffff;
|
|
|
|
dos->e_ss = 0x0000;
|
|
|
|
dos->e_sp = 0x00b8;
|
|
|
|
dos->e_lfarlc = lfanew;
|
|
|
|
dos->e_lfanew = lfanew;
|
|
|
|
memcpy( dos + 1, fakedll_signature, sizeof(fakedll_signature) );
|
|
|
|
|
|
|
|
nt = info.nt = (IMAGE_NT_HEADERS *)(buffer + lfanew);
|
|
|
|
/* some fields are copied from the source dll */
|
2014-03-22 16:41:30 +01:00
|
|
|
#if defined __x86_64__
|
2010-03-27 11:50:23 +01:00
|
|
|
nt->FileHeader.Machine = IMAGE_FILE_MACHINE_AMD64;
|
2014-03-22 16:41:30 +01:00
|
|
|
#elif defined __aarch64__
|
|
|
|
nt->FileHeader.Machine = IMAGE_FILE_MACHINE_ARM64;
|
2014-03-25 19:46:04 +01:00
|
|
|
#elif defined __arm__
|
|
|
|
nt->FileHeader.Machine = IMAGE_FILE_MACHINE_ARMNT;
|
2014-03-22 16:41:30 +01:00
|
|
|
#elif defined __powerpc__
|
|
|
|
nt->FileHeader.Machine = IMAGE_FILE_MACHINE_POWERPC;
|
2010-03-27 11:50:23 +01:00
|
|
|
#else
|
2009-10-14 17:07:32 +02:00
|
|
|
nt->FileHeader.Machine = IMAGE_FILE_MACHINE_I386;
|
2010-03-27 11:50:23 +01:00
|
|
|
#endif
|
2009-10-14 17:07:32 +02:00
|
|
|
nt->FileHeader.TimeDateStamp = 0;
|
2013-05-20 12:48:09 +02:00
|
|
|
nt->FileHeader.Characteristics = 0;
|
2009-10-14 17:07:32 +02:00
|
|
|
nt->OptionalHeader.MajorLinkerVersion = 1;
|
|
|
|
nt->OptionalHeader.MinorLinkerVersion = 0;
|
|
|
|
nt->OptionalHeader.MajorOperatingSystemVersion = 1;
|
|
|
|
nt->OptionalHeader.MinorOperatingSystemVersion = 0;
|
|
|
|
nt->OptionalHeader.MajorImageVersion = 1;
|
|
|
|
nt->OptionalHeader.MinorImageVersion = 0;
|
|
|
|
nt->OptionalHeader.MajorSubsystemVersion = 4;
|
|
|
|
nt->OptionalHeader.MinorSubsystemVersion = 0;
|
|
|
|
nt->OptionalHeader.Win32VersionValue = 0;
|
|
|
|
nt->OptionalHeader.Subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
|
|
|
|
nt->OptionalHeader.DllCharacteristics = 0;
|
|
|
|
nt->OptionalHeader.SizeOfStackReserve = 0;
|
|
|
|
nt->OptionalHeader.SizeOfStackCommit = 0;
|
|
|
|
nt->OptionalHeader.SizeOfHeapReserve = 0;
|
|
|
|
nt->OptionalHeader.SizeOfHeapCommit = 0;
|
2006-03-21 11:30:48 +01:00
|
|
|
/* other fields have fixed values */
|
|
|
|
nt->Signature = IMAGE_NT_SIGNATURE;
|
|
|
|
nt->FileHeader.NumberOfSections = 0;
|
|
|
|
nt->FileHeader.SizeOfOptionalHeader = IMAGE_SIZEOF_NT_OPTIONAL_HEADER;
|
|
|
|
nt->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
|
|
|
|
nt->OptionalHeader.ImageBase = 0x10000000;
|
|
|
|
nt->OptionalHeader.SectionAlignment = section_alignment;
|
|
|
|
nt->OptionalHeader.FileAlignment = file_alignment;
|
|
|
|
nt->OptionalHeader.NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
|
|
|
|
|
|
|
|
header_size = (BYTE *)(nt + 1) - buffer;
|
|
|
|
info.mem_pos = ALIGN( header_size, section_alignment );
|
|
|
|
info.file_pos = ALIGN( header_size, file_alignment );
|
|
|
|
|
|
|
|
nt->OptionalHeader.AddressOfEntryPoint = info.mem_pos;
|
|
|
|
nt->OptionalHeader.BaseOfCode = info.mem_pos;
|
|
|
|
|
2019-07-04 15:41:03 +02:00
|
|
|
ext = wcsrchr( name, '.' );
|
2020-02-11 09:00:21 +01:00
|
|
|
if (!ext || wcsicmp( ext, L".exe" )) nt->FileHeader.Characteristics |= IMAGE_FILE_DLL;
|
2013-05-20 12:48:09 +02:00
|
|
|
|
2006-03-21 11:30:48 +01:00
|
|
|
if (nt->FileHeader.Characteristics & IMAGE_FILE_DLL)
|
|
|
|
{
|
|
|
|
size = sizeof(dll_code_section);
|
|
|
|
if (!xwrite( &info, dll_code_section, size, info.file_pos )) goto done;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size = sizeof(exe_code_section);
|
|
|
|
if (!xwrite( &info, exe_code_section, size, info.file_pos )) goto done;
|
|
|
|
}
|
|
|
|
nt->OptionalHeader.SizeOfCode = size;
|
|
|
|
add_section( &info, ".text", size, IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ );
|
|
|
|
|
|
|
|
if (!xwrite( &info, &reloc_section, sizeof(reloc_section), info.file_pos )) goto done;
|
|
|
|
add_directory( &info, IMAGE_DIRECTORY_ENTRY_BASERELOC, info.mem_pos, sizeof(reloc_section) );
|
|
|
|
add_section( &info, ".reloc", sizeof(reloc_section),
|
|
|
|
IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE | IMAGE_SCN_MEM_READ );
|
|
|
|
|
|
|
|
header_size += nt->FileHeader.NumberOfSections * sizeof(IMAGE_SECTION_HEADER);
|
|
|
|
nt->OptionalHeader.SizeOfHeaders = ALIGN( header_size, file_alignment );
|
|
|
|
nt->OptionalHeader.SizeOfImage = ALIGN( info.mem_pos, section_alignment );
|
|
|
|
ret = xwrite( &info, buffer, header_size, 0 );
|
|
|
|
done:
|
|
|
|
HeapFree( GetProcessHeap(), 0, buffer );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if an existing file is a fake dll so that we can overwrite it */
|
|
|
|
static BOOL is_fake_dll( HANDLE h )
|
|
|
|
{
|
|
|
|
IMAGE_DOS_HEADER *dos;
|
|
|
|
DWORD size;
|
2019-04-22 11:40:41 +02:00
|
|
|
BYTE buffer[sizeof(*dos) + 32];
|
2006-03-21 11:30:48 +01:00
|
|
|
|
|
|
|
if (!ReadFile( h, buffer, sizeof(buffer), &size, NULL ) || size != sizeof(buffer))
|
|
|
|
return FALSE;
|
|
|
|
dos = (IMAGE_DOS_HEADER *)buffer;
|
|
|
|
if (dos->e_magic != IMAGE_DOS_SIGNATURE) return FALSE;
|
|
|
|
if (dos->e_lfanew < size) return FALSE;
|
2019-04-22 11:40:41 +02:00
|
|
|
return (!memcmp( dos + 1, builtin_signature, sizeof(builtin_signature) ) ||
|
|
|
|
!memcmp( dos + 1, fakedll_signature, sizeof(fakedll_signature) ));
|
2006-03-21 11:30:48 +01:00
|
|
|
}
|
|
|
|
|
2008-04-17 20:34:06 +02:00
|
|
|
/* create directories leading to a given file */
|
|
|
|
static void create_directories( const WCHAR *name )
|
|
|
|
{
|
|
|
|
WCHAR *path, *p;
|
|
|
|
|
|
|
|
/* create the directory/directories */
|
2019-07-04 15:41:03 +02:00
|
|
|
path = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(name) + 1)*sizeof(WCHAR));
|
|
|
|
lstrcpyW(path, name);
|
2008-04-17 20:34:06 +02:00
|
|
|
|
2019-07-04 15:41:03 +02:00
|
|
|
p = wcschr(path, '\\');
|
2008-04-17 20:34:06 +02:00
|
|
|
while (p != NULL)
|
|
|
|
{
|
|
|
|
*p = 0;
|
|
|
|
if (!CreateDirectoryW(path, NULL))
|
|
|
|
TRACE("Couldn't create directory %s - error: %d\n", wine_dbgstr_w(path), GetLastError());
|
|
|
|
*p = '\\';
|
2019-07-04 15:41:03 +02:00
|
|
|
p = wcschr(p+1, '\\');
|
2008-04-17 20:34:06 +02:00
|
|
|
}
|
|
|
|
HeapFree(GetProcessHeap(), 0, path);
|
|
|
|
}
|
|
|
|
|
2019-07-04 15:41:03 +02:00
|
|
|
static inline WCHAR *prepend( WCHAR *buffer, const WCHAR *str, size_t len )
|
2009-08-18 11:38:22 +02:00
|
|
|
{
|
2019-07-04 15:41:03 +02:00
|
|
|
return memcpy( buffer - len, str, len * sizeof(WCHAR) );
|
|
|
|
}
|
|
|
|
|
|
|
|
static const WCHAR *enum_load_path( unsigned int idx )
|
|
|
|
{
|
|
|
|
WCHAR buffer[32];
|
2020-02-11 09:00:21 +01:00
|
|
|
swprintf( buffer, ARRAY_SIZE(buffer), L"WINEDLLDIR%u", idx );
|
2019-07-04 15:41:03 +02:00
|
|
|
return _wgetenv( buffer );
|
2009-08-18 11:38:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* try to load a pre-compiled fake dll */
|
2011-08-02 12:58:26 +02:00
|
|
|
static void *load_fake_dll( const WCHAR *name, SIZE_T *size )
|
2009-08-18 11:38:22 +02:00
|
|
|
{
|
2020-02-11 09:00:21 +01:00
|
|
|
const WCHAR *build_dir = _wgetenv( L"WINEBUILDDIR" );
|
2019-07-04 15:41:03 +02:00
|
|
|
const WCHAR *path;
|
|
|
|
WCHAR *file, *ptr;
|
2009-10-15 12:20:14 +02:00
|
|
|
void *data = NULL;
|
2009-08-18 11:38:22 +02:00
|
|
|
unsigned int i, pos, len, namelen, maxlen = 0;
|
|
|
|
WCHAR *p;
|
2009-10-15 12:20:14 +02:00
|
|
|
int res = 0;
|
2009-08-18 11:38:22 +02:00
|
|
|
|
2019-07-04 15:41:03 +02:00
|
|
|
if ((p = wcsrchr( name, '\\' ))) name = p + 1;
|
2009-08-18 11:38:22 +02:00
|
|
|
|
|
|
|
i = 0;
|
2019-07-04 15:41:03 +02:00
|
|
|
len = lstrlenW( name );
|
2020-02-11 09:00:21 +01:00
|
|
|
if (build_dir) maxlen = lstrlenW(build_dir) + ARRAY_SIZE(L"\\programs") + len + 1;
|
2019-07-04 15:41:03 +02:00
|
|
|
while ((path = enum_load_path( i++ ))) maxlen = max( maxlen, lstrlenW(path) );
|
2020-02-11 09:00:21 +01:00
|
|
|
maxlen += ARRAY_SIZE(L"\\fakedlls") + len + ARRAY_SIZE(L".fake");
|
2009-08-18 11:38:22 +02:00
|
|
|
|
2019-07-04 15:41:03 +02:00
|
|
|
if (!(file = HeapAlloc( GetProcessHeap(), 0, maxlen * sizeof(WCHAR) ))) return NULL;
|
2009-08-18 11:38:22 +02:00
|
|
|
|
2020-02-11 09:00:21 +01:00
|
|
|
pos = maxlen - len - ARRAY_SIZE(L".fake");
|
2019-07-04 15:41:03 +02:00
|
|
|
lstrcpyW( file + pos, name );
|
|
|
|
file[--pos] = '\\';
|
2009-08-18 11:38:22 +02:00
|
|
|
|
|
|
|
if (build_dir)
|
|
|
|
{
|
|
|
|
/* try as a dll */
|
|
|
|
ptr = file + pos;
|
|
|
|
namelen = len + 1;
|
2019-04-22 11:40:41 +02:00
|
|
|
file[pos + len + 1] = 0;
|
2020-02-11 09:00:21 +01:00
|
|
|
if (namelen > 4 && !wcsncmp( ptr + namelen - 4, L".dll", 4 )) namelen -= 4;
|
2009-08-18 11:38:22 +02:00
|
|
|
ptr = prepend( ptr, ptr, namelen );
|
2020-02-11 09:00:21 +01:00
|
|
|
ptr = prepend( ptr, L"\\dlls", 5 );
|
2019-07-04 15:41:03 +02:00
|
|
|
ptr = prepend( ptr, build_dir, lstrlenW(build_dir) );
|
2019-04-22 11:40:41 +02:00
|
|
|
if ((res = read_file( ptr, &data, size, TRUE ))) goto done;
|
2020-02-11 09:00:21 +01:00
|
|
|
lstrcpyW( file + pos + len + 1, L".fake" );
|
2019-04-22 11:40:41 +02:00
|
|
|
if ((res = read_file( ptr, &data, size, FALSE ))) goto done;
|
2009-08-18 11:38:22 +02:00
|
|
|
|
|
|
|
/* now as a program */
|
|
|
|
ptr = file + pos;
|
|
|
|
namelen = len + 1;
|
2019-04-22 11:40:41 +02:00
|
|
|
file[pos + len + 1] = 0;
|
2020-02-11 09:00:21 +01:00
|
|
|
if (namelen > 4 && !wcsncmp( ptr + namelen - 4, L".exe", 4 )) namelen -= 4;
|
2009-08-18 11:38:22 +02:00
|
|
|
ptr = prepend( ptr, ptr, namelen );
|
2020-02-11 09:00:21 +01:00
|
|
|
ptr = prepend( ptr, L"\\programs", 9 );
|
2019-07-04 15:41:03 +02:00
|
|
|
ptr = prepend( ptr, build_dir, lstrlenW(build_dir) );
|
2019-04-22 11:40:41 +02:00
|
|
|
if ((res = read_file( ptr, &data, size, TRUE ))) goto done;
|
2020-02-11 09:00:21 +01:00
|
|
|
lstrcpyW( file + pos + len + 1, L".fake" );
|
2019-04-22 11:40:41 +02:00
|
|
|
if ((res = read_file( ptr, &data, size, FALSE ))) goto done;
|
2009-08-18 11:38:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
file[pos + len + 1] = 0;
|
2019-07-04 15:41:03 +02:00
|
|
|
for (i = 0; (path = enum_load_path( i )); i++)
|
2009-08-18 11:38:22 +02:00
|
|
|
{
|
2019-07-04 15:41:03 +02:00
|
|
|
ptr = prepend( file + pos, path, lstrlenW(path) );
|
2019-04-22 11:40:41 +02:00
|
|
|
if ((res = read_file( ptr, &data, size, TRUE ))) break;
|
2020-02-11 09:00:21 +01:00
|
|
|
ptr = prepend( file + pos, L"\\fakedlls", 9 );
|
2019-07-04 15:41:03 +02:00
|
|
|
ptr = prepend( ptr, path, lstrlenW(path) );
|
2019-04-22 11:40:41 +02:00
|
|
|
if ((res = read_file( ptr, &data, size, FALSE ))) break;
|
2009-08-18 11:38:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
HeapFree( GetProcessHeap(), 0, file );
|
2009-10-15 12:20:14 +02:00
|
|
|
if (res == 1) return data;
|
|
|
|
return NULL;
|
2009-08-18 11:38:22 +02:00
|
|
|
}
|
|
|
|
|
2009-10-15 12:25:11 +02:00
|
|
|
/* create the fake dll destination file */
|
|
|
|
static HANDLE create_dest_file( const WCHAR *name )
|
2006-03-21 11:30:48 +01:00
|
|
|
{
|
|
|
|
/* first check for an existing file */
|
2009-10-15 12:25:11 +02:00
|
|
|
HANDLE h = CreateFileW( name, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL );
|
2006-03-21 11:30:48 +01:00
|
|
|
if (h != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
if (!is_fake_dll( h ))
|
|
|
|
{
|
|
|
|
TRACE( "%s is not a fake dll, not overwriting it\n", debugstr_w(name) );
|
|
|
|
CloseHandle( h );
|
2009-10-15 12:25:11 +02:00
|
|
|
return 0;
|
2006-03-21 11:30:48 +01:00
|
|
|
}
|
|
|
|
/* truncate the file */
|
|
|
|
SetFilePointer( h, 0, NULL, FILE_BEGIN );
|
|
|
|
SetEndOfFile( h );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-04-17 20:34:06 +02:00
|
|
|
if (GetLastError() == ERROR_PATH_NOT_FOUND) create_directories( name );
|
2007-08-20 19:03:16 +02:00
|
|
|
|
2006-03-21 11:30:48 +01:00
|
|
|
h = CreateFileW( name, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL );
|
|
|
|
if (h == INVALID_HANDLE_VALUE)
|
2007-08-20 19:03:16 +02:00
|
|
|
ERR( "failed to create %s (error=%u)\n", debugstr_w(name), GetLastError() );
|
2006-03-21 11:30:48 +01:00
|
|
|
}
|
2009-10-15 12:25:11 +02:00
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
2011-08-25 15:33:41 +02:00
|
|
|
/* XML parsing code copied from ntdll */
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
const char *ptr;
|
|
|
|
unsigned int len;
|
|
|
|
} xmlstr_t;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
const char *ptr;
|
|
|
|
const char *end;
|
|
|
|
} xmlbuf_t;
|
|
|
|
|
|
|
|
static inline BOOL xmlstr_cmp(const xmlstr_t* xmlstr, const char *str)
|
|
|
|
{
|
|
|
|
return !strncmp(xmlstr->ptr, str, xmlstr->len) && !str[xmlstr->len];
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline BOOL isxmlspace( char ch )
|
|
|
|
{
|
|
|
|
return (ch == ' ' || ch == '\r' || ch == '\n' || ch == '\t');
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL next_xml_elem( xmlbuf_t *xmlbuf, xmlstr_t *elem )
|
|
|
|
{
|
|
|
|
const char *ptr;
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
ptr = memchr(xmlbuf->ptr, '<', xmlbuf->end - xmlbuf->ptr);
|
|
|
|
if (!ptr)
|
|
|
|
{
|
|
|
|
xmlbuf->ptr = xmlbuf->end;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
ptr++;
|
|
|
|
if (ptr + 3 < xmlbuf->end && ptr[0] == '!' && ptr[1] == '-' && ptr[2] == '-') /* skip comment */
|
|
|
|
{
|
|
|
|
for (ptr += 3; ptr + 3 <= xmlbuf->end; ptr++)
|
|
|
|
if (ptr[0] == '-' && ptr[1] == '-' && ptr[2] == '>') break;
|
|
|
|
|
|
|
|
if (ptr + 3 > xmlbuf->end)
|
|
|
|
{
|
|
|
|
xmlbuf->ptr = xmlbuf->end;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
xmlbuf->ptr = ptr + 3;
|
|
|
|
}
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
|
|
|
|
xmlbuf->ptr = ptr;
|
|
|
|
while (ptr < xmlbuf->end && !isxmlspace(*ptr) && *ptr != '>' && (*ptr != '/' || ptr == xmlbuf->ptr))
|
|
|
|
ptr++;
|
|
|
|
|
|
|
|
elem->ptr = xmlbuf->ptr;
|
|
|
|
elem->len = ptr - xmlbuf->ptr;
|
|
|
|
xmlbuf->ptr = ptr;
|
|
|
|
return xmlbuf->ptr != xmlbuf->end;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL next_xml_attr(xmlbuf_t* xmlbuf, xmlstr_t* name, xmlstr_t* value,
|
|
|
|
BOOL* error, BOOL* end)
|
|
|
|
{
|
|
|
|
const char *ptr;
|
|
|
|
|
|
|
|
*error = TRUE;
|
|
|
|
|
|
|
|
while (xmlbuf->ptr < xmlbuf->end && isxmlspace(*xmlbuf->ptr))
|
|
|
|
xmlbuf->ptr++;
|
|
|
|
|
|
|
|
if (xmlbuf->ptr == xmlbuf->end) return FALSE;
|
|
|
|
|
|
|
|
if (*xmlbuf->ptr == '/')
|
|
|
|
{
|
|
|
|
xmlbuf->ptr++;
|
|
|
|
if (xmlbuf->ptr == xmlbuf->end || *xmlbuf->ptr != '>')
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
xmlbuf->ptr++;
|
|
|
|
*end = TRUE;
|
|
|
|
*error = FALSE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*xmlbuf->ptr == '>')
|
|
|
|
{
|
|
|
|
xmlbuf->ptr++;
|
|
|
|
*error = FALSE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr = xmlbuf->ptr;
|
|
|
|
while (ptr < xmlbuf->end && *ptr != '=' && *ptr != '>' && !isxmlspace(*ptr)) ptr++;
|
|
|
|
|
|
|
|
if (ptr == xmlbuf->end || *ptr != '=') return FALSE;
|
|
|
|
|
|
|
|
name->ptr = xmlbuf->ptr;
|
|
|
|
name->len = ptr-xmlbuf->ptr;
|
|
|
|
xmlbuf->ptr = ptr;
|
|
|
|
|
|
|
|
ptr++;
|
|
|
|
if (ptr == xmlbuf->end || (*ptr != '"' && *ptr != '\'')) return FALSE;
|
|
|
|
|
|
|
|
value->ptr = ++ptr;
|
|
|
|
if (ptr == xmlbuf->end) return FALSE;
|
|
|
|
|
|
|
|
ptr = memchr(ptr, ptr[-1], xmlbuf->end - ptr);
|
|
|
|
if (!ptr)
|
|
|
|
{
|
|
|
|
xmlbuf->ptr = xmlbuf->end;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
value->len = ptr - value->ptr;
|
|
|
|
xmlbuf->ptr = ptr + 1;
|
|
|
|
|
|
|
|
if (xmlbuf->ptr == xmlbuf->end) return FALSE;
|
|
|
|
|
|
|
|
*error = FALSE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2020-02-11 09:00:21 +01:00
|
|
|
static void append_manifest_filename( const xmlstr_t *arch, const xmlstr_t *name, const xmlstr_t *key,
|
|
|
|
const xmlstr_t *version, const xmlstr_t *lang, WCHAR *buffer, DWORD size )
|
2011-08-25 15:33:41 +02:00
|
|
|
{
|
2020-02-11 09:00:21 +01:00
|
|
|
DWORD pos = lstrlenW( buffer );
|
2011-08-25 15:33:41 +02:00
|
|
|
|
2020-02-11 09:00:21 +01:00
|
|
|
pos += MultiByteToWideChar( CP_UTF8, 0, arch->ptr, arch->len, buffer + pos, size - pos );
|
2011-08-25 15:33:41 +02:00
|
|
|
buffer[pos++] = '_';
|
|
|
|
pos += MultiByteToWideChar( CP_UTF8, 0, name->ptr, name->len, buffer + pos, size - pos );
|
|
|
|
buffer[pos++] = '_';
|
|
|
|
pos += MultiByteToWideChar( CP_UTF8, 0, key->ptr, key->len, buffer + pos, size - pos );
|
|
|
|
buffer[pos++] = '_';
|
|
|
|
pos += MultiByteToWideChar( CP_UTF8, 0, version->ptr, version->len, buffer + pos, size - pos );
|
2011-08-27 13:53:00 +02:00
|
|
|
buffer[pos++] = '_';
|
|
|
|
pos += MultiByteToWideChar( CP_UTF8, 0, lang->ptr, lang->len, buffer + pos, size - pos );
|
2020-02-11 09:00:21 +01:00
|
|
|
lstrcpyW( buffer + pos, L"_deadbeef" );
|
2019-07-04 15:41:03 +02:00
|
|
|
wcslwr( buffer );
|
2011-08-25 15:33:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL create_winsxs_dll( const WCHAR *dll_name, const xmlstr_t *arch, const xmlstr_t *name,
|
2011-08-27 13:53:00 +02:00
|
|
|
const xmlstr_t *key, const xmlstr_t *version, const xmlstr_t *lang,
|
2011-08-25 15:33:41 +02:00
|
|
|
const void *dll_data, size_t dll_size )
|
|
|
|
{
|
|
|
|
WCHAR *path;
|
|
|
|
const WCHAR *filename;
|
2020-02-11 09:00:21 +01:00
|
|
|
DWORD written, path_len;
|
2011-08-25 15:33:41 +02:00
|
|
|
HANDLE handle;
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
2019-07-04 15:41:03 +02:00
|
|
|
if (!(filename = wcsrchr( dll_name, '\\' ))) filename = dll_name;
|
2011-08-25 15:33:41 +02:00
|
|
|
else filename++;
|
|
|
|
|
2020-02-11 09:00:21 +01:00
|
|
|
path_len = GetWindowsDirectoryW( NULL, 0 ) + ARRAY_SIZE( L"\\winsxs\\" )
|
2019-07-04 15:41:03 +02:00
|
|
|
+ arch->len + name->len + key->len + version->len + 18 + lstrlenW( filename ) + 1;
|
2011-08-25 15:33:41 +02:00
|
|
|
|
|
|
|
path = HeapAlloc( GetProcessHeap(), 0, path_len * sizeof(WCHAR) );
|
2020-02-11 09:00:21 +01:00
|
|
|
GetWindowsDirectoryW( path, path_len );
|
|
|
|
lstrcatW( path, L"\\winsxs\\" );
|
|
|
|
append_manifest_filename( arch, name, key, version, lang, path, path_len );
|
|
|
|
lstrcatW( path, L"\\" );
|
|
|
|
lstrcatW( path, filename );
|
2011-08-25 15:33:41 +02:00
|
|
|
handle = create_dest_file( path );
|
|
|
|
if (handle && handle != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
TRACE( "creating %s\n", debugstr_w(path) );
|
|
|
|
ret = (WriteFile( handle, dll_data, dll_size, &written, NULL ) && written == dll_size);
|
|
|
|
if (!ret) ERR( "failed to write to %s (error=%u)\n", debugstr_w(path), GetLastError() );
|
|
|
|
CloseHandle( handle );
|
|
|
|
if (!ret) DeleteFileW( path );
|
|
|
|
}
|
|
|
|
HeapFree( GetProcessHeap(), 0, path );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL create_manifest( const xmlstr_t *arch, const xmlstr_t *name, const xmlstr_t *key,
|
2011-08-27 13:53:00 +02:00
|
|
|
const xmlstr_t *version, const xmlstr_t *lang, const void *data, DWORD len )
|
2011-08-25 15:33:41 +02:00
|
|
|
{
|
|
|
|
WCHAR *path;
|
2020-02-11 09:00:21 +01:00
|
|
|
DWORD written, path_len;
|
2011-08-25 15:33:41 +02:00
|
|
|
HANDLE handle;
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
2020-02-11 09:00:21 +01:00
|
|
|
path_len = GetWindowsDirectoryW( NULL, 0 ) + ARRAY_SIZE( L"\\winsxs\\manifests\\" )
|
|
|
|
+ arch->len + name->len + key->len + version->len + 18 + ARRAY_SIZE( L".manifest" );
|
2011-08-25 15:33:41 +02:00
|
|
|
|
|
|
|
path = HeapAlloc( GetProcessHeap(), 0, path_len * sizeof(WCHAR) );
|
2020-02-11 09:00:21 +01:00
|
|
|
GetWindowsDirectoryW( path, path_len );
|
|
|
|
lstrcatW( path, L"\\winsxs\\manifests\\" );
|
|
|
|
append_manifest_filename( arch, name, key, version, lang, path, path_len );
|
|
|
|
lstrcatW( path, L".manifest" );
|
2011-08-25 15:33:41 +02:00
|
|
|
handle = CreateFileW( path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL );
|
|
|
|
if (handle == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND)
|
|
|
|
{
|
|
|
|
create_directories( path );
|
|
|
|
handle = CreateFileW( path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
if (handle != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
TRACE( "creating %s\n", debugstr_w(path) );
|
|
|
|
ret = (WriteFile( handle, data, len, &written, NULL ) && written == len);
|
|
|
|
if (!ret) ERR( "failed to write to %s (error=%u)\n", debugstr_w(path), GetLastError() );
|
|
|
|
CloseHandle( handle );
|
|
|
|
if (!ret) DeleteFileW( path );
|
|
|
|
}
|
|
|
|
HeapFree( GetProcessHeap(), 0, path );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-06-11 12:49:27 +02:00
|
|
|
struct dll_data {
|
|
|
|
const WCHAR *name;
|
|
|
|
const void *data;
|
|
|
|
SIZE_T size;
|
|
|
|
};
|
|
|
|
|
|
|
|
static BOOL CALLBACK register_manifest( HMODULE module, const WCHAR *type, WCHAR *res_name, LONG_PTR arg )
|
2011-08-25 15:33:41 +02:00
|
|
|
{
|
|
|
|
#ifdef __i386__
|
|
|
|
static const char current_arch[] = "x86";
|
|
|
|
#elif defined __x86_64__
|
|
|
|
static const char current_arch[] = "amd64";
|
2017-11-01 10:41:51 +01:00
|
|
|
#elif defined __arm__
|
|
|
|
static const char current_arch[] = "arm";
|
|
|
|
#elif defined __aarch64__
|
|
|
|
static const char current_arch[] = "arm64";
|
2011-08-25 15:33:41 +02:00
|
|
|
#else
|
|
|
|
static const char current_arch[] = "none";
|
|
|
|
#endif
|
2015-06-11 12:49:27 +02:00
|
|
|
const struct dll_data *dll_data = (const struct dll_data*)arg;
|
2011-08-25 15:33:41 +02:00
|
|
|
xmlbuf_t buffer;
|
|
|
|
xmlstr_t elem, attr_name, attr_value;
|
2011-08-27 13:53:00 +02:00
|
|
|
xmlstr_t name, version, arch, key, lang;
|
2011-08-25 15:33:41 +02:00
|
|
|
BOOL end = FALSE, error;
|
2015-06-11 12:49:27 +02:00
|
|
|
const char *manifest;
|
|
|
|
SIZE_T len;
|
|
|
|
HRSRC rsrc;
|
|
|
|
|
2020-02-11 09:00:21 +01:00
|
|
|
if (IS_INTRESOURCE( res_name ) || wcsncmp( res_name, L"WINE_MANIFEST", 13 )) return TRUE;
|
2015-06-11 12:49:27 +02:00
|
|
|
|
|
|
|
rsrc = FindResourceW( module, res_name, type );
|
|
|
|
manifest = LoadResource( module, rsrc );
|
|
|
|
len = SizeofResource( module, rsrc );
|
2011-08-25 15:33:41 +02:00
|
|
|
|
|
|
|
buffer.ptr = manifest;
|
|
|
|
buffer.end = manifest + len;
|
2011-08-27 13:53:00 +02:00
|
|
|
name.ptr = version.ptr = arch.ptr = key.ptr = lang.ptr = NULL;
|
|
|
|
name.len = version.len = arch.len = key.len = lang.len = 0;
|
2011-08-25 15:33:41 +02:00
|
|
|
|
|
|
|
while (next_xml_elem( &buffer, &elem ))
|
|
|
|
{
|
|
|
|
if (!xmlstr_cmp( &elem, "assemblyIdentity" )) continue;
|
|
|
|
while (next_xml_attr( &buffer, &attr_name, &attr_value, &error, &end ))
|
|
|
|
{
|
|
|
|
if (xmlstr_cmp(&attr_name, "name")) name = attr_value;
|
|
|
|
else if (xmlstr_cmp(&attr_name, "version")) version = attr_value;
|
|
|
|
else if (xmlstr_cmp(&attr_name, "processorArchitecture")) arch = attr_value;
|
|
|
|
else if (xmlstr_cmp(&attr_name, "publicKeyToken")) key = attr_value;
|
2011-08-27 13:53:00 +02:00
|
|
|
else if (xmlstr_cmp(&attr_name, "language")) lang = attr_value;
|
2011-08-25 15:33:41 +02:00
|
|
|
}
|
|
|
|
if (!error && name.ptr && version.ptr && arch.ptr && key.ptr)
|
|
|
|
{
|
2011-08-27 13:53:00 +02:00
|
|
|
if (!lang.ptr)
|
|
|
|
{
|
|
|
|
lang.ptr = "none";
|
|
|
|
lang.len = strlen( lang.ptr );
|
|
|
|
}
|
2011-08-25 15:33:41 +02:00
|
|
|
if (!arch.len) /* fixup the architecture */
|
|
|
|
{
|
|
|
|
char *new_buffer = HeapAlloc( GetProcessHeap(), 0, len + sizeof(current_arch) );
|
|
|
|
memcpy( new_buffer, manifest, arch.ptr - manifest );
|
|
|
|
strcpy( new_buffer + (arch.ptr - manifest), current_arch );
|
|
|
|
memcpy( new_buffer + strlen(new_buffer), arch.ptr, len - (arch.ptr - manifest) );
|
|
|
|
arch.ptr = current_arch;
|
|
|
|
arch.len = strlen( current_arch );
|
2015-06-11 12:49:27 +02:00
|
|
|
if (create_winsxs_dll( dll_data->name, &arch, &name, &key, &version, &lang, dll_data->data, dll_data->size ))
|
2011-08-27 13:53:00 +02:00
|
|
|
create_manifest( &arch, &name, &key, &version, &lang, new_buffer, len + arch.len );
|
2011-08-25 15:33:41 +02:00
|
|
|
HeapFree( GetProcessHeap(), 0, new_buffer );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-06-11 12:49:27 +02:00
|
|
|
if (create_winsxs_dll( dll_data->name, &arch, &name, &key, &version, &lang, dll_data->data, dll_data->size ))
|
2011-08-27 13:53:00 +02:00
|
|
|
create_manifest( &arch, &name, &key, &version, &lang, manifest, len );
|
2011-08-25 15:33:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-11 12:49:27 +02:00
|
|
|
|
|
|
|
return TRUE;
|
2011-08-25 15:33:41 +02:00
|
|
|
}
|
|
|
|
|
2011-08-02 13:47:43 +02:00
|
|
|
static BOOL CALLBACK register_resource( HMODULE module, LPCWSTR type, LPWSTR name, LONG_PTR arg )
|
|
|
|
{
|
|
|
|
HRESULT *hr = (HRESULT *)arg;
|
|
|
|
WCHAR *buffer;
|
|
|
|
HRSRC rsrc = FindResourceW( module, name, type );
|
|
|
|
char *str = LoadResource( module, rsrc );
|
|
|
|
DWORD lenW, lenA = SizeofResource( module, rsrc );
|
|
|
|
|
|
|
|
if (!str) return FALSE;
|
|
|
|
lenW = MultiByteToWideChar( CP_UTF8, 0, str, lenA, NULL, 0 ) + 1;
|
|
|
|
if (!(buffer = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) ))) return FALSE;
|
|
|
|
MultiByteToWideChar( CP_UTF8, 0, str, lenA, buffer, lenW );
|
|
|
|
buffer[lenW - 1] = 0;
|
|
|
|
*hr = IRegistrar_StringRegister( registrar, buffer );
|
|
|
|
HeapFree( GetProcessHeap(), 0, buffer );
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void register_fake_dll( const WCHAR *name, const void *data, size_t size )
|
|
|
|
{
|
|
|
|
const IMAGE_RESOURCE_DIRECTORY *resdir;
|
|
|
|
LDR_RESOURCE_INFO info;
|
|
|
|
HRESULT hr = S_OK;
|
|
|
|
HMODULE module = (HMODULE)((ULONG_PTR)data | 1);
|
2015-06-11 12:49:27 +02:00
|
|
|
struct dll_data dll_data = { name, data, size };
|
2011-08-25 15:33:41 +02:00
|
|
|
|
2015-06-11 12:49:27 +02:00
|
|
|
EnumResourceNamesW( module, (WCHAR*)RT_MANIFEST, register_manifest, (LONG_PTR)&dll_data );
|
2011-08-02 13:47:43 +02:00
|
|
|
|
2020-02-11 09:00:21 +01:00
|
|
|
info.Type = (ULONG_PTR)L"WINE_REGISTRY";
|
2011-08-02 13:47:43 +02:00
|
|
|
if (LdrFindResourceDirectory_U( module, &info, 1, &resdir )) return;
|
|
|
|
|
|
|
|
if (!registrar)
|
|
|
|
{
|
2012-11-28 21:25:00 +01:00
|
|
|
HRESULT (WINAPI *pAtlCreateRegistrar)(IRegistrar**);
|
2020-02-11 09:00:21 +01:00
|
|
|
HMODULE atl = LoadLibraryW( L"atl100.dll" );
|
2011-08-02 13:47:43 +02:00
|
|
|
|
2012-11-28 21:25:00 +01:00
|
|
|
if ((pAtlCreateRegistrar = (void *)GetProcAddress( atl, "AtlCreateRegistrar" )))
|
|
|
|
hr = pAtlCreateRegistrar( ®istrar );
|
|
|
|
else
|
|
|
|
hr = E_NOINTERFACE;
|
|
|
|
|
2011-08-02 13:47:43 +02:00
|
|
|
if (!registrar)
|
|
|
|
{
|
|
|
|
ERR( "failed to create IRegistrar: %x\n", hr );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TRACE( "registering %s\n", debugstr_w(name) );
|
|
|
|
IRegistrar_ClearReplacements( registrar );
|
2020-02-11 09:00:21 +01:00
|
|
|
IRegistrar_AddReplacement( registrar, L"MODULE", name );
|
|
|
|
EnumResourceNamesW( module, L"WINE_REGISTRY", register_resource, (LONG_PTR)&hr );
|
2011-08-02 13:47:43 +02:00
|
|
|
if (FAILED(hr)) ERR( "failed to register %s: %x\n", debugstr_w(name), hr );
|
|
|
|
}
|
|
|
|
|
2009-10-15 13:53:14 +02:00
|
|
|
/* copy a fake dll file to the dest directory */
|
2019-07-04 15:41:03 +02:00
|
|
|
static int install_fake_dll( WCHAR *dest, WCHAR *file, const WCHAR *ext, BOOL expect_builtin )
|
2009-10-15 13:53:14 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2011-08-02 12:58:26 +02:00
|
|
|
SIZE_T size;
|
2009-10-15 13:53:14 +02:00
|
|
|
void *data;
|
|
|
|
DWORD written;
|
2019-07-04 15:41:03 +02:00
|
|
|
WCHAR *destname = dest + lstrlenW(dest);
|
|
|
|
WCHAR *name = wcsrchr( file, '\\' ) + 1;
|
|
|
|
WCHAR *end = name + lstrlenW(name);
|
2009-10-15 13:53:14 +02:00
|
|
|
|
2019-07-04 15:41:03 +02:00
|
|
|
if (ext) lstrcpyW( end, ext );
|
2019-04-22 11:40:41 +02:00
|
|
|
if (!(ret = read_file( file, &data, &size, expect_builtin ))) return 0;
|
2009-10-15 13:53:14 +02:00
|
|
|
|
2020-02-11 09:00:21 +01:00
|
|
|
if (end > name + 2 && !wcsncmp( end - 2, L"16", 2 )) end -= 2; /* remove "16" suffix */
|
2019-07-04 15:41:03 +02:00
|
|
|
memcpy( destname, name, (end - name) * sizeof(WCHAR) );
|
|
|
|
destname[end - name] = 0;
|
2009-10-15 13:53:14 +02:00
|
|
|
if (!add_handled_dll( destname )) ret = -1;
|
|
|
|
|
|
|
|
if (ret != -1)
|
|
|
|
{
|
|
|
|
HANDLE h = create_dest_file( dest );
|
|
|
|
|
|
|
|
if (h && h != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
2019-07-04 15:41:03 +02:00
|
|
|
TRACE( "%s -> %s\n", debugstr_w(file), debugstr_w(dest) );
|
2009-10-15 13:53:14 +02:00
|
|
|
|
|
|
|
ret = (WriteFile( h, data, size, &written, NULL ) && written == size);
|
|
|
|
if (!ret) ERR( "failed to write to %s (error=%u)\n", debugstr_w(dest), GetLastError() );
|
|
|
|
CloseHandle( h );
|
2011-08-02 13:47:43 +02:00
|
|
|
if (ret) register_fake_dll( dest, data, size );
|
|
|
|
else DeleteFileW( dest );
|
2009-10-15 13:53:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
*destname = 0; /* restore it for next file */
|
2019-04-22 11:40:41 +02:00
|
|
|
return ret;
|
2009-10-15 13:53:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* find and install all fake dlls in a given lib directory */
|
2019-07-04 15:41:03 +02:00
|
|
|
static void install_lib_dir( WCHAR *dest, WCHAR *file, const WCHAR *default_ext, BOOL expect_builtin )
|
|
|
|
{
|
|
|
|
WCHAR *name;
|
|
|
|
intptr_t handle;
|
|
|
|
struct _wfinddata_t data;
|
|
|
|
|
|
|
|
file[1] = '\\'; /* change \??\ to \\?\ */
|
|
|
|
name = file + lstrlenW(file);
|
|
|
|
*name++ = '\\';
|
2020-02-11 09:00:21 +01:00
|
|
|
lstrcpyW( name, L"*" );
|
2019-07-04 15:41:03 +02:00
|
|
|
|
|
|
|
if ((handle = _wfindfirst( file, &data )) == -1) return;
|
|
|
|
do
|
2009-10-15 13:53:14 +02:00
|
|
|
{
|
2019-07-04 15:41:03 +02:00
|
|
|
if (lstrlenW( data.name ) > max_dll_name_len) continue;
|
2020-02-11 09:00:21 +01:00
|
|
|
if (!wcscmp( data.name, L"." )) continue;
|
|
|
|
if (!wcscmp( data.name, L".." )) continue;
|
2019-07-04 15:41:03 +02:00
|
|
|
lstrcpyW( name, data.name );
|
2009-10-15 13:53:14 +02:00
|
|
|
if (default_ext) /* inside build dir */
|
|
|
|
{
|
2020-02-11 09:00:21 +01:00
|
|
|
lstrcatW( name, L"\\" );
|
2019-07-04 15:41:03 +02:00
|
|
|
lstrcatW( name, data.name );
|
|
|
|
if (!wcschr( data.name, '.' )) lstrcatW( name, default_ext );
|
|
|
|
if (!install_fake_dll( dest, file, NULL, expect_builtin ))
|
2020-02-11 09:00:21 +01:00
|
|
|
install_fake_dll( dest, file, L".fake", FALSE );
|
2009-10-15 13:53:14 +02:00
|
|
|
}
|
2019-07-04 15:41:03 +02:00
|
|
|
else install_fake_dll( dest, file, NULL, expect_builtin );
|
2009-10-15 13:53:14 +02:00
|
|
|
}
|
2019-07-04 15:41:03 +02:00
|
|
|
while (!_wfindnext( handle, &data ));
|
|
|
|
_findclose( handle );
|
2009-10-15 13:53:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* create fake dlls in dirname for all the files we can find */
|
|
|
|
static BOOL create_wildcard_dlls( const WCHAR *dirname )
|
|
|
|
{
|
2020-02-11 09:00:21 +01:00
|
|
|
const WCHAR *build_dir = _wgetenv( L"WINEBUILDDIR" );
|
2019-07-04 15:41:03 +02:00
|
|
|
const WCHAR *path;
|
2009-10-15 13:53:14 +02:00
|
|
|
unsigned int i, maxlen = 0;
|
2019-07-04 15:41:03 +02:00
|
|
|
WCHAR *file, *dest;
|
2009-10-15 13:53:14 +02:00
|
|
|
|
2020-02-11 09:00:21 +01:00
|
|
|
if (build_dir) maxlen = lstrlenW(build_dir) + ARRAY_SIZE(L"\\programs") + 1;
|
2019-07-04 15:41:03 +02:00
|
|
|
for (i = 0; (path = enum_load_path(i)); i++) maxlen = max( maxlen, lstrlenW(path) );
|
|
|
|
maxlen += 2 * max_dll_name_len + 2 + 10; /* ".dll.fake" */
|
|
|
|
if (!(file = HeapAlloc( GetProcessHeap(), 0, maxlen * sizeof(WCHAR) ))) return FALSE;
|
2009-10-15 13:53:14 +02:00
|
|
|
|
2019-07-04 15:41:03 +02:00
|
|
|
if (!(dest = HeapAlloc( GetProcessHeap(), 0, (lstrlenW(dirname) + max_dll_name_len) * sizeof(WCHAR) )))
|
2009-10-15 13:53:14 +02:00
|
|
|
{
|
|
|
|
HeapFree( GetProcessHeap(), 0, file );
|
|
|
|
return FALSE;
|
|
|
|
}
|
2019-07-04 15:41:03 +02:00
|
|
|
lstrcpyW( dest, dirname );
|
|
|
|
dest[lstrlenW(dest) - 1] = 0; /* remove wildcard */
|
2009-10-15 13:53:14 +02:00
|
|
|
|
|
|
|
if (build_dir)
|
|
|
|
{
|
2019-07-04 15:41:03 +02:00
|
|
|
lstrcpyW( file, build_dir );
|
2020-02-11 09:00:21 +01:00
|
|
|
lstrcatW( file, L"\\dlls" );
|
|
|
|
install_lib_dir( dest, file, L".dll", TRUE );
|
2019-07-04 15:41:03 +02:00
|
|
|
lstrcpyW( file, build_dir );
|
2020-02-11 09:00:21 +01:00
|
|
|
lstrcatW( file, L"\\programs" );
|
|
|
|
install_lib_dir( dest, file, L".exe", TRUE );
|
2009-10-15 13:53:14 +02:00
|
|
|
}
|
2019-07-04 15:41:03 +02:00
|
|
|
for (i = 0; (path = enum_load_path( i )); i++)
|
2009-10-15 13:53:14 +02:00
|
|
|
{
|
2019-07-04 15:41:03 +02:00
|
|
|
lstrcpyW( file, path );
|
2019-04-22 11:40:41 +02:00
|
|
|
install_lib_dir( dest, file, NULL, TRUE );
|
2019-07-04 15:41:03 +02:00
|
|
|
lstrcpyW( file, path );
|
2020-02-11 09:00:21 +01:00
|
|
|
lstrcatW( file, L"\\fakedlls" );
|
2019-04-22 11:40:41 +02:00
|
|
|
install_lib_dir( dest, file, NULL, FALSE );
|
2009-10-15 13:53:14 +02:00
|
|
|
}
|
|
|
|
HeapFree( GetProcessHeap(), 0, file );
|
|
|
|
HeapFree( GetProcessHeap(), 0, dest );
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2009-10-15 12:25:11 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* create_fake_dll
|
|
|
|
*/
|
|
|
|
BOOL create_fake_dll( const WCHAR *name, const WCHAR *source )
|
|
|
|
{
|
|
|
|
HANDLE h;
|
|
|
|
BOOL ret;
|
2011-08-02 12:58:26 +02:00
|
|
|
SIZE_T size;
|
2009-10-15 12:25:11 +02:00
|
|
|
const WCHAR *filename;
|
|
|
|
void *buffer;
|
|
|
|
|
2019-07-04 15:41:03 +02:00
|
|
|
if (!(filename = wcsrchr( name, '\\' ))) filename = name;
|
2009-10-15 12:25:11 +02:00
|
|
|
else filename++;
|
|
|
|
|
|
|
|
/* check for empty name which means to only create the directory */
|
|
|
|
if (!filename[0])
|
|
|
|
{
|
|
|
|
create_directories( name );
|
|
|
|
return TRUE;
|
|
|
|
}
|
2009-10-15 13:53:14 +02:00
|
|
|
if (filename[0] == '*' && !filename[1]) return create_wildcard_dlls( name );
|
|
|
|
|
2009-10-15 13:31:24 +02:00
|
|
|
add_handled_dll( filename );
|
2009-10-15 12:25:11 +02:00
|
|
|
|
|
|
|
if (!(h = create_dest_file( name ))) return TRUE; /* not a fake dll */
|
|
|
|
if (h == INVALID_HANDLE_VALUE) return FALSE;
|
2006-03-21 11:30:48 +01:00
|
|
|
|
2009-10-15 14:05:46 +02:00
|
|
|
if (source[0] == '-' && !source[1])
|
|
|
|
{
|
|
|
|
/* '-' source means delete the file */
|
|
|
|
TRACE( "deleting %s\n", debugstr_w(name) );
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else if ((buffer = load_fake_dll( source, &size )))
|
2009-08-18 11:38:22 +02:00
|
|
|
{
|
|
|
|
DWORD written;
|
2006-03-21 11:30:48 +01:00
|
|
|
|
2009-08-18 11:38:22 +02:00
|
|
|
ret = (WriteFile( h, buffer, size, &written, NULL ) && written == size);
|
2011-08-02 13:47:43 +02:00
|
|
|
if (ret) register_fake_dll( name, buffer, size );
|
|
|
|
else ERR( "failed to write to %s (error=%u)\n", debugstr_w(name), GetLastError() );
|
2009-08-18 11:38:22 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
WARN( "fake dll %s not found for %s\n", debugstr_w(source), debugstr_w(name) );
|
2013-05-20 12:48:09 +02:00
|
|
|
ret = build_fake_dll( h, name );
|
2009-08-18 11:38:22 +02:00
|
|
|
}
|
2006-03-21 11:30:48 +01:00
|
|
|
|
|
|
|
CloseHandle( h );
|
|
|
|
if (!ret) DeleteFileW( name );
|
|
|
|
return ret;
|
|
|
|
}
|
2009-10-15 12:20:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* cleanup_fake_dlls
|
|
|
|
*/
|
|
|
|
void cleanup_fake_dlls(void)
|
|
|
|
{
|
2011-08-02 12:58:26 +02:00
|
|
|
if (file_buffer) VirtualFree( file_buffer, 0, MEM_RELEASE );
|
2009-10-15 12:20:14 +02:00
|
|
|
file_buffer = NULL;
|
2009-10-15 13:31:24 +02:00
|
|
|
HeapFree( GetProcessHeap(), 0, handled_dlls );
|
2010-03-27 11:50:01 +01:00
|
|
|
handled_dlls = NULL;
|
2009-10-15 13:31:24 +02:00
|
|
|
handled_count = handled_total = 0;
|
2011-08-02 13:47:43 +02:00
|
|
|
if (registrar) IRegistrar_Release( registrar );
|
|
|
|
registrar = NULL;
|
2009-10-15 12:20:14 +02:00
|
|
|
}
|