LdrAccessResource should pop 16 bytes off the stack since it is a
[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 "winternl.h"
43 #include "excpt.h"
44 #include "wine/exception.h"
45 #include "wine/unicode.h"
46 #include "wine/debug.h"
47
48 WINE_DEFAULT_DEBUG_CHANNEL(resource);
49
50 static LCID user_lcid, system_lcid;
51 static LANGID user_ui_language, system_ui_language;
52
53 static WINE_EXCEPTION_FILTER(page_fault)
54 {
55     if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ||
56         GetExceptionCode() == EXCEPTION_PRIV_INSTRUCTION)
57         return EXCEPTION_EXECUTE_HANDLER;
58     return EXCEPTION_CONTINUE_SEARCH;
59 }
60
61 /**********************************************************************
62  *  is_data_file_module
63  *
64  * Check if a module handle is for a LOAD_LIBRARY_AS_DATAFILE module.
65  */
66 inline static int is_data_file_module( HMODULE hmod )
67 {
68     return (ULONG_PTR)hmod & 1;
69 }
70
71
72 /**********************************************************************
73  *  push_language
74  *
75  * push a language in the list of languages to try
76  */
77 static inline int push_language( WORD *list, int pos, WORD lang )
78 {
79     int i;
80     for (i = 0; i < pos; i++) if (list[i] == lang) return pos;
81     list[pos++] = lang;
82     return pos;
83 }
84
85
86 /**********************************************************************
87  *  find_first_entry
88  *
89  * Find the first suitable entry in a resource directory
90  */
91 static const IMAGE_RESOURCE_DIRECTORY *find_first_entry( const IMAGE_RESOURCE_DIRECTORY *dir,
92                                                          const void *root, int want_dir )
93 {
94     const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
95     int pos;
96
97     for (pos = 0; pos < dir->NumberOfNamedEntries + dir->NumberOfIdEntries; pos++)
98     {
99         if (!entry[pos].u2.s3.DataIsDirectory == !want_dir)
100             return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s3.OffsetToDirectory);
101     }
102     return NULL;
103 }
104
105
106 /**********************************************************************
107  *  find_entry_by_id
108  *
109  * Find an entry by id in a resource directory
110  */
111 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
112                                                          WORD id, const void *root, int want_dir )
113 {
114     const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
115     int min, max, pos;
116
117     entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
118     min = dir->NumberOfNamedEntries;
119     max = min + dir->NumberOfIdEntries - 1;
120     while (min <= max)
121     {
122         pos = (min + max) / 2;
123         if (entry[pos].u1.s2.Id == id)
124         {
125             if (!entry[pos].u2.s3.DataIsDirectory == !want_dir)
126             {
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);
130             }
131             break;
132         }
133         if (entry[pos].u1.s2.Id > id) max = pos - 1;
134         else min = pos + 1;
135     }
136     TRACE("root %p dir %p id %04x not found\n", root, dir, id );
137     return NULL;
138 }
139
140
141 /**********************************************************************
142  *  find_entry_by_name
143  *
144  * Find an entry by name in a resource directory
145  */
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 )
149 {
150     const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
151     const IMAGE_RESOURCE_DIR_STRING_U *str;
152     int min, max, res, pos, namelen;
153
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)
160     {
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)
165         {
166             if (!entry[pos].u2.s3.DataIsDirectory == !want_dir)
167             {
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);
171             }
172             break;
173         }
174         if (res < 0) max = pos - 1;
175         else min = pos + 1;
176     }
177     TRACE("root %p dir %p name %s not found\n", root, dir, debugstr_w(name) );
178     return NULL;
179 }
180
181
182 /**********************************************************************
183  *  find_entry
184  *
185  * Find a resource entry
186  */
187 static NTSTATUS find_entry( HMODULE hmod, const LDR_RESOURCE_INFO *info,
188                             ULONG level, const void **ret, int want_dir )
189 {
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;
195
196     root = RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_RESOURCE, &size );
197     if (!root) return STATUS_RESOURCE_DATA_NOT_FOUND;
198     resdirptr = root;
199
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;
204
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 */
210
211     /* 1. specified language */
212     pos = push_language( list, pos, info->Language );
213
214     /* 2. specified language with neutral sublanguage */
215     pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(info->Language), SUBLANG_NEUTRAL ) );
216
217     /* 3. neutral language with neutral sublanguage */
218     pos = push_language( list, pos, MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ) );
219
220     /* if no explicitly specified language, try some defaults */
221     if (PRIMARYLANGID(info->Language) == LANG_NEUTRAL)
222     {
223         /* user defaults, unless SYS_DEFAULT sublanguage specified  */
224         if (SUBLANGID(info->Language) != SUBLANG_SYS_DEFAULT)
225         {
226             /* 4. current thread locale language */
227             pos = push_language( list, pos, LANGIDFROMLCID(NtCurrentTeb()->CurrentLocale) );
228
229             /* 5. user locale language */
230             pos = push_language( list, pos, LANGIDFROMLCID(user_lcid) );
231
232             /* 6. user locale language with neutral sublanguage  */
233             pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(user_lcid), SUBLANG_NEUTRAL ) );
234         }
235
236         /* now system defaults */
237
238         /* 7. system locale language */
239         pos = push_language( list, pos, LANGIDFROMLCID( system_lcid ) );
240
241         /* 8. system locale language with neutral sublanguage */
242         pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(system_lcid), SUBLANG_NEUTRAL ) );
243
244         /* 9. English */
245         pos = push_language( list, pos, MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT ) );
246     }
247
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;
251
252     /* if no explicitly specified language, return the first entry */
253     if (PRIMARYLANGID(info->Language) == LANG_NEUTRAL)
254     {
255         if ((*ret = find_first_entry( resdirptr, root, want_dir ))) return STATUS_SUCCESS;
256     }
257     return STATUS_RESOURCE_LANG_NOT_FOUND;
258
259 done:
260     *ret = resdirptr;
261     return STATUS_SUCCESS;
262 }
263
264
265 /**********************************************************************
266  *      LdrFindResourceDirectory_U  (NTDLL.@)
267  */
268 NTSTATUS WINAPI LdrFindResourceDirectory_U( HMODULE hmod, const LDR_RESOURCE_INFO *info,
269                                             ULONG level, const IMAGE_RESOURCE_DIRECTORY **dir )
270 {
271     const void *res;
272     NTSTATUS status;
273
274     __TRY
275     {
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 );
280
281         status = find_entry( hmod, info, level, &res, TRUE );
282         if (status == STATUS_SUCCESS) *dir = res;
283     }
284     __EXCEPT(page_fault)
285     {
286         return GetExceptionCode();
287     }
288     __ENDTRY;
289     return status;
290 }
291
292
293 /**********************************************************************
294  *      LdrFindResource_U  (NTDLL.@)
295  */
296 NTSTATUS WINAPI LdrFindResource_U( HMODULE hmod, const LDR_RESOURCE_INFO *info,
297                                    ULONG level, const IMAGE_RESOURCE_DATA_ENTRY **entry )
298 {
299     const void *res;
300     NTSTATUS status;
301
302     __TRY
303     {
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 );
308
309         status = find_entry( hmod, info, level, &res, FALSE );
310         if (status == STATUS_SUCCESS) *entry = res;
311     }
312     __EXCEPT(page_fault)
313     {
314         return GetExceptionCode();
315     }
316     __ENDTRY;
317     return status;
318 }
319
320
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
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  *      LdrAccessResource  (NTDLL.@)
363  */
364 #ifdef __i386__
365 /* Shrinker depends on the "call access_resource" instruction being there */
366 __ASM_GLOBAL_FUNC( LdrAccessResource,
367     "pushl %ebp\n"
368     "movl %esp, %ebp\n"
369     "pushl 24(%ebp)\n"
370     "pushl 20(%ebp)\n"
371     "pushl 16(%ebp)\n"
372     "pushl 12(%ebp)\n"
373     "pushl 8(%ebp)\n"
374     "call access_resource\n"
375     "leave\n"
376     "ret $16\n"
377 );
378 #else
379 NTSTATUS WINAPI LdrAccessResource( HMODULE hmod, const IMAGE_RESOURCE_DATA_ENTRY *entry,
380                                    void **ptr, ULONG *size )
381 {
382     return access_resource( hmod, entry, ptr, size );
383 }
384 #endif
385
386 /**********************************************************************
387  *      RtlFindMessage  (NTDLL.@)
388  */
389 NTSTATUS WINAPI RtlFindMessage( HMODULE hmod, ULONG type, ULONG lang,
390                                 ULONG msg_id, const MESSAGE_RESOURCE_ENTRY **ret )
391 {
392     const MESSAGE_RESOURCE_DATA *data;
393     const MESSAGE_RESOURCE_BLOCK *block;
394     const IMAGE_RESOURCE_DATA_ENTRY *rsrc;
395     LDR_RESOURCE_INFO info;
396     NTSTATUS status;
397     void *ptr;
398     unsigned int i;
399
400     info.Type     = type;
401     info.Name     = 1;
402     info.Language = lang;
403
404     if ((status = LdrFindResource_U( hmod, &info, 3, &rsrc )) != STATUS_SUCCESS)
405         return status;
406     if ((status = LdrAccessResource( hmod, rsrc, &ptr, NULL )) != STATUS_SUCCESS)
407         return status;
408
409     data = ptr;
410     block = data->Blocks;
411     for (i = 0; i < data->NumberOfBlocks; i++, block++)
412     {
413         if (msg_id >= block->LowId && msg_id <= block->HighId)
414         {
415             const MESSAGE_RESOURCE_ENTRY *entry;
416
417             entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)data + block->OffsetToEntries);
418             for (i = msg_id - block->LowId; i > 0; i--)
419                 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)entry + entry->Length);
420             *ret = entry;
421             return STATUS_SUCCESS;
422         }
423     }
424     return STATUS_MESSAGE_NOT_FOUND;
425 }
426
427 /**********************************************************************
428  *      RtlFormatMessage  (NTDLL.@)
429  *
430  * Formats a message (similar to sprintf).
431  *
432  * PARAMS
433  *   Message          [I] Message to format.
434  *   MaxWidth         [I] Maximum width in characters of each output line.
435  *   IgnoreInserts    [I] Whether to copy the message without processing inserts.
436  *   Ansi             [I] Whether Arguments may have ANSI strings.
437  *   ArgumentsIsArray [I] Whether Arguments is actually an array rather than a va_list *.
438  *   Buffer           [O] Buffer to store processed message in.
439  *   BufferSize       [I] Size of Buffer (in bytes?).
440  *
441  * RETURNS
442  *      NTSTATUS code.
443  */
444 NTSTATUS WINAPI RtlFormatMessage( LPWSTR Message, UCHAR MaxWidth,
445                                   BOOLEAN IgnoreInserts, BOOLEAN Ansi,
446                                   BOOLEAN ArgumentIsArray, va_list * Arguments,
447                                   LPWSTR Buffer, ULONG BufferSize )
448 {
449     FIXME("(%s, %u, %s, %s, %s, %p, %p, %ld)\n", debugstr_w(Message),
450         MaxWidth, IgnoreInserts ? "TRUE" : "FALSE", Ansi ? "TRUE" : "FALSE",
451         ArgumentIsArray ? "TRUE" : "FALSE", Arguments, Buffer, BufferSize);
452     return STATUS_SUCCESS;
453 }
454
455
456 /**********************************************************************
457  *      NtQueryDefaultLocale  (NTDLL.@)
458  */
459 NTSTATUS WINAPI NtQueryDefaultLocale( BOOLEAN user, LCID *lcid )
460 {
461     *lcid = user ? user_lcid : system_lcid;
462     return STATUS_SUCCESS;
463 }
464
465
466 /**********************************************************************
467  *      NtSetDefaultLocale  (NTDLL.@)
468  */
469 NTSTATUS WINAPI NtSetDefaultLocale( BOOLEAN user, LCID lcid )
470 {
471     if (user) user_lcid = lcid;
472     else
473     {
474         system_lcid = lcid;
475         system_ui_language = LANGIDFROMLCID(lcid); /* there is no separate call to set it */
476     }
477     return STATUS_SUCCESS;
478 }
479
480
481 /**********************************************************************
482  *      NtQueryDefaultUILanguage  (NTDLL.@)
483  */
484 NTSTATUS WINAPI NtQueryDefaultUILanguage( LANGID *lang )
485 {
486     *lang = user_ui_language;
487     return STATUS_SUCCESS;
488 }
489
490
491 /**********************************************************************
492  *      NtSetDefaultUILanguage  (NTDLL.@)
493  */
494 NTSTATUS WINAPI NtSetDefaultUILanguage( LANGID lang )
495 {
496     user_ui_language = lang;
497     return STATUS_SUCCESS;
498 }
499
500
501 /**********************************************************************
502  *      NtQueryInstallUILanguage  (NTDLL.@)
503  */
504 NTSTATUS WINAPI NtQueryInstallUILanguage( LANGID *lang )
505 {
506     *lang = system_ui_language;
507     return STATUS_SUCCESS;
508 }