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
35 #include "wine/winbase16.h"
36 #include "wine/debug.h"
37 #include "wine/exception.h"
38 #include "wine/unicode.h"
39 #include "wine/list.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(resource);
43 /* handle conversions */
44 #define HRSRC_32(h16) ((HRSRC)(ULONG_PTR)(h16))
45 #define HRSRC_16(h32) (LOWORD(h32))
46 #define HGLOBAL_32(h16) ((HGLOBAL)(ULONG_PTR)(h16))
47 #define HGLOBAL_16(h32) (LOWORD(h32))
48 #define HMODULE_16(h32) (LOWORD(h32))
50 /* retrieve the resource name to pass to the ntdll functions */
51 static NTSTATUS get_res_nameA( LPCSTR name, UNICODE_STRING *str )
55 str->Buffer = (LPWSTR)name;
56 return STATUS_SUCCESS;
61 if (RtlCharToInteger( name + 1, 10, &value ) != STATUS_SUCCESS || HIWORD(value))
62 return STATUS_INVALID_PARAMETER;
63 str->Buffer = (LPWSTR)value;
64 return STATUS_SUCCESS;
66 RtlCreateUnicodeStringFromAsciiz( str, name );
67 RtlUpcaseUnicodeString( str, str, FALSE );
68 return STATUS_SUCCESS;
71 /* retrieve the resource name to pass to the ntdll functions */
72 static NTSTATUS get_res_nameW( LPCWSTR name, UNICODE_STRING *str )
76 str->Buffer = (LPWSTR)name;
77 return STATUS_SUCCESS;
82 RtlInitUnicodeString( str, name + 1 );
83 if (RtlUnicodeStringToInteger( str, 10, &value ) != STATUS_SUCCESS || HIWORD(value))
84 return STATUS_INVALID_PARAMETER;
85 str->Buffer = (LPWSTR)value;
86 return STATUS_SUCCESS;
88 RtlCreateUnicodeString( str, name );
89 RtlUpcaseUnicodeString( str, str, FALSE );
90 return STATUS_SUCCESS;
93 /* retrieve the resource names for the 16-bit FindResource function */
94 static BOOL get_res_name_type_WtoA( LPCWSTR name, LPCWSTR type, LPSTR *nameA, LPSTR *typeA )
96 *nameA = *typeA = NULL;
102 DWORD len = WideCharToMultiByte( CP_ACP, 0, name, -1, NULL, 0, NULL, NULL );
103 *nameA = HeapAlloc( GetProcessHeap(), 0, len );
104 if (*nameA) WideCharToMultiByte( CP_ACP, 0, name, -1, *nameA, len, NULL, NULL );
106 else *nameA = (LPSTR)name;
110 DWORD len = WideCharToMultiByte( CP_ACP, 0, type, -1, NULL, 0, NULL, NULL );
111 *typeA = HeapAlloc( GetProcessHeap(), 0, len );
112 if (*typeA) WideCharToMultiByte( CP_ACP, 0, type, -1, *typeA, len, NULL, NULL );
114 else *typeA = (LPSTR)type;
118 if (HIWORD(*nameA)) HeapFree( GetProcessHeap(), 0, *nameA );
119 if (HIWORD(*typeA)) HeapFree( GetProcessHeap(), 0, *typeA );
120 SetLastError( ERROR_INVALID_PARAMETER );
127 /* implementation of FindResourceExA */
128 static HRSRC find_resourceA( HMODULE hModule, LPCSTR type, LPCSTR name, WORD lang )
131 UNICODE_STRING nameW, typeW;
132 LDR_RESOURCE_INFO info;
133 const IMAGE_RESOURCE_DATA_ENTRY *entry = NULL;
137 if ((status = get_res_nameA( name, &nameW )) != STATUS_SUCCESS) goto done;
138 if ((status = get_res_nameA( type, &typeW )) != STATUS_SUCCESS) goto done;
139 info.Type = (ULONG_PTR)typeW.Buffer;
140 info.Name = (ULONG_PTR)nameW.Buffer;
141 info.Language = lang;
142 status = LdrFindResource_U( hModule, &info, 3, &entry );
144 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
148 SetLastError( ERROR_INVALID_PARAMETER );
152 if (HIWORD(nameW.Buffer)) HeapFree( GetProcessHeap(), 0, nameW.Buffer );
153 if (HIWORD(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
158 /* implementation of FindResourceExW */
159 static HRSRC find_resourceW( HMODULE hModule, LPCWSTR type, LPCWSTR name, WORD lang )
162 UNICODE_STRING nameW, typeW;
163 LDR_RESOURCE_INFO info;
164 const IMAGE_RESOURCE_DATA_ENTRY *entry = NULL;
166 nameW.Buffer = typeW.Buffer = NULL;
170 if ((status = get_res_nameW( name, &nameW )) != STATUS_SUCCESS) goto done;
171 if ((status = get_res_nameW( type, &typeW )) != STATUS_SUCCESS) goto done;
172 info.Type = (ULONG_PTR)typeW.Buffer;
173 info.Name = (ULONG_PTR)nameW.Buffer;
174 info.Language = lang;
175 status = LdrFindResource_U( hModule, &info, 3, &entry );
177 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
181 SetLastError( ERROR_INVALID_PARAMETER );
185 if (HIWORD(nameW.Buffer)) HeapFree( GetProcessHeap(), 0, nameW.Buffer );
186 if (HIWORD(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
190 /**********************************************************************
191 * FindResourceExA (KERNEL32.@)
193 HRSRC WINAPI FindResourceExA( HMODULE hModule, LPCSTR type, LPCSTR name, WORD lang )
195 TRACE( "%p %s %s %04x\n", hModule, debugstr_a(type), debugstr_a(name), lang );
197 if (!hModule) hModule = GetModuleHandleW(0);
198 else if (!HIWORD(hModule))
200 return HRSRC_32( FindResource16( HMODULE_16(hModule), name, type ) );
202 return find_resourceA( hModule, type, name, lang );
206 /**********************************************************************
207 * FindResourceA (KERNEL32.@)
209 HRSRC WINAPI FindResourceA( HMODULE hModule, LPCSTR name, LPCSTR type )
211 return FindResourceExA( hModule, type, name, MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ) );
215 /**********************************************************************
216 * FindResourceExW (KERNEL32.@)
218 HRSRC WINAPI FindResourceExW( HMODULE hModule, LPCWSTR type, LPCWSTR name, WORD lang )
220 TRACE( "%p %s %s %04x\n", hModule, debugstr_w(type), debugstr_w(name), lang );
222 if (!hModule) hModule = GetModuleHandleW(0);
223 else if (!HIWORD(hModule))
228 if (!get_res_name_type_WtoA( name, type, &nameA, &typeA )) return NULL;
230 ret = FindResource16( HMODULE_16(hModule), nameA, typeA );
231 if (HIWORD(nameA)) HeapFree( GetProcessHeap(), 0, nameA );
232 if (HIWORD(typeA)) HeapFree( GetProcessHeap(), 0, typeA );
233 return HRSRC_32(ret);
236 return find_resourceW( hModule, type, name, lang );
240 /**********************************************************************
241 * FindResourceW (KERNEL32.@)
243 HRSRC WINAPI FindResourceW( HINSTANCE hModule, LPCWSTR name, LPCWSTR type )
245 return FindResourceExW( hModule, type, name, MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ) );
249 /**********************************************************************
250 * EnumResourceTypesA (KERNEL32.@)
252 BOOL WINAPI EnumResourceTypesA( HMODULE hmod, ENUMRESTYPEPROCA lpfun, LONG_PTR lparam )
257 DWORD len = 0, newlen;
259 const IMAGE_RESOURCE_DIRECTORY *resdir;
260 const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
261 const IMAGE_RESOURCE_DIR_STRING_U *str;
263 TRACE( "%p %p %lx\n", hmod, lpfun, lparam );
265 if (!hmod) hmod = GetModuleHandleA( NULL );
267 if ((status = LdrFindResourceDirectory_U( hmod, NULL, 0, &resdir )) != STATUS_SUCCESS)
269 SetLastError( RtlNtStatusToDosError(status) );
272 et = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resdir + 1);
273 for (i = 0; i < resdir->NumberOfNamedEntries+resdir->NumberOfIdEntries; i++)
275 if (et[i].u1.s1.NameIsString)
277 str = (const IMAGE_RESOURCE_DIR_STRING_U *)((const BYTE *)resdir + et[i].u1.s1.NameOffset);
278 newlen = WideCharToMultiByte( CP_ACP, 0, str->NameString, str->Length, NULL, 0, NULL, NULL);
279 if (newlen + 1 > len)
282 HeapFree( GetProcessHeap(), 0, type );
283 if (!(type = HeapAlloc( GetProcessHeap(), 0, len ))) return FALSE;
285 WideCharToMultiByte( CP_ACP, 0, str->NameString, str->Length, type, len, NULL, NULL);
287 ret = lpfun(hmod,type,lparam);
291 ret = lpfun( hmod, (LPSTR)(int)et[i].u1.s2.Id, lparam );
295 HeapFree( GetProcessHeap(), 0, type );
300 /**********************************************************************
301 * EnumResourceTypesW (KERNEL32.@)
303 BOOL WINAPI EnumResourceTypesW( HMODULE hmod, ENUMRESTYPEPROCW lpfun, LONG_PTR lparam )
309 const IMAGE_RESOURCE_DIRECTORY *resdir;
310 const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
311 const IMAGE_RESOURCE_DIR_STRING_U *str;
313 TRACE( "%p %p %lx\n", hmod, lpfun, lparam );
315 if (!hmod) hmod = GetModuleHandleW( NULL );
317 if ((status = LdrFindResourceDirectory_U( hmod, NULL, 0, &resdir )) != STATUS_SUCCESS)
319 SetLastError( RtlNtStatusToDosError(status) );
322 et = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resdir + 1);
323 for (i = 0; i < resdir->NumberOfNamedEntries + resdir->NumberOfIdEntries; i++)
325 if (et[i].u1.s1.NameIsString)
327 str = (const IMAGE_RESOURCE_DIR_STRING_U *)((const BYTE *)resdir + et[i].u1.s1.NameOffset);
328 if (str->Length + 1 > len)
330 len = str->Length + 1;
331 HeapFree( GetProcessHeap(), 0, type );
332 if (!(type = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return FALSE;
334 memcpy(type, str->NameString, str->Length * sizeof (WCHAR));
335 type[str->Length] = 0;
336 ret = lpfun(hmod,type,lparam);
340 ret = lpfun( hmod, (LPWSTR)(int)et[i].u1.s2.Id, lparam );
344 HeapFree( GetProcessHeap(), 0, type );
349 /**********************************************************************
350 * EnumResourceNamesA (KERNEL32.@)
352 BOOL WINAPI EnumResourceNamesA( HMODULE hmod, LPCSTR type, ENUMRESNAMEPROCA lpfun, LONG_PTR lparam )
356 DWORD len = 0, newlen;
359 UNICODE_STRING typeW;
360 LDR_RESOURCE_INFO info;
361 const IMAGE_RESOURCE_DIRECTORY *basedir, *resdir;
362 const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
363 const IMAGE_RESOURCE_DIR_STRING_U *str;
365 TRACE( "%p %s %p %lx\n", hmod, debugstr_a(type), lpfun, lparam );
367 if (!hmod) hmod = GetModuleHandleA( NULL );
369 if ((status = LdrFindResourceDirectory_U( hmod, NULL, 0, &basedir )) != STATUS_SUCCESS)
371 if ((status = get_res_nameA( type, &typeW )) != STATUS_SUCCESS)
373 info.Type = (ULONG)typeW.Buffer;
374 if ((status = LdrFindResourceDirectory_U( hmod, &info, 1, &resdir )) != STATUS_SUCCESS)
377 et = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resdir + 1);
378 for (i = 0; i < resdir->NumberOfNamedEntries+resdir->NumberOfIdEntries; i++)
380 if (et[i].u1.s1.NameIsString)
382 str = (const IMAGE_RESOURCE_DIR_STRING_U *)((const BYTE *)basedir + et[i].u1.s1.NameOffset);
383 newlen = WideCharToMultiByte(CP_ACP, 0, str->NameString, str->Length, NULL, 0, NULL, NULL);
384 if (newlen + 1 > len)
387 HeapFree( GetProcessHeap(), 0, name );
388 if (!(name = HeapAlloc(GetProcessHeap(), 0, len + 1 )))
394 WideCharToMultiByte( CP_ACP, 0, str->NameString, str->Length, name, len, NULL, NULL );
396 ret = lpfun(hmod,type,name,lparam);
400 ret = lpfun( hmod, type, (LPSTR)(int)et[i].u1.s2.Id, lparam );
405 HeapFree( GetProcessHeap(), 0, name );
406 if (HIWORD(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
407 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
412 /**********************************************************************
413 * EnumResourceNamesW (KERNEL32.@)
415 BOOL WINAPI EnumResourceNamesW( HMODULE hmod, LPCWSTR type, ENUMRESNAMEPROCW lpfun, LONG_PTR lparam )
421 UNICODE_STRING typeW;
422 LDR_RESOURCE_INFO info;
423 const IMAGE_RESOURCE_DIRECTORY *basedir, *resdir;
424 const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
425 const IMAGE_RESOURCE_DIR_STRING_U *str;
427 TRACE( "%p %s %p %lx\n", hmod, debugstr_w(type), lpfun, lparam );
429 if (!hmod) hmod = GetModuleHandleW( NULL );
431 if ((status = LdrFindResourceDirectory_U( hmod, NULL, 0, &basedir )) != STATUS_SUCCESS)
433 if ((status = get_res_nameW( type, &typeW )) != STATUS_SUCCESS)
435 info.Type = (ULONG)typeW.Buffer;
436 if ((status = LdrFindResourceDirectory_U( hmod, &info, 1, &resdir )) != STATUS_SUCCESS)
439 et = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resdir + 1);
440 for (i = 0; i < resdir->NumberOfNamedEntries+resdir->NumberOfIdEntries; i++)
442 if (et[i].u1.s1.NameIsString)
444 str = (const IMAGE_RESOURCE_DIR_STRING_U *)((const BYTE *)basedir + et[i].u1.s1.NameOffset);
445 if (str->Length + 1 > len)
447 len = str->Length + 1;
448 HeapFree( GetProcessHeap(), 0, name );
449 if (!(name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
455 memcpy(name, str->NameString, str->Length * sizeof (WCHAR));
456 name[str->Length] = 0;
457 ret = lpfun(hmod,type,name,lparam);
461 ret = lpfun( hmod, type, (LPWSTR)(int)et[i].u1.s2.Id, lparam );
466 HeapFree( GetProcessHeap(), 0, name );
467 if (HIWORD(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
468 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
473 /**********************************************************************
474 * EnumResourceLanguagesA (KERNEL32.@)
476 BOOL WINAPI EnumResourceLanguagesA( HMODULE hmod, LPCSTR type, LPCSTR name,
477 ENUMRESLANGPROCA lpfun, LONG_PTR lparam )
482 UNICODE_STRING typeW, nameW;
483 LDR_RESOURCE_INFO info;
484 const IMAGE_RESOURCE_DIRECTORY *basedir, *resdir;
485 const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
487 TRACE( "%p %s %s %p %lx\n", hmod, debugstr_a(type), debugstr_a(name), lpfun, lparam );
489 if (!hmod) hmod = GetModuleHandleA( NULL );
490 typeW.Buffer = nameW.Buffer = NULL;
491 if ((status = LdrFindResourceDirectory_U( hmod, NULL, 0, &basedir )) != STATUS_SUCCESS)
493 if ((status = get_res_nameA( type, &typeW )) != STATUS_SUCCESS)
495 if ((status = get_res_nameA( name, &nameW )) != STATUS_SUCCESS)
497 info.Type = (ULONG_PTR)typeW.Buffer;
498 info.Name = (ULONG_PTR)nameW.Buffer;
499 if ((status = LdrFindResourceDirectory_U( hmod, &info, 2, &resdir )) != STATUS_SUCCESS)
502 et = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resdir + 1);
503 for (i = 0; i < resdir->NumberOfNamedEntries + resdir->NumberOfIdEntries; i++)
505 ret = lpfun( hmod, type, name, et[i].u1.s2.Id, lparam );
509 if (HIWORD(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
510 if (HIWORD(nameW.Buffer)) HeapFree( GetProcessHeap(), 0, nameW.Buffer );
511 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
516 /**********************************************************************
517 * EnumResourceLanguagesW (KERNEL32.@)
519 BOOL WINAPI EnumResourceLanguagesW( HMODULE hmod, LPCWSTR type, LPCWSTR name,
520 ENUMRESLANGPROCW lpfun, LONG_PTR lparam )
525 UNICODE_STRING typeW, nameW;
526 LDR_RESOURCE_INFO info;
527 const IMAGE_RESOURCE_DIRECTORY *basedir, *resdir;
528 const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
530 TRACE( "%p %s %s %p %lx\n", hmod, debugstr_w(type), debugstr_w(name), lpfun, lparam );
532 if (!hmod) hmod = GetModuleHandleW( NULL );
533 typeW.Buffer = nameW.Buffer = NULL;
534 if ((status = LdrFindResourceDirectory_U( hmod, NULL, 0, &basedir )) != STATUS_SUCCESS)
536 if ((status = get_res_nameW( type, &typeW )) != STATUS_SUCCESS)
538 if ((status = get_res_nameW( name, &nameW )) != STATUS_SUCCESS)
540 info.Type = (ULONG_PTR)typeW.Buffer;
541 info.Name = (ULONG_PTR)nameW.Buffer;
542 if ((status = LdrFindResourceDirectory_U( hmod, &info, 2, &resdir )) != STATUS_SUCCESS)
545 et = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resdir + 1);
546 for (i = 0; i < resdir->NumberOfNamedEntries + resdir->NumberOfIdEntries; i++)
548 ret = lpfun( hmod, type, name, et[i].u1.s2.Id, lparam );
552 if (HIWORD(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
553 if (HIWORD(nameW.Buffer)) HeapFree( GetProcessHeap(), 0, nameW.Buffer );
554 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
559 /**********************************************************************
560 * LoadResource (KERNEL32.@)
562 HGLOBAL WINAPI LoadResource( HINSTANCE hModule, HRSRC hRsrc )
567 TRACE( "%p %p\n", hModule, hRsrc );
569 if (hModule && !HIWORD(hModule))
570 /* FIXME: should convert return to 32-bit resource */
571 return HGLOBAL_32( LoadResource16( HMODULE_16(hModule), HRSRC_16(hRsrc) ) );
573 if (!hRsrc) return 0;
574 if (!hModule) hModule = GetModuleHandleA( NULL );
575 status = LdrAccessResource( hModule, (IMAGE_RESOURCE_DATA_ENTRY *)hRsrc, &ret, NULL );
576 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
581 /**********************************************************************
582 * LockResource (KERNEL32.@)
584 LPVOID WINAPI LockResource( HGLOBAL handle )
586 TRACE("(%p)\n", handle );
588 if (HIWORD( handle )) /* 32-bit memory handle */
589 return (LPVOID)handle;
591 /* 16-bit memory handle */
592 return LockResource16( HGLOBAL_16(handle) );
596 /**********************************************************************
597 * FreeResource (KERNEL32.@)
599 BOOL WINAPI FreeResource( HGLOBAL handle )
601 if (HIWORD(handle)) return 0; /* 32-bit memory handle: nothing to do */
602 return FreeResource16( HGLOBAL_16(handle) );
606 /**********************************************************************
607 * SizeofResource (KERNEL32.@)
609 DWORD WINAPI SizeofResource( HINSTANCE hModule, HRSRC hRsrc )
611 if (hModule && !HIWORD(hModule))
612 return SizeofResource16( HMODULE_16(hModule), HRSRC_16(hRsrc) );
614 if (!hRsrc) return 0;
615 return ((PIMAGE_RESOURCE_DATA_ENTRY)hRsrc)->Size;
619 * Data structure for updating resources.
620 * Type/Name/Language is a keyset for accessing resource data.
622 * QUEUEDUPDATES (root) ->
623 * list of struct resource_dir_entry (Type) ->
624 * list of struct resource_dir_entry (Name) ->
625 * list of struct resource_data Language + Data
631 BOOL bDeleteExistingResources;
635 /* this structure is shared for types and names */
636 struct resource_dir_entry {
639 struct list children;
642 /* this structure is the leaf */
643 struct resource_data {
651 static int resource_strcmp( LPCWSTR a, LPCWSTR b )
655 if (HIWORD( a ) && HIWORD( b ) )
656 return lstrcmpW( a, b );
657 /* strings come before ids */
658 if (HIWORD( a ) && !HIWORD( b ))
660 if (HIWORD( b ) && !HIWORD( a ))
662 return ( a < b ) ? -1 : 1;
665 static struct resource_dir_entry *find_resource_dir_entry( struct list *dir, LPCWSTR id )
667 struct resource_dir_entry *ent;
669 /* match either IDs or strings */
670 LIST_FOR_EACH_ENTRY( ent, dir, struct resource_dir_entry, entry )
671 if (!resource_strcmp( id, ent->id ))
677 static struct resource_data *find_resource_data( struct list *dir, LANGID lang )
679 struct resource_data *res_data;
681 /* match only languages here */
682 LIST_FOR_EACH_ENTRY( res_data, dir, struct resource_data, entry )
683 if ( lang == res_data->lang )
689 static void add_resource_dir_entry( struct list *dir, struct resource_dir_entry *resdir )
691 struct resource_dir_entry *ent;
693 LIST_FOR_EACH_ENTRY( ent, dir, struct resource_dir_entry, entry )
695 if (0>resource_strcmp( ent->id, resdir->id ))
698 list_add_before( &ent->entry, &resdir->entry );
701 list_add_tail( dir, &resdir->entry );
704 static void add_resource_data_entry( struct list *dir, struct resource_data *resdata )
706 struct resource_data *ent;
708 LIST_FOR_EACH_ENTRY( ent, dir, struct resource_data, entry )
710 if (ent->lang < resdata->lang)
713 list_add_before( &ent->entry, &resdata->entry );
716 list_add_tail( dir, &resdata->entry );
719 static LPWSTR res_strdupW( LPCWSTR str )
724 if (HIWORD(str) == 0)
725 return (LPWSTR) (UINT_PTR) LOWORD(str);
726 len = (lstrlenW( str ) + 1) * sizeof (WCHAR);
727 ret = HeapAlloc( GetProcessHeap(), 0, len );
728 memcpy( ret, str, len );
732 static void res_free_str( LPWSTR str )
735 HeapFree( GetProcessHeap(), 0, str );
738 static BOOL update_add_resource( QUEUEDUPDATES *updates, LPCWSTR Type, LPCWSTR Name,
739 struct resource_data *resdata, BOOL overwrite_existing )
741 struct resource_dir_entry *restype, *resname;
742 struct resource_data *existing;
744 TRACE("%p %s %s %p %d\n", updates,
745 debugstr_w(Type), debugstr_w(Name), resdata, overwrite_existing );
747 restype = find_resource_dir_entry( &updates->root, Type );
750 restype = HeapAlloc( GetProcessHeap(), 0, sizeof *restype );
751 restype->id = res_strdupW( Type );
752 list_init( &restype->children );
753 add_resource_dir_entry( &updates->root, restype );
756 resname = find_resource_dir_entry( &restype->children, Name );
759 resname = HeapAlloc( GetProcessHeap(), 0, sizeof *resname );
760 resname->id = res_strdupW( Name );
761 list_init( &resname->children );
762 add_resource_dir_entry( &restype->children, resname );
766 * If there's an existing resource entry with matching (Type,Name,Language)
767 * it needs to be removed before adding the new data.
769 existing = find_resource_data( &resname->children, resdata->lang );
772 if (!overwrite_existing)
774 list_remove( &existing->entry );
775 HeapFree( GetProcessHeap(), 0, existing );
778 add_resource_data_entry( &resname->children, resdata );
783 static struct resource_data *allocate_resource_data( WORD Language, DWORD codepage,
784 LPVOID lpData, DWORD cbData, BOOL copy_data )
786 struct resource_data *resdata;
788 if (!lpData || !cbData)
791 resdata = HeapAlloc( GetProcessHeap(), 0, sizeof *resdata + (copy_data ? cbData : 0) );
794 resdata->lang = Language;
795 resdata->codepage = codepage;
796 resdata->cbData = cbData;
799 resdata->lpData = &resdata[1];
800 memcpy( resdata->lpData, lpData, cbData );
803 resdata->lpData = lpData;
809 static void free_resource_directory( struct list *head, int level )
811 struct list *ptr = NULL;
813 while ((ptr = list_head( head )))
818 struct resource_dir_entry *ent;
820 ent = LIST_ENTRY( ptr, struct resource_dir_entry, entry );
821 res_free_str( ent->id );
822 free_resource_directory( &ent->children, level - 1 );
823 HeapFree(GetProcessHeap(), 0, ent);
827 struct resource_data *data;
829 data = LIST_ENTRY( ptr, struct resource_data, entry );
830 HeapFree( GetProcessHeap(), 0, data );
835 static IMAGE_NT_HEADERS *get_nt_header( void *base, DWORD mapping_size )
837 IMAGE_NT_HEADERS *nt;
838 IMAGE_DOS_HEADER *dos;
840 if (mapping_size<sizeof (*dos))
844 if (dos->e_magic != IMAGE_DOS_SIGNATURE)
847 if ((dos->e_lfanew + sizeof (*nt)) > mapping_size)
850 nt = (void*) ((BYTE*)base + dos->e_lfanew);
852 if (nt->Signature != IMAGE_NT_SIGNATURE)
858 static IMAGE_SECTION_HEADER *get_section_header( void *base, DWORD mapping_size, DWORD *num_sections )
860 IMAGE_NT_HEADERS *nt;
861 IMAGE_SECTION_HEADER *sec;
864 nt = get_nt_header( base, mapping_size );
868 /* check that we don't go over the end of the file accessing the sections */
869 section_ofs = FIELD_OFFSET(IMAGE_NT_HEADERS, OptionalHeader) + nt->FileHeader.SizeOfOptionalHeader;
870 if ((nt->FileHeader.NumberOfSections * sizeof (*sec) + section_ofs) > mapping_size)
874 *num_sections = nt->FileHeader.NumberOfSections;
876 /* from here we have a valid PE exe to update */
877 return (void*) ((BYTE*)nt + section_ofs);
880 static BOOL check_pe_exe( HANDLE file, QUEUEDUPDATES *updates )
882 const IMAGE_NT_HEADERS *nt;
883 const IMAGE_SECTION_HEADER *sec;
886 DWORD mapping_size, num_sections = 0;
889 mapping_size = GetFileSize( file, NULL );
891 mapping = CreateFileMappingW( file, NULL, PAGE_READONLY, 0, 0, NULL );
895 base = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, mapping_size );
899 nt = get_nt_header( base, mapping_size );
903 TRACE("resources: %08x %08x\n",
904 nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress,
905 nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].Size);
907 sec = get_section_header( base, mapping_size, &num_sections );
915 UnmapViewOfFile( base );
917 CloseHandle( mapping );
922 struct resource_size_info {
926 DWORD data_entry_ofs;
932 struct mapping_info {
940 static const IMAGE_SECTION_HEADER *section_from_rva( void *base, DWORD mapping_size, DWORD rva )
942 const IMAGE_SECTION_HEADER *sec;
943 DWORD num_sections = 0;
946 sec = get_section_header( base, mapping_size, &num_sections );
950 for (i=num_sections-1; i>=0; i--)
952 if (sec[i].VirtualAddress <= rva &&
953 rva <= (DWORD)sec[i].VirtualAddress + sec[i].SizeOfRawData)
962 static void *address_from_rva( void *base, DWORD mapping_size, DWORD rva, DWORD len )
964 const IMAGE_SECTION_HEADER *sec;
966 sec = section_from_rva( base, mapping_size, rva );
970 if (rva + len <= (DWORD)sec->VirtualAddress + sec->SizeOfRawData)
971 return (void*)((const BYTE*) base + (sec->PointerToRawData + rva - sec->VirtualAddress));
976 static LPWSTR resource_dup_string( const IMAGE_RESOURCE_DIRECTORY *root, const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry )
978 const IMAGE_RESOURCE_DIR_STRING_U* string;
981 if (!entry->u1.s1.NameIsString)
982 return (LPWSTR) (DWORD) entry->u1.s2.Id;
984 string = (const IMAGE_RESOURCE_DIR_STRING_U*) (((const char *)root) + entry->u1.s1.NameOffset);
985 s = HeapAlloc(GetProcessHeap(), 0, (string->Length + 1)*sizeof (WCHAR) );
986 memcpy( s, string->NameString, (string->Length + 1)*sizeof (WCHAR) );
987 s[string->Length] = 0;
992 /* this function is based on the code in winedump's pe.c */
993 static BOOL enumerate_mapped_resources( QUEUEDUPDATES *updates,
994 void *base, DWORD mapping_size,
995 const IMAGE_RESOURCE_DIRECTORY *root )
997 const IMAGE_RESOURCE_DIRECTORY *namedir, *langdir;
998 const IMAGE_RESOURCE_DIRECTORY_ENTRY *e1, *e2, *e3;
999 const IMAGE_RESOURCE_DATA_ENTRY *data;
1002 TRACE("version (%d.%d) %d named %d id entries\n",
1003 root->MajorVersion, root->MinorVersion, root->NumberOfNamedEntries, root->NumberOfIdEntries);
1005 for (i = 0; i< root->NumberOfNamedEntries + root->NumberOfIdEntries; i++)
1009 e1 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(root + 1) + i;
1011 Type = resource_dup_string( root, e1 );
1013 namedir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e1->u2.s3.OffsetToDirectory);
1014 for (j = 0; j < namedir->NumberOfNamedEntries + namedir->NumberOfIdEntries; j++)
1018 e2 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(namedir + 1) + j;
1020 Name = resource_dup_string( root, e2 );
1022 langdir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e2->u2.s3.OffsetToDirectory);
1023 for (k = 0; k < langdir->NumberOfNamedEntries + langdir->NumberOfIdEntries; k++)
1027 struct resource_data *resdata;
1029 e3 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(langdir + 1) + k;
1031 Lang = e3->u1.s2.Id;
1033 data = (const IMAGE_RESOURCE_DATA_ENTRY *)((const char *)root + e3->u2.OffsetToData);
1035 p = address_from_rva( base, mapping_size, data->OffsetToData, data->Size );
1037 resdata = allocate_resource_data( Lang, data->CodePage, p, data->Size, FALSE );
1039 update_add_resource( updates, Type, Name, resdata, FALSE );
1041 res_free_str( Name );
1043 res_free_str( Type );
1049 static BOOL read_mapped_resources( QUEUEDUPDATES *updates, void *base, DWORD mapping_size )
1051 const IMAGE_RESOURCE_DIRECTORY *root;
1052 const IMAGE_NT_HEADERS *nt;
1053 const IMAGE_SECTION_HEADER *sec;
1054 DWORD num_sections = 0, i;
1056 nt = get_nt_header( base, mapping_size );
1060 sec = get_section_header( base, mapping_size, &num_sections );
1064 for (i=0; i<num_sections; i++)
1065 if (!memcmp(sec[i].Name, ".rsrc", 6))
1068 if (i == num_sections)
1071 /* check the resource data is inside the mapping */
1072 if (sec[i].PointerToRawData > mapping_size ||
1073 (sec[i].PointerToRawData + sec[i].SizeOfRawData) > mapping_size)
1076 TRACE("found .rsrc at %08x, size %08x\n", sec[i].PointerToRawData, sec[i].SizeOfRawData);
1078 root = (void*) ((BYTE*)base + sec[i].PointerToRawData);
1079 enumerate_mapped_resources( updates, base, mapping_size, root );
1084 static BOOL map_file_into_memory( struct mapping_info *mi )
1086 DWORD page_attr, perm;
1090 page_attr = PAGE_READWRITE;
1091 perm = FILE_MAP_WRITE | FILE_MAP_READ;
1095 page_attr = PAGE_READONLY;
1096 perm = FILE_MAP_READ;
1099 mi->mapping = CreateFileMappingW( mi->file, NULL, page_attr, 0, 0, NULL );
1103 mi->base = MapViewOfFile( mi->mapping, perm, 0, 0, mi->size );
1110 static BOOL unmap_file_from_memory( struct mapping_info *mi )
1113 UnmapViewOfFile( mi->base );
1116 CloseHandle( mi->mapping );
1121 static void destroy_mapping( struct mapping_info *mi )
1125 unmap_file_from_memory( mi );
1127 CloseHandle( mi->file );
1128 HeapFree( GetProcessHeap(), 0, mi );
1131 static struct mapping_info *create_mapping( LPCWSTR name, BOOL rw )
1133 struct mapping_info *mi;
1135 mi = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof *mi );
1139 mi->read_write = rw;
1141 mi->file = CreateFileW( name, GENERIC_READ | (rw ? GENERIC_WRITE : 0),
1142 0, NULL, OPEN_EXISTING, 0, 0 );
1144 if (mi->file != INVALID_HANDLE_VALUE)
1146 mi->size = GetFileSize( mi->file, NULL );
1148 if (map_file_into_memory( mi ))
1152 unmap_file_from_memory( mi );
1157 static BOOL resize_mapping( struct mapping_info *mi, DWORD new_size )
1159 if (!unmap_file_from_memory( mi ))
1162 /* change the file size */
1163 SetFilePointer( mi->file, new_size, NULL, FILE_BEGIN );
1164 if (!SetEndOfFile( mi->file ))
1166 ERR("failed to set file size to %08x\n", new_size );
1170 mi->size = new_size;
1172 return map_file_into_memory( mi );
1175 static void get_resource_sizes( QUEUEDUPDATES *updates, struct resource_size_info *si )
1177 struct resource_dir_entry *types, *names;
1178 struct resource_data *data;
1179 DWORD num_types = 0, num_names = 0, num_langs = 0, strings_size = 0, data_size = 0;
1181 memset( si, 0, sizeof *si );
1183 LIST_FOR_EACH_ENTRY( types, &updates->root, struct resource_dir_entry, entry )
1186 if (HIWORD( types->id ))
1187 strings_size += sizeof (WORD) + lstrlenW( types->id )*sizeof (WCHAR);
1189 LIST_FOR_EACH_ENTRY( names, &types->children, struct resource_dir_entry, entry )
1193 if (HIWORD( names->id ))
1194 strings_size += sizeof (WORD) + lstrlenW( names->id )*sizeof (WCHAR);
1196 LIST_FOR_EACH_ENTRY( data, &names->children, struct resource_data, entry )
1199 data_size += (data->cbData + 3) & ~3;
1204 /* names are at the end of the types */
1205 si->names_ofs = sizeof (IMAGE_RESOURCE_DIRECTORY) +
1206 num_types * sizeof (IMAGE_RESOURCE_DIRECTORY_ENTRY);
1208 /* language directories are at the end of the names */
1209 si->langs_ofs = si->names_ofs +
1210 num_types * sizeof (IMAGE_RESOURCE_DIRECTORY) +
1211 num_names * sizeof (IMAGE_RESOURCE_DIRECTORY_ENTRY);
1213 si->data_entry_ofs = si->langs_ofs +
1214 num_names * sizeof (IMAGE_RESOURCE_DIRECTORY) +
1215 num_langs * sizeof (IMAGE_RESOURCE_DIRECTORY_ENTRY);
1217 si->strings_ofs = si->data_entry_ofs +
1218 num_langs * sizeof (IMAGE_RESOURCE_DATA_ENTRY);
1220 si->data_ofs = si->strings_ofs + ((strings_size + 3) & ~3);
1222 si->total_size = si->data_ofs + data_size;
1224 TRACE("names %08x langs %08x data entries %08x strings %08x data %08x total %08x\n",
1225 si->names_ofs, si->langs_ofs, si->data_entry_ofs,
1226 si->strings_ofs, si->data_ofs, si->total_size);
1229 static void res_write_padding( BYTE *res_base, DWORD size )
1231 static const BYTE pad[] = {
1232 'P','A','D','D','I','N','G','X','X','P','A','D','D','I','N','G' };
1235 for ( i = 0; i < size / sizeof pad; i++ )
1236 memcpy( &res_base[i*sizeof pad], pad, sizeof pad );
1237 memcpy( &res_base[i*sizeof pad], pad, size%sizeof pad );
1240 static BOOL write_resources( QUEUEDUPDATES *updates, LPBYTE base, struct resource_size_info *si, DWORD rva )
1242 struct resource_dir_entry *types, *names;
1243 struct resource_data *data;
1244 IMAGE_RESOURCE_DIRECTORY *root;
1246 TRACE("%p %p %p %08x\n", updates, base, si, rva );
1248 memset( base, 0, si->total_size );
1250 /* the root entry always exists */
1251 root = (IMAGE_RESOURCE_DIRECTORY*) base;
1252 memset( root, 0, sizeof *root );
1253 root->MajorVersion = 4;
1254 si->types_ofs = sizeof *root;
1255 LIST_FOR_EACH_ENTRY( types, &updates->root, struct resource_dir_entry, entry )
1257 IMAGE_RESOURCE_DIRECTORY_ENTRY *e1;
1258 IMAGE_RESOURCE_DIRECTORY *namedir;
1260 e1 = (IMAGE_RESOURCE_DIRECTORY_ENTRY*) &base[si->types_ofs];
1261 memset( e1, 0, sizeof *e1 );
1262 if (HIWORD( types->id ))
1267 root->NumberOfNamedEntries++;
1268 e1->u1.s1.NameIsString = 1;
1269 e1->u1.s1.NameOffset = si->strings_ofs;
1271 strings = (WCHAR*) &base[si->strings_ofs];
1272 len = lstrlenW( types->id );
1274 memcpy( &strings[1], types->id, len * sizeof (WCHAR) );
1275 si->strings_ofs += (len + 1) * sizeof (WCHAR);
1279 root->NumberOfIdEntries++;
1280 e1->u1.s2.Id = LOWORD( types->id );
1282 e1->u2.s3.OffsetToDirectory = si->names_ofs;
1283 e1->u2.s3.DataIsDirectory = TRUE;
1284 si->types_ofs += sizeof (IMAGE_RESOURCE_DIRECTORY_ENTRY);
1286 namedir = (IMAGE_RESOURCE_DIRECTORY*) &base[si->names_ofs];
1287 memset( namedir, 0, sizeof *namedir );
1288 namedir->MajorVersion = 4;
1289 si->names_ofs += sizeof (IMAGE_RESOURCE_DIRECTORY);
1291 LIST_FOR_EACH_ENTRY( names, &types->children, struct resource_dir_entry, entry )
1293 IMAGE_RESOURCE_DIRECTORY_ENTRY *e2;
1294 IMAGE_RESOURCE_DIRECTORY *langdir;
1296 e2 = (IMAGE_RESOURCE_DIRECTORY_ENTRY*) &base[si->names_ofs];
1297 memset( e2, 0, sizeof *e2 );
1298 if (HIWORD( names->id ))
1303 namedir->NumberOfNamedEntries++;
1304 e2->u1.s1.NameIsString = 1;
1305 e2->u1.s1.NameOffset = si->strings_ofs;
1307 strings = (WCHAR*) &base[si->strings_ofs];
1308 len = lstrlenW( names->id );
1310 memcpy( &strings[1], names->id, len * sizeof (WCHAR) );
1311 si->strings_ofs += (len + 1) * sizeof (WCHAR);
1315 namedir->NumberOfIdEntries++;
1316 e2->u1.s2.Id = LOWORD( names->id );
1318 e2->u2.s3.OffsetToDirectory = si->langs_ofs;
1319 e2->u2.s3.DataIsDirectory = TRUE;
1320 si->names_ofs += sizeof (IMAGE_RESOURCE_DIRECTORY_ENTRY);
1322 langdir = (IMAGE_RESOURCE_DIRECTORY*) &base[si->langs_ofs];
1323 memset( langdir, 0, sizeof *langdir );
1324 langdir->MajorVersion = 4;
1325 si->langs_ofs += sizeof (IMAGE_RESOURCE_DIRECTORY);
1327 LIST_FOR_EACH_ENTRY( data, &names->children, struct resource_data, entry )
1329 IMAGE_RESOURCE_DIRECTORY_ENTRY *e3;
1330 IMAGE_RESOURCE_DATA_ENTRY *de;
1333 e3 = (IMAGE_RESOURCE_DIRECTORY_ENTRY*) &base[si->langs_ofs];
1334 memset( e3, 0, sizeof *e3 );
1335 langdir->NumberOfIdEntries++;
1336 e3->u1.s2.Id = LOWORD( data->lang );
1337 e3->u2.OffsetToData = si->data_entry_ofs;
1339 si->langs_ofs += sizeof (IMAGE_RESOURCE_DIRECTORY_ENTRY);
1341 /* write out all the data entries */
1342 de = (IMAGE_RESOURCE_DATA_ENTRY*) &base[si->data_entry_ofs];
1343 memset( de, 0, sizeof *de );
1344 de->OffsetToData = si->data_ofs + rva;
1345 de->Size = data->cbData;
1346 de->CodePage = data->codepage;
1347 si->data_entry_ofs += sizeof (IMAGE_RESOURCE_DATA_ENTRY);
1349 /* write out the resource data */
1350 memcpy( &base[si->data_ofs], data->lpData, data->cbData );
1351 si->data_ofs += data->cbData;
1353 pad_size = (-si->data_ofs)&3;
1354 res_write_padding( &base[si->data_ofs], pad_size );
1355 si->data_ofs += pad_size;
1365 * Assumes that the resources are in .rsrc
1366 * and .rsrc is the last section in the file.
1367 * Not sure whether updating resources will other cases on Windows.
1368 * If the resources lie in a section containing other data,
1369 * resizing that section could possibly cause trouble.
1370 * If the section with the resources isn't last, the remaining
1371 * sections need to be moved down in the file, and the section header
1372 * would need to be adjusted.
1373 * If we needed to add a section, what would we name it?
1374 * If we needed to add a section and there wasn't space in the file
1375 * header, how would that work?
1376 * Seems that at least some of these cases can't be handled properly.
1378 static IMAGE_SECTION_HEADER *get_resource_section( void *base, DWORD mapping_size )
1380 IMAGE_SECTION_HEADER *sec;
1381 IMAGE_NT_HEADERS *nt;
1382 DWORD i, num_sections = 0;
1384 nt = get_nt_header( base, mapping_size );
1388 sec = get_section_header( base, mapping_size, &num_sections );
1392 /* find the resources section */
1393 for (i=0; i<num_sections; i++)
1394 if (!memcmp(sec[i].Name, ".rsrc", 6))
1397 if (i == num_sections)
1399 FIXME(".rsrc doesn't exist\n");
1403 /* check that the resources section is last */
1404 if (i != num_sections - 1)
1406 FIXME(".rsrc isn't the last section\n");
1413 static DWORD get_init_data_size( void *base, DWORD mapping_size )
1415 DWORD i, sz = 0, num_sections = 0;
1416 IMAGE_SECTION_HEADER *s;
1418 s = get_section_header( base, mapping_size, &num_sections );
1420 for (i=0; i<num_sections; i++)
1421 if (s[i].Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA)
1422 sz += s[i].SizeOfRawData;
1424 TRACE("size = %08x\n", sz);
1429 static BOOL write_raw_resources( QUEUEDUPDATES *updates )
1431 static const WCHAR prefix[] = { 'r','e','s','u',0 };
1432 WCHAR tempdir[MAX_PATH], tempfile[MAX_PATH];
1433 DWORD mapping_size, section_size, old_size;
1435 IMAGE_SECTION_HEADER *sec;
1436 IMAGE_NT_HEADERS *nt;
1437 struct resource_size_info res_size;
1439 struct mapping_info *read_map = NULL, *write_map = NULL;
1441 /* copy the exe to a temp file then update the temp file... */
1443 if (!GetTempPathW( MAX_PATH, tempdir ))
1446 if (!GetTempFileNameW( tempdir, prefix, 0, tempfile ))
1449 if (!CopyFileW( updates->pFileName, tempfile, FALSE ))
1452 TRACE("tempfile %s\n", debugstr_w(tempfile));
1454 if (!updates->bDeleteExistingResources)
1456 read_map = create_mapping( updates->pFileName, FALSE );
1460 ret = read_mapped_resources( updates, read_map->base, read_map->size );
1463 ERR("failed to read existing resources\n");
1468 write_map = create_mapping( tempfile, TRUE );
1472 nt = get_nt_header( write_map->base, write_map->size );
1476 if (nt->OptionalHeader.SectionAlignment <= 0)
1478 ERR("invalid section alignment %04x\n", nt->OptionalHeader.SectionAlignment);
1482 sec = get_resource_section( write_map->base, write_map->size );
1486 if ((sec->SizeOfRawData + sec->PointerToRawData) != write_map->size)
1488 FIXME(".rsrc isn't at the end of the image %08x + %08x != %08x\n",
1489 sec->SizeOfRawData, sec->PointerToRawData, write_map->size);
1493 TRACE("before .rsrc at %08x, size %08x\n", sec->PointerToRawData, sec->SizeOfRawData);
1495 get_resource_sizes( updates, &res_size );
1497 /* round up the section size */
1498 section_size = res_size.total_size;
1499 section_size += (-section_size) % nt->OptionalHeader.SectionAlignment;
1501 mapping_size = sec->PointerToRawData + section_size;
1503 TRACE("requires %08x (%08x) bytes\n", res_size.total_size, section_size );
1505 /* check if the file size needs to be changed */
1506 if (section_size != sec->SizeOfRawData)
1508 old_size = write_map->size;
1510 TRACE("file size %08x -> %08x\n", old_size, mapping_size);
1512 /* unmap the file before changing the file size */
1513 ret = resize_mapping( write_map, mapping_size );
1515 /* get the pointers again - they might be different after remapping */
1516 nt = get_nt_header( write_map->base, mapping_size );
1519 ERR("couldn't get NT header\n");
1523 sec = get_resource_section( write_map->base, mapping_size );
1527 /* adjust the PE header information */
1528 nt->OptionalHeader.SizeOfImage += (mapping_size - old_size);
1529 sec->SizeOfRawData = section_size;
1530 sec->Misc.VirtualSize = section_size;
1531 nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].Size = res_size.total_size;
1532 nt->OptionalHeader.SizeOfInitializedData = get_init_data_size( write_map->base, mapping_size );
1535 res_base = (LPBYTE) write_map->base + sec->PointerToRawData;
1537 TRACE("base = %p offset = %08x\n", write_map->base, sec->PointerToRawData);
1539 ret = write_resources( updates, res_base, &res_size, sec->VirtualAddress );
1541 res_write_padding( res_base + res_size.total_size, section_size - res_size.total_size );
1543 TRACE("after .rsrc at %08x, size %08x\n", sec->PointerToRawData, sec->SizeOfRawData);
1546 destroy_mapping( read_map );
1547 destroy_mapping( write_map );
1550 ret = CopyFileW( tempfile, updates->pFileName, FALSE );
1552 DeleteFileW( tempfile );
1557 /***********************************************************************
1558 * BeginUpdateResourceW (KERNEL32.@)
1560 HANDLE WINAPI BeginUpdateResourceW( LPCWSTR pFileName, BOOL bDeleteExistingResources )
1562 QUEUEDUPDATES *updates = NULL;
1563 HANDLE hUpdate, file, ret = NULL;
1565 TRACE("%s, %d\n", debugstr_w(pFileName), bDeleteExistingResources);
1567 hUpdate = GlobalAlloc(GHND, sizeof(QUEUEDUPDATES));
1571 updates = GlobalLock(hUpdate);
1574 list_init( &updates->root );
1575 updates->bDeleteExistingResources = bDeleteExistingResources;
1576 updates->pFileName = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(pFileName)+1)*sizeof(WCHAR));
1577 if (updates->pFileName)
1579 lstrcpyW(updates->pFileName, pFileName);
1581 file = CreateFileW( pFileName, GENERIC_READ | GENERIC_WRITE,
1582 0, NULL, OPEN_EXISTING, 0, 0 );
1584 /* if resources are deleted, only the file's presence is checked */
1585 if (file != INVALID_HANDLE_VALUE &&
1586 (bDeleteExistingResources || check_pe_exe( file, updates )))
1589 HeapFree( GetProcessHeap(), 0, updates->pFileName );
1591 CloseHandle( file );
1593 GlobalUnlock(hUpdate);
1597 GlobalFree(hUpdate);
1603 /***********************************************************************
1604 * BeginUpdateResourceA (KERNEL32.@)
1606 HANDLE WINAPI BeginUpdateResourceA( LPCSTR pFileName, BOOL bDeleteExistingResources )
1608 UNICODE_STRING FileNameW;
1610 RtlCreateUnicodeStringFromAsciiz(&FileNameW, pFileName);
1611 ret = BeginUpdateResourceW(FileNameW.Buffer, bDeleteExistingResources);
1612 RtlFreeUnicodeString(&FileNameW);
1617 /***********************************************************************
1618 * EndUpdateResourceW (KERNEL32.@)
1620 BOOL WINAPI EndUpdateResourceW( HANDLE hUpdate, BOOL fDiscard )
1622 QUEUEDUPDATES *updates;
1625 TRACE("%p %d\n", hUpdate, fDiscard);
1627 updates = GlobalLock(hUpdate);
1631 ret = fDiscard || write_raw_resources( updates );
1633 free_resource_directory( &updates->root, 2 );
1635 HeapFree( GetProcessHeap(), 0, updates->pFileName );
1636 GlobalUnlock( hUpdate );
1637 GlobalFree( hUpdate );
1643 /***********************************************************************
1644 * EndUpdateResourceA (KERNEL32.@)
1646 BOOL WINAPI EndUpdateResourceA( HANDLE hUpdate, BOOL fDiscard )
1648 return EndUpdateResourceW(hUpdate, fDiscard);
1652 /***********************************************************************
1653 * UpdateResourceW (KERNEL32.@)
1655 BOOL WINAPI UpdateResourceW( HANDLE hUpdate, LPCWSTR lpType, LPCWSTR lpName,
1656 WORD wLanguage, LPVOID lpData, DWORD cbData)
1658 QUEUEDUPDATES *updates;
1661 TRACE("%p %s %s %08x %p %d\n", hUpdate,
1662 debugstr_w(lpType), debugstr_w(lpName), wLanguage, lpData, cbData);
1664 updates = GlobalLock(hUpdate);
1667 struct resource_data *data;
1668 data = allocate_resource_data( wLanguage, 0, lpData, cbData, TRUE );
1670 ret = update_add_resource( updates, lpType, lpName, data, TRUE );
1671 GlobalUnlock(hUpdate);
1677 /***********************************************************************
1678 * UpdateResourceA (KERNEL32.@)
1680 BOOL WINAPI UpdateResourceA( HANDLE hUpdate, LPCSTR lpType, LPCSTR lpName,
1681 WORD wLanguage, LPVOID lpData, DWORD cbData)
1684 UNICODE_STRING TypeW;
1685 UNICODE_STRING NameW;
1687 TypeW.Buffer = (LPWSTR)lpType;
1689 RtlCreateUnicodeStringFromAsciiz(&TypeW, lpType);
1691 NameW.Buffer = (LPWSTR)lpName;
1693 RtlCreateUnicodeStringFromAsciiz(&NameW, lpName);
1694 ret = UpdateResourceW(hUpdate, TypeW.Buffer, NameW.Buffer, wLanguage, lpData, cbData);
1695 if(HIWORD(lpType)) RtlFreeUnicodeString(&TypeW);
1696 if(HIWORD(lpName)) RtlFreeUnicodeString(&NameW);