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
31 #include <sys/types.h>
33 #define NONAMELESSUNION
34 #define NONAMELESSSTRUCT
42 #include "wine/exception.h"
43 #include "wine/unicode.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(resource);
48 static LCID user_lcid, system_lcid;
50 static WINE_EXCEPTION_FILTER(page_fault)
52 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ||
53 GetExceptionCode() == EXCEPTION_PRIV_INSTRUCTION)
54 return EXCEPTION_EXECUTE_HANDLER;
55 return EXCEPTION_CONTINUE_SEARCH;
58 /**********************************************************************
61 * Check if a module handle is for a LOAD_LIBRARY_AS_DATAFILE module.
63 inline static int is_data_file_module( HMODULE hmod )
65 return (ULONG_PTR)hmod & 1;
69 /**********************************************************************
72 * push a language in the list of languages to try
74 static inline int push_language( WORD *list, int pos, WORD lang )
77 for (i = 0; i < pos; i++) if (list[i] == lang) return pos;
83 /**********************************************************************
86 * Find the first suitable entry in a resource directory
88 static const IMAGE_RESOURCE_DIRECTORY *find_first_entry( const IMAGE_RESOURCE_DIRECTORY *dir,
89 const void *root, int want_dir )
91 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
94 for (pos = 0; pos < dir->NumberOfNamedEntries + dir->NumberOfIdEntries; pos++)
96 if (!entry[pos].u2.s3.DataIsDirectory == !want_dir)
97 return (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry[pos].u2.s3.OffsetToDirectory);
103 /**********************************************************************
106 * Find an entry by id in a resource directory
108 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
109 WORD id, const void *root, int want_dir )
111 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
114 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
115 min = dir->NumberOfNamedEntries;
116 max = min + dir->NumberOfIdEntries - 1;
119 pos = (min + max) / 2;
120 if (entry[pos].u1.s2.Id == id)
122 if (!entry[pos].u2.s3.DataIsDirectory == !want_dir)
124 TRACE("root %p dir %p id %04x ret %p\n",
125 root, dir, id, (char *)root + entry[pos].u2.s3.OffsetToDirectory);
126 return (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry[pos].u2.s3.OffsetToDirectory);
130 if (entry[pos].u1.s2.Id > id) max = pos - 1;
133 TRACE("root %p dir %p id %04x not found\n", root, dir, id );
138 /**********************************************************************
141 * Find an entry by name in a resource directory
143 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_name( const IMAGE_RESOURCE_DIRECTORY *dir,
144 LPCWSTR name, const void *root,
147 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
148 const IMAGE_RESOURCE_DIR_STRING_U *str;
149 int min, max, res, pos, namelen;
151 if (!HIWORD(name)) return find_entry_by_id( dir, LOWORD(name), root, want_dir );
152 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
153 namelen = strlenW(name);
155 max = dir->NumberOfNamedEntries - 1;
158 pos = (min + max) / 2;
159 str = (IMAGE_RESOURCE_DIR_STRING_U *)((char *)root + entry[pos].u1.s1.NameOffset);
160 res = strncmpW( name, str->NameString, str->Length );
161 if (!res && namelen == str->Length)
163 if (!entry[pos].u2.s3.DataIsDirectory == !want_dir)
165 TRACE("root %p dir %p name %s ret %p\n",
166 root, dir, debugstr_w(name), (char *)root + entry[pos].u2.s3.OffsetToDirectory);
167 return (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry[pos].u2.s3.OffsetToDirectory);
171 if (res < 0) max = pos - 1;
174 TRACE("root %p dir %p name %s not found\n", root, dir, debugstr_w(name) );
179 /**********************************************************************
182 * Find a resource entry
184 static NTSTATUS find_entry( HMODULE hmod, const LDR_RESOURCE_INFO *info,
185 ULONG level, const void **ret, int want_dir )
189 const IMAGE_RESOURCE_DIRECTORY *resdirptr;
190 WORD list[9]; /* list of languages to try */
193 root = RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_RESOURCE, &size );
194 if (!root) return STATUS_RESOURCE_DATA_NOT_FOUND;
197 if (!level--) goto done;
198 if (!(*ret = find_entry_by_name( resdirptr, (LPCWSTR)info->Type, root, want_dir || level )))
199 return STATUS_RESOURCE_TYPE_NOT_FOUND;
200 if (!level--) return STATUS_SUCCESS;
203 if (!(*ret = find_entry_by_name( resdirptr, (LPCWSTR)info->Name, root, want_dir || level )))
204 return STATUS_RESOURCE_NAME_NOT_FOUND;
205 if (!level--) return STATUS_SUCCESS;
206 if (level) return STATUS_INVALID_PARAMETER; /* level > 3 */
208 /* 1. specified language */
209 pos = push_language( list, pos, info->Language );
211 /* 2. specified language with neutral sublanguage */
212 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(info->Language), SUBLANG_NEUTRAL ) );
214 /* 3. neutral language with neutral sublanguage */
215 pos = push_language( list, pos, MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ) );
217 /* if no explicitly specified language, try some defaults */
218 if (PRIMARYLANGID(info->Language) == LANG_NEUTRAL)
220 /* user defaults, unless SYS_DEFAULT sublanguage specified */
221 if (SUBLANGID(info->Language) != SUBLANG_SYS_DEFAULT)
223 /* 4. current thread locale language */
224 pos = push_language( list, pos, LANGIDFROMLCID(NtCurrentTeb()->CurrentLocale) );
226 /* 5. user locale language */
227 pos = push_language( list, pos, LANGIDFROMLCID(user_lcid) );
229 /* 6. user locale language with neutral sublanguage */
230 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(user_lcid), SUBLANG_NEUTRAL ) );
233 /* now system defaults */
235 /* 7. system locale language */
236 pos = push_language( list, pos, LANGIDFROMLCID( system_lcid ) );
238 /* 8. system locale language with neutral sublanguage */
239 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(system_lcid), SUBLANG_NEUTRAL ) );
242 pos = push_language( list, pos, MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT ) );
246 for (i = 0; i < pos; i++)
247 if ((*ret = find_entry_by_id( resdirptr, list[i], root, want_dir ))) return STATUS_SUCCESS;
249 /* if no explicitly specified language, return the first entry */
250 if (PRIMARYLANGID(info->Language) == LANG_NEUTRAL)
252 if ((*ret = find_first_entry( resdirptr, root, want_dir ))) return STATUS_SUCCESS;
254 return STATUS_RESOURCE_LANG_NOT_FOUND;
258 return STATUS_SUCCESS;
262 /**********************************************************************
263 * LdrFindResourceDirectory_U (NTDLL.@)
265 NTSTATUS WINAPI LdrFindResourceDirectory_U( HMODULE hmod, const LDR_RESOURCE_INFO *info,
266 ULONG level, const IMAGE_RESOURCE_DIRECTORY **dir )
271 if (info) TRACE( "module %p type %s name %s lang %04lx level %ld\n",
272 hmod, debugstr_w((LPCWSTR)info->Type),
273 debugstr_w((LPCWSTR)info->Name), info->Language, level );
277 status = find_entry( hmod, info, level, &res, TRUE );
278 if (status == STATUS_SUCCESS) *dir = res;
282 return GetExceptionCode();
289 /**********************************************************************
290 * LdrFindResource_U (NTDLL.@)
292 NTSTATUS WINAPI LdrFindResource_U( HMODULE hmod, const LDR_RESOURCE_INFO *info,
293 ULONG level, const IMAGE_RESOURCE_DATA_ENTRY **entry )
298 if (info) TRACE( "module %p type %s name %s lang %04lx level %ld\n",
299 hmod, debugstr_w((LPCWSTR)info->Type),
300 debugstr_w((LPCWSTR)info->Name), info->Language, level );
304 status = find_entry( hmod, info, level, &res, FALSE );
305 if (status == STATUS_SUCCESS) *entry = res;
309 return GetExceptionCode();
316 /**********************************************************************
317 * LdrAccessResource (NTDLL.@)
319 NTSTATUS WINAPI LdrAccessResource( HMODULE hmod, const IMAGE_RESOURCE_DATA_ENTRY *entry,
320 void **ptr, ULONG *size )
328 if (!RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_RESOURCE, &dirsize ))
329 status = STATUS_RESOURCE_DATA_NOT_FOUND;
334 if (is_data_file_module(hmod))
336 HMODULE mod = (HMODULE)((ULONG_PTR)hmod & ~1);
337 *ptr = RtlImageRvaToVa( RtlImageNtHeader(mod), mod, entry->OffsetToData, NULL );
339 else *ptr = (char *)hmod + entry->OffsetToData;
341 if (size) *size = entry->Size;
342 status = STATUS_SUCCESS;
347 return GetExceptionCode();
354 /**********************************************************************
355 * RtlFindMessage (NTDLL.@)
357 NTSTATUS WINAPI RtlFindMessage( HMODULE hmod, ULONG type, ULONG lang,
358 ULONG msg_id, const MESSAGE_RESOURCE_ENTRY **ret )
360 const MESSAGE_RESOURCE_DATA *data;
361 const MESSAGE_RESOURCE_BLOCK *block;
362 const IMAGE_RESOURCE_DATA_ENTRY *rsrc;
363 LDR_RESOURCE_INFO info;
370 info.Language = lang;
372 if ((status = LdrFindResource_U( hmod, &info, 3, &rsrc )) != STATUS_SUCCESS)
374 if ((status = LdrAccessResource( hmod, rsrc, &ptr, NULL )) != STATUS_SUCCESS)
378 block = data->Blocks;
379 for (i = 0; i < data->NumberOfBlocks; i++, block++)
381 if (msg_id >= block->LowId && msg_id <= block->HighId)
383 const MESSAGE_RESOURCE_ENTRY *entry;
385 entry = (MESSAGE_RESOURCE_ENTRY *)((char *)data + block->OffsetToEntries);
386 for (i = msg_id - block->LowId; i > 0; i--)
387 entry = (MESSAGE_RESOURCE_ENTRY *)((char *)entry + entry->Length);
389 return STATUS_SUCCESS;
392 return STATUS_MESSAGE_NOT_FOUND;
396 /**********************************************************************
397 * NtQueryDefaultLocale (NTDLL.@)
399 NTSTATUS WINAPI NtQueryDefaultLocale( BOOLEAN user, LCID *lcid )
401 *lcid = user ? user_lcid : system_lcid;
402 return STATUS_SUCCESS;
406 /**********************************************************************
407 * NtSetDefaultLocale (NTDLL.@)
409 NTSTATUS WINAPI NtSetDefaultLocale( BOOLEAN user, LCID lcid )
411 if (user) user_lcid = lcid;
412 else system_lcid = lcid;
413 return STATUS_SUCCESS;