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/debug.h"
36 #include "wine/exception.h"
37 #include "wine/unicode.h"
38 #include "wine/list.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(resource
);
42 /* retrieve the resource name to pass to the ntdll functions */
43 static NTSTATUS
get_res_nameA( LPCSTR name
, UNICODE_STRING
*str
)
47 str
->Buffer
= ULongToPtr(LOWORD(name
));
48 return STATUS_SUCCESS
;
53 if (RtlCharToInteger( name
+ 1, 10, &value
) != STATUS_SUCCESS
|| HIWORD(value
))
54 return STATUS_INVALID_PARAMETER
;
55 str
->Buffer
= ULongToPtr(value
);
56 return STATUS_SUCCESS
;
58 RtlCreateUnicodeStringFromAsciiz( str
, name
);
59 RtlUpcaseUnicodeString( str
, str
, FALSE
);
60 return STATUS_SUCCESS
;
63 /* retrieve the resource name to pass to the ntdll functions */
64 static NTSTATUS
get_res_nameW( LPCWSTR name
, UNICODE_STRING
*str
)
68 str
->Buffer
= ULongToPtr(LOWORD(name
));
69 return STATUS_SUCCESS
;
74 RtlInitUnicodeString( str
, name
+ 1 );
75 if (RtlUnicodeStringToInteger( str
, 10, &value
) != STATUS_SUCCESS
|| HIWORD(value
))
76 return STATUS_INVALID_PARAMETER
;
77 str
->Buffer
= ULongToPtr(value
);
78 return STATUS_SUCCESS
;
80 RtlCreateUnicodeString( str
, name
);
81 RtlUpcaseUnicodeString( str
, str
, FALSE
);
82 return STATUS_SUCCESS
;
85 /* implementation of FindResourceExA */
86 static HRSRC
find_resourceA( HMODULE hModule
, LPCSTR type
, LPCSTR name
, WORD lang
)
89 UNICODE_STRING nameW
, typeW
;
90 LDR_RESOURCE_INFO info
;
91 const IMAGE_RESOURCE_DATA_ENTRY
*entry
= NULL
;
98 if ((status
= get_res_nameA( name
, &nameW
)) != STATUS_SUCCESS
) goto done
;
99 if ((status
= get_res_nameA( type
, &typeW
)) != STATUS_SUCCESS
) goto done
;
100 info
.Type
= (ULONG_PTR
)typeW
.Buffer
;
101 info
.Name
= (ULONG_PTR
)nameW
.Buffer
;
102 info
.Language
= lang
;
103 status
= LdrFindResource_U( hModule
, &info
, 3, &entry
);
105 if (status
!= STATUS_SUCCESS
) SetLastError( RtlNtStatusToDosError(status
) );
109 SetLastError( ERROR_INVALID_PARAMETER
);
113 if (HIWORD(nameW
.Buffer
)) HeapFree( GetProcessHeap(), 0, nameW
.Buffer
);
114 if (HIWORD(typeW
.Buffer
)) HeapFree( GetProcessHeap(), 0, typeW
.Buffer
);
119 /* implementation of FindResourceExW */
120 static HRSRC
find_resourceW( HMODULE hModule
, LPCWSTR type
, LPCWSTR name
, WORD lang
)
123 UNICODE_STRING nameW
, typeW
;
124 LDR_RESOURCE_INFO info
;
125 const IMAGE_RESOURCE_DATA_ENTRY
*entry
= NULL
;
127 nameW
.Buffer
= typeW
.Buffer
= NULL
;
131 if ((status
= get_res_nameW( name
, &nameW
)) != STATUS_SUCCESS
) goto done
;
132 if ((status
= get_res_nameW( type
, &typeW
)) != STATUS_SUCCESS
) goto done
;
133 info
.Type
= (ULONG_PTR
)typeW
.Buffer
;
134 info
.Name
= (ULONG_PTR
)nameW
.Buffer
;
135 info
.Language
= lang
;
136 status
= LdrFindResource_U( hModule
, &info
, 3, &entry
);
138 if (status
!= STATUS_SUCCESS
) SetLastError( RtlNtStatusToDosError(status
) );
142 SetLastError( ERROR_INVALID_PARAMETER
);
146 if (HIWORD(nameW
.Buffer
)) HeapFree( GetProcessHeap(), 0, nameW
.Buffer
);
147 if (HIWORD(typeW
.Buffer
)) HeapFree( GetProcessHeap(), 0, typeW
.Buffer
);
151 /**********************************************************************
152 * FindResourceExA (KERNEL32.@)
154 HRSRC WINAPI
FindResourceExA( HMODULE hModule
, LPCSTR type
, LPCSTR name
, WORD lang
)
156 TRACE( "%p %s %s %04x\n", hModule
, debugstr_a(type
), debugstr_a(name
), lang
);
158 if (!hModule
) hModule
= GetModuleHandleW(0);
159 return find_resourceA( hModule
, type
, name
, lang
);
163 /**********************************************************************
164 * FindResourceA (KERNEL32.@)
166 HRSRC WINAPI
FindResourceA( HMODULE hModule
, LPCSTR name
, LPCSTR type
)
168 return FindResourceExA( hModule
, type
, name
, MAKELANGID( LANG_NEUTRAL
, SUBLANG_NEUTRAL
) );
172 /**********************************************************************
173 * FindResourceExW (KERNEL32.@)
175 HRSRC WINAPI
FindResourceExW( HMODULE hModule
, LPCWSTR type
, LPCWSTR name
, WORD lang
)
177 TRACE( "%p %s %s %04x\n", hModule
, debugstr_w(type
), debugstr_w(name
), lang
);
179 if (!hModule
) hModule
= GetModuleHandleW(0);
180 return find_resourceW( hModule
, type
, name
, lang
);
184 /**********************************************************************
185 * FindResourceW (KERNEL32.@)
187 HRSRC WINAPI
FindResourceW( HINSTANCE hModule
, LPCWSTR name
, LPCWSTR type
)
189 return FindResourceExW( hModule
, type
, name
, MAKELANGID( LANG_NEUTRAL
, SUBLANG_NEUTRAL
) );
193 /**********************************************************************
194 * EnumResourceTypesA (KERNEL32.@)
196 BOOL WINAPI
EnumResourceTypesA( HMODULE hmod
, ENUMRESTYPEPROCA lpfun
, LONG_PTR lparam
)
201 DWORD len
= 0, newlen
;
203 const IMAGE_RESOURCE_DIRECTORY
*resdir
;
204 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*et
;
205 const IMAGE_RESOURCE_DIR_STRING_U
*str
;
207 TRACE( "%p %p %lx\n", hmod
, lpfun
, lparam
);
209 if (!hmod
) hmod
= GetModuleHandleA( NULL
);
211 if ((status
= LdrFindResourceDirectory_U( hmod
, NULL
, 0, &resdir
)) != STATUS_SUCCESS
)
213 SetLastError( RtlNtStatusToDosError(status
) );
216 et
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(resdir
+ 1);
217 for (i
= 0; i
< resdir
->NumberOfNamedEntries
+resdir
->NumberOfIdEntries
; i
++)
219 if (et
[i
].u1
.s1
.NameIsString
)
221 str
= (const IMAGE_RESOURCE_DIR_STRING_U
*)((const BYTE
*)resdir
+ et
[i
].u1
.s1
.NameOffset
);
222 newlen
= WideCharToMultiByte( CP_ACP
, 0, str
->NameString
, str
->Length
, NULL
, 0, NULL
, NULL
);
223 if (newlen
+ 1 > len
)
226 HeapFree( GetProcessHeap(), 0, type
);
227 if (!(type
= HeapAlloc( GetProcessHeap(), 0, len
))) return FALSE
;
229 WideCharToMultiByte( CP_ACP
, 0, str
->NameString
, str
->Length
, type
, len
, NULL
, NULL
);
231 ret
= lpfun(hmod
,type
,lparam
);
235 ret
= lpfun( hmod
, UIntToPtr(et
[i
].u1
.s2
.Id
), lparam
);
239 HeapFree( GetProcessHeap(), 0, type
);
244 /**********************************************************************
245 * EnumResourceTypesW (KERNEL32.@)
247 BOOL WINAPI
EnumResourceTypesW( HMODULE hmod
, ENUMRESTYPEPROCW lpfun
, LONG_PTR lparam
)
253 const IMAGE_RESOURCE_DIRECTORY
*resdir
;
254 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*et
;
255 const IMAGE_RESOURCE_DIR_STRING_U
*str
;
257 TRACE( "%p %p %lx\n", hmod
, lpfun
, lparam
);
259 if (!hmod
) hmod
= GetModuleHandleW( NULL
);
261 if ((status
= LdrFindResourceDirectory_U( hmod
, NULL
, 0, &resdir
)) != STATUS_SUCCESS
)
263 SetLastError( RtlNtStatusToDosError(status
) );
266 et
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(resdir
+ 1);
267 for (i
= 0; i
< resdir
->NumberOfNamedEntries
+ resdir
->NumberOfIdEntries
; i
++)
269 if (et
[i
].u1
.s1
.NameIsString
)
271 str
= (const IMAGE_RESOURCE_DIR_STRING_U
*)((const BYTE
*)resdir
+ et
[i
].u1
.s1
.NameOffset
);
272 if (str
->Length
+ 1 > len
)
274 len
= str
->Length
+ 1;
275 HeapFree( GetProcessHeap(), 0, type
);
276 if (!(type
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) ))) return FALSE
;
278 memcpy(type
, str
->NameString
, str
->Length
* sizeof (WCHAR
));
279 type
[str
->Length
] = 0;
280 ret
= lpfun(hmod
,type
,lparam
);
284 ret
= lpfun( hmod
, UIntToPtr(et
[i
].u1
.s2
.Id
), lparam
);
288 HeapFree( GetProcessHeap(), 0, type
);
293 /**********************************************************************
294 * EnumResourceNamesA (KERNEL32.@)
296 BOOL WINAPI
EnumResourceNamesA( HMODULE hmod
, LPCSTR type
, ENUMRESNAMEPROCA lpfun
, LONG_PTR lparam
)
300 DWORD len
= 0, newlen
;
303 UNICODE_STRING typeW
;
304 LDR_RESOURCE_INFO info
;
305 const IMAGE_RESOURCE_DIRECTORY
*basedir
, *resdir
;
306 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*et
;
307 const IMAGE_RESOURCE_DIR_STRING_U
*str
;
309 TRACE( "%p %s %p %lx\n", hmod
, debugstr_a(type
), lpfun
, lparam
);
311 if (!hmod
) hmod
= GetModuleHandleA( NULL
);
313 if ((status
= LdrFindResourceDirectory_U( hmod
, NULL
, 0, &basedir
)) != STATUS_SUCCESS
)
315 if ((status
= get_res_nameA( type
, &typeW
)) != STATUS_SUCCESS
)
317 info
.Type
= (ULONG_PTR
)typeW
.Buffer
;
318 if ((status
= LdrFindResourceDirectory_U( hmod
, &info
, 1, &resdir
)) != STATUS_SUCCESS
)
321 et
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(resdir
+ 1);
324 for (i
= 0; i
< resdir
->NumberOfNamedEntries
+resdir
->NumberOfIdEntries
; i
++)
326 if (et
[i
].u1
.s1
.NameIsString
)
328 str
= (const IMAGE_RESOURCE_DIR_STRING_U
*)((const BYTE
*)basedir
+ et
[i
].u1
.s1
.NameOffset
);
329 newlen
= WideCharToMultiByte(CP_ACP
, 0, str
->NameString
, str
->Length
, NULL
, 0, NULL
, NULL
);
330 if (newlen
+ 1 > len
)
333 HeapFree( GetProcessHeap(), 0, name
);
334 if (!(name
= HeapAlloc(GetProcessHeap(), 0, len
+ 1 )))
340 WideCharToMultiByte( CP_ACP
, 0, str
->NameString
, str
->Length
, name
, len
, NULL
, NULL
);
342 ret
= lpfun(hmod
,type
,name
,lparam
);
346 ret
= lpfun( hmod
, type
, UIntToPtr(et
[i
].u1
.s2
.Id
), lparam
);
354 status
= STATUS_ACCESS_VIOLATION
;
359 HeapFree( GetProcessHeap(), 0, name
);
360 if (HIWORD(typeW
.Buffer
)) HeapFree( GetProcessHeap(), 0, typeW
.Buffer
);
361 if (status
!= STATUS_SUCCESS
) SetLastError( RtlNtStatusToDosError(status
) );
366 /**********************************************************************
367 * EnumResourceNamesW (KERNEL32.@)
369 BOOL WINAPI
EnumResourceNamesW( HMODULE hmod
, LPCWSTR type
, ENUMRESNAMEPROCW lpfun
, LONG_PTR lparam
)
375 UNICODE_STRING typeW
;
376 LDR_RESOURCE_INFO info
;
377 const IMAGE_RESOURCE_DIRECTORY
*basedir
, *resdir
;
378 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*et
;
379 const IMAGE_RESOURCE_DIR_STRING_U
*str
;
381 TRACE( "%p %s %p %lx\n", hmod
, debugstr_w(type
), lpfun
, lparam
);
383 if (!hmod
) hmod
= GetModuleHandleW( NULL
);
385 if ((status
= LdrFindResourceDirectory_U( hmod
, NULL
, 0, &basedir
)) != STATUS_SUCCESS
)
387 if ((status
= get_res_nameW( type
, &typeW
)) != STATUS_SUCCESS
)
389 info
.Type
= (ULONG_PTR
)typeW
.Buffer
;
390 if ((status
= LdrFindResourceDirectory_U( hmod
, &info
, 1, &resdir
)) != STATUS_SUCCESS
)
393 et
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(resdir
+ 1);
396 for (i
= 0; i
< resdir
->NumberOfNamedEntries
+resdir
->NumberOfIdEntries
; i
++)
398 if (et
[i
].u1
.s1
.NameIsString
)
400 str
= (const IMAGE_RESOURCE_DIR_STRING_U
*)((const BYTE
*)basedir
+ et
[i
].u1
.s1
.NameOffset
);
401 if (str
->Length
+ 1 > len
)
403 len
= str
->Length
+ 1;
404 HeapFree( GetProcessHeap(), 0, name
);
405 if (!(name
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
411 memcpy(name
, str
->NameString
, str
->Length
* sizeof (WCHAR
));
412 name
[str
->Length
] = 0;
413 ret
= lpfun(hmod
,type
,name
,lparam
);
417 ret
= lpfun( hmod
, type
, UIntToPtr(et
[i
].u1
.s2
.Id
), lparam
);
425 status
= STATUS_ACCESS_VIOLATION
;
429 HeapFree( GetProcessHeap(), 0, name
);
430 if (HIWORD(typeW
.Buffer
)) HeapFree( GetProcessHeap(), 0, typeW
.Buffer
);
431 if (status
!= STATUS_SUCCESS
) SetLastError( RtlNtStatusToDosError(status
) );
436 /**********************************************************************
437 * EnumResourceLanguagesA (KERNEL32.@)
439 BOOL WINAPI
EnumResourceLanguagesA( HMODULE hmod
, LPCSTR type
, LPCSTR name
,
440 ENUMRESLANGPROCA lpfun
, LONG_PTR lparam
)
445 UNICODE_STRING typeW
, nameW
;
446 LDR_RESOURCE_INFO info
;
447 const IMAGE_RESOURCE_DIRECTORY
*basedir
, *resdir
;
448 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*et
;
450 TRACE( "%p %s %s %p %lx\n", hmod
, debugstr_a(type
), debugstr_a(name
), lpfun
, lparam
);
452 if (!hmod
) hmod
= GetModuleHandleA( NULL
);
453 typeW
.Buffer
= nameW
.Buffer
= NULL
;
454 if ((status
= LdrFindResourceDirectory_U( hmod
, NULL
, 0, &basedir
)) != STATUS_SUCCESS
)
456 if ((status
= get_res_nameA( type
, &typeW
)) != STATUS_SUCCESS
)
458 if ((status
= get_res_nameA( name
, &nameW
)) != STATUS_SUCCESS
)
460 info
.Type
= (ULONG_PTR
)typeW
.Buffer
;
461 info
.Name
= (ULONG_PTR
)nameW
.Buffer
;
462 if ((status
= LdrFindResourceDirectory_U( hmod
, &info
, 2, &resdir
)) != STATUS_SUCCESS
)
465 et
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(resdir
+ 1);
468 for (i
= 0; i
< resdir
->NumberOfNamedEntries
+ resdir
->NumberOfIdEntries
; i
++)
470 ret
= lpfun( hmod
, type
, name
, et
[i
].u1
.s2
.Id
, lparam
);
477 status
= STATUS_ACCESS_VIOLATION
;
481 if (HIWORD(typeW
.Buffer
)) HeapFree( GetProcessHeap(), 0, typeW
.Buffer
);
482 if (HIWORD(nameW
.Buffer
)) HeapFree( GetProcessHeap(), 0, nameW
.Buffer
);
483 if (status
!= STATUS_SUCCESS
) SetLastError( RtlNtStatusToDosError(status
) );
488 /**********************************************************************
489 * EnumResourceLanguagesW (KERNEL32.@)
491 BOOL WINAPI
EnumResourceLanguagesW( HMODULE hmod
, LPCWSTR type
, LPCWSTR name
,
492 ENUMRESLANGPROCW lpfun
, LONG_PTR lparam
)
497 UNICODE_STRING typeW
, nameW
;
498 LDR_RESOURCE_INFO info
;
499 const IMAGE_RESOURCE_DIRECTORY
*basedir
, *resdir
;
500 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*et
;
502 TRACE( "%p %s %s %p %lx\n", hmod
, debugstr_w(type
), debugstr_w(name
), lpfun
, lparam
);
504 if (!hmod
) hmod
= GetModuleHandleW( NULL
);
505 typeW
.Buffer
= nameW
.Buffer
= NULL
;
506 if ((status
= LdrFindResourceDirectory_U( hmod
, NULL
, 0, &basedir
)) != STATUS_SUCCESS
)
508 if ((status
= get_res_nameW( type
, &typeW
)) != STATUS_SUCCESS
)
510 if ((status
= get_res_nameW( name
, &nameW
)) != STATUS_SUCCESS
)
512 info
.Type
= (ULONG_PTR
)typeW
.Buffer
;
513 info
.Name
= (ULONG_PTR
)nameW
.Buffer
;
514 if ((status
= LdrFindResourceDirectory_U( hmod
, &info
, 2, &resdir
)) != STATUS_SUCCESS
)
517 et
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(resdir
+ 1);
520 for (i
= 0; i
< resdir
->NumberOfNamedEntries
+ resdir
->NumberOfIdEntries
; i
++)
522 ret
= lpfun( hmod
, type
, name
, et
[i
].u1
.s2
.Id
, lparam
);
529 status
= STATUS_ACCESS_VIOLATION
;
533 if (HIWORD(typeW
.Buffer
)) HeapFree( GetProcessHeap(), 0, typeW
.Buffer
);
534 if (HIWORD(nameW
.Buffer
)) HeapFree( GetProcessHeap(), 0, nameW
.Buffer
);
535 if (status
!= STATUS_SUCCESS
) SetLastError( RtlNtStatusToDosError(status
) );
540 /**********************************************************************
541 * LoadResource (KERNEL32.@)
543 HGLOBAL WINAPI
LoadResource( HINSTANCE hModule
, HRSRC hRsrc
)
548 TRACE( "%p %p\n", hModule
, hRsrc
);
550 if (!hRsrc
) return 0;
551 if (!hModule
) hModule
= GetModuleHandleA( NULL
);
552 status
= LdrAccessResource( hModule
, (IMAGE_RESOURCE_DATA_ENTRY
*)hRsrc
, &ret
, NULL
);
553 if (status
!= STATUS_SUCCESS
) SetLastError( RtlNtStatusToDosError(status
) );
558 /**********************************************************************
559 * LockResource (KERNEL32.@)
561 LPVOID WINAPI
LockResource( HGLOBAL handle
)
567 /**********************************************************************
568 * FreeResource (KERNEL32.@)
570 BOOL WINAPI
FreeResource( HGLOBAL handle
)
576 /**********************************************************************
577 * SizeofResource (KERNEL32.@)
579 DWORD WINAPI
SizeofResource( HINSTANCE hModule
, HRSRC hRsrc
)
581 if (!hRsrc
) return 0;
582 return ((PIMAGE_RESOURCE_DATA_ENTRY
)hRsrc
)->Size
;
586 * Data structure for updating resources.
587 * Type/Name/Language is a keyset for accessing resource data.
589 * QUEUEDUPDATES (root) ->
590 * list of struct resource_dir_entry (Type) ->
591 * list of struct resource_dir_entry (Name) ->
592 * list of struct resource_data Language + Data
598 BOOL bDeleteExistingResources
;
602 /* this structure is shared for types and names */
603 struct resource_dir_entry
{
606 struct list children
;
609 /* this structure is the leaf */
610 struct resource_data
{
618 static int resource_strcmp( LPCWSTR a
, LPCWSTR b
)
622 if (HIWORD( a
) && HIWORD( b
) )
623 return lstrcmpW( a
, b
);
624 /* strings come before ids */
625 if (HIWORD( a
) && !HIWORD( b
))
627 if (HIWORD( b
) && !HIWORD( a
))
629 return ( a
< b
) ? -1 : 1;
632 static struct resource_dir_entry
*find_resource_dir_entry( struct list
*dir
, LPCWSTR id
)
634 struct resource_dir_entry
*ent
;
636 /* match either IDs or strings */
637 LIST_FOR_EACH_ENTRY( ent
, dir
, struct resource_dir_entry
, entry
)
638 if (!resource_strcmp( id
, ent
->id
))
644 static struct resource_data
*find_resource_data( struct list
*dir
, LANGID lang
)
646 struct resource_data
*res_data
;
648 /* match only languages here */
649 LIST_FOR_EACH_ENTRY( res_data
, dir
, struct resource_data
, entry
)
650 if ( lang
== res_data
->lang
)
656 static void add_resource_dir_entry( struct list
*dir
, struct resource_dir_entry
*resdir
)
658 struct resource_dir_entry
*ent
;
660 LIST_FOR_EACH_ENTRY( ent
, dir
, struct resource_dir_entry
, entry
)
662 if (0>resource_strcmp( ent
->id
, resdir
->id
))
665 list_add_before( &ent
->entry
, &resdir
->entry
);
668 list_add_tail( dir
, &resdir
->entry
);
671 static void add_resource_data_entry( struct list
*dir
, struct resource_data
*resdata
)
673 struct resource_data
*ent
;
675 LIST_FOR_EACH_ENTRY( ent
, dir
, struct resource_data
, entry
)
677 if (ent
->lang
< resdata
->lang
)
680 list_add_before( &ent
->entry
, &resdata
->entry
);
683 list_add_tail( dir
, &resdata
->entry
);
686 static LPWSTR
res_strdupW( LPCWSTR str
)
691 if (HIWORD(str
) == 0)
692 return (LPWSTR
) (UINT_PTR
) LOWORD(str
);
693 len
= (lstrlenW( str
) + 1) * sizeof (WCHAR
);
694 ret
= HeapAlloc( GetProcessHeap(), 0, len
);
695 memcpy( ret
, str
, len
);
699 static void res_free_str( LPWSTR str
)
702 HeapFree( GetProcessHeap(), 0, str
);
705 static BOOL
update_add_resource( QUEUEDUPDATES
*updates
, LPCWSTR Type
, LPCWSTR Name
,
706 struct resource_data
*resdata
, BOOL overwrite_existing
)
708 struct resource_dir_entry
*restype
, *resname
;
709 struct resource_data
*existing
;
711 TRACE("%p %s %s %p %d\n", updates
,
712 debugstr_w(Type
), debugstr_w(Name
), resdata
, overwrite_existing
);
714 restype
= find_resource_dir_entry( &updates
->root
, Type
);
717 restype
= HeapAlloc( GetProcessHeap(), 0, sizeof *restype
);
718 restype
->id
= res_strdupW( Type
);
719 list_init( &restype
->children
);
720 add_resource_dir_entry( &updates
->root
, restype
);
723 resname
= find_resource_dir_entry( &restype
->children
, Name
);
726 resname
= HeapAlloc( GetProcessHeap(), 0, sizeof *resname
);
727 resname
->id
= res_strdupW( Name
);
728 list_init( &resname
->children
);
729 add_resource_dir_entry( &restype
->children
, resname
);
733 * If there's an existing resource entry with matching (Type,Name,Language)
734 * it needs to be removed before adding the new data.
736 existing
= find_resource_data( &resname
->children
, resdata
->lang
);
739 if (!overwrite_existing
)
741 list_remove( &existing
->entry
);
742 HeapFree( GetProcessHeap(), 0, existing
);
745 add_resource_data_entry( &resname
->children
, resdata
);
750 static struct resource_data
*allocate_resource_data( WORD Language
, DWORD codepage
,
751 LPVOID lpData
, DWORD cbData
, BOOL copy_data
)
753 struct resource_data
*resdata
;
755 if (!lpData
|| !cbData
)
758 resdata
= HeapAlloc( GetProcessHeap(), 0, sizeof *resdata
+ (copy_data
? cbData
: 0) );
761 resdata
->lang
= Language
;
762 resdata
->codepage
= codepage
;
763 resdata
->cbData
= cbData
;
766 resdata
->lpData
= &resdata
[1];
767 memcpy( resdata
->lpData
, lpData
, cbData
);
770 resdata
->lpData
= lpData
;
776 static void free_resource_directory( struct list
*head
, int level
)
778 struct list
*ptr
= NULL
;
780 while ((ptr
= list_head( head
)))
785 struct resource_dir_entry
*ent
;
787 ent
= LIST_ENTRY( ptr
, struct resource_dir_entry
, entry
);
788 res_free_str( ent
->id
);
789 free_resource_directory( &ent
->children
, level
- 1 );
790 HeapFree(GetProcessHeap(), 0, ent
);
794 struct resource_data
*data
;
796 data
= LIST_ENTRY( ptr
, struct resource_data
, entry
);
797 HeapFree( GetProcessHeap(), 0, data
);
802 static IMAGE_NT_HEADERS
*get_nt_header( void *base
, DWORD mapping_size
)
804 IMAGE_NT_HEADERS
*nt
;
805 IMAGE_DOS_HEADER
*dos
;
807 if (mapping_size
<sizeof (*dos
))
811 if (dos
->e_magic
!= IMAGE_DOS_SIGNATURE
)
814 if ((dos
->e_lfanew
+ sizeof (*nt
)) > mapping_size
)
817 nt
= (void*) ((BYTE
*)base
+ dos
->e_lfanew
);
819 if (nt
->Signature
!= IMAGE_NT_SIGNATURE
)
825 static IMAGE_SECTION_HEADER
*get_section_header( void *base
, DWORD mapping_size
, DWORD
*num_sections
)
827 IMAGE_NT_HEADERS
*nt
;
828 IMAGE_SECTION_HEADER
*sec
;
831 nt
= get_nt_header( base
, mapping_size
);
835 /* check that we don't go over the end of the file accessing the sections */
836 section_ofs
= FIELD_OFFSET(IMAGE_NT_HEADERS
, OptionalHeader
) + nt
->FileHeader
.SizeOfOptionalHeader
;
837 if ((nt
->FileHeader
.NumberOfSections
* sizeof (*sec
) + section_ofs
) > mapping_size
)
841 *num_sections
= nt
->FileHeader
.NumberOfSections
;
843 /* from here we have a valid PE exe to update */
844 return (void*) ((BYTE
*)nt
+ section_ofs
);
847 static BOOL
check_pe_exe( HANDLE file
, QUEUEDUPDATES
*updates
)
849 const IMAGE_NT_HEADERS
*nt
;
850 const IMAGE_SECTION_HEADER
*sec
;
853 DWORD mapping_size
, num_sections
= 0;
856 mapping_size
= GetFileSize( file
, NULL
);
858 mapping
= CreateFileMappingW( file
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
862 base
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, mapping_size
);
866 nt
= get_nt_header( base
, mapping_size
);
870 TRACE("resources: %08x %08x\n",
871 nt
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_RESOURCE
].VirtualAddress
,
872 nt
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_RESOURCE
].Size
);
874 sec
= get_section_header( base
, mapping_size
, &num_sections
);
882 UnmapViewOfFile( base
);
884 CloseHandle( mapping
);
889 struct resource_size_info
{
893 DWORD data_entry_ofs
;
899 struct mapping_info
{
907 static const IMAGE_SECTION_HEADER
*section_from_rva( void *base
, DWORD mapping_size
, DWORD rva
)
909 const IMAGE_SECTION_HEADER
*sec
;
910 DWORD num_sections
= 0;
913 sec
= get_section_header( base
, mapping_size
, &num_sections
);
917 for (i
=num_sections
-1; i
>=0; i
--)
919 if (sec
[i
].VirtualAddress
<= rva
&&
920 rva
<= (DWORD
)sec
[i
].VirtualAddress
+ sec
[i
].SizeOfRawData
)
929 static void *address_from_rva( void *base
, DWORD mapping_size
, DWORD rva
, DWORD len
)
931 const IMAGE_SECTION_HEADER
*sec
;
933 sec
= section_from_rva( base
, mapping_size
, rva
);
937 if (rva
+ len
<= (DWORD
)sec
->VirtualAddress
+ sec
->SizeOfRawData
)
938 return (void*)((LPBYTE
) base
+ (sec
->PointerToRawData
+ rva
- sec
->VirtualAddress
));
943 static LPWSTR
resource_dup_string( const IMAGE_RESOURCE_DIRECTORY
*root
, const IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
)
945 const IMAGE_RESOURCE_DIR_STRING_U
* string
;
948 if (!entry
->u1
.s1
.NameIsString
)
949 return UIntToPtr(entry
->u1
.s2
.Id
);
951 string
= (const IMAGE_RESOURCE_DIR_STRING_U
*) (((const char *)root
) + entry
->u1
.s1
.NameOffset
);
952 s
= HeapAlloc(GetProcessHeap(), 0, (string
->Length
+ 1)*sizeof (WCHAR
) );
953 memcpy( s
, string
->NameString
, (string
->Length
+ 1)*sizeof (WCHAR
) );
954 s
[string
->Length
] = 0;
959 /* this function is based on the code in winedump's pe.c */
960 static BOOL
enumerate_mapped_resources( QUEUEDUPDATES
*updates
,
961 void *base
, DWORD mapping_size
,
962 const IMAGE_RESOURCE_DIRECTORY
*root
)
964 const IMAGE_RESOURCE_DIRECTORY
*namedir
, *langdir
;
965 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*e1
, *e2
, *e3
;
966 const IMAGE_RESOURCE_DATA_ENTRY
*data
;
969 TRACE("version (%d.%d) %d named %d id entries\n",
970 root
->MajorVersion
, root
->MinorVersion
, root
->NumberOfNamedEntries
, root
->NumberOfIdEntries
);
972 for (i
= 0; i
< root
->NumberOfNamedEntries
+ root
->NumberOfIdEntries
; i
++)
976 e1
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(root
+ 1) + i
;
978 Type
= resource_dup_string( root
, e1
);
980 namedir
= (const IMAGE_RESOURCE_DIRECTORY
*)((const char *)root
+ e1
->u2
.s3
.OffsetToDirectory
);
981 for (j
= 0; j
< namedir
->NumberOfNamedEntries
+ namedir
->NumberOfIdEntries
; j
++)
985 e2
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(namedir
+ 1) + j
;
987 Name
= resource_dup_string( root
, e2
);
989 langdir
= (const IMAGE_RESOURCE_DIRECTORY
*)((const char *)root
+ e2
->u2
.s3
.OffsetToDirectory
);
990 for (k
= 0; k
< langdir
->NumberOfNamedEntries
+ langdir
->NumberOfIdEntries
; k
++)
994 struct resource_data
*resdata
;
996 e3
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(langdir
+ 1) + k
;
1000 data
= (const IMAGE_RESOURCE_DATA_ENTRY
*)((const char *)root
+ e3
->u2
.OffsetToData
);
1002 p
= address_from_rva( base
, mapping_size
, data
->OffsetToData
, data
->Size
);
1004 resdata
= allocate_resource_data( Lang
, data
->CodePage
, p
, data
->Size
, FALSE
);
1007 if (!update_add_resource( updates
, Type
, Name
, resdata
, FALSE
))
1008 HeapFree( GetProcessHeap(), 0, resdata
);
1011 res_free_str( Name
);
1013 res_free_str( Type
);
1019 static BOOL
read_mapped_resources( QUEUEDUPDATES
*updates
, void *base
, DWORD mapping_size
)
1021 const IMAGE_RESOURCE_DIRECTORY
*root
;
1022 const IMAGE_NT_HEADERS
*nt
;
1023 const IMAGE_SECTION_HEADER
*sec
;
1024 DWORD num_sections
= 0, i
;
1026 nt
= get_nt_header( base
, mapping_size
);
1030 sec
= get_section_header( base
, mapping_size
, &num_sections
);
1034 for (i
=0; i
<num_sections
; i
++)
1035 if (!memcmp(sec
[i
].Name
, ".rsrc", 6))
1038 if (i
== num_sections
)
1041 /* check the resource data is inside the mapping */
1042 if (sec
[i
].PointerToRawData
> mapping_size
||
1043 (sec
[i
].PointerToRawData
+ sec
[i
].SizeOfRawData
) > mapping_size
)
1046 TRACE("found .rsrc at %08x, size %08x\n", sec
[i
].PointerToRawData
, sec
[i
].SizeOfRawData
);
1048 if (!sec
[i
].PointerToRawData
|| sec
[i
].SizeOfRawData
< sizeof(IMAGE_RESOURCE_DIRECTORY
))
1051 root
= (void*) ((BYTE
*)base
+ sec
[i
].PointerToRawData
);
1052 enumerate_mapped_resources( updates
, base
, mapping_size
, root
);
1057 static BOOL
map_file_into_memory( struct mapping_info
*mi
)
1059 DWORD page_attr
, perm
;
1063 page_attr
= PAGE_READWRITE
;
1064 perm
= FILE_MAP_WRITE
| FILE_MAP_READ
;
1068 page_attr
= PAGE_READONLY
;
1069 perm
= FILE_MAP_READ
;
1072 mi
->mapping
= CreateFileMappingW( mi
->file
, NULL
, page_attr
, 0, 0, NULL
);
1076 mi
->base
= MapViewOfFile( mi
->mapping
, perm
, 0, 0, mi
->size
);
1083 static BOOL
unmap_file_from_memory( struct mapping_info
*mi
)
1086 UnmapViewOfFile( mi
->base
);
1089 CloseHandle( mi
->mapping
);
1094 static void destroy_mapping( struct mapping_info
*mi
)
1098 unmap_file_from_memory( mi
);
1100 CloseHandle( mi
->file
);
1101 HeapFree( GetProcessHeap(), 0, mi
);
1104 static struct mapping_info
*create_mapping( LPCWSTR name
, BOOL rw
)
1106 struct mapping_info
*mi
;
1108 mi
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof *mi
);
1112 mi
->read_write
= rw
;
1114 mi
->file
= CreateFileW( name
, GENERIC_READ
| (rw
? GENERIC_WRITE
: 0),
1115 0, NULL
, OPEN_EXISTING
, 0, 0 );
1117 if (mi
->file
!= INVALID_HANDLE_VALUE
)
1119 mi
->size
= GetFileSize( mi
->file
, NULL
);
1121 if (map_file_into_memory( mi
))
1125 unmap_file_from_memory( mi
);
1126 HeapFree( GetProcessHeap(), 0, mi
);
1131 static BOOL
resize_mapping( struct mapping_info
*mi
, DWORD new_size
)
1133 if (!unmap_file_from_memory( mi
))
1136 /* change the file size */
1137 SetFilePointer( mi
->file
, new_size
, NULL
, FILE_BEGIN
);
1138 if (!SetEndOfFile( mi
->file
))
1140 ERR("failed to set file size to %08x\n", new_size
);
1144 mi
->size
= new_size
;
1146 return map_file_into_memory( mi
);
1149 static void get_resource_sizes( QUEUEDUPDATES
*updates
, struct resource_size_info
*si
)
1151 struct resource_dir_entry
*types
, *names
;
1152 struct resource_data
*data
;
1153 DWORD num_types
= 0, num_names
= 0, num_langs
= 0, strings_size
= 0, data_size
= 0;
1155 memset( si
, 0, sizeof *si
);
1157 LIST_FOR_EACH_ENTRY( types
, &updates
->root
, struct resource_dir_entry
, entry
)
1160 if (HIWORD( types
->id
))
1161 strings_size
+= sizeof (WORD
) + lstrlenW( types
->id
)*sizeof (WCHAR
);
1163 LIST_FOR_EACH_ENTRY( names
, &types
->children
, struct resource_dir_entry
, entry
)
1167 if (HIWORD( names
->id
))
1168 strings_size
+= sizeof (WORD
) + lstrlenW( names
->id
)*sizeof (WCHAR
);
1170 LIST_FOR_EACH_ENTRY( data
, &names
->children
, struct resource_data
, entry
)
1173 data_size
+= (data
->cbData
+ 3) & ~3;
1178 /* names are at the end of the types */
1179 si
->names_ofs
= sizeof (IMAGE_RESOURCE_DIRECTORY
) +
1180 num_types
* sizeof (IMAGE_RESOURCE_DIRECTORY_ENTRY
);
1182 /* language directories are at the end of the names */
1183 si
->langs_ofs
= si
->names_ofs
+
1184 num_types
* sizeof (IMAGE_RESOURCE_DIRECTORY
) +
1185 num_names
* sizeof (IMAGE_RESOURCE_DIRECTORY_ENTRY
);
1187 si
->data_entry_ofs
= si
->langs_ofs
+
1188 num_names
* sizeof (IMAGE_RESOURCE_DIRECTORY
) +
1189 num_langs
* sizeof (IMAGE_RESOURCE_DIRECTORY_ENTRY
);
1191 si
->strings_ofs
= si
->data_entry_ofs
+
1192 num_langs
* sizeof (IMAGE_RESOURCE_DATA_ENTRY
);
1194 si
->data_ofs
= si
->strings_ofs
+ ((strings_size
+ 3) & ~3);
1196 si
->total_size
= si
->data_ofs
+ data_size
;
1198 TRACE("names %08x langs %08x data entries %08x strings %08x data %08x total %08x\n",
1199 si
->names_ofs
, si
->langs_ofs
, si
->data_entry_ofs
,
1200 si
->strings_ofs
, si
->data_ofs
, si
->total_size
);
1203 static void res_write_padding( BYTE
*res_base
, DWORD size
)
1205 static const BYTE pad
[] = {
1206 'P','A','D','D','I','N','G','X','X','P','A','D','D','I','N','G' };
1209 for ( i
= 0; i
< size
/ sizeof pad
; i
++ )
1210 memcpy( &res_base
[i
*sizeof pad
], pad
, sizeof pad
);
1211 memcpy( &res_base
[i
*sizeof pad
], pad
, size
%sizeof pad
);
1214 static BOOL
write_resources( QUEUEDUPDATES
*updates
, LPBYTE base
, struct resource_size_info
*si
, DWORD rva
)
1216 struct resource_dir_entry
*types
, *names
;
1217 struct resource_data
*data
;
1218 IMAGE_RESOURCE_DIRECTORY
*root
;
1220 TRACE("%p %p %p %08x\n", updates
, base
, si
, rva
);
1222 memset( base
, 0, si
->total_size
);
1224 /* the root entry always exists */
1225 root
= (IMAGE_RESOURCE_DIRECTORY
*) base
;
1226 memset( root
, 0, sizeof *root
);
1227 root
->MajorVersion
= 4;
1228 si
->types_ofs
= sizeof *root
;
1229 LIST_FOR_EACH_ENTRY( types
, &updates
->root
, struct resource_dir_entry
, entry
)
1231 IMAGE_RESOURCE_DIRECTORY_ENTRY
*e1
;
1232 IMAGE_RESOURCE_DIRECTORY
*namedir
;
1234 e1
= (IMAGE_RESOURCE_DIRECTORY_ENTRY
*) &base
[si
->types_ofs
];
1235 memset( e1
, 0, sizeof *e1
);
1236 if (HIWORD( types
->id
))
1241 root
->NumberOfNamedEntries
++;
1242 e1
->u1
.s1
.NameIsString
= 1;
1243 e1
->u1
.s1
.NameOffset
= si
->strings_ofs
;
1245 strings
= (WCHAR
*) &base
[si
->strings_ofs
];
1246 len
= lstrlenW( types
->id
);
1248 memcpy( &strings
[1], types
->id
, len
* sizeof (WCHAR
) );
1249 si
->strings_ofs
+= (len
+ 1) * sizeof (WCHAR
);
1253 root
->NumberOfIdEntries
++;
1254 e1
->u1
.s2
.Id
= LOWORD( types
->id
);
1256 e1
->u2
.s3
.OffsetToDirectory
= si
->names_ofs
;
1257 e1
->u2
.s3
.DataIsDirectory
= TRUE
;
1258 si
->types_ofs
+= sizeof (IMAGE_RESOURCE_DIRECTORY_ENTRY
);
1260 namedir
= (IMAGE_RESOURCE_DIRECTORY
*) &base
[si
->names_ofs
];
1261 memset( namedir
, 0, sizeof *namedir
);
1262 namedir
->MajorVersion
= 4;
1263 si
->names_ofs
+= sizeof (IMAGE_RESOURCE_DIRECTORY
);
1265 LIST_FOR_EACH_ENTRY( names
, &types
->children
, struct resource_dir_entry
, entry
)
1267 IMAGE_RESOURCE_DIRECTORY_ENTRY
*e2
;
1268 IMAGE_RESOURCE_DIRECTORY
*langdir
;
1270 e2
= (IMAGE_RESOURCE_DIRECTORY_ENTRY
*) &base
[si
->names_ofs
];
1271 memset( e2
, 0, sizeof *e2
);
1272 if (HIWORD( names
->id
))
1277 namedir
->NumberOfNamedEntries
++;
1278 e2
->u1
.s1
.NameIsString
= 1;
1279 e2
->u1
.s1
.NameOffset
= si
->strings_ofs
;
1281 strings
= (WCHAR
*) &base
[si
->strings_ofs
];
1282 len
= lstrlenW( names
->id
);
1284 memcpy( &strings
[1], names
->id
, len
* sizeof (WCHAR
) );
1285 si
->strings_ofs
+= (len
+ 1) * sizeof (WCHAR
);
1289 namedir
->NumberOfIdEntries
++;
1290 e2
->u1
.s2
.Id
= LOWORD( names
->id
);
1292 e2
->u2
.s3
.OffsetToDirectory
= si
->langs_ofs
;
1293 e2
->u2
.s3
.DataIsDirectory
= TRUE
;
1294 si
->names_ofs
+= sizeof (IMAGE_RESOURCE_DIRECTORY_ENTRY
);
1296 langdir
= (IMAGE_RESOURCE_DIRECTORY
*) &base
[si
->langs_ofs
];
1297 memset( langdir
, 0, sizeof *langdir
);
1298 langdir
->MajorVersion
= 4;
1299 si
->langs_ofs
+= sizeof (IMAGE_RESOURCE_DIRECTORY
);
1301 LIST_FOR_EACH_ENTRY( data
, &names
->children
, struct resource_data
, entry
)
1303 IMAGE_RESOURCE_DIRECTORY_ENTRY
*e3
;
1304 IMAGE_RESOURCE_DATA_ENTRY
*de
;
1307 e3
= (IMAGE_RESOURCE_DIRECTORY_ENTRY
*) &base
[si
->langs_ofs
];
1308 memset( e3
, 0, sizeof *e3
);
1309 langdir
->NumberOfIdEntries
++;
1310 e3
->u1
.s2
.Id
= LOWORD( data
->lang
);
1311 e3
->u2
.OffsetToData
= si
->data_entry_ofs
;
1313 si
->langs_ofs
+= sizeof (IMAGE_RESOURCE_DIRECTORY_ENTRY
);
1315 /* write out all the data entries */
1316 de
= (IMAGE_RESOURCE_DATA_ENTRY
*) &base
[si
->data_entry_ofs
];
1317 memset( de
, 0, sizeof *de
);
1318 de
->OffsetToData
= si
->data_ofs
+ rva
;
1319 de
->Size
= data
->cbData
;
1320 de
->CodePage
= data
->codepage
;
1321 si
->data_entry_ofs
+= sizeof (IMAGE_RESOURCE_DATA_ENTRY
);
1323 /* write out the resource data */
1324 memcpy( &base
[si
->data_ofs
], data
->lpData
, data
->cbData
);
1325 si
->data_ofs
+= data
->cbData
;
1327 pad_size
= (-si
->data_ofs
)&3;
1328 res_write_padding( &base
[si
->data_ofs
], pad_size
);
1329 si
->data_ofs
+= pad_size
;
1339 * Assumes that the resources are in .rsrc
1340 * and .rsrc is the last section in the file.
1341 * Not sure whether updating resources will other cases on Windows.
1342 * If the resources lie in a section containing other data,
1343 * resizing that section could possibly cause trouble.
1344 * If the section with the resources isn't last, the remaining
1345 * sections need to be moved down in the file, and the section header
1346 * would need to be adjusted.
1347 * If we needed to add a section, what would we name it?
1348 * If we needed to add a section and there wasn't space in the file
1349 * header, how would that work?
1350 * Seems that at least some of these cases can't be handled properly.
1352 static IMAGE_SECTION_HEADER
*get_resource_section( void *base
, DWORD mapping_size
)
1354 IMAGE_SECTION_HEADER
*sec
;
1355 IMAGE_NT_HEADERS
*nt
;
1356 DWORD i
, num_sections
= 0;
1358 nt
= get_nt_header( base
, mapping_size
);
1362 sec
= get_section_header( base
, mapping_size
, &num_sections
);
1366 /* find the resources section */
1367 for (i
=0; i
<num_sections
; i
++)
1368 if (!memcmp(sec
[i
].Name
, ".rsrc", 6))
1371 if (i
== num_sections
)
1373 FIXME(".rsrc doesn't exist\n");
1380 static DWORD
get_init_data_size( void *base
, DWORD mapping_size
)
1382 DWORD i
, sz
= 0, num_sections
= 0;
1383 IMAGE_SECTION_HEADER
*s
;
1385 s
= get_section_header( base
, mapping_size
, &num_sections
);
1387 for (i
=0; i
<num_sections
; i
++)
1388 if (s
[i
].Characteristics
& IMAGE_SCN_CNT_INITIALIZED_DATA
)
1389 sz
+= s
[i
].SizeOfRawData
;
1391 TRACE("size = %08x\n", sz
);
1396 static BOOL
write_raw_resources( QUEUEDUPDATES
*updates
)
1398 static const WCHAR prefix
[] = { 'r','e','s','u',0 };
1399 WCHAR tempdir
[MAX_PATH
], tempfile
[MAX_PATH
];
1400 DWORD mapping_size
, section_size
, old_size
;
1402 IMAGE_SECTION_HEADER
*sec
;
1403 IMAGE_NT_HEADERS
*nt
;
1404 struct resource_size_info res_size
;
1406 struct mapping_info
*read_map
= NULL
, *write_map
= NULL
;
1408 /* copy the exe to a temp file then update the temp file... */
1410 if (!GetTempPathW( MAX_PATH
, tempdir
))
1413 if (!GetTempFileNameW( tempdir
, prefix
, 0, tempfile
))
1416 if (!CopyFileW( updates
->pFileName
, tempfile
, FALSE
))
1419 TRACE("tempfile %s\n", debugstr_w(tempfile
));
1421 if (!updates
->bDeleteExistingResources
)
1423 read_map
= create_mapping( updates
->pFileName
, FALSE
);
1427 ret
= read_mapped_resources( updates
, read_map
->base
, read_map
->size
);
1430 ERR("failed to read existing resources\n");
1435 write_map
= create_mapping( tempfile
, TRUE
);
1439 nt
= get_nt_header( write_map
->base
, write_map
->size
);
1443 if (nt
->OptionalHeader
.SectionAlignment
<= 0)
1445 ERR("invalid section alignment %04x\n", nt
->OptionalHeader
.SectionAlignment
);
1449 sec
= get_resource_section( write_map
->base
, write_map
->size
);
1453 if (!sec
->PointerToRawData
) /* empty section */
1455 sec
->PointerToRawData
= write_map
->size
;
1456 sec
->SizeOfRawData
= 0;
1458 else if ((sec
->SizeOfRawData
+ sec
->PointerToRawData
) != write_map
->size
)
1460 FIXME(".rsrc isn't at the end of the image %08x + %08x != %08x for %s\n",
1461 sec
->SizeOfRawData
, sec
->PointerToRawData
, write_map
->size
, debugstr_w(updates
->pFileName
));
1465 TRACE("before .rsrc at %08x, size %08x\n", sec
->PointerToRawData
, sec
->SizeOfRawData
);
1467 get_resource_sizes( updates
, &res_size
);
1469 /* round up the section size */
1470 section_size
= res_size
.total_size
;
1471 section_size
+= (-section_size
) % nt
->OptionalHeader
.SectionAlignment
;
1473 mapping_size
= sec
->PointerToRawData
+ section_size
;
1475 TRACE("requires %08x (%08x) bytes\n", res_size
.total_size
, section_size
);
1477 /* check if the file size needs to be changed */
1478 if (section_size
!= sec
->SizeOfRawData
)
1480 old_size
= write_map
->size
;
1482 TRACE("file size %08x -> %08x\n", old_size
, mapping_size
);
1484 /* unmap the file before changing the file size */
1485 ret
= resize_mapping( write_map
, mapping_size
);
1487 /* get the pointers again - they might be different after remapping */
1488 nt
= get_nt_header( write_map
->base
, mapping_size
);
1491 ERR("couldn't get NT header\n");
1495 sec
= get_resource_section( write_map
->base
, mapping_size
);
1499 /* adjust the PE header information */
1500 nt
->OptionalHeader
.SizeOfImage
+= (mapping_size
- old_size
);
1501 sec
->SizeOfRawData
= section_size
;
1502 sec
->Misc
.VirtualSize
= section_size
;
1503 nt
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_RESOURCE
].Size
= res_size
.total_size
;
1504 nt
->OptionalHeader
.SizeOfInitializedData
= get_init_data_size( write_map
->base
, mapping_size
);
1507 res_base
= (LPBYTE
) write_map
->base
+ sec
->PointerToRawData
;
1509 TRACE("base = %p offset = %08x\n", write_map
->base
, sec
->PointerToRawData
);
1511 ret
= write_resources( updates
, res_base
, &res_size
, sec
->VirtualAddress
);
1513 res_write_padding( res_base
+ res_size
.total_size
, section_size
- res_size
.total_size
);
1515 TRACE("after .rsrc at %08x, size %08x\n", sec
->PointerToRawData
, sec
->SizeOfRawData
);
1518 destroy_mapping( read_map
);
1519 destroy_mapping( write_map
);
1522 ret
= CopyFileW( tempfile
, updates
->pFileName
, FALSE
);
1524 DeleteFileW( tempfile
);
1529 /***********************************************************************
1530 * BeginUpdateResourceW (KERNEL32.@)
1532 HANDLE WINAPI
BeginUpdateResourceW( LPCWSTR pFileName
, BOOL bDeleteExistingResources
)
1534 QUEUEDUPDATES
*updates
= NULL
;
1535 HANDLE hUpdate
, file
, ret
= NULL
;
1537 TRACE("%s, %d\n", debugstr_w(pFileName
), bDeleteExistingResources
);
1539 hUpdate
= GlobalAlloc(GHND
, sizeof(QUEUEDUPDATES
));
1543 updates
= GlobalLock(hUpdate
);
1546 list_init( &updates
->root
);
1547 updates
->bDeleteExistingResources
= bDeleteExistingResources
;
1548 updates
->pFileName
= HeapAlloc(GetProcessHeap(), 0, (lstrlenW(pFileName
)+1)*sizeof(WCHAR
));
1549 if (updates
->pFileName
)
1551 lstrcpyW(updates
->pFileName
, pFileName
);
1553 file
= CreateFileW( pFileName
, GENERIC_READ
| GENERIC_WRITE
,
1554 0, NULL
, OPEN_EXISTING
, 0, 0 );
1556 /* if resources are deleted, only the file's presence is checked */
1557 if (file
!= INVALID_HANDLE_VALUE
&&
1558 (bDeleteExistingResources
|| check_pe_exe( file
, updates
)))
1561 HeapFree( GetProcessHeap(), 0, updates
->pFileName
);
1563 CloseHandle( file
);
1565 GlobalUnlock(hUpdate
);
1569 GlobalFree(hUpdate
);
1575 /***********************************************************************
1576 * BeginUpdateResourceA (KERNEL32.@)
1578 HANDLE WINAPI
BeginUpdateResourceA( LPCSTR pFileName
, BOOL bDeleteExistingResources
)
1580 UNICODE_STRING FileNameW
;
1582 RtlCreateUnicodeStringFromAsciiz(&FileNameW
, pFileName
);
1583 ret
= BeginUpdateResourceW(FileNameW
.Buffer
, bDeleteExistingResources
);
1584 RtlFreeUnicodeString(&FileNameW
);
1589 /***********************************************************************
1590 * EndUpdateResourceW (KERNEL32.@)
1592 BOOL WINAPI
EndUpdateResourceW( HANDLE hUpdate
, BOOL fDiscard
)
1594 QUEUEDUPDATES
*updates
;
1597 TRACE("%p %d\n", hUpdate
, fDiscard
);
1599 updates
= GlobalLock(hUpdate
);
1603 ret
= fDiscard
|| write_raw_resources( updates
);
1605 free_resource_directory( &updates
->root
, 2 );
1607 HeapFree( GetProcessHeap(), 0, updates
->pFileName
);
1608 GlobalUnlock( hUpdate
);
1609 GlobalFree( hUpdate
);
1615 /***********************************************************************
1616 * EndUpdateResourceA (KERNEL32.@)
1618 BOOL WINAPI
EndUpdateResourceA( HANDLE hUpdate
, BOOL fDiscard
)
1620 return EndUpdateResourceW(hUpdate
, fDiscard
);
1624 /***********************************************************************
1625 * UpdateResourceW (KERNEL32.@)
1627 BOOL WINAPI
UpdateResourceW( HANDLE hUpdate
, LPCWSTR lpType
, LPCWSTR lpName
,
1628 WORD wLanguage
, LPVOID lpData
, DWORD cbData
)
1630 QUEUEDUPDATES
*updates
;
1633 TRACE("%p %s %s %08x %p %d\n", hUpdate
,
1634 debugstr_w(lpType
), debugstr_w(lpName
), wLanguage
, lpData
, cbData
);
1636 updates
= GlobalLock(hUpdate
);
1639 struct resource_data
*data
;
1640 data
= allocate_resource_data( wLanguage
, 0, lpData
, cbData
, TRUE
);
1642 ret
= update_add_resource( updates
, lpType
, lpName
, data
, TRUE
);
1643 GlobalUnlock(hUpdate
);
1649 /***********************************************************************
1650 * UpdateResourceA (KERNEL32.@)
1652 BOOL WINAPI
UpdateResourceA( HANDLE hUpdate
, LPCSTR lpType
, LPCSTR lpName
,
1653 WORD wLanguage
, LPVOID lpData
, DWORD cbData
)
1656 UNICODE_STRING TypeW
;
1657 UNICODE_STRING NameW
;
1659 TypeW
.Buffer
= ULongToPtr(LOWORD(lpType
));
1661 RtlCreateUnicodeStringFromAsciiz(&TypeW
, lpType
);
1663 NameW
.Buffer
= ULongToPtr(LOWORD(lpName
));
1665 RtlCreateUnicodeStringFromAsciiz(&NameW
, lpName
);
1666 ret
= UpdateResourceW(hUpdate
, TypeW
.Buffer
, NameW
.Buffer
, wLanguage
, lpData
, cbData
);
1667 if(HIWORD(lpType
)) RtlFreeUnicodeString(&TypeW
);
1668 if(HIWORD(lpName
)) RtlFreeUnicodeString(&NameW
);