update for HEAD-2003021201
[reactos.git] / lib / ntdll / ldr / res.c
1 /* $Id$
2  * 
3  * COPYRIGHT:       See COPYING in the top level directory
4  * PROJECT:         ReactOS kernel
5  * FILE:            lib/ntdll/ldr/res.c
6  * PURPOSE:         Resource access for PE executables
7  * PROGRAMMERS:     Jean Michault
8  *                  Rex Jolliff (rex@lvcablemodem.com)
9  *                  Robert Dickenson (robd@mok.lvcm.com)
10  */
11
12 /*
13  * TODO:
14  *  - any comments ??
15  */
16
17 /* INCLUDES *****************************************************************/
18
19 #include <reactos/config.h>
20 #include <ddk/ntddk.h>
21 #include <windows.h>
22 #include <string.h>
23 #include <wchar.h>
24 #include <ntdll/ldr.h>
25 #include <ntos/minmax.h>
26
27 #define NDEBUG
28 #include <ntdll/ntdll.h>
29
30 /* PROTOTYPES ****************************************************************/
31
32
33
34 /* FUNCTIONS *****************************************************************/
35
36 /*
37         Status = LdrFindResource_U (hModule,
38                                     &ResourceInfo,
39                                     RESOURCE_DATA_LEVEL,
40                                     &ResourceDataEntry);
41  */
42 NTSTATUS STDCALL
43 LdrFindResource_U(PVOID BaseAddress,
44                   PLDR_RESOURCE_INFO ResourceInfo,
45                   ULONG Level,
46                   PIMAGE_RESOURCE_DATA_ENTRY* ResourceDataEntry)
47 {
48     PIMAGE_RESOURCE_DIRECTORY ResDir;
49     PIMAGE_RESOURCE_DIRECTORY ResBase;
50     PIMAGE_RESOURCE_DIRECTORY_ENTRY ResEntry;
51     NTSTATUS Status = STATUS_SUCCESS;
52     ULONG EntryCount;
53     PWCHAR ws;
54     ULONG i;
55     ULONG Id;
56
57     //DPRINT("LdrFindResource_U()\n");
58     DPRINT("LdrFindResource_U(%08x, %08x, %d, %08x)\n", BaseAddress, ResourceInfo, Level, ResourceDataEntry);
59
60     /* Get the pointer to the resource directory */
61     ResDir = (PIMAGE_RESOURCE_DIRECTORY)RtlImageDirectoryEntryToData(BaseAddress,
62                       TRUE, IMAGE_DIRECTORY_ENTRY_RESOURCE, &i);
63     if (ResDir == NULL) {
64         return STATUS_RESOURCE_DATA_NOT_FOUND;
65     }
66
67     DPRINT("ResourceDirectory: %x  Size: %d\n", (ULONG)ResDir, (int)i);
68
69     ResBase = ResDir;
70
71     /* Let's go into resource tree */
72     for (i = 0; i < Level; i++) {
73         DPRINT("ResDir: %x  Level: %d\n", (ULONG)ResDir, i);
74
75         Id = ((PULONG)ResourceInfo)[i];
76 //      ResourceInfo.Type = (ULONG)lpType;
77 //      ResourceInfo.Name = (ULONG)lpName;
78 //      ResourceInfo.Language = (ULONG)wLanguage;
79
80         EntryCount = ResDir->NumberOfNamedEntries;
81         DPRINT("    Id: %d  NumberOfNamedEntries: %d\n", Id, EntryCount);
82         ResEntry = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(ResDir + 1);
83         //DPRINT("ResEntry %x\n", (ULONG)ResEntry);
84         if (Id & 0xFFFF0000) {
85             /* Resource name is a unicode string */
86             DPRINT("ResEntry %x - Resource name is a unicode string\n", (ULONG)ResEntry);
87             DPRINT("EntryCount %d\n", (ULONG)EntryCount);
88             for (; EntryCount--; ResEntry++) {
89                 /* Scan entries for equal name */
90                 if (ResEntry->Name & 0x80000000) {
91                     ws = (PWCHAR)((ULONG)ResDir + (ResEntry->Name & 0x7FFFFFFF));
92                     if (!wcsncmp((PWCHAR)Id, ws + 1, *ws) &&
93                           wcslen((PWCHAR)Id) == (int)*ws) {
94                         goto found;
95                     }
96                 }
97             }
98         } else {
99             /* We use ID number instead of string */
100             DPRINT("ResEntry %x - Resource ID number instead of string\n", (ULONG)ResEntry);
101             DPRINT("EntryCount %d\n", (ULONG)EntryCount);
102             ResEntry += EntryCount;
103             EntryCount = ResDir->NumberOfIdEntries;
104             DPRINT("EntryCount %d\n", (ULONG)EntryCount);
105             for (; EntryCount--; ResEntry++) {
106                 /* Scan entries for equal name */
107                 DPRINT("EntryCount %d  ResEntry %x\n", (ULONG)EntryCount, ResEntry);
108                 DPRINT("ResEntry->Name %x  Id %x\n", (ULONG)ResEntry->Name, Id);
109                 if (ResEntry->Name == Id) {
110                     DPRINT("ID entry found %x\n", Id);
111                     goto found;
112                 }
113             }
114         }
115
116         //DPRINT("Error %lu\n", i);
117
118         switch (i) {
119         case 0:
120             DPRINT("Error %lu - STATUS_RESOURCE_TYPE_NOT_FOUND\n", i);
121             return STATUS_RESOURCE_TYPE_NOT_FOUND;
122         case 1:
123             DPRINT("Error %lu - STATUS_RESOURCE_NAME_NOT_FOUND\n", i);
124             return STATUS_RESOURCE_NAME_NOT_FOUND;
125         case 2:
126             if (ResDir->NumberOfNamedEntries || ResDir->NumberOfIdEntries) {
127                 /* Use the first available language */
128                 ResEntry = (IMAGE_RESOURCE_DIRECTORY_ENTRY*)(ResDir + 1);
129                 break;
130             }
131             DPRINT("Error %lu - STATUS_RESOURCE_LANG_NOT_FOUND\n", i);
132             return STATUS_RESOURCE_LANG_NOT_FOUND;
133          case 3:
134             DPRINT("Error %lu - STATUS_RESOURCE_DATA_NOT_FOUND\n", i);
135             return STATUS_RESOURCE_DATA_NOT_FOUND;
136          default:
137             DPRINT("Error %lu - STATUS_INVALID_PARAMETER\n", i);
138             return STATUS_INVALID_PARAMETER;
139         }
140 found:;
141         ResDir = (PIMAGE_RESOURCE_DIRECTORY)((ULONG)ResBase +
142                      (ResEntry->OffsetToData & 0x7FFFFFFF));
143     }
144     DPRINT("ResourceDataEntry: %x\n", (ULONG)ResDir);
145
146     if (ResourceDataEntry) {
147         *ResourceDataEntry = (PVOID)ResDir;
148     }
149     return Status;
150 }
151
152
153 NTSTATUS STDCALL
154 LdrAccessResource(IN  PVOID BaseAddress,
155                   IN  PIMAGE_RESOURCE_DATA_ENTRY ResourceDataEntry,
156                   OUT PVOID* Resource OPTIONAL,
157                   OUT PULONG Size OPTIONAL)
158 {
159     PIMAGE_SECTION_HEADER Section;
160     PIMAGE_NT_HEADERS NtHeader;
161     ULONG SectionRva;
162     ULONG SectionVa;
163     ULONG DataSize;
164     ULONG Offset = 0;
165     ULONG Data;
166
167     Data = (ULONG)RtlImageDirectoryEntryToData(BaseAddress,
168                            TRUE, IMAGE_DIRECTORY_ENTRY_RESOURCE, &DataSize);
169     if (Data == 0) {
170         return STATUS_RESOURCE_DATA_NOT_FOUND;
171     }
172     if ((ULONG)BaseAddress & 1) {
173         /* loaded as ordinary file */
174         NtHeader = RtlImageNtHeader((PVOID)((ULONG)BaseAddress & ~1UL));
175         Offset = (ULONG)BaseAddress - Data + NtHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress;
176         Section = RtlImageRvaToSection(NtHeader, BaseAddress, NtHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress);
177         if (Section == NULL) {
178             return STATUS_RESOURCE_DATA_NOT_FOUND;
179         }
180         if (Section->Misc.VirtualSize < ResourceDataEntry->OffsetToData) {
181             SectionRva = RtlImageRvaToSection (NtHeader, BaseAddress, ResourceDataEntry->OffsetToData)->VirtualAddress;
182             SectionVa = RtlImageRvaToVa(NtHeader, BaseAddress, SectionRva, NULL);
183             Offset = SectionRva - SectionVa + Data - Section->VirtualAddress;
184         }
185     }
186     if (Resource) {
187         *Resource = (PVOID)(ResourceDataEntry->OffsetToData - Offset + (ULONG)BaseAddress);
188     }
189     if (Size) {
190         *Size = ResourceDataEntry->Size;
191     }
192     return STATUS_SUCCESS;
193 }
194
195
196 NTSTATUS STDCALL
197 LdrFindResourceDirectory_U(IN PVOID BaseAddress,
198                            WCHAR** name,
199                            DWORD level,
200                            OUT PVOID* addr)
201 {
202     PIMAGE_RESOURCE_DIRECTORY ResDir;
203     PIMAGE_RESOURCE_DIRECTORY_ENTRY ResEntry;
204     ULONG EntryCount;
205     ULONG i;
206     NTSTATUS Status = STATUS_SUCCESS;
207     WCHAR* ws;
208
209     /* Get the pointer to the resource directory */
210     ResDir = (PIMAGE_RESOURCE_DIRECTORY)
211     RtlImageDirectoryEntryToData(BaseAddress, TRUE, IMAGE_DIRECTORY_ENTRY_RESOURCE, &i);
212     if (ResDir == NULL) {
213         return STATUS_RESOURCE_DATA_NOT_FOUND;
214     }
215
216     /* Let's go into resource tree */
217     for (i = 0; i < level; i++, name++) {
218         EntryCount = ResDir->NumberOfNamedEntries;
219         ResEntry = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(ResDir + 1);
220         if ((ULONG)(*name) & 0xFFFF0000) {
221             /* Resource name is a unicode string */
222             for (; EntryCount--; ResEntry++) {
223                 /* Scan entries for equal name */
224                 if (ResEntry->Name & 0x80000000) {
225                     ws = (WCHAR*)((ULONG)ResDir + (ResEntry->Name & 0x7FFFFFFF));
226                     if (!wcsncmp(*name, ws + 1, *ws) && wcslen(*name) == (int)*ws) {
227                         goto found;
228                     }
229                 }
230             }
231         } else {
232             /* We use ID number instead of string */
233             ResEntry += EntryCount;
234             EntryCount = ResDir->NumberOfIdEntries;
235             for (; EntryCount--; ResEntry++) {
236                 /* Scan entries for equal name */
237                 if (ResEntry->Name == (ULONG)(*name))
238                     goto found;
239             }
240         }
241         switch (i) {
242         case 0:
243             return STATUS_RESOURCE_TYPE_NOT_FOUND;
244         case 1:
245             return STATUS_RESOURCE_NAME_NOT_FOUND;
246         case 2:
247             Status = STATUS_RESOURCE_LANG_NOT_FOUND;
248             /* Just use first language entry */
249             if (ResDir->NumberOfNamedEntries || ResDir->NumberOfIdEntries) {
250                 ResEntry = (IMAGE_RESOURCE_DIRECTORY_ENTRY*)(ResDir + 1);
251                 break;
252             }
253             return Status;
254         case 3:
255             return STATUS_RESOURCE_DATA_NOT_FOUND;
256         default:
257             return STATUS_INVALID_PARAMETER;
258         }
259 found:;
260         ResDir = (PIMAGE_RESOURCE_DIRECTORY)((ULONG)ResDir + ResEntry->OffsetToData);
261     }
262     if (addr) {
263         *addr = (PVOID)ResDir;
264     }
265     return Status;
266 }
267
268 /* EOF */