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