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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "wine/port.h"
33 #include <sys/types.h>
35 #define NONAMELESSUNION
36 #define NONAMELESSSTRUCT
38 #define WIN32_NO_STATUS
44 #include "wine/exception.h"
45 #include "wine/unicode.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(resource
);
50 static LCID user_lcid
, system_lcid
;
51 static LANGID user_ui_language
, system_ui_language
;
53 #define IS_INTRESOURCE(x) (((ULONG_PTR)(x) >> 16) == 0)
55 /**********************************************************************
58 * Check if a module handle is for a LOAD_LIBRARY_AS_DATAFILE module.
60 static inline BOOL
is_data_file_module( HMODULE hmod
)
62 return (ULONG_PTR
)hmod
& 1;
66 /**********************************************************************
69 * push a language in the list of languages to try
71 static inline int push_language( WORD
*list
, int pos
, WORD lang
)
74 for (i
= 0; i
< pos
; i
++) if (list
[i
] == lang
) return pos
;
80 /**********************************************************************
83 * Find the first suitable entry in a resource directory
85 static const IMAGE_RESOURCE_DIRECTORY
*find_first_entry( const IMAGE_RESOURCE_DIRECTORY
*dir
,
86 const void *root
, int want_dir
)
88 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(dir
+ 1);
91 for (pos
= 0; pos
< dir
->NumberOfNamedEntries
+ dir
->NumberOfIdEntries
; pos
++)
93 if (!entry
[pos
].u2
.s2
.DataIsDirectory
== !want_dir
)
94 return (const IMAGE_RESOURCE_DIRECTORY
*)((const char *)root
+ entry
[pos
].u2
.s2
.OffsetToDirectory
);
100 /**********************************************************************
103 * Find an entry by id in a resource directory
105 static const IMAGE_RESOURCE_DIRECTORY
*find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY
*dir
,
106 WORD id
, const void *root
, int want_dir
)
108 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
;
111 entry
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(dir
+ 1);
112 min
= dir
->NumberOfNamedEntries
;
113 max
= min
+ dir
->NumberOfIdEntries
- 1;
116 pos
= (min
+ max
) / 2;
117 if (entry
[pos
].u
.Id
== id
)
119 if (!entry
[pos
].u2
.s2
.DataIsDirectory
== !want_dir
)
121 TRACE("root %p dir %p id %04x ret %p\n",
122 root
, dir
, id
, (const char*)root
+ entry
[pos
].u2
.s2
.OffsetToDirectory
);
123 return (const IMAGE_RESOURCE_DIRECTORY
*)((const char *)root
+ entry
[pos
].u2
.s2
.OffsetToDirectory
);
127 if (entry
[pos
].u
.Id
> id
) max
= pos
- 1;
130 TRACE("root %p dir %p id %04x not found\n", root
, dir
, id
);
135 /**********************************************************************
138 * Find an entry by name in a resource directory
140 static const IMAGE_RESOURCE_DIRECTORY
*find_entry_by_name( const IMAGE_RESOURCE_DIRECTORY
*dir
,
141 LPCWSTR name
, const void *root
,
144 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
;
145 const IMAGE_RESOURCE_DIR_STRING_U
*str
;
146 int min
, max
, res
, pos
, namelen
;
148 if (IS_INTRESOURCE(name
)) return find_entry_by_id( dir
, LOWORD(name
), root
, want_dir
);
149 entry
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(dir
+ 1);
150 namelen
= strlenW(name
);
152 max
= dir
->NumberOfNamedEntries
- 1;
155 pos
= (min
+ max
) / 2;
156 str
= (const IMAGE_RESOURCE_DIR_STRING_U
*)((const char *)root
+ entry
[pos
].u
.s
.NameOffset
);
157 res
= strncmpW( name
, str
->NameString
, str
->Length
);
158 if (!res
&& namelen
== str
->Length
)
160 if (!entry
[pos
].u2
.s2
.DataIsDirectory
== !want_dir
)
162 TRACE("root %p dir %p name %s ret %p\n",
163 root
, dir
, debugstr_w(name
), (const char*)root
+ entry
[pos
].u2
.s2
.OffsetToDirectory
);
164 return (const IMAGE_RESOURCE_DIRECTORY
*)((const char *)root
+ entry
[pos
].u2
.s2
.OffsetToDirectory
);
168 if (res
< 0) max
= pos
- 1;
171 TRACE("root %p dir %p name %s not found\n", root
, dir
, debugstr_w(name
) );
176 /**********************************************************************
179 * Find a resource entry
181 static NTSTATUS
find_entry( HMODULE hmod
, const LDR_RESOURCE_INFO
*info
,
182 ULONG level
, const void **ret
, int want_dir
)
186 const IMAGE_RESOURCE_DIRECTORY
*resdirptr
;
187 WORD list
[9]; /* list of languages to try */
190 root
= RtlImageDirectoryEntryToData( hmod
, TRUE
, IMAGE_DIRECTORY_ENTRY_RESOURCE
, &size
);
191 if (!root
) return STATUS_RESOURCE_DATA_NOT_FOUND
;
192 if (size
< sizeof(*resdirptr
)) return STATUS_RESOURCE_DATA_NOT_FOUND
;
195 if (!level
--) goto done
;
196 if (!(*ret
= find_entry_by_name( resdirptr
, (LPCWSTR
)info
->Type
, root
, want_dir
|| level
)))
197 return STATUS_RESOURCE_TYPE_NOT_FOUND
;
198 if (!level
--) return STATUS_SUCCESS
;
201 if (!(*ret
= find_entry_by_name( resdirptr
, (LPCWSTR
)info
->Name
, root
, want_dir
|| level
)))
202 return STATUS_RESOURCE_NAME_NOT_FOUND
;
203 if (!level
--) return STATUS_SUCCESS
;
204 if (level
) return STATUS_INVALID_PARAMETER
; /* level > 3 */
206 /* 1. specified language */
207 pos
= push_language( list
, pos
, info
->Language
);
209 /* 2. specified language with neutral sublanguage */
210 pos
= push_language( list
, pos
, MAKELANGID( PRIMARYLANGID(info
->Language
), SUBLANG_NEUTRAL
) );
212 /* 3. neutral language with neutral sublanguage */
213 pos
= push_language( list
, pos
, MAKELANGID( LANG_NEUTRAL
, SUBLANG_NEUTRAL
) );
215 /* if no explicitly specified language, try some defaults */
216 if (PRIMARYLANGID(info
->Language
) == LANG_NEUTRAL
)
218 /* user defaults, unless SYS_DEFAULT sublanguage specified */
219 if (SUBLANGID(info
->Language
) != SUBLANG_SYS_DEFAULT
)
221 /* 4. current thread locale language */
222 pos
= push_language( list
, pos
, LANGIDFROMLCID(NtCurrentTeb()->CurrentLocale
) );
224 /* 5. user locale language */
225 pos
= push_language( list
, pos
, LANGIDFROMLCID(user_lcid
) );
227 /* 6. user locale language with neutral sublanguage */
228 pos
= push_language( list
, pos
, MAKELANGID( PRIMARYLANGID(user_lcid
), SUBLANG_NEUTRAL
) );
231 /* now system defaults */
233 /* 7. system locale language */
234 pos
= push_language( list
, pos
, LANGIDFROMLCID( system_lcid
) );
236 /* 8. system locale language with neutral sublanguage */
237 pos
= push_language( list
, pos
, MAKELANGID( PRIMARYLANGID(system_lcid
), SUBLANG_NEUTRAL
) );
240 pos
= push_language( list
, pos
, MAKELANGID( LANG_ENGLISH
, SUBLANG_DEFAULT
) );
244 for (i
= 0; i
< pos
; i
++)
245 if ((*ret
= find_entry_by_id( resdirptr
, list
[i
], root
, want_dir
))) return STATUS_SUCCESS
;
247 /* if no explicitly specified language, return the first entry */
248 if (PRIMARYLANGID(info
->Language
) == LANG_NEUTRAL
)
250 if ((*ret
= find_first_entry( resdirptr
, root
, want_dir
))) return STATUS_SUCCESS
;
252 return STATUS_RESOURCE_LANG_NOT_FOUND
;
256 return STATUS_SUCCESS
;
260 /**********************************************************************
261 * LdrFindResourceDirectory_U (NTDLL.@)
263 NTSTATUS WINAPI DECLSPEC_HOTPATCH
LdrFindResourceDirectory_U( HMODULE hmod
, const LDR_RESOURCE_INFO
*info
,
264 ULONG level
, const IMAGE_RESOURCE_DIRECTORY
**dir
)
271 if (info
) TRACE( "module %p type %s name %s lang %04x level %d\n",
272 hmod
, debugstr_w((LPCWSTR
)info
->Type
),
273 level
> 1 ? debugstr_w((LPCWSTR
)info
->Name
) : "",
274 level
> 2 ? info
->Language
: 0, level
);
276 status
= find_entry( hmod
, info
, level
, &res
, TRUE
);
277 if (status
== STATUS_SUCCESS
) *dir
= res
;
281 return GetExceptionCode();
288 /**********************************************************************
289 * LdrFindResource_U (NTDLL.@)
291 NTSTATUS WINAPI DECLSPEC_HOTPATCH
LdrFindResource_U( HMODULE hmod
, const LDR_RESOURCE_INFO
*info
,
292 ULONG level
, const IMAGE_RESOURCE_DATA_ENTRY
**entry
)
299 if (info
) TRACE( "module %p type %s name %s lang %04x level %d\n",
300 hmod
, debugstr_w((LPCWSTR
)info
->Type
),
301 level
> 1 ? debugstr_w((LPCWSTR
)info
->Name
) : "",
302 level
> 2 ? info
->Language
: 0, level
);
304 status
= find_entry( hmod
, info
, level
, &res
, FALSE
);
305 if (status
== STATUS_SUCCESS
) *entry
= res
;
309 return GetExceptionCode();
316 /* don't penalize other platforms stuff needed on i386 for compatibility */
318 NTSTATUS WINAPI DECLSPEC_HIDDEN
access_resource( HMODULE hmod
, const IMAGE_RESOURCE_DATA_ENTRY
*entry
,
319 void **ptr
, ULONG
*size
)
321 static inline NTSTATUS
access_resource( HMODULE hmod
, const IMAGE_RESOURCE_DATA_ENTRY
*entry
,
322 void **ptr
, ULONG
*size
)
331 if (!RtlImageDirectoryEntryToData( hmod
, TRUE
, IMAGE_DIRECTORY_ENTRY_RESOURCE
, &dirsize
))
332 status
= STATUS_RESOURCE_DATA_NOT_FOUND
;
337 BOOL is_data_file
= is_data_file_module(hmod
);
338 hmod
= (HMODULE
)((ULONG_PTR
)hmod
& ~3);
340 *ptr
= RtlImageRvaToVa( RtlImageNtHeader(hmod
), hmod
, entry
->OffsetToData
, NULL
);
342 *ptr
= (char *)hmod
+ entry
->OffsetToData
;
344 if (size
) *size
= entry
->Size
;
345 status
= STATUS_SUCCESS
;
350 return GetExceptionCode();
356 /**********************************************************************
357 * LdrAccessResource (NTDLL.@)
360 * On x86, Shrinker, an executable compressor, depends on the
361 * "call access_resource" instruction being there.
364 __ASM_STDCALL_FUNC( LdrAccessResource
, 16,
366 "movl %esp, %ebp\n\t"
373 "call " __ASM_NAME("access_resource") "\n\t"
378 NTSTATUS WINAPI
LdrAccessResource( HMODULE hmod
, const IMAGE_RESOURCE_DATA_ENTRY
*entry
,
379 void **ptr
, ULONG
*size
)
381 return access_resource( hmod
, entry
, ptr
, size
);
385 /**********************************************************************
386 * RtlFindMessage (NTDLL.@)
388 NTSTATUS WINAPI
RtlFindMessage( HMODULE hmod
, ULONG type
, ULONG lang
,
389 ULONG msg_id
, const MESSAGE_RESOURCE_ENTRY
**ret
)
391 const MESSAGE_RESOURCE_DATA
*data
;
392 const MESSAGE_RESOURCE_BLOCK
*block
;
393 const IMAGE_RESOURCE_DATA_ENTRY
*rsrc
;
394 LDR_RESOURCE_INFO info
;
401 info
.Language
= lang
;
403 if ((status
= LdrFindResource_U( hmod
, &info
, 3, &rsrc
)) != STATUS_SUCCESS
)
405 if ((status
= LdrAccessResource( hmod
, rsrc
, &ptr
, NULL
)) != STATUS_SUCCESS
)
409 block
= data
->Blocks
;
410 for (i
= 0; i
< data
->NumberOfBlocks
; i
++, block
++)
412 if (msg_id
>= block
->LowId
&& msg_id
<= block
->HighId
)
414 const MESSAGE_RESOURCE_ENTRY
*entry
;
416 entry
= (const MESSAGE_RESOURCE_ENTRY
*)((const char *)data
+ block
->OffsetToEntries
);
417 for (i
= msg_id
- block
->LowId
; i
> 0; i
--)
418 entry
= (const MESSAGE_RESOURCE_ENTRY
*)((const char *)entry
+ entry
->Length
);
420 return STATUS_SUCCESS
;
423 return STATUS_MESSAGE_NOT_FOUND
;
426 /**********************************************************************
427 * RtlFormatMessage (NTDLL.@)
429 * Formats a message (similar to sprintf).
432 * Message [I] Message to format.
433 * MaxWidth [I] Maximum width in characters of each output line.
434 * IgnoreInserts [I] Whether to copy the message without processing inserts.
435 * Ansi [I] Whether Arguments may have ANSI strings.
436 * ArgumentsIsArray [I] Whether Arguments is actually an array rather than a va_list *.
437 * Buffer [O] Buffer to store processed message in.
438 * BufferSize [I] Size of Buffer (in bytes?).
443 NTSTATUS WINAPI
RtlFormatMessage( LPWSTR Message
, UCHAR MaxWidth
,
444 BOOLEAN IgnoreInserts
, BOOLEAN Ansi
,
445 BOOLEAN ArgumentIsArray
, __ms_va_list
* Arguments
,
446 LPWSTR Buffer
, ULONG BufferSize
)
448 FIXME("(%s, %u, %s, %s, %s, %p, %p, %d)\n", debugstr_w(Message
),
449 MaxWidth
, IgnoreInserts
? "TRUE" : "FALSE", Ansi
? "TRUE" : "FALSE",
450 ArgumentIsArray
? "TRUE" : "FALSE", Arguments
, Buffer
, BufferSize
);
451 return STATUS_SUCCESS
;
455 /**********************************************************************
456 * NtQueryDefaultLocale (NTDLL.@)
458 NTSTATUS WINAPI
NtQueryDefaultLocale( BOOLEAN user
, LCID
*lcid
)
460 *lcid
= user
? user_lcid
: system_lcid
;
461 return STATUS_SUCCESS
;
465 /**********************************************************************
466 * NtSetDefaultLocale (NTDLL.@)
468 NTSTATUS WINAPI
NtSetDefaultLocale( BOOLEAN user
, LCID lcid
)
470 if (user
) user_lcid
= lcid
;
474 system_ui_language
= LANGIDFROMLCID(lcid
); /* there is no separate call to set it */
476 return STATUS_SUCCESS
;
480 /**********************************************************************
481 * NtQueryDefaultUILanguage (NTDLL.@)
483 NTSTATUS WINAPI
NtQueryDefaultUILanguage( LANGID
*lang
)
485 *lang
= user_ui_language
;
486 return STATUS_SUCCESS
;
490 /**********************************************************************
491 * NtSetDefaultUILanguage (NTDLL.@)
493 NTSTATUS WINAPI
NtSetDefaultUILanguage( LANGID lang
)
495 user_ui_language
= lang
;
496 return STATUS_SUCCESS
;
500 /**********************************************************************
501 * NtQueryInstallUILanguage (NTDLL.@)
503 NTSTATUS WINAPI
NtQueryInstallUILanguage( LANGID
*lang
)
505 *lang
= system_ui_language
;
506 return STATUS_SUCCESS
;