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
43 #include "ntdll_misc.h"
45 #include "wine/exception.h"
46 #include "wine/unicode.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(resource
);
51 #define IS_INTRESOURCE(x) (((ULONG_PTR)(x) >> 16) == 0)
53 /**********************************************************************
56 * Check if a module handle is for a LOAD_LIBRARY_AS_DATAFILE module.
58 static inline BOOL
is_data_file_module( HMODULE hmod
)
60 return (ULONG_PTR
)hmod
& 1;
64 /**********************************************************************
67 * push a language in the list of languages to try
69 static inline int push_language( WORD
*list
, int pos
, WORD lang
)
72 for (i
= 0; i
< pos
; i
++) if (list
[i
] == lang
) return pos
;
78 /**********************************************************************
81 * Find the first suitable entry in a resource directory
83 static const IMAGE_RESOURCE_DIRECTORY
*find_first_entry( const IMAGE_RESOURCE_DIRECTORY
*dir
,
84 const void *root
, int want_dir
)
86 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(dir
+ 1);
89 for (pos
= 0; pos
< dir
->NumberOfNamedEntries
+ dir
->NumberOfIdEntries
; pos
++)
91 if (!entry
[pos
].u2
.s2
.DataIsDirectory
== !want_dir
)
92 return (const IMAGE_RESOURCE_DIRECTORY
*)((const char *)root
+ entry
[pos
].u2
.s2
.OffsetToDirectory
);
98 /**********************************************************************
101 * Find an entry by id in a resource directory
103 static const IMAGE_RESOURCE_DIRECTORY
*find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY
*dir
,
104 WORD id
, const void *root
, int want_dir
)
106 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
;
109 entry
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(dir
+ 1);
110 min
= dir
->NumberOfNamedEntries
;
111 max
= min
+ dir
->NumberOfIdEntries
- 1;
114 pos
= (min
+ max
) / 2;
115 if (entry
[pos
].u
.Id
== id
)
117 if (!entry
[pos
].u2
.s2
.DataIsDirectory
== !want_dir
)
119 TRACE("root %p dir %p id %04x ret %p\n",
120 root
, dir
, id
, (const char*)root
+ entry
[pos
].u2
.s2
.OffsetToDirectory
);
121 return (const IMAGE_RESOURCE_DIRECTORY
*)((const char *)root
+ entry
[pos
].u2
.s2
.OffsetToDirectory
);
125 if (entry
[pos
].u
.Id
> id
) max
= pos
- 1;
128 TRACE("root %p dir %p id %04x not found\n", root
, dir
, id
);
133 /**********************************************************************
136 * Find an entry by name in a resource directory
138 static const IMAGE_RESOURCE_DIRECTORY
*find_entry_by_name( const IMAGE_RESOURCE_DIRECTORY
*dir
,
139 LPCWSTR name
, const void *root
,
142 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
;
143 const IMAGE_RESOURCE_DIR_STRING_U
*str
;
144 int min
, max
, res
, pos
, namelen
;
146 if (IS_INTRESOURCE(name
)) return find_entry_by_id( dir
, LOWORD(name
), root
, want_dir
);
147 entry
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(dir
+ 1);
148 namelen
= strlenW(name
);
150 max
= dir
->NumberOfNamedEntries
- 1;
153 pos
= (min
+ max
) / 2;
154 str
= (const IMAGE_RESOURCE_DIR_STRING_U
*)((const char *)root
+ entry
[pos
].u
.s
.NameOffset
);
155 res
= strncmpW( name
, str
->NameString
, str
->Length
);
156 if (!res
&& namelen
== str
->Length
)
158 if (!entry
[pos
].u2
.s2
.DataIsDirectory
== !want_dir
)
160 TRACE("root %p dir %p name %s ret %p\n",
161 root
, dir
, debugstr_w(name
), (const char*)root
+ entry
[pos
].u2
.s2
.OffsetToDirectory
);
162 return (const IMAGE_RESOURCE_DIRECTORY
*)((const char *)root
+ entry
[pos
].u2
.s2
.OffsetToDirectory
);
166 if (res
< 0) max
= pos
- 1;
169 TRACE("root %p dir %p name %s not found\n", root
, dir
, debugstr_w(name
) );
174 /**********************************************************************
177 * Find a resource entry
179 static NTSTATUS
find_entry( HMODULE hmod
, const LDR_RESOURCE_INFO
*info
,
180 ULONG level
, const void **ret
, int want_dir
)
184 const IMAGE_RESOURCE_DIRECTORY
*resdirptr
;
185 WORD list
[9]; /* list of languages to try */
188 root
= RtlImageDirectoryEntryToData( hmod
, TRUE
, IMAGE_DIRECTORY_ENTRY_RESOURCE
, &size
);
189 if (!root
) return STATUS_RESOURCE_DATA_NOT_FOUND
;
190 if (size
< sizeof(*resdirptr
)) return STATUS_RESOURCE_DATA_NOT_FOUND
;
193 if (!level
--) goto done
;
194 if (!(*ret
= find_entry_by_name( resdirptr
, (LPCWSTR
)info
->Type
, root
, want_dir
|| level
)))
195 return STATUS_RESOURCE_TYPE_NOT_FOUND
;
196 if (!level
--) return STATUS_SUCCESS
;
199 if (!(*ret
= find_entry_by_name( resdirptr
, (LPCWSTR
)info
->Name
, root
, want_dir
|| level
)))
200 return STATUS_RESOURCE_NAME_NOT_FOUND
;
201 if (!level
--) return STATUS_SUCCESS
;
202 if (level
) return STATUS_INVALID_PARAMETER
; /* level > 3 */
204 /* 1. specified language */
205 pos
= push_language( list
, pos
, info
->Language
);
207 /* 2. specified language with neutral sublanguage */
208 pos
= push_language( list
, pos
, MAKELANGID( PRIMARYLANGID(info
->Language
), SUBLANG_NEUTRAL
) );
210 /* 3. neutral language with neutral sublanguage */
211 pos
= push_language( list
, pos
, MAKELANGID( LANG_NEUTRAL
, SUBLANG_NEUTRAL
) );
213 /* if no explicitly specified language, try some defaults */
214 if (PRIMARYLANGID(info
->Language
) == LANG_NEUTRAL
)
216 /* user defaults, unless SYS_DEFAULT sublanguage specified */
217 if (SUBLANGID(info
->Language
) != SUBLANG_SYS_DEFAULT
)
219 /* 4. current thread locale language */
220 pos
= push_language( list
, pos
, LANGIDFROMLCID(NtCurrentTeb()->CurrentLocale
) );
222 /* 5. user locale language */
223 pos
= push_language( list
, pos
, LANGIDFROMLCID(user_lcid
) );
225 /* 6. user locale language with neutral sublanguage */
226 pos
= push_language( list
, pos
, MAKELANGID( PRIMARYLANGID(user_lcid
), SUBLANG_NEUTRAL
) );
229 /* now system defaults */
231 /* 7. system locale language */
232 pos
= push_language( list
, pos
, LANGIDFROMLCID( system_lcid
) );
234 /* 8. system locale language with neutral sublanguage */
235 pos
= push_language( list
, pos
, MAKELANGID( PRIMARYLANGID(system_lcid
), SUBLANG_NEUTRAL
) );
238 pos
= push_language( list
, pos
, MAKELANGID( LANG_ENGLISH
, SUBLANG_DEFAULT
) );
242 for (i
= 0; i
< pos
; i
++)
243 if ((*ret
= find_entry_by_id( resdirptr
, list
[i
], root
, want_dir
))) return STATUS_SUCCESS
;
245 /* if no explicitly specified language, return the first entry */
246 if (PRIMARYLANGID(info
->Language
) == LANG_NEUTRAL
)
248 if ((*ret
= find_first_entry( resdirptr
, root
, want_dir
))) return STATUS_SUCCESS
;
250 return STATUS_RESOURCE_LANG_NOT_FOUND
;
254 return STATUS_SUCCESS
;
258 /**********************************************************************
259 * LdrFindResourceDirectory_U (NTDLL.@)
261 NTSTATUS WINAPI DECLSPEC_HOTPATCH
LdrFindResourceDirectory_U( HMODULE hmod
, const LDR_RESOURCE_INFO
*info
,
262 ULONG level
, const IMAGE_RESOURCE_DIRECTORY
**dir
)
269 if (info
) TRACE( "module %p type %s name %s lang %04x level %d\n",
270 hmod
, debugstr_w((LPCWSTR
)info
->Type
),
271 level
> 1 ? debugstr_w((LPCWSTR
)info
->Name
) : "",
272 level
> 2 ? info
->Language
: 0, level
);
274 status
= find_entry( hmod
, info
, level
, &res
, TRUE
);
275 if (status
== STATUS_SUCCESS
) *dir
= res
;
279 return GetExceptionCode();
286 /**********************************************************************
287 * LdrFindResource_U (NTDLL.@)
289 NTSTATUS WINAPI DECLSPEC_HOTPATCH
LdrFindResource_U( HMODULE hmod
, const LDR_RESOURCE_INFO
*info
,
290 ULONG level
, const IMAGE_RESOURCE_DATA_ENTRY
**entry
)
297 if (info
) TRACE( "module %p type %s name %s lang %04x level %d\n",
298 hmod
, debugstr_w((LPCWSTR
)info
->Type
),
299 level
> 1 ? debugstr_w((LPCWSTR
)info
->Name
) : "",
300 level
> 2 ? info
->Language
: 0, level
);
302 status
= find_entry( hmod
, info
, level
, &res
, FALSE
);
303 if (status
== STATUS_SUCCESS
) *entry
= res
;
307 return GetExceptionCode();
314 /* don't penalize other platforms stuff needed on i386 for compatibility */
316 NTSTATUS WINAPI DECLSPEC_HIDDEN
access_resource( HMODULE hmod
, const IMAGE_RESOURCE_DATA_ENTRY
*entry
,
317 void **ptr
, ULONG
*size
)
319 static inline NTSTATUS
access_resource( HMODULE hmod
, const IMAGE_RESOURCE_DATA_ENTRY
*entry
,
320 void **ptr
, ULONG
*size
)
329 if (!RtlImageDirectoryEntryToData( hmod
, TRUE
, IMAGE_DIRECTORY_ENTRY_RESOURCE
, &dirsize
))
330 status
= STATUS_RESOURCE_DATA_NOT_FOUND
;
335 BOOL is_data_file
= is_data_file_module(hmod
);
336 hmod
= (HMODULE
)((ULONG_PTR
)hmod
& ~3);
338 *ptr
= RtlImageRvaToVa( RtlImageNtHeader(hmod
), hmod
, entry
->OffsetToData
, NULL
);
340 *ptr
= (char *)hmod
+ entry
->OffsetToData
;
342 if (size
) *size
= entry
->Size
;
343 status
= STATUS_SUCCESS
;
348 return GetExceptionCode();
354 /**********************************************************************
355 * LdrAccessResource (NTDLL.@)
358 * On x86, Shrinker, an executable compressor, depends on the
359 * "call access_resource" instruction being there.
362 __ASM_STDCALL_FUNC( LdrAccessResource
, 16,
364 "movl %esp, %ebp\n\t"
371 "call " __ASM_NAME("access_resource") "\n\t"
376 NTSTATUS WINAPI
LdrAccessResource( HMODULE hmod
, const IMAGE_RESOURCE_DATA_ENTRY
*entry
,
377 void **ptr
, ULONG
*size
)
379 return access_resource( hmod
, entry
, ptr
, size
);
383 /**********************************************************************
384 * RtlFindMessage (NTDLL.@)
386 NTSTATUS WINAPI
RtlFindMessage( HMODULE hmod
, ULONG type
, ULONG lang
,
387 ULONG msg_id
, const MESSAGE_RESOURCE_ENTRY
**ret
)
389 const MESSAGE_RESOURCE_DATA
*data
;
390 const MESSAGE_RESOURCE_BLOCK
*block
;
391 const IMAGE_RESOURCE_DATA_ENTRY
*rsrc
;
392 LDR_RESOURCE_INFO info
;
399 info
.Language
= lang
;
401 if ((status
= LdrFindResource_U( hmod
, &info
, 3, &rsrc
)) != STATUS_SUCCESS
)
403 if ((status
= LdrAccessResource( hmod
, rsrc
, &ptr
, NULL
)) != STATUS_SUCCESS
)
407 block
= data
->Blocks
;
408 for (i
= 0; i
< data
->NumberOfBlocks
; i
++, block
++)
410 if (msg_id
>= block
->LowId
&& msg_id
<= block
->HighId
)
412 const MESSAGE_RESOURCE_ENTRY
*entry
;
414 entry
= (const MESSAGE_RESOURCE_ENTRY
*)((const char *)data
+ block
->OffsetToEntries
);
415 for (i
= msg_id
- block
->LowId
; i
> 0; i
--)
416 entry
= (const MESSAGE_RESOURCE_ENTRY
*)((const char *)entry
+ entry
->Length
);
418 return STATUS_SUCCESS
;
421 return STATUS_MESSAGE_NOT_FOUND
;
424 /**********************************************************************
425 * RtlFormatMessage (NTDLL.@)
427 * Formats a message (similar to sprintf).
430 * Message [I] Message to format.
431 * MaxWidth [I] Maximum width in characters of each output line.
432 * IgnoreInserts [I] Whether to copy the message without processing inserts.
433 * Ansi [I] Whether Arguments may have ANSI strings.
434 * ArgumentsIsArray [I] Whether Arguments is actually an array rather than a va_list *.
435 * Buffer [O] Buffer to store processed message in.
436 * BufferSize [I] Size of Buffer (in bytes?).
441 NTSTATUS WINAPI
RtlFormatMessage( LPWSTR Message
, UCHAR MaxWidth
,
442 BOOLEAN IgnoreInserts
, BOOLEAN Ansi
,
443 BOOLEAN ArgumentIsArray
, __ms_va_list
* Arguments
,
444 LPWSTR Buffer
, ULONG BufferSize
)
446 FIXME("(%s, %u, %s, %s, %s, %p, %p, %d)\n", debugstr_w(Message
),
447 MaxWidth
, IgnoreInserts
? "TRUE" : "FALSE", Ansi
? "TRUE" : "FALSE",
448 ArgumentIsArray
? "TRUE" : "FALSE", Arguments
, Buffer
, BufferSize
);
449 return STATUS_SUCCESS
;