ntoskrnl.exe: Implement ExAcquireFastMutex and ExReleaseFastMutex.
[wine.git] / dlls / ntdll / resource.c
blobf649e200cba25544a47c56d109a9862bf3a95f77
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 #define WIN32_NO_STATUS
39 #include "windef.h"
40 #include "winbase.h"
41 #include "winnt.h"
42 #include "winternl.h"
43 #include "wine/asm.h"
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 #define IS_INTRESOURCE(x) (((ULONG_PTR)(x) >> 16) == 0)
55 /**********************************************************************
56 * is_data_file_module
58 * Check if a module handle is for a LOAD_LIBRARY_AS_DATAFILE module.
60 static inline BOOL is_data_file_module( HMODULE hmod )
62 return (ULONG_PTR)hmod & 1;
66 /**********************************************************************
67 * push_language
69 * push a language in the list of languages to try
71 static inline int push_language( WORD *list, int pos, WORD lang )
73 int i;
74 for (i = 0; i < pos; i++) if (list[i] == lang) return pos;
75 list[pos++] = lang;
76 return pos;
80 /**********************************************************************
81 * find_first_entry
83 * Find the first suitable entry in a resource directory
85 static const IMAGE_RESOURCE_DIRECTORY *find_first_entry( const IMAGE_RESOURCE_DIRECTORY *dir,
86 const void *root, int want_dir )
88 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
89 int pos;
91 for (pos = 0; pos < dir->NumberOfNamedEntries + dir->NumberOfIdEntries; pos++)
93 if (!entry[pos].u2.s2.DataIsDirectory == !want_dir)
94 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s2.OffsetToDirectory);
96 return NULL;
100 /**********************************************************************
101 * find_entry_by_id
103 * Find an entry by id in a resource directory
105 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
106 WORD id, const void *root, int want_dir )
108 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
109 int min, max, pos;
111 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
112 min = dir->NumberOfNamedEntries;
113 max = min + dir->NumberOfIdEntries - 1;
114 while (min <= max)
116 pos = (min + max) / 2;
117 if (entry[pos].u.Id == id)
119 if (!entry[pos].u2.s2.DataIsDirectory == !want_dir)
121 TRACE("root %p dir %p id %04x ret %p\n",
122 root, dir, id, (const char*)root + entry[pos].u2.s2.OffsetToDirectory);
123 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s2.OffsetToDirectory);
125 break;
127 if (entry[pos].u.Id > id) max = pos - 1;
128 else min = pos + 1;
130 TRACE("root %p dir %p id %04x not found\n", root, dir, id );
131 return NULL;
135 /**********************************************************************
136 * find_entry_by_name
138 * Find an entry by name in a resource directory
140 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_name( const IMAGE_RESOURCE_DIRECTORY *dir,
141 LPCWSTR name, const void *root,
142 int want_dir )
144 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
145 const IMAGE_RESOURCE_DIR_STRING_U *str;
146 int min, max, res, pos, namelen;
148 if (IS_INTRESOURCE(name)) return find_entry_by_id( dir, LOWORD(name), root, want_dir );
149 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
150 namelen = strlenW(name);
151 min = 0;
152 max = dir->NumberOfNamedEntries - 1;
153 while (min <= max)
155 pos = (min + max) / 2;
156 str = (const IMAGE_RESOURCE_DIR_STRING_U *)((const char *)root + entry[pos].u.s.NameOffset);
157 res = strncmpW( name, str->NameString, str->Length );
158 if (!res && namelen == str->Length)
160 if (!entry[pos].u2.s2.DataIsDirectory == !want_dir)
162 TRACE("root %p dir %p name %s ret %p\n",
163 root, dir, debugstr_w(name), (const char*)root + entry[pos].u2.s2.OffsetToDirectory);
164 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s2.OffsetToDirectory);
166 break;
168 if (res < 0) max = pos - 1;
169 else min = pos + 1;
171 TRACE("root %p dir %p name %s not found\n", root, dir, debugstr_w(name) );
172 return NULL;
176 /**********************************************************************
177 * find_entry
179 * Find a resource entry
181 static NTSTATUS find_entry( HMODULE hmod, const LDR_RESOURCE_INFO *info,
182 ULONG level, const void **ret, int want_dir )
184 ULONG size;
185 const void *root;
186 const IMAGE_RESOURCE_DIRECTORY *resdirptr;
187 WORD list[9]; /* list of languages to try */
188 int i, pos = 0;
190 root = RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_RESOURCE, &size );
191 if (!root) return STATUS_RESOURCE_DATA_NOT_FOUND;
192 if (size < sizeof(*resdirptr)) return STATUS_RESOURCE_DATA_NOT_FOUND;
193 resdirptr = root;
195 if (!level--) goto done;
196 if (!(*ret = find_entry_by_name( resdirptr, (LPCWSTR)info->Type, root, want_dir || level )))
197 return STATUS_RESOURCE_TYPE_NOT_FOUND;
198 if (!level--) return STATUS_SUCCESS;
200 resdirptr = *ret;
201 if (!(*ret = find_entry_by_name( resdirptr, (LPCWSTR)info->Name, root, want_dir || level )))
202 return STATUS_RESOURCE_NAME_NOT_FOUND;
203 if (!level--) return STATUS_SUCCESS;
204 if (level) return STATUS_INVALID_PARAMETER; /* level > 3 */
206 /* 1. specified language */
207 pos = push_language( list, pos, info->Language );
209 /* 2. specified language with neutral sublanguage */
210 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(info->Language), SUBLANG_NEUTRAL ) );
212 /* 3. neutral language with neutral sublanguage */
213 pos = push_language( list, pos, MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ) );
215 /* if no explicitly specified language, try some defaults */
216 if (PRIMARYLANGID(info->Language) == LANG_NEUTRAL)
218 /* user defaults, unless SYS_DEFAULT sublanguage specified */
219 if (SUBLANGID(info->Language) != SUBLANG_SYS_DEFAULT)
221 /* 4. current thread locale language */
222 pos = push_language( list, pos, LANGIDFROMLCID(NtCurrentTeb()->CurrentLocale) );
224 /* 5. user locale language */
225 pos = push_language( list, pos, LANGIDFROMLCID(user_lcid) );
227 /* 6. user locale language with neutral sublanguage */
228 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(user_lcid), SUBLANG_NEUTRAL ) );
231 /* now system defaults */
233 /* 7. system locale language */
234 pos = push_language( list, pos, LANGIDFROMLCID( system_lcid ) );
236 /* 8. system locale language with neutral sublanguage */
237 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(system_lcid), SUBLANG_NEUTRAL ) );
239 /* 9. English */
240 pos = push_language( list, pos, MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT ) );
243 resdirptr = *ret;
244 for (i = 0; i < pos; i++)
245 if ((*ret = find_entry_by_id( resdirptr, list[i], root, want_dir ))) return STATUS_SUCCESS;
247 /* if no explicitly specified language, return the first entry */
248 if (PRIMARYLANGID(info->Language) == LANG_NEUTRAL)
250 if ((*ret = find_first_entry( resdirptr, root, want_dir ))) return STATUS_SUCCESS;
252 return STATUS_RESOURCE_LANG_NOT_FOUND;
254 done:
255 *ret = resdirptr;
256 return STATUS_SUCCESS;
260 /**********************************************************************
261 * LdrFindResourceDirectory_U (NTDLL.@)
263 NTSTATUS WINAPI DECLSPEC_HOTPATCH LdrFindResourceDirectory_U( HMODULE hmod, const LDR_RESOURCE_INFO *info,
264 ULONG level, const IMAGE_RESOURCE_DIRECTORY **dir )
266 const void *res;
267 NTSTATUS status;
269 __TRY
271 if (info) TRACE( "module %p type %s name %s lang %04x level %d\n",
272 hmod, debugstr_w((LPCWSTR)info->Type),
273 level > 1 ? debugstr_w((LPCWSTR)info->Name) : "",
274 level > 2 ? info->Language : 0, level );
276 status = find_entry( hmod, info, level, &res, TRUE );
277 if (status == STATUS_SUCCESS) *dir = res;
279 __EXCEPT_PAGE_FAULT
281 return GetExceptionCode();
283 __ENDTRY;
284 return status;
288 /**********************************************************************
289 * LdrFindResource_U (NTDLL.@)
291 NTSTATUS WINAPI DECLSPEC_HOTPATCH LdrFindResource_U( HMODULE hmod, const LDR_RESOURCE_INFO *info,
292 ULONG level, const IMAGE_RESOURCE_DATA_ENTRY **entry )
294 const void *res;
295 NTSTATUS status;
297 __TRY
299 if (info) TRACE( "module %p type %s name %s lang %04x level %d\n",
300 hmod, debugstr_w((LPCWSTR)info->Type),
301 level > 1 ? debugstr_w((LPCWSTR)info->Name) : "",
302 level > 2 ? info->Language : 0, level );
304 status = find_entry( hmod, info, level, &res, FALSE );
305 if (status == STATUS_SUCCESS) *entry = res;
307 __EXCEPT_PAGE_FAULT
309 return GetExceptionCode();
311 __ENDTRY;
312 return status;
316 /* don't penalize other platforms stuff needed on i386 for compatibility */
317 #ifdef __i386__
318 NTSTATUS WINAPI DECLSPEC_HIDDEN access_resource( HMODULE hmod, const IMAGE_RESOURCE_DATA_ENTRY *entry,
319 void **ptr, ULONG *size )
320 #else
321 static inline NTSTATUS access_resource( HMODULE hmod, const IMAGE_RESOURCE_DATA_ENTRY *entry,
322 void **ptr, ULONG *size )
323 #endif
325 NTSTATUS status;
327 __TRY
329 ULONG dirsize;
331 if (!RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_RESOURCE, &dirsize ))
332 status = STATUS_RESOURCE_DATA_NOT_FOUND;
333 else
335 if (ptr)
337 BOOL is_data_file = is_data_file_module(hmod);
338 hmod = (HMODULE)((ULONG_PTR)hmod & ~3);
339 if (is_data_file)
340 *ptr = RtlImageRvaToVa( RtlImageNtHeader(hmod), hmod, entry->OffsetToData, NULL );
341 else
342 *ptr = (char *)hmod + entry->OffsetToData;
344 if (size) *size = entry->Size;
345 status = STATUS_SUCCESS;
348 __EXCEPT_PAGE_FAULT
350 return GetExceptionCode();
352 __ENDTRY;
353 return status;
356 /**********************************************************************
357 * LdrAccessResource (NTDLL.@)
359 * NOTE
360 * On x86, Shrinker, an executable compressor, depends on the
361 * "call access_resource" instruction being there.
363 #ifdef __i386__
364 __ASM_STDCALL_FUNC( LdrAccessResource, 16,
365 "pushl %ebp\n\t"
366 "movl %esp, %ebp\n\t"
367 "subl $4,%esp\n\t"
368 "pushl 24(%ebp)\n\t"
369 "pushl 20(%ebp)\n\t"
370 "pushl 16(%ebp)\n\t"
371 "pushl 12(%ebp)\n\t"
372 "pushl 8(%ebp)\n\t"
373 "call " __ASM_NAME("access_resource") "\n\t"
374 "leave\n\t"
375 "ret $16"
377 #else
378 NTSTATUS WINAPI LdrAccessResource( HMODULE hmod, const IMAGE_RESOURCE_DATA_ENTRY *entry,
379 void **ptr, ULONG *size )
381 return access_resource( hmod, entry, ptr, size );
383 #endif
385 /**********************************************************************
386 * RtlFindMessage (NTDLL.@)
388 NTSTATUS WINAPI RtlFindMessage( HMODULE hmod, ULONG type, ULONG lang,
389 ULONG msg_id, const MESSAGE_RESOURCE_ENTRY **ret )
391 const MESSAGE_RESOURCE_DATA *data;
392 const MESSAGE_RESOURCE_BLOCK *block;
393 const IMAGE_RESOURCE_DATA_ENTRY *rsrc;
394 LDR_RESOURCE_INFO info;
395 NTSTATUS status;
396 void *ptr;
397 unsigned int i;
399 info.Type = type;
400 info.Name = 1;
401 info.Language = lang;
403 if ((status = LdrFindResource_U( hmod, &info, 3, &rsrc )) != STATUS_SUCCESS)
404 return status;
405 if ((status = LdrAccessResource( hmod, rsrc, &ptr, NULL )) != STATUS_SUCCESS)
406 return status;
408 data = ptr;
409 block = data->Blocks;
410 for (i = 0; i < data->NumberOfBlocks; i++, block++)
412 if (msg_id >= block->LowId && msg_id <= block->HighId)
414 const MESSAGE_RESOURCE_ENTRY *entry;
416 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)data + block->OffsetToEntries);
417 for (i = msg_id - block->LowId; i > 0; i--)
418 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)entry + entry->Length);
419 *ret = entry;
420 return STATUS_SUCCESS;
423 return STATUS_MESSAGE_NOT_FOUND;
426 /**********************************************************************
427 * RtlFormatMessage (NTDLL.@)
429 * Formats a message (similar to sprintf).
431 * PARAMS
432 * Message [I] Message to format.
433 * MaxWidth [I] Maximum width in characters of each output line.
434 * IgnoreInserts [I] Whether to copy the message without processing inserts.
435 * Ansi [I] Whether Arguments may have ANSI strings.
436 * ArgumentsIsArray [I] Whether Arguments is actually an array rather than a va_list *.
437 * Buffer [O] Buffer to store processed message in.
438 * BufferSize [I] Size of Buffer (in bytes?).
440 * RETURNS
441 * NTSTATUS code.
443 NTSTATUS WINAPI RtlFormatMessage( LPWSTR Message, UCHAR MaxWidth,
444 BOOLEAN IgnoreInserts, BOOLEAN Ansi,
445 BOOLEAN ArgumentIsArray, __ms_va_list * Arguments,
446 LPWSTR Buffer, ULONG BufferSize )
448 FIXME("(%s, %u, %s, %s, %s, %p, %p, %d)\n", debugstr_w(Message),
449 MaxWidth, IgnoreInserts ? "TRUE" : "FALSE", Ansi ? "TRUE" : "FALSE",
450 ArgumentIsArray ? "TRUE" : "FALSE", Arguments, Buffer, BufferSize);
451 return STATUS_SUCCESS;
455 /**********************************************************************
456 * NtQueryDefaultLocale (NTDLL.@)
458 NTSTATUS WINAPI NtQueryDefaultLocale( BOOLEAN user, LCID *lcid )
460 *lcid = user ? user_lcid : system_lcid;
461 return STATUS_SUCCESS;
465 /**********************************************************************
466 * NtSetDefaultLocale (NTDLL.@)
468 NTSTATUS WINAPI NtSetDefaultLocale( BOOLEAN user, LCID lcid )
470 if (user) user_lcid = lcid;
471 else
473 system_lcid = lcid;
474 system_ui_language = LANGIDFROMLCID(lcid); /* there is no separate call to set it */
476 return STATUS_SUCCESS;
480 /**********************************************************************
481 * NtQueryDefaultUILanguage (NTDLL.@)
483 NTSTATUS WINAPI NtQueryDefaultUILanguage( LANGID *lang )
485 *lang = user_ui_language;
486 return STATUS_SUCCESS;
490 /**********************************************************************
491 * NtSetDefaultUILanguage (NTDLL.@)
493 NTSTATUS WINAPI NtSetDefaultUILanguage( LANGID lang )
495 user_ui_language = lang;
496 return STATUS_SUCCESS;
500 /**********************************************************************
501 * NtQueryInstallUILanguage (NTDLL.@)
503 NTSTATUS WINAPI NtQueryInstallUILanguage( LANGID *lang )
505 *lang = system_ui_language;
506 return STATUS_SUCCESS;