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
29 #include "wine/port.h"
33 #include <sys/types.h>
35 #define NONAMELESSUNION
36 #define NONAMELESSSTRUCT
38 #define WIN32_NO_STATUS
43 #include "wine/exception.h"
44 #include "wine/unicode.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(resource);
49 static LCID user_lcid, system_lcid;
50 static LANGID user_ui_language, system_ui_language;
52 /**********************************************************************
55 * Check if a module handle is for a LOAD_LIBRARY_AS_DATAFILE module.
57 static inline int is_data_file_module( HMODULE hmod )
59 return (ULONG_PTR)hmod & 1;
63 /**********************************************************************
66 * push a language in the list of languages to try
68 static inline int push_language( WORD *list, int pos, WORD lang )
71 for (i = 0; i < pos; i++) if (list[i] == lang) return pos;
77 /**********************************************************************
80 * Find the first suitable entry in a resource directory
82 static const IMAGE_RESOURCE_DIRECTORY *find_first_entry( const IMAGE_RESOURCE_DIRECTORY *dir,
83 const void *root, int want_dir )
85 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
88 for (pos = 0; pos < dir->NumberOfNamedEntries + dir->NumberOfIdEntries; pos++)
90 if (!entry[pos].u2.s3.DataIsDirectory == !want_dir)
91 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s3.OffsetToDirectory);
97 /**********************************************************************
100 * Find an entry by id in a resource directory
102 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
103 WORD id, const void *root, int want_dir )
105 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
108 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
109 min = dir->NumberOfNamedEntries;
110 max = min + dir->NumberOfIdEntries - 1;
113 pos = (min + max) / 2;
114 if (entry[pos].u1.s2.Id == id)
116 if (!entry[pos].u2.s3.DataIsDirectory == !want_dir)
118 TRACE("root %p dir %p id %04x ret %p\n",
119 root, dir, id, (const char*)root + entry[pos].u2.s3.OffsetToDirectory);
120 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s3.OffsetToDirectory);
124 if (entry[pos].u1.s2.Id > id) max = pos - 1;
127 TRACE("root %p dir %p id %04x not found\n", root, dir, id );
132 /**********************************************************************
135 * Find an entry by name in a resource directory
137 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_name( const IMAGE_RESOURCE_DIRECTORY *dir,
138 LPCWSTR name, const void *root,
141 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
142 const IMAGE_RESOURCE_DIR_STRING_U *str;
143 int min, max, res, pos, namelen;
145 if (!HIWORD(name)) return find_entry_by_id( dir, LOWORD(name), root, want_dir );
146 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
147 namelen = strlenW(name);
149 max = dir->NumberOfNamedEntries - 1;
152 pos = (min + max) / 2;
153 str = (const IMAGE_RESOURCE_DIR_STRING_U *)((const char *)root + entry[pos].u1.s1.NameOffset);
154 res = strncmpW( name, str->NameString, str->Length );
155 if (!res && namelen == str->Length)
157 if (!entry[pos].u2.s3.DataIsDirectory == !want_dir)
159 TRACE("root %p dir %p name %s ret %p\n",
160 root, dir, debugstr_w(name), (const char*)root + entry[pos].u2.s3.OffsetToDirectory);
161 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s3.OffsetToDirectory);
165 if (res < 0) max = pos - 1;
168 TRACE("root %p dir %p name %s not found\n", root, dir, debugstr_w(name) );
173 /**********************************************************************
176 * Find a resource entry
178 static NTSTATUS find_entry( HMODULE hmod, const LDR_RESOURCE_INFO *info,
179 ULONG level, const void **ret, int want_dir )
183 const IMAGE_RESOURCE_DIRECTORY *resdirptr;
184 WORD list[9]; /* list of languages to try */
187 root = RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_RESOURCE, &size );
188 if (!root) return STATUS_RESOURCE_DATA_NOT_FOUND;
191 if (!level--) goto done;
192 if (!(*ret = find_entry_by_name( resdirptr, (LPCWSTR)info->Type, root, want_dir || level )))
193 return STATUS_RESOURCE_TYPE_NOT_FOUND;
194 if (!level--) return STATUS_SUCCESS;
197 if (!(*ret = find_entry_by_name( resdirptr, (LPCWSTR)info->Name, root, want_dir || level )))
198 return STATUS_RESOURCE_NAME_NOT_FOUND;
199 if (!level--) return STATUS_SUCCESS;
200 if (level) return STATUS_INVALID_PARAMETER; /* level > 3 */
202 /* 1. specified language */
203 pos = push_language( list, pos, info->Language );
205 /* 2. specified language with neutral sublanguage */
206 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(info->Language), SUBLANG_NEUTRAL ) );
208 /* 3. neutral language with neutral sublanguage */
209 pos = push_language( list, pos, MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ) );
211 /* if no explicitly specified language, try some defaults */
212 if (PRIMARYLANGID(info->Language) == LANG_NEUTRAL)
214 /* user defaults, unless SYS_DEFAULT sublanguage specified */
215 if (SUBLANGID(info->Language) != SUBLANG_SYS_DEFAULT)
217 /* 4. current thread locale language */
218 pos = push_language( list, pos, LANGIDFROMLCID(NtCurrentTeb()->CurrentLocale) );
220 /* 5. user locale language */
221 pos = push_language( list, pos, LANGIDFROMLCID(user_lcid) );
223 /* 6. user locale language with neutral sublanguage */
224 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(user_lcid), SUBLANG_NEUTRAL ) );
227 /* now system defaults */
229 /* 7. system locale language */
230 pos = push_language( list, pos, LANGIDFROMLCID( system_lcid ) );
232 /* 8. system locale language with neutral sublanguage */
233 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(system_lcid), SUBLANG_NEUTRAL ) );
236 pos = push_language( list, pos, MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT ) );
240 for (i = 0; i < pos; i++)
241 if ((*ret = find_entry_by_id( resdirptr, list[i], root, want_dir ))) return STATUS_SUCCESS;
243 /* if no explicitly specified language, return the first entry */
244 if (PRIMARYLANGID(info->Language) == LANG_NEUTRAL)
246 if ((*ret = find_first_entry( resdirptr, root, want_dir ))) return STATUS_SUCCESS;
248 return STATUS_RESOURCE_LANG_NOT_FOUND;
252 return STATUS_SUCCESS;
256 /**********************************************************************
257 * LdrFindResourceDirectory_U (NTDLL.@)
259 NTSTATUS WINAPI LdrFindResourceDirectory_U( HMODULE hmod, const LDR_RESOURCE_INFO *info,
260 ULONG level, const IMAGE_RESOURCE_DIRECTORY **dir )
267 if (info) TRACE( "module %p type %s name %s lang %04x level %d\n",
268 hmod, debugstr_w((LPCWSTR)info->Type),
269 level > 1 ? debugstr_w((LPCWSTR)info->Name) : "",
270 level > 2 ? info->Language : 0, level );
272 status = find_entry( hmod, info, level, &res, TRUE );
273 if (status == STATUS_SUCCESS) *dir = res;
277 return GetExceptionCode();
284 /**********************************************************************
285 * LdrFindResource_U (NTDLL.@)
287 NTSTATUS WINAPI LdrFindResource_U( HMODULE hmod, const LDR_RESOURCE_INFO *info,
288 ULONG level, const IMAGE_RESOURCE_DATA_ENTRY **entry )
295 if (info) TRACE( "module %p type %s name %s lang %04x level %d\n",
296 hmod, debugstr_w((LPCWSTR)info->Type),
297 level > 1 ? debugstr_w((LPCWSTR)info->Name) : "",
298 level > 2 ? info->Language : 0, level );
300 status = find_entry( hmod, info, level, &res, FALSE );
301 if (status == STATUS_SUCCESS) *entry = res;
305 return GetExceptionCode();
312 /* don't penalize other platforms stuff needed on i386 for compatibility */
314 NTSTATUS WINAPI access_resource( HMODULE hmod, const IMAGE_RESOURCE_DATA_ENTRY *entry,
315 void **ptr, ULONG *size )
317 static inline NTSTATUS access_resource( HMODULE hmod, const IMAGE_RESOURCE_DATA_ENTRY *entry,
318 void **ptr, ULONG *size )
327 if (!RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_RESOURCE, &dirsize ))
328 status = STATUS_RESOURCE_DATA_NOT_FOUND;
333 if (is_data_file_module(hmod))
335 HMODULE mod = (HMODULE)((ULONG_PTR)hmod & ~1);
336 *ptr = RtlImageRvaToVa( RtlImageNtHeader(mod), mod, entry->OffsetToData, NULL );
338 else *ptr = (char *)hmod + entry->OffsetToData;
340 if (size) *size = entry->Size;
341 status = STATUS_SUCCESS;
346 return GetExceptionCode();
352 /**********************************************************************
353 * LdrAccessResource (NTDLL.@)
356 * On x86, Shrinker, an executable compressor, depends on the
357 * "call access_resource" instruction being there.
360 __ASM_GLOBAL_FUNC( LdrAccessResource,
362 "movl %esp, %ebp\n\t"
369 "call " __ASM_NAME("access_resource") "\n\t"
374 NTSTATUS WINAPI LdrAccessResource( HMODULE hmod, const IMAGE_RESOURCE_DATA_ENTRY *entry,
375 void **ptr, ULONG *size )
377 return access_resource( hmod, entry, ptr, size );
381 /**********************************************************************
382 * RtlFindMessage (NTDLL.@)
384 NTSTATUS WINAPI RtlFindMessage( HMODULE hmod, ULONG type, ULONG lang,
385 ULONG msg_id, const MESSAGE_RESOURCE_ENTRY **ret )
387 const MESSAGE_RESOURCE_DATA *data;
388 const MESSAGE_RESOURCE_BLOCK *block;
389 const IMAGE_RESOURCE_DATA_ENTRY *rsrc;
390 LDR_RESOURCE_INFO info;
397 info.Language = lang;
399 if ((status = LdrFindResource_U( hmod, &info, 3, &rsrc )) != STATUS_SUCCESS)
401 if ((status = LdrAccessResource( hmod, rsrc, &ptr, NULL )) != STATUS_SUCCESS)
405 block = data->Blocks;
406 for (i = 0; i < data->NumberOfBlocks; i++, block++)
408 if (msg_id >= block->LowId && msg_id <= block->HighId)
410 const MESSAGE_RESOURCE_ENTRY *entry;
412 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)data + block->OffsetToEntries);
413 for (i = msg_id - block->LowId; i > 0; i--)
414 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)entry + entry->Length);
416 return STATUS_SUCCESS;
419 return STATUS_MESSAGE_NOT_FOUND;
422 /**********************************************************************
423 * RtlFormatMessage (NTDLL.@)
425 * Formats a message (similar to sprintf).
428 * Message [I] Message to format.
429 * MaxWidth [I] Maximum width in characters of each output line.
430 * IgnoreInserts [I] Whether to copy the message without processing inserts.
431 * Ansi [I] Whether Arguments may have ANSI strings.
432 * ArgumentsIsArray [I] Whether Arguments is actually an array rather than a va_list *.
433 * Buffer [O] Buffer to store processed message in.
434 * BufferSize [I] Size of Buffer (in bytes?).
439 NTSTATUS WINAPI RtlFormatMessage( LPWSTR Message, UCHAR MaxWidth,
440 BOOLEAN IgnoreInserts, BOOLEAN Ansi,
441 BOOLEAN ArgumentIsArray, __ms_va_list * Arguments,
442 LPWSTR Buffer, ULONG BufferSize )
444 FIXME("(%s, %u, %s, %s, %s, %p, %p, %d)\n", debugstr_w(Message),
445 MaxWidth, IgnoreInserts ? "TRUE" : "FALSE", Ansi ? "TRUE" : "FALSE",
446 ArgumentIsArray ? "TRUE" : "FALSE", Arguments, Buffer, BufferSize);
447 return STATUS_SUCCESS;
451 /**********************************************************************
452 * NtQueryDefaultLocale (NTDLL.@)
454 NTSTATUS WINAPI NtQueryDefaultLocale( BOOLEAN user, LCID *lcid )
456 *lcid = user ? user_lcid : system_lcid;
457 return STATUS_SUCCESS;
461 /**********************************************************************
462 * NtSetDefaultLocale (NTDLL.@)
464 NTSTATUS WINAPI NtSetDefaultLocale( BOOLEAN user, LCID lcid )
466 if (user) user_lcid = lcid;
470 system_ui_language = LANGIDFROMLCID(lcid); /* there is no separate call to set it */
472 return STATUS_SUCCESS;
476 /**********************************************************************
477 * NtQueryDefaultUILanguage (NTDLL.@)
479 NTSTATUS WINAPI NtQueryDefaultUILanguage( LANGID *lang )
481 *lang = user_ui_language;
482 return STATUS_SUCCESS;
486 /**********************************************************************
487 * NtSetDefaultUILanguage (NTDLL.@)
489 NTSTATUS WINAPI NtSetDefaultUILanguage( LANGID lang )
491 user_ui_language = lang;
492 return STATUS_SUCCESS;
496 /**********************************************************************
497 * NtQueryInstallUILanguage (NTDLL.@)
499 NTSTATUS WINAPI NtQueryInstallUILanguage( LANGID *lang )
501 *lang = system_ui_language;
502 return STATUS_SUCCESS;