Documentation tweaks to make winapi_check happy.
[wine/winequartzdrv.git] / dlls / ntdll / resource.c
blob0d8aa2cb02464d235dcb8fa5acc549fe0f62fd86
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 #include "windef.h"
39 #include "winbase.h"
40 #include "winnls.h"
41 #include "winnt.h"
42 #include "winternl.h"
43 #include "excpt.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 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 /**********************************************************************
62 * is_data_file_module
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 /**********************************************************************
73 * push_language
75 * push a language in the list of languages to try
77 static inline int push_language( WORD *list, int pos, WORD lang )
79 int i;
80 for (i = 0; i < pos; i++) if (list[i] == lang) return pos;
81 list[pos++] = lang;
82 return pos;
86 /**********************************************************************
87 * find_first_entry
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);
95 int pos;
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);
102 return NULL;
106 /**********************************************************************
107 * find_entry_by_id
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;
115 int min, max, pos;
117 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
118 min = dir->NumberOfNamedEntries;
119 max = min + dir->NumberOfIdEntries - 1;
120 while (min <= max)
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);
131 break;
133 if (entry[pos].u1.s2.Id > id) max = pos - 1;
134 else min = pos + 1;
136 TRACE("root %p dir %p id %04x not found\n", root, dir, id );
137 return NULL;
141 /**********************************************************************
142 * find_entry_by_name
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,
148 int want_dir )
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);
157 min = 0;
158 max = dir->NumberOfNamedEntries - 1;
159 while (min <= max)
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);
172 break;
174 if (res < 0) max = pos - 1;
175 else min = pos + 1;
177 TRACE("root %p dir %p name %s not found\n", root, dir, debugstr_w(name) );
178 return NULL;
182 /**********************************************************************
183 * find_entry
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 )
190 ULONG size;
191 const void *root;
192 const IMAGE_RESOURCE_DIRECTORY *resdirptr;
193 WORD list[9]; /* list of languages to try */
194 int i, pos = 0;
196 root = RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_RESOURCE, &size );
197 if (!root) return STATUS_RESOURCE_DATA_NOT_FOUND;
198 resdirptr = root;
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;
205 resdirptr = *ret;
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 ) );
244 /* 9. English */
245 pos = push_language( list, pos, MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT ) );
248 resdirptr = *ret;
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;
259 done:
260 *ret = resdirptr;
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 )
271 const void *res;
272 NTSTATUS status;
274 __TRY
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;
284 __EXCEPT(page_fault)
286 return GetExceptionCode();
288 __ENDTRY;
289 return status;
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 )
299 const void *res;
300 NTSTATUS status;
302 __TRY
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;
312 __EXCEPT(page_fault)
314 return GetExceptionCode();
316 __ENDTRY;
317 return status;
321 /* don't penalize other platforms stuff needed on i386 for compatibility */
322 #ifdef __i386__
323 NTSTATUS WINAPI access_resource( HMODULE hmod, const IMAGE_RESOURCE_DATA_ENTRY *entry,
324 void **ptr, ULONG *size )
325 #else
326 static inline NTSTATUS access_resource( HMODULE hmod, const IMAGE_RESOURCE_DATA_ENTRY *entry,
327 void **ptr, ULONG *size )
328 #endif
330 NTSTATUS status;
332 __TRY
334 ULONG dirsize;
336 if (!RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_RESOURCE, &dirsize ))
337 status = STATUS_RESOURCE_DATA_NOT_FOUND;
338 else
340 if (ptr)
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;
353 __EXCEPT(page_fault)
355 return GetExceptionCode();
357 __ENDTRY;
358 return status;
361 /**********************************************************************
362 * LdrAccessResource (NTDLL.@)
364 * NOTE
365 * On x86, Shrinker, an executable compressor, depends on the
366 * "call access_resource" instruction being there.
368 #ifdef __i386__
369 __ASM_GLOBAL_FUNC( LdrAccessResource,
370 "pushl %ebp\n\t"
371 "movl %esp, %ebp\n\t"
372 "subl $4,%esp\n\t"
373 "pushl 24(%ebp)\n\t"
374 "pushl 20(%ebp)\n\t"
375 "pushl 16(%ebp)\n\t"
376 "pushl 12(%ebp)\n\t"
377 "pushl 8(%ebp)\n\t"
378 "call " __ASM_NAME("access_resource") "\n\t"
379 "leave\n\t"
380 "ret $16"
382 #else
383 NTSTATUS WINAPI LdrAccessResource( HMODULE hmod, const IMAGE_RESOURCE_DATA_ENTRY *entry,
384 void **ptr, ULONG *size )
386 return access_resource( hmod, entry, ptr, size );
388 #endif
390 /**********************************************************************
391 * RtlFindMessage (NTDLL.@)
393 NTSTATUS WINAPI RtlFindMessage( HMODULE hmod, ULONG type, ULONG lang,
394 ULONG msg_id, const MESSAGE_RESOURCE_ENTRY **ret )
396 const MESSAGE_RESOURCE_DATA *data;
397 const MESSAGE_RESOURCE_BLOCK *block;
398 const IMAGE_RESOURCE_DATA_ENTRY *rsrc;
399 LDR_RESOURCE_INFO info;
400 NTSTATUS status;
401 void *ptr;
402 unsigned int i;
404 info.Type = type;
405 info.Name = 1;
406 info.Language = lang;
408 if ((status = LdrFindResource_U( hmod, &info, 3, &rsrc )) != STATUS_SUCCESS)
409 return status;
410 if ((status = LdrAccessResource( hmod, rsrc, &ptr, NULL )) != STATUS_SUCCESS)
411 return status;
413 data = ptr;
414 block = data->Blocks;
415 for (i = 0; i < data->NumberOfBlocks; i++, block++)
417 if (msg_id >= block->LowId && msg_id <= block->HighId)
419 const MESSAGE_RESOURCE_ENTRY *entry;
421 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)data + block->OffsetToEntries);
422 for (i = msg_id - block->LowId; i > 0; i--)
423 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)entry + entry->Length);
424 *ret = entry;
425 return STATUS_SUCCESS;
428 return STATUS_MESSAGE_NOT_FOUND;
431 /**********************************************************************
432 * RtlFormatMessage (NTDLL.@)
434 * Formats a message (similar to sprintf).
436 * PARAMS
437 * Message [I] Message to format.
438 * MaxWidth [I] Maximum width in characters of each output line.
439 * IgnoreInserts [I] Whether to copy the message without processing inserts.
440 * Ansi [I] Whether Arguments may have ANSI strings.
441 * ArgumentsIsArray [I] Whether Arguments is actually an array rather than a va_list *.
442 * Buffer [O] Buffer to store processed message in.
443 * BufferSize [I] Size of Buffer (in bytes?).
445 * RETURNS
446 * NTSTATUS code.
448 NTSTATUS WINAPI RtlFormatMessage( LPWSTR Message, UCHAR MaxWidth,
449 BOOLEAN IgnoreInserts, BOOLEAN Ansi,
450 BOOLEAN ArgumentIsArray, va_list * Arguments,
451 LPWSTR Buffer, ULONG BufferSize )
453 FIXME("(%s, %u, %s, %s, %s, %p, %p, %ld)\n", debugstr_w(Message),
454 MaxWidth, IgnoreInserts ? "TRUE" : "FALSE", Ansi ? "TRUE" : "FALSE",
455 ArgumentIsArray ? "TRUE" : "FALSE", Arguments, Buffer, BufferSize);
456 return STATUS_SUCCESS;
460 /**********************************************************************
461 * NtQueryDefaultLocale (NTDLL.@)
463 NTSTATUS WINAPI NtQueryDefaultLocale( BOOLEAN user, LCID *lcid )
465 *lcid = user ? user_lcid : system_lcid;
466 return STATUS_SUCCESS;
470 /**********************************************************************
471 * NtSetDefaultLocale (NTDLL.@)
473 NTSTATUS WINAPI NtSetDefaultLocale( BOOLEAN user, LCID lcid )
475 if (user) user_lcid = lcid;
476 else
478 system_lcid = lcid;
479 system_ui_language = LANGIDFROMLCID(lcid); /* there is no separate call to set it */
481 return STATUS_SUCCESS;
485 /**********************************************************************
486 * NtQueryDefaultUILanguage (NTDLL.@)
488 NTSTATUS WINAPI NtQueryDefaultUILanguage( LANGID *lang )
490 *lang = user_ui_language;
491 return STATUS_SUCCESS;
495 /**********************************************************************
496 * NtSetDefaultUILanguage (NTDLL.@)
498 NTSTATUS WINAPI NtSetDefaultUILanguage( LANGID lang )
500 user_ui_language = lang;
501 return STATUS_SUCCESS;
505 /**********************************************************************
506 * NtQueryInstallUILanguage (NTDLL.@)
508 NTSTATUS WINAPI NtQueryInstallUILanguage( LANGID *lang )
510 *lang = system_ui_language;
511 return STATUS_SUCCESS;