Release 941017
[wine/gsoc-2012-control.git] / loader / pe_resource.c
blobd82789cdfe109d8e3dc37ad1dbaf0267d92a3626
1 /*
2 * (c) 1994 Erik Bos <erik@hacktic.nl>
4 * based on Eric Youndale's pe-test and:
6 * ftp.microsoft.com:/pub/developer/MSDN/CD8/PEFILE.ZIP
7 */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
15 #include <unistd.h>
16 #include "windows.h"
17 #include "neexe.h"
18 #include "peexe.h"
19 #include "dlls.h"
20 #include "resource.h"
22 #define DEBUG_RESOURCE
24 static int
25 find_lang(char *root, struct PE_Resource_Directory *resource, RESOURCE *r)
27 int i;
28 struct PE_Directory_Entry *type_dir;
29 struct PE_Resource_Leaf_Entry *leaf;
31 type_dir = (struct PE_Directory_Entry *)(resource + 1);
32 type_dir += resource->NumberOfNamedEntries;
34 /* grab the 1st resource available */
35 leaf = (struct PE_Resource_Leaf_Entry *) (root + type_dir->OffsetToData);
36 #ifdef DEBUG_RESOURCE
37 printf("\t\tPE_findlang: id %8x\n", (int) type_dir->Name);
38 printf("\t\taddress %d, size %d, language id %d\n", leaf->OffsetToData, leaf->Size, leaf->CodePage);
39 #endif
40 r->offset = leaf->OffsetToData - r->wpnt->pe->resource_offset;
41 r->size = leaf->Size;
42 printf("\t\toffset %d, size %d\n", r->offset, r->size);
43 return 1;
45 /* for(i=0; i< resource->NumberOfIdEntries; i++) {
46 leaf = (root + (type_dir->OffsetToData & ~IMAGE_RESOURCE_DATA_IS_DIRECTORY));
47 #ifdef DEBUG_RESOURCE
48 printf("\t\tPE_findlang: id %8x\n", (int) type_dir->Name);
49 printf("\t\t%x %x %x\n", leaf->OffsetToData, leaf->Size,
50 leaf->CodePage);
51 #endif
52 type_dir++;
53 } */
56 static int
57 find_resource(char *root, struct PE_Resource_Directory *resource,
58 LPSTR resource_name, RESOURCE *r)
60 int i;
61 char res_name[256];
62 struct PE_Directory_Entry *type_dir;
63 struct PE_Directory_Name_String_U *name;
65 type_dir = (struct PE_Directory_Entry *)(resource + 1);
67 if (HIWORD((DWORD)resource_name)) {
68 for(i=0; i< resource->NumberOfNamedEntries; i++) {
69 name = (struct PE_Directory_Name_String_U *)(root + (type_dir->Name & ~IMAGE_RESOURCE_NAME_IS_STRING));
70 memset(res_name, 0, sizeof(res_name));
71 my_wcstombs(res_name, name->NameString, name->Length);
72 #ifdef DEBUG_RESOURCE
73 printf("\tPE_findresource: name %s\n", res_name);
74 #endif
75 if (strcasecmp(res_name, resource_name) == 0)
76 return find_lang(root, (struct PE_Resource_Directory *) (root + (type_dir->OffsetToData & ~IMAGE_RESOURCE_DATA_IS_DIRECTORY)), r);
77 type_dir++;
79 } else {
80 type_dir += resource->NumberOfNamedEntries;
81 for(i=0; i< resource->NumberOfIdEntries; i++) {
82 #ifdef DEBUG_RESOURCE
83 printf("\tPE_findresource: name %8x\n", (int) type_dir->Name);
84 #endif
85 if (type_dir->Name == ((int) resource_name & 0xff))
86 return find_lang(root, (struct PE_Resource_Directory *) (root + (type_dir->OffsetToData & ~IMAGE_RESOURCE_DATA_IS_DIRECTORY)), r);
87 type_dir++;
90 return 0;
93 static int
94 find_type(struct PE_Resource_Directory *resource, LPSTR resource_name,
95 LPSTR type_name, RESOURCE *r)
97 int i;
98 char *root, res_name[256];
99 struct PE_Directory_Entry *type_dir;
100 struct PE_Directory_Name_String_U *name;
102 root = (char *) resource;
103 type_dir = (struct PE_Directory_Entry *)(resource + 1);
105 if (HIWORD((DWORD)type_name)) {
106 for(i=0; i< resource->NumberOfNamedEntries; i++) {
107 name = (struct PE_Directory_Name_String_U *)(root + (type_dir->Name & ~IMAGE_RESOURCE_NAME_IS_STRING));
108 memset(res_name, 0, sizeof(res_name));
109 my_wcstombs(res_name, name->NameString, name->Length);
110 #ifdef DEBUG_RESOURCE
111 printf("PE_findtype: type %s\n", res_name);
112 #endif
113 if (strcasecmp(res_name, type_name) == 0)
114 return find_resource(root, (struct PE_Resource_Directory *) (root + (type_dir->OffsetToData & ~IMAGE_RESOURCE_DATA_IS_DIRECTORY)), resource_name, r);
115 type_dir++;
117 } else {
118 type_dir += resource->NumberOfNamedEntries;
119 for(i=0; i< resource->NumberOfIdEntries; i++) {
120 #ifdef DEBUG_RESOURCE
121 printf("PE_findtype: type %8x\n", (int) type_dir->Name);
122 #endif
123 if (type_dir->Name == ((int) type_name & 0xff))
124 return find_resource(root, (struct PE_Resource_Directory *) (root + (type_dir->OffsetToData & ~IMAGE_RESOURCE_DATA_IS_DIRECTORY)), resource_name, r);
125 type_dir++;
128 return 0;
131 /**********************************************************************
132 * PE_FindResource [KERNEL.60]
135 PE_FindResource(HANDLE instance, LPSTR resource_name, LPSTR type_name,
136 RESOURCE *r)
138 #ifdef DEBUG_RESOURCE
139 printf("PE_FindResource hInst=%04X typename=%08X resname=%08X\n",
140 instance, (int) type_name, (int) resource_name);
141 #endif
142 if (HIWORD((DWORD)resource_name))
143 if (resource_name[0] == '#')
144 resource_name = (LPSTR) atoi(resource_name + 1);
146 if (HIWORD((DWORD)type_name))
147 if (type_name[0] == '#')
148 type_name = (LPSTR) atoi(type_name + 1);
150 return find_type(r->wpnt->pe->pe_resource, resource_name, type_name,r);