Release 951226
[wine/gsoc-2012-control.git] / loader / pe_resource.c
blobb9a356d187871bafc0b86bdb4d855c662017720a
1 #ifndef WINELIB
2 /*
3 * (c) 1994 Erik Bos <erik@xs4all.nl>
5 * based on Eric Youndale's pe-test and:
7 * ftp.microsoft.com:/pub/developer/MSDN/CD8/PEFILE.ZIP
8 */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <fcntl.h>
16 #include <unistd.h>
17 #include "windows.h"
18 #include "ldt.h"
19 #include "neexe.h"
20 #include "peexe.h"
21 #include "dlls.h"
22 #include "pe_image.h"
23 #include "resource.h"
24 #include "stddebug.h"
25 /* #define DEBUG_RESOURCE */
26 #include "debug.h"
28 #if 0
30 static int
31 find_lang(char *root, struct PE_Resource_Directory *resource, RESOURCE *r)
33 struct PE_Directory_Entry *type_dir;
34 struct PE_Resource_Leaf_Entry *leaf;
36 type_dir = (struct PE_Directory_Entry *)(resource + 1);
37 type_dir += resource->NumberOfNamedEntries;
39 /* grab the 1st resource available */
40 leaf = (struct PE_Resource_Leaf_Entry *) (root + type_dir->OffsetToData);
41 dprintf_resource(stddeb, "\t\tPE_findlang: id %8x\n", (int) type_dir->Name);
42 dprintf_resource(stddeb, "\t\taddress %ld, size %ld, language id %ld\n", leaf->OffsetToData, leaf->Size, leaf->CodePage);
43 r->offset = leaf->OffsetToData - r->wpnt->pe->resource_offset;
44 r->size = leaf->Size;
45 printf("\t\toffset %d, size %d\n", r->offset, r->size);
46 return 1;
48 /* for(i=0; i< resource->NumberOfIdEntries; i++) {
49 leaf = (root + (type_dir->OffsetToData & ~IMAGE_RESOURCE_DATA_IS_DIRECTORY));
50 dprintf_resource(stddeb, "\t\tPE_findlang: id %8x\n",
51 (int) type_dir->Name);
52 dprintf_resource(stddeb, "\t\t%x %x %x\n", leaf->OffsetToData,
53 leaf->Size, leaf->CodePage);
54 type_dir++;
55 } */
58 static int
59 find_resource(char *root, struct PE_Resource_Directory *resource,
60 LPSTR resource_name, RESOURCE *r)
62 int i;
63 char res_name[256];
64 struct PE_Directory_Entry *type_dir;
65 struct PE_Directory_Name_String_U *name;
67 type_dir = (struct PE_Directory_Entry *)(resource + 1);
69 if (HIWORD((DWORD)resource_name)) {
70 for(i=0; i< resource->NumberOfNamedEntries; i++) {
71 name = (struct PE_Directory_Name_String_U *)(root + (type_dir->Name & ~IMAGE_RESOURCE_NAME_IS_STRING));
72 memset(res_name, 0, sizeof(res_name));
73 my_wcstombs(res_name, name->NameString, name->Length);
74 dprintf_resource(stddeb, "\tPE_findresource: name %s\n", res_name);
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 dprintf_resource(stddeb, "\tPE_findresource: name %8x\n", (int) type_dir->Name);
83 if (type_dir->Name == ((int) resource_name & 0xff))
84 return find_lang(root, (struct PE_Resource_Directory *) (root + (type_dir->OffsetToData & ~IMAGE_RESOURCE_DATA_IS_DIRECTORY)), r);
85 type_dir++;
88 return 0;
91 static int
92 find_type(struct PE_Resource_Directory *resource, LPSTR resource_name,
93 LPSTR type_name)
95 int i;
96 char *root, res_name[256];
97 struct PE_Directory_Entry *type_dir;
98 struct PE_Directory_Name_String_U *name;
100 root = (char *) resource;
101 type_dir = (struct PE_Directory_Entry *)(resource + 1);
103 if (HIWORD((DWORD)type_name)) {
104 for(i=0; i< resource->NumberOfNamedEntries; i++) {
105 name = (struct PE_Directory_Name_String_U *)(root + (type_dir->Name & ~IMAGE_RESOURCE_NAME_IS_STRING));
106 memset(res_name, 0, sizeof(res_name));
107 my_wcstombs(res_name, name->NameString, name->Length);
108 dprintf_resource(stddeb, "PE_findtype: type %s\n",
109 res_name);
110 if (strcasecmp(res_name, type_name) == 0)
111 return find_resource(root, (struct PE_Resource_Directory *) (root + (type_dir->OffsetToData & ~IMAGE_RESOURCE_DATA_IS_DIRECTORY)), resource_name, r);
112 type_dir++;
114 } else {
115 type_dir += resource->NumberOfNamedEntries;
116 for(i=0; i< resource->NumberOfIdEntries; i++) {
117 dprintf_resource(stddeb, "PE_findtype: type %8x\n", (int) type_dir->Name);
118 if (type_dir->Name == ((int) type_name & 0xff))
119 return find_resource(root, (struct PE_Resource_Directory *) (root + (type_dir->OffsetToData & ~IMAGE_RESOURCE_DATA_IS_DIRECTORY)), resource_name, r);
120 type_dir++;
123 return 0;
126 /**********************************************************************
127 * PE_FindResource [KERNEL.60]
130 PE_FindResource(HANDLE instance, SEGPTR resource_name, SEGPTR type_name,
131 RESOURCE *r)
133 dprintf_resource(stddeb, "PE_FindResource hInst=%04X typename=%08X resname=%08X\n",
134 instance, (int) type_name, (int) resource_name);
135 if (HIWORD(resource_name))
137 char *resource_name_ptr = PTR_SEG_TO_LIN( resource_name );
138 if (resource_name_ptr[0] == '#')
139 resource_name = (SEGPTR) atoi(resource_name_ptr + 1);
140 else
141 resource_name = (SEGPTR)resource_name_ptr;
143 if (HIWORD(type_name))
145 char *type_name_ptr = PTR_SEG_TO_LIN( type_name );
146 if (type_name_ptr[0] == '#')
147 type_name = (SEGPTR) atoi(type_name_ptr + 1);
148 else
149 type_name = (SEGPTR) type_name_ptr;
151 return find_type(r->wpnt->pe->pe_resource, resource_name, type_name);
153 #endif
155 #endif /* WINELIB */