4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995, 2003 Alexandre Julliard
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
30 #define WIN32_NO_STATUS
35 #include "wine/winbase16.h"
36 #include "wine/debug.h"
38 #include "wine/exception.h"
39 #include "wine/unicode.h"
40 #include "wine/list.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(resource);
44 /* handle conversions */
45 #define HRSRC_32(h16) ((HRSRC)(ULONG_PTR)(h16))
46 #define HRSRC_16(h32) (LOWORD(h32))
47 #define HGLOBAL_32(h16) ((HGLOBAL)(ULONG_PTR)(h16))
48 #define HGLOBAL_16(h32) (LOWORD(h32))
49 #define HMODULE_16(h32) (LOWORD(h32))
51 /* retrieve the resource name to pass to the ntdll functions */
52 static NTSTATUS get_res_nameA( LPCSTR name, UNICODE_STRING *str )
56 str->Buffer = (LPWSTR)name;
57 return STATUS_SUCCESS;
62 if (RtlCharToInteger( name + 1, 10, &value ) != STATUS_SUCCESS || HIWORD(value))
63 return STATUS_INVALID_PARAMETER;
64 str->Buffer = (LPWSTR)value;
65 return STATUS_SUCCESS;
67 RtlCreateUnicodeStringFromAsciiz( str, name );
68 RtlUpcaseUnicodeString( str, str, FALSE );
69 return STATUS_SUCCESS;
72 /* retrieve the resource name to pass to the ntdll functions */
73 static NTSTATUS get_res_nameW( LPCWSTR name, UNICODE_STRING *str )
77 str->Buffer = (LPWSTR)name;
78 return STATUS_SUCCESS;
83 RtlInitUnicodeString( str, name + 1 );
84 if (RtlUnicodeStringToInteger( str, 10, &value ) != STATUS_SUCCESS || HIWORD(value))
85 return STATUS_INVALID_PARAMETER;
86 str->Buffer = (LPWSTR)value;
87 return STATUS_SUCCESS;
89 RtlCreateUnicodeString( str, name );
90 RtlUpcaseUnicodeString( str, str, FALSE );
91 return STATUS_SUCCESS;
94 /* retrieve the resource names for the 16-bit FindResource function */
95 static BOOL get_res_name_type_WtoA( LPCWSTR name, LPCWSTR type, LPSTR *nameA, LPSTR *typeA )
97 *nameA = *typeA = NULL;
103 DWORD len = WideCharToMultiByte( CP_ACP, 0, name, -1, NULL, 0, NULL, NULL );
104 *nameA = HeapAlloc( GetProcessHeap(), 0, len );
105 if (*nameA) WideCharToMultiByte( CP_ACP, 0, name, -1, *nameA, len, NULL, NULL );
107 else *nameA = (LPSTR)name;
111 DWORD len = WideCharToMultiByte( CP_ACP, 0, type, -1, NULL, 0, NULL, NULL );
112 *typeA = HeapAlloc( GetProcessHeap(), 0, len );
113 if (*typeA) WideCharToMultiByte( CP_ACP, 0, type, -1, *typeA, len, NULL, NULL );
115 else *typeA = (LPSTR)type;
119 if (HIWORD(*nameA)) HeapFree( GetProcessHeap(), 0, *nameA );
120 if (HIWORD(*typeA)) HeapFree( GetProcessHeap(), 0, *typeA );
121 SetLastError( ERROR_INVALID_PARAMETER );
128 /* implementation of FindResourceExA */
129 static HRSRC find_resourceA( HMODULE hModule, LPCSTR type, LPCSTR name, WORD lang )
132 UNICODE_STRING nameW, typeW;
133 LDR_RESOURCE_INFO info;
134 const IMAGE_RESOURCE_DATA_ENTRY *entry = NULL;
138 if ((status = get_res_nameA( name, &nameW )) != STATUS_SUCCESS) goto done;
139 if ((status = get_res_nameA( type, &typeW )) != STATUS_SUCCESS) goto done;
140 info.Type = (ULONG_PTR)typeW.Buffer;
141 info.Name = (ULONG_PTR)nameW.Buffer;
142 info.Language = lang;
143 status = LdrFindResource_U( hModule, &info, 3, &entry );
145 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
149 SetLastError( ERROR_INVALID_PARAMETER );
153 if (HIWORD(nameW.Buffer)) HeapFree( GetProcessHeap(), 0, nameW.Buffer );
154 if (HIWORD(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
159 /* implementation of FindResourceExW */
160 static HRSRC find_resourceW( HMODULE hModule, LPCWSTR type, LPCWSTR name, WORD lang )
163 UNICODE_STRING nameW, typeW;
164 LDR_RESOURCE_INFO info;
165 const IMAGE_RESOURCE_DATA_ENTRY *entry = NULL;
167 nameW.Buffer = typeW.Buffer = NULL;
171 if ((status = get_res_nameW( name, &nameW )) != STATUS_SUCCESS) goto done;
172 if ((status = get_res_nameW( type, &typeW )) != STATUS_SUCCESS) goto done;
173 info.Type = (ULONG_PTR)typeW.Buffer;
174 info.Name = (ULONG_PTR)nameW.Buffer;
175 info.Language = lang;
176 status = LdrFindResource_U( hModule, &info, 3, &entry );
178 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
182 SetLastError( ERROR_INVALID_PARAMETER );
186 if (HIWORD(nameW.Buffer)) HeapFree( GetProcessHeap(), 0, nameW.Buffer );
187 if (HIWORD(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
191 /**********************************************************************
192 * FindResourceExA (KERNEL32.@)
194 HRSRC WINAPI FindResourceExA( HMODULE hModule, LPCSTR type, LPCSTR name, WORD lang )
196 TRACE( "%p %s %s %04x\n", hModule, debugstr_a(type), debugstr_a(name), lang );
198 if (!hModule) hModule = GetModuleHandleW(0);
199 else if (!HIWORD(hModule))
201 return HRSRC_32( FindResource16( HMODULE_16(hModule), name, type ) );
203 return find_resourceA( hModule, type, name, lang );
207 /**********************************************************************
208 * FindResourceA (KERNEL32.@)
210 HRSRC WINAPI FindResourceA( HMODULE hModule, LPCSTR name, LPCSTR type )
212 return FindResourceExA( hModule, type, name, MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ) );
216 /**********************************************************************
217 * FindResourceExW (KERNEL32.@)
219 HRSRC WINAPI FindResourceExW( HMODULE hModule, LPCWSTR type, LPCWSTR name, WORD lang )
221 TRACE( "%p %s %s %04x\n", hModule, debugstr_w(type), debugstr_w(name), lang );
223 if (!hModule) hModule = GetModuleHandleW(0);
224 else if (!HIWORD(hModule))
229 if (!get_res_name_type_WtoA( name, type, &nameA, &typeA )) return NULL;
231 ret = FindResource16( HMODULE_16(hModule), nameA, typeA );
232 if (HIWORD(nameA)) HeapFree( GetProcessHeap(), 0, nameA );
233 if (HIWORD(typeA)) HeapFree( GetProcessHeap(), 0, typeA );
234 return HRSRC_32(ret);
237 return find_resourceW( hModule, type, name, lang );
241 /**********************************************************************
242 * FindResourceW (KERNEL32.@)
244 HRSRC WINAPI FindResourceW( HINSTANCE hModule, LPCWSTR name, LPCWSTR type )
246 return FindResourceExW( hModule, type, name, MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ) );
250 /**********************************************************************
251 * EnumResourceTypesA (KERNEL32.@)
253 BOOL WINAPI EnumResourceTypesA( HMODULE hmod, ENUMRESTYPEPROCA lpfun, LONG_PTR lparam )
258 DWORD len = 0, newlen;
260 const IMAGE_RESOURCE_DIRECTORY *resdir;
261 const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
262 const IMAGE_RESOURCE_DIR_STRING_U *str;
264 TRACE( "%p %p %lx\n", hmod, lpfun, lparam );
266 if (!hmod) hmod = GetModuleHandleA( NULL );
268 if ((status = LdrFindResourceDirectory_U( hmod, NULL, 0, &resdir )) != STATUS_SUCCESS)
270 SetLastError( RtlNtStatusToDosError(status) );
273 et = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resdir + 1);
274 for (i = 0; i < resdir->NumberOfNamedEntries+resdir->NumberOfIdEntries; i++)
276 if (et[i].u1.s1.NameIsString)
278 str = (const IMAGE_RESOURCE_DIR_STRING_U *)((const BYTE *)resdir + et[i].u1.s1.NameOffset);
279 newlen = WideCharToMultiByte( CP_ACP, 0, str->NameString, str->Length, NULL, 0, NULL, NULL);
280 if (newlen + 1 > len)
283 HeapFree( GetProcessHeap(), 0, type );
284 if (!(type = HeapAlloc( GetProcessHeap(), 0, len ))) return FALSE;
286 WideCharToMultiByte( CP_ACP, 0, str->NameString, str->Length, type, len, NULL, NULL);
288 ret = lpfun(hmod,type,lparam);
292 ret = lpfun( hmod, (LPSTR)(int)et[i].u1.s2.Id, lparam );
296 HeapFree( GetProcessHeap(), 0, type );
301 /**********************************************************************
302 * EnumResourceTypesW (KERNEL32.@)
304 BOOL WINAPI EnumResourceTypesW( HMODULE hmod, ENUMRESTYPEPROCW lpfun, LONG_PTR lparam )
310 const IMAGE_RESOURCE_DIRECTORY *resdir;
311 const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
312 const IMAGE_RESOURCE_DIR_STRING_U *str;
314 TRACE( "%p %p %lx\n", hmod, lpfun, lparam );
316 if (!hmod) hmod = GetModuleHandleW( NULL );
318 if ((status = LdrFindResourceDirectory_U( hmod, NULL, 0, &resdir )) != STATUS_SUCCESS)
320 SetLastError( RtlNtStatusToDosError(status) );
323 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 *)resdir + et[i].u1.s1.NameOffset);
329 if (str->Length + 1 > len)
331 len = str->Length + 1;
332 HeapFree( GetProcessHeap(), 0, type );
333 if (!(type = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return FALSE;
335 memcpy(type, str->NameString, str->Length * sizeof (WCHAR));
336 type[str->Length] = 0;
337 ret = lpfun(hmod,type,lparam);
341 ret = lpfun( hmod, (LPWSTR)(int)et[i].u1.s2.Id, lparam );
345 HeapFree( GetProcessHeap(), 0, type );
350 /**********************************************************************
351 * EnumResourceNamesA (KERNEL32.@)
353 BOOL WINAPI EnumResourceNamesA( HMODULE hmod, LPCSTR type, ENUMRESNAMEPROCA lpfun, LONG_PTR lparam )
357 DWORD len = 0, newlen;
360 UNICODE_STRING typeW;
361 LDR_RESOURCE_INFO info;
362 const IMAGE_RESOURCE_DIRECTORY *basedir, *resdir;
363 const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
364 const IMAGE_RESOURCE_DIR_STRING_U *str;
366 TRACE( "%p %s %p %lx\n", hmod, debugstr_a(type), lpfun, lparam );
368 if (!hmod) hmod = GetModuleHandleA( NULL );
370 if ((status = LdrFindResourceDirectory_U( hmod, NULL, 0, &basedir )) != STATUS_SUCCESS)
372 if ((status = get_res_nameA( type, &typeW )) != STATUS_SUCCESS)
374 info.Type = (ULONG)typeW.Buffer;
375 if ((status = LdrFindResourceDirectory_U( hmod, &info, 1, &resdir )) != STATUS_SUCCESS)
378 et = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resdir + 1);
379 for (i = 0; i < resdir->NumberOfNamedEntries+resdir->NumberOfIdEntries; i++)
381 if (et[i].u1.s1.NameIsString)
383 str = (const IMAGE_RESOURCE_DIR_STRING_U *)((const BYTE *)basedir + et[i].u1.s1.NameOffset);
384 newlen = WideCharToMultiByte(CP_ACP, 0, str->NameString, str->Length, NULL, 0, NULL, NULL);
385 if (newlen + 1 > len)
388 HeapFree( GetProcessHeap(), 0, name );
389 if (!(name = HeapAlloc(GetProcessHeap(), 0, len + 1 )))
395 WideCharToMultiByte( CP_ACP, 0, str->NameString, str->Length, name, len, NULL, NULL );
397 ret = lpfun(hmod,type,name,lparam);
401 ret = lpfun( hmod, type, (LPSTR)(int)et[i].u1.s2.Id, lparam );
406 HeapFree( GetProcessHeap(), 0, name );
407 if (HIWORD(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
408 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
413 /**********************************************************************
414 * EnumResourceNamesW (KERNEL32.@)
416 BOOL WINAPI EnumResourceNamesW( HMODULE hmod, LPCWSTR type, ENUMRESNAMEPROCW lpfun, LONG_PTR lparam )
422 UNICODE_STRING typeW;
423 LDR_RESOURCE_INFO info;
424 const IMAGE_RESOURCE_DIRECTORY *basedir, *resdir;
425 const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
426 const IMAGE_RESOURCE_DIR_STRING_U *str;
428 TRACE( "%p %s %p %lx\n", hmod, debugstr_w(type), lpfun, lparam );
430 if (!hmod) hmod = GetModuleHandleW( NULL );
432 if ((status = LdrFindResourceDirectory_U( hmod, NULL, 0, &basedir )) != STATUS_SUCCESS)
434 if ((status = get_res_nameW( type, &typeW )) != STATUS_SUCCESS)
436 info.Type = (ULONG)typeW.Buffer;
437 if ((status = LdrFindResourceDirectory_U( hmod, &info, 1, &resdir )) != STATUS_SUCCESS)
440 et = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resdir + 1);
441 for (i = 0; i < resdir->NumberOfNamedEntries+resdir->NumberOfIdEntries; i++)
443 if (et[i].u1.s1.NameIsString)
445 str = (const IMAGE_RESOURCE_DIR_STRING_U *)((const BYTE *)basedir + et[i].u1.s1.NameOffset);
446 if (str->Length + 1 > len)
448 len = str->Length + 1;
449 HeapFree( GetProcessHeap(), 0, name );
450 if (!(name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
456 memcpy(name, str->NameString, str->Length * sizeof (WCHAR));
457 name[str->Length] = 0;
458 ret = lpfun(hmod,type,name,lparam);
462 ret = lpfun( hmod, type, (LPWSTR)(int)et[i].u1.s2.Id, lparam );
467 HeapFree( GetProcessHeap(), 0, name );
468 if (HIWORD(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
469 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
474 /**********************************************************************
475 * EnumResourceLanguagesA (KERNEL32.@)
477 BOOL WINAPI EnumResourceLanguagesA( HMODULE hmod, LPCSTR type, LPCSTR name,
478 ENUMRESLANGPROCA lpfun, LONG_PTR lparam )
483 UNICODE_STRING typeW, nameW;
484 LDR_RESOURCE_INFO info;
485 const IMAGE_RESOURCE_DIRECTORY *basedir, *resdir;
486 const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
488 TRACE( "%p %s %s %p %lx\n", hmod, debugstr_a(type), debugstr_a(name), lpfun, lparam );
490 if (!hmod) hmod = GetModuleHandleA( NULL );
491 typeW.Buffer = nameW.Buffer = NULL;
492 if ((status = LdrFindResourceDirectory_U( hmod, NULL, 0, &basedir )) != STATUS_SUCCESS)
494 if ((status = get_res_nameA( type, &typeW )) != STATUS_SUCCESS)
496 if ((status = get_res_nameA( name, &nameW )) != STATUS_SUCCESS)
498 info.Type = (ULONG_PTR)typeW.Buffer;
499 info.Name = (ULONG_PTR)nameW.Buffer;
500 if ((status = LdrFindResourceDirectory_U( hmod, &info, 2, &resdir )) != STATUS_SUCCESS)
503 et = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resdir + 1);
504 for (i = 0; i < resdir->NumberOfNamedEntries + resdir->NumberOfIdEntries; i++)
506 ret = lpfun( hmod, type, name, et[i].u1.s2.Id, lparam );
510 if (HIWORD(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
511 if (HIWORD(nameW.Buffer)) HeapFree( GetProcessHeap(), 0, nameW.Buffer );
512 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
517 /**********************************************************************
518 * EnumResourceLanguagesW (KERNEL32.@)
520 BOOL WINAPI EnumResourceLanguagesW( HMODULE hmod, LPCWSTR type, LPCWSTR name,
521 ENUMRESLANGPROCW lpfun, LONG_PTR lparam )
526 UNICODE_STRING typeW, nameW;
527 LDR_RESOURCE_INFO info;
528 const IMAGE_RESOURCE_DIRECTORY *basedir, *resdir;
529 const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
531 TRACE( "%p %s %s %p %lx\n", hmod, debugstr_w(type), debugstr_w(name), lpfun, lparam );
533 if (!hmod) hmod = GetModuleHandleW( NULL );
534 typeW.Buffer = nameW.Buffer = NULL;
535 if ((status = LdrFindResourceDirectory_U( hmod, NULL, 0, &basedir )) != STATUS_SUCCESS)
537 if ((status = get_res_nameW( type, &typeW )) != STATUS_SUCCESS)
539 if ((status = get_res_nameW( name, &nameW )) != STATUS_SUCCESS)
541 info.Type = (ULONG_PTR)typeW.Buffer;
542 info.Name = (ULONG_PTR)nameW.Buffer;
543 if ((status = LdrFindResourceDirectory_U( hmod, &info, 2, &resdir )) != STATUS_SUCCESS)
546 et = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resdir + 1);
547 for (i = 0; i < resdir->NumberOfNamedEntries + resdir->NumberOfIdEntries; i++)
549 ret = lpfun( hmod, type, name, et[i].u1.s2.Id, lparam );
553 if (HIWORD(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
554 if (HIWORD(nameW.Buffer)) HeapFree( GetProcessHeap(), 0, nameW.Buffer );
555 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
560 /**********************************************************************
561 * LoadResource (KERNEL32.@)
563 HGLOBAL WINAPI LoadResource( HINSTANCE hModule, HRSRC hRsrc )
568 TRACE( "%p %p\n", hModule, hRsrc );
570 if (hModule && !HIWORD(hModule))
571 /* FIXME: should convert return to 32-bit resource */
572 return HGLOBAL_32( LoadResource16( HMODULE_16(hModule), HRSRC_16(hRsrc) ) );
574 if (!hRsrc) return 0;
575 if (!hModule) hModule = GetModuleHandleA( NULL );
576 status = LdrAccessResource( hModule, (IMAGE_RESOURCE_DATA_ENTRY *)hRsrc, &ret, NULL );
577 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
582 /**********************************************************************
583 * LockResource (KERNEL32.@)
585 LPVOID WINAPI LockResource( HGLOBAL handle )
587 TRACE("(%p)\n", handle );
589 if (HIWORD( handle )) /* 32-bit memory handle */
590 return (LPVOID)handle;
592 /* 16-bit memory handle */
593 return LockResource16( HGLOBAL_16(handle) );
597 /**********************************************************************
598 * FreeResource (KERNEL32.@)
600 BOOL WINAPI FreeResource( HGLOBAL handle )
602 if (HIWORD(handle)) return 0; /* 32-bit memory handle: nothing to do */
603 return FreeResource16( HGLOBAL_16(handle) );
607 /**********************************************************************
608 * SizeofResource (KERNEL32.@)
610 DWORD WINAPI SizeofResource( HINSTANCE hModule, HRSRC hRsrc )
612 if (hModule && !HIWORD(hModule))
613 return SizeofResource16( HMODULE_16(hModule), HRSRC_16(hRsrc) );
615 if (!hRsrc) return 0;
616 return ((PIMAGE_RESOURCE_DATA_ENTRY)hRsrc)->Size;
620 * Data structure for updating resources.
621 * Type/Name/Language is a keyset for accessing resource data.
623 * QUEUEDUPDATES (root) ->
624 * list of struct resouce_dir_entry (Type) ->
625 * list of struct resouce_dir_entry (Name) ->
626 * list of struct resouce_data Language + Data
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 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 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 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 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 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 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 void res_free_str( LPWSTR str )
735 HeapFree( GetProcessHeap(), 0, str );
738 BOOL update_add_resource( QUEUEDUPDATES *updates, LPCWSTR Type, LPCWSTR Name,
739 WORD Language, DWORD codepage, LPCVOID lpData, DWORD cbData )
741 struct resource_dir_entry *restype, *resname;
742 struct resource_data *resdata;
744 TRACE("%p %s %s %04x %04x %p %d bytes\n", updates, debugstr_w(Type), debugstr_w(Name), Language, codepage, lpData, cbData);
746 if (!lpData || !cbData)
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 resdata = find_resource_data( &resname->children, Language );
774 list_remove( &resdata->entry );
775 HeapFree( GetProcessHeap(), 0, resdata );
778 resdata = HeapAlloc( GetProcessHeap(), 0, sizeof *resdata + cbData );
779 resdata->lang = Language;
780 resdata->codepage = codepage;
781 resdata->cbData = cbData;
782 memcpy( resdata->data, lpData, cbData );
784 add_resource_data_entry( &resname->children, resdata );
789 void free_resource_directory( struct list *head, int level )
791 struct list *ptr = NULL;
793 while ((ptr = list_head( head )))
798 struct resource_dir_entry *ent;
800 ent = LIST_ENTRY( ptr, struct resource_dir_entry, entry );
801 res_free_str( ent->id );
802 free_resource_directory( &ent->children, level - 1 );
803 HeapFree(GetProcessHeap(), 0, ent);
807 struct resource_data *data;
809 data = LIST_ENTRY( ptr, struct resource_data, entry );
810 HeapFree( GetProcessHeap(), 0, data );
815 IMAGE_NT_HEADERS *get_nt_header( void *base, DWORD mapping_size )
817 IMAGE_NT_HEADERS *nt;
818 IMAGE_DOS_HEADER *dos;
820 if (mapping_size<sizeof (*dos))
824 if (dos->e_magic != IMAGE_DOS_SIGNATURE)
827 if ((dos->e_lfanew + sizeof (*nt)) > mapping_size)
830 nt = (void*) ((BYTE*)base + dos->e_lfanew);
832 if (nt->Signature != IMAGE_NT_SIGNATURE)
838 IMAGE_SECTION_HEADER *get_section_header( void *base, DWORD mapping_size, DWORD *num_sections )
840 IMAGE_NT_HEADERS *nt;
841 IMAGE_SECTION_HEADER *sec;
844 nt = get_nt_header( base, mapping_size );
848 /* check that we don't go over the end of the file accessing the sections */
849 section_ofs = FIELD_OFFSET(IMAGE_NT_HEADERS, OptionalHeader) + nt->FileHeader.SizeOfOptionalHeader;
850 if ((nt->FileHeader.NumberOfSections * sizeof (*sec) + section_ofs) > mapping_size)
854 *num_sections = nt->FileHeader.NumberOfSections;
856 /* from here we have a valid PE exe to update */
857 return (void*) ((BYTE*)nt + section_ofs);
860 static BOOL load_raw_resources( HANDLE file, QUEUEDUPDATES *updates )
862 const IMAGE_NT_HEADERS *nt;
863 const IMAGE_SECTION_HEADER *sec;
866 DWORD mapping_size, num_sections = 0;
869 mapping_size = GetFileSize( file, NULL );
871 mapping = CreateFileMappingW( file, NULL, PAGE_READONLY, 0, 0, NULL );
875 base = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, mapping_size );
879 nt = get_nt_header( base, mapping_size );
883 TRACE("resources: %08x %08x\n",
884 nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress,
885 nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].Size);
887 sec = get_section_header( base, mapping_size, &num_sections );
893 FIXME("not implemented\n");
897 UnmapViewOfFile( base );
899 CloseHandle( mapping );
904 BOOL write_raw_resources( QUEUEDUPDATES *updates )
906 FIXME("not implemented\n");
910 /***********************************************************************
911 * BeginUpdateResourceW (KERNEL32.@)
913 HANDLE WINAPI BeginUpdateResourceW( LPCWSTR pFileName, BOOL bDeleteExistingResources )
915 QUEUEDUPDATES *updates = NULL;
916 HANDLE hUpdate, file, ret = NULL;
918 TRACE("%s, %d\n", debugstr_w(pFileName), bDeleteExistingResources);
920 hUpdate = GlobalAlloc(GHND, sizeof(QUEUEDUPDATES));
924 updates = GlobalLock(hUpdate);
927 list_init( &updates->root );
928 updates->pFileName = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(pFileName)+1)*sizeof(WCHAR));
929 if (updates->pFileName)
931 lstrcpyW(updates->pFileName, pFileName);
933 file = CreateFileW( pFileName, GENERIC_READ | GENERIC_WRITE,
934 0, NULL, OPEN_EXISTING, 0, 0 );
936 /* if resources are deleted, only the file's presence is checked */
937 if (file != INVALID_HANDLE_VALUE &&
938 (bDeleteExistingResources || load_raw_resources( file, updates )))
941 HeapFree( GetProcessHeap(), 0, updates->pFileName );
945 GlobalUnlock(hUpdate);
955 /***********************************************************************
956 * BeginUpdateResourceA (KERNEL32.@)
958 HANDLE WINAPI BeginUpdateResourceA( LPCSTR pFileName, BOOL bDeleteExistingResources )
960 UNICODE_STRING FileNameW;
962 RtlCreateUnicodeStringFromAsciiz(&FileNameW, pFileName);
963 ret = BeginUpdateResourceW(FileNameW.Buffer, bDeleteExistingResources);
964 RtlFreeUnicodeString(&FileNameW);
969 /***********************************************************************
970 * EndUpdateResourceW (KERNEL32.@)
972 BOOL WINAPI EndUpdateResourceW( HANDLE hUpdate, BOOL fDiscard )
974 QUEUEDUPDATES *updates;
977 TRACE("%p %d\n", hUpdate, fDiscard);
979 updates = GlobalLock(hUpdate);
983 ret = fDiscard || write_raw_resources( updates );
985 free_resource_directory( &updates->root, 2 );
987 HeapFree( GetProcessHeap(), 0, updates->pFileName );
988 GlobalUnlock( hUpdate );
989 GlobalFree( hUpdate );
995 /***********************************************************************
996 * EndUpdateResourceA (KERNEL32.@)
998 BOOL WINAPI EndUpdateResourceA( HANDLE hUpdate, BOOL fDiscard )
1000 return EndUpdateResourceW(hUpdate, fDiscard);
1004 /***********************************************************************
1005 * UpdateResourceW (KERNEL32.@)
1007 BOOL WINAPI UpdateResourceW( HANDLE hUpdate, LPCWSTR lpType, LPCWSTR lpName,
1008 WORD wLanguage, LPVOID lpData, DWORD cbData)
1010 QUEUEDUPDATES *updates;
1013 TRACE("%p %s %s %08x %p %d\n", hUpdate,
1014 debugstr_w(lpType), debugstr_w(lpName), wLanguage, lpData, cbData);
1016 updates = GlobalLock(hUpdate);
1019 ret = update_add_resource( updates, lpType, lpName,
1020 wLanguage, GetACP(), lpData, cbData );
1021 GlobalUnlock(hUpdate);
1027 /***********************************************************************
1028 * UpdateResourceA (KERNEL32.@)
1030 BOOL WINAPI UpdateResourceA( HANDLE hUpdate, LPCSTR lpType, LPCSTR lpName,
1031 WORD wLanguage, LPVOID lpData, DWORD cbData)
1034 UNICODE_STRING TypeW;
1035 UNICODE_STRING NameW;
1037 TypeW.Buffer = (LPWSTR)lpType;
1039 RtlCreateUnicodeStringFromAsciiz(&TypeW, lpType);
1041 NameW.Buffer = (LPWSTR)lpName;
1043 RtlCreateUnicodeStringFromAsciiz(&NameW, lpName);
1044 ret = UpdateResourceW(hUpdate, TypeW.Buffer, NameW.Buffer, wLanguage, lpData, cbData);
1045 if(HIWORD(lpType)) RtlFreeUnicodeString(&TypeW);
1046 if(HIWORD(lpName)) RtlFreeUnicodeString(&NameW);