ReactOS  0.4.15-dev-5608-gafb953a
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 
12 typedef struct _TEST_RESOURCES
13 {
33 
34 typedef struct _TEST_IMAGE
35 {
41 
42 static
43 VOID
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 
57 static
58 VOID
62  PWCHAR Name,
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 
75 static
76 VOID
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 
90 static
91 VOID
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 
105 static
106 VOID
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 
154 VOID
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;
166  TestImage->NtHeaders.FileHeader.NumberOfSections = 1;
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 
183  ResourceDirectory = &TestImage->NtHeaders.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE];
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 
221 static
222 void
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);
240  ok_dec(ResourceCount, 8);
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);
256  ok_dec(ResourceCount, 4);
257 
258 }
259 
260 
261 static
262 void
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);
279  ok_dec(ResourceCount, 0);
280 
281  ResourceInfo.Type = 1;
282  ResourceInfo.Name = 7;
283  ResourceInfo.Language = 0x406;
284  ResourceCount = 8;
285  Status = LdrEnumResources(TestImage, &ResourceInfo, 0, &ResourceCount, Resources);
287  ok_dec(ResourceCount, 8);
288 
289  ResourceCount = 8;
290  Status = LdrEnumResources(TestImage, &ResourceInfo, 1, &ResourceCount, Resources);
292  ok_dec(ResourceCount, 4);
293 
294  ResourceCount = 8;
295  Status = LdrEnumResources(TestImage, &ResourceInfo, 2, &ResourceCount, Resources);
297  ok_dec(ResourceCount, 2);
298 
299  ResourceCount = 8;
300  Status = LdrEnumResources(TestImage, &ResourceInfo, 3, &ResourceCount, Resources);
302  ok_dec(ResourceCount, 1);
303 
304  ResourceCount = 8;
305  Status = LdrEnumResources(TestImage, &ResourceInfo, 99, &ResourceCount, Resources);
307  ok_dec(ResourceCount, 1);
308 
309  ResourceCount = 0;
310  Status = LdrEnumResources(TestImage, &ResourceInfo, 0, &ResourceCount, Resources);
312  ok_dec(ResourceCount, 8);
313 
314  ResourceCount = 7;
315  Status = LdrEnumResources(TestImage, &ResourceInfo, 0, &ResourceCount, Resources);
317  ok_dec(ResourceCount, 8);
318 
319  ResourceCount = 8;
320  Status = LdrEnumResources(NULL, &ResourceInfo, 1, &ResourceCount, Resources);
322  ok_dec(ResourceCount, 0);
323 
324  ResourceCount = 8;
325  Status = LdrEnumResources(TestImage, &ResourceInfo, 1, &ResourceCount, NULL);
327  ok_dec(ResourceCount, 4);
328 
329  ResourceCount = 8;
330  _SEH2_TRY
331  {
333  }
335  {
336  Status = 0xDeadC0de;
337  }
338  _SEH2_END
339  ok_hex(Status, 0xDeadC0de);
340 
341  ResourceCount = 42;
342  _SEH2_TRY
343  {
344  Status = LdrEnumResources(TestImage, &ResourceInfo, 1, NULL, Resources);
345  }
347  {
348  Status = 0xDeadC0de;
349  }
350  _SEH2_END
351  ok_hex(Status, 0xDeadC0de);
352  ok_dec(ResourceCount, 42);
353 
354  ResourceCount = 8;
355  Status = LdrEnumResources(TestImage + 2, &ResourceInfo, 1, &ResourceCount, NULL);
357  ok_dec(ResourceCount, 0);
358 
359  TestImage->Resources.TypeEntries[0].DataIsDirectory = 0;
360  Status = LdrEnumResources(TestImage, &ResourceInfo, 1, &ResourceCount, NULL);
362  ok_dec(ResourceCount, 0);
363 
364 }
365 
367 {
368  TEST_IMAGE TestImage;
369  RtlZeroMemory(&TestImage, sizeof(TestImage));
370 
371  Test_Parameters(&TestImage);
372  Test_Data(&TestImage);
373 
374 }
_SEH2_TRY
Definition: create.c:4226
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
DWORD CodePage
Definition: pedump.c:461
IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES]
Definition: ntddk_ex.h:178
#define STATUS_INFO_LENGTH_MISMATCH
Definition: udferr_usr.h:133
static void Test_Parameters(PTEST_IMAGE TestImage)
#define DWORD_PTR
Definition: treelist.c:76
IMAGE_RESOURCE_DIRECTORY Name1Directory
WORD NumberOfRelocations
Definition: pedump.c:293
WCHAR StringBuffer[20]
IMAGE_RESOURCE_DIRECTORY_ENTRY Lang1Entries[2]
unsigned char * PUCHAR
Definition: retypes.h:3
union _IMAGE_SECTION_HEADER::@1517 Misc
LONG NTSTATUS
Definition: precomp.h:26
struct _TEST_IMAGE TEST_IMAGE
#define IMAGE_NT_OPTIONAL_HDR32_MAGIC
Definition: ntimage.h:376
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:361
IMAGE_RESOURCE_DIRECTORY Lang2Directory
#define ok_ptr(expression, result)
Definition: atltest.h:108
uint16_t * PWCHAR
Definition: typedefs.h:56
_SEH2_END
Definition: create.c:4400
IMAGE_RESOURCE_DIRECTORY Name2Directory
DWORD PointerToRawData
Definition: pedump.c:290
#define ok_enumres(_Res, _Type, _Name, _Lang, _Data, _Size)
IMAGE_RESOURCE_DIRECTORY Lang4Directory
#define ok_hex(expression, result)
Definition: atltest.h:94
ULONG_PTR Type
Definition: ldrtypes.h:181
DWORD Id
#define IMAGE_DOS_SIGNATURE
Definition: pedump.c:89
TEST_RESOURCES Resources
IMAGE_RESOURCE_DIRECTORY Lang3Directory
#define L(x)
Definition: ntvdm.h:50
_Acquires_exclusive_lock_ Resource _Acquires_shared_lock_ Resource _Inout_ PERESOURCE Resource
Definition: cdprocs.h:843
START_TEST(LdrEnumResources)
IMAGE_RESOURCE_DIRECTORY_STRING Name1String
ULONG_PTR Name
Definition: ldrtypes.h:182
Definition: pedump.c:457
WORD SizeOfOptionalHeader
Definition: ntddk_ex.h:127
IMAGE_FILE_HEADER FileHeader
Definition: ntddk_ex.h:183
struct _TEST_IMAGE * PTEST_IMAGE
#define IMAGE_NT_SIGNATURE
Definition: pedump.c:93
Status
Definition: gdiplustypes.h:24
DWORD OffsetToData
Definition: pedump.c:459
IMAGE_RESOURCE_DIRECTORY_ENTRY TypeEntries[2]
struct _IMAGE_OPTIONAL_HEADER IMAGE_OPTIONAL_HEADER
WORD NumberOfLinenumbers
Definition: pedump.c:294
#define STATUS_INVALID_IMAGE_FORMAT
Definition: ntstatus.h:359
DWORD NumberOfSymbols
Definition: ntddk_ex.h:126
IMAGE_NT_HEADERS NtHeaders
DWORD PointerToLinenumbers
Definition: pedump.c:292
__wchar_t WCHAR
Definition: xmlstorage.h:180
IMAGE_RESOURCE_DIRECTORY Lang1Directory
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
IMAGE_DOS_HEADER DosHeader
struct _TEST_RESOURCES TEST_RESOURCES
#define IMAGE_NT_OPTIONAL_HDR64_MAGIC
Definition: ntimage.h:377
VOID InitializeTestImage(PTEST_IMAGE TestImage)
IMAGE_RESOURCE_DATA_ENTRY DataEntries[8]
static VOID InitializeIdEntry(PTEST_RESOURCES Resource, PIMAGE_RESOURCE_DIRECTORY_ENTRY DirEntry, USHORT Id, PVOID Data)
static ULONG ResourceCount
Definition: inbv.c:50
struct _TEST_RESOURCES * PTEST_RESOURCES
#define STATUS_RESOURCE_DATA_NOT_FOUND
Definition: ntstatus.h:373
_CRTIMP wchar_t *__cdecl wcscpy(_Out_writes_z_(_String_length_(_Source)+1) wchar_t *_Dest, _In_z_ const wchar_t *_Source)
static VOID InitializeResourceDirectory(PIMAGE_RESOURCE_DIRECTORY ResourceDirectory, USHORT NumberOfNamedEntries, USHORT NumberOfIdEntries)
IMAGE_RESOURCE_DIRECTORY_ENTRY Name1Entries[2]
static VOID InitializeDataEntry(PVOID ImageBase, PIMAGE_RESOURCE_DATA_ENTRY DataEntry, PVOID Data, ULONG Size)
static VOID InitializeTestResource(PVOID ImageBase, PTEST_RESOURCES Resource)
struct _IMAGE_DOS_HEADER IMAGE_DOS_HEADER
IMAGE_OPTIONAL_HEADER32 OptionalHeader
Definition: ntddk_ex.h:184
static VOID InitializeNamedEntry(PTEST_RESOURCES Resource, PIMAGE_RESOURCE_DIRECTORY_ENTRY DirEntry, PWCHAR Name, PVOID Data)
IMAGE_RESOURCE_DIRECTORY_ENTRY Lang3Entries[2]
IMAGE_RESOURCE_DIRECTORY TypeDirectory
Definition: pedump.c:413
static void Test_Data(PTEST_IMAGE TestImage)
ULONG DataIsDirectory
Definition: ntimage.h:195
unsigned short USHORT
Definition: pedump.c:61
DWORD PointerToSymbolTable
Definition: ntddk_ex.h:125
#define FIELD_OFFSET(t, f)
Definition: typedefs.h:255
#define NULL
Definition: types.h:112
IMAGE_SECTION_HEADER SectionHeaders[1]
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
BYTE Name[IMAGE_SIZEOF_SHORT_NAME]
Definition: pedump.c:281
ULONG_PTR Language
Definition: ldrtypes.h:183
IMAGE_RESOURCE_DIRECTORY_ENTRY Name2Entries[2]
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
unsigned int ULONG
Definition: retypes.h:1
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:40
#define STATUS_SUCCESS
Definition: shellext.h:65
#define IMAGE_DIRECTORY_ENTRY_RESOURCE
Definition: pedump.c:261
#define memset(x, y, z)
Definition: compat.h:39
#define ok_dec(expression, result)
Definition: atltest.h:101
WCHAR StringBuffer[156]
Definition: ldrinit.c:41
DWORD PointerToRelocations
Definition: pedump.c:291
IMAGE_RESOURCE_DIRECTORY_ENTRY Lang4Entries[2]
DWORD Reserved
Definition: pedump.c:462
IMAGE_RESOURCE_DIRECTORY_ENTRY Lang2Entries[2]
DWORD Size
Definition: pedump.c:460