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 /* don't penalize other platforms stuff needed on i386 for compatibility */
323 NTSTATUS WINAPI access_resource( HMODULE hmod, const IMAGE_RESOURCE_DATA_ENTRY *entry,
324 void **ptr, ULONG *size )
326 static inline NTSTATUS access_resource( HMODULE hmod, const IMAGE_RESOURCE_DATA_ENTRY *entry,
327 void **ptr, ULONG *size )
336 if (!RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_RESOURCE, &dirsize ))
337 status = STATUS_RESOURCE_DATA_NOT_FOUND;
342 if (is_data_file_module(hmod))
344 HMODULE mod = (HMODULE)((ULONG_PTR)hmod & ~1);
345 *ptr = RtlImageRvaToVa( RtlImageNtHeader(mod), mod, entry->OffsetToData, NULL );
347 else *ptr = (char *)hmod + entry->OffsetToData;
349 if (size) *size = entry->Size;
350 status = STATUS_SUCCESS;
355 return GetExceptionCode();
361 /**********************************************************************
362 * LdrAccessResource (NTDLL.@)
365 /* Shrinker depends on the "call access_resource" instruction being there */
366 __ASM_GLOBAL_FUNC( LdrAccessResource,
374 "call " __ASM_NAME("access_resource") "\n"
379 NTSTATUS WINAPI LdrAccessResource( HMODULE hmod, const IMAGE_RESOURCE_DATA_ENTRY *entry,
380 void **ptr, ULONG *size )
382 return access_resource( hmod, entry, ptr, size );
386 /**********************************************************************
387 * RtlFindMessage (NTDLL.@)
389 NTSTATUS WINAPI RtlFindMessage( HMODULE hmod, ULONG type, ULONG lang,
390 ULONG msg_id, const MESSAGE_RESOURCE_ENTRY **ret )
392 const MESSAGE_RESOURCE_DATA *data;
393 const MESSAGE_RESOURCE_BLOCK *block;
394 const IMAGE_RESOURCE_DATA_ENTRY *rsrc;
395 LDR_RESOURCE_INFO info;
402 info.Language = lang;
404 if ((status = LdrFindResource_U( hmod, &info, 3, &rsrc )) != STATUS_SUCCESS)
406 if ((status = LdrAccessResource( hmod, rsrc, &ptr, NULL )) != STATUS_SUCCESS)
410 block = data->Blocks;
411 for (i = 0; i < data->NumberOfBlocks; i++, block++)
413 if (msg_id >= block->LowId && msg_id <= block->HighId)
415 const MESSAGE_RESOURCE_ENTRY *entry;
417 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)data + block->OffsetToEntries);
418 for (i = msg_id - block->LowId; i > 0; i--)
419 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)entry + entry->Length);
421 return STATUS_SUCCESS;
424 return STATUS_MESSAGE_NOT_FOUND;
427 /**********************************************************************
428 * RtlFormatMessage (NTDLL.@)
430 * Formats a message (similar to sprintf).
433 * Message [I] Message to format.
434 * MaxWidth [I] Maximum width in characters of each output line.
435 * IgnoreInserts [I] Whether to copy the message without processing inserts.
436 * Ansi [I] Whether Arguments may have ANSI strings.
437 * ArgumentsIsArray [I] Whether Arguments is actually an array rather than a va_list *.
438 * Buffer [O] Buffer to store processed message in.
439 * BufferSize [I] Size of Buffer (in bytes?).
444 NTSTATUS WINAPI RtlFormatMessage( LPWSTR Message, UCHAR MaxWidth,
445 BOOLEAN IgnoreInserts, BOOLEAN Ansi,
446 BOOLEAN ArgumentIsArray, va_list * Arguments,
447 LPWSTR Buffer, ULONG BufferSize )
449 FIXME("(%s, %u, %s, %s, %s, %p, %p, %ld)\n", debugstr_w(Message),
450 MaxWidth, IgnoreInserts ? "TRUE" : "FALSE", Ansi ? "TRUE" : "FALSE",
451 ArgumentIsArray ? "TRUE" : "FALSE", Arguments, Buffer, BufferSize);
452 return STATUS_SUCCESS;
456 /**********************************************************************
457 * NtQueryDefaultLocale (NTDLL.@)
459 NTSTATUS WINAPI NtQueryDefaultLocale( BOOLEAN user, LCID *lcid )
461 *lcid = user ? user_lcid : system_lcid;
462 return STATUS_SUCCESS;
466 /**********************************************************************
467 * NtSetDefaultLocale (NTDLL.@)
469 NTSTATUS WINAPI NtSetDefaultLocale( BOOLEAN user, LCID lcid )
471 if (user) user_lcid = lcid;
475 system_ui_language = LANGIDFROMLCID(lcid); /* there is no separate call to set it */
477 return STATUS_SUCCESS;
481 /**********************************************************************
482 * NtQueryDefaultUILanguage (NTDLL.@)
484 NTSTATUS WINAPI NtQueryDefaultUILanguage( LANGID *lang )
486 *lang = user_ui_language;
487 return STATUS_SUCCESS;
491 /**********************************************************************
492 * NtSetDefaultUILanguage (NTDLL.@)
494 NTSTATUS WINAPI NtSetDefaultUILanguage( LANGID lang )
496 user_ui_language = lang;
497 return STATUS_SUCCESS;
501 /**********************************************************************
502 * NtQueryInstallUILanguage (NTDLL.@)
504 NTSTATUS WINAPI NtQueryInstallUILanguage( LANGID *lang )
506 *lang = system_ui_language;
507 return STATUS_SUCCESS;