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
32 #include <sys/types.h>
34 #define NONAMELESSUNION
35 #define NONAMELESSSTRUCT
46 #include "wine/exception.h"
47 #include "wine/unicode.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(resource
);
52 static LCID user_lcid
, system_lcid
;
54 static WINE_EXCEPTION_FILTER(page_fault
)
56 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION
||
57 GetExceptionCode() == EXCEPTION_PRIV_INSTRUCTION
)
58 return EXCEPTION_EXECUTE_HANDLER
;
59 return EXCEPTION_CONTINUE_SEARCH
;
62 /**********************************************************************
65 * Check if a module handle is for a LOAD_LIBRARY_AS_DATAFILE module.
67 inline static int is_data_file_module( HMODULE hmod
)
69 return (ULONG_PTR
)hmod
& 1;
73 /**********************************************************************
76 * push a language in the list of languages to try
78 static inline int push_language( WORD
*list
, int pos
, WORD lang
)
81 for (i
= 0; i
< pos
; i
++) if (list
[i
] == lang
) return pos
;
87 /**********************************************************************
90 * Find the first suitable entry in a resource directory
92 static const IMAGE_RESOURCE_DIRECTORY
*find_first_entry( const IMAGE_RESOURCE_DIRECTORY
*dir
,
93 const void *root
, int want_dir
)
95 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(dir
+ 1);
98 for (pos
= 0; pos
< dir
->NumberOfNamedEntries
+ dir
->NumberOfIdEntries
; pos
++)
100 if (!entry
[pos
].u2
.s3
.DataIsDirectory
== !want_dir
)
101 return (IMAGE_RESOURCE_DIRECTORY
*)((char *)root
+ entry
[pos
].u2
.s3
.OffsetToDirectory
);
107 /**********************************************************************
110 * Find an entry by id in a resource directory
112 static const IMAGE_RESOURCE_DIRECTORY
*find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY
*dir
,
113 WORD id
, const void *root
, int want_dir
)
115 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
;
118 entry
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(dir
+ 1);
119 min
= dir
->NumberOfNamedEntries
;
120 max
= min
+ dir
->NumberOfIdEntries
- 1;
123 pos
= (min
+ max
) / 2;
124 if (entry
[pos
].u1
.s2
.Id
== id
)
126 if (!entry
[pos
].u2
.s3
.DataIsDirectory
== !want_dir
)
128 TRACE("root %p dir %p id %04x ret %p\n",
129 root
, dir
, id
, (char *)root
+ entry
[pos
].u2
.s3
.OffsetToDirectory
);
130 return (IMAGE_RESOURCE_DIRECTORY
*)((char *)root
+ entry
[pos
].u2
.s3
.OffsetToDirectory
);
134 if (entry
[pos
].u1
.s2
.Id
> id
) max
= pos
- 1;
137 TRACE("root %p dir %p id %04x not found\n", root
, dir
, id
);
142 /**********************************************************************
145 * Find an entry by name in a resource directory
147 static const IMAGE_RESOURCE_DIRECTORY
*find_entry_by_name( const IMAGE_RESOURCE_DIRECTORY
*dir
,
148 LPCWSTR name
, const void *root
,
151 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
;
152 const IMAGE_RESOURCE_DIR_STRING_U
*str
;
153 int min
, max
, res
, pos
, namelen
;
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
);
159 max
= dir
->NumberOfNamedEntries
- 1;
162 pos
= (min
+ max
) / 2;
163 str
= (IMAGE_RESOURCE_DIR_STRING_U
*)((char *)root
+ entry
[pos
].u1
.s1
.NameOffset
);
164 res
= strncmpW( name
, str
->NameString
, str
->Length
);
165 if (!res
&& namelen
== str
->Length
)
167 if (!entry
[pos
].u2
.s3
.DataIsDirectory
== !want_dir
)
169 TRACE("root %p dir %p name %s ret %p\n",
170 root
, dir
, debugstr_w(name
), (char *)root
+ entry
[pos
].u2
.s3
.OffsetToDirectory
);
171 return (IMAGE_RESOURCE_DIRECTORY
*)((char *)root
+ entry
[pos
].u2
.s3
.OffsetToDirectory
);
175 if (res
< 0) max
= pos
- 1;
178 TRACE("root %p dir %p name %s not found\n", root
, dir
, debugstr_w(name
) );
183 /**********************************************************************
186 * Find a resource entry
188 static NTSTATUS
find_entry( HMODULE hmod
, const LDR_RESOURCE_INFO
*info
,
189 ULONG level
, const void **ret
, int want_dir
)
193 const IMAGE_RESOURCE_DIRECTORY
*resdirptr
;
194 WORD list
[9]; /* list of languages to try */
197 root
= RtlImageDirectoryEntryToData( hmod
, TRUE
, IMAGE_DIRECTORY_ENTRY_RESOURCE
, &size
);
198 if (!root
) return STATUS_RESOURCE_DATA_NOT_FOUND
;
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
;
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 */
212 /* 1. specified language */
213 pos
= push_language( list
, pos
, info
->Language
);
215 /* 2. specified language with neutral sublanguage */
216 pos
= push_language( list
, pos
, MAKELANGID( PRIMARYLANGID(info
->Language
), SUBLANG_NEUTRAL
) );
218 /* 3. neutral language with neutral sublanguage */
219 pos
= push_language( list
, pos
, MAKELANGID( LANG_NEUTRAL
, SUBLANG_NEUTRAL
) );
221 /* if no explicitly specified language, try some defaults */
222 if (PRIMARYLANGID(info
->Language
) == LANG_NEUTRAL
)
224 /* user defaults, unless SYS_DEFAULT sublanguage specified */
225 if (SUBLANGID(info
->Language
) != SUBLANG_SYS_DEFAULT
)
227 /* 4. current thread locale language */
228 pos
= push_language( list
, pos
, LANGIDFROMLCID(NtCurrentTeb()->CurrentLocale
) );
230 /* 5. user locale language */
231 pos
= push_language( list
, pos
, LANGIDFROMLCID(user_lcid
) );
233 /* 6. user locale language with neutral sublanguage */
234 pos
= push_language( list
, pos
, MAKELANGID( PRIMARYLANGID(user_lcid
), SUBLANG_NEUTRAL
) );
237 /* now system defaults */
239 /* 7. system locale language */
240 pos
= push_language( list
, pos
, LANGIDFROMLCID( system_lcid
) );
242 /* 8. system locale language with neutral sublanguage */
243 pos
= push_language( list
, pos
, MAKELANGID( PRIMARYLANGID(system_lcid
), SUBLANG_NEUTRAL
) );
246 pos
= push_language( list
, pos
, MAKELANGID( LANG_ENGLISH
, SUBLANG_DEFAULT
) );
250 for (i
= 0; i
< pos
; i
++)
251 if ((*ret
= find_entry_by_id( resdirptr
, list
[i
], root
, want_dir
))) return STATUS_SUCCESS
;
253 /* if no explicitly specified language, return the first entry */
254 if (PRIMARYLANGID(info
->Language
) == LANG_NEUTRAL
)
256 if ((*ret
= find_first_entry( resdirptr
, root
, want_dir
))) return STATUS_SUCCESS
;
258 return STATUS_RESOURCE_LANG_NOT_FOUND
;
262 return STATUS_SUCCESS
;
266 /**********************************************************************
267 * LdrFindResourceDirectory_U (NTDLL.@)
269 NTSTATUS WINAPI
LdrFindResourceDirectory_U( HMODULE hmod
, const LDR_RESOURCE_INFO
*info
,
270 ULONG level
, const IMAGE_RESOURCE_DIRECTORY
**dir
)
275 if (info
) TRACE( "module %p type %s name %s lang %04lx level %ld\n",
276 hmod
, debugstr_w((LPCWSTR
)info
->Type
),
277 debugstr_w((LPCWSTR
)info
->Name
), info
->Language
, level
);
281 status
= find_entry( hmod
, info
, level
, &res
, TRUE
);
282 if (status
== STATUS_SUCCESS
) *dir
= res
;
286 return GetExceptionCode();
293 /**********************************************************************
294 * LdrFindResource_U (NTDLL.@)
296 NTSTATUS WINAPI
LdrFindResource_U( HMODULE hmod
, const LDR_RESOURCE_INFO
*info
,
297 ULONG level
, const IMAGE_RESOURCE_DATA_ENTRY
**entry
)
302 if (info
) TRACE( "module %p type %s name %s lang %04lx level %ld\n",
303 hmod
, debugstr_w((LPCWSTR
)info
->Type
),
304 debugstr_w((LPCWSTR
)info
->Name
), info
->Language
, level
);
308 status
= find_entry( hmod
, info
, level
, &res
, FALSE
);
309 if (status
== STATUS_SUCCESS
) *entry
= res
;
313 return GetExceptionCode();
320 /**********************************************************************
321 * LdrAccessResource (NTDLL.@)
323 NTSTATUS WINAPI
LdrAccessResource( HMODULE hmod
, const IMAGE_RESOURCE_DATA_ENTRY
*entry
,
324 void **ptr
, ULONG
*size
)
332 if (!RtlImageDirectoryEntryToData( hmod
, TRUE
, IMAGE_DIRECTORY_ENTRY_RESOURCE
, &dirsize
))
333 status
= STATUS_RESOURCE_DATA_NOT_FOUND
;
338 if (is_data_file_module(hmod
))
340 HMODULE mod
= (HMODULE
)((ULONG_PTR
)hmod
& ~1);
341 *ptr
= RtlImageRvaToVa( RtlImageNtHeader(mod
), mod
, entry
->OffsetToData
, NULL
);
343 else *ptr
= (char *)hmod
+ entry
->OffsetToData
;
345 if (size
) *size
= entry
->Size
;
346 status
= STATUS_SUCCESS
;
351 return GetExceptionCode();
358 /**********************************************************************
359 * RtlFindMessage (NTDLL.@)
361 NTSTATUS WINAPI
RtlFindMessage( HMODULE hmod
, ULONG type
, ULONG lang
,
362 ULONG msg_id
, const MESSAGE_RESOURCE_ENTRY
**ret
)
364 const MESSAGE_RESOURCE_DATA
*data
;
365 const MESSAGE_RESOURCE_BLOCK
*block
;
366 const IMAGE_RESOURCE_DATA_ENTRY
*rsrc
;
367 LDR_RESOURCE_INFO info
;
374 info
.Language
= lang
;
376 if ((status
= LdrFindResource_U( hmod
, &info
, 3, &rsrc
)) != STATUS_SUCCESS
)
378 if ((status
= LdrAccessResource( hmod
, rsrc
, &ptr
, NULL
)) != STATUS_SUCCESS
)
382 block
= data
->Blocks
;
383 for (i
= 0; i
< data
->NumberOfBlocks
; i
++, block
++)
385 if (msg_id
>= block
->LowId
&& msg_id
<= block
->HighId
)
387 const MESSAGE_RESOURCE_ENTRY
*entry
;
389 entry
= (MESSAGE_RESOURCE_ENTRY
*)((char *)data
+ block
->OffsetToEntries
);
390 for (i
= msg_id
- block
->LowId
; i
> 0; i
--)
391 entry
= (MESSAGE_RESOURCE_ENTRY
*)((char *)entry
+ entry
->Length
);
393 return STATUS_SUCCESS
;
396 return STATUS_MESSAGE_NOT_FOUND
;
400 /**********************************************************************
401 * NtQueryDefaultLocale (NTDLL.@)
403 NTSTATUS WINAPI
NtQueryDefaultLocale( BOOLEAN user
, LCID
*lcid
)
405 *lcid
= user
? user_lcid
: system_lcid
;
406 return STATUS_SUCCESS
;
410 /**********************************************************************
411 * NtSetDefaultLocale (NTDLL.@)
413 NTSTATUS WINAPI
NtSetDefaultLocale( BOOLEAN user
, LCID lcid
)
415 if (user
) user_lcid
= lcid
;
416 else system_lcid
= lcid
;
417 return STATUS_SUCCESS
;