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
29 #include "wine/port.h"
33 #include <sys/types.h>
35 #define NONAMELESSUNION
36 #define NONAMELESSSTRUCT
44 #include "wine/exception.h"
45 #include "wine/unicode.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(resource);
50 static LCID user_lcid, system_lcid;
51 static LANGID user_ui_language, system_ui_language;
53 static WINE_EXCEPTION_FILTER(page_fault)
55 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ||
56 GetExceptionCode() == EXCEPTION_PRIV_INSTRUCTION)
57 return EXCEPTION_EXECUTE_HANDLER;
58 return EXCEPTION_CONTINUE_SEARCH;
61 /**********************************************************************
64 * Check if a module handle is for a LOAD_LIBRARY_AS_DATAFILE module.
66 inline static int is_data_file_module( HMODULE hmod )
68 return (ULONG_PTR)hmod & 1;
72 /**********************************************************************
75 * push a language in the list of languages to try
77 static inline int push_language( WORD *list, int pos, WORD lang )
80 for (i = 0; i < pos; i++) if (list[i] == lang) return pos;
86 /**********************************************************************
89 * Find the first suitable entry in a resource directory
91 static const IMAGE_RESOURCE_DIRECTORY *find_first_entry( const IMAGE_RESOURCE_DIRECTORY *dir,
92 const void *root, int want_dir )
94 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
97 for (pos = 0; pos < dir->NumberOfNamedEntries + dir->NumberOfIdEntries; pos++)
99 if (!entry[pos].u2.s3.DataIsDirectory == !want_dir)
100 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s3.OffsetToDirectory);
106 /**********************************************************************
109 * Find an entry by id in a resource directory
111 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
112 WORD id, const void *root, int want_dir )
114 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
117 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
118 min = dir->NumberOfNamedEntries;
119 max = min + dir->NumberOfIdEntries - 1;
122 pos = (min + max) / 2;
123 if (entry[pos].u1.s2.Id == id)
125 if (!entry[pos].u2.s3.DataIsDirectory == !want_dir)
127 TRACE("root %p dir %p id %04x ret %p\n",
128 root, dir, id, (const char*)root + entry[pos].u2.s3.OffsetToDirectory);
129 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s3.OffsetToDirectory);
133 if (entry[pos].u1.s2.Id > id) max = pos - 1;
136 TRACE("root %p dir %p id %04x not found\n", root, dir, id );
141 /**********************************************************************
144 * Find an entry by name in a resource directory
146 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_name( const IMAGE_RESOURCE_DIRECTORY *dir,
147 LPCWSTR name, const void *root,
150 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
151 const IMAGE_RESOURCE_DIR_STRING_U *str;
152 int min, max, res, pos, namelen;
154 if (!HIWORD(name)) return find_entry_by_id( dir, LOWORD(name), root, want_dir );
155 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
156 namelen = strlenW(name);
158 max = dir->NumberOfNamedEntries - 1;
161 pos = (min + max) / 2;
162 str = (const IMAGE_RESOURCE_DIR_STRING_U *)((const char *)root + entry[pos].u1.s1.NameOffset);
163 res = strncmpW( name, str->NameString, str->Length );
164 if (!res && namelen == str->Length)
166 if (!entry[pos].u2.s3.DataIsDirectory == !want_dir)
168 TRACE("root %p dir %p name %s ret %p\n",
169 root, dir, debugstr_w(name), (const char*)root + entry[pos].u2.s3.OffsetToDirectory);
170 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s3.OffsetToDirectory);
174 if (res < 0) max = pos - 1;
177 TRACE("root %p dir %p name %s not found\n", root, dir, debugstr_w(name) );
182 /**********************************************************************
185 * Find a resource entry
187 static NTSTATUS find_entry( HMODULE hmod, const LDR_RESOURCE_INFO *info,
188 ULONG level, const void **ret, int want_dir )
192 const IMAGE_RESOURCE_DIRECTORY *resdirptr;
193 WORD list[9]; /* list of languages to try */
196 root = RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_RESOURCE, &size );
197 if (!root) return STATUS_RESOURCE_DATA_NOT_FOUND;
200 if (!level--) goto done;
201 if (!(*ret = find_entry_by_name( resdirptr, (LPCWSTR)info->Type, root, want_dir || level )))
202 return STATUS_RESOURCE_TYPE_NOT_FOUND;
203 if (!level--) return STATUS_SUCCESS;
206 if (!(*ret = find_entry_by_name( resdirptr, (LPCWSTR)info->Name, root, want_dir || level )))
207 return STATUS_RESOURCE_NAME_NOT_FOUND;
208 if (!level--) return STATUS_SUCCESS;
209 if (level) return STATUS_INVALID_PARAMETER; /* level > 3 */
211 /* 1. specified language */
212 pos = push_language( list, pos, info->Language );
214 /* 2. specified language with neutral sublanguage */
215 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(info->Language), SUBLANG_NEUTRAL ) );
217 /* 3. neutral language with neutral sublanguage */
218 pos = push_language( list, pos, MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ) );
220 /* if no explicitly specified language, try some defaults */
221 if (PRIMARYLANGID(info->Language) == LANG_NEUTRAL)
223 /* user defaults, unless SYS_DEFAULT sublanguage specified */
224 if (SUBLANGID(info->Language) != SUBLANG_SYS_DEFAULT)
226 /* 4. current thread locale language */
227 pos = push_language( list, pos, LANGIDFROMLCID(NtCurrentTeb()->CurrentLocale) );
229 /* 5. user locale language */
230 pos = push_language( list, pos, LANGIDFROMLCID(user_lcid) );
232 /* 6. user locale language with neutral sublanguage */
233 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(user_lcid), SUBLANG_NEUTRAL ) );
236 /* now system defaults */
238 /* 7. system locale language */
239 pos = push_language( list, pos, LANGIDFROMLCID( system_lcid ) );
241 /* 8. system locale language with neutral sublanguage */
242 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(system_lcid), SUBLANG_NEUTRAL ) );
245 pos = push_language( list, pos, MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT ) );
249 for (i = 0; i < pos; i++)
250 if ((*ret = find_entry_by_id( resdirptr, list[i], root, want_dir ))) return STATUS_SUCCESS;
252 /* if no explicitly specified language, return the first entry */
253 if (PRIMARYLANGID(info->Language) == LANG_NEUTRAL)
255 if ((*ret = find_first_entry( resdirptr, root, want_dir ))) return STATUS_SUCCESS;
257 return STATUS_RESOURCE_LANG_NOT_FOUND;
261 return STATUS_SUCCESS;
265 /**********************************************************************
266 * LdrFindResourceDirectory_U (NTDLL.@)
268 NTSTATUS WINAPI LdrFindResourceDirectory_U( HMODULE hmod, const LDR_RESOURCE_INFO *info,
269 ULONG level, const IMAGE_RESOURCE_DIRECTORY **dir )
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 status = find_entry( hmod, info, level, &res, TRUE );
282 if (status == STATUS_SUCCESS) *dir = res;
286 return GetExceptionCode();
293 /**********************************************************************
294 * LdrFindResource_U (NTDLL.@)
296 NTSTATUS WINAPI LdrFindResource_U( HMODULE hmod, const LDR_RESOURCE_INFO *info,
297 ULONG level, const IMAGE_RESOURCE_DATA_ENTRY **entry )
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 status = find_entry( hmod, info, level, &res, FALSE );
310 if (status == STATUS_SUCCESS) *entry = res;
314 return GetExceptionCode();
321 /**********************************************************************
322 * LdrAccessResource (NTDLL.@)
324 NTSTATUS WINAPI LdrAccessResource( HMODULE hmod, const IMAGE_RESOURCE_DATA_ENTRY *entry,
325 void **ptr, ULONG *size )
333 if (!RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_RESOURCE, &dirsize ))
334 status = STATUS_RESOURCE_DATA_NOT_FOUND;
339 if (is_data_file_module(hmod))
341 HMODULE mod = (HMODULE)((ULONG_PTR)hmod & ~1);
342 *ptr = RtlImageRvaToVa( RtlImageNtHeader(mod), mod, entry->OffsetToData, NULL );
344 else *ptr = (char *)hmod + entry->OffsetToData;
346 if (size) *size = entry->Size;
347 status = STATUS_SUCCESS;
352 return GetExceptionCode();
359 /**********************************************************************
360 * RtlFindMessage (NTDLL.@)
362 NTSTATUS WINAPI RtlFindMessage( HMODULE hmod, ULONG type, ULONG lang,
363 ULONG msg_id, const MESSAGE_RESOURCE_ENTRY **ret )
365 const MESSAGE_RESOURCE_DATA *data;
366 const MESSAGE_RESOURCE_BLOCK *block;
367 const IMAGE_RESOURCE_DATA_ENTRY *rsrc;
368 LDR_RESOURCE_INFO info;
375 info.Language = lang;
377 if ((status = LdrFindResource_U( hmod, &info, 3, &rsrc )) != STATUS_SUCCESS)
379 if ((status = LdrAccessResource( hmod, rsrc, &ptr, NULL )) != STATUS_SUCCESS)
383 block = data->Blocks;
384 for (i = 0; i < data->NumberOfBlocks; i++, block++)
386 if (msg_id >= block->LowId && msg_id <= block->HighId)
388 const MESSAGE_RESOURCE_ENTRY *entry;
390 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)data + block->OffsetToEntries);
391 for (i = msg_id - block->LowId; i > 0; i--)
392 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)entry + entry->Length);
394 return STATUS_SUCCESS;
397 return STATUS_MESSAGE_NOT_FOUND;
400 /**********************************************************************
401 * RtlFormatMessage (NTDLL.@)
403 * Formats a message (similar to sprintf).
406 * Message [I] Message to format.
407 * MaxWidth [I] Maximum width in characters of each output line.
408 * IgnoreInserts [I] Whether to copy the message without processing inserts.
409 * Ansi [I] Whether Arguments may have ANSI strings.
410 * ArgumentsIsArray [I] Whether Arguments is actually an array rather than a va_list *.
411 * Buffer [O] Buffer to store processed message in.
412 * BufferSize [I] Size of Buffer (in bytes?).
417 NTSTATUS WINAPI RtlFormatMessage( LPWSTR Message, UCHAR MaxWidth,
418 BOOLEAN IgnoreInserts, BOOLEAN Ansi,
419 BOOLEAN ArgumentIsArray, va_list * Arguments,
420 LPWSTR Buffer, ULONG BufferSize )
422 FIXME("(%s, %u, %s, %s, %s, %p, %p, %ld)\n", debugstr_w(Message),
423 MaxWidth, IgnoreInserts ? "TRUE" : "FALSE", Ansi ? "TRUE" : "FALSE",
424 ArgumentIsArray ? "TRUE" : "FALSE", Arguments, Buffer, BufferSize);
425 return STATUS_SUCCESS;
429 /**********************************************************************
430 * NtQueryDefaultLocale (NTDLL.@)
432 NTSTATUS WINAPI NtQueryDefaultLocale( BOOLEAN user, LCID *lcid )
434 *lcid = user ? user_lcid : system_lcid;
435 return STATUS_SUCCESS;
439 /**********************************************************************
440 * NtSetDefaultLocale (NTDLL.@)
442 NTSTATUS WINAPI NtSetDefaultLocale( BOOLEAN user, LCID lcid )
444 if (user) user_lcid = lcid;
448 system_ui_language = LANGIDFROMLCID(lcid); /* there is no separate call to set it */
450 return STATUS_SUCCESS;
454 /**********************************************************************
455 * NtQueryDefaultUILanguage (NTDLL.@)
457 NTSTATUS WINAPI NtQueryDefaultUILanguage( LANGID *lang )
459 *lang = user_ui_language;
460 return STATUS_SUCCESS;
464 /**********************************************************************
465 * NtSetDefaultUILanguage (NTDLL.@)
467 NTSTATUS WINAPI NtSetDefaultUILanguage( LANGID lang )
469 user_ui_language = lang;
470 return STATUS_SUCCESS;
474 /**********************************************************************
475 * NtQueryInstallUILanguage (NTDLL.@)
477 NTSTATUS WINAPI NtQueryInstallUILanguage( LANGID *lang )
479 *lang = system_ui_language;
480 return STATUS_SUCCESS;