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
30 #include <sys/types.h>
32 #define NONAMELESSUNION
33 #define NONAMELESSSTRUCT
35 #define WIN32_NO_STATUS
40 #include "ntdll_misc.h"
42 #include "wine/exception.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(resource
);
47 #define IS_INTRESOURCE(x) (((ULONG_PTR)(x) >> 16) == 0)
49 /**********************************************************************
52 * Check if a module handle is for a LOAD_LIBRARY_AS_DATAFILE module.
54 static inline BOOL
is_data_file_module( HMODULE hmod
)
56 return (ULONG_PTR
)hmod
& 1;
60 /**********************************************************************
63 * push a language in the list of languages to try
65 static inline int push_language( WORD
*list
, int pos
, WORD lang
)
68 for (i
= 0; i
< pos
; i
++) if (list
[i
] == lang
) return pos
;
74 /**********************************************************************
77 * Find the first suitable entry in a resource directory
79 static const IMAGE_RESOURCE_DIRECTORY
*find_first_entry( const IMAGE_RESOURCE_DIRECTORY
*dir
,
80 const void *root
, int want_dir
)
82 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(dir
+ 1);
85 for (pos
= 0; pos
< dir
->NumberOfNamedEntries
+ dir
->NumberOfIdEntries
; pos
++)
87 if (!entry
[pos
].u2
.s2
.DataIsDirectory
== !want_dir
)
88 return (const IMAGE_RESOURCE_DIRECTORY
*)((const char *)root
+ entry
[pos
].u2
.s2
.OffsetToDirectory
);
94 /**********************************************************************
97 * Find an entry by id in a resource directory
99 static const IMAGE_RESOURCE_DIRECTORY
*find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY
*dir
,
100 WORD id
, const void *root
, int want_dir
)
102 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
;
105 entry
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(dir
+ 1);
106 min
= dir
->NumberOfNamedEntries
;
107 max
= min
+ dir
->NumberOfIdEntries
- 1;
110 pos
= (min
+ max
) / 2;
111 if (entry
[pos
].u
.Id
== id
)
113 if (!entry
[pos
].u2
.s2
.DataIsDirectory
== !want_dir
)
115 TRACE("root %p dir %p id %04x ret %p\n",
116 root
, dir
, id
, (const char*)root
+ entry
[pos
].u2
.s2
.OffsetToDirectory
);
117 return (const IMAGE_RESOURCE_DIRECTORY
*)((const char *)root
+ entry
[pos
].u2
.s2
.OffsetToDirectory
);
121 if (entry
[pos
].u
.Id
> id
) max
= pos
- 1;
124 TRACE("root %p dir %p id %04x not found\n", root
, dir
, id
);
129 /**********************************************************************
132 * Find an entry by name in a resource directory
134 static const IMAGE_RESOURCE_DIRECTORY
*find_entry_by_name( const IMAGE_RESOURCE_DIRECTORY
*dir
,
135 LPCWSTR name
, const void *root
,
138 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
;
139 const IMAGE_RESOURCE_DIR_STRING_U
*str
;
140 int min
, max
, res
, pos
, namelen
;
142 if (IS_INTRESOURCE(name
)) return find_entry_by_id( dir
, LOWORD(name
), root
, want_dir
);
143 entry
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(dir
+ 1);
144 namelen
= wcslen(name
);
146 max
= dir
->NumberOfNamedEntries
- 1;
149 pos
= (min
+ max
) / 2;
150 str
= (const IMAGE_RESOURCE_DIR_STRING_U
*)((const char *)root
+ entry
[pos
].u
.s
.NameOffset
);
151 res
= wcsncmp( name
, str
->NameString
, str
->Length
);
152 if (!res
&& namelen
== str
->Length
)
154 if (!entry
[pos
].u2
.s2
.DataIsDirectory
== !want_dir
)
156 TRACE("root %p dir %p name %s ret %p\n",
157 root
, dir
, debugstr_w(name
), (const char*)root
+ entry
[pos
].u2
.s2
.OffsetToDirectory
);
158 return (const IMAGE_RESOURCE_DIRECTORY
*)((const char *)root
+ entry
[pos
].u2
.s2
.OffsetToDirectory
);
162 if (res
< 0) max
= pos
- 1;
165 TRACE("root %p dir %p name %s not found\n", root
, dir
, debugstr_w(name
) );
170 /**********************************************************************
173 * Find a resource entry
175 static NTSTATUS
find_entry( HMODULE hmod
, const LDR_RESOURCE_INFO
*info
,
176 ULONG level
, const void **ret
, int want_dir
)
180 const IMAGE_RESOURCE_DIRECTORY
*resdirptr
;
181 WORD list
[9]; /* list of languages to try */
184 root
= RtlImageDirectoryEntryToData( hmod
, TRUE
, IMAGE_DIRECTORY_ENTRY_RESOURCE
, &size
);
185 if (!root
) return STATUS_RESOURCE_DATA_NOT_FOUND
;
186 if (size
< sizeof(*resdirptr
)) return STATUS_RESOURCE_DATA_NOT_FOUND
;
189 if (!level
--) goto done
;
190 if (!(*ret
= find_entry_by_name( resdirptr
, (LPCWSTR
)info
->Type
, root
, want_dir
|| level
)))
191 return STATUS_RESOURCE_TYPE_NOT_FOUND
;
192 if (!level
--) return STATUS_SUCCESS
;
195 if (!(*ret
= find_entry_by_name( resdirptr
, (LPCWSTR
)info
->Name
, root
, want_dir
|| level
)))
196 return STATUS_RESOURCE_NAME_NOT_FOUND
;
197 if (!level
--) return STATUS_SUCCESS
;
198 if (level
) return STATUS_INVALID_PARAMETER
; /* level > 3 */
200 /* 1. specified language */
201 pos
= push_language( list
, pos
, info
->Language
);
203 /* 2. specified language with neutral sublanguage */
204 pos
= push_language( list
, pos
, MAKELANGID( PRIMARYLANGID(info
->Language
), SUBLANG_NEUTRAL
) );
206 /* 3. neutral language with neutral sublanguage */
207 pos
= push_language( list
, pos
, MAKELANGID( LANG_NEUTRAL
, SUBLANG_NEUTRAL
) );
209 /* if no explicitly specified language, try some defaults */
210 if (PRIMARYLANGID(info
->Language
) == LANG_NEUTRAL
)
212 LANGID user_lang
, user_neutral_lang
, system_lang
;
214 get_resource_lcids( &user_lang
, &user_neutral_lang
, &system_lang
);
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
, user_lang
);
225 /* 6. user locale language with neutral sublanguage */
226 pos
= push_language( list
, pos
, user_neutral_lang
);
229 /* 7. system locale language */
230 pos
= push_language( list
, pos
, system_lang
);
232 /* 8. system locale language with neutral sublanguage */
233 pos
= push_language( list
, pos
, PRIMARYLANGID( system_lang
));
236 pos
= push_language( list
, pos
, MAKELANGID( LANG_ENGLISH
, SUBLANG_DEFAULT
) );
240 for (i
= 0; i
< pos
; i
++)
241 if ((*ret
= find_entry_by_id( resdirptr
, list
[i
], root
, want_dir
))) return STATUS_SUCCESS
;
243 /* if no explicitly specified language, return the first entry */
244 if (PRIMARYLANGID(info
->Language
) == LANG_NEUTRAL
)
246 if ((*ret
= find_first_entry( resdirptr
, root
, want_dir
))) return STATUS_SUCCESS
;
248 return STATUS_RESOURCE_LANG_NOT_FOUND
;
252 return STATUS_SUCCESS
;
256 /**********************************************************************
257 * LdrFindResourceDirectory_U (NTDLL.@)
259 NTSTATUS WINAPI DECLSPEC_HOTPATCH
LdrFindResourceDirectory_U( HMODULE hmod
, const LDR_RESOURCE_INFO
*info
,
260 ULONG level
, const IMAGE_RESOURCE_DIRECTORY
**dir
)
267 if (info
) TRACE( "module %p type %s name %s lang %04lx level %ld\n",
268 hmod
, debugstr_w((LPCWSTR
)info
->Type
),
269 level
> 1 ? debugstr_w((LPCWSTR
)info
->Name
) : "",
270 level
> 2 ? info
->Language
: 0, level
);
272 status
= find_entry( hmod
, info
, level
, &res
, TRUE
);
273 if (status
== STATUS_SUCCESS
) *dir
= res
;
277 return GetExceptionCode();
284 /**********************************************************************
285 * LdrFindResource_U (NTDLL.@)
287 NTSTATUS WINAPI DECLSPEC_HOTPATCH
LdrFindResource_U( HMODULE hmod
, const LDR_RESOURCE_INFO
*info
,
288 ULONG level
, const IMAGE_RESOURCE_DATA_ENTRY
**entry
)
295 if (info
) TRACE( "module %p type %s name %s lang %04lx level %ld\n",
296 hmod
, debugstr_w((LPCWSTR
)info
->Type
),
297 level
> 1 ? debugstr_w((LPCWSTR
)info
->Name
) : "",
298 level
> 2 ? info
->Language
: 0, level
);
300 status
= find_entry( hmod
, info
, level
, &res
, FALSE
);
301 if (status
== STATUS_SUCCESS
) *entry
= res
;
305 return GetExceptionCode();
312 /* don't penalize other platforms with stuff needed on i386 for compatibility */
314 NTSTATUS WINAPI DECLSPEC_HIDDEN
access_resource( HMODULE hmod
, const IMAGE_RESOURCE_DATA_ENTRY
*entry
,
315 void **ptr
, ULONG
*size
)
317 static inline NTSTATUS
access_resource( HMODULE hmod
, const IMAGE_RESOURCE_DATA_ENTRY
*entry
,
318 void **ptr
, ULONG
*size
)
327 if (!RtlImageDirectoryEntryToData( hmod
, TRUE
, IMAGE_DIRECTORY_ENTRY_RESOURCE
, &dirsize
))
328 status
= STATUS_RESOURCE_DATA_NOT_FOUND
;
333 BOOL is_data_file
= is_data_file_module(hmod
);
334 hmod
= (HMODULE
)((ULONG_PTR
)hmod
& ~3);
336 *ptr
= RtlImageRvaToVa( RtlImageNtHeader(hmod
), hmod
, entry
->OffsetToData
, NULL
);
338 *ptr
= (char *)hmod
+ entry
->OffsetToData
;
340 if (size
) *size
= entry
->Size
;
341 status
= STATUS_SUCCESS
;
346 return GetExceptionCode();
352 /**********************************************************************
353 * LdrAccessResource (NTDLL.@)
356 * On x86, Shrinker, an executable compressor, depends on the
357 * "call access_resource" instruction being there.
360 __ASM_STDCALL_FUNC( LdrAccessResource
, 16,
362 "movl %esp, %ebp\n\t"
369 "call " __ASM_STDCALL("access_resource",16) "\n\t"
374 NTSTATUS WINAPI
LdrAccessResource( HMODULE hmod
, const IMAGE_RESOURCE_DATA_ENTRY
*entry
,
375 void **ptr
, ULONG
*size
)
377 return access_resource( hmod
, entry
, ptr
, size
);
381 /**********************************************************************
382 * RtlFindMessage (NTDLL.@)
384 NTSTATUS WINAPI
RtlFindMessage( HMODULE hmod
, ULONG type
, ULONG lang
,
385 ULONG msg_id
, const MESSAGE_RESOURCE_ENTRY
**ret
)
387 const MESSAGE_RESOURCE_DATA
*data
;
388 const MESSAGE_RESOURCE_BLOCK
*block
;
389 const IMAGE_RESOURCE_DATA_ENTRY
*rsrc
;
390 LDR_RESOURCE_INFO info
;
397 info
.Language
= lang
;
399 if ((status
= LdrFindResource_U( hmod
, &info
, 3, &rsrc
)) != STATUS_SUCCESS
)
401 if ((status
= LdrAccessResource( hmod
, rsrc
, &ptr
, NULL
)) != STATUS_SUCCESS
)
405 block
= data
->Blocks
;
406 for (i
= 0; i
< data
->NumberOfBlocks
; i
++, block
++)
408 if (msg_id
>= block
->LowId
&& msg_id
<= block
->HighId
)
410 const MESSAGE_RESOURCE_ENTRY
*entry
;
412 entry
= (const MESSAGE_RESOURCE_ENTRY
*)((const char *)data
+ block
->OffsetToEntries
);
413 for (i
= msg_id
- block
->LowId
; i
> 0; i
--)
414 entry
= (const MESSAGE_RESOURCE_ENTRY
*)((const char *)entry
+ entry
->Length
);
416 return STATUS_SUCCESS
;
419 return STATUS_MESSAGE_NOT_FOUND
;