ReactOS 0.4.15-dev-8207-gd73a838
LdrEnumResources.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS api tests
3 * LICENSE: GPLv2+ - See COPYING in the top level directory
4 * PURPOSE: Test for LdrEnumResources
5 * PROGRAMMER: Timo Kreuzer
6 */
7
8#include "precomp.h"
9
10#include <pseh/pseh2.h>
11
12typedef struct _TEST_RESOURCES
13{
33
34typedef struct _TEST_IMAGE
35{
41
42static
43VOID
45 PIMAGE_RESOURCE_DIRECTORY ResourceDirectory,
46 USHORT NumberOfNamedEntries,
47 USHORT NumberOfIdEntries)
48{
49 ResourceDirectory->Characteristics = 0;
50 ResourceDirectory->TimeDateStamp = 0;
51 ResourceDirectory->MajorVersion = 0;
52 ResourceDirectory->MinorVersion = 0;
53 ResourceDirectory->NumberOfNamedEntries = NumberOfNamedEntries;
54 ResourceDirectory->NumberOfIdEntries = NumberOfIdEntries;
55}
56
57static
58VOID
63 PVOID Data)
64{
65 DirEntry->Name = Resource->StringIndex * 2 + FIELD_OFFSET(TEST_RESOURCES, StringBuffer);
66 DirEntry->NameIsString = 1;
67 DirEntry->OffsetToData = (PUCHAR)Data - (PUCHAR)Resource;
68 if (DirEntry < Resource->Lang1Entries)
69 DirEntry->DataIsDirectory = 1;
70 Resource->StringBuffer[Resource->StringIndex] = (USHORT)wcslen(Name);
71 wcscpy(&Resource->StringBuffer[Resource->StringIndex + 1], Name);
72 Resource->StringIndex += Resource->StringBuffer[Resource->StringIndex] * 2 + 1;
73}
74
75static
76VOID
80 USHORT Id,
81 PVOID Data)
82{
83 DirEntry->Name = Id;
84 DirEntry->NameIsString = 0;
85 DirEntry->OffsetToData = (PUCHAR)Data - (PUCHAR)Resource;
86 if (DirEntry < Resource->Lang1Entries)
87 DirEntry->DataIsDirectory = 1;
88}
89
90static
91VOID
93 PVOID ImageBase,
95 PVOID Data,
96 ULONG Size)
97{
98
99 DataEntry->OffsetToData = (PUCHAR)Data - (PUCHAR)ImageBase;
100 DataEntry->Size = Size;
101 DataEntry->CodePage = 0;
102 DataEntry->Reserved = 0;
103}
104
105static
106VOID
108 PVOID ImageBase,
110{
111
112 memset(Resource->StringBuffer, 0, sizeof(Resource->StringBuffer));
113 Resource->StringIndex = 0;
114
115 InitializeResourceDirectory(&Resource->TypeDirectory, 0, 2);
116 InitializeIdEntry(Resource, &Resource->TypeEntries[0], 1, &Resource->Name1Directory);
117 InitializeIdEntry(Resource, &Resource->TypeEntries[1], 2, &Resource->Name2Directory);
118
119 InitializeResourceDirectory(&Resource->Name1Directory, 1, 1);
120 InitializeNamedEntry(Resource, &Resource->Name1Entries[0], L"TEST", &Resource->Lang1Directory);
121 InitializeIdEntry(Resource, &Resource->Name1Entries[1], 7, &Resource->Lang2Directory);
122
123 InitializeResourceDirectory(&Resource->Name2Directory, 2, 0);
124 InitializeNamedEntry(Resource, &Resource->Name2Entries[0], L"LOL", &Resource->Lang3Directory);
125 InitializeNamedEntry(Resource, &Resource->Name2Entries[1], L"xD", &Resource->Lang4Directory);
126
127 InitializeResourceDirectory(&Resource->Lang1Directory, 0, 2);
128 InitializeIdEntry(Resource, &Resource->Lang1Entries[0], 0x409, &Resource->DataEntries[0]);
129 InitializeIdEntry(Resource, &Resource->Lang1Entries[1], 0x407, &Resource->DataEntries[1]);
130
131 InitializeResourceDirectory(&Resource->Lang2Directory, 0, 2);
132 InitializeIdEntry(Resource, &Resource->Lang2Entries[0], 0x408, &Resource->DataEntries[2]);
133 InitializeIdEntry(Resource, &Resource->Lang2Entries[1], 0x406, &Resource->DataEntries[3]);
134
135 InitializeResourceDirectory(&Resource->Lang3Directory, 0, 2);
136 InitializeIdEntry(Resource, &Resource->Lang3Entries[0], 0x405, &Resource->DataEntries[4]);
137 InitializeIdEntry(Resource, &Resource->Lang3Entries[1], 0x403, &Resource->DataEntries[5]);
138
139 InitializeResourceDirectory(&Resource->Lang4Directory, 0, 2);
140 InitializeIdEntry(Resource, &Resource->Lang4Entries[0], 0x402, &Resource->DataEntries[6]);
141 InitializeIdEntry(Resource, &Resource->Lang4Entries[1], 0x404, &Resource->DataEntries[7]);
142
143 InitializeDataEntry(ImageBase, &Resource->DataEntries[0], Resource->StringBuffer + 0, 2);
144 InitializeDataEntry(ImageBase, &Resource->DataEntries[1], Resource->StringBuffer + 2, 4);
145 InitializeDataEntry(ImageBase, &Resource->DataEntries[2], Resource->StringBuffer + 4, 6);
146 InitializeDataEntry(ImageBase, &Resource->DataEntries[3], Resource->StringBuffer + 6, 8);
147 InitializeDataEntry(ImageBase, &Resource->DataEntries[4], Resource->StringBuffer + 8, 10);
148 InitializeDataEntry(ImageBase, &Resource->DataEntries[5], Resource->StringBuffer + 10, 12);
149 InitializeDataEntry(ImageBase, &Resource->DataEntries[6], Resource->StringBuffer + 12, 14);
150 InitializeDataEntry(ImageBase, &Resource->DataEntries[7], Resource->StringBuffer + 14, 16);
151
152}
153
154VOID
156 PTEST_IMAGE TestImage)
157{
158 PIMAGE_DATA_DIRECTORY ResourceDirectory;
159
161 TestImage->DosHeader.e_lfanew = sizeof(IMAGE_DOS_HEADER);
162
164
165 TestImage->NtHeaders.FileHeader.Machine = IMAGE_FILE_MACHINE_NATIVE;
167 TestImage->NtHeaders.FileHeader.TimeDateStamp = 0;
169 TestImage->NtHeaders.FileHeader.NumberOfSymbols = 0;
171 TestImage->NtHeaders.FileHeader.Characteristics = 0;
172
173#ifdef _WIN64
175#else
177#endif
178 TestImage->NtHeaders.OptionalHeader.ImageBase = (DWORD_PTR)TestImage;
179 TestImage->NtHeaders.OptionalHeader.SizeOfImage = sizeof(TEST_IMAGE);
182
184 ResourceDirectory->VirtualAddress = FIELD_OFFSET(TEST_IMAGE, Resources);
185 ResourceDirectory->Size = sizeof(TEST_RESOURCES);
186
187 strcpy((char*)TestImage->SectionHeaders[0].Name, ".rsrc");
188 TestImage->SectionHeaders[0].Misc.VirtualSize = sizeof(TEST_IMAGE);
190 TestImage->SectionHeaders[0].SizeOfRawData = sizeof(TEST_IMAGE);
192 TestImage->SectionHeaders[0].PointerToRelocations = 0;
193 TestImage->SectionHeaders[0].PointerToLinenumbers = 0;
194 TestImage->SectionHeaders[0].NumberOfRelocations = 0;
195 TestImage->SectionHeaders[0].NumberOfLinenumbers = 0;
196 TestImage->SectionHeaders[0].Characteristics = 0;
197
198 InitializeTestResource(TestImage, &TestImage->Resources);
199}
200
201#define ok_nwstr(str1, str2, count) \
202 ok(wcsncmp((PWCHAR)str1, (PWCHAR)str2, count) == 0, \
203 "string is wrong, expected: '%S', got '%S'\n", str1, str2); \
204
205#define ok_enumres(_Res, _Type, _Name, _Lang, _Data, _Size) \
206 ok_dec((_Res)->Type, _Type); \
207 if ((ULONG_PTR)(_Name) > 0xFFFF) \
208 { \
209 ok_size_t(*(WORD*)((_Res)->Name), wcslen((PWCHAR)(_Name))); \
210 ok_nwstr((PWCHAR)((_Res)->Name + 2), (PWCHAR)_Name, *(WORD*)((_Res)->Name)); \
211 } \
212 else \
213 { \
214 ok_dec((_Res)->Name, (ULONG_PTR)_Name); \
215 } \
216 ok_hex((_Res)->Language, _Lang); \
217 ok_ptr((PVOID)(_Res)->Data, _Data); \
218 ok_dec((_Res)->Size, _Size); \
219 ok_dec((_Res)->Reserved, 0);
220
221static
222void
224{
225 LDR_RESOURCE_INFO ResourceInfo;
226 LDR_ENUM_RESOURCE_INFO EnumRes[8];
229
230 InitializeTestImage(TestImage);
231
232 memset(EnumRes, 0xcc, sizeof(EnumRes));
233
234 ResourceInfo.Type = 1;
235 ResourceInfo.Name = 1;
236 ResourceInfo.Language = 0x408;
237 ResourceCount = 8;
238 Status = LdrEnumResources(TestImage, &ResourceInfo, 0, &ResourceCount, EnumRes);
241
242 ok_ptr((PVOID)EnumRes[0].Data, TestImage->Resources.StringBuffer);
243 ok_dec(EnumRes[0].Size, 2);
244
245 ok_enumres(&EnumRes[0], 1, L"TEST", 0x409, TestImage->Resources.StringBuffer, 2)
246 ok_enumres(&EnumRes[1], 1, L"TEST", 0x407, TestImage->Resources.StringBuffer + 2, 4)
247 ok_enumres(&EnumRes[2], 1, 7, 0x408, TestImage->Resources.StringBuffer + 4, 6)
248 ok_enumres(&EnumRes[3], 1, 7, 0x406, TestImage->Resources.StringBuffer + 6, 8)
249 ok_enumres(&EnumRes[4], 2, L"LOL", 0x405, TestImage->Resources.StringBuffer + 8, 10)
250 ok_enumres(&EnumRes[5], 2, L"LOL", 0x403, TestImage->Resources.StringBuffer + 10, 12)
251 ok_enumres(&EnumRes[6], 2, L"xD", 0x402, TestImage->Resources.StringBuffer + 12, 14)
252 ok_enumres(&EnumRes[7], 2, L"xD", 0x404, TestImage->Resources.StringBuffer + 14, 16)
253
254 Status = LdrEnumResources(TestImage, &ResourceInfo, 1, &ResourceCount, EnumRes);
257
258}
259
260
261static
262void
264{
265 LDR_RESOURCE_INFO ResourceInfo;
269
270 InitializeTestImage(TestImage);
271
272 ResourceInfo.Type = 6;
273 ResourceInfo.Name = 1;
274 ResourceInfo.Language = 0x409;
275
276 ResourceCount = 8;
277 Status = LdrEnumResources(TestImage, &ResourceInfo, 3, &ResourceCount, Resources);
280
281 ResourceInfo.Type = 1;
282 ResourceInfo.Name = 7;
283 ResourceInfo.Language = 0x406;
284 ResourceCount = 8;
285 Status = LdrEnumResources(TestImage, &ResourceInfo, 0, &ResourceCount, Resources);
288
289 ResourceCount = 8;
290 Status = LdrEnumResources(TestImage, &ResourceInfo, 1, &ResourceCount, Resources);
293
294 ResourceCount = 8;
295 Status = LdrEnumResources(TestImage, &ResourceInfo, 2, &ResourceCount, Resources);
298
299 ResourceCount = 8;
300 Status = LdrEnumResources(TestImage, &ResourceInfo, 3, &ResourceCount, Resources);
303
304 ResourceCount = 8;
305 Status = LdrEnumResources(TestImage, &ResourceInfo, 99, &ResourceCount, Resources);
308
309 ResourceCount = 0;
310 Status = LdrEnumResources(TestImage, &ResourceInfo, 0, &ResourceCount, Resources);
313
314 ResourceCount = 7;
315 Status = LdrEnumResources(TestImage, &ResourceInfo, 0, &ResourceCount, Resources);
318
319 ResourceCount = 8;
320 Status = LdrEnumResources(NULL, &ResourceInfo, 1, &ResourceCount, Resources);
323
324 ResourceCount = 8;
325 Status = LdrEnumResources(TestImage, &ResourceInfo, 1, &ResourceCount, NULL);
328
329 ResourceCount = 8;
331 {
333 }
335 {
336 Status = 0xDeadC0de;
337 }
339 ok_hex(Status, 0xDeadC0de);
340
341 ResourceCount = 42;
343 {
344 Status = LdrEnumResources(TestImage, &ResourceInfo, 1, NULL, Resources);
345 }
347 {
348 Status = 0xDeadC0de;
349 }
351 ok_hex(Status, 0xDeadC0de);
353
354 ResourceCount = 8;
355 Status = LdrEnumResources(TestImage + 2, &ResourceInfo, 1, &ResourceCount, NULL);
358
359 TestImage->Resources.TypeEntries[0].DataIsDirectory = 0;
360 Status = LdrEnumResources(TestImage, &ResourceInfo, 1, &ResourceCount, NULL);
363
364}
365
367{
368 TEST_IMAGE TestImage;
369 RtlZeroMemory(&TestImage, sizeof(TestImage));
370
371 Test_Parameters(&TestImage);
372 Test_Data(&TestImage);
373
374}
DWORD Id
static VOID InitializeDataEntry(PVOID ImageBase, PIMAGE_RESOURCE_DATA_ENTRY DataEntry, PVOID Data, ULONG Size)
struct _TEST_IMAGE TEST_IMAGE
#define ok_enumres(_Res, _Type, _Name, _Lang, _Data, _Size)
struct _TEST_RESOURCES * PTEST_RESOURCES
static void Test_Data(PTEST_IMAGE TestImage)
struct _TEST_RESOURCES TEST_RESOURCES
static VOID InitializeNamedEntry(PTEST_RESOURCES Resource, PIMAGE_RESOURCE_DIRECTORY_ENTRY DirEntry, PWCHAR Name, PVOID Data)
struct _TEST_IMAGE * PTEST_IMAGE
static VOID InitializeResourceDirectory(PIMAGE_RESOURCE_DIRECTORY ResourceDirectory, USHORT NumberOfNamedEntries, USHORT NumberOfIdEntries)
VOID InitializeTestImage(PTEST_IMAGE TestImage)
static void Test_Parameters(PTEST_IMAGE TestImage)
static VOID InitializeTestResource(PVOID ImageBase, PTEST_RESOURCES Resource)
static VOID InitializeIdEntry(PTEST_RESOURCES Resource, PIMAGE_RESOURCE_DIRECTORY_ENTRY DirEntry, USHORT Id, PVOID Data)
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
#define ok_hex(expression, result)
Definition: atltest.h:94
#define ok_dec(expression, result)
Definition: atltest.h:101
#define START_TEST(x)
Definition: atltest.h:75
#define ok_ptr(expression, result)
Definition: atltest.h:108
LONG NTSTATUS
Definition: precomp.h:26
_Acquires_exclusive_lock_ Resource _Acquires_shared_lock_ Resource _Inout_ PERESOURCE Resource
Definition: cdprocs.h:843
#define NULL
Definition: types.h:112
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
#define _SEH2_END
Definition: filesup.c:22
#define _SEH2_TRY
Definition: filesup.c:19
struct _IMAGE_DOS_HEADER IMAGE_DOS_HEADER
Status
Definition: gdiplustypes.h:25
static ULONG ResourceCount
Definition: inbv.c:50
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
NTSTATUS NTAPI LdrEnumResources(_In_ PVOID BaseAddress, _In_ PLDR_RESOURCE_INFO ResourceInfo, _In_ ULONG Level, _Inout_ ULONG *ResourceCount, _Out_writes_to_(*ResourceCount, *ResourceCount) LDR_ENUM_RESOURCE_INFO *Resources)
Definition: res.c:343
WCHAR StringBuffer[156]
Definition: ldrinit.c:41
#define IMAGE_NT_OPTIONAL_HDR32_MAGIC
Definition: ntimage.h:376
#define IMAGE_NT_OPTIONAL_HDR64_MAGIC
Definition: ntimage.h:377
#define STATUS_INVALID_IMAGE_FORMAT
Definition: ntstatus.h:359
#define STATUS_RESOURCE_DATA_NOT_FOUND
Definition: ntstatus.h:373
#define L(x)
Definition: ntvdm.h:50
struct _IMAGE_OPTIONAL_HEADER IMAGE_OPTIONAL_HEADER
#define IMAGE_NT_SIGNATURE
Definition: pedump.c:93
unsigned short USHORT
Definition: pedump.c:61
#define IMAGE_DOS_SIGNATURE
Definition: pedump.c:89
#define IMAGE_DIRECTORY_ENTRY_RESOURCE
Definition: pedump.c:261
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:66
_CRTIMP wchar_t *__cdecl wcscpy(_Out_writes_z_(_String_length_(_Source)+1) wchar_t *_Dest, _In_z_ const wchar_t *_Source)
#define memset(x, y, z)
Definition: compat.h:39
#define STATUS_SUCCESS
Definition: shellext.h:65
DWORD NumberOfSymbols
Definition: ntddk_ex.h:126
DWORD PointerToSymbolTable
Definition: ntddk_ex.h:125
WORD SizeOfOptionalHeader
Definition: ntddk_ex.h:127
IMAGE_OPTIONAL_HEADER32 OptionalHeader
Definition: ntddk_ex.h:184
IMAGE_FILE_HEADER FileHeader
Definition: ntddk_ex.h:183
IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES]
Definition: ntddk_ex.h:178
Definition: pedump.c:458
DWORD Reserved
Definition: pedump.c:462
DWORD CodePage
Definition: pedump.c:461
DWORD OffsetToData
Definition: pedump.c:459
DWORD Size
Definition: pedump.c:460
Definition: pedump.c:414
ULONG DataIsDirectory
Definition: ntimage.h:195
DWORD PointerToLinenumbers
Definition: pedump.c:292
DWORD PointerToRelocations
Definition: pedump.c:291
DWORD PointerToRawData
Definition: pedump.c:290
BYTE Name[IMAGE_SIZEOF_SHORT_NAME]
Definition: pedump.c:281
WORD NumberOfLinenumbers
Definition: pedump.c:294
WORD NumberOfRelocations
Definition: pedump.c:293
union _IMAGE_SECTION_HEADER::@1565 Misc
ULONG_PTR Language
Definition: ldrtypes.h:183
ULONG_PTR Name
Definition: ldrtypes.h:182
ULONG_PTR Type
Definition: ldrtypes.h:181
TEST_RESOURCES Resources
IMAGE_SECTION_HEADER SectionHeaders[1]
IMAGE_DOS_HEADER DosHeader
IMAGE_NT_HEADERS NtHeaders
IMAGE_RESOURCE_DIRECTORY Lang2Directory
IMAGE_RESOURCE_DIRECTORY_ENTRY Lang4Entries[2]
IMAGE_RESOURCE_DIRECTORY_ENTRY Lang1Entries[2]
IMAGE_RESOURCE_DIRECTORY_ENTRY Lang2Entries[2]
IMAGE_RESOURCE_DIRECTORY_ENTRY Lang3Entries[2]
IMAGE_RESOURCE_DIRECTORY_ENTRY TypeEntries[2]
IMAGE_RESOURCE_DATA_ENTRY DataEntries[8]
IMAGE_RESOURCE_DIRECTORY Lang4Directory
IMAGE_RESOURCE_DIRECTORY_STRING Name1String
IMAGE_RESOURCE_DIRECTORY Lang1Directory
IMAGE_RESOURCE_DIRECTORY_ENTRY Name2Entries[2]
WCHAR StringBuffer[20]
IMAGE_RESOURCE_DIRECTORY Name1Directory
IMAGE_RESOURCE_DIRECTORY_ENTRY Name1Entries[2]
IMAGE_RESOURCE_DIRECTORY Lang3Directory
IMAGE_RESOURCE_DIRECTORY TypeDirectory
IMAGE_RESOURCE_DIRECTORY Name2Directory
#define DWORD_PTR
Definition: treelist.c:76
#define FIELD_OFFSET(t, f)
Definition: typedefs.h:255
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
uint16_t * PWCHAR
Definition: typedefs.h:56
unsigned char * PUCHAR
Definition: typedefs.h:53
uint32_t ULONG
Definition: typedefs.h:59
#define STATUS_INFO_LENGTH_MISMATCH
Definition: udferr_usr.h:133
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ DEVPROPTYPE _In_ ULONG Size
Definition: wdfdevice.h:4533
__wchar_t WCHAR
Definition: xmlstorage.h:180