gdiplus: In GdipImageSelectActiveFrame rely on codec->select_func() to fail.
[wine.git] / dlls / ntdll / resource.c
blobf1c6e04141f56c0c097909da27217d8a45bad104
1 /*
2 * PE file resources
4 * Copyright 1995 Thomas Sandford
5 * Copyright 1996 Martin von Loewis
6 * Copyright 2003 Alexandre Julliard
8 * Based on the Win16 resource handling code in loader/resource.c
9 * Copyright 1993 Robert J. Amstadt
10 * Copyright 1995 Alexandre Julliard
11 * Copyright 1997 Marcus Meissner
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #include <stdarg.h>
29 #include <stdlib.h>
30 #include <sys/types.h>
32 #include "ntstatus.h"
33 #define WIN32_NO_STATUS
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winnt.h"
37 #include "winternl.h"
38 #include "ntdll_misc.h"
39 #include "wine/asm.h"
40 #include "wine/exception.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(resource);
45 #define IS_INTRESOURCE(x) (((ULONG_PTR)(x) >> 16) == 0)
47 /**********************************************************************
48 * is_data_file_module
50 * Check if a module handle is for a LOAD_LIBRARY_AS_DATAFILE module.
52 static inline BOOL is_data_file_module( HMODULE hmod )
54 return (ULONG_PTR)hmod & 1;
58 /**********************************************************************
59 * push_language
61 * push a language in the list of languages to try
63 static inline int push_language( WORD *list, int pos, WORD lang )
65 int i;
66 for (i = 0; i < pos; i++) if (list[i] == lang) return pos;
67 list[pos++] = lang;
68 return pos;
72 /**********************************************************************
73 * find_first_entry
75 * Find the first suitable entry in a resource directory
77 static const IMAGE_RESOURCE_DIRECTORY *find_first_entry( const IMAGE_RESOURCE_DIRECTORY *dir,
78 const void *root, int want_dir )
80 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
81 int pos;
83 for (pos = 0; pos < dir->NumberOfNamedEntries + dir->NumberOfIdEntries; pos++)
85 if (!entry[pos].DataIsDirectory == !want_dir)
86 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].OffsetToDirectory);
88 return NULL;
92 /**********************************************************************
93 * find_entry_by_id
95 * Find an entry by id in a resource directory
97 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
98 WORD id, const void *root, int want_dir )
100 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
101 int min, max, pos;
103 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
104 min = dir->NumberOfNamedEntries;
105 max = min + dir->NumberOfIdEntries - 1;
106 while (min <= max)
108 pos = (min + max) / 2;
109 if (entry[pos].Id == id)
111 if (!entry[pos].DataIsDirectory == !want_dir)
113 TRACE("root %p dir %p id %04x ret %p\n",
114 root, dir, id, (const char*)root + entry[pos].OffsetToDirectory);
115 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].OffsetToDirectory);
117 break;
119 if (entry[pos].Id > id) max = pos - 1;
120 else min = pos + 1;
122 TRACE("root %p dir %p id %04x not found\n", root, dir, id );
123 return NULL;
127 /**********************************************************************
128 * find_entry_by_name
130 * Find an entry by name in a resource directory
132 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_name( const IMAGE_RESOURCE_DIRECTORY *dir,
133 LPCWSTR name, const void *root,
134 int want_dir )
136 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
137 const IMAGE_RESOURCE_DIR_STRING_U *str;
138 int min, max, res, pos, namelen;
140 if (IS_INTRESOURCE(name)) return find_entry_by_id( dir, LOWORD(name), root, want_dir );
141 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
142 namelen = wcslen(name);
143 min = 0;
144 max = dir->NumberOfNamedEntries - 1;
145 while (min <= max)
147 pos = (min + max) / 2;
148 str = (const IMAGE_RESOURCE_DIR_STRING_U *)((const char *)root + entry[pos].NameOffset);
149 res = wcsncmp( name, str->NameString, str->Length );
150 if (!res && namelen == str->Length)
152 if (!entry[pos].DataIsDirectory == !want_dir)
154 TRACE("root %p dir %p name %s ret %p\n",
155 root, dir, debugstr_w(name), (const char*)root + entry[pos].OffsetToDirectory);
156 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].OffsetToDirectory);
158 break;
160 if (res < 0) max = pos - 1;
161 else min = pos + 1;
163 TRACE("root %p dir %p name %s not found\n", root, dir, debugstr_w(name) );
164 return NULL;
168 /**********************************************************************
169 * find_entry
171 * Find a resource entry
173 static NTSTATUS find_entry( HMODULE hmod, const LDR_RESOURCE_INFO *info,
174 ULONG level, const void **ret, int want_dir )
176 ULONG size;
177 const void *root;
178 const IMAGE_RESOURCE_DIRECTORY *resdirptr;
179 WORD list[9]; /* list of languages to try */
180 int i, pos = 0;
182 root = RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_RESOURCE, &size );
183 if (!root) return STATUS_RESOURCE_DATA_NOT_FOUND;
184 if (size < sizeof(*resdirptr)) return STATUS_RESOURCE_DATA_NOT_FOUND;
185 resdirptr = root;
187 if (!level--) goto done;
188 if (!(*ret = find_entry_by_name( resdirptr, (LPCWSTR)info->Type, root, want_dir || level )))
189 return STATUS_RESOURCE_TYPE_NOT_FOUND;
190 if (!level--) return STATUS_SUCCESS;
192 resdirptr = *ret;
193 if (!(*ret = find_entry_by_name( resdirptr, (LPCWSTR)info->Name, root, want_dir || level )))
194 return STATUS_RESOURCE_NAME_NOT_FOUND;
195 if (!level--) return STATUS_SUCCESS;
196 if (level) return STATUS_INVALID_PARAMETER; /* level > 3 */
198 /* 1. specified language */
199 pos = push_language( list, pos, info->Language );
201 /* 2. specified language with neutral sublanguage */
202 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(info->Language), SUBLANG_NEUTRAL ) );
204 /* 3. neutral language with neutral sublanguage */
205 pos = push_language( list, pos, MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ) );
207 /* if no explicitly specified language, try some defaults */
208 if (PRIMARYLANGID(info->Language) == LANG_NEUTRAL)
210 LANGID user_lang, user_neutral_lang, system_lang;
212 get_resource_lcids( &user_lang, &user_neutral_lang, &system_lang );
214 /* user defaults, unless SYS_DEFAULT sublanguage specified */
215 if (SUBLANGID(info->Language) != SUBLANG_SYS_DEFAULT)
217 /* 4. current thread locale language */
218 pos = push_language( list, pos, LANGIDFROMLCID(NtCurrentTeb()->CurrentLocale) );
220 /* 5. user locale language */
221 pos = push_language( list, pos, user_lang );
223 /* 6. user locale language with neutral sublanguage */
224 pos = push_language( list, pos, user_neutral_lang );
227 /* 7. system locale language */
228 pos = push_language( list, pos, system_lang );
230 /* 8. system locale language with neutral sublanguage */
231 pos = push_language( list, pos, PRIMARYLANGID( system_lang ));
233 /* 9. English */
234 pos = push_language( list, pos, MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT ) );
237 resdirptr = *ret;
238 for (i = 0; i < pos; i++)
239 if ((*ret = find_entry_by_id( resdirptr, list[i], root, want_dir ))) return STATUS_SUCCESS;
241 /* if no explicitly specified language, return the first entry */
242 if (PRIMARYLANGID(info->Language) == LANG_NEUTRAL)
244 if ((*ret = find_first_entry( resdirptr, root, want_dir ))) return STATUS_SUCCESS;
246 return STATUS_RESOURCE_LANG_NOT_FOUND;
248 done:
249 *ret = resdirptr;
250 return STATUS_SUCCESS;
254 /**********************************************************************
255 * LdrFindResourceDirectory_U (NTDLL.@)
257 NTSTATUS WINAPI DECLSPEC_HOTPATCH LdrFindResourceDirectory_U( HMODULE hmod, const LDR_RESOURCE_INFO *info,
258 ULONG level, const IMAGE_RESOURCE_DIRECTORY **dir )
260 const void *res;
261 NTSTATUS status;
263 __TRY
265 if (info) TRACE( "module %p type %s name %s lang %04lx level %ld\n",
266 hmod, debugstr_w((LPCWSTR)info->Type),
267 level > 1 ? debugstr_w((LPCWSTR)info->Name) : "",
268 level > 2 ? info->Language : 0, level );
270 status = find_entry( hmod, info, level, &res, TRUE );
271 if (status == STATUS_SUCCESS) *dir = res;
273 __EXCEPT_PAGE_FAULT
275 return GetExceptionCode();
277 __ENDTRY;
278 return status;
282 /**********************************************************************
283 * LdrFindResource_U (NTDLL.@)
285 NTSTATUS WINAPI DECLSPEC_HOTPATCH LdrFindResource_U( HMODULE hmod, const LDR_RESOURCE_INFO *info,
286 ULONG level, const IMAGE_RESOURCE_DATA_ENTRY **entry )
288 const void *res;
289 NTSTATUS status;
291 __TRY
293 if (info) TRACE( "module %p type %s name %s lang %04lx level %ld\n",
294 hmod, debugstr_w((LPCWSTR)info->Type),
295 level > 1 ? debugstr_w((LPCWSTR)info->Name) : "",
296 level > 2 ? info->Language : 0, level );
298 status = find_entry( hmod, info, level, &res, FALSE );
299 if (status == STATUS_SUCCESS) *entry = res;
301 __EXCEPT_PAGE_FAULT
303 return GetExceptionCode();
305 __ENDTRY;
306 return status;
310 /* don't penalize other platforms with stuff needed on i386 for compatibility */
311 #ifdef __i386__
312 NTSTATUS WINAPI DECLSPEC_HIDDEN access_resource( HMODULE hmod, const IMAGE_RESOURCE_DATA_ENTRY *entry,
313 void **ptr, ULONG *size )
314 #else
315 static inline NTSTATUS access_resource( HMODULE hmod, const IMAGE_RESOURCE_DATA_ENTRY *entry,
316 void **ptr, ULONG *size )
317 #endif
319 NTSTATUS status;
321 __TRY
323 ULONG dirsize;
325 if (!RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_RESOURCE, &dirsize ))
326 status = STATUS_RESOURCE_DATA_NOT_FOUND;
327 else
329 if (ptr)
331 BOOL is_data_file = is_data_file_module(hmod);
332 hmod = (HMODULE)((ULONG_PTR)hmod & ~3);
333 if (is_data_file)
334 *ptr = RtlImageRvaToVa( RtlImageNtHeader(hmod), hmod, entry->OffsetToData, NULL );
335 else
336 *ptr = (char *)hmod + entry->OffsetToData;
338 if (size) *size = entry->Size;
339 status = STATUS_SUCCESS;
342 __EXCEPT_PAGE_FAULT
344 return GetExceptionCode();
346 __ENDTRY;
347 return status;
350 /**********************************************************************
351 * LdrAccessResource (NTDLL.@)
353 * NOTE
354 * On x86, Shrinker, an executable compressor, depends on the
355 * "call access_resource" instruction being there.
357 #ifdef __i386__
358 __ASM_STDCALL_FUNC( LdrAccessResource, 16,
359 "pushl %ebp\n\t"
360 "movl %esp, %ebp\n\t"
361 "subl $4,%esp\n\t"
362 "pushl 24(%ebp)\n\t"
363 "pushl 20(%ebp)\n\t"
364 "pushl 16(%ebp)\n\t"
365 "pushl 12(%ebp)\n\t"
366 "pushl 8(%ebp)\n\t"
367 "call " __ASM_STDCALL("access_resource",16) "\n\t"
368 "leave\n\t"
369 "ret $16"
371 #else
372 NTSTATUS WINAPI LdrAccessResource( HMODULE hmod, const IMAGE_RESOURCE_DATA_ENTRY *entry,
373 void **ptr, ULONG *size )
375 return access_resource( hmod, entry, ptr, size );
377 #endif
379 /**********************************************************************
380 * RtlFindMessage (NTDLL.@)
382 NTSTATUS WINAPI RtlFindMessage( HMODULE hmod, ULONG type, ULONG lang,
383 ULONG msg_id, const MESSAGE_RESOURCE_ENTRY **ret )
385 const MESSAGE_RESOURCE_DATA *data;
386 const MESSAGE_RESOURCE_BLOCK *block;
387 const IMAGE_RESOURCE_DATA_ENTRY *rsrc;
388 LDR_RESOURCE_INFO info;
389 NTSTATUS status;
390 void *ptr;
391 unsigned int i;
393 info.Type = type;
394 info.Name = 1;
395 info.Language = lang;
397 if ((status = LdrFindResource_U( hmod, &info, 3, &rsrc )) != STATUS_SUCCESS)
398 return status;
399 if ((status = LdrAccessResource( hmod, rsrc, &ptr, NULL )) != STATUS_SUCCESS)
400 return status;
402 data = ptr;
403 block = data->Blocks;
404 for (i = 0; i < data->NumberOfBlocks; i++, block++)
406 if (msg_id >= block->LowId && msg_id <= block->HighId)
408 const MESSAGE_RESOURCE_ENTRY *entry;
410 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)data + block->OffsetToEntries);
411 for (i = msg_id - block->LowId; i > 0; i--)
412 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)entry + entry->Length);
413 *ret = entry;
414 return STATUS_SUCCESS;
417 return STATUS_MESSAGE_NOT_FOUND;