Release 20040408.
[wine.git] / dlls / ntdll / resource.c
blob9458b35b25c84d6056a9e560cfc4a2ca556c11d5
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "config.h"
29 #include "wine/port.h"
31 #include <stdarg.h>
32 #include <stdlib.h>
33 #include <sys/types.h>
35 #define NONAMELESSUNION
36 #define NONAMELESSSTRUCT
37 #include "ntstatus.h"
38 #include "windef.h"
39 #include "winbase.h"
40 #include "winnls.h"
41 #include "winnt.h"
42 #include "winreg.h"
43 #include "winternl.h"
44 #include "winerror.h"
45 #include "thread.h"
46 #include "excpt.h"
47 #include "wine/exception.h"
48 #include "wine/unicode.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(resource);
53 static LCID user_lcid, system_lcid;
55 static WINE_EXCEPTION_FILTER(page_fault)
57 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ||
58 GetExceptionCode() == EXCEPTION_PRIV_INSTRUCTION)
59 return EXCEPTION_EXECUTE_HANDLER;
60 return EXCEPTION_CONTINUE_SEARCH;
63 /**********************************************************************
64 * is_data_file_module
66 * Check if a module handle is for a LOAD_LIBRARY_AS_DATAFILE module.
68 inline static int is_data_file_module( HMODULE hmod )
70 return (ULONG_PTR)hmod & 1;
74 /**********************************************************************
75 * push_language
77 * push a language in the list of languages to try
79 static inline int push_language( WORD *list, int pos, WORD lang )
81 int i;
82 for (i = 0; i < pos; i++) if (list[i] == lang) return pos;
83 list[pos++] = lang;
84 return pos;
88 /**********************************************************************
89 * find_first_entry
91 * Find the first suitable entry in a resource directory
93 static const IMAGE_RESOURCE_DIRECTORY *find_first_entry( const IMAGE_RESOURCE_DIRECTORY *dir,
94 const void *root, int want_dir )
96 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
97 int pos;
99 for (pos = 0; pos < dir->NumberOfNamedEntries + dir->NumberOfIdEntries; pos++)
101 if (!entry[pos].u2.s3.DataIsDirectory == !want_dir)
102 return (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry[pos].u2.s3.OffsetToDirectory);
104 return NULL;
108 /**********************************************************************
109 * find_entry_by_id
111 * Find an entry by id in a resource directory
113 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
114 WORD id, const void *root, int want_dir )
116 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
117 int min, max, pos;
119 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
120 min = dir->NumberOfNamedEntries;
121 max = min + dir->NumberOfIdEntries - 1;
122 while (min <= max)
124 pos = (min + max) / 2;
125 if (entry[pos].u1.s2.Id == id)
127 if (!entry[pos].u2.s3.DataIsDirectory == !want_dir)
129 TRACE("root %p dir %p id %04x ret %p\n",
130 root, dir, id, (char *)root + entry[pos].u2.s3.OffsetToDirectory);
131 return (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry[pos].u2.s3.OffsetToDirectory);
133 break;
135 if (entry[pos].u1.s2.Id > id) max = pos - 1;
136 else min = pos + 1;
138 TRACE("root %p dir %p id %04x not found\n", root, dir, id );
139 return NULL;
143 /**********************************************************************
144 * find_entry_by_name
146 * Find an entry by name in a resource directory
148 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_name( const IMAGE_RESOURCE_DIRECTORY *dir,
149 LPCWSTR name, const void *root,
150 int want_dir )
152 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
153 const IMAGE_RESOURCE_DIR_STRING_U *str;
154 int min, max, res, pos, namelen;
156 if (!HIWORD(name)) return find_entry_by_id( dir, LOWORD(name), root, want_dir );
157 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
158 namelen = strlenW(name);
159 min = 0;
160 max = dir->NumberOfNamedEntries - 1;
161 while (min <= max)
163 pos = (min + max) / 2;
164 str = (IMAGE_RESOURCE_DIR_STRING_U *)((char *)root + entry[pos].u1.s1.NameOffset);
165 res = strncmpW( name, str->NameString, str->Length );
166 if (!res && namelen == str->Length)
168 if (!entry[pos].u2.s3.DataIsDirectory == !want_dir)
170 TRACE("root %p dir %p name %s ret %p\n",
171 root, dir, debugstr_w(name), (char *)root + entry[pos].u2.s3.OffsetToDirectory);
172 return (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry[pos].u2.s3.OffsetToDirectory);
174 break;
176 if (res < 0) max = pos - 1;
177 else min = pos + 1;
179 TRACE("root %p dir %p name %s not found\n", root, dir, debugstr_w(name) );
180 return NULL;
184 /**********************************************************************
185 * find_entry
187 * Find a resource entry
189 static NTSTATUS find_entry( HMODULE hmod, const LDR_RESOURCE_INFO *info,
190 ULONG level, const void **ret, int want_dir )
192 ULONG size;
193 const void *root;
194 const IMAGE_RESOURCE_DIRECTORY *resdirptr;
195 WORD list[9]; /* list of languages to try */
196 int i, pos = 0;
198 root = RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_RESOURCE, &size );
199 if (!root) return STATUS_RESOURCE_DATA_NOT_FOUND;
200 resdirptr = root;
202 if (!level--) goto done;
203 if (!(*ret = find_entry_by_name( resdirptr, (LPCWSTR)info->Type, root, want_dir || level )))
204 return STATUS_RESOURCE_TYPE_NOT_FOUND;
205 if (!level--) return STATUS_SUCCESS;
207 resdirptr = *ret;
208 if (!(*ret = find_entry_by_name( resdirptr, (LPCWSTR)info->Name, root, want_dir || level )))
209 return STATUS_RESOURCE_NAME_NOT_FOUND;
210 if (!level--) return STATUS_SUCCESS;
211 if (level) return STATUS_INVALID_PARAMETER; /* level > 3 */
213 /* 1. specified language */
214 pos = push_language( list, pos, info->Language );
216 /* 2. specified language with neutral sublanguage */
217 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(info->Language), SUBLANG_NEUTRAL ) );
219 /* 3. neutral language with neutral sublanguage */
220 pos = push_language( list, pos, MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ) );
222 /* if no explicitly specified language, try some defaults */
223 if (PRIMARYLANGID(info->Language) == LANG_NEUTRAL)
225 /* user defaults, unless SYS_DEFAULT sublanguage specified */
226 if (SUBLANGID(info->Language) != SUBLANG_SYS_DEFAULT)
228 /* 4. current thread locale language */
229 pos = push_language( list, pos, LANGIDFROMLCID(NtCurrentTeb()->CurrentLocale) );
231 /* 5. user locale language */
232 pos = push_language( list, pos, LANGIDFROMLCID(user_lcid) );
234 /* 6. user locale language with neutral sublanguage */
235 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(user_lcid), SUBLANG_NEUTRAL ) );
238 /* now system defaults */
240 /* 7. system locale language */
241 pos = push_language( list, pos, LANGIDFROMLCID( system_lcid ) );
243 /* 8. system locale language with neutral sublanguage */
244 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(system_lcid), SUBLANG_NEUTRAL ) );
246 /* 9. English */
247 pos = push_language( list, pos, MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT ) );
250 resdirptr = *ret;
251 for (i = 0; i < pos; i++)
252 if ((*ret = find_entry_by_id( resdirptr, list[i], root, want_dir ))) return STATUS_SUCCESS;
254 /* if no explicitly specified language, return the first entry */
255 if (PRIMARYLANGID(info->Language) == LANG_NEUTRAL)
257 if ((*ret = find_first_entry( resdirptr, root, want_dir ))) return STATUS_SUCCESS;
259 return STATUS_RESOURCE_LANG_NOT_FOUND;
261 done:
262 *ret = resdirptr;
263 return STATUS_SUCCESS;
267 /**********************************************************************
268 * LdrFindResourceDirectory_U (NTDLL.@)
270 NTSTATUS WINAPI LdrFindResourceDirectory_U( HMODULE hmod, const LDR_RESOURCE_INFO *info,
271 ULONG level, const IMAGE_RESOURCE_DIRECTORY **dir )
273 const void *res;
274 NTSTATUS status;
276 if (info) TRACE( "module %p type %s name %s lang %04lx level %ld\n",
277 hmod, debugstr_w((LPCWSTR)info->Type),
278 level > 1 ? debugstr_w((LPCWSTR)info->Name) : "",
279 level > 2 ? info->Language : 0, level );
281 __TRY
283 status = find_entry( hmod, info, level, &res, TRUE );
284 if (status == STATUS_SUCCESS) *dir = res;
286 __EXCEPT(page_fault)
288 return GetExceptionCode();
290 __ENDTRY;
291 return status;
295 /**********************************************************************
296 * LdrFindResource_U (NTDLL.@)
298 NTSTATUS WINAPI LdrFindResource_U( HMODULE hmod, const LDR_RESOURCE_INFO *info,
299 ULONG level, const IMAGE_RESOURCE_DATA_ENTRY **entry )
301 const void *res;
302 NTSTATUS status;
304 if (info) TRACE( "module %p type %s name %s lang %04lx level %ld\n",
305 hmod, debugstr_w((LPCWSTR)info->Type),
306 level > 1 ? debugstr_w((LPCWSTR)info->Name) : "",
307 level > 2 ? info->Language : 0, level );
309 __TRY
311 status = find_entry( hmod, info, level, &res, FALSE );
312 if (status == STATUS_SUCCESS) *entry = res;
314 __EXCEPT(page_fault)
316 return GetExceptionCode();
318 __ENDTRY;
319 return status;
323 /**********************************************************************
324 * LdrAccessResource (NTDLL.@)
326 NTSTATUS WINAPI LdrAccessResource( HMODULE hmod, const IMAGE_RESOURCE_DATA_ENTRY *entry,
327 void **ptr, ULONG *size )
329 NTSTATUS status;
331 __TRY
333 ULONG dirsize;
335 if (!RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_RESOURCE, &dirsize ))
336 status = STATUS_RESOURCE_DATA_NOT_FOUND;
337 else
339 if (ptr)
341 if (is_data_file_module(hmod))
343 HMODULE mod = (HMODULE)((ULONG_PTR)hmod & ~1);
344 *ptr = RtlImageRvaToVa( RtlImageNtHeader(mod), mod, entry->OffsetToData, NULL );
346 else *ptr = (char *)hmod + entry->OffsetToData;
348 if (size) *size = entry->Size;
349 status = STATUS_SUCCESS;
352 __EXCEPT(page_fault)
354 return GetExceptionCode();
356 __ENDTRY;
357 return status;
361 /**********************************************************************
362 * RtlFindMessage (NTDLL.@)
364 NTSTATUS WINAPI RtlFindMessage( HMODULE hmod, ULONG type, ULONG lang,
365 ULONG msg_id, const MESSAGE_RESOURCE_ENTRY **ret )
367 const MESSAGE_RESOURCE_DATA *data;
368 const MESSAGE_RESOURCE_BLOCK *block;
369 const IMAGE_RESOURCE_DATA_ENTRY *rsrc;
370 LDR_RESOURCE_INFO info;
371 NTSTATUS status;
372 void *ptr;
373 int i;
375 info.Type = type;
376 info.Name = 1;
377 info.Language = lang;
379 if ((status = LdrFindResource_U( hmod, &info, 3, &rsrc )) != STATUS_SUCCESS)
380 return status;
381 if ((status = LdrAccessResource( hmod, rsrc, &ptr, NULL )) != STATUS_SUCCESS)
382 return status;
384 data = ptr;
385 block = data->Blocks;
386 for (i = 0; i < data->NumberOfBlocks; i++, block++)
388 if (msg_id >= block->LowId && msg_id <= block->HighId)
390 const MESSAGE_RESOURCE_ENTRY *entry;
392 entry = (MESSAGE_RESOURCE_ENTRY *)((char *)data + block->OffsetToEntries);
393 for (i = msg_id - block->LowId; i > 0; i--)
394 entry = (MESSAGE_RESOURCE_ENTRY *)((char *)entry + entry->Length);
395 *ret = entry;
396 return STATUS_SUCCESS;
399 return STATUS_MESSAGE_NOT_FOUND;
403 /**********************************************************************
404 * NtQueryDefaultLocale (NTDLL.@)
406 NTSTATUS WINAPI NtQueryDefaultLocale( BOOLEAN user, LCID *lcid )
408 *lcid = user ? user_lcid : system_lcid;
409 return STATUS_SUCCESS;
413 /**********************************************************************
414 * NtSetDefaultLocale (NTDLL.@)
416 NTSTATUS WINAPI NtSetDefaultLocale( BOOLEAN user, LCID lcid )
418 if (user) user_lcid = lcid;
419 else system_lcid = lcid;
420 return STATUS_SUCCESS;