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>
25 /* #define DEBUG_RESOURCE */
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
;
45 printf("\t\toffset %d, size %d\n", r
->offset
, r
->size
);
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);
59 find_resource(char *root
, struct PE_Resource_Directory
*resource
,
60 LPSTR resource_name
, RESOURCE
*r
)
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
);
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
);
92 find_type(struct PE_Resource_Directory
*resource
, LPSTR resource_name
,
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",
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
);
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
);
126 /**********************************************************************
127 * PE_FindResource [KERNEL.60]
130 PE_FindResource(HANDLE instance
, SEGPTR resource_name
, SEGPTR type_name
,
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);
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);
149 type_name
= (SEGPTR
) type_name_ptr
;
151 return find_type(r
->wpnt
->pe
->pe_resource
, resource_name
, type_name
);