2003-04-30 00:53:23 +00:00
|
|
|
/*
|
|
|
|
* Resources
|
|
|
|
*
|
|
|
|
* Copyright 1993 Robert J. Amstadt
|
|
|
|
* Copyright 1995, 2003 Alexandre Julliard
|
|
|
|
*
|
|
|
|
* 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 12:49:52 +00:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2003-04-30 00:53:23 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "wine/port.h"
|
|
|
|
|
2003-09-05 23:08:26 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
2003-05-07 04:01:52 +00:00
|
|
|
#define NONAMELESSUNION
|
|
|
|
#define NONAMELESSSTRUCT
|
2003-09-05 23:08:26 +00:00
|
|
|
#include "ntstatus.h"
|
2005-11-28 16:32:54 +00:00
|
|
|
#define WIN32_NO_STATUS
|
2003-04-30 00:53:23 +00:00
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
2003-09-05 23:08:26 +00:00
|
|
|
#include "winternl.h"
|
2003-04-30 00:53:23 +00:00
|
|
|
#include "wownt32.h"
|
|
|
|
#include "wine/winbase16.h"
|
|
|
|
#include "wine/debug.h"
|
2003-12-13 01:36:14 +00:00
|
|
|
#include "excpt.h"
|
|
|
|
#include "wine/exception.h"
|
2004-09-14 01:06:54 +00:00
|
|
|
#include "wine/unicode.h"
|
|
|
|
#include "wine/list.h"
|
2003-04-30 00:53:23 +00:00
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(resource);
|
|
|
|
|
|
|
|
/* handle conversions */
|
|
|
|
#define HRSRC_32(h16) ((HRSRC)(ULONG_PTR)(h16))
|
|
|
|
#define HRSRC_16(h32) (LOWORD(h32))
|
|
|
|
#define HGLOBAL_32(h16) ((HGLOBAL)(ULONG_PTR)(h16))
|
|
|
|
#define HGLOBAL_16(h32) (LOWORD(h32))
|
|
|
|
#define HMODULE_16(h32) (LOWORD(h32))
|
|
|
|
|
|
|
|
/* retrieve the resource name to pass to the ntdll functions */
|
|
|
|
static NTSTATUS get_res_nameA( LPCSTR name, UNICODE_STRING *str )
|
|
|
|
{
|
|
|
|
if (!HIWORD(name))
|
|
|
|
{
|
|
|
|
str->Buffer = (LPWSTR)name;
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
if (name[0] == '#')
|
|
|
|
{
|
|
|
|
ULONG value;
|
2003-05-13 23:36:49 +00:00
|
|
|
if (RtlCharToInteger( name + 1, 10, &value ) != STATUS_SUCCESS || HIWORD(value))
|
2003-04-30 00:53:23 +00:00
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
str->Buffer = (LPWSTR)value;
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
RtlCreateUnicodeStringFromAsciiz( str, name );
|
|
|
|
RtlUpcaseUnicodeString( str, str, FALSE );
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* retrieve the resource name to pass to the ntdll functions */
|
|
|
|
static NTSTATUS get_res_nameW( LPCWSTR name, UNICODE_STRING *str )
|
|
|
|
{
|
|
|
|
if (!HIWORD(name))
|
|
|
|
{
|
|
|
|
str->Buffer = (LPWSTR)name;
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
if (name[0] == '#')
|
|
|
|
{
|
|
|
|
ULONG value;
|
2003-05-13 23:36:49 +00:00
|
|
|
RtlInitUnicodeString( str, name + 1 );
|
2003-04-30 00:53:23 +00:00
|
|
|
if (RtlUnicodeStringToInteger( str, 10, &value ) != STATUS_SUCCESS || HIWORD(value))
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
str->Buffer = (LPWSTR)value;
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
RtlCreateUnicodeString( str, name );
|
|
|
|
RtlUpcaseUnicodeString( str, str, FALSE );
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2003-12-13 01:36:14 +00:00
|
|
|
/* retrieve the resource names for the 16-bit FindResource function */
|
|
|
|
static BOOL get_res_name_type_WtoA( LPCWSTR name, LPCWSTR type, LPSTR *nameA, LPSTR *typeA )
|
|
|
|
{
|
|
|
|
*nameA = *typeA = NULL;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
if (HIWORD(name))
|
|
|
|
{
|
|
|
|
DWORD len = WideCharToMultiByte( CP_ACP, 0, name, -1, NULL, 0, NULL, NULL );
|
|
|
|
*nameA = HeapAlloc( GetProcessHeap(), 0, len );
|
|
|
|
if (*nameA) WideCharToMultiByte( CP_ACP, 0, name, -1, *nameA, len, NULL, NULL );
|
|
|
|
}
|
|
|
|
else *nameA = (LPSTR)name;
|
|
|
|
|
|
|
|
if (HIWORD(type))
|
|
|
|
{
|
|
|
|
DWORD len = WideCharToMultiByte( CP_ACP, 0, type, -1, NULL, 0, NULL, NULL );
|
|
|
|
*typeA = HeapAlloc( GetProcessHeap(), 0, len );
|
|
|
|
if (*typeA) WideCharToMultiByte( CP_ACP, 0, type, -1, *typeA, len, NULL, NULL );
|
|
|
|
}
|
|
|
|
else *typeA = (LPSTR)type;
|
|
|
|
}
|
2005-12-16 16:17:57 +00:00
|
|
|
__EXCEPT_PAGE_FAULT
|
2003-12-13 01:36:14 +00:00
|
|
|
{
|
|
|
|
if (HIWORD(*nameA)) HeapFree( GetProcessHeap(), 0, *nameA );
|
|
|
|
if (HIWORD(*typeA)) HeapFree( GetProcessHeap(), 0, *typeA );
|
|
|
|
SetLastError( ERROR_INVALID_PARAMETER );
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* implementation of FindResourceExA */
|
|
|
|
static HRSRC find_resourceA( HMODULE hModule, LPCSTR type, LPCSTR name, WORD lang )
|
2003-04-30 00:53:23 +00:00
|
|
|
{
|
|
|
|
NTSTATUS status;
|
|
|
|
UNICODE_STRING nameW, typeW;
|
|
|
|
LDR_RESOURCE_INFO info;
|
|
|
|
const IMAGE_RESOURCE_DATA_ENTRY *entry = NULL;
|
|
|
|
|
2003-12-13 01:36:14 +00:00
|
|
|
__TRY
|
2003-04-30 00:53:23 +00:00
|
|
|
{
|
2003-12-13 01:36:14 +00:00
|
|
|
if ((status = get_res_nameA( name, &nameW )) != STATUS_SUCCESS) goto done;
|
|
|
|
if ((status = get_res_nameA( type, &typeW )) != STATUS_SUCCESS) goto done;
|
2006-07-10 06:58:12 +00:00
|
|
|
info.Type = (ULONG_PTR)typeW.Buffer;
|
|
|
|
info.Name = (ULONG_PTR)nameW.Buffer;
|
2003-12-13 01:36:14 +00:00
|
|
|
info.Language = lang;
|
|
|
|
status = LdrFindResource_U( hModule, &info, 3, &entry );
|
|
|
|
done:
|
|
|
|
if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
|
|
|
|
}
|
2005-12-16 16:17:57 +00:00
|
|
|
__EXCEPT_PAGE_FAULT
|
2003-12-13 01:36:14 +00:00
|
|
|
{
|
|
|
|
SetLastError( ERROR_INVALID_PARAMETER );
|
2003-04-30 00:53:23 +00:00
|
|
|
}
|
2003-12-13 01:36:14 +00:00
|
|
|
__ENDTRY
|
|
|
|
|
|
|
|
if (HIWORD(nameW.Buffer)) HeapFree( GetProcessHeap(), 0, nameW.Buffer );
|
|
|
|
if (HIWORD(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
|
|
|
|
return (HRSRC)entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* implementation of FindResourceExW */
|
|
|
|
static HRSRC find_resourceW( HMODULE hModule, LPCWSTR type, LPCWSTR name, WORD lang )
|
|
|
|
{
|
|
|
|
NTSTATUS status;
|
|
|
|
UNICODE_STRING nameW, typeW;
|
|
|
|
LDR_RESOURCE_INFO info;
|
|
|
|
const IMAGE_RESOURCE_DATA_ENTRY *entry = NULL;
|
2003-04-30 00:53:23 +00:00
|
|
|
|
|
|
|
nameW.Buffer = typeW.Buffer = NULL;
|
2003-12-13 01:36:14 +00:00
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
if ((status = get_res_nameW( name, &nameW )) != STATUS_SUCCESS) goto done;
|
|
|
|
if ((status = get_res_nameW( type, &typeW )) != STATUS_SUCCESS) goto done;
|
2006-07-10 06:58:12 +00:00
|
|
|
info.Type = (ULONG_PTR)typeW.Buffer;
|
|
|
|
info.Name = (ULONG_PTR)nameW.Buffer;
|
2003-12-13 01:36:14 +00:00
|
|
|
info.Language = lang;
|
|
|
|
status = LdrFindResource_U( hModule, &info, 3, &entry );
|
|
|
|
done:
|
|
|
|
if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
|
|
|
|
}
|
2005-12-16 16:17:57 +00:00
|
|
|
__EXCEPT_PAGE_FAULT
|
2003-12-13 01:36:14 +00:00
|
|
|
{
|
|
|
|
SetLastError( ERROR_INVALID_PARAMETER );
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
|
2003-04-30 00:53:23 +00:00
|
|
|
if (HIWORD(nameW.Buffer)) HeapFree( GetProcessHeap(), 0, nameW.Buffer );
|
|
|
|
if (HIWORD(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
|
|
|
|
return (HRSRC)entry;
|
|
|
|
}
|
|
|
|
|
2003-12-13 01:36:14 +00:00
|
|
|
/**********************************************************************
|
|
|
|
* FindResourceExA (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
HRSRC WINAPI FindResourceExA( HMODULE hModule, LPCSTR type, LPCSTR name, WORD lang )
|
|
|
|
{
|
|
|
|
TRACE( "%p %s %s %04x\n", hModule, debugstr_a(type), debugstr_a(name), lang );
|
|
|
|
|
|
|
|
if (!hModule) hModule = GetModuleHandleW(0);
|
|
|
|
else if (!HIWORD(hModule))
|
|
|
|
{
|
|
|
|
return HRSRC_32( FindResource16( HMODULE_16(hModule), name, type ) );
|
|
|
|
}
|
|
|
|
return find_resourceA( hModule, type, name, lang );
|
|
|
|
}
|
|
|
|
|
2003-04-30 00:53:23 +00:00
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* FindResourceA (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
HRSRC WINAPI FindResourceA( HMODULE hModule, LPCSTR name, LPCSTR type )
|
|
|
|
{
|
|
|
|
return FindResourceExA( hModule, type, name, MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* FindResourceExW (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
HRSRC WINAPI FindResourceExW( HMODULE hModule, LPCWSTR type, LPCWSTR name, WORD lang )
|
|
|
|
{
|
|
|
|
TRACE( "%p %s %s %04x\n", hModule, debugstr_w(type), debugstr_w(name), lang );
|
|
|
|
|
|
|
|
if (!hModule) hModule = GetModuleHandleW(0);
|
|
|
|
else if (!HIWORD(hModule))
|
|
|
|
{
|
|
|
|
LPSTR nameA, typeA;
|
|
|
|
HRSRC16 ret;
|
|
|
|
|
2003-12-13 01:36:14 +00:00
|
|
|
if (!get_res_name_type_WtoA( name, type, &nameA, &typeA )) return NULL;
|
2003-04-30 00:53:23 +00:00
|
|
|
|
|
|
|
ret = FindResource16( HMODULE_16(hModule), nameA, typeA );
|
|
|
|
if (HIWORD(nameA)) HeapFree( GetProcessHeap(), 0, nameA );
|
|
|
|
if (HIWORD(typeA)) HeapFree( GetProcessHeap(), 0, typeA );
|
|
|
|
return HRSRC_32(ret);
|
|
|
|
}
|
|
|
|
|
2003-12-13 01:36:14 +00:00
|
|
|
return find_resourceW( hModule, type, name, lang );
|
2003-04-30 00:53:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* FindResourceW (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
HRSRC WINAPI FindResourceW( HINSTANCE hModule, LPCWSTR name, LPCWSTR type )
|
|
|
|
{
|
|
|
|
return FindResourceExW( hModule, type, name, MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-05-07 03:21:26 +00:00
|
|
|
/**********************************************************************
|
|
|
|
* EnumResourceTypesA (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
BOOL WINAPI EnumResourceTypesA( HMODULE hmod, ENUMRESTYPEPROCA lpfun, LONG_PTR lparam )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
LPSTR type = NULL;
|
|
|
|
DWORD len = 0, newlen;
|
|
|
|
NTSTATUS status;
|
|
|
|
const IMAGE_RESOURCE_DIRECTORY *resdir;
|
|
|
|
const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
|
|
|
|
const IMAGE_RESOURCE_DIR_STRING_U *str;
|
|
|
|
|
|
|
|
TRACE( "%p %p %lx\n", hmod, lpfun, lparam );
|
|
|
|
|
|
|
|
if (!hmod) hmod = GetModuleHandleA( NULL );
|
|
|
|
|
|
|
|
if ((status = LdrFindResourceDirectory_U( hmod, NULL, 0, &resdir )) != STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
SetLastError( RtlNtStatusToDosError(status) );
|
|
|
|
return FALSE;
|
|
|
|
}
|
2006-09-14 16:41:56 +00:00
|
|
|
et = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resdir + 1);
|
2003-05-07 03:21:26 +00:00
|
|
|
for (i = 0; i < resdir->NumberOfNamedEntries+resdir->NumberOfIdEntries; i++)
|
|
|
|
{
|
|
|
|
if (et[i].u1.s1.NameIsString)
|
|
|
|
{
|
2006-09-14 16:41:56 +00:00
|
|
|
str = (const IMAGE_RESOURCE_DIR_STRING_U *)((const BYTE *)resdir + et[i].u1.s1.NameOffset);
|
2003-05-07 03:21:26 +00:00
|
|
|
newlen = WideCharToMultiByte( CP_ACP, 0, str->NameString, str->Length, NULL, 0, NULL, NULL);
|
|
|
|
if (newlen + 1 > len)
|
|
|
|
{
|
|
|
|
len = newlen + 1;
|
2004-12-23 17:06:43 +00:00
|
|
|
HeapFree( GetProcessHeap(), 0, type );
|
2003-05-07 03:21:26 +00:00
|
|
|
if (!(type = HeapAlloc( GetProcessHeap(), 0, len ))) return FALSE;
|
|
|
|
}
|
|
|
|
WideCharToMultiByte( CP_ACP, 0, str->NameString, str->Length, type, len, NULL, NULL);
|
|
|
|
type[newlen] = 0;
|
|
|
|
ret = lpfun(hmod,type,lparam);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = lpfun( hmod, (LPSTR)(int)et[i].u1.s2.Id, lparam );
|
|
|
|
}
|
|
|
|
if (!ret) break;
|
|
|
|
}
|
2004-12-23 17:06:43 +00:00
|
|
|
HeapFree( GetProcessHeap(), 0, type );
|
2003-05-07 03:21:26 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* EnumResourceTypesW (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
BOOL WINAPI EnumResourceTypesW( HMODULE hmod, ENUMRESTYPEPROCW lpfun, LONG_PTR lparam )
|
|
|
|
{
|
2004-08-10 23:43:21 +00:00
|
|
|
int i, len = 0;
|
2003-05-07 03:21:26 +00:00
|
|
|
BOOL ret = FALSE;
|
|
|
|
LPWSTR type = NULL;
|
|
|
|
NTSTATUS status;
|
|
|
|
const IMAGE_RESOURCE_DIRECTORY *resdir;
|
|
|
|
const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
|
|
|
|
const IMAGE_RESOURCE_DIR_STRING_U *str;
|
|
|
|
|
|
|
|
TRACE( "%p %p %lx\n", hmod, lpfun, lparam );
|
|
|
|
|
|
|
|
if (!hmod) hmod = GetModuleHandleW( NULL );
|
|
|
|
|
|
|
|
if ((status = LdrFindResourceDirectory_U( hmod, NULL, 0, &resdir )) != STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
SetLastError( RtlNtStatusToDosError(status) );
|
|
|
|
return FALSE;
|
|
|
|
}
|
2006-09-14 16:41:56 +00:00
|
|
|
et = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resdir + 1);
|
2003-05-07 03:21:26 +00:00
|
|
|
for (i = 0; i < resdir->NumberOfNamedEntries + resdir->NumberOfIdEntries; i++)
|
|
|
|
{
|
|
|
|
if (et[i].u1.s1.NameIsString)
|
|
|
|
{
|
2006-09-14 16:41:56 +00:00
|
|
|
str = (const IMAGE_RESOURCE_DIR_STRING_U *)((const BYTE *)resdir + et[i].u1.s1.NameOffset);
|
2003-05-07 03:21:26 +00:00
|
|
|
if (str->Length + 1 > len)
|
|
|
|
{
|
|
|
|
len = str->Length + 1;
|
2004-12-23 17:06:43 +00:00
|
|
|
HeapFree( GetProcessHeap(), 0, type );
|
2003-05-07 03:21:26 +00:00
|
|
|
if (!(type = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return FALSE;
|
|
|
|
}
|
|
|
|
memcpy(type, str->NameString, str->Length * sizeof (WCHAR));
|
|
|
|
type[str->Length] = 0;
|
|
|
|
ret = lpfun(hmod,type,lparam);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = lpfun( hmod, (LPWSTR)(int)et[i].u1.s2.Id, lparam );
|
|
|
|
}
|
|
|
|
if (!ret) break;
|
|
|
|
}
|
2004-12-23 17:06:43 +00:00
|
|
|
HeapFree( GetProcessHeap(), 0, type );
|
2003-05-07 03:21:26 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* EnumResourceNamesA (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
BOOL WINAPI EnumResourceNamesA( HMODULE hmod, LPCSTR type, ENUMRESNAMEPROCA lpfun, LONG_PTR lparam )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
DWORD len = 0, newlen;
|
|
|
|
LPSTR name = NULL;
|
|
|
|
NTSTATUS status;
|
|
|
|
UNICODE_STRING typeW;
|
|
|
|
LDR_RESOURCE_INFO info;
|
|
|
|
const IMAGE_RESOURCE_DIRECTORY *basedir, *resdir;
|
|
|
|
const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
|
|
|
|
const IMAGE_RESOURCE_DIR_STRING_U *str;
|
|
|
|
|
|
|
|
TRACE( "%p %s %p %lx\n", hmod, debugstr_a(type), lpfun, lparam );
|
|
|
|
|
|
|
|
if (!hmod) hmod = GetModuleHandleA( NULL );
|
|
|
|
typeW.Buffer = NULL;
|
|
|
|
if ((status = LdrFindResourceDirectory_U( hmod, NULL, 0, &basedir )) != STATUS_SUCCESS)
|
|
|
|
goto done;
|
|
|
|
if ((status = get_res_nameA( type, &typeW )) != STATUS_SUCCESS)
|
|
|
|
goto done;
|
|
|
|
info.Type = (ULONG)typeW.Buffer;
|
|
|
|
if ((status = LdrFindResourceDirectory_U( hmod, &info, 1, &resdir )) != STATUS_SUCCESS)
|
|
|
|
goto done;
|
|
|
|
|
2006-09-14 16:41:56 +00:00
|
|
|
et = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resdir + 1);
|
2003-05-07 03:21:26 +00:00
|
|
|
for (i = 0; i < resdir->NumberOfNamedEntries+resdir->NumberOfIdEntries; i++)
|
|
|
|
{
|
|
|
|
if (et[i].u1.s1.NameIsString)
|
|
|
|
{
|
2006-09-14 16:41:56 +00:00
|
|
|
str = (const IMAGE_RESOURCE_DIR_STRING_U *)((const BYTE *)basedir + et[i].u1.s1.NameOffset);
|
2003-05-07 03:21:26 +00:00
|
|
|
newlen = WideCharToMultiByte(CP_ACP, 0, str->NameString, str->Length, NULL, 0, NULL, NULL);
|
|
|
|
if (newlen + 1 > len)
|
|
|
|
{
|
|
|
|
len = newlen + 1;
|
2004-12-23 17:06:43 +00:00
|
|
|
HeapFree( GetProcessHeap(), 0, name );
|
2003-05-07 03:21:26 +00:00
|
|
|
if (!(name = HeapAlloc(GetProcessHeap(), 0, len + 1 )))
|
|
|
|
{
|
|
|
|
ret = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
WideCharToMultiByte( CP_ACP, 0, str->NameString, str->Length, name, len, NULL, NULL );
|
|
|
|
name[newlen] = 0;
|
|
|
|
ret = lpfun(hmod,type,name,lparam);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = lpfun( hmod, type, (LPSTR)(int)et[i].u1.s2.Id, lparam );
|
|
|
|
}
|
|
|
|
if (!ret) break;
|
|
|
|
}
|
|
|
|
done:
|
2004-12-23 17:06:43 +00:00
|
|
|
HeapFree( GetProcessHeap(), 0, name );
|
2003-05-07 03:21:26 +00:00
|
|
|
if (HIWORD(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
|
|
|
|
if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* EnumResourceNamesW (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
BOOL WINAPI EnumResourceNamesW( HMODULE hmod, LPCWSTR type, ENUMRESNAMEPROCW lpfun, LONG_PTR lparam )
|
|
|
|
{
|
2004-08-10 23:43:21 +00:00
|
|
|
int i, len = 0;
|
2003-05-07 03:21:26 +00:00
|
|
|
BOOL ret = FALSE;
|
|
|
|
LPWSTR name = NULL;
|
|
|
|
NTSTATUS status;
|
|
|
|
UNICODE_STRING typeW;
|
|
|
|
LDR_RESOURCE_INFO info;
|
|
|
|
const IMAGE_RESOURCE_DIRECTORY *basedir, *resdir;
|
|
|
|
const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
|
|
|
|
const IMAGE_RESOURCE_DIR_STRING_U *str;
|
|
|
|
|
|
|
|
TRACE( "%p %s %p %lx\n", hmod, debugstr_w(type), lpfun, lparam );
|
|
|
|
|
|
|
|
if (!hmod) hmod = GetModuleHandleW( NULL );
|
|
|
|
typeW.Buffer = NULL;
|
|
|
|
if ((status = LdrFindResourceDirectory_U( hmod, NULL, 0, &basedir )) != STATUS_SUCCESS)
|
|
|
|
goto done;
|
|
|
|
if ((status = get_res_nameW( type, &typeW )) != STATUS_SUCCESS)
|
|
|
|
goto done;
|
|
|
|
info.Type = (ULONG)typeW.Buffer;
|
|
|
|
if ((status = LdrFindResourceDirectory_U( hmod, &info, 1, &resdir )) != STATUS_SUCCESS)
|
|
|
|
goto done;
|
|
|
|
|
2006-09-14 16:41:56 +00:00
|
|
|
et = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resdir + 1);
|
2003-05-07 03:21:26 +00:00
|
|
|
for (i = 0; i < resdir->NumberOfNamedEntries+resdir->NumberOfIdEntries; i++)
|
|
|
|
{
|
|
|
|
if (et[i].u1.s1.NameIsString)
|
|
|
|
{
|
2006-09-14 16:41:56 +00:00
|
|
|
str = (const IMAGE_RESOURCE_DIR_STRING_U *)((const BYTE *)basedir + et[i].u1.s1.NameOffset);
|
2003-05-07 03:21:26 +00:00
|
|
|
if (str->Length + 1 > len)
|
|
|
|
{
|
|
|
|
len = str->Length + 1;
|
2004-12-23 17:06:43 +00:00
|
|
|
HeapFree( GetProcessHeap(), 0, name );
|
2003-05-07 03:21:26 +00:00
|
|
|
if (!(name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
|
|
|
|
{
|
|
|
|
ret = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
memcpy(name, str->NameString, str->Length * sizeof (WCHAR));
|
|
|
|
name[str->Length] = 0;
|
|
|
|
ret = lpfun(hmod,type,name,lparam);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = lpfun( hmod, type, (LPWSTR)(int)et[i].u1.s2.Id, lparam );
|
|
|
|
}
|
|
|
|
if (!ret) break;
|
|
|
|
}
|
|
|
|
done:
|
2004-12-23 17:06:43 +00:00
|
|
|
HeapFree( GetProcessHeap(), 0, name );
|
2003-05-07 03:21:26 +00:00
|
|
|
if (HIWORD(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
|
|
|
|
if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* EnumResourceLanguagesA (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
BOOL WINAPI EnumResourceLanguagesA( HMODULE hmod, LPCSTR type, LPCSTR name,
|
|
|
|
ENUMRESLANGPROCA lpfun, LONG_PTR lparam )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
NTSTATUS status;
|
|
|
|
UNICODE_STRING typeW, nameW;
|
|
|
|
LDR_RESOURCE_INFO info;
|
|
|
|
const IMAGE_RESOURCE_DIRECTORY *basedir, *resdir;
|
|
|
|
const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
|
|
|
|
|
|
|
|
TRACE( "%p %s %s %p %lx\n", hmod, debugstr_a(type), debugstr_a(name), lpfun, lparam );
|
|
|
|
|
|
|
|
if (!hmod) hmod = GetModuleHandleA( NULL );
|
|
|
|
typeW.Buffer = nameW.Buffer = NULL;
|
|
|
|
if ((status = LdrFindResourceDirectory_U( hmod, NULL, 0, &basedir )) != STATUS_SUCCESS)
|
|
|
|
goto done;
|
|
|
|
if ((status = get_res_nameA( type, &typeW )) != STATUS_SUCCESS)
|
|
|
|
goto done;
|
|
|
|
if ((status = get_res_nameA( name, &nameW )) != STATUS_SUCCESS)
|
|
|
|
goto done;
|
2006-07-10 06:58:12 +00:00
|
|
|
info.Type = (ULONG_PTR)typeW.Buffer;
|
|
|
|
info.Name = (ULONG_PTR)nameW.Buffer;
|
2003-05-07 03:21:26 +00:00
|
|
|
if ((status = LdrFindResourceDirectory_U( hmod, &info, 2, &resdir )) != STATUS_SUCCESS)
|
|
|
|
goto done;
|
|
|
|
|
2006-09-14 16:41:56 +00:00
|
|
|
et = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resdir + 1);
|
2003-05-07 03:21:26 +00:00
|
|
|
for (i = 0; i < resdir->NumberOfNamedEntries + resdir->NumberOfIdEntries; i++)
|
|
|
|
{
|
|
|
|
ret = lpfun( hmod, type, name, et[i].u1.s2.Id, lparam );
|
|
|
|
if (!ret) break;
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
if (HIWORD(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
|
|
|
|
if (HIWORD(nameW.Buffer)) HeapFree( GetProcessHeap(), 0, nameW.Buffer );
|
|
|
|
if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* EnumResourceLanguagesW (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
BOOL WINAPI EnumResourceLanguagesW( HMODULE hmod, LPCWSTR type, LPCWSTR name,
|
|
|
|
ENUMRESLANGPROCW lpfun, LONG_PTR lparam )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
NTSTATUS status;
|
|
|
|
UNICODE_STRING typeW, nameW;
|
|
|
|
LDR_RESOURCE_INFO info;
|
|
|
|
const IMAGE_RESOURCE_DIRECTORY *basedir, *resdir;
|
|
|
|
const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
|
|
|
|
|
|
|
|
TRACE( "%p %s %s %p %lx\n", hmod, debugstr_w(type), debugstr_w(name), lpfun, lparam );
|
|
|
|
|
|
|
|
if (!hmod) hmod = GetModuleHandleW( NULL );
|
|
|
|
typeW.Buffer = nameW.Buffer = NULL;
|
|
|
|
if ((status = LdrFindResourceDirectory_U( hmod, NULL, 0, &basedir )) != STATUS_SUCCESS)
|
|
|
|
goto done;
|
|
|
|
if ((status = get_res_nameW( type, &typeW )) != STATUS_SUCCESS)
|
|
|
|
goto done;
|
|
|
|
if ((status = get_res_nameW( name, &nameW )) != STATUS_SUCCESS)
|
|
|
|
goto done;
|
2006-07-10 06:58:12 +00:00
|
|
|
info.Type = (ULONG_PTR)typeW.Buffer;
|
|
|
|
info.Name = (ULONG_PTR)nameW.Buffer;
|
2003-05-07 03:21:26 +00:00
|
|
|
if ((status = LdrFindResourceDirectory_U( hmod, &info, 2, &resdir )) != STATUS_SUCCESS)
|
|
|
|
goto done;
|
|
|
|
|
2006-09-14 16:41:56 +00:00
|
|
|
et = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resdir + 1);
|
2003-05-07 03:21:26 +00:00
|
|
|
for (i = 0; i < resdir->NumberOfNamedEntries + resdir->NumberOfIdEntries; i++)
|
|
|
|
{
|
|
|
|
ret = lpfun( hmod, type, name, et[i].u1.s2.Id, lparam );
|
|
|
|
if (!ret) break;
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
if (HIWORD(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
|
|
|
|
if (HIWORD(nameW.Buffer)) HeapFree( GetProcessHeap(), 0, nameW.Buffer );
|
|
|
|
if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-04-30 00:53:23 +00:00
|
|
|
/**********************************************************************
|
|
|
|
* LoadResource (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
HGLOBAL WINAPI LoadResource( HINSTANCE hModule, HRSRC hRsrc )
|
|
|
|
{
|
|
|
|
NTSTATUS status;
|
|
|
|
void *ret = NULL;
|
|
|
|
|
|
|
|
TRACE( "%p %p\n", hModule, hRsrc );
|
|
|
|
|
|
|
|
if (hModule && !HIWORD(hModule))
|
|
|
|
/* FIXME: should convert return to 32-bit resource */
|
|
|
|
return HGLOBAL_32( LoadResource16( HMODULE_16(hModule), HRSRC_16(hRsrc) ) );
|
|
|
|
|
|
|
|
if (!hRsrc) return 0;
|
|
|
|
if (!hModule) hModule = GetModuleHandleA( NULL );
|
|
|
|
status = LdrAccessResource( hModule, (IMAGE_RESOURCE_DATA_ENTRY *)hRsrc, &ret, NULL );
|
|
|
|
if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* LockResource (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
LPVOID WINAPI LockResource( HGLOBAL handle )
|
|
|
|
{
|
|
|
|
TRACE("(%p)\n", handle );
|
|
|
|
|
|
|
|
if (HIWORD( handle )) /* 32-bit memory handle */
|
|
|
|
return (LPVOID)handle;
|
|
|
|
|
|
|
|
/* 16-bit memory handle */
|
|
|
|
return LockResource16( HGLOBAL_16(handle) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* FreeResource (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
BOOL WINAPI FreeResource( HGLOBAL handle )
|
|
|
|
{
|
|
|
|
if (HIWORD(handle)) return 0; /* 32-bit memory handle: nothing to do */
|
|
|
|
return FreeResource16( HGLOBAL_16(handle) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* SizeofResource (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
DWORD WINAPI SizeofResource( HINSTANCE hModule, HRSRC hRsrc )
|
|
|
|
{
|
|
|
|
if (hModule && !HIWORD(hModule))
|
|
|
|
return SizeofResource16( HMODULE_16(hModule), HRSRC_16(hRsrc) );
|
|
|
|
|
|
|
|
if (!hRsrc) return 0;
|
|
|
|
return ((PIMAGE_RESOURCE_DATA_ENTRY)hRsrc)->Size;
|
|
|
|
}
|
2003-06-24 03:34:15 +00:00
|
|
|
|
2007-01-03 08:32:52 +00:00
|
|
|
/*
|
|
|
|
* Data structure for updating resources.
|
|
|
|
* Type/Name/Language is a keyset for accessing resource data.
|
|
|
|
*
|
|
|
|
* QUEUEDUPDATES (root) ->
|
|
|
|
* list of struct resouce_dir_entry (Type) ->
|
|
|
|
* list of struct resouce_dir_entry (Name) ->
|
|
|
|
* list of struct resouce_data Language + Data
|
|
|
|
*/
|
|
|
|
|
2004-09-14 01:06:54 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
LPWSTR pFileName;
|
2007-01-08 09:08:15 +00:00
|
|
|
BOOL bDeleteExistingResources;
|
2007-01-03 08:32:52 +00:00
|
|
|
struct list root;
|
2004-09-14 01:06:54 +00:00
|
|
|
} QUEUEDUPDATES;
|
|
|
|
|
2007-01-03 08:32:52 +00:00
|
|
|
/* this structure is shared for types and names */
|
|
|
|
struct resource_dir_entry {
|
|
|
|
struct list entry;
|
|
|
|
LPWSTR id;
|
|
|
|
struct list children;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* this structure is the leaf */
|
|
|
|
struct resource_data {
|
|
|
|
struct list entry;
|
|
|
|
LANGID lang;
|
|
|
|
DWORD codepage;
|
|
|
|
DWORD cbData;
|
|
|
|
BYTE data[1];
|
|
|
|
};
|
|
|
|
|
2007-01-10 18:28:25 +00:00
|
|
|
static int resource_strcmp( LPCWSTR a, LPCWSTR b )
|
2007-01-03 08:32:52 +00:00
|
|
|
{
|
|
|
|
if ( a == b )
|
|
|
|
return 0;
|
|
|
|
if (HIWORD( a ) && HIWORD( b ) )
|
|
|
|
return lstrcmpW( a, b );
|
|
|
|
/* strings come before ids */
|
|
|
|
if (HIWORD( a ) && !HIWORD( b ))
|
|
|
|
return -1;
|
|
|
|
if (HIWORD( b ) && !HIWORD( a ))
|
|
|
|
return 1;
|
|
|
|
return ( a < b ) ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
2007-01-10 18:28:25 +00:00
|
|
|
static struct resource_dir_entry *find_resource_dir_entry( struct list *dir, LPCWSTR id )
|
2007-01-03 08:32:52 +00:00
|
|
|
{
|
|
|
|
struct resource_dir_entry *ent;
|
|
|
|
|
|
|
|
/* match either IDs or strings */
|
|
|
|
LIST_FOR_EACH_ENTRY( ent, dir, struct resource_dir_entry, entry )
|
|
|
|
if (!resource_strcmp( id, ent->id ))
|
|
|
|
return ent;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-01-10 18:28:25 +00:00
|
|
|
static struct resource_data *find_resource_data( struct list *dir, LANGID lang )
|
2007-01-03 08:32:52 +00:00
|
|
|
{
|
|
|
|
struct resource_data *res_data;
|
|
|
|
|
|
|
|
/* match only languages here */
|
|
|
|
LIST_FOR_EACH_ENTRY( res_data, dir, struct resource_data, entry )
|
|
|
|
if ( lang == res_data->lang )
|
|
|
|
return res_data;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-01-10 18:28:25 +00:00
|
|
|
static void add_resource_dir_entry( struct list *dir, struct resource_dir_entry *resdir )
|
2007-01-03 08:32:52 +00:00
|
|
|
{
|
|
|
|
struct resource_dir_entry *ent;
|
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY( ent, dir, struct resource_dir_entry, entry )
|
|
|
|
{
|
|
|
|
if (0>resource_strcmp( ent->id, resdir->id ))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
list_add_before( &ent->entry, &resdir->entry );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
list_add_tail( dir, &resdir->entry );
|
|
|
|
}
|
|
|
|
|
2007-01-10 18:28:25 +00:00
|
|
|
static void add_resource_data_entry( struct list *dir, struct resource_data *resdata )
|
2007-01-03 08:32:52 +00:00
|
|
|
{
|
|
|
|
struct resource_data *ent;
|
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY( ent, dir, struct resource_data, entry )
|
|
|
|
{
|
|
|
|
if (ent->lang < resdata->lang)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
list_add_before( &ent->entry, &resdata->entry );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
list_add_tail( dir, &resdata->entry );
|
|
|
|
}
|
|
|
|
|
2007-01-10 18:28:25 +00:00
|
|
|
static LPWSTR res_strdupW( LPCWSTR str )
|
2007-01-03 08:32:52 +00:00
|
|
|
{
|
|
|
|
LPWSTR ret;
|
|
|
|
UINT len;
|
|
|
|
|
|
|
|
if (HIWORD(str) == 0)
|
|
|
|
return (LPWSTR) (UINT_PTR) LOWORD(str);
|
|
|
|
len = (lstrlenW( str ) + 1) * sizeof (WCHAR);
|
|
|
|
ret = HeapAlloc( GetProcessHeap(), 0, len );
|
|
|
|
memcpy( ret, str, len );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-01-10 18:28:25 +00:00
|
|
|
static void res_free_str( LPWSTR str )
|
2007-01-03 08:32:52 +00:00
|
|
|
{
|
|
|
|
if (HIWORD(str))
|
|
|
|
HeapFree( GetProcessHeap(), 0, str );
|
|
|
|
}
|
|
|
|
|
2007-01-10 18:28:25 +00:00
|
|
|
static BOOL update_add_resource( QUEUEDUPDATES *updates, LPCWSTR Type, LPCWSTR Name,
|
|
|
|
WORD Language, DWORD codepage, LPCVOID lpData, DWORD cbData )
|
2003-06-24 03:34:15 +00:00
|
|
|
{
|
2007-01-03 08:32:52 +00:00
|
|
|
struct resource_dir_entry *restype, *resname;
|
|
|
|
struct resource_data *resdata;
|
|
|
|
|
|
|
|
TRACE("%p %s %s %04x %04x %p %d bytes\n", updates, debugstr_w(Type), debugstr_w(Name), Language, codepage, lpData, cbData);
|
|
|
|
|
|
|
|
if (!lpData || !cbData)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
restype = find_resource_dir_entry( &updates->root, Type );
|
|
|
|
if (!restype)
|
|
|
|
{
|
|
|
|
restype = HeapAlloc( GetProcessHeap(), 0, sizeof *restype );
|
|
|
|
restype->id = res_strdupW( Type );
|
|
|
|
list_init( &restype->children );
|
|
|
|
add_resource_dir_entry( &updates->root, restype );
|
|
|
|
}
|
|
|
|
|
|
|
|
resname = find_resource_dir_entry( &restype->children, Name );
|
|
|
|
if (!resname)
|
|
|
|
{
|
|
|
|
resname = HeapAlloc( GetProcessHeap(), 0, sizeof *resname );
|
|
|
|
resname->id = res_strdupW( Name );
|
|
|
|
list_init( &resname->children );
|
|
|
|
add_resource_dir_entry( &restype->children, resname );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If there's an existing resource entry with matching (Type,Name,Language)
|
|
|
|
* it needs to be removed before adding the new data.
|
|
|
|
*/
|
|
|
|
resdata = find_resource_data( &resname->children, Language );
|
|
|
|
if (resdata)
|
|
|
|
{
|
|
|
|
list_remove( &resdata->entry );
|
|
|
|
HeapFree( GetProcessHeap(), 0, resdata );
|
|
|
|
}
|
|
|
|
|
|
|
|
resdata = HeapAlloc( GetProcessHeap(), 0, sizeof *resdata + cbData );
|
|
|
|
resdata->lang = Language;
|
|
|
|
resdata->codepage = codepage;
|
|
|
|
resdata->cbData = cbData;
|
|
|
|
memcpy( resdata->data, lpData, cbData );
|
|
|
|
|
|
|
|
add_resource_data_entry( &resname->children, resdata );
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2007-01-10 18:28:25 +00:00
|
|
|
static void free_resource_directory( struct list *head, int level )
|
2007-01-03 08:32:52 +00:00
|
|
|
{
|
|
|
|
struct list *ptr = NULL;
|
|
|
|
|
|
|
|
while ((ptr = list_head( head )))
|
|
|
|
{
|
|
|
|
list_remove( ptr );
|
|
|
|
if (level)
|
|
|
|
{
|
|
|
|
struct resource_dir_entry *ent;
|
|
|
|
|
|
|
|
ent = LIST_ENTRY( ptr, struct resource_dir_entry, entry );
|
|
|
|
res_free_str( ent->id );
|
|
|
|
free_resource_directory( &ent->children, level - 1 );
|
|
|
|
HeapFree(GetProcessHeap(), 0, ent);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct resource_data *data;
|
|
|
|
|
|
|
|
data = LIST_ENTRY( ptr, struct resource_data, entry );
|
|
|
|
HeapFree( GetProcessHeap(), 0, data );
|
|
|
|
}
|
|
|
|
}
|
2004-09-14 01:06:54 +00:00
|
|
|
}
|
|
|
|
|
2007-01-10 18:28:25 +00:00
|
|
|
static IMAGE_NT_HEADERS *get_nt_header( void *base, DWORD mapping_size )
|
2004-09-14 01:06:54 +00:00
|
|
|
{
|
2006-12-28 10:58:35 +00:00
|
|
|
IMAGE_NT_HEADERS *nt;
|
|
|
|
IMAGE_DOS_HEADER *dos;
|
2003-06-24 03:34:15 +00:00
|
|
|
|
2006-12-28 10:58:35 +00:00
|
|
|
if (mapping_size<sizeof (*dos))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
dos = base;
|
|
|
|
if (dos->e_magic != IMAGE_DOS_SIGNATURE)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if ((dos->e_lfanew + sizeof (*nt)) > mapping_size)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
nt = (void*) ((BYTE*)base + dos->e_lfanew);
|
|
|
|
|
|
|
|
if (nt->Signature != IMAGE_NT_SIGNATURE)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return nt;
|
2004-09-14 01:06:54 +00:00
|
|
|
}
|
2003-06-24 03:34:15 +00:00
|
|
|
|
2007-01-10 18:28:25 +00:00
|
|
|
static IMAGE_SECTION_HEADER *get_section_header( void *base, DWORD mapping_size, DWORD *num_sections )
|
2004-09-29 21:10:44 +00:00
|
|
|
{
|
2006-12-28 10:58:35 +00:00
|
|
|
IMAGE_NT_HEADERS *nt;
|
|
|
|
IMAGE_SECTION_HEADER *sec;
|
|
|
|
DWORD section_ofs;
|
|
|
|
|
|
|
|
nt = get_nt_header( base, mapping_size );
|
|
|
|
if (!nt)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* check that we don't go over the end of the file accessing the sections */
|
|
|
|
section_ofs = FIELD_OFFSET(IMAGE_NT_HEADERS, OptionalHeader) + nt->FileHeader.SizeOfOptionalHeader;
|
|
|
|
if ((nt->FileHeader.NumberOfSections * sizeof (*sec) + section_ofs) > mapping_size)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (num_sections)
|
|
|
|
*num_sections = nt->FileHeader.NumberOfSections;
|
|
|
|
|
|
|
|
/* from here we have a valid PE exe to update */
|
|
|
|
return (void*) ((BYTE*)nt + section_ofs);
|
2004-09-29 21:10:44 +00:00
|
|
|
}
|
|
|
|
|
2007-01-08 09:08:15 +00:00
|
|
|
static BOOL check_pe_exe( HANDLE file, QUEUEDUPDATES *updates )
|
2004-09-29 21:10:44 +00:00
|
|
|
{
|
2006-12-28 10:58:35 +00:00
|
|
|
const IMAGE_NT_HEADERS *nt;
|
|
|
|
const IMAGE_SECTION_HEADER *sec;
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
HANDLE mapping;
|
|
|
|
DWORD mapping_size, num_sections = 0;
|
|
|
|
void *base = NULL;
|
|
|
|
|
|
|
|
mapping_size = GetFileSize( file, NULL );
|
|
|
|
|
|
|
|
mapping = CreateFileMappingW( file, NULL, PAGE_READONLY, 0, 0, NULL );
|
|
|
|
if (!mapping)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
base = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, mapping_size );
|
|
|
|
if (!base)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
nt = get_nt_header( base, mapping_size );
|
|
|
|
if (!nt)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
TRACE("resources: %08x %08x\n",
|
|
|
|
nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress,
|
|
|
|
nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].Size);
|
|
|
|
|
|
|
|
sec = get_section_header( base, mapping_size, &num_sections );
|
|
|
|
if (!sec)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
ret = TRUE;
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (base)
|
|
|
|
UnmapViewOfFile( base );
|
|
|
|
if (mapping)
|
|
|
|
CloseHandle( mapping );
|
|
|
|
|
|
|
|
return ret;
|
2004-09-29 21:10:44 +00:00
|
|
|
}
|
|
|
|
|
2007-01-08 09:08:15 +00:00
|
|
|
struct resource_size_info {
|
|
|
|
DWORD types_ofs;
|
|
|
|
DWORD names_ofs;
|
|
|
|
DWORD langs_ofs;
|
|
|
|
DWORD data_entry_ofs;
|
|
|
|
DWORD strings_ofs;
|
|
|
|
DWORD data_ofs;
|
|
|
|
DWORD total_size;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void get_resource_sizes( QUEUEDUPDATES *updates, struct resource_size_info *si )
|
2004-09-29 21:10:44 +00:00
|
|
|
{
|
2007-01-08 09:08:15 +00:00
|
|
|
struct resource_dir_entry *types, *names;
|
|
|
|
struct resource_data *data;
|
|
|
|
DWORD num_types = 0, num_names = 0, num_langs = 0, strings_size = 0, data_size = 0;
|
|
|
|
|
|
|
|
memset( si, 0, sizeof *si );
|
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY( types, &updates->root, struct resource_dir_entry, entry )
|
|
|
|
{
|
|
|
|
num_types++;
|
|
|
|
if (HIWORD( types->id ))
|
|
|
|
strings_size += sizeof (WORD) + lstrlenW( types->id )*sizeof (WCHAR);
|
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY( names, &types->children, struct resource_dir_entry, entry )
|
|
|
|
{
|
|
|
|
num_names++;
|
|
|
|
|
|
|
|
if (HIWORD( names->id ))
|
|
|
|
strings_size += sizeof (WORD) + lstrlenW( names->id )*sizeof (WCHAR);
|
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY( data, &names->children, struct resource_data, entry )
|
|
|
|
{
|
|
|
|
num_langs++;
|
|
|
|
data_size += (data->cbData + 3) & ~3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* names are at the end of the types */
|
|
|
|
si->names_ofs = sizeof (IMAGE_RESOURCE_DIRECTORY) +
|
|
|
|
num_types * sizeof (IMAGE_RESOURCE_DIRECTORY_ENTRY);
|
|
|
|
|
|
|
|
/* language directories are at the end of the names */
|
|
|
|
si->langs_ofs = si->names_ofs +
|
|
|
|
num_types * sizeof (IMAGE_RESOURCE_DIRECTORY) +
|
|
|
|
num_names * sizeof (IMAGE_RESOURCE_DIRECTORY_ENTRY);
|
|
|
|
|
|
|
|
si->data_entry_ofs = si->langs_ofs +
|
|
|
|
num_names * sizeof (IMAGE_RESOURCE_DIRECTORY) +
|
|
|
|
num_langs * sizeof (IMAGE_RESOURCE_DIRECTORY_ENTRY);
|
|
|
|
|
|
|
|
si->strings_ofs = si->data_entry_ofs +
|
|
|
|
num_langs * sizeof (IMAGE_RESOURCE_DATA_ENTRY);
|
|
|
|
|
|
|
|
si->data_ofs = si->strings_ofs + ((strings_size + 3) & ~3);
|
|
|
|
|
|
|
|
si->total_size = si->data_ofs + data_size;
|
|
|
|
|
|
|
|
TRACE("names %08x langs %08x data entries %08x strings %08x data %08x total %08x\n",
|
|
|
|
si->names_ofs, si->langs_ofs, si->data_entry_ofs,
|
|
|
|
si->strings_ofs, si->data_ofs, si->total_size);
|
|
|
|
}
|
|
|
|
|
2007-01-10 18:28:25 +00:00
|
|
|
static void res_write_padding( BYTE *res_base, DWORD size )
|
2007-01-08 09:08:15 +00:00
|
|
|
{
|
|
|
|
static const BYTE pad[] = {
|
|
|
|
'P','A','D','D','I','N','G','X','X','P','A','D','D','I','N','G' };
|
|
|
|
DWORD i;
|
|
|
|
|
|
|
|
for ( i = 0; i < size / sizeof pad; i++ )
|
|
|
|
memcpy( &res_base[i*sizeof pad], pad, sizeof pad );
|
|
|
|
memcpy( &res_base[i*sizeof pad], pad, size%sizeof pad );
|
|
|
|
}
|
|
|
|
|
2007-01-10 18:28:25 +00:00
|
|
|
static BOOL write_resources( QUEUEDUPDATES *updates, LPBYTE base, struct resource_size_info *si, DWORD rva )
|
2007-01-08 09:08:15 +00:00
|
|
|
{
|
|
|
|
struct resource_dir_entry *types, *names;
|
|
|
|
struct resource_data *data;
|
|
|
|
IMAGE_RESOURCE_DIRECTORY *root;
|
|
|
|
|
|
|
|
TRACE("%p %p %p %08x\n", updates, base, si, rva );
|
|
|
|
|
|
|
|
memset( base, 0, si->total_size );
|
|
|
|
|
|
|
|
/* the root entry always exists */
|
|
|
|
root = (IMAGE_RESOURCE_DIRECTORY*) base;
|
|
|
|
memset( root, 0, sizeof *root );
|
|
|
|
root->MajorVersion = 4;
|
|
|
|
si->types_ofs = sizeof *root;
|
|
|
|
LIST_FOR_EACH_ENTRY( types, &updates->root, struct resource_dir_entry, entry )
|
|
|
|
{
|
|
|
|
IMAGE_RESOURCE_DIRECTORY_ENTRY *e1;
|
|
|
|
IMAGE_RESOURCE_DIRECTORY *namedir;
|
|
|
|
|
|
|
|
e1 = (IMAGE_RESOURCE_DIRECTORY_ENTRY*) &base[si->types_ofs];
|
|
|
|
memset( e1, 0, sizeof *e1 );
|
|
|
|
if (HIWORD( types->id ))
|
|
|
|
{
|
|
|
|
WCHAR *strings;
|
|
|
|
DWORD len;
|
|
|
|
|
|
|
|
root->NumberOfNamedEntries++;
|
|
|
|
e1->u1.s1.NameIsString = 1;
|
|
|
|
e1->u1.s1.NameOffset = si->strings_ofs;
|
|
|
|
|
|
|
|
strings = (WCHAR*) &base[si->strings_ofs];
|
|
|
|
len = lstrlenW( types->id );
|
|
|
|
strings[0] = len;
|
|
|
|
memcpy( &strings[1], types->id, len * sizeof (WCHAR) );
|
|
|
|
si->strings_ofs += (len + 1) * sizeof (WCHAR);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
root->NumberOfIdEntries++;
|
|
|
|
e1->u1.s2.Id = LOWORD( types->id );
|
|
|
|
}
|
|
|
|
e1->u2.s3.OffsetToDirectory = si->names_ofs;
|
|
|
|
e1->u2.s3.DataIsDirectory = TRUE;
|
|
|
|
si->types_ofs += sizeof (IMAGE_RESOURCE_DIRECTORY_ENTRY);
|
|
|
|
|
|
|
|
namedir = (IMAGE_RESOURCE_DIRECTORY*) &base[si->names_ofs];
|
|
|
|
memset( namedir, 0, sizeof *namedir );
|
|
|
|
namedir->MajorVersion = 4;
|
|
|
|
si->names_ofs += sizeof (IMAGE_RESOURCE_DIRECTORY);
|
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY( names, &types->children, struct resource_dir_entry, entry )
|
|
|
|
{
|
|
|
|
IMAGE_RESOURCE_DIRECTORY_ENTRY *e2;
|
|
|
|
IMAGE_RESOURCE_DIRECTORY *langdir;
|
|
|
|
|
|
|
|
e2 = (IMAGE_RESOURCE_DIRECTORY_ENTRY*) &base[si->names_ofs];
|
|
|
|
memset( e2, 0, sizeof *e2 );
|
|
|
|
if (HIWORD( names->id ))
|
|
|
|
{
|
|
|
|
WCHAR *strings;
|
|
|
|
DWORD len;
|
|
|
|
|
|
|
|
namedir->NumberOfNamedEntries++;
|
|
|
|
e2->u1.s1.NameIsString = 1;
|
|
|
|
e2->u1.s1.NameOffset = si->strings_ofs;
|
|
|
|
|
|
|
|
strings = (WCHAR*) &base[si->strings_ofs];
|
|
|
|
len = lstrlenW( names->id );
|
|
|
|
strings[0] = len;
|
|
|
|
memcpy( &strings[1], names->id, len * sizeof (WCHAR) );
|
|
|
|
si->strings_ofs += (len + 1) * sizeof (WCHAR);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
namedir->NumberOfIdEntries++;
|
|
|
|
e2->u1.s2.Id = LOWORD( names->id );
|
|
|
|
}
|
|
|
|
e2->u2.s3.OffsetToDirectory = si->langs_ofs;
|
|
|
|
e2->u2.s3.DataIsDirectory = TRUE;
|
|
|
|
si->names_ofs += sizeof (IMAGE_RESOURCE_DIRECTORY_ENTRY);
|
|
|
|
|
|
|
|
langdir = (IMAGE_RESOURCE_DIRECTORY*) &base[si->langs_ofs];
|
|
|
|
memset( langdir, 0, sizeof *langdir );
|
|
|
|
langdir->MajorVersion = 4;
|
|
|
|
si->langs_ofs += sizeof (IMAGE_RESOURCE_DIRECTORY);
|
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY( data, &names->children, struct resource_data, entry )
|
|
|
|
{
|
|
|
|
IMAGE_RESOURCE_DIRECTORY_ENTRY *e3;
|
|
|
|
IMAGE_RESOURCE_DATA_ENTRY *de;
|
|
|
|
int pad_size;
|
|
|
|
|
|
|
|
e3 = (IMAGE_RESOURCE_DIRECTORY_ENTRY*) &base[si->langs_ofs];
|
|
|
|
memset( e3, 0, sizeof *e3 );
|
|
|
|
langdir->NumberOfIdEntries++;
|
|
|
|
e3->u1.s2.Id = LOWORD( data->lang );
|
|
|
|
e3->u2.OffsetToData = si->data_entry_ofs;
|
|
|
|
|
|
|
|
si->langs_ofs += sizeof (IMAGE_RESOURCE_DIRECTORY_ENTRY);
|
|
|
|
|
|
|
|
/* write out all the data entries */
|
|
|
|
de = (IMAGE_RESOURCE_DATA_ENTRY*) &base[si->data_entry_ofs];
|
|
|
|
memset( de, 0, sizeof *de );
|
|
|
|
de->OffsetToData = si->data_ofs + rva;
|
|
|
|
de->Size = data->cbData;
|
|
|
|
de->CodePage = data->codepage;
|
|
|
|
si->data_entry_ofs += sizeof (IMAGE_RESOURCE_DATA_ENTRY);
|
|
|
|
|
|
|
|
/* write out the resource data */
|
|
|
|
memcpy( &base[si->data_ofs], data->data, data->cbData );
|
|
|
|
si->data_ofs += data->cbData;
|
|
|
|
|
|
|
|
pad_size = (-si->data_ofs)&3;
|
|
|
|
res_write_padding( &base[si->data_ofs], pad_size );
|
|
|
|
si->data_ofs += pad_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FIXME:
|
|
|
|
* Assumes that the resources are in .rsrc
|
|
|
|
* and .rsrc is the last section in the file.
|
|
|
|
* Not sure whether updating resources will other cases on Windows.
|
|
|
|
* If the resources lie in a section containing other data,
|
|
|
|
* resizing that section could possibly cause trouble.
|
|
|
|
* If the section with the resources isn't last, the remaining
|
|
|
|
* sections need to be moved down in the file, and the section header
|
|
|
|
* would need to be adjusted.
|
|
|
|
* If we needed to add a section, what would we name it?
|
|
|
|
* If we needed to add a section and there wasn't space in the file
|
|
|
|
* header, how would that work?
|
|
|
|
* Seems that at least some of these cases can't be handled properly.
|
|
|
|
*/
|
2007-01-10 18:28:25 +00:00
|
|
|
static IMAGE_SECTION_HEADER *get_resource_section( void *base, DWORD mapping_size )
|
2007-01-08 09:08:15 +00:00
|
|
|
{
|
|
|
|
IMAGE_SECTION_HEADER *sec;
|
|
|
|
IMAGE_NT_HEADERS *nt;
|
|
|
|
DWORD i, num_sections = 0;
|
|
|
|
|
|
|
|
nt = get_nt_header( base, mapping_size );
|
|
|
|
if (!nt)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
sec = get_section_header( base, mapping_size, &num_sections );
|
|
|
|
if (!sec)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* find the resources section */
|
|
|
|
for (i=0; i<num_sections; i++)
|
|
|
|
if (!memcmp(sec[i].Name, ".rsrc", 6))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (i == num_sections)
|
|
|
|
{
|
|
|
|
FIXME(".rsrc doesn't exist\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check that the resources section is last */
|
|
|
|
if (i != num_sections - 1)
|
|
|
|
{
|
|
|
|
FIXME(".rsrc isn't the last section\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return &sec[i];
|
|
|
|
}
|
|
|
|
|
2007-01-10 18:28:25 +00:00
|
|
|
static DWORD get_init_data_size( void *base, DWORD mapping_size )
|
2007-01-08 09:08:15 +00:00
|
|
|
{
|
|
|
|
DWORD i, sz = 0, num_sections = 0;
|
|
|
|
IMAGE_SECTION_HEADER *s;
|
|
|
|
|
|
|
|
s = get_section_header( base, mapping_size, &num_sections );
|
|
|
|
|
|
|
|
for (i=0; i<num_sections; i++)
|
|
|
|
if (s[i].Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA)
|
|
|
|
sz += s[i].SizeOfRawData;
|
|
|
|
|
|
|
|
TRACE("size = %08x\n", sz);
|
|
|
|
|
|
|
|
return sz;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL write_raw_resources( QUEUEDUPDATES *updates )
|
|
|
|
{
|
|
|
|
static const WCHAR prefix[] = { 'r','e','s','u',0 };
|
|
|
|
WCHAR tempdir[MAX_PATH], tempfile[MAX_PATH];
|
|
|
|
DWORD mapping_size, section_size, old_size;
|
|
|
|
HANDLE file = NULL, mapping = NULL;
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
void *base = NULL;
|
|
|
|
IMAGE_SECTION_HEADER *sec;
|
|
|
|
IMAGE_NT_HEADERS *nt;
|
|
|
|
struct resource_size_info res_size;
|
|
|
|
BYTE *res_base;
|
|
|
|
|
|
|
|
/* copy the exe to a temp file then update the temp file... */
|
|
|
|
tempdir[0] = 0;
|
|
|
|
if (!GetTempPathW( MAX_PATH, tempdir ))
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (!GetTempFileNameW( tempdir, prefix, 0, tempfile ))
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (!CopyFileW( updates->pFileName, tempfile, FALSE ))
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
TRACE("tempfile %s\n", debugstr_w(tempfile));
|
|
|
|
|
|
|
|
file = CreateFileW( tempfile, GENERIC_READ | GENERIC_WRITE,
|
|
|
|
0, NULL, OPEN_EXISTING, 0, 0 );
|
|
|
|
|
|
|
|
mapping_size = GetFileSize( file, NULL );
|
|
|
|
old_size = mapping_size;
|
|
|
|
|
|
|
|
mapping = CreateFileMappingW( file, NULL, PAGE_READWRITE, 0, 0, NULL );
|
|
|
|
if (!mapping)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
base = MapViewOfFile( mapping, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, mapping_size );
|
|
|
|
if (!base)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
nt = get_nt_header( base, mapping_size );
|
|
|
|
if (!nt)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
if (nt->OptionalHeader.SectionAlignment <= 0)
|
|
|
|
{
|
|
|
|
ERR("invalid section alignment %04x\n", nt->OptionalHeader.SectionAlignment);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
sec = get_resource_section( base, mapping_size );
|
|
|
|
if (!sec)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
if ((sec->SizeOfRawData + sec->PointerToRawData) != mapping_size)
|
|
|
|
{
|
|
|
|
FIXME(".rsrc isn't at the end of the image %08x + %08x != %08x\n",
|
|
|
|
sec->SizeOfRawData, sec->PointerToRawData, mapping_size);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
TRACE("before .rsrc at %08x, size %08x\n", sec->PointerToRawData, sec->SizeOfRawData);
|
|
|
|
|
|
|
|
get_resource_sizes( updates, &res_size );
|
|
|
|
|
|
|
|
/* round up the section size */
|
|
|
|
section_size = res_size.total_size;
|
|
|
|
section_size += (-section_size) % nt->OptionalHeader.SectionAlignment;
|
|
|
|
|
|
|
|
mapping_size = sec->PointerToRawData + section_size;
|
|
|
|
|
|
|
|
TRACE("requires %08x (%08x) bytes\n", res_size.total_size, section_size );
|
|
|
|
|
|
|
|
/* check if the file size needs to be changed */
|
|
|
|
if (section_size != sec->SizeOfRawData)
|
|
|
|
{
|
|
|
|
TRACE("file size %08x -> %08x\n", old_size, mapping_size);
|
|
|
|
|
|
|
|
/* unmap the file before changing the file size */
|
|
|
|
UnmapViewOfFile( base );
|
|
|
|
base = NULL;
|
|
|
|
CloseHandle( mapping );
|
|
|
|
mapping = NULL;
|
|
|
|
|
|
|
|
/* change the file size */
|
|
|
|
SetFilePointer( file, mapping_size, NULL, FILE_BEGIN );
|
|
|
|
if (!SetEndOfFile( file ))
|
|
|
|
{
|
|
|
|
ERR("failed to set file size to %08x\n", mapping_size );
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
mapping = CreateFileMappingW( file, NULL, PAGE_READWRITE, 0, 0, NULL );
|
|
|
|
if (!mapping)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
/* remap the file */
|
|
|
|
base = MapViewOfFile( mapping, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, mapping_size );
|
|
|
|
if (!base)
|
|
|
|
{
|
|
|
|
ERR("failed to map file again\n");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the pointers again - they might be different after remapping */
|
|
|
|
nt = get_nt_header( base, mapping_size );
|
|
|
|
if (!nt)
|
|
|
|
{
|
|
|
|
ERR("couldn't get NT header\n");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
sec = get_resource_section( base, mapping_size );
|
|
|
|
if (!sec)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
/* adjust the PE header information */
|
|
|
|
nt->OptionalHeader.SizeOfImage += (mapping_size - old_size);
|
|
|
|
sec->SizeOfRawData = section_size;
|
|
|
|
sec->Misc.VirtualSize = section_size;
|
|
|
|
nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].Size = res_size.total_size;
|
|
|
|
nt->OptionalHeader.SizeOfInitializedData = get_init_data_size( base, mapping_size );
|
|
|
|
}
|
|
|
|
|
|
|
|
res_base = (LPBYTE) base + sec->PointerToRawData;
|
|
|
|
|
|
|
|
TRACE("base = %p offset = %08x\n", base, sec->PointerToRawData);
|
|
|
|
|
|
|
|
ret = write_resources( updates, res_base, &res_size, sec->VirtualAddress );
|
|
|
|
|
|
|
|
res_write_padding( res_base + res_size.total_size, section_size - res_size.total_size );
|
|
|
|
|
|
|
|
TRACE("after .rsrc at %08x, size %08x\n", sec->PointerToRawData, sec->SizeOfRawData);
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (base)
|
|
|
|
{
|
|
|
|
FlushViewOfFile( base, mapping_size );
|
|
|
|
UnmapViewOfFile( base );
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mapping)
|
|
|
|
CloseHandle( mapping );
|
|
|
|
|
|
|
|
if (file)
|
|
|
|
CloseHandle( file );
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
ret = CopyFileW( tempfile, updates->pFileName, FALSE );
|
|
|
|
|
|
|
|
DeleteFileW( tempfile );
|
|
|
|
|
|
|
|
return ret;
|
2004-09-29 21:10:44 +00:00
|
|
|
}
|
|
|
|
|
2003-06-24 03:34:15 +00:00
|
|
|
/***********************************************************************
|
|
|
|
* BeginUpdateResourceW (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
HANDLE WINAPI BeginUpdateResourceW( LPCWSTR pFileName, BOOL bDeleteExistingResources )
|
|
|
|
{
|
2006-12-28 10:58:35 +00:00
|
|
|
QUEUEDUPDATES *updates = NULL;
|
|
|
|
HANDLE hUpdate, file, ret = NULL;
|
2004-09-14 01:06:54 +00:00
|
|
|
|
2006-12-28 10:58:35 +00:00
|
|
|
TRACE("%s, %d\n", debugstr_w(pFileName), bDeleteExistingResources);
|
2004-09-14 01:06:54 +00:00
|
|
|
|
2006-12-28 10:58:35 +00:00
|
|
|
hUpdate = GlobalAlloc(GHND, sizeof(QUEUEDUPDATES));
|
|
|
|
if (!hUpdate)
|
|
|
|
return ret;
|
2004-09-14 01:06:54 +00:00
|
|
|
|
2006-12-28 10:58:35 +00:00
|
|
|
updates = GlobalLock(hUpdate);
|
|
|
|
if (updates)
|
2004-09-14 01:06:54 +00:00
|
|
|
{
|
2007-01-03 08:32:52 +00:00
|
|
|
list_init( &updates->root );
|
2007-01-08 09:08:15 +00:00
|
|
|
updates->bDeleteExistingResources = bDeleteExistingResources;
|
2006-12-28 10:58:35 +00:00
|
|
|
updates->pFileName = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(pFileName)+1)*sizeof(WCHAR));
|
|
|
|
if (updates->pFileName)
|
|
|
|
{
|
|
|
|
lstrcpyW(updates->pFileName, pFileName);
|
2004-09-14 01:06:54 +00:00
|
|
|
|
2006-12-28 10:58:35 +00:00
|
|
|
file = CreateFileW( pFileName, GENERIC_READ | GENERIC_WRITE,
|
|
|
|
0, NULL, OPEN_EXISTING, 0, 0 );
|
2004-09-14 01:06:54 +00:00
|
|
|
|
2006-12-28 10:58:35 +00:00
|
|
|
/* if resources are deleted, only the file's presence is checked */
|
|
|
|
if (file != INVALID_HANDLE_VALUE &&
|
2007-01-08 09:08:15 +00:00
|
|
|
(bDeleteExistingResources || check_pe_exe( file, updates )))
|
2006-12-28 10:58:35 +00:00
|
|
|
ret = hUpdate;
|
|
|
|
else
|
|
|
|
HeapFree( GetProcessHeap(), 0, updates->pFileName );
|
2004-09-14 01:06:54 +00:00
|
|
|
|
2006-12-28 10:58:35 +00:00
|
|
|
CloseHandle( file );
|
|
|
|
}
|
2004-09-14 01:06:54 +00:00
|
|
|
GlobalUnlock(hUpdate);
|
|
|
|
}
|
2006-12-28 10:58:35 +00:00
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
GlobalFree(hUpdate);
|
|
|
|
|
2004-09-14 01:06:54 +00:00
|
|
|
return ret;
|
2003-06-24 03:34:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
2004-09-14 01:06:54 +00:00
|
|
|
* BeginUpdateResourceA (KERNEL32.@)
|
2003-06-24 03:34:15 +00:00
|
|
|
*/
|
2004-09-14 01:06:54 +00:00
|
|
|
HANDLE WINAPI BeginUpdateResourceA( LPCSTR pFileName, BOOL bDeleteExistingResources )
|
2003-06-24 03:34:15 +00:00
|
|
|
{
|
2004-09-14 01:06:54 +00:00
|
|
|
UNICODE_STRING FileNameW;
|
|
|
|
HANDLE ret;
|
|
|
|
RtlCreateUnicodeStringFromAsciiz(&FileNameW, pFileName);
|
|
|
|
ret = BeginUpdateResourceW(FileNameW.Buffer, bDeleteExistingResources);
|
|
|
|
RtlFreeUnicodeString(&FileNameW);
|
|
|
|
return ret;
|
2003-06-24 03:34:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* EndUpdateResourceW (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
BOOL WINAPI EndUpdateResourceW( HANDLE hUpdate, BOOL fDiscard )
|
|
|
|
{
|
2006-12-28 10:58:35 +00:00
|
|
|
QUEUEDUPDATES *updates;
|
|
|
|
BOOL ret;
|
2004-09-14 01:06:54 +00:00
|
|
|
|
2006-12-28 10:58:35 +00:00
|
|
|
TRACE("%p %d\n", hUpdate, fDiscard);
|
2004-09-14 01:06:54 +00:00
|
|
|
|
2006-12-28 10:58:35 +00:00
|
|
|
updates = GlobalLock(hUpdate);
|
|
|
|
if (!updates)
|
|
|
|
return FALSE;
|
2004-09-14 01:06:54 +00:00
|
|
|
|
2007-01-08 09:08:15 +00:00
|
|
|
if (!updates->bDeleteExistingResources)
|
|
|
|
{
|
|
|
|
FIXME("preserving existing resources not yet implemented\n");
|
|
|
|
fDiscard = TRUE;
|
|
|
|
}
|
|
|
|
|
2006-12-28 10:58:35 +00:00
|
|
|
ret = fDiscard || write_raw_resources( updates );
|
|
|
|
|
2007-01-03 08:32:52 +00:00
|
|
|
free_resource_directory( &updates->root, 2 );
|
|
|
|
|
2006-12-28 10:58:35 +00:00
|
|
|
HeapFree( GetProcessHeap(), 0, updates->pFileName );
|
|
|
|
GlobalUnlock( hUpdate );
|
|
|
|
GlobalFree( hUpdate );
|
2004-09-14 01:06:54 +00:00
|
|
|
|
|
|
|
return ret;
|
2003-06-24 03:34:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
2004-09-14 01:06:54 +00:00
|
|
|
* EndUpdateResourceA (KERNEL32.@)
|
2003-06-24 03:34:15 +00:00
|
|
|
*/
|
2004-09-14 01:06:54 +00:00
|
|
|
BOOL WINAPI EndUpdateResourceA( HANDLE hUpdate, BOOL fDiscard )
|
2003-06-24 03:34:15 +00:00
|
|
|
{
|
2004-09-14 01:06:54 +00:00
|
|
|
return EndUpdateResourceW(hUpdate, fDiscard);
|
2003-06-24 03:34:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* UpdateResourceW (KERNEL32.@)
|
|
|
|
*/
|
2004-09-14 01:06:54 +00:00
|
|
|
BOOL WINAPI UpdateResourceW( HANDLE hUpdate, LPCWSTR lpType, LPCWSTR lpName,
|
|
|
|
WORD wLanguage, LPVOID lpData, DWORD cbData)
|
|
|
|
{
|
2006-12-28 10:58:35 +00:00
|
|
|
QUEUEDUPDATES *updates;
|
2004-09-14 01:06:54 +00:00
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
2006-12-28 10:58:35 +00:00
|
|
|
TRACE("%p %s %s %08x %p %d\n", hUpdate,
|
|
|
|
debugstr_w(lpType), debugstr_w(lpName), wLanguage, lpData, cbData);
|
2004-09-14 01:06:54 +00:00
|
|
|
|
2006-12-28 10:58:35 +00:00
|
|
|
updates = GlobalLock(hUpdate);
|
|
|
|
if (updates)
|
2004-09-14 01:06:54 +00:00
|
|
|
{
|
2006-12-28 10:58:35 +00:00
|
|
|
ret = update_add_resource( updates, lpType, lpName,
|
|
|
|
wLanguage, GetACP(), lpData, cbData );
|
|
|
|
GlobalUnlock(hUpdate);
|
2004-09-14 01:06:54 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* UpdateResourceA (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
BOOL WINAPI UpdateResourceA( HANDLE hUpdate, LPCSTR lpType, LPCSTR lpName,
|
|
|
|
WORD wLanguage, LPVOID lpData, DWORD cbData)
|
2003-06-24 03:34:15 +00:00
|
|
|
{
|
2004-09-14 01:06:54 +00:00
|
|
|
BOOL ret;
|
|
|
|
UNICODE_STRING TypeW;
|
|
|
|
UNICODE_STRING NameW;
|
|
|
|
if(!HIWORD(lpType))
|
|
|
|
TypeW.Buffer = (LPWSTR)lpType;
|
|
|
|
else
|
|
|
|
RtlCreateUnicodeStringFromAsciiz(&TypeW, lpType);
|
|
|
|
if(!HIWORD(lpName))
|
|
|
|
NameW.Buffer = (LPWSTR)lpName;
|
|
|
|
else
|
|
|
|
RtlCreateUnicodeStringFromAsciiz(&NameW, lpName);
|
|
|
|
ret = UpdateResourceW(hUpdate, TypeW.Buffer, NameW.Buffer, wLanguage, lpData, cbData);
|
|
|
|
if(HIWORD(lpType)) RtlFreeUnicodeString(&TypeW);
|
|
|
|
if(HIWORD(lpName)) RtlFreeUnicodeString(&NameW);
|
|
|
|
return ret;
|
2003-06-24 03:34:15 +00:00
|
|
|
}
|