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
13 #include <sys/types.h>
24 /* #define DEBUG_RESOURCE */
30 find_lang(char *root
, struct PE_Resource_Directory
*resource
, RESOURCE
*r
)
32 struct PE_Directory_Entry
*type_dir
;
33 struct PE_Resource_Leaf_Entry
*leaf
;
35 type_dir
= (struct PE_Directory_Entry
*)(resource
+ 1);
36 type_dir
+= resource
->NumberOfNamedEntries
;
38 /* grab the 1st resource available */
39 leaf
= (struct PE_Resource_Leaf_Entry
*) (root
+ type_dir
->OffsetToData
);
40 dprintf_resource(stddeb
, "\t\tPE_findlang: id %8x\n", (int) type_dir
->Name
);
41 dprintf_resource(stddeb
, "\t\taddress %ld, size %ld, language id %ld\n", leaf
->OffsetToData
, leaf
->Size
, leaf
->CodePage
);
42 r
->offset
= leaf
->OffsetToData
- r
->wpnt
->pe
->resource_offset
;
44 printf("\t\toffset %d, size %d\n", r
->offset
, r
->size
);
47 /* for(i=0; i< resource->NumberOfIdEntries; i++) {
48 leaf = (root + (type_dir->OffsetToData & ~IMAGE_RESOURCE_DATA_IS_DIRECTORY));
49 dprintf_resource(stddeb, "\t\tPE_findlang: id %8x\n",
50 (int) type_dir->Name);
51 dprintf_resource(stddeb, "\t\t%x %x %x\n", leaf->OffsetToData,
52 leaf->Size, leaf->CodePage);
58 find_resource(char *root
, struct PE_Resource_Directory
*resource
,
59 LPSTR resource_name
, RESOURCE
*r
)
63 struct PE_Directory_Entry
*type_dir
;
64 struct PE_Directory_Name_String_U
*name
;
66 type_dir
= (struct PE_Directory_Entry
*)(resource
+ 1);
68 if (HIWORD((DWORD
)resource_name
)) {
69 for(i
=0; i
< resource
->NumberOfNamedEntries
; i
++) {
70 name
= (struct PE_Directory_Name_String_U
*)(root
+ (type_dir
->Name
& ~IMAGE_RESOURCE_NAME_IS_STRING
));
71 memset(res_name
, 0, sizeof(res_name
));
72 my_wcstombs(res_name
, name
->NameString
, name
->Length
);
73 dprintf_resource(stddeb
, "\tPE_findresource: name %s\n", res_name
);
74 if (lstrcmpi(res_name
, resource_name
) == 0)
75 return find_lang(root
, (struct PE_Resource_Directory
*) (root
+ (type_dir
->OffsetToData
& ~IMAGE_RESOURCE_DATA_IS_DIRECTORY
)), r
);
79 type_dir
+= resource
->NumberOfNamedEntries
;
80 for(i
=0; i
< resource
->NumberOfIdEntries
; i
++) {
81 dprintf_resource(stddeb
, "\tPE_findresource: name %8x\n", (int) type_dir
->Name
);
82 if (type_dir
->Name
== ((int) resource_name
& 0xff))
83 return find_lang(root
, (struct PE_Resource_Directory
*) (root
+ (type_dir
->OffsetToData
& ~IMAGE_RESOURCE_DATA_IS_DIRECTORY
)), r
);
91 find_type(struct PE_Resource_Directory
*resource
, LPSTR resource_name
,
95 char *root
, res_name
[256];
96 struct PE_Directory_Entry
*type_dir
;
97 struct PE_Directory_Name_String_U
*name
;
99 root
= (char *) resource
;
100 type_dir
= (struct PE_Directory_Entry
*)(resource
+ 1);
102 if (HIWORD((DWORD
)type_name
)) {
103 for(i
=0; i
< resource
->NumberOfNamedEntries
; i
++) {
104 name
= (struct PE_Directory_Name_String_U
*)(root
+ (type_dir
->Name
& ~IMAGE_RESOURCE_NAME_IS_STRING
));
105 memset(res_name
, 0, sizeof(res_name
));
106 my_wcstombs(res_name
, name
->NameString
, name
->Length
);
107 dprintf_resource(stddeb
, "PE_findtype: type %s\n",
109 if (lstrcmpi(res_name
, type_name
) == 0)
110 return find_resource(root
, (struct PE_Resource_Directory
*) (root
+ (type_dir
->OffsetToData
& ~IMAGE_RESOURCE_DATA_IS_DIRECTORY
)), resource_name
, r
);
114 type_dir
+= resource
->NumberOfNamedEntries
;
115 for(i
=0; i
< resource
->NumberOfIdEntries
; i
++) {
116 dprintf_resource(stddeb
, "PE_findtype: type %8x\n", (int) type_dir
->Name
);
117 if (type_dir
->Name
== ((int) type_name
& 0xff))
118 return find_resource(root
, (struct PE_Resource_Directory
*) (root
+ (type_dir
->OffsetToData
& ~IMAGE_RESOURCE_DATA_IS_DIRECTORY
)), resource_name
, r
);
125 /**********************************************************************
126 * PE_FindResource [KERNEL.60]
129 PE_FindResource(HANDLE instance
, SEGPTR resource_name
, SEGPTR type_name
,
132 dprintf_resource(stddeb
, "PE_FindResource hInst=%04X typename=%08X resname=%08X\n",
133 instance
, (int) type_name
, (int) resource_name
);
134 if (HIWORD(resource_name
))
136 char *resource_name_ptr
= PTR_SEG_TO_LIN( resource_name
);
137 if (resource_name_ptr
[0] == '#')
138 resource_name
= (SEGPTR
) atoi(resource_name_ptr
+ 1);
140 resource_name
= (SEGPTR
)resource_name_ptr
;
142 if (HIWORD(type_name
))
144 char *type_name_ptr
= PTR_SEG_TO_LIN( type_name
);
145 if (type_name_ptr
[0] == '#')
146 type_name
= (SEGPTR
) atoi(type_name_ptr
+ 1);
148 type_name
= (SEGPTR
) type_name_ptr
;
150 return find_type(r
->wpnt
->pe
->pe_resource
, resource_name
, type_name
);