Fix signed/unsigned comparison warnings.
[wine] / dlls / ntdll / resource.c
1 /*
2  * PE file resources
3  *
4  * Copyright 1995 Thomas Sandford
5  * Copyright 1996 Martin von Loewis
6  * Copyright 2003 Alexandre Julliard
7  *
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
12  *
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.
17  *
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.
22  *
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
26  */
27
28 #include "config.h"
29 #include "wine/port.h"
30
31 #include <stdarg.h>
32 #include <stdlib.h>
33 #include <sys/types.h>
34
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 "winreg.h"
43 #include "winternl.h"
44 #include "winerror.h"
45 #include "thread.h"
46 #include "excpt.h"
47 #include "wine/exception.h"
48 #include "wine/unicode.h"
49 #include "wine/debug.h"
50
51 WINE_DEFAULT_DEBUG_CHANNEL(resource);
52
53 static LCID user_lcid, system_lcid;
54 static LANGID user_ui_language, system_ui_language;
55
56 static WINE_EXCEPTION_FILTER(page_fault)
57 {
58     if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ||
59         GetExceptionCode() == EXCEPTION_PRIV_INSTRUCTION)
60         return EXCEPTION_EXECUTE_HANDLER;
61     return EXCEPTION_CONTINUE_SEARCH;
62 }
63
64 /**********************************************************************
65  *  is_data_file_module
66  *
67  * Check if a module handle is for a LOAD_LIBRARY_AS_DATAFILE module.
68  */
69 inline static int is_data_file_module( HMODULE hmod )
70 {
71     return (ULONG_PTR)hmod & 1;
72 }
73
74
75 /**********************************************************************
76  *  push_language
77  *
78  * push a language in the list of languages to try
79  */
80 static inline int push_language( WORD *list, int pos, WORD lang )
81 {
82     int i;
83     for (i = 0; i < pos; i++) if (list[i] == lang) return pos;
84     list[pos++] = lang;
85     return pos;
86 }
87
88
89 /**********************************************************************
90  *  find_first_entry
91  *
92  * Find the first suitable entry in a resource directory
93  */
94 static const IMAGE_RESOURCE_DIRECTORY *find_first_entry( const IMAGE_RESOURCE_DIRECTORY *dir,
95                                                          const void *root, int want_dir )
96 {
97     const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
98     int pos;
99
100     for (pos = 0; pos < dir->NumberOfNamedEntries + dir->NumberOfIdEntries; pos++)
101     {
102         if (!entry[pos].u2.s3.DataIsDirectory == !want_dir)
103             return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s3.OffsetToDirectory);
104     }
105     return NULL;
106 }
107
108
109 /**********************************************************************
110  *  find_entry_by_id
111  *
112  * Find an entry by id in a resource directory
113  */
114 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
115                                                          WORD id, const void *root, int want_dir )
116 {
117     const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
118     int min, max, pos;
119
120     entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
121     min = dir->NumberOfNamedEntries;
122     max = min + dir->NumberOfIdEntries - 1;
123     while (min <= max)
124     {
125         pos = (min + max) / 2;
126         if (entry[pos].u1.s2.Id == id)
127         {
128             if (!entry[pos].u2.s3.DataIsDirectory == !want_dir)
129             {
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);
133             }
134             break;
135         }
136         if (entry[pos].u1.s2.Id > id) max = pos - 1;
137         else min = pos + 1;
138     }
139     TRACE("root %p dir %p id %04x not found\n", root, dir, id );
140     return NULL;
141 }
142
143
144 /**********************************************************************
145  *  find_entry_by_name
146  *
147  * Find an entry by name in a resource directory
148  */
149 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_name( const IMAGE_RESOURCE_DIRECTORY *dir,
150                                                            LPCWSTR name, const void *root,
151                                                            int want_dir )
152 {
153     const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
154     const IMAGE_RESOURCE_DIR_STRING_U *str;
155     int min, max, res, pos, namelen;
156
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);
160     min = 0;
161     max = dir->NumberOfNamedEntries - 1;
162     while (min <= max)
163     {
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)
168         {
169             if (!entry[pos].u2.s3.DataIsDirectory == !want_dir)
170             {
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);
174             }
175             break;
176         }
177         if (res < 0) max = pos - 1;
178         else min = pos + 1;
179     }
180     TRACE("root %p dir %p name %s not found\n", root, dir, debugstr_w(name) );
181     return NULL;
182 }
183
184
185 /**********************************************************************
186  *  find_entry
187  *
188  * Find a resource entry
189  */
190 static NTSTATUS find_entry( HMODULE hmod, const LDR_RESOURCE_INFO *info,
191                             ULONG level, const void **ret, int want_dir )
192 {
193     ULONG size;
194     const void *root;
195     const IMAGE_RESOURCE_DIRECTORY *resdirptr;
196     WORD list[9];  /* list of languages to try */
197     int i, pos = 0;
198
199     root = RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_RESOURCE, &size );
200     if (!root) return STATUS_RESOURCE_DATA_NOT_FOUND;
201     resdirptr = root;
202
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;
207
208     resdirptr = *ret;
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 */
213
214     /* 1. specified language */
215     pos = push_language( list, pos, info->Language );
216
217     /* 2. specified language with neutral sublanguage */
218     pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(info->Language), SUBLANG_NEUTRAL ) );
219
220     /* 3. neutral language with neutral sublanguage */
221     pos = push_language( list, pos, MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ) );
222
223     /* if no explicitly specified language, try some defaults */
224     if (PRIMARYLANGID(info->Language) == LANG_NEUTRAL)
225     {
226         /* user defaults, unless SYS_DEFAULT sublanguage specified  */
227         if (SUBLANGID(info->Language) != SUBLANG_SYS_DEFAULT)
228         {
229             /* 4. current thread locale language */
230             pos = push_language( list, pos, LANGIDFROMLCID(NtCurrentTeb()->CurrentLocale) );
231
232             /* 5. user locale language */
233             pos = push_language( list, pos, LANGIDFROMLCID(user_lcid) );
234
235             /* 6. user locale language with neutral sublanguage  */
236             pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(user_lcid), SUBLANG_NEUTRAL ) );
237         }
238
239         /* now system defaults */
240
241         /* 7. system locale language */
242         pos = push_language( list, pos, LANGIDFROMLCID( system_lcid ) );
243
244         /* 8. system locale language with neutral sublanguage */
245         pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(system_lcid), SUBLANG_NEUTRAL ) );
246
247         /* 9. English */
248         pos = push_language( list, pos, MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT ) );
249     }
250
251     resdirptr = *ret;
252     for (i = 0; i < pos; i++)
253         if ((*ret = find_entry_by_id( resdirptr, list[i], root, want_dir ))) return STATUS_SUCCESS;
254
255     /* if no explicitly specified language, return the first entry */
256     if (PRIMARYLANGID(info->Language) == LANG_NEUTRAL)
257     {
258         if ((*ret = find_first_entry( resdirptr, root, want_dir ))) return STATUS_SUCCESS;
259     }
260     return STATUS_RESOURCE_LANG_NOT_FOUND;
261
262 done:
263     *ret = resdirptr;
264     return STATUS_SUCCESS;
265 }
266
267
268 /**********************************************************************
269  *      LdrFindResourceDirectory_U  (NTDLL.@)
270  */
271 NTSTATUS WINAPI LdrFindResourceDirectory_U( HMODULE hmod, const LDR_RESOURCE_INFO *info,
272                                             ULONG level, const IMAGE_RESOURCE_DIRECTORY **dir )
273 {
274     const void *res;
275     NTSTATUS status;
276
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 );
281
282     __TRY
283     {
284         status = find_entry( hmod, info, level, &res, TRUE );
285         if (status == STATUS_SUCCESS) *dir = res;
286     }
287     __EXCEPT(page_fault)
288     {
289         return GetExceptionCode();
290     }
291     __ENDTRY;
292     return status;
293 }
294
295
296 /**********************************************************************
297  *      LdrFindResource_U  (NTDLL.@)
298  */
299 NTSTATUS WINAPI LdrFindResource_U( HMODULE hmod, const LDR_RESOURCE_INFO *info,
300                                    ULONG level, const IMAGE_RESOURCE_DATA_ENTRY **entry )
301 {
302     const void *res;
303     NTSTATUS status;
304
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 );
309
310     __TRY
311     {
312         status = find_entry( hmod, info, level, &res, FALSE );
313         if (status == STATUS_SUCCESS) *entry = res;
314     }
315     __EXCEPT(page_fault)
316     {
317         return GetExceptionCode();
318     }
319     __ENDTRY;
320     return status;
321 }
322
323
324 /**********************************************************************
325  *      LdrAccessResource  (NTDLL.@)
326  */
327 NTSTATUS WINAPI LdrAccessResource( HMODULE hmod, const IMAGE_RESOURCE_DATA_ENTRY *entry,
328                                    void **ptr, ULONG *size )
329 {
330     NTSTATUS status;
331
332     __TRY
333     {
334         ULONG dirsize;
335
336         if (!RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_RESOURCE, &dirsize ))
337             status = STATUS_RESOURCE_DATA_NOT_FOUND;
338         else
339         {
340             if (ptr)
341             {
342                 if (is_data_file_module(hmod))
343                 {
344                     HMODULE mod = (HMODULE)((ULONG_PTR)hmod & ~1);
345                     *ptr = RtlImageRvaToVa( RtlImageNtHeader(mod), mod, entry->OffsetToData, NULL );
346                 }
347                 else *ptr = (char *)hmod + entry->OffsetToData;
348             }
349             if (size) *size = entry->Size;
350             status = STATUS_SUCCESS;
351         }
352     }
353     __EXCEPT(page_fault)
354     {
355         return GetExceptionCode();
356     }
357     __ENDTRY;
358     return status;
359 }
360
361
362 /**********************************************************************
363  *      RtlFindMessage  (NTDLL.@)
364  */
365 NTSTATUS WINAPI RtlFindMessage( HMODULE hmod, ULONG type, ULONG lang,
366                                 ULONG msg_id, const MESSAGE_RESOURCE_ENTRY **ret )
367 {
368     const MESSAGE_RESOURCE_DATA *data;
369     const MESSAGE_RESOURCE_BLOCK *block;
370     const IMAGE_RESOURCE_DATA_ENTRY *rsrc;
371     LDR_RESOURCE_INFO info;
372     NTSTATUS status;
373     void *ptr;
374     unsigned int i;
375
376     info.Type     = type;
377     info.Name     = 1;
378     info.Language = lang;
379
380     if ((status = LdrFindResource_U( hmod, &info, 3, &rsrc )) != STATUS_SUCCESS)
381         return status;
382     if ((status = LdrAccessResource( hmod, rsrc, &ptr, NULL )) != STATUS_SUCCESS)
383         return status;
384
385     data = ptr;
386     block = data->Blocks;
387     for (i = 0; i < data->NumberOfBlocks; i++, block++)
388     {
389         if (msg_id >= block->LowId && msg_id <= block->HighId)
390         {
391             const MESSAGE_RESOURCE_ENTRY *entry;
392
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);
396             *ret = entry;
397             return STATUS_SUCCESS;
398         }
399     }
400     return STATUS_MESSAGE_NOT_FOUND;
401 }
402
403 /**********************************************************************
404  *      RtlFormatMessage  (NTDLL.@)
405  *
406  * Formats a message (similar to sprintf).
407  *
408  * PARAMS
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?).
416  *
417  * RETURNS
418  *      NTSTATUS code.
419  */
420 NTSTATUS WINAPI RtlFormatMessage( LPWSTR Message, UCHAR MaxWidth,
421                                   BOOLEAN IgnoreInserts, BOOLEAN Ansi,
422                                   BOOLEAN ArgumentIsArray, va_list * Arguments,
423                                   LPWSTR Buffer, ULONG BufferSize )
424 {
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;
429 }
430
431
432 /**********************************************************************
433  *      NtQueryDefaultLocale  (NTDLL.@)
434  */
435 NTSTATUS WINAPI NtQueryDefaultLocale( BOOLEAN user, LCID *lcid )
436 {
437     *lcid = user ? user_lcid : system_lcid;
438     return STATUS_SUCCESS;
439 }
440
441
442 /**********************************************************************
443  *      NtSetDefaultLocale  (NTDLL.@)
444  */
445 NTSTATUS WINAPI NtSetDefaultLocale( BOOLEAN user, LCID lcid )
446 {
447     if (user) user_lcid = lcid;
448     else
449     {
450         system_lcid = lcid;
451         system_ui_language = LANGIDFROMLCID(lcid); /* there is no separate call to set it */
452     }
453     return STATUS_SUCCESS;
454 }
455
456
457 /**********************************************************************
458  *      NtQueryDefaultUILanguage  (NTDLL.@)
459  */
460 NTSTATUS WINAPI NtQueryDefaultUILanguage( LANGID *lang )
461 {
462     *lang = user_ui_language;
463     return STATUS_SUCCESS;
464 }
465
466
467 /**********************************************************************
468  *      NtSetDefaultUILanguage  (NTDLL.@)
469  */
470 NTSTATUS WINAPI NtSetDefaultUILanguage( LANGID lang )
471 {
472     user_ui_language = lang;
473     return STATUS_SUCCESS;
474 }
475
476
477 /**********************************************************************
478  *      NtQueryInstallUILanguage  (NTDLL.@)
479  */
480 NTSTATUS WINAPI NtQueryInstallUILanguage( LANGID *lang )
481 {
482     *lang = system_ui_language;
483     return STATUS_SUCCESS;
484 }