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
12 #include <sys/types.h>
22 #define DEBUG_RESOURCE
25 find_lang(char *root
, struct PE_Resource_Directory
*resource
, RESOURCE
*r
)
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
);
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
);
40 r
->offset
= leaf
->OffsetToData
- r
->wpnt
->pe
->resource_offset
;
42 printf("\t\toffset %d, size %d\n", r
->offset
, r
->size
);
45 /* for(i=0; i< resource->NumberOfIdEntries; i++) {
46 leaf = (root + (type_dir->OffsetToData & ~IMAGE_RESOURCE_DATA_IS_DIRECTORY));
48 printf("\t\tPE_findlang: id %8x\n", (int) type_dir->Name);
49 printf("\t\t%x %x %x\n", leaf->OffsetToData, leaf->Size,
57 find_resource(char *root
, struct PE_Resource_Directory
*resource
,
58 LPSTR resource_name
, RESOURCE
*r
)
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
);
73 printf("\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
);
80 type_dir
+= resource
->NumberOfNamedEntries
;
81 for(i
=0; i
< resource
->NumberOfIdEntries
; i
++) {
83 printf("\tPE_findresource: name %8x\n", (int) type_dir
->Name
);
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
);
94 find_type(struct PE_Resource_Directory
*resource
, LPSTR resource_name
,
95 LPSTR type_name
, RESOURCE
*r
)
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
);
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
);
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
);
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
);
131 /**********************************************************************
132 * PE_FindResource [KERNEL.60]
135 PE_FindResource(HANDLE instance
, LPSTR resource_name
, LPSTR type_name
,
138 #ifdef DEBUG_RESOURCE
139 printf("PE_FindResource hInst=%04X typename=%08X resname=%08X\n",
140 instance
, (int) type_name
, (int) resource_name
);
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
);