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
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;
54 static LANGID user_ui_language, system_ui_language;
56 static WINE_EXCEPTION_FILTER(page_fault)
58 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ||
59 GetExceptionCode() == EXCEPTION_PRIV_INSTRUCTION)
60 return EXCEPTION_EXECUTE_HANDLER;
61 return EXCEPTION_CONTINUE_SEARCH;
64 /**********************************************************************
67 * Check if a module handle is for a LOAD_LIBRARY_AS_DATAFILE module.
69 inline static int is_data_file_module( HMODULE hmod )
71 return (ULONG_PTR)hmod & 1;
75 /**********************************************************************
78 * push a language in the list of languages to try
80 static inline int push_language( WORD *list, int pos, WORD lang )
83 for (i = 0; i < pos; i++) if (list[i] == lang) return pos;
89 /**********************************************************************
92 * Find the first suitable entry in a resource directory
94 static const IMAGE_RESOURCE_DIRECTORY *find_first_entry( const IMAGE_RESOURCE_DIRECTORY *dir,
95 const void *root, int want_dir )
97 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
100 for (pos = 0; pos < dir->NumberOfNamedEntries + dir->NumberOfIdEntries; pos++)
102 if (!entry[pos].u2.s3.DataIsDirectory == !want_dir)
103 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s3.OffsetToDirectory);
109 /**********************************************************************
112 * Find an entry by id in a resource directory
114 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
115 WORD id, const void *root, int want_dir )
117 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
120 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
121 min = dir->NumberOfNamedEntries;
122 max = min + dir->NumberOfIdEntries - 1;
125 pos = (min + max) / 2;
126 if (entry[pos].u1.s2.Id == id)
128 if (!entry[pos].u2.s3.DataIsDirectory == !want_dir)
130 TRACE("root %p dir %p id %04x ret %p\n",
131 root, dir, id, (const char*)root + entry[pos].u2.s3.OffsetToDirectory);
132 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s3.OffsetToDirectory);
136 if (entry[pos].u1.s2.Id > id) max = pos - 1;
139 TRACE("root %p dir %p id %04x not found\n", root, dir, id );
144 /**********************************************************************
147 * Find an entry by name in a resource directory
149 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_name( const IMAGE_RESOURCE_DIRECTORY *dir,
150 LPCWSTR name, const void *root,
153 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
154 const IMAGE_RESOURCE_DIR_STRING_U *str;
155 int min, max, res, pos, namelen;
157 if (!HIWORD(name)) return find_entry_by_id( dir, LOWORD(name), root, want_dir );
158 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
159 namelen = strlenW(name);
161 max = dir->NumberOfNamedEntries - 1;
164 pos = (min + max) / 2;
165 str = (const IMAGE_RESOURCE_DIR_STRING_U *)((const char *)root + entry[pos].u1.s1.NameOffset);
166 res = strncmpW( name, str->NameString, str->Length );
167 if (!res && namelen == str->Length)
169 if (!entry[pos].u2.s3.DataIsDirectory == !want_dir)
171 TRACE("root %p dir %p name %s ret %p\n",
172 root, dir, debugstr_w(name), (const char*)root + entry[pos].u2.s3.OffsetToDirectory);
173 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s3.OffsetToDirectory);
177 if (res < 0) max = pos - 1;
180 TRACE("root %p dir %p name %s not found\n", root, dir, debugstr_w(name) );
185 /**********************************************************************
188 * Find a resource entry
190 static NTSTATUS find_entry( HMODULE hmod, const LDR_RESOURCE_INFO *info,
191 ULONG level, const void **ret, int want_dir )
195 const IMAGE_RESOURCE_DIRECTORY *resdirptr;
196 WORD list[9]; /* list of languages to try */
199 root = RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_RESOURCE, &size );
200 if (!root) return STATUS_RESOURCE_DATA_NOT_FOUND;
203 if (!level--) goto done;
204 if (!(*ret = find_entry_by_name( resdirptr, (LPCWSTR)info->Type, root, want_dir || level )))
205 return STATUS_RESOURCE_TYPE_NOT_FOUND;
206 if (!level--) return STATUS_SUCCESS;
209 if (!(*ret = find_entry_by_name( resdirptr, (LPCWSTR)info->Name, root, want_dir || level )))
210 return STATUS_RESOURCE_NAME_NOT_FOUND;
211 if (!level--) return STATUS_SUCCESS;
212 if (level) return STATUS_INVALID_PARAMETER; /* level > 3 */
214 /* 1. specified language */
215 pos = push_language( list, pos, info->Language );
217 /* 2. specified language with neutral sublanguage */
218 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(info->Language), SUBLANG_NEUTRAL ) );
220 /* 3. neutral language with neutral sublanguage */
221 pos = push_language( list, pos, MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ) );
223 /* if no explicitly specified language, try some defaults */
224 if (PRIMARYLANGID(info->Language) == LANG_NEUTRAL)
226 /* user defaults, unless SYS_DEFAULT sublanguage specified */
227 if (SUBLANGID(info->Language) != SUBLANG_SYS_DEFAULT)
229 /* 4. current thread locale language */
230 pos = push_language( list, pos, LANGIDFROMLCID(NtCurrentTeb()->CurrentLocale) );
232 /* 5. user locale language */
233 pos = push_language( list, pos, LANGIDFROMLCID(user_lcid) );
235 /* 6. user locale language with neutral sublanguage */
236 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(user_lcid), SUBLANG_NEUTRAL ) );
239 /* now system defaults */
241 /* 7. system locale language */
242 pos = push_language( list, pos, LANGIDFROMLCID( system_lcid ) );
244 /* 8. system locale language with neutral sublanguage */
245 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(system_lcid), SUBLANG_NEUTRAL ) );
248 pos = push_language( list, pos, MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT ) );
252 for (i = 0; i < pos; i++)
253 if ((*ret = find_entry_by_id( resdirptr, list[i], root, want_dir ))) return STATUS_SUCCESS;
255 /* if no explicitly specified language, return the first entry */
256 if (PRIMARYLANGID(info->Language) == LANG_NEUTRAL)
258 if ((*ret = find_first_entry( resdirptr, root, want_dir ))) return STATUS_SUCCESS;
260 return STATUS_RESOURCE_LANG_NOT_FOUND;
264 return STATUS_SUCCESS;
268 /**********************************************************************
269 * LdrFindResourceDirectory_U (NTDLL.@)
271 NTSTATUS WINAPI LdrFindResourceDirectory_U( HMODULE hmod, const LDR_RESOURCE_INFO *info,
272 ULONG level, const IMAGE_RESOURCE_DIRECTORY **dir )
277 if (info) TRACE( "module %p type %s name %s lang %04lx level %ld\n",
278 hmod, debugstr_w((LPCWSTR)info->Type),
279 level > 1 ? debugstr_w((LPCWSTR)info->Name) : "",
280 level > 2 ? info->Language : 0, level );
284 status = find_entry( hmod, info, level, &res, TRUE );
285 if (status == STATUS_SUCCESS) *dir = res;
289 return GetExceptionCode();
296 /**********************************************************************
297 * LdrFindResource_U (NTDLL.@)
299 NTSTATUS WINAPI LdrFindResource_U( HMODULE hmod, const LDR_RESOURCE_INFO *info,
300 ULONG level, const IMAGE_RESOURCE_DATA_ENTRY **entry )
305 if (info) TRACE( "module %p type %s name %s lang %04lx level %ld\n",
306 hmod, debugstr_w((LPCWSTR)info->Type),
307 level > 1 ? debugstr_w((LPCWSTR)info->Name) : "",
308 level > 2 ? info->Language : 0, level );
312 status = find_entry( hmod, info, level, &res, FALSE );
313 if (status == STATUS_SUCCESS) *entry = res;
317 return GetExceptionCode();
324 /**********************************************************************
325 * LdrAccessResource (NTDLL.@)
327 NTSTATUS WINAPI LdrAccessResource( HMODULE hmod, const IMAGE_RESOURCE_DATA_ENTRY *entry,
328 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();
362 /**********************************************************************
363 * RtlFindMessage (NTDLL.@)
365 NTSTATUS WINAPI RtlFindMessage( HMODULE hmod, ULONG type, ULONG lang,
366 ULONG msg_id, const MESSAGE_RESOURCE_ENTRY **ret )
368 const MESSAGE_RESOURCE_DATA *data;
369 const MESSAGE_RESOURCE_BLOCK *block;
370 const IMAGE_RESOURCE_DATA_ENTRY *rsrc;
371 LDR_RESOURCE_INFO info;
378 info.Language = lang;
380 if ((status = LdrFindResource_U( hmod, &info, 3, &rsrc )) != STATUS_SUCCESS)
382 if ((status = LdrAccessResource( hmod, rsrc, &ptr, NULL )) != STATUS_SUCCESS)
386 block = data->Blocks;
387 for (i = 0; i < data->NumberOfBlocks; i++, block++)
389 if (msg_id >= block->LowId && msg_id <= block->HighId)
391 const MESSAGE_RESOURCE_ENTRY *entry;
393 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)data + block->OffsetToEntries);
394 for (i = msg_id - block->LowId; i > 0; i--)
395 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)entry + entry->Length);
397 return STATUS_SUCCESS;
400 return STATUS_MESSAGE_NOT_FOUND;
403 /**********************************************************************
404 * RtlFormatMessage (NTDLL.@)
406 * Formats a message (similar to sprintf).
409 * Message [I] Message to format.
410 * MaxWidth [I] Maximum width in characters of each output line.
411 * IgnoreInserts [I] Whether to copy the message without processing inserts.
412 * Ansi [I] Whether Arguments may have ANSI strings.
413 * ArgumentsIsArray [I] Whether Arguments is actually an array rather than a va_list *.
414 * Buffer [O] Buffer to store processed message in.
415 * BufferSize [I] Size of Buffer (in bytes?).
420 NTSTATUS WINAPI RtlFormatMessage( LPWSTR Message, UCHAR MaxWidth,
421 BOOLEAN IgnoreInserts, BOOLEAN Ansi,
422 BOOLEAN ArgumentIsArray, va_list * Arguments,
423 LPWSTR Buffer, ULONG BufferSize )
425 FIXME("(%s, %u, %s, %s, %s, %p, %p, %ld)\n", debugstr_w(Message),
426 MaxWidth, IgnoreInserts ? "TRUE" : "FALSE", Ansi ? "TRUE" : "FALSE",
427 ArgumentIsArray ? "TRUE" : "FALSE", Arguments, Buffer, BufferSize);
428 return STATUS_SUCCESS;
432 /**********************************************************************
433 * NtQueryDefaultLocale (NTDLL.@)
435 NTSTATUS WINAPI NtQueryDefaultLocale( BOOLEAN user, LCID *lcid )
437 *lcid = user ? user_lcid : system_lcid;
438 return STATUS_SUCCESS;
442 /**********************************************************************
443 * NtSetDefaultLocale (NTDLL.@)
445 NTSTATUS WINAPI NtSetDefaultLocale( BOOLEAN user, LCID lcid )
447 if (user) user_lcid = lcid;
451 system_ui_language = LANGIDFROMLCID(lcid); /* there is no separate call to set it */
453 return STATUS_SUCCESS;
457 /**********************************************************************
458 * NtQueryDefaultUILanguage (NTDLL.@)
460 NTSTATUS WINAPI NtQueryDefaultUILanguage( LANGID *lang )
462 *lang = user_ui_language;
463 return STATUS_SUCCESS;
467 /**********************************************************************
468 * NtSetDefaultUILanguage (NTDLL.@)
470 NTSTATUS WINAPI NtSetDefaultUILanguage( LANGID lang )
472 user_ui_language = lang;
473 return STATUS_SUCCESS;
477 /**********************************************************************
478 * NtQueryInstallUILanguage (NTDLL.@)
480 NTSTATUS WINAPI NtQueryInstallUILanguage( LANGID *lang )
482 *lang = system_ui_language;
483 return STATUS_SUCCESS;