wined3d: Use the bo binding in wined3d_context_gl_map_bo_address().
[wine.git] / dlls / ntdll / resource.c
blob9ebf374b444ef18cb0bf64b1a8931aff4c7c23b7
1 /*
2 * PE file resources
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
28 #include "config.h"
29 #include "wine/port.h"
31 #include <stdarg.h>
32 #include <stdlib.h>
33 #include <sys/types.h>
35 #define NONAMELESSUNION
36 #define NONAMELESSSTRUCT
37 #include "ntstatus.h"
38 #define WIN32_NO_STATUS
39 #include "windef.h"
40 #include "winbase.h"
41 #include "winnt.h"
42 #include "winternl.h"
43 #include "ntdll_misc.h"
44 #include "wine/asm.h"
45 #include "wine/exception.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(resource);
50 #define IS_INTRESOURCE(x) (((ULONG_PTR)(x) >> 16) == 0)
52 /**********************************************************************
53 * is_data_file_module
55 * Check if a module handle is for a LOAD_LIBRARY_AS_DATAFILE module.
57 static inline BOOL is_data_file_module( HMODULE hmod )
59 return (ULONG_PTR)hmod & 1;
63 /**********************************************************************
64 * push_language
66 * push a language in the list of languages to try
68 static inline int push_language( WORD *list, int pos, WORD lang )
70 int i;
71 for (i = 0; i < pos; i++) if (list[i] == lang) return pos;
72 list[pos++] = lang;
73 return pos;
77 /**********************************************************************
78 * find_first_entry
80 * Find the first suitable entry in a resource directory
82 static const IMAGE_RESOURCE_DIRECTORY *find_first_entry( const IMAGE_RESOURCE_DIRECTORY *dir,
83 const void *root, int want_dir )
85 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
86 int pos;
88 for (pos = 0; pos < dir->NumberOfNamedEntries + dir->NumberOfIdEntries; pos++)
90 if (!entry[pos].u2.s2.DataIsDirectory == !want_dir)
91 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s2.OffsetToDirectory);
93 return NULL;
97 /**********************************************************************
98 * find_entry_by_id
100 * Find an entry by id in a resource directory
102 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
103 WORD id, const void *root, int want_dir )
105 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
106 int min, max, pos;
108 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
109 min = dir->NumberOfNamedEntries;
110 max = min + dir->NumberOfIdEntries - 1;
111 while (min <= max)
113 pos = (min + max) / 2;
114 if (entry[pos].u.Id == id)
116 if (!entry[pos].u2.s2.DataIsDirectory == !want_dir)
118 TRACE("root %p dir %p id %04x ret %p\n",
119 root, dir, id, (const char*)root + entry[pos].u2.s2.OffsetToDirectory);
120 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s2.OffsetToDirectory);
122 break;
124 if (entry[pos].u.Id > id) max = pos - 1;
125 else min = pos + 1;
127 TRACE("root %p dir %p id %04x not found\n", root, dir, id );
128 return NULL;
132 /**********************************************************************
133 * find_entry_by_name
135 * Find an entry by name in a resource directory
137 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_name( const IMAGE_RESOURCE_DIRECTORY *dir,
138 LPCWSTR name, const void *root,
139 int want_dir )
141 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
142 const IMAGE_RESOURCE_DIR_STRING_U *str;
143 int min, max, res, pos, namelen;
145 if (IS_INTRESOURCE(name)) return find_entry_by_id( dir, LOWORD(name), root, want_dir );
146 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
147 namelen = wcslen(name);
148 min = 0;
149 max = dir->NumberOfNamedEntries - 1;
150 while (min <= max)
152 pos = (min + max) / 2;
153 str = (const IMAGE_RESOURCE_DIR_STRING_U *)((const char *)root + entry[pos].u.s.NameOffset);
154 res = wcsncmp( name, str->NameString, str->Length );
155 if (!res && namelen == str->Length)
157 if (!entry[pos].u2.s2.DataIsDirectory == !want_dir)
159 TRACE("root %p dir %p name %s ret %p\n",
160 root, dir, debugstr_w(name), (const char*)root + entry[pos].u2.s2.OffsetToDirectory);
161 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s2.OffsetToDirectory);
163 break;
165 if (res < 0) max = pos - 1;
166 else min = pos + 1;
168 TRACE("root %p dir %p name %s not found\n", root, dir, debugstr_w(name) );
169 return NULL;
173 /**********************************************************************
174 * find_entry
176 * Find a resource entry
178 static NTSTATUS find_entry( HMODULE hmod, const LDR_RESOURCE_INFO *info,
179 ULONG level, const void **ret, int want_dir )
181 ULONG size;
182 const void *root;
183 const IMAGE_RESOURCE_DIRECTORY *resdirptr;
184 WORD list[9]; /* list of languages to try */
185 int i, pos = 0;
187 root = RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_RESOURCE, &size );
188 if (!root) return STATUS_RESOURCE_DATA_NOT_FOUND;
189 if (size < sizeof(*resdirptr)) return STATUS_RESOURCE_DATA_NOT_FOUND;
190 resdirptr = root;
192 if (!level--) goto done;
193 if (!(*ret = find_entry_by_name( resdirptr, (LPCWSTR)info->Type, root, want_dir || level )))
194 return STATUS_RESOURCE_TYPE_NOT_FOUND;
195 if (!level--) return STATUS_SUCCESS;
197 resdirptr = *ret;
198 if (!(*ret = find_entry_by_name( resdirptr, (LPCWSTR)info->Name, root, want_dir || level )))
199 return STATUS_RESOURCE_NAME_NOT_FOUND;
200 if (!level--) return STATUS_SUCCESS;
201 if (level) return STATUS_INVALID_PARAMETER; /* level > 3 */
203 /* 1. specified language */
204 pos = push_language( list, pos, info->Language );
206 /* 2. specified language with neutral sublanguage */
207 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(info->Language), SUBLANG_NEUTRAL ) );
209 /* 3. neutral language with neutral sublanguage */
210 pos = push_language( list, pos, MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ) );
212 /* if no explicitly specified language, try some defaults */
213 if (PRIMARYLANGID(info->Language) == LANG_NEUTRAL)
215 /* user defaults, unless SYS_DEFAULT sublanguage specified */
216 if (SUBLANGID(info->Language) != SUBLANG_SYS_DEFAULT)
218 /* 4. current thread locale language */
219 pos = push_language( list, pos, LANGIDFROMLCID(NtCurrentTeb()->CurrentLocale) );
221 /* 5. user locale language */
222 pos = push_language( list, pos, LANGIDFROMLCID(user_lcid) );
224 /* 6. user locale language with neutral sublanguage */
225 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(user_lcid), SUBLANG_NEUTRAL ) );
228 /* now system defaults */
230 /* 7. system locale language */
231 pos = push_language( list, pos, LANGIDFROMLCID( system_lcid ) );
233 /* 8. system locale language with neutral sublanguage */
234 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(system_lcid), SUBLANG_NEUTRAL ) );
236 /* 9. English */
237 pos = push_language( list, pos, MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT ) );
240 resdirptr = *ret;
241 for (i = 0; i < pos; i++)
242 if ((*ret = find_entry_by_id( resdirptr, list[i], root, want_dir ))) return STATUS_SUCCESS;
244 /* if no explicitly specified language, return the first entry */
245 if (PRIMARYLANGID(info->Language) == LANG_NEUTRAL)
247 if ((*ret = find_first_entry( resdirptr, root, want_dir ))) return STATUS_SUCCESS;
249 return STATUS_RESOURCE_LANG_NOT_FOUND;
251 done:
252 *ret = resdirptr;
253 return STATUS_SUCCESS;
257 /**********************************************************************
258 * LdrFindResourceDirectory_U (NTDLL.@)
260 NTSTATUS WINAPI DECLSPEC_HOTPATCH LdrFindResourceDirectory_U( HMODULE hmod, const LDR_RESOURCE_INFO *info,
261 ULONG level, const IMAGE_RESOURCE_DIRECTORY **dir )
263 const void *res;
264 NTSTATUS status;
266 __TRY
268 if (info) TRACE( "module %p type %s name %s lang %04x level %d\n",
269 hmod, debugstr_w((LPCWSTR)info->Type),
270 level > 1 ? debugstr_w((LPCWSTR)info->Name) : "",
271 level > 2 ? info->Language : 0, level );
273 status = find_entry( hmod, info, level, &res, TRUE );
274 if (status == STATUS_SUCCESS) *dir = res;
276 __EXCEPT_PAGE_FAULT
278 return GetExceptionCode();
280 __ENDTRY;
281 return status;
285 /**********************************************************************
286 * LdrFindResource_U (NTDLL.@)
288 NTSTATUS WINAPI DECLSPEC_HOTPATCH LdrFindResource_U( HMODULE hmod, const LDR_RESOURCE_INFO *info,
289 ULONG level, const IMAGE_RESOURCE_DATA_ENTRY **entry )
291 const void *res;
292 NTSTATUS status;
294 __TRY
296 if (info) TRACE( "module %p type %s name %s lang %04x level %d\n",
297 hmod, debugstr_w((LPCWSTR)info->Type),
298 level > 1 ? debugstr_w((LPCWSTR)info->Name) : "",
299 level > 2 ? info->Language : 0, level );
301 status = find_entry( hmod, info, level, &res, FALSE );
302 if (status == STATUS_SUCCESS) *entry = res;
304 __EXCEPT_PAGE_FAULT
306 return GetExceptionCode();
308 __ENDTRY;
309 return status;
313 /* don't penalize other platforms stuff needed on i386 for compatibility */
314 #ifdef __i386__
315 NTSTATUS WINAPI DECLSPEC_HIDDEN access_resource( HMODULE hmod, const IMAGE_RESOURCE_DATA_ENTRY *entry,
316 void **ptr, ULONG *size )
317 #else
318 static inline NTSTATUS access_resource( HMODULE hmod, const IMAGE_RESOURCE_DATA_ENTRY *entry,
319 void **ptr, ULONG *size )
320 #endif
322 NTSTATUS status;
324 __TRY
326 ULONG dirsize;
328 if (!RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_RESOURCE, &dirsize ))
329 status = STATUS_RESOURCE_DATA_NOT_FOUND;
330 else
332 if (ptr)
334 BOOL is_data_file = is_data_file_module(hmod);
335 hmod = (HMODULE)((ULONG_PTR)hmod & ~3);
336 if (is_data_file)
337 *ptr = RtlImageRvaToVa( RtlImageNtHeader(hmod), hmod, entry->OffsetToData, NULL );
338 else
339 *ptr = (char *)hmod + entry->OffsetToData;
341 if (size) *size = entry->Size;
342 status = STATUS_SUCCESS;
345 __EXCEPT_PAGE_FAULT
347 return GetExceptionCode();
349 __ENDTRY;
350 return status;
353 /**********************************************************************
354 * LdrAccessResource (NTDLL.@)
356 * NOTE
357 * On x86, Shrinker, an executable compressor, depends on the
358 * "call access_resource" instruction being there.
360 #ifdef __i386__
361 __ASM_STDCALL_FUNC( LdrAccessResource, 16,
362 "pushl %ebp\n\t"
363 "movl %esp, %ebp\n\t"
364 "subl $4,%esp\n\t"
365 "pushl 24(%ebp)\n\t"
366 "pushl 20(%ebp)\n\t"
367 "pushl 16(%ebp)\n\t"
368 "pushl 12(%ebp)\n\t"
369 "pushl 8(%ebp)\n\t"
370 "call " __ASM_NAME("access_resource") "\n\t"
371 "leave\n\t"
372 "ret $16"
374 #else
375 NTSTATUS WINAPI LdrAccessResource( HMODULE hmod, const IMAGE_RESOURCE_DATA_ENTRY *entry,
376 void **ptr, ULONG *size )
378 return access_resource( hmod, entry, ptr, size );
380 #endif
382 /**********************************************************************
383 * RtlFindMessage (NTDLL.@)
385 NTSTATUS WINAPI RtlFindMessage( HMODULE hmod, ULONG type, ULONG lang,
386 ULONG msg_id, const MESSAGE_RESOURCE_ENTRY **ret )
388 const MESSAGE_RESOURCE_DATA *data;
389 const MESSAGE_RESOURCE_BLOCK *block;
390 const IMAGE_RESOURCE_DATA_ENTRY *rsrc;
391 LDR_RESOURCE_INFO info;
392 NTSTATUS status;
393 void *ptr;
394 unsigned int i;
396 info.Type = type;
397 info.Name = 1;
398 info.Language = lang;
400 if ((status = LdrFindResource_U( hmod, &info, 3, &rsrc )) != STATUS_SUCCESS)
401 return status;
402 if ((status = LdrAccessResource( hmod, rsrc, &ptr, NULL )) != STATUS_SUCCESS)
403 return status;
405 data = ptr;
406 block = data->Blocks;
407 for (i = 0; i < data->NumberOfBlocks; i++, block++)
409 if (msg_id >= block->LowId && msg_id <= block->HighId)
411 const MESSAGE_RESOURCE_ENTRY *entry;
413 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)data + block->OffsetToEntries);
414 for (i = msg_id - block->LowId; i > 0; i--)
415 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)entry + entry->Length);
416 *ret = entry;
417 return STATUS_SUCCESS;
420 return STATUS_MESSAGE_NOT_FOUND;
423 /**********************************************************************
424 * RtlFormatMessage (NTDLL.@)
426 * Formats a message (similar to sprintf).
428 * PARAMS
429 * Message [I] Message to format.
430 * MaxWidth [I] Maximum width in characters of each output line.
431 * IgnoreInserts [I] Whether to copy the message without processing inserts.
432 * Ansi [I] Whether Arguments may have ANSI strings.
433 * ArgumentsIsArray [I] Whether Arguments is actually an array rather than a va_list *.
434 * Buffer [O] Buffer to store processed message in.
435 * BufferSize [I] Size of Buffer (in bytes?).
437 * RETURNS
438 * NTSTATUS code.
440 NTSTATUS WINAPI RtlFormatMessage( LPWSTR Message, UCHAR MaxWidth,
441 BOOLEAN IgnoreInserts, BOOLEAN Ansi,
442 BOOLEAN ArgumentIsArray, __ms_va_list * Arguments,
443 LPWSTR Buffer, ULONG BufferSize )
445 FIXME("(%s, %u, %s, %s, %s, %p, %p, %d)\n", debugstr_w(Message),
446 MaxWidth, IgnoreInserts ? "TRUE" : "FALSE", Ansi ? "TRUE" : "FALSE",
447 ArgumentIsArray ? "TRUE" : "FALSE", Arguments, Buffer, BufferSize);
448 return STATUS_SUCCESS;