4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995, 2003 Alexandre Julliard
6 * Copyright 2006 Mike McCormack
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
28 #define NONAMELESSUNION
29 #define NONAMELESSSTRUCT
31 #define WIN32_NO_STATUS
36 #include "wine/winbase16.h"
37 #include "wine/debug.h"
39 #include "wine/exception.h"
40 #include "wine/unicode.h"
41 #include "wine/list.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(resource);
45 /* handle conversions */
46 #define HRSRC_32(h16) ((HRSRC)(ULONG_PTR)(h16))
47 #define HRSRC_16(h32) (LOWORD(h32))
48 #define HGLOBAL_32(h16) ((HGLOBAL)(ULONG_PTR)(h16))
49 #define HGLOBAL_16(h32) (LOWORD(h32))
50 #define HMODULE_16(h32) (LOWORD(h32))
52 /* retrieve the resource name to pass to the ntdll functions */
53 static NTSTATUS get_res_nameA( LPCSTR name, UNICODE_STRING *str )
57 str->Buffer = (LPWSTR)name;
58 return STATUS_SUCCESS;
63 if (RtlCharToInteger( name + 1, 10, &value ) != STATUS_SUCCESS || HIWORD(value))
64 return STATUS_INVALID_PARAMETER;
65 str->Buffer = (LPWSTR)value;
66 return STATUS_SUCCESS;
68 RtlCreateUnicodeStringFromAsciiz( str, name );
69 RtlUpcaseUnicodeString( str, str, FALSE );
70 return STATUS_SUCCESS;
73 /* retrieve the resource name to pass to the ntdll functions */
74 static NTSTATUS get_res_nameW( LPCWSTR name, UNICODE_STRING *str )
78 str->Buffer = (LPWSTR)name;
79 return STATUS_SUCCESS;
84 RtlInitUnicodeString( str, name + 1 );
85 if (RtlUnicodeStringToInteger( str, 10, &value ) != STATUS_SUCCESS || HIWORD(value))
86 return STATUS_INVALID_PARAMETER;
87 str->Buffer = (LPWSTR)value;
88 return STATUS_SUCCESS;
90 RtlCreateUnicodeString( str, name );
91 RtlUpcaseUnicodeString( str, str, FALSE );
92 return STATUS_SUCCESS;
95 /* retrieve the resource names for the 16-bit FindResource function */
96 static BOOL get_res_name_type_WtoA( LPCWSTR name, LPCWSTR type, LPSTR *nameA, LPSTR *typeA )
98 *nameA = *typeA = NULL;
104 DWORD len = WideCharToMultiByte( CP_ACP, 0, name, -1, NULL, 0, NULL, NULL );
105 *nameA = HeapAlloc( GetProcessHeap(), 0, len );
106 if (*nameA) WideCharToMultiByte( CP_ACP, 0, name, -1, *nameA, len, NULL, NULL );
108 else *nameA = (LPSTR)name;
112 DWORD len = WideCharToMultiByte( CP_ACP, 0, type, -1, NULL, 0, NULL, NULL );
113 *typeA = HeapAlloc( GetProcessHeap(), 0, len );
114 if (*typeA) WideCharToMultiByte( CP_ACP, 0, type, -1, *typeA, len, NULL, NULL );
116 else *typeA = (LPSTR)type;
120 if (HIWORD(*nameA)) HeapFree( GetProcessHeap(), 0, *nameA );
121 if (HIWORD(*typeA)) HeapFree( GetProcessHeap(), 0, *typeA );
122 SetLastError( ERROR_INVALID_PARAMETER );
129 /* implementation of FindResourceExA */
130 static HRSRC find_resourceA( HMODULE hModule, LPCSTR type, LPCSTR name, WORD lang )
133 UNICODE_STRING nameW, typeW;
134 LDR_RESOURCE_INFO info;
135 const IMAGE_RESOURCE_DATA_ENTRY *entry = NULL;
139 if ((status = get_res_nameA( name, &nameW )) != STATUS_SUCCESS) goto done;
140 if ((status = get_res_nameA( type, &typeW )) != STATUS_SUCCESS) goto done;
141 info.Type = (ULONG_PTR)typeW.Buffer;
142 info.Name = (ULONG_PTR)nameW.Buffer;
143 info.Language = lang;
144 status = LdrFindResource_U( hModule, &info, 3, &entry );
146 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
150 SetLastError( ERROR_INVALID_PARAMETER );
154 if (HIWORD(nameW.Buffer)) HeapFree( GetProcessHeap(), 0, nameW.Buffer );
155 if (HIWORD(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
160 /* implementation of FindResourceExW */
161 static HRSRC find_resourceW( HMODULE hModule, LPCWSTR type, LPCWSTR name, WORD lang )
164 UNICODE_STRING nameW, typeW;
165 LDR_RESOURCE_INFO info;
166 const IMAGE_RESOURCE_DATA_ENTRY *entry = NULL;
168 nameW.Buffer = typeW.Buffer = NULL;
172 if ((status = get_res_nameW( name, &nameW )) != STATUS_SUCCESS) goto done;
173 if ((status = get_res_nameW( type, &typeW )) != STATUS_SUCCESS) goto done;
174 info.Type = (ULONG_PTR)typeW.Buffer;
175 info.Name = (ULONG_PTR)nameW.Buffer;
176 info.Language = lang;
177 status = LdrFindResource_U( hModule, &info, 3, &entry );
179 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
183 SetLastError( ERROR_INVALID_PARAMETER );
187 if (HIWORD(nameW.Buffer)) HeapFree( GetProcessHeap(), 0, nameW.Buffer );
188 if (HIWORD(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
192 /**********************************************************************
193 * FindResourceExA (KERNEL32.@)
195 HRSRC WINAPI FindResourceExA( HMODULE hModule, LPCSTR type, LPCSTR name, WORD lang )
197 TRACE( "%p %s %s %04x\n", hModule, debugstr_a(type), debugstr_a(name), lang );
199 if (!hModule) hModule = GetModuleHandleW(0);
200 else if (!HIWORD(hModule))
202 return HRSRC_32( FindResource16( HMODULE_16(hModule), name, type ) );
204 return find_resourceA( hModule, type, name, lang );
208 /**********************************************************************
209 * FindResourceA (KERNEL32.@)
211 HRSRC WINAPI FindResourceA( HMODULE hModule, LPCSTR name, LPCSTR type )
213 return FindResourceExA( hModule, type, name, MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ) );
217 /**********************************************************************
218 * FindResourceExW (KERNEL32.@)
220 HRSRC WINAPI FindResourceExW( HMODULE hModule, LPCWSTR type, LPCWSTR name, WORD lang )
222 TRACE( "%p %s %s %04x\n", hModule, debugstr_w(type), debugstr_w(name), lang );
224 if (!hModule) hModule = GetModuleHandleW(0);
225 else if (!HIWORD(hModule))
230 if (!get_res_name_type_WtoA( name, type, &nameA, &typeA )) return NULL;
232 ret = FindResource16( HMODULE_16(hModule), nameA, typeA );
233 if (HIWORD(nameA)) HeapFree( GetProcessHeap(), 0, nameA );
234 if (HIWORD(typeA)) HeapFree( GetProcessHeap(), 0, typeA );
235 return HRSRC_32(ret);
238 return find_resourceW( hModule, type, name, lang );
242 /**********************************************************************
243 * FindResourceW (KERNEL32.@)
245 HRSRC WINAPI FindResourceW( HINSTANCE hModule, LPCWSTR name, LPCWSTR type )
247 return FindResourceExW( hModule, type, name, MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ) );
251 /**********************************************************************
252 * EnumResourceTypesA (KERNEL32.@)
254 BOOL WINAPI EnumResourceTypesA( HMODULE hmod, ENUMRESTYPEPROCA lpfun, LONG_PTR lparam )
259 DWORD len = 0, newlen;
261 const IMAGE_RESOURCE_DIRECTORY *resdir;
262 const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
263 const IMAGE_RESOURCE_DIR_STRING_U *str;
265 TRACE( "%p %p %lx\n", hmod, lpfun, lparam );
267 if (!hmod) hmod = GetModuleHandleA( NULL );
269 if ((status = LdrFindResourceDirectory_U( hmod, NULL, 0, &resdir )) != STATUS_SUCCESS)
271 SetLastError( RtlNtStatusToDosError(status) );
274 et = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resdir + 1);
275 for (i = 0; i < resdir->NumberOfNamedEntries+resdir->NumberOfIdEntries; i++)
277 if (et[i].u1.s1.NameIsString)
279 str = (const IMAGE_RESOURCE_DIR_STRING_U *)((const BYTE *)resdir + et[i].u1.s1.NameOffset);
280 newlen = WideCharToMultiByte( CP_ACP, 0, str->NameString, str->Length, NULL, 0, NULL, NULL);
281 if (newlen + 1 > len)
284 HeapFree( GetProcessHeap(), 0, type );
285 if (!(type = HeapAlloc( GetProcessHeap(), 0, len ))) return FALSE;
287 WideCharToMultiByte( CP_ACP, 0, str->NameString, str->Length, type, len, NULL, NULL);
289 ret = lpfun(hmod,type,lparam);
293 ret = lpfun( hmod, (LPSTR)(int)et[i].u1.s2.Id, lparam );
297 HeapFree( GetProcessHeap(), 0, type );
302 /**********************************************************************
303 * EnumResourceTypesW (KERNEL32.@)
305 BOOL WINAPI EnumResourceTypesW( HMODULE hmod, ENUMRESTYPEPROCW lpfun, LONG_PTR lparam )
311 const IMAGE_RESOURCE_DIRECTORY *resdir;
312 const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
313 const IMAGE_RESOURCE_DIR_STRING_U *str;
315 TRACE( "%p %p %lx\n", hmod, lpfun, lparam );
317 if (!hmod) hmod = GetModuleHandleW( NULL );
319 if ((status = LdrFindResourceDirectory_U( hmod, NULL, 0, &resdir )) != STATUS_SUCCESS)
321 SetLastError( RtlNtStatusToDosError(status) );
324 et = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resdir + 1);
325 for (i = 0; i < resdir->NumberOfNamedEntries + resdir->NumberOfIdEntries; i++)
327 if (et[i].u1.s1.NameIsString)
329 str = (const IMAGE_RESOURCE_DIR_STRING_U *)((const BYTE *)resdir + et[i].u1.s1.NameOffset);
330 if (str->Length + 1 > len)
332 len = str->Length + 1;
333 HeapFree( GetProcessHeap(), 0, type );
334 if (!(type = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return FALSE;
336 memcpy(type, str->NameString, str->Length * sizeof (WCHAR));
337 type[str->Length] = 0;
338 ret = lpfun(hmod,type,lparam);
342 ret = lpfun( hmod, (LPWSTR)(int)et[i].u1.s2.Id, lparam );
346 HeapFree( GetProcessHeap(), 0, type );
351 /**********************************************************************
352 * EnumResourceNamesA (KERNEL32.@)
354 BOOL WINAPI EnumResourceNamesA( HMODULE hmod, LPCSTR type, ENUMRESNAMEPROCA lpfun, LONG_PTR lparam )
358 DWORD len = 0, newlen;
361 UNICODE_STRING typeW;
362 LDR_RESOURCE_INFO info;
363 const IMAGE_RESOURCE_DIRECTORY *basedir, *resdir;
364 const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
365 const IMAGE_RESOURCE_DIR_STRING_U *str;
367 TRACE( "%p %s %p %lx\n", hmod, debugstr_a(type), lpfun, lparam );
369 if (!hmod) hmod = GetModuleHandleA( NULL );
371 if ((status = LdrFindResourceDirectory_U( hmod, NULL, 0, &basedir )) != STATUS_SUCCESS)
373 if ((status = get_res_nameA( type, &typeW )) != STATUS_SUCCESS)
375 info.Type = (ULONG)typeW.Buffer;
376 if ((status = LdrFindResourceDirectory_U( hmod, &info, 1, &resdir )) != STATUS_SUCCESS)
379 et = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resdir + 1);
380 for (i = 0; i < resdir->NumberOfNamedEntries+resdir->NumberOfIdEntries; i++)
382 if (et[i].u1.s1.NameIsString)
384 str = (const IMAGE_RESOURCE_DIR_STRING_U *)((const BYTE *)basedir + et[i].u1.s1.NameOffset);
385 newlen = WideCharToMultiByte(CP_ACP, 0, str->NameString, str->Length, NULL, 0, NULL, NULL);
386 if (newlen + 1 > len)
389 HeapFree( GetProcessHeap(), 0, name );
390 if (!(name = HeapAlloc(GetProcessHeap(), 0, len + 1 )))
396 WideCharToMultiByte( CP_ACP, 0, str->NameString, str->Length, name, len, NULL, NULL );
398 ret = lpfun(hmod,type,name,lparam);
402 ret = lpfun( hmod, type, (LPSTR)(int)et[i].u1.s2.Id, lparam );
407 HeapFree( GetProcessHeap(), 0, name );
408 if (HIWORD(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
409 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
414 /**********************************************************************
415 * EnumResourceNamesW (KERNEL32.@)
417 BOOL WINAPI EnumResourceNamesW( HMODULE hmod, LPCWSTR type, ENUMRESNAMEPROCW lpfun, LONG_PTR lparam )
423 UNICODE_STRING typeW;
424 LDR_RESOURCE_INFO info;
425 const IMAGE_RESOURCE_DIRECTORY *basedir, *resdir;
426 const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
427 const IMAGE_RESOURCE_DIR_STRING_U *str;
429 TRACE( "%p %s %p %lx\n", hmod, debugstr_w(type), lpfun, lparam );
431 if (!hmod) hmod = GetModuleHandleW( NULL );
433 if ((status = LdrFindResourceDirectory_U( hmod, NULL, 0, &basedir )) != STATUS_SUCCESS)
435 if ((status = get_res_nameW( type, &typeW )) != STATUS_SUCCESS)
437 info.Type = (ULONG)typeW.Buffer;
438 if ((status = LdrFindResourceDirectory_U( hmod, &info, 1, &resdir )) != STATUS_SUCCESS)
441 et = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resdir + 1);
442 for (i = 0; i < resdir->NumberOfNamedEntries+resdir->NumberOfIdEntries; i++)
444 if (et[i].u1.s1.NameIsString)
446 str = (const IMAGE_RESOURCE_DIR_STRING_U *)((const BYTE *)basedir + et[i].u1.s1.NameOffset);
447 if (str->Length + 1 > len)
449 len = str->Length + 1;
450 HeapFree( GetProcessHeap(), 0, name );
451 if (!(name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
457 memcpy(name, str->NameString, str->Length * sizeof (WCHAR));
458 name[str->Length] = 0;
459 ret = lpfun(hmod,type,name,lparam);
463 ret = lpfun( hmod, type, (LPWSTR)(int)et[i].u1.s2.Id, lparam );
468 HeapFree( GetProcessHeap(), 0, name );
469 if (HIWORD(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
470 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
475 /**********************************************************************
476 * EnumResourceLanguagesA (KERNEL32.@)
478 BOOL WINAPI EnumResourceLanguagesA( HMODULE hmod, LPCSTR type, LPCSTR name,
479 ENUMRESLANGPROCA lpfun, LONG_PTR lparam )
484 UNICODE_STRING typeW, nameW;
485 LDR_RESOURCE_INFO info;
486 const IMAGE_RESOURCE_DIRECTORY *basedir, *resdir;
487 const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
489 TRACE( "%p %s %s %p %lx\n", hmod, debugstr_a(type), debugstr_a(name), lpfun, lparam );
491 if (!hmod) hmod = GetModuleHandleA( NULL );
492 typeW.Buffer = nameW.Buffer = NULL;
493 if ((status = LdrFindResourceDirectory_U( hmod, NULL, 0, &basedir )) != STATUS_SUCCESS)
495 if ((status = get_res_nameA( type, &typeW )) != STATUS_SUCCESS)
497 if ((status = get_res_nameA( name, &nameW )) != STATUS_SUCCESS)
499 info.Type = (ULONG_PTR)typeW.Buffer;
500 info.Name = (ULONG_PTR)nameW.Buffer;
501 if ((status = LdrFindResourceDirectory_U( hmod, &info, 2, &resdir )) != STATUS_SUCCESS)
504 et = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resdir + 1);
505 for (i = 0; i < resdir->NumberOfNamedEntries + resdir->NumberOfIdEntries; i++)
507 ret = lpfun( hmod, type, name, et[i].u1.s2.Id, lparam );
511 if (HIWORD(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
512 if (HIWORD(nameW.Buffer)) HeapFree( GetProcessHeap(), 0, nameW.Buffer );
513 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
518 /**********************************************************************
519 * EnumResourceLanguagesW (KERNEL32.@)
521 BOOL WINAPI EnumResourceLanguagesW( HMODULE hmod, LPCWSTR type, LPCWSTR name,
522 ENUMRESLANGPROCW lpfun, LONG_PTR lparam )
527 UNICODE_STRING typeW, nameW;
528 LDR_RESOURCE_INFO info;
529 const IMAGE_RESOURCE_DIRECTORY *basedir, *resdir;
530 const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
532 TRACE( "%p %s %s %p %lx\n", hmod, debugstr_w(type), debugstr_w(name), lpfun, lparam );
534 if (!hmod) hmod = GetModuleHandleW( NULL );
535 typeW.Buffer = nameW.Buffer = NULL;
536 if ((status = LdrFindResourceDirectory_U( hmod, NULL, 0, &basedir )) != STATUS_SUCCESS)
538 if ((status = get_res_nameW( type, &typeW )) != STATUS_SUCCESS)
540 if ((status = get_res_nameW( name, &nameW )) != STATUS_SUCCESS)
542 info.Type = (ULONG_PTR)typeW.Buffer;
543 info.Name = (ULONG_PTR)nameW.Buffer;
544 if ((status = LdrFindResourceDirectory_U( hmod, &info, 2, &resdir )) != STATUS_SUCCESS)
547 et = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resdir + 1);
548 for (i = 0; i < resdir->NumberOfNamedEntries + resdir->NumberOfIdEntries; i++)
550 ret = lpfun( hmod, type, name, et[i].u1.s2.Id, lparam );
554 if (HIWORD(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
555 if (HIWORD(nameW.Buffer)) HeapFree( GetProcessHeap(), 0, nameW.Buffer );
556 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
561 /**********************************************************************
562 * LoadResource (KERNEL32.@)
564 HGLOBAL WINAPI LoadResource( HINSTANCE hModule, HRSRC hRsrc )
569 TRACE( "%p %p\n", hModule, hRsrc );
571 if (hModule && !HIWORD(hModule))
572 /* FIXME: should convert return to 32-bit resource */
573 return HGLOBAL_32( LoadResource16( HMODULE_16(hModule), HRSRC_16(hRsrc) ) );
575 if (!hRsrc) return 0;
576 if (!hModule) hModule = GetModuleHandleA( NULL );
577 status = LdrAccessResource( hModule, (IMAGE_RESOURCE_DATA_ENTRY *)hRsrc, &ret, NULL );
578 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
583 /**********************************************************************
584 * LockResource (KERNEL32.@)
586 LPVOID WINAPI LockResource( HGLOBAL handle )
588 TRACE("(%p)\n", handle );
590 if (HIWORD( handle )) /* 32-bit memory handle */
591 return (LPVOID)handle;
593 /* 16-bit memory handle */
594 return LockResource16( HGLOBAL_16(handle) );
598 /**********************************************************************
599 * FreeResource (KERNEL32.@)
601 BOOL WINAPI FreeResource( HGLOBAL handle )
603 if (HIWORD(handle)) return 0; /* 32-bit memory handle: nothing to do */
604 return FreeResource16( HGLOBAL_16(handle) );
608 /**********************************************************************
609 * SizeofResource (KERNEL32.@)
611 DWORD WINAPI SizeofResource( HINSTANCE hModule, HRSRC hRsrc )
613 if (hModule && !HIWORD(hModule))
614 return SizeofResource16( HMODULE_16(hModule), HRSRC_16(hRsrc) );
616 if (!hRsrc) return 0;
617 return ((PIMAGE_RESOURCE_DATA_ENTRY)hRsrc)->Size;
621 * Data structure for updating resources.
622 * Type/Name/Language is a keyset for accessing resource data.
624 * QUEUEDUPDATES (root) ->
625 * list of struct resource_dir_entry (Type) ->
626 * list of struct resource_dir_entry (Name) ->
627 * list of struct resource_data Language + Data
633 BOOL bDeleteExistingResources;
637 /* this structure is shared for types and names */
638 struct resource_dir_entry {
641 struct list children;
644 /* this structure is the leaf */
645 struct resource_data {
653 static int resource_strcmp( LPCWSTR a, LPCWSTR b )
657 if (HIWORD( a ) && HIWORD( b ) )
658 return lstrcmpW( a, b );
659 /* strings come before ids */
660 if (HIWORD( a ) && !HIWORD( b ))
662 if (HIWORD( b ) && !HIWORD( a ))
664 return ( a < b ) ? -1 : 1;
667 static struct resource_dir_entry *find_resource_dir_entry( struct list *dir, LPCWSTR id )
669 struct resource_dir_entry *ent;
671 /* match either IDs or strings */
672 LIST_FOR_EACH_ENTRY( ent, dir, struct resource_dir_entry, entry )
673 if (!resource_strcmp( id, ent->id ))
679 static struct resource_data *find_resource_data( struct list *dir, LANGID lang )
681 struct resource_data *res_data;
683 /* match only languages here */
684 LIST_FOR_EACH_ENTRY( res_data, dir, struct resource_data, entry )
685 if ( lang == res_data->lang )
691 static void add_resource_dir_entry( struct list *dir, struct resource_dir_entry *resdir )
693 struct resource_dir_entry *ent;
695 LIST_FOR_EACH_ENTRY( ent, dir, struct resource_dir_entry, entry )
697 if (0>resource_strcmp( ent->id, resdir->id ))
700 list_add_before( &ent->entry, &resdir->entry );
703 list_add_tail( dir, &resdir->entry );
706 static void add_resource_data_entry( struct list *dir, struct resource_data *resdata )
708 struct resource_data *ent;
710 LIST_FOR_EACH_ENTRY( ent, dir, struct resource_data, entry )
712 if (ent->lang < resdata->lang)
715 list_add_before( &ent->entry, &resdata->entry );
718 list_add_tail( dir, &resdata->entry );
721 static LPWSTR res_strdupW( LPCWSTR str )
726 if (HIWORD(str) == 0)
727 return (LPWSTR) (UINT_PTR) LOWORD(str);
728 len = (lstrlenW( str ) + 1) * sizeof (WCHAR);
729 ret = HeapAlloc( GetProcessHeap(), 0, len );
730 memcpy( ret, str, len );
734 static void res_free_str( LPWSTR str )
737 HeapFree( GetProcessHeap(), 0, str );
740 static BOOL update_add_resource( QUEUEDUPDATES *updates, LPCWSTR Type, LPCWSTR Name,
741 struct resource_data *resdata, BOOL overwrite_existing )
743 struct resource_dir_entry *restype, *resname;
744 struct resource_data *existing;
746 TRACE("%p %s %s %p %d\n", updates,
747 debugstr_w(Type), debugstr_w(Name), resdata, overwrite_existing );
749 restype = find_resource_dir_entry( &updates->root, Type );
752 restype = HeapAlloc( GetProcessHeap(), 0, sizeof *restype );
753 restype->id = res_strdupW( Type );
754 list_init( &restype->children );
755 add_resource_dir_entry( &updates->root, restype );
758 resname = find_resource_dir_entry( &restype->children, Name );
761 resname = HeapAlloc( GetProcessHeap(), 0, sizeof *resname );
762 resname->id = res_strdupW( Name );
763 list_init( &resname->children );
764 add_resource_dir_entry( &restype->children, resname );
768 * If there's an existing resource entry with matching (Type,Name,Language)
769 * it needs to be removed before adding the new data.
771 existing = find_resource_data( &resname->children, resdata->lang );
774 if (!overwrite_existing)
776 list_remove( &existing->entry );
777 HeapFree( GetProcessHeap(), 0, existing );
780 add_resource_data_entry( &resname->children, resdata );
785 static struct resource_data *allocate_resource_data( WORD Language, DWORD codepage,
786 LPVOID lpData, DWORD cbData, BOOL copy_data )
788 struct resource_data *resdata;
790 if (!lpData || !cbData)
793 resdata = HeapAlloc( GetProcessHeap(), 0, sizeof *resdata + (copy_data ? cbData : 0) );
796 resdata->lang = Language;
797 resdata->codepage = codepage;
798 resdata->cbData = cbData;
801 resdata->lpData = &resdata[1];
802 memcpy( resdata->lpData, lpData, cbData );
805 resdata->lpData = lpData;
811 static void free_resource_directory( struct list *head, int level )
813 struct list *ptr = NULL;
815 while ((ptr = list_head( head )))
820 struct resource_dir_entry *ent;
822 ent = LIST_ENTRY( ptr, struct resource_dir_entry, entry );
823 res_free_str( ent->id );
824 free_resource_directory( &ent->children, level - 1 );
825 HeapFree(GetProcessHeap(), 0, ent);
829 struct resource_data *data;
831 data = LIST_ENTRY( ptr, struct resource_data, entry );
832 HeapFree( GetProcessHeap(), 0, data );
837 static IMAGE_NT_HEADERS *get_nt_header( void *base, DWORD mapping_size )
839 IMAGE_NT_HEADERS *nt;
840 IMAGE_DOS_HEADER *dos;
842 if (mapping_size<sizeof (*dos))
846 if (dos->e_magic != IMAGE_DOS_SIGNATURE)
849 if ((dos->e_lfanew + sizeof (*nt)) > mapping_size)
852 nt = (void*) ((BYTE*)base + dos->e_lfanew);
854 if (nt->Signature != IMAGE_NT_SIGNATURE)
860 static IMAGE_SECTION_HEADER *get_section_header( void *base, DWORD mapping_size, DWORD *num_sections )
862 IMAGE_NT_HEADERS *nt;
863 IMAGE_SECTION_HEADER *sec;
866 nt = get_nt_header( base, mapping_size );
870 /* check that we don't go over the end of the file accessing the sections */
871 section_ofs = FIELD_OFFSET(IMAGE_NT_HEADERS, OptionalHeader) + nt->FileHeader.SizeOfOptionalHeader;
872 if ((nt->FileHeader.NumberOfSections * sizeof (*sec) + section_ofs) > mapping_size)
876 *num_sections = nt->FileHeader.NumberOfSections;
878 /* from here we have a valid PE exe to update */
879 return (void*) ((BYTE*)nt + section_ofs);
882 static BOOL check_pe_exe( HANDLE file, QUEUEDUPDATES *updates )
884 const IMAGE_NT_HEADERS *nt;
885 const IMAGE_SECTION_HEADER *sec;
888 DWORD mapping_size, num_sections = 0;
891 mapping_size = GetFileSize( file, NULL );
893 mapping = CreateFileMappingW( file, NULL, PAGE_READONLY, 0, 0, NULL );
897 base = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, mapping_size );
901 nt = get_nt_header( base, mapping_size );
905 TRACE("resources: %08x %08x\n",
906 nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress,
907 nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].Size);
909 sec = get_section_header( base, mapping_size, &num_sections );
917 UnmapViewOfFile( base );
919 CloseHandle( mapping );
924 struct resource_size_info {
928 DWORD data_entry_ofs;
934 struct mapping_info {
942 static const IMAGE_SECTION_HEADER *section_from_rva( void *base, DWORD mapping_size, DWORD rva )
944 const IMAGE_SECTION_HEADER *sec;
945 DWORD num_sections = 0;
948 sec = get_section_header( base, mapping_size, &num_sections );
952 for (i=num_sections-1; i>=0; i--)
954 if (sec[i].VirtualAddress <= rva &&
955 rva <= (DWORD)sec[i].VirtualAddress + sec[i].SizeOfRawData)
964 static void *address_from_rva( void *base, DWORD mapping_size, DWORD rva, DWORD len )
966 const IMAGE_SECTION_HEADER *sec;
968 sec = section_from_rva( base, mapping_size, rva );
972 if (rva + len <= (DWORD)sec->VirtualAddress + sec->SizeOfRawData)
973 return (void*)((const BYTE*) base + (sec->PointerToRawData + rva - sec->VirtualAddress));
978 static LPWSTR resource_dup_string( const IMAGE_RESOURCE_DIRECTORY *root, const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry )
980 const IMAGE_RESOURCE_DIR_STRING_U* string;
983 if (!entry->u1.s1.NameIsString)
984 return (LPWSTR) (DWORD) entry->u1.s2.Id;
986 string = (const IMAGE_RESOURCE_DIR_STRING_U*) (((const char *)root) + entry->u1.s1.NameOffset);
987 s = HeapAlloc(GetProcessHeap(), 0, (string->Length + 1)*sizeof (WCHAR) );
988 memcpy( s, string->NameString, (string->Length + 1)*sizeof (WCHAR) );
989 s[string->Length] = 0;
994 /* this function is based on the code in winedump's pe.c */
995 static BOOL enumerate_mapped_resources( QUEUEDUPDATES *updates,
996 void *base, DWORD mapping_size,
997 const IMAGE_RESOURCE_DIRECTORY *root )
999 const IMAGE_RESOURCE_DIRECTORY *namedir, *langdir;
1000 const IMAGE_RESOURCE_DIRECTORY_ENTRY *e1, *e2, *e3;
1001 const IMAGE_RESOURCE_DATA_ENTRY *data;
1004 TRACE("version (%d.%d) %d named %d id entries\n",
1005 root->MajorVersion, root->MinorVersion, root->NumberOfNamedEntries, root->NumberOfIdEntries);
1007 for (i = 0; i< root->NumberOfNamedEntries + root->NumberOfIdEntries; i++)
1011 e1 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(root + 1) + i;
1013 Type = resource_dup_string( root, e1 );
1015 namedir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e1->u2.s3.OffsetToDirectory);
1016 for (j = 0; j < namedir->NumberOfNamedEntries + namedir->NumberOfIdEntries; j++)
1020 e2 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(namedir + 1) + j;
1022 Name = resource_dup_string( root, e2 );
1024 langdir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e2->u2.s3.OffsetToDirectory);
1025 for (k = 0; k < langdir->NumberOfNamedEntries + langdir->NumberOfIdEntries; k++)
1029 struct resource_data *resdata;
1031 e3 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(langdir + 1) + k;
1033 Lang = e3->u1.s2.Id;
1035 data = (const IMAGE_RESOURCE_DATA_ENTRY *)((const char *)root + e3->u2.OffsetToData);
1037 p = address_from_rva( base, mapping_size, data->OffsetToData, data->Size );
1039 resdata = allocate_resource_data( Lang, data->CodePage, p, data->Size, FALSE );
1041 update_add_resource( updates, Type, Name, resdata, FALSE );
1043 res_free_str( Name );
1045 res_free_str( Type );
1051 static BOOL read_mapped_resources( QUEUEDUPDATES *updates, void *base, DWORD mapping_size )
1053 const IMAGE_RESOURCE_DIRECTORY *root;
1054 const IMAGE_NT_HEADERS *nt;
1055 const IMAGE_SECTION_HEADER *sec;
1056 DWORD num_sections = 0, i;
1058 nt = get_nt_header( base, mapping_size );
1062 sec = get_section_header( base, mapping_size, &num_sections );
1066 for (i=0; i<num_sections; i++)
1067 if (!memcmp(sec[i].Name, ".rsrc", 6))
1070 if (i == num_sections)
1073 /* check the resource data is inside the mapping */
1074 if (sec[i].PointerToRawData > mapping_size ||
1075 (sec[i].PointerToRawData + sec[i].SizeOfRawData) > mapping_size)
1078 TRACE("found .rsrc at %08x, size %08x\n", sec[i].PointerToRawData, sec[i].SizeOfRawData);
1080 root = (void*) ((BYTE*)base + sec[i].PointerToRawData);
1081 enumerate_mapped_resources( updates, base, mapping_size, root );
1086 static BOOL map_file_into_memory( struct mapping_info *mi )
1088 DWORD page_attr, perm;
1092 page_attr = PAGE_READWRITE;
1093 perm = FILE_MAP_WRITE | FILE_MAP_READ;
1097 page_attr = PAGE_READONLY;
1098 perm = FILE_MAP_READ;
1101 mi->mapping = CreateFileMappingW( mi->file, NULL, page_attr, 0, 0, NULL );
1105 mi->base = MapViewOfFile( mi->mapping, perm, 0, 0, mi->size );
1112 static BOOL unmap_file_from_memory( struct mapping_info *mi )
1115 UnmapViewOfFile( mi->base );
1118 CloseHandle( mi->mapping );
1123 static void destroy_mapping( struct mapping_info *mi )
1127 unmap_file_from_memory( mi );
1129 CloseHandle( mi->file );
1130 HeapFree( GetProcessHeap(), 0, mi );
1133 static struct mapping_info *create_mapping( LPCWSTR name, BOOL rw )
1135 struct mapping_info *mi;
1137 mi = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof *mi );
1141 mi->read_write = rw;
1143 mi->file = CreateFileW( name, GENERIC_READ | (rw ? GENERIC_WRITE : 0),
1144 0, NULL, OPEN_EXISTING, 0, 0 );
1146 if (mi->file != INVALID_HANDLE_VALUE)
1148 mi->size = GetFileSize( mi->file, NULL );
1150 if (map_file_into_memory( mi ))
1154 unmap_file_from_memory( mi );
1159 static BOOL resize_mapping( struct mapping_info *mi, DWORD new_size )
1161 if (!unmap_file_from_memory( mi ))
1164 /* change the file size */
1165 SetFilePointer( mi->file, new_size, NULL, FILE_BEGIN );
1166 if (!SetEndOfFile( mi->file ))
1168 ERR("failed to set file size to %08x\n", new_size );
1172 mi->size = new_size;
1174 return map_file_into_memory( mi );
1177 static void get_resource_sizes( QUEUEDUPDATES *updates, struct resource_size_info *si )
1179 struct resource_dir_entry *types, *names;
1180 struct resource_data *data;
1181 DWORD num_types = 0, num_names = 0, num_langs = 0, strings_size = 0, data_size = 0;
1183 memset( si, 0, sizeof *si );
1185 LIST_FOR_EACH_ENTRY( types, &updates->root, struct resource_dir_entry, entry )
1188 if (HIWORD( types->id ))
1189 strings_size += sizeof (WORD) + lstrlenW( types->id )*sizeof (WCHAR);
1191 LIST_FOR_EACH_ENTRY( names, &types->children, struct resource_dir_entry, entry )
1195 if (HIWORD( names->id ))
1196 strings_size += sizeof (WORD) + lstrlenW( names->id )*sizeof (WCHAR);
1198 LIST_FOR_EACH_ENTRY( data, &names->children, struct resource_data, entry )
1201 data_size += (data->cbData + 3) & ~3;
1206 /* names are at the end of the types */
1207 si->names_ofs = sizeof (IMAGE_RESOURCE_DIRECTORY) +
1208 num_types * sizeof (IMAGE_RESOURCE_DIRECTORY_ENTRY);
1210 /* language directories are at the end of the names */
1211 si->langs_ofs = si->names_ofs +
1212 num_types * sizeof (IMAGE_RESOURCE_DIRECTORY) +
1213 num_names * sizeof (IMAGE_RESOURCE_DIRECTORY_ENTRY);
1215 si->data_entry_ofs = si->langs_ofs +
1216 num_names * sizeof (IMAGE_RESOURCE_DIRECTORY) +
1217 num_langs * sizeof (IMAGE_RESOURCE_DIRECTORY_ENTRY);
1219 si->strings_ofs = si->data_entry_ofs +
1220 num_langs * sizeof (IMAGE_RESOURCE_DATA_ENTRY);
1222 si->data_ofs = si->strings_ofs + ((strings_size + 3) & ~3);
1224 si->total_size = si->data_ofs + data_size;
1226 TRACE("names %08x langs %08x data entries %08x strings %08x data %08x total %08x\n",
1227 si->names_ofs, si->langs_ofs, si->data_entry_ofs,
1228 si->strings_ofs, si->data_ofs, si->total_size);
1231 static void res_write_padding( BYTE *res_base, DWORD size )
1233 static const BYTE pad[] = {
1234 'P','A','D','D','I','N','G','X','X','P','A','D','D','I','N','G' };
1237 for ( i = 0; i < size / sizeof pad; i++ )
1238 memcpy( &res_base[i*sizeof pad], pad, sizeof pad );
1239 memcpy( &res_base[i*sizeof pad], pad, size%sizeof pad );
1242 static BOOL write_resources( QUEUEDUPDATES *updates, LPBYTE base, struct resource_size_info *si, DWORD rva )
1244 struct resource_dir_entry *types, *names;
1245 struct resource_data *data;
1246 IMAGE_RESOURCE_DIRECTORY *root;
1248 TRACE("%p %p %p %08x\n", updates, base, si, rva );
1250 memset( base, 0, si->total_size );
1252 /* the root entry always exists */
1253 root = (IMAGE_RESOURCE_DIRECTORY*) base;
1254 memset( root, 0, sizeof *root );
1255 root->MajorVersion = 4;
1256 si->types_ofs = sizeof *root;
1257 LIST_FOR_EACH_ENTRY( types, &updates->root, struct resource_dir_entry, entry )
1259 IMAGE_RESOURCE_DIRECTORY_ENTRY *e1;
1260 IMAGE_RESOURCE_DIRECTORY *namedir;
1262 e1 = (IMAGE_RESOURCE_DIRECTORY_ENTRY*) &base[si->types_ofs];
1263 memset( e1, 0, sizeof *e1 );
1264 if (HIWORD( types->id ))
1269 root->NumberOfNamedEntries++;
1270 e1->u1.s1.NameIsString = 1;
1271 e1->u1.s1.NameOffset = si->strings_ofs;
1273 strings = (WCHAR*) &base[si->strings_ofs];
1274 len = lstrlenW( types->id );
1276 memcpy( &strings[1], types->id, len * sizeof (WCHAR) );
1277 si->strings_ofs += (len + 1) * sizeof (WCHAR);
1281 root->NumberOfIdEntries++;
1282 e1->u1.s2.Id = LOWORD( types->id );
1284 e1->u2.s3.OffsetToDirectory = si->names_ofs;
1285 e1->u2.s3.DataIsDirectory = TRUE;
1286 si->types_ofs += sizeof (IMAGE_RESOURCE_DIRECTORY_ENTRY);
1288 namedir = (IMAGE_RESOURCE_DIRECTORY*) &base[si->names_ofs];
1289 memset( namedir, 0, sizeof *namedir );
1290 namedir->MajorVersion = 4;
1291 si->names_ofs += sizeof (IMAGE_RESOURCE_DIRECTORY);
1293 LIST_FOR_EACH_ENTRY( names, &types->children, struct resource_dir_entry, entry )
1295 IMAGE_RESOURCE_DIRECTORY_ENTRY *e2;
1296 IMAGE_RESOURCE_DIRECTORY *langdir;
1298 e2 = (IMAGE_RESOURCE_DIRECTORY_ENTRY*) &base[si->names_ofs];
1299 memset( e2, 0, sizeof *e2 );
1300 if (HIWORD( names->id ))
1305 namedir->NumberOfNamedEntries++;
1306 e2->u1.s1.NameIsString = 1;
1307 e2->u1.s1.NameOffset = si->strings_ofs;
1309 strings = (WCHAR*) &base[si->strings_ofs];
1310 len = lstrlenW( names->id );
1312 memcpy( &strings[1], names->id, len * sizeof (WCHAR) );
1313 si->strings_ofs += (len + 1) * sizeof (WCHAR);
1317 namedir->NumberOfIdEntries++;
1318 e2->u1.s2.Id = LOWORD( names->id );
1320 e2->u2.s3.OffsetToDirectory = si->langs_ofs;
1321 e2->u2.s3.DataIsDirectory = TRUE;
1322 si->names_ofs += sizeof (IMAGE_RESOURCE_DIRECTORY_ENTRY);
1324 langdir = (IMAGE_RESOURCE_DIRECTORY*) &base[si->langs_ofs];
1325 memset( langdir, 0, sizeof *langdir );
1326 langdir->MajorVersion = 4;
1327 si->langs_ofs += sizeof (IMAGE_RESOURCE_DIRECTORY);
1329 LIST_FOR_EACH_ENTRY( data, &names->children, struct resource_data, entry )
1331 IMAGE_RESOURCE_DIRECTORY_ENTRY *e3;
1332 IMAGE_RESOURCE_DATA_ENTRY *de;
1335 e3 = (IMAGE_RESOURCE_DIRECTORY_ENTRY*) &base[si->langs_ofs];
1336 memset( e3, 0, sizeof *e3 );
1337 langdir->NumberOfIdEntries++;
1338 e3->u1.s2.Id = LOWORD( data->lang );
1339 e3->u2.OffsetToData = si->data_entry_ofs;
1341 si->langs_ofs += sizeof (IMAGE_RESOURCE_DIRECTORY_ENTRY);
1343 /* write out all the data entries */
1344 de = (IMAGE_RESOURCE_DATA_ENTRY*) &base[si->data_entry_ofs];
1345 memset( de, 0, sizeof *de );
1346 de->OffsetToData = si->data_ofs + rva;
1347 de->Size = data->cbData;
1348 de->CodePage = data->codepage;
1349 si->data_entry_ofs += sizeof (IMAGE_RESOURCE_DATA_ENTRY);
1351 /* write out the resource data */
1352 memcpy( &base[si->data_ofs], data->lpData, data->cbData );
1353 si->data_ofs += data->cbData;
1355 pad_size = (-si->data_ofs)&3;
1356 res_write_padding( &base[si->data_ofs], pad_size );
1357 si->data_ofs += pad_size;
1367 * Assumes that the resources are in .rsrc
1368 * and .rsrc is the last section in the file.
1369 * Not sure whether updating resources will other cases on Windows.
1370 * If the resources lie in a section containing other data,
1371 * resizing that section could possibly cause trouble.
1372 * If the section with the resources isn't last, the remaining
1373 * sections need to be moved down in the file, and the section header
1374 * would need to be adjusted.
1375 * If we needed to add a section, what would we name it?
1376 * If we needed to add a section and there wasn't space in the file
1377 * header, how would that work?
1378 * Seems that at least some of these cases can't be handled properly.
1380 static IMAGE_SECTION_HEADER *get_resource_section( void *base, DWORD mapping_size )
1382 IMAGE_SECTION_HEADER *sec;
1383 IMAGE_NT_HEADERS *nt;
1384 DWORD i, num_sections = 0;
1386 nt = get_nt_header( base, mapping_size );
1390 sec = get_section_header( base, mapping_size, &num_sections );
1394 /* find the resources section */
1395 for (i=0; i<num_sections; i++)
1396 if (!memcmp(sec[i].Name, ".rsrc", 6))
1399 if (i == num_sections)
1401 FIXME(".rsrc doesn't exist\n");
1405 /* check that the resources section is last */
1406 if (i != num_sections - 1)
1408 FIXME(".rsrc isn't the last section\n");
1415 static DWORD get_init_data_size( void *base, DWORD mapping_size )
1417 DWORD i, sz = 0, num_sections = 0;
1418 IMAGE_SECTION_HEADER *s;
1420 s = get_section_header( base, mapping_size, &num_sections );
1422 for (i=0; i<num_sections; i++)
1423 if (s[i].Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA)
1424 sz += s[i].SizeOfRawData;
1426 TRACE("size = %08x\n", sz);
1431 static BOOL write_raw_resources( QUEUEDUPDATES *updates )
1433 static const WCHAR prefix[] = { 'r','e','s','u',0 };
1434 WCHAR tempdir[MAX_PATH], tempfile[MAX_PATH];
1435 DWORD mapping_size, section_size, old_size;
1437 IMAGE_SECTION_HEADER *sec;
1438 IMAGE_NT_HEADERS *nt;
1439 struct resource_size_info res_size;
1441 struct mapping_info *read_map = NULL, *write_map = NULL;
1443 /* copy the exe to a temp file then update the temp file... */
1445 if (!GetTempPathW( MAX_PATH, tempdir ))
1448 if (!GetTempFileNameW( tempdir, prefix, 0, tempfile ))
1451 if (!CopyFileW( updates->pFileName, tempfile, FALSE ))
1454 TRACE("tempfile %s\n", debugstr_w(tempfile));
1456 if (!updates->bDeleteExistingResources)
1458 read_map = create_mapping( updates->pFileName, FALSE );
1462 ret = read_mapped_resources( updates, read_map->base, read_map->size );
1465 ERR("failed to read existing resources\n");
1470 write_map = create_mapping( tempfile, TRUE );
1474 nt = get_nt_header( write_map->base, write_map->size );
1478 if (nt->OptionalHeader.SectionAlignment <= 0)
1480 ERR("invalid section alignment %04x\n", nt->OptionalHeader.SectionAlignment);
1484 sec = get_resource_section( write_map->base, write_map->size );
1488 if ((sec->SizeOfRawData + sec->PointerToRawData) != write_map->size)
1490 FIXME(".rsrc isn't at the end of the image %08x + %08x != %08x\n",
1491 sec->SizeOfRawData, sec->PointerToRawData, write_map->size);
1495 TRACE("before .rsrc at %08x, size %08x\n", sec->PointerToRawData, sec->SizeOfRawData);
1497 get_resource_sizes( updates, &res_size );
1499 /* round up the section size */
1500 section_size = res_size.total_size;
1501 section_size += (-section_size) % nt->OptionalHeader.SectionAlignment;
1503 mapping_size = sec->PointerToRawData + section_size;
1505 TRACE("requires %08x (%08x) bytes\n", res_size.total_size, section_size );
1507 /* check if the file size needs to be changed */
1508 if (section_size != sec->SizeOfRawData)
1510 old_size = write_map->size;
1512 TRACE("file size %08x -> %08x\n", old_size, mapping_size);
1514 /* unmap the file before changing the file size */
1515 ret = resize_mapping( write_map, mapping_size );
1517 /* get the pointers again - they might be different after remapping */
1518 nt = get_nt_header( write_map->base, mapping_size );
1521 ERR("couldn't get NT header\n");
1525 sec = get_resource_section( write_map->base, mapping_size );
1529 /* adjust the PE header information */
1530 nt->OptionalHeader.SizeOfImage += (mapping_size - old_size);
1531 sec->SizeOfRawData = section_size;
1532 sec->Misc.VirtualSize = section_size;
1533 nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].Size = res_size.total_size;
1534 nt->OptionalHeader.SizeOfInitializedData = get_init_data_size( write_map->base, mapping_size );
1537 res_base = (LPBYTE) write_map->base + sec->PointerToRawData;
1539 TRACE("base = %p offset = %08x\n", write_map->base, sec->PointerToRawData);
1541 ret = write_resources( updates, res_base, &res_size, sec->VirtualAddress );
1543 res_write_padding( res_base + res_size.total_size, section_size - res_size.total_size );
1545 TRACE("after .rsrc at %08x, size %08x\n", sec->PointerToRawData, sec->SizeOfRawData);
1548 destroy_mapping( read_map );
1549 destroy_mapping( write_map );
1552 ret = CopyFileW( tempfile, updates->pFileName, FALSE );
1554 DeleteFileW( tempfile );
1559 /***********************************************************************
1560 * BeginUpdateResourceW (KERNEL32.@)
1562 HANDLE WINAPI BeginUpdateResourceW( LPCWSTR pFileName, BOOL bDeleteExistingResources )
1564 QUEUEDUPDATES *updates = NULL;
1565 HANDLE hUpdate, file, ret = NULL;
1567 TRACE("%s, %d\n", debugstr_w(pFileName), bDeleteExistingResources);
1569 hUpdate = GlobalAlloc(GHND, sizeof(QUEUEDUPDATES));
1573 updates = GlobalLock(hUpdate);
1576 list_init( &updates->root );
1577 updates->bDeleteExistingResources = bDeleteExistingResources;
1578 updates->pFileName = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(pFileName)+1)*sizeof(WCHAR));
1579 if (updates->pFileName)
1581 lstrcpyW(updates->pFileName, pFileName);
1583 file = CreateFileW( pFileName, GENERIC_READ | GENERIC_WRITE,
1584 0, NULL, OPEN_EXISTING, 0, 0 );
1586 /* if resources are deleted, only the file's presence is checked */
1587 if (file != INVALID_HANDLE_VALUE &&
1588 (bDeleteExistingResources || check_pe_exe( file, updates )))
1591 HeapFree( GetProcessHeap(), 0, updates->pFileName );
1593 CloseHandle( file );
1595 GlobalUnlock(hUpdate);
1599 GlobalFree(hUpdate);
1605 /***********************************************************************
1606 * BeginUpdateResourceA (KERNEL32.@)
1608 HANDLE WINAPI BeginUpdateResourceA( LPCSTR pFileName, BOOL bDeleteExistingResources )
1610 UNICODE_STRING FileNameW;
1612 RtlCreateUnicodeStringFromAsciiz(&FileNameW, pFileName);
1613 ret = BeginUpdateResourceW(FileNameW.Buffer, bDeleteExistingResources);
1614 RtlFreeUnicodeString(&FileNameW);
1619 /***********************************************************************
1620 * EndUpdateResourceW (KERNEL32.@)
1622 BOOL WINAPI EndUpdateResourceW( HANDLE hUpdate, BOOL fDiscard )
1624 QUEUEDUPDATES *updates;
1627 TRACE("%p %d\n", hUpdate, fDiscard);
1629 updates = GlobalLock(hUpdate);
1633 ret = fDiscard || write_raw_resources( updates );
1635 free_resource_directory( &updates->root, 2 );
1637 HeapFree( GetProcessHeap(), 0, updates->pFileName );
1638 GlobalUnlock( hUpdate );
1639 GlobalFree( hUpdate );
1645 /***********************************************************************
1646 * EndUpdateResourceA (KERNEL32.@)
1648 BOOL WINAPI EndUpdateResourceA( HANDLE hUpdate, BOOL fDiscard )
1650 return EndUpdateResourceW(hUpdate, fDiscard);
1654 /***********************************************************************
1655 * UpdateResourceW (KERNEL32.@)
1657 BOOL WINAPI UpdateResourceW( HANDLE hUpdate, LPCWSTR lpType, LPCWSTR lpName,
1658 WORD wLanguage, LPVOID lpData, DWORD cbData)
1660 QUEUEDUPDATES *updates;
1663 TRACE("%p %s %s %08x %p %d\n", hUpdate,
1664 debugstr_w(lpType), debugstr_w(lpName), wLanguage, lpData, cbData);
1666 updates = GlobalLock(hUpdate);
1669 struct resource_data *data;
1670 data = allocate_resource_data( wLanguage, 0, lpData, cbData, TRUE );
1672 ret = update_add_resource( updates, lpType, lpName, data, TRUE );
1673 GlobalUnlock(hUpdate);
1679 /***********************************************************************
1680 * UpdateResourceA (KERNEL32.@)
1682 BOOL WINAPI UpdateResourceA( HANDLE hUpdate, LPCSTR lpType, LPCSTR lpName,
1683 WORD wLanguage, LPVOID lpData, DWORD cbData)
1686 UNICODE_STRING TypeW;
1687 UNICODE_STRING NameW;
1689 TypeW.Buffer = (LPWSTR)lpType;
1691 RtlCreateUnicodeStringFromAsciiz(&TypeW, lpType);
1693 NameW.Buffer = (LPWSTR)lpName;
1695 RtlCreateUnicodeStringFromAsciiz(&NameW, lpName);
1696 ret = UpdateResourceW(hUpdate, TypeW.Buffer, NameW.Buffer, wLanguage, lpData, cbData);
1697 if(HIWORD(lpType)) RtlFreeUnicodeString(&TypeW);
1698 if(HIWORD(lpName)) RtlFreeUnicodeString(&NameW);