dinput: Factor out IDirectInputDevice ansi vtable.
[wine.git] / dlls / ntdll / resource.c
blob58a0fc7d2e245425e77107e111a0d0a3f3c8c2f7
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 #define NONAMELESSUNION
33 #define NONAMELESSSTRUCT
34 #include "ntstatus.h"
35 #define WIN32_NO_STATUS
36 #include "windef.h"
37 #include "winbase.h"
38 #include "winnt.h"
39 #include "winternl.h"
40 #include "ntdll_misc.h"
41 #include "wine/asm.h"
42 #include "wine/exception.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(resource);
47 #define IS_INTRESOURCE(x) (((ULONG_PTR)(x) >> 16) == 0)
49 /**********************************************************************
50 * is_data_file_module
52 * Check if a module handle is for a LOAD_LIBRARY_AS_DATAFILE module.
54 static inline BOOL is_data_file_module( HMODULE hmod )
56 return (ULONG_PTR)hmod & 1;
60 /**********************************************************************
61 * push_language
63 * push a language in the list of languages to try
65 static inline int push_language( WORD *list, int pos, WORD lang )
67 int i;
68 for (i = 0; i < pos; i++) if (list[i] == lang) return pos;
69 list[pos++] = lang;
70 return pos;
74 /**********************************************************************
75 * find_first_entry
77 * Find the first suitable entry in a resource directory
79 static const IMAGE_RESOURCE_DIRECTORY *find_first_entry( const IMAGE_RESOURCE_DIRECTORY *dir,
80 const void *root, int want_dir )
82 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
83 int pos;
85 for (pos = 0; pos < dir->NumberOfNamedEntries + dir->NumberOfIdEntries; pos++)
87 if (!entry[pos].u2.s2.DataIsDirectory == !want_dir)
88 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s2.OffsetToDirectory);
90 return NULL;
94 /**********************************************************************
95 * find_entry_by_id
97 * Find an entry by id in a resource directory
99 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
100 WORD id, const void *root, int want_dir )
102 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
103 int min, max, pos;
105 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
106 min = dir->NumberOfNamedEntries;
107 max = min + dir->NumberOfIdEntries - 1;
108 while (min <= max)
110 pos = (min + max) / 2;
111 if (entry[pos].u.Id == id)
113 if (!entry[pos].u2.s2.DataIsDirectory == !want_dir)
115 TRACE("root %p dir %p id %04x ret %p\n",
116 root, dir, id, (const char*)root + entry[pos].u2.s2.OffsetToDirectory);
117 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s2.OffsetToDirectory);
119 break;
121 if (entry[pos].u.Id > id) max = pos - 1;
122 else min = pos + 1;
124 TRACE("root %p dir %p id %04x not found\n", root, dir, id );
125 return NULL;
129 /**********************************************************************
130 * find_entry_by_name
132 * Find an entry by name in a resource directory
134 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_name( const IMAGE_RESOURCE_DIRECTORY *dir,
135 LPCWSTR name, const void *root,
136 int want_dir )
138 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
139 const IMAGE_RESOURCE_DIR_STRING_U *str;
140 int min, max, res, pos, namelen;
142 if (IS_INTRESOURCE(name)) return find_entry_by_id( dir, LOWORD(name), root, want_dir );
143 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
144 namelen = wcslen(name);
145 min = 0;
146 max = dir->NumberOfNamedEntries - 1;
147 while (min <= max)
149 pos = (min + max) / 2;
150 str = (const IMAGE_RESOURCE_DIR_STRING_U *)((const char *)root + entry[pos].u.s.NameOffset);
151 res = wcsncmp( name, str->NameString, str->Length );
152 if (!res && namelen == str->Length)
154 if (!entry[pos].u2.s2.DataIsDirectory == !want_dir)
156 TRACE("root %p dir %p name %s ret %p\n",
157 root, dir, debugstr_w(name), (const char*)root + entry[pos].u2.s2.OffsetToDirectory);
158 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s2.OffsetToDirectory);
160 break;
162 if (res < 0) max = pos - 1;
163 else min = pos + 1;
165 TRACE("root %p dir %p name %s not found\n", root, dir, debugstr_w(name) );
166 return NULL;
170 /**********************************************************************
171 * find_entry
173 * Find a resource entry
175 static NTSTATUS find_entry( HMODULE hmod, const LDR_RESOURCE_INFO *info,
176 ULONG level, const void **ret, int want_dir )
178 static LCID user_lcid, system_lcid;
179 ULONG size;
180 const void *root;
181 const IMAGE_RESOURCE_DIRECTORY *resdirptr;
182 WORD list[9]; /* list of languages to try */
183 int i, pos = 0;
185 root = RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_RESOURCE, &size );
186 if (!root) return STATUS_RESOURCE_DATA_NOT_FOUND;
187 if (size < sizeof(*resdirptr)) return STATUS_RESOURCE_DATA_NOT_FOUND;
188 resdirptr = root;
190 if (!level--) goto done;
191 if (!(*ret = find_entry_by_name( resdirptr, (LPCWSTR)info->Type, root, want_dir || level )))
192 return STATUS_RESOURCE_TYPE_NOT_FOUND;
193 if (!level--) return STATUS_SUCCESS;
195 resdirptr = *ret;
196 if (!(*ret = find_entry_by_name( resdirptr, (LPCWSTR)info->Name, root, want_dir || level )))
197 return STATUS_RESOURCE_NAME_NOT_FOUND;
198 if (!level--) return STATUS_SUCCESS;
199 if (level) return STATUS_INVALID_PARAMETER; /* level > 3 */
201 /* 1. specified language */
202 pos = push_language( list, pos, info->Language );
204 /* 2. specified language with neutral sublanguage */
205 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(info->Language), SUBLANG_NEUTRAL ) );
207 /* 3. neutral language with neutral sublanguage */
208 pos = push_language( list, pos, MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ) );
210 /* if no explicitly specified language, try some defaults */
211 if (PRIMARYLANGID(info->Language) == LANG_NEUTRAL)
213 /* user defaults, unless SYS_DEFAULT sublanguage specified */
214 if (SUBLANGID(info->Language) != SUBLANG_SYS_DEFAULT)
216 if (!user_lcid) NtQueryDefaultLocale( TRUE, &user_lcid );
218 /* 4. current thread locale language */
219 pos = push_language( list, pos, LANGIDFROMLCID(NtCurrentTeb()->CurrentLocale) );
221 /* 5. user locale language */
222 pos = push_language( list, pos, LANGIDFROMLCID(user_lcid) );
224 /* 6. user locale language with neutral sublanguage */
225 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(user_lcid), SUBLANG_NEUTRAL ) );
228 /* now system defaults */
229 if (!system_lcid) NtQueryDefaultLocale( FALSE, &system_lcid );
231 /* 7. system locale language */
232 pos = push_language( list, pos, LANGIDFROMLCID( system_lcid ) );
234 /* 8. system locale language with neutral sublanguage */
235 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(system_lcid), SUBLANG_NEUTRAL ) );
237 /* 9. English */
238 pos = push_language( list, pos, MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT ) );
241 resdirptr = *ret;
242 for (i = 0; i < pos; i++)
243 if ((*ret = find_entry_by_id( resdirptr, list[i], root, want_dir ))) return STATUS_SUCCESS;
245 /* if no explicitly specified language, return the first entry */
246 if (PRIMARYLANGID(info->Language) == LANG_NEUTRAL)
248 if ((*ret = find_first_entry( resdirptr, root, want_dir ))) return STATUS_SUCCESS;
250 return STATUS_RESOURCE_LANG_NOT_FOUND;
252 done:
253 *ret = resdirptr;
254 return STATUS_SUCCESS;
258 /**********************************************************************
259 * LdrFindResourceDirectory_U (NTDLL.@)
261 NTSTATUS WINAPI DECLSPEC_HOTPATCH LdrFindResourceDirectory_U( HMODULE hmod, const LDR_RESOURCE_INFO *info,
262 ULONG level, const IMAGE_RESOURCE_DIRECTORY **dir )
264 const void *res;
265 NTSTATUS status;
267 __TRY
269 if (info) TRACE( "module %p type %s name %s lang %04x level %d\n",
270 hmod, debugstr_w((LPCWSTR)info->Type),
271 level > 1 ? debugstr_w((LPCWSTR)info->Name) : "",
272 level > 2 ? info->Language : 0, level );
274 status = find_entry( hmod, info, level, &res, TRUE );
275 if (status == STATUS_SUCCESS) *dir = res;
277 __EXCEPT_PAGE_FAULT
279 return GetExceptionCode();
281 __ENDTRY;
282 return status;
286 /**********************************************************************
287 * LdrFindResource_U (NTDLL.@)
289 NTSTATUS WINAPI DECLSPEC_HOTPATCH LdrFindResource_U( HMODULE hmod, const LDR_RESOURCE_INFO *info,
290 ULONG level, const IMAGE_RESOURCE_DATA_ENTRY **entry )
292 const void *res;
293 NTSTATUS status;
295 __TRY
297 if (info) TRACE( "module %p type %s name %s lang %04x level %d\n",
298 hmod, debugstr_w((LPCWSTR)info->Type),
299 level > 1 ? debugstr_w((LPCWSTR)info->Name) : "",
300 level > 2 ? info->Language : 0, level );
302 status = find_entry( hmod, info, level, &res, FALSE );
303 if (status == STATUS_SUCCESS) *entry = res;
305 __EXCEPT_PAGE_FAULT
307 return GetExceptionCode();
309 __ENDTRY;
310 return status;
314 /* don't penalize other platforms with stuff needed on i386 for compatibility */
315 #ifdef __i386__
316 NTSTATUS WINAPI DECLSPEC_HIDDEN access_resource( HMODULE hmod, const IMAGE_RESOURCE_DATA_ENTRY *entry,
317 void **ptr, ULONG *size )
318 #else
319 static inline NTSTATUS access_resource( HMODULE hmod, const IMAGE_RESOURCE_DATA_ENTRY *entry,
320 void **ptr, ULONG *size )
321 #endif
323 NTSTATUS status;
325 __TRY
327 ULONG dirsize;
329 if (!RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_RESOURCE, &dirsize ))
330 status = STATUS_RESOURCE_DATA_NOT_FOUND;
331 else
333 if (ptr)
335 BOOL is_data_file = is_data_file_module(hmod);
336 hmod = (HMODULE)((ULONG_PTR)hmod & ~3);
337 if (is_data_file)
338 *ptr = RtlImageRvaToVa( RtlImageNtHeader(hmod), hmod, entry->OffsetToData, NULL );
339 else
340 *ptr = (char *)hmod + entry->OffsetToData;
342 if (size) *size = entry->Size;
343 status = STATUS_SUCCESS;
346 __EXCEPT_PAGE_FAULT
348 return GetExceptionCode();
350 __ENDTRY;
351 return status;
354 /**********************************************************************
355 * LdrAccessResource (NTDLL.@)
357 * NOTE
358 * On x86, Shrinker, an executable compressor, depends on the
359 * "call access_resource" instruction being there.
361 #ifdef __i386__
362 __ASM_STDCALL_FUNC( LdrAccessResource, 16,
363 "pushl %ebp\n\t"
364 "movl %esp, %ebp\n\t"
365 "subl $4,%esp\n\t"
366 "pushl 24(%ebp)\n\t"
367 "pushl 20(%ebp)\n\t"
368 "pushl 16(%ebp)\n\t"
369 "pushl 12(%ebp)\n\t"
370 "pushl 8(%ebp)\n\t"
371 "call " __ASM_STDCALL("access_resource",16) "\n\t"
372 "leave\n\t"
373 "ret $16"
375 #else
376 NTSTATUS WINAPI LdrAccessResource( HMODULE hmod, const IMAGE_RESOURCE_DATA_ENTRY *entry,
377 void **ptr, ULONG *size )
379 return access_resource( hmod, entry, ptr, size );
381 #endif
383 /**********************************************************************
384 * RtlFindMessage (NTDLL.@)
386 NTSTATUS WINAPI RtlFindMessage( HMODULE hmod, ULONG type, ULONG lang,
387 ULONG msg_id, const MESSAGE_RESOURCE_ENTRY **ret )
389 const MESSAGE_RESOURCE_DATA *data;
390 const MESSAGE_RESOURCE_BLOCK *block;
391 const IMAGE_RESOURCE_DATA_ENTRY *rsrc;
392 LDR_RESOURCE_INFO info;
393 NTSTATUS status;
394 void *ptr;
395 unsigned int i;
397 info.Type = type;
398 info.Name = 1;
399 info.Language = lang;
401 if ((status = LdrFindResource_U( hmod, &info, 3, &rsrc )) != STATUS_SUCCESS)
402 return status;
403 if ((status = LdrAccessResource( hmod, rsrc, &ptr, NULL )) != STATUS_SUCCESS)
404 return status;
406 data = ptr;
407 block = data->Blocks;
408 for (i = 0; i < data->NumberOfBlocks; i++, block++)
410 if (msg_id >= block->LowId && msg_id <= block->HighId)
412 const MESSAGE_RESOURCE_ENTRY *entry;
414 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)data + block->OffsetToEntries);
415 for (i = msg_id - block->LowId; i > 0; i--)
416 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)entry + entry->Length);
417 *ret = entry;
418 return STATUS_SUCCESS;
421 return STATUS_MESSAGE_NOT_FOUND;