Try not to display uninitialized data in traces (found by Valgrind).
[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
55 static WINE_EXCEPTION_FILTER(page_fault)
56 {
57     if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ||
58         GetExceptionCode() == EXCEPTION_PRIV_INSTRUCTION)
59         return EXCEPTION_EXECUTE_HANDLER;
60     return EXCEPTION_CONTINUE_SEARCH;
61 }
62
63 /**********************************************************************
64  *  is_data_file_module
65  *
66  * Check if a module handle is for a LOAD_LIBRARY_AS_DATAFILE module.
67  */
68 inline static int is_data_file_module( HMODULE hmod )
69 {
70     return (ULONG_PTR)hmod & 1;
71 }
72
73
74 /**********************************************************************
75  *  push_language
76  *
77  * push a language in the list of languages to try
78  */
79 static inline int push_language( WORD *list, int pos, WORD lang )
80 {
81     int i;
82     for (i = 0; i < pos; i++) if (list[i] == lang) return pos;
83     list[pos++] = lang;
84     return pos;
85 }
86
87
88 /**********************************************************************
89  *  find_first_entry
90  *
91  * Find the first suitable entry in a resource directory
92  */
93 static const IMAGE_RESOURCE_DIRECTORY *find_first_entry( const IMAGE_RESOURCE_DIRECTORY *dir,
94                                                          const void *root, int want_dir )
95 {
96     const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
97     int pos;
98
99     for (pos = 0; pos < dir->NumberOfNamedEntries + dir->NumberOfIdEntries; pos++)
100     {
101         if (!entry[pos].u2.s3.DataIsDirectory == !want_dir)
102             return (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry[pos].u2.s3.OffsetToDirectory);
103     }
104     return NULL;
105 }
106
107
108 /**********************************************************************
109  *  find_entry_by_id
110  *
111  * Find an entry by id in a resource directory
112  */
113 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
114                                                          WORD id, const void *root, int want_dir )
115 {
116     const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
117     int min, max, pos;
118
119     entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
120     min = dir->NumberOfNamedEntries;
121     max = min + dir->NumberOfIdEntries - 1;
122     while (min <= max)
123     {
124         pos = (min + max) / 2;
125         if (entry[pos].u1.s2.Id == id)
126         {
127             if (!entry[pos].u2.s3.DataIsDirectory == !want_dir)
128             {
129                 TRACE("root %p dir %p id %04x ret %p\n",
130                       root, dir, id, (char *)root + entry[pos].u2.s3.OffsetToDirectory);
131                 return (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry[pos].u2.s3.OffsetToDirectory);
132             }
133             break;
134         }
135         if (entry[pos].u1.s2.Id > id) max = pos - 1;
136         else min = pos + 1;
137     }
138     TRACE("root %p dir %p id %04x not found\n", root, dir, id );
139     return NULL;
140 }
141
142
143 /**********************************************************************
144  *  find_entry_by_name
145  *
146  * Find an entry by name in a resource directory
147  */
148 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_name( const IMAGE_RESOURCE_DIRECTORY *dir,
149                                                            LPCWSTR name, const void *root,
150                                                            int want_dir )
151 {
152     const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
153     const IMAGE_RESOURCE_DIR_STRING_U *str;
154     int min, max, res, pos, namelen;
155
156     if (!HIWORD(name)) return find_entry_by_id( dir, LOWORD(name), root, want_dir );
157     entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
158     namelen = strlenW(name);
159     min = 0;
160     max = dir->NumberOfNamedEntries - 1;
161     while (min <= max)
162     {
163         pos = (min + max) / 2;
164         str = (IMAGE_RESOURCE_DIR_STRING_U *)((char *)root + entry[pos].u1.s1.NameOffset);
165         res = strncmpW( name, str->NameString, str->Length );
166         if (!res && namelen == str->Length)
167         {
168             if (!entry[pos].u2.s3.DataIsDirectory == !want_dir)
169             {
170                 TRACE("root %p dir %p name %s ret %p\n",
171                       root, dir, debugstr_w(name), (char *)root + entry[pos].u2.s3.OffsetToDirectory);
172                 return (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry[pos].u2.s3.OffsetToDirectory);
173             }
174             break;
175         }
176         if (res < 0) max = pos - 1;
177         else min = pos + 1;
178     }
179     TRACE("root %p dir %p name %s not found\n", root, dir, debugstr_w(name) );
180     return NULL;
181 }
182
183
184 /**********************************************************************
185  *  find_entry
186  *
187  * Find a resource entry
188  */
189 static NTSTATUS find_entry( HMODULE hmod, const LDR_RESOURCE_INFO *info,
190                             ULONG level, const void **ret, int want_dir )
191 {
192     ULONG size;
193     const void *root;
194     const IMAGE_RESOURCE_DIRECTORY *resdirptr;
195     WORD list[9];  /* list of languages to try */
196     int i, pos = 0;
197
198     root = RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_RESOURCE, &size );
199     if (!root) return STATUS_RESOURCE_DATA_NOT_FOUND;
200     resdirptr = root;
201
202     if (!level--) goto done;
203     if (!(*ret = find_entry_by_name( resdirptr, (LPCWSTR)info->Type, root, want_dir || level )))
204         return STATUS_RESOURCE_TYPE_NOT_FOUND;
205     if (!level--) return STATUS_SUCCESS;
206
207     resdirptr = *ret;
208     if (!(*ret = find_entry_by_name( resdirptr, (LPCWSTR)info->Name, root, want_dir || level )))
209         return STATUS_RESOURCE_NAME_NOT_FOUND;
210     if (!level--) return STATUS_SUCCESS;
211     if (level) return STATUS_INVALID_PARAMETER;  /* level > 3 */
212
213     /* 1. specified language */
214     pos = push_language( list, pos, info->Language );
215
216     /* 2. specified language with neutral sublanguage */
217     pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(info->Language), SUBLANG_NEUTRAL ) );
218
219     /* 3. neutral language with neutral sublanguage */
220     pos = push_language( list, pos, MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ) );
221
222     /* if no explicitly specified language, try some defaults */
223     if (PRIMARYLANGID(info->Language) == LANG_NEUTRAL)
224     {
225         /* user defaults, unless SYS_DEFAULT sublanguage specified  */
226         if (SUBLANGID(info->Language) != SUBLANG_SYS_DEFAULT)
227         {
228             /* 4. current thread locale language */
229             pos = push_language( list, pos, LANGIDFROMLCID(NtCurrentTeb()->CurrentLocale) );
230
231             /* 5. user locale language */
232             pos = push_language( list, pos, LANGIDFROMLCID(user_lcid) );
233
234             /* 6. user locale language with neutral sublanguage  */
235             pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(user_lcid), SUBLANG_NEUTRAL ) );
236         }
237
238         /* now system defaults */
239
240         /* 7. system locale language */
241         pos = push_language( list, pos, LANGIDFROMLCID( system_lcid ) );
242
243         /* 8. system locale language with neutral sublanguage */
244         pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(system_lcid), SUBLANG_NEUTRAL ) );
245
246         /* 9. English */
247         pos = push_language( list, pos, MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT ) );
248     }
249
250     resdirptr = *ret;
251     for (i = 0; i < pos; i++)
252         if ((*ret = find_entry_by_id( resdirptr, list[i], root, want_dir ))) return STATUS_SUCCESS;
253
254     /* if no explicitly specified language, return the first entry */
255     if (PRIMARYLANGID(info->Language) == LANG_NEUTRAL)
256     {
257         if ((*ret = find_first_entry( resdirptr, root, want_dir ))) return STATUS_SUCCESS;
258     }
259     return STATUS_RESOURCE_LANG_NOT_FOUND;
260
261 done:
262     *ret = resdirptr;
263     return STATUS_SUCCESS;
264 }
265
266
267 /**********************************************************************
268  *      LdrFindResourceDirectory_U  (NTDLL.@)
269  */
270 NTSTATUS WINAPI LdrFindResourceDirectory_U( HMODULE hmod, const LDR_RESOURCE_INFO *info,
271                                             ULONG level, const IMAGE_RESOURCE_DIRECTORY **dir )
272 {
273     const void *res;
274     NTSTATUS status;
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     __TRY
282     {
283         status = find_entry( hmod, info, level, &res, TRUE );
284         if (status == STATUS_SUCCESS) *dir = res;
285     }
286     __EXCEPT(page_fault)
287     {
288         return GetExceptionCode();
289     }
290     __ENDTRY;
291     return status;
292 }
293
294
295 /**********************************************************************
296  *      LdrFindResource_U  (NTDLL.@)
297  */
298 NTSTATUS WINAPI LdrFindResource_U( HMODULE hmod, const LDR_RESOURCE_INFO *info,
299                                    ULONG level, const IMAGE_RESOURCE_DATA_ENTRY **entry )
300 {
301     const void *res;
302     NTSTATUS status;
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     __TRY
310     {
311         status = find_entry( hmod, info, level, &res, FALSE );
312         if (status == STATUS_SUCCESS) *entry = res;
313     }
314     __EXCEPT(page_fault)
315     {
316         return GetExceptionCode();
317     }
318     __ENDTRY;
319     return status;
320 }
321
322
323 /**********************************************************************
324  *      LdrAccessResource  (NTDLL.@)
325  */
326 NTSTATUS WINAPI LdrAccessResource( HMODULE hmod, const IMAGE_RESOURCE_DATA_ENTRY *entry,
327                                    void **ptr, ULONG *size )
328 {
329     NTSTATUS status;
330
331     __TRY
332     {
333         ULONG dirsize;
334
335         if (!RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_RESOURCE, &dirsize ))
336             status = STATUS_RESOURCE_DATA_NOT_FOUND;
337         else
338         {
339             if (ptr)
340             {
341                 if (is_data_file_module(hmod))
342                 {
343                     HMODULE mod = (HMODULE)((ULONG_PTR)hmod & ~1);
344                     *ptr = RtlImageRvaToVa( RtlImageNtHeader(mod), mod, entry->OffsetToData, NULL );
345                 }
346                 else *ptr = (char *)hmod + entry->OffsetToData;
347             }
348             if (size) *size = entry->Size;
349             status = STATUS_SUCCESS;
350         }
351     }
352     __EXCEPT(page_fault)
353     {
354         return GetExceptionCode();
355     }
356     __ENDTRY;
357     return status;
358 }
359
360
361 /**********************************************************************
362  *      RtlFindMessage  (NTDLL.@)
363  */
364 NTSTATUS WINAPI RtlFindMessage( HMODULE hmod, ULONG type, ULONG lang,
365                                 ULONG msg_id, const MESSAGE_RESOURCE_ENTRY **ret )
366 {
367     const MESSAGE_RESOURCE_DATA *data;
368     const MESSAGE_RESOURCE_BLOCK *block;
369     const IMAGE_RESOURCE_DATA_ENTRY *rsrc;
370     LDR_RESOURCE_INFO info;
371     NTSTATUS status;
372     void *ptr;
373     int i;
374
375     info.Type     = type;
376     info.Name     = 1;
377     info.Language = lang;
378
379     if ((status = LdrFindResource_U( hmod, &info, 3, &rsrc )) != STATUS_SUCCESS)
380         return status;
381     if ((status = LdrAccessResource( hmod, rsrc, &ptr, NULL )) != STATUS_SUCCESS)
382         return status;
383
384     data = ptr;
385     block = data->Blocks;
386     for (i = 0; i < data->NumberOfBlocks; i++, block++)
387     {
388         if (msg_id >= block->LowId && msg_id <= block->HighId)
389         {
390             const MESSAGE_RESOURCE_ENTRY *entry;
391
392             entry = (MESSAGE_RESOURCE_ENTRY *)((char *)data + block->OffsetToEntries);
393             for (i = msg_id - block->LowId; i > 0; i--)
394                 entry = (MESSAGE_RESOURCE_ENTRY *)((char *)entry + entry->Length);
395             *ret = entry;
396             return STATUS_SUCCESS;
397         }
398     }
399     return STATUS_MESSAGE_NOT_FOUND;
400 }
401
402
403 /**********************************************************************
404  *      NtQueryDefaultLocale  (NTDLL.@)
405  */
406 NTSTATUS WINAPI NtQueryDefaultLocale( BOOLEAN user, LCID *lcid )
407 {
408     *lcid = user ? user_lcid : system_lcid;
409     return STATUS_SUCCESS;
410 }
411
412
413 /**********************************************************************
414  *      NtSetDefaultLocale  (NTDLL.@)
415  */
416 NTSTATUS WINAPI NtSetDefaultLocale( BOOLEAN user, LCID lcid )
417 {
418     if (user) user_lcid = lcid;
419     else system_lcid = lcid;
420     return STATUS_SUCCESS;
421 }