/
resource.c
506 lines (441 loc) · 16.2 KB
/
resource.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/*
* PE file resources
*
* Copyright 1995 Thomas Sandford
* Copyright 1996 Martin von Loewis
* Copyright 2003 Alexandre Julliard
*
* Based on the Win16 resource handling code in loader/resource.c
* Copyright 1993 Robert J. Amstadt
* Copyright 1995 Alexandre Julliard
* Copyright 1997 Marcus Meissner
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "config.h"
#include "wine/port.h"
#include <stdarg.h>
#include <stdlib.h>
#include <sys/types.h>
#define NONAMELESSUNION
#define NONAMELESSSTRUCT
#include "ntstatus.h"
#define WIN32_NO_STATUS
#include "windef.h"
#include "winbase.h"
#include "winnt.h"
#include "winternl.h"
#include "wine/exception.h"
#include "wine/unicode.h"
#include "wine/debug.h"
WINE_DEFAULT_DEBUG_CHANNEL(resource);
static LCID user_lcid, system_lcid;
static LANGID user_ui_language, system_ui_language;
#define IS_INTRESOURCE(x) (((ULONG_PTR)(x) >> 16) == 0)
/**********************************************************************
* is_data_file_module
*
* Check if a module handle is for a LOAD_LIBRARY_AS_DATAFILE module.
*/
static inline BOOL is_data_file_module( HMODULE hmod )
{
return (ULONG_PTR)hmod & 1;
}
/**********************************************************************
* push_language
*
* push a language in the list of languages to try
*/
static inline int push_language( WORD *list, int pos, WORD lang )
{
int i;
for (i = 0; i < pos; i++) if (list[i] == lang) return pos;
list[pos++] = lang;
return pos;
}
/**********************************************************************
* find_first_entry
*
* Find the first suitable entry in a resource directory
*/
static const IMAGE_RESOURCE_DIRECTORY *find_first_entry( const IMAGE_RESOURCE_DIRECTORY *dir,
const void *root, int want_dir )
{
const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
int pos;
for (pos = 0; pos < dir->NumberOfNamedEntries + dir->NumberOfIdEntries; pos++)
{
if (!entry[pos].u2.s2.DataIsDirectory == !want_dir)
return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s2.OffsetToDirectory);
}
return NULL;
}
/**********************************************************************
* find_entry_by_id
*
* Find an entry by id in a resource directory
*/
static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
WORD id, const void *root, int want_dir )
{
const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
int min, max, pos;
entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
min = dir->NumberOfNamedEntries;
max = min + dir->NumberOfIdEntries - 1;
while (min <= max)
{
pos = (min + max) / 2;
if (entry[pos].u.Id == id)
{
if (!entry[pos].u2.s2.DataIsDirectory == !want_dir)
{
TRACE("root %p dir %p id %04x ret %p\n",
root, dir, id, (const char*)root + entry[pos].u2.s2.OffsetToDirectory);
return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s2.OffsetToDirectory);
}
break;
}
if (entry[pos].u.Id > id) max = pos - 1;
else min = pos + 1;
}
TRACE("root %p dir %p id %04x not found\n", root, dir, id );
return NULL;
}
/**********************************************************************
* find_entry_by_name
*
* Find an entry by name in a resource directory
*/
static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_name( const IMAGE_RESOURCE_DIRECTORY *dir,
LPCWSTR name, const void *root,
int want_dir )
{
const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
const IMAGE_RESOURCE_DIR_STRING_U *str;
int min, max, res, pos, namelen;
if (IS_INTRESOURCE(name)) return find_entry_by_id( dir, LOWORD(name), root, want_dir );
entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
namelen = strlenW(name);
min = 0;
max = dir->NumberOfNamedEntries - 1;
while (min <= max)
{
pos = (min + max) / 2;
str = (const IMAGE_RESOURCE_DIR_STRING_U *)((const char *)root + entry[pos].u.s.NameOffset);
res = strncmpW( name, str->NameString, str->Length );
if (!res && namelen == str->Length)
{
if (!entry[pos].u2.s2.DataIsDirectory == !want_dir)
{
TRACE("root %p dir %p name %s ret %p\n",
root, dir, debugstr_w(name), (const char*)root + entry[pos].u2.s2.OffsetToDirectory);
return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s2.OffsetToDirectory);
}
break;
}
if (res < 0) max = pos - 1;
else min = pos + 1;
}
TRACE("root %p dir %p name %s not found\n", root, dir, debugstr_w(name) );
return NULL;
}
/**********************************************************************
* find_entry
*
* Find a resource entry
*/
static NTSTATUS find_entry( HMODULE hmod, const LDR_RESOURCE_INFO *info,
ULONG level, const void **ret, int want_dir )
{
ULONG size;
const void *root;
const IMAGE_RESOURCE_DIRECTORY *resdirptr;
WORD list[9]; /* list of languages to try */
int i, pos = 0;
root = RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_RESOURCE, &size );
if (!root) return STATUS_RESOURCE_DATA_NOT_FOUND;
if (size < sizeof(*resdirptr)) return STATUS_RESOURCE_DATA_NOT_FOUND;
resdirptr = root;
if (!level--) goto done;
if (!(*ret = find_entry_by_name( resdirptr, (LPCWSTR)info->Type, root, want_dir || level )))
return STATUS_RESOURCE_TYPE_NOT_FOUND;
if (!level--) return STATUS_SUCCESS;
resdirptr = *ret;
if (!(*ret = find_entry_by_name( resdirptr, (LPCWSTR)info->Name, root, want_dir || level )))
return STATUS_RESOURCE_NAME_NOT_FOUND;
if (!level--) return STATUS_SUCCESS;
if (level) return STATUS_INVALID_PARAMETER; /* level > 3 */
/* 1. specified language */
pos = push_language( list, pos, info->Language );
/* 2. specified language with neutral sublanguage */
pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(info->Language), SUBLANG_NEUTRAL ) );
/* 3. neutral language with neutral sublanguage */
pos = push_language( list, pos, MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ) );
/* if no explicitly specified language, try some defaults */
if (PRIMARYLANGID(info->Language) == LANG_NEUTRAL)
{
/* user defaults, unless SYS_DEFAULT sublanguage specified */
if (SUBLANGID(info->Language) != SUBLANG_SYS_DEFAULT)
{
/* 4. current thread locale language */
pos = push_language( list, pos, LANGIDFROMLCID(NtCurrentTeb()->CurrentLocale) );
/* 5. user locale language */
pos = push_language( list, pos, LANGIDFROMLCID(user_lcid) );
/* 6. user locale language with neutral sublanguage */
pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(user_lcid), SUBLANG_NEUTRAL ) );
}
/* now system defaults */
/* 7. system locale language */
pos = push_language( list, pos, LANGIDFROMLCID( system_lcid ) );
/* 8. system locale language with neutral sublanguage */
pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(system_lcid), SUBLANG_NEUTRAL ) );
/* 9. English */
pos = push_language( list, pos, MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT ) );
}
resdirptr = *ret;
for (i = 0; i < pos; i++)
if ((*ret = find_entry_by_id( resdirptr, list[i], root, want_dir ))) return STATUS_SUCCESS;
/* if no explicitly specified language, return the first entry */
if (PRIMARYLANGID(info->Language) == LANG_NEUTRAL)
{
if ((*ret = find_first_entry( resdirptr, root, want_dir ))) return STATUS_SUCCESS;
}
return STATUS_RESOURCE_LANG_NOT_FOUND;
done:
*ret = resdirptr;
return STATUS_SUCCESS;
}
/**********************************************************************
* LdrFindResourceDirectory_U (NTDLL.@)
*/
NTSTATUS WINAPI LdrFindResourceDirectory_U( HMODULE hmod, const LDR_RESOURCE_INFO *info,
ULONG level, const IMAGE_RESOURCE_DIRECTORY **dir )
{
const void *res;
NTSTATUS status;
__TRY
{
if (info) TRACE( "module %p type %s name %s lang %04x level %d\n",
hmod, debugstr_w((LPCWSTR)info->Type),
level > 1 ? debugstr_w((LPCWSTR)info->Name) : "",
level > 2 ? info->Language : 0, level );
status = find_entry( hmod, info, level, &res, TRUE );
if (status == STATUS_SUCCESS) *dir = res;
}
__EXCEPT_PAGE_FAULT
{
return GetExceptionCode();
}
__ENDTRY;
return status;
}
/**********************************************************************
* LdrFindResource_U (NTDLL.@)
*/
NTSTATUS WINAPI LdrFindResource_U( HMODULE hmod, const LDR_RESOURCE_INFO *info,
ULONG level, const IMAGE_RESOURCE_DATA_ENTRY **entry )
{
const void *res;
NTSTATUS status;
__TRY
{
if (info) TRACE( "module %p type %s name %s lang %04x level %d\n",
hmod, debugstr_w((LPCWSTR)info->Type),
level > 1 ? debugstr_w((LPCWSTR)info->Name) : "",
level > 2 ? info->Language : 0, level );
status = find_entry( hmod, info, level, &res, FALSE );
if (status == STATUS_SUCCESS) *entry = res;
}
__EXCEPT_PAGE_FAULT
{
return GetExceptionCode();
}
__ENDTRY;
return status;
}
/* don't penalize other platforms stuff needed on i386 for compatibility */
#ifdef __i386__
NTSTATUS WINAPI access_resource( HMODULE hmod, const IMAGE_RESOURCE_DATA_ENTRY *entry,
void **ptr, ULONG *size )
#else
static inline NTSTATUS access_resource( HMODULE hmod, const IMAGE_RESOURCE_DATA_ENTRY *entry,
void **ptr, ULONG *size )
#endif
{
NTSTATUS status;
__TRY
{
ULONG dirsize;
if (!RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_RESOURCE, &dirsize ))
status = STATUS_RESOURCE_DATA_NOT_FOUND;
else
{
if (ptr)
{
if (is_data_file_module(hmod))
{
HMODULE mod = (HMODULE)((ULONG_PTR)hmod & ~1);
*ptr = RtlImageRvaToVa( RtlImageNtHeader(mod), mod, entry->OffsetToData, NULL );
}
else *ptr = (char *)hmod + entry->OffsetToData;
}
if (size) *size = entry->Size;
status = STATUS_SUCCESS;
}
}
__EXCEPT_PAGE_FAULT
{
return GetExceptionCode();
}
__ENDTRY;
return status;
}
/**********************************************************************
* LdrAccessResource (NTDLL.@)
*
* NOTE
* On x86, Shrinker, an executable compressor, depends on the
* "call access_resource" instruction being there.
*/
#ifdef __i386__
__ASM_STDCALL_FUNC( LdrAccessResource, 16,
"pushl %ebp\n\t"
"movl %esp, %ebp\n\t"
"subl $4,%esp\n\t"
"pushl 24(%ebp)\n\t"
"pushl 20(%ebp)\n\t"
"pushl 16(%ebp)\n\t"
"pushl 12(%ebp)\n\t"
"pushl 8(%ebp)\n\t"
"call " __ASM_NAME("access_resource") "\n\t"
"leave\n\t"
"ret $16"
)
#else
NTSTATUS WINAPI LdrAccessResource( HMODULE hmod, const IMAGE_RESOURCE_DATA_ENTRY *entry,
void **ptr, ULONG *size )
{
return access_resource( hmod, entry, ptr, size );
}
#endif
/**********************************************************************
* RtlFindMessage (NTDLL.@)
*/
NTSTATUS WINAPI RtlFindMessage( HMODULE hmod, ULONG type, ULONG lang,
ULONG msg_id, const MESSAGE_RESOURCE_ENTRY **ret )
{
const MESSAGE_RESOURCE_DATA *data;
const MESSAGE_RESOURCE_BLOCK *block;
const IMAGE_RESOURCE_DATA_ENTRY *rsrc;
LDR_RESOURCE_INFO info;
NTSTATUS status;
void *ptr;
unsigned int i;
info.Type = type;
info.Name = 1;
info.Language = lang;
if ((status = LdrFindResource_U( hmod, &info, 3, &rsrc )) != STATUS_SUCCESS)
return status;
if ((status = LdrAccessResource( hmod, rsrc, &ptr, NULL )) != STATUS_SUCCESS)
return status;
data = ptr;
block = data->Blocks;
for (i = 0; i < data->NumberOfBlocks; i++, block++)
{
if (msg_id >= block->LowId && msg_id <= block->HighId)
{
const MESSAGE_RESOURCE_ENTRY *entry;
entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)data + block->OffsetToEntries);
for (i = msg_id - block->LowId; i > 0; i--)
entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)entry + entry->Length);
*ret = entry;
return STATUS_SUCCESS;
}
}
return STATUS_MESSAGE_NOT_FOUND;
}
/**********************************************************************
* RtlFormatMessage (NTDLL.@)
*
* Formats a message (similar to sprintf).
*
* PARAMS
* Message [I] Message to format.
* MaxWidth [I] Maximum width in characters of each output line.
* IgnoreInserts [I] Whether to copy the message without processing inserts.
* Ansi [I] Whether Arguments may have ANSI strings.
* ArgumentsIsArray [I] Whether Arguments is actually an array rather than a va_list *.
* Buffer [O] Buffer to store processed message in.
* BufferSize [I] Size of Buffer (in bytes?).
*
* RETURNS
* NTSTATUS code.
*/
NTSTATUS WINAPI RtlFormatMessage( LPWSTR Message, UCHAR MaxWidth,
BOOLEAN IgnoreInserts, BOOLEAN Ansi,
BOOLEAN ArgumentIsArray, __ms_va_list * Arguments,
LPWSTR Buffer, ULONG BufferSize )
{
FIXME("(%s, %u, %s, %s, %s, %p, %p, %d)\n", debugstr_w(Message),
MaxWidth, IgnoreInserts ? "TRUE" : "FALSE", Ansi ? "TRUE" : "FALSE",
ArgumentIsArray ? "TRUE" : "FALSE", Arguments, Buffer, BufferSize);
return STATUS_SUCCESS;
}
/**********************************************************************
* NtQueryDefaultLocale (NTDLL.@)
*/
NTSTATUS WINAPI NtQueryDefaultLocale( BOOLEAN user, LCID *lcid )
{
*lcid = user ? user_lcid : system_lcid;
return STATUS_SUCCESS;
}
/**********************************************************************
* NtSetDefaultLocale (NTDLL.@)
*/
NTSTATUS WINAPI NtSetDefaultLocale( BOOLEAN user, LCID lcid )
{
if (user) user_lcid = lcid;
else
{
system_lcid = lcid;
system_ui_language = LANGIDFROMLCID(lcid); /* there is no separate call to set it */
}
return STATUS_SUCCESS;
}
/**********************************************************************
* NtQueryDefaultUILanguage (NTDLL.@)
*/
NTSTATUS WINAPI NtQueryDefaultUILanguage( LANGID *lang )
{
*lang = user_ui_language;
return STATUS_SUCCESS;
}
/**********************************************************************
* NtSetDefaultUILanguage (NTDLL.@)
*/
NTSTATUS WINAPI NtSetDefaultUILanguage( LANGID lang )
{
user_ui_language = lang;
return STATUS_SUCCESS;
}
/**********************************************************************
* NtQueryInstallUILanguage (NTDLL.@)
*/
NTSTATUS WINAPI NtQueryInstallUILanguage( LANGID *lang )
{
*lang = system_ui_language;
return STATUS_SUCCESS;
}