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
29 #include "wine/port.h"
33 #include <sys/types.h>
35 #define NONAMELESSUNION
36 #define NONAMELESSSTRUCT
47 #include "wine/exception.h"
48 #include "wine/unicode.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(resource
);
53 static LCID user_lcid
, system_lcid
;
55 static WINE_EXCEPTION_FILTER(page_fault
)
57 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION
||
58 GetExceptionCode() == EXCEPTION_PRIV_INSTRUCTION
)
59 return EXCEPTION_EXECUTE_HANDLER
;
60 return EXCEPTION_CONTINUE_SEARCH
;
63 /**********************************************************************
66 * Check if a module handle is for a LOAD_LIBRARY_AS_DATAFILE module.
68 inline static int is_data_file_module( HMODULE hmod
)
70 return (ULONG_PTR
)hmod
& 1;
74 /**********************************************************************
77 * push a language in the list of languages to try
79 static inline int push_language( WORD
*list
, int pos
, WORD lang
)
82 for (i
= 0; i
< pos
; i
++) if (list
[i
] == lang
) return pos
;
88 /**********************************************************************
91 * Find the first suitable entry in a resource directory
93 static const IMAGE_RESOURCE_DIRECTORY
*find_first_entry( const IMAGE_RESOURCE_DIRECTORY
*dir
,
94 const void *root
, int want_dir
)
96 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(dir
+ 1);
99 for (pos
= 0; pos
< dir
->NumberOfNamedEntries
+ dir
->NumberOfIdEntries
; pos
++)
101 if (!entry
[pos
].u2
.s3
.DataIsDirectory
== !want_dir
)
102 return (const IMAGE_RESOURCE_DIRECTORY
*)((const char *)root
+ entry
[pos
].u2
.s3
.OffsetToDirectory
);
108 /**********************************************************************
111 * Find an entry by id in a resource directory
113 static const IMAGE_RESOURCE_DIRECTORY
*find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY
*dir
,
114 WORD id
, const void *root
, int want_dir
)
116 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
;
119 entry
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(dir
+ 1);
120 min
= dir
->NumberOfNamedEntries
;
121 max
= min
+ dir
->NumberOfIdEntries
- 1;
124 pos
= (min
+ max
) / 2;
125 if (entry
[pos
].u1
.s2
.Id
== id
)
127 if (!entry
[pos
].u2
.s3
.DataIsDirectory
== !want_dir
)
129 TRACE("root %p dir %p id %04x ret %p\n",
130 root
, dir
, id
, (const char*)root
+ entry
[pos
].u2
.s3
.OffsetToDirectory
);
131 return (const IMAGE_RESOURCE_DIRECTORY
*)((const char *)root
+ entry
[pos
].u2
.s3
.OffsetToDirectory
);
135 if (entry
[pos
].u1
.s2
.Id
> id
) max
= pos
- 1;
138 TRACE("root %p dir %p id %04x not found\n", root
, dir
, id
);
143 /**********************************************************************
146 * Find an entry by name in a resource directory
148 static const IMAGE_RESOURCE_DIRECTORY
*find_entry_by_name( const IMAGE_RESOURCE_DIRECTORY
*dir
,
149 LPCWSTR name
, const void *root
,
152 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
;
153 const IMAGE_RESOURCE_DIR_STRING_U
*str
;
154 int min
, max
, res
, pos
, namelen
;
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
);
160 max
= dir
->NumberOfNamedEntries
- 1;
163 pos
= (min
+ max
) / 2;
164 str
= (const IMAGE_RESOURCE_DIR_STRING_U
*)((const char *)root
+ entry
[pos
].u1
.s1
.NameOffset
);
165 res
= strncmpW( name
, str
->NameString
, str
->Length
);
166 if (!res
&& namelen
== str
->Length
)
168 if (!entry
[pos
].u2
.s3
.DataIsDirectory
== !want_dir
)
170 TRACE("root %p dir %p name %s ret %p\n",
171 root
, dir
, debugstr_w(name
), (const char*)root
+ entry
[pos
].u2
.s3
.OffsetToDirectory
);
172 return (const IMAGE_RESOURCE_DIRECTORY
*)((const char *)root
+ entry
[pos
].u2
.s3
.OffsetToDirectory
);
176 if (res
< 0) max
= pos
- 1;
179 TRACE("root %p dir %p name %s not found\n", root
, dir
, debugstr_w(name
) );
184 /**********************************************************************
187 * Find a resource entry
189 static NTSTATUS
find_entry( HMODULE hmod
, const LDR_RESOURCE_INFO
*info
,
190 ULONG level
, const void **ret
, int want_dir
)
194 const IMAGE_RESOURCE_DIRECTORY
*resdirptr
;
195 WORD list
[9]; /* list of languages to try */
198 root
= RtlImageDirectoryEntryToData( hmod
, TRUE
, IMAGE_DIRECTORY_ENTRY_RESOURCE
, &size
);
199 if (!root
) return STATUS_RESOURCE_DATA_NOT_FOUND
;
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
;
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 */
213 /* 1. specified language */
214 pos
= push_language( list
, pos
, info
->Language
);
216 /* 2. specified language with neutral sublanguage */
217 pos
= push_language( list
, pos
, MAKELANGID( PRIMARYLANGID(info
->Language
), SUBLANG_NEUTRAL
) );
219 /* 3. neutral language with neutral sublanguage */
220 pos
= push_language( list
, pos
, MAKELANGID( LANG_NEUTRAL
, SUBLANG_NEUTRAL
) );
222 /* if no explicitly specified language, try some defaults */
223 if (PRIMARYLANGID(info
->Language
) == LANG_NEUTRAL
)
225 /* user defaults, unless SYS_DEFAULT sublanguage specified */
226 if (SUBLANGID(info
->Language
) != SUBLANG_SYS_DEFAULT
)
228 /* 4. current thread locale language */
229 pos
= push_language( list
, pos
, LANGIDFROMLCID(NtCurrentTeb()->CurrentLocale
) );
231 /* 5. user locale language */
232 pos
= push_language( list
, pos
, LANGIDFROMLCID(user_lcid
) );
234 /* 6. user locale language with neutral sublanguage */
235 pos
= push_language( list
, pos
, MAKELANGID( PRIMARYLANGID(user_lcid
), SUBLANG_NEUTRAL
) );
238 /* now system defaults */
240 /* 7. system locale language */
241 pos
= push_language( list
, pos
, LANGIDFROMLCID( system_lcid
) );
243 /* 8. system locale language with neutral sublanguage */
244 pos
= push_language( list
, pos
, MAKELANGID( PRIMARYLANGID(system_lcid
), SUBLANG_NEUTRAL
) );
247 pos
= push_language( list
, pos
, MAKELANGID( LANG_ENGLISH
, SUBLANG_DEFAULT
) );
251 for (i
= 0; i
< pos
; i
++)
252 if ((*ret
= find_entry_by_id( resdirptr
, list
[i
], root
, want_dir
))) return STATUS_SUCCESS
;
254 /* if no explicitly specified language, return the first entry */
255 if (PRIMARYLANGID(info
->Language
) == LANG_NEUTRAL
)
257 if ((*ret
= find_first_entry( resdirptr
, root
, want_dir
))) return STATUS_SUCCESS
;
259 return STATUS_RESOURCE_LANG_NOT_FOUND
;
263 return STATUS_SUCCESS
;
267 /**********************************************************************
268 * LdrFindResourceDirectory_U (NTDLL.@)
270 NTSTATUS WINAPI
LdrFindResourceDirectory_U( HMODULE hmod
, const LDR_RESOURCE_INFO
*info
,
271 ULONG level
, const IMAGE_RESOURCE_DIRECTORY
**dir
)
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
);
283 status
= find_entry( hmod
, info
, level
, &res
, TRUE
);
284 if (status
== STATUS_SUCCESS
) *dir
= res
;
288 return GetExceptionCode();
295 /**********************************************************************
296 * LdrFindResource_U (NTDLL.@)
298 NTSTATUS WINAPI
LdrFindResource_U( HMODULE hmod
, const LDR_RESOURCE_INFO
*info
,
299 ULONG level
, const IMAGE_RESOURCE_DATA_ENTRY
**entry
)
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
);
311 status
= find_entry( hmod
, info
, level
, &res
, FALSE
);
312 if (status
== STATUS_SUCCESS
) *entry
= res
;
316 return GetExceptionCode();
323 /**********************************************************************
324 * LdrAccessResource (NTDLL.@)
326 NTSTATUS WINAPI
LdrAccessResource( HMODULE hmod
, const IMAGE_RESOURCE_DATA_ENTRY
*entry
,
327 void **ptr
, ULONG
*size
)
335 if (!RtlImageDirectoryEntryToData( hmod
, TRUE
, IMAGE_DIRECTORY_ENTRY_RESOURCE
, &dirsize
))
336 status
= STATUS_RESOURCE_DATA_NOT_FOUND
;
341 if (is_data_file_module(hmod
))
343 HMODULE mod
= (HMODULE
)((ULONG_PTR
)hmod
& ~1);
344 *ptr
= RtlImageRvaToVa( RtlImageNtHeader(mod
), mod
, entry
->OffsetToData
, NULL
);
346 else *ptr
= (char *)hmod
+ entry
->OffsetToData
;
348 if (size
) *size
= entry
->Size
;
349 status
= STATUS_SUCCESS
;
354 return GetExceptionCode();
361 /**********************************************************************
362 * RtlFindMessage (NTDLL.@)
364 NTSTATUS WINAPI
RtlFindMessage( HMODULE hmod
, ULONG type
, ULONG lang
,
365 ULONG msg_id
, const MESSAGE_RESOURCE_ENTRY
**ret
)
367 const MESSAGE_RESOURCE_DATA
*data
;
368 const MESSAGE_RESOURCE_BLOCK
*block
;
369 const IMAGE_RESOURCE_DATA_ENTRY
*rsrc
;
370 LDR_RESOURCE_INFO info
;
377 info
.Language
= lang
;
379 if ((status
= LdrFindResource_U( hmod
, &info
, 3, &rsrc
)) != STATUS_SUCCESS
)
381 if ((status
= LdrAccessResource( hmod
, rsrc
, &ptr
, NULL
)) != STATUS_SUCCESS
)
385 block
= data
->Blocks
;
386 for (i
= 0; i
< data
->NumberOfBlocks
; i
++, block
++)
388 if (msg_id
>= block
->LowId
&& msg_id
<= block
->HighId
)
390 const MESSAGE_RESOURCE_ENTRY
*entry
;
392 entry
= (const MESSAGE_RESOURCE_ENTRY
*)((const char *)data
+ block
->OffsetToEntries
);
393 for (i
= msg_id
- block
->LowId
; i
> 0; i
--)
394 entry
= (const MESSAGE_RESOURCE_ENTRY
*)((const char *)entry
+ entry
->Length
);
396 return STATUS_SUCCESS
;
399 return STATUS_MESSAGE_NOT_FOUND
;
402 /**********************************************************************
403 * RtlFormatMessage (NTDLL.@)
405 * Formats a message (similar to sprintf).
408 * Message [I] Message to format.
409 * MaxWidth [I] Maximum width in characters of each output line.
410 * IgnoreInserts [I] Whether to copy the message without processing inserts.
411 * Ansi [I] Whether Arguments may have ANSI strings.
412 * ArgumentsIsArray [I] Whether Arguments is actually an array rather than a va_list *.
413 * Buffer [O] Buffer to store processed message in.
414 * BufferSize [I] Size of Buffer (in bytes?).
419 NTSTATUS WINAPI
RtlFormatMessage( LPWSTR Message
, UCHAR MaxWidth
,
420 BOOLEAN IgnoreInserts
, BOOLEAN Ansi
,
421 BOOLEAN ArgumentIsArray
, va_list * Arguments
,
422 LPWSTR Buffer
, ULONG BufferSize
)
424 FIXME("(%s, %u, %s, %s, %s, %p, %p, %ld)\n", debugstr_w(Message
),
425 MaxWidth
, IgnoreInserts
? "TRUE" : "FALSE", Ansi
? "TRUE" : "FALSE",
426 ArgumentIsArray
? "TRUE" : "FALSE", Arguments
, Buffer
, BufferSize
);
427 return STATUS_SUCCESS
;
431 /**********************************************************************
432 * NtQueryDefaultLocale (NTDLL.@)
434 NTSTATUS WINAPI
NtQueryDefaultLocale( BOOLEAN user
, LCID
*lcid
)
436 *lcid
= user
? user_lcid
: system_lcid
;
437 return STATUS_SUCCESS
;
441 /**********************************************************************
442 * NtSetDefaultLocale (NTDLL.@)
444 NTSTATUS WINAPI
NtSetDefaultLocale( BOOLEAN user
, LCID lcid
)
446 if (user
) user_lcid
= lcid
;
447 else system_lcid
= lcid
;
448 return STATUS_SUCCESS
;