2 static char RCSId[] = "$Id: ne_resource.c,v 1.4 1993/07/04 04:04:21 root Exp root $";
3 static char Copyright[] = "Copyright Robert J. Amstadt, 1993";
23 /**********************************************************************
26 static void NE_LoadNameTable(struct w_files
*wpnt
)
28 struct resource_typeinfo_s typeinfo
;
29 struct resource_nameinfo_s nameinfo
;
30 unsigned short size_shift
;
40 * Move to beginning of resource table.
42 lseek(wpnt
->fd
, wpnt
->mz_header
->ne_offset
+
43 wpnt
->ne
->ne_header
->resource_tab_offset
, SEEK_SET
);
48 if (read(wpnt
->fd
, &size_shift
, sizeof(size_shift
)) != sizeof(size_shift
))
51 size_shift
= CONV_SHORT(size_shift
);
56 typeinfo
.type_id
= 0xffff;
57 while (typeinfo
.type_id
!= 0)
59 if (read(wpnt
->fd
, &typeinfo
, sizeof(typeinfo
)) != sizeof(typeinfo
))
62 if (typeinfo
.type_id
== 0)
65 if (typeinfo
.type_id
== 0x800f)
67 for (i
= 0; i
< typeinfo
.count
; i
++)
69 if (read(wpnt
->fd
, &nameinfo
, sizeof(nameinfo
)) != sizeof(nameinfo
))
72 saved_pos
= lseek(wpnt
->fd
, 0, SEEK_CUR
);
73 lseek(wpnt
->fd
, (long) nameinfo
.offset
<< size_shift
,
75 read(wpnt
->fd
, &len
, sizeof(len
));
78 new = (RESNAMTAB
*) GlobalQuickAlloc(sizeof(*new));
82 read(wpnt
->fd
, &new->type_ord
, 2);
83 read(wpnt
->fd
, &new->id_ord
, 2);
84 read(wpnt
->fd
, read_buf
, len
- 6);
86 p
= read_buf
+ strlen(read_buf
) + 1;
87 strncpy(new->id
, p
, MAX_NAME_LENGTH
);
88 new->id
[MAX_NAME_LENGTH
- 1] = '\0';
90 read(wpnt
->fd
, &len
, sizeof(len
));
92 lseek(wpnt
->fd
, saved_pos
, SEEK_SET
);
95 lseek(wpnt
->fd
, (typeinfo
.count
* sizeof(nameinfo
)), SEEK_CUR
);
97 wpnt
->ne
->resnamtab
= top
;
100 static int type_match(int type_id1
, int type_id2
, int fd
, off_t off
)
109 if ((type_id1
& 0xffff0000) == 0) {
110 if ((type_id2
& 0x8000) == 0)
112 return (type_id1
& 0x000f) == (type_id2
& 0x000f);
114 if ((type_id2
& 0x8000) != 0)
116 dprintf_resource(stddeb
, "type_compare: type_id2=%04X !\n", type_id2
);
118 old_pos
= lseek(fd
, 0, SEEK_CUR
);
119 lseek(fd
, off
+ type_id2
, SEEK_SET
);
121 nbytes
= CONV_CHAR_TO_LONG(c
);
123 dprintf_resource(stddeb
, "type_compare: namesize=%d\n", nbytes
);
124 read(fd
, name
, nbytes
);
125 lseek(fd
, old_pos
, SEEK_SET
);
127 dprintf_resource(stddeb
, "type_compare: name=`%s'\n", name
);
128 return strcasecmp((char *) type_id1
, name
) == 0;
131 /**********************************************************************
132 * FindResourceByNumber
134 static int FindResourceByNumber(RESOURCE
*r
, int type_id
, int resource_id
)
136 struct resource_typeinfo_s typeinfo
;
137 struct resource_nameinfo_s nameinfo
;
138 unsigned short size_shift
;
142 dprintf_resource(stddeb
, "FindResourceByNumber: type_id =%x,m res_id = %x\n",
143 type_id
, resource_id
);
145 /* Move to beginning of resource table */
146 rtoff
= (r
->wpnt
->mz_header
->ne_offset
+
147 r
->wpnt
->ne
->ne_header
->resource_tab_offset
);
148 lseek(r
->wpnt
->fd
, rtoff
, SEEK_SET
);
150 /* Read block size */
151 if (read(r
->wpnt
->fd
, &size_shift
, sizeof(size_shift
)) != sizeof(size_shift
)) {
152 printf("FindResourceByNumber (%d) bad block size !\n",(int) resource_id
);
155 size_shift
= CONV_SHORT(size_shift
);
159 if (read(r
->wpnt
->fd
, &typeinfo
, sizeof(typeinfo
)) != sizeof(typeinfo
)) {
160 printf("FindResourceByNumber (%X) bad typeinfo size !\n", resource_id
);
163 dprintf_resource(stddeb
, "FindResourceByNumber type=%X count=%d ?=%ld searched=%08X\n",
164 typeinfo
.type_id
, typeinfo
.count
, typeinfo
.reserved
, type_id
);
165 if (typeinfo
.type_id
== 0)
167 if (type_match(type_id
, typeinfo
.type_id
, r
->wpnt
->fd
, rtoff
)) {
169 for (i
= 0; i
< typeinfo
.count
; i
++) {
171 if (read(r
->wpnt
->fd
, &nameinfo
, sizeof(nameinfo
)) != sizeof(nameinfo
))
173 if (!load_nameinfo(r
->wpnt
->fd
, &nameinfo
))
176 printf("FindResourceByNumber (%X) bad nameinfo size !\n", resource_id
);
179 dprintf_resource(stddeb
, "FindResource: search type=%X id=%X // type=%X id=%X\n",
180 type_id
, resource_id
, typeinfo
.type_id
, nameinfo
.id
);
181 if (nameinfo
.id
== resource_id
) {
182 r
->size
= nameinfo
.length
<< size_shift
;
183 r
->offset
= nameinfo
.offset
<< size_shift
;
189 lseek(r
->wpnt
->fd
, (typeinfo
.count
* sizeof(nameinfo
)), SEEK_CUR
);
194 /**********************************************************************
197 static int FindResourceByName(RESOURCE
*r
, int type_id
, char *resource_name
)
199 struct resource_typeinfo_s typeinfo
;
200 struct resource_nameinfo_s nameinfo
;
201 unsigned short size_shift
;
202 off_t old_pos
, new_pos
;
203 unsigned char nbytes
;
208 /* Check for loaded name table */
209 if (r
->wpnt
->ne
->resnamtab
!= NULL
) {
212 for (e
= r
->wpnt
->ne
->resnamtab
; e
!= NULL
; e
= e
->next
)
213 if (e
->type_ord
== (type_id
& 0x000f) &&
214 strcasecmp(e
->id
, resource_name
) == 0)
216 return FindResourceByNumber(r
, type_id
, e
->id_ord
);
221 /* Move to beginning of resource table */
222 rtoff
= (r
->wpnt
->mz_header
->ne_offset
+
223 r
->wpnt
->ne
->ne_header
->resource_tab_offset
);
224 lseek(r
->wpnt
->fd
, rtoff
, SEEK_SET
);
226 /* Read block size */
227 if (read(r
->wpnt
->fd
, &size_shift
, sizeof(size_shift
)) != sizeof(size_shift
))
229 printf("FindResourceByName (%s) bad block size !\n", resource_name
);
232 size_shift
= CONV_SHORT (size_shift
);
237 if (read(r
->wpnt
->fd
, &typeinfo
, sizeof(typeinfo
)) != sizeof(typeinfo
)) {
238 printf("FindResourceByName (%s) bad typeinfo size !\n", resource_name
);
241 dprintf_resource(stddeb
, "FindResourceByName typeinfo.type_id=%X count=%d type_id=%X\n",
242 typeinfo
.type_id
, typeinfo
.count
, type_id
);
243 if (typeinfo
.type_id
== 0)
245 if (type_match(type_id
, typeinfo
.type_id
, r
->wpnt
->fd
, rtoff
))
247 for (i
= 0; i
< typeinfo
.count
; i
++)
250 if (read(r
->wpnt
->fd
, &nameinfo
, sizeof(nameinfo
)) != sizeof(nameinfo
))
252 if (!load_nameinfo (r
->wpnt
->fd
, &nameinfo
))
255 printf("FindResourceByName (%s) bad nameinfo size !\n", resource_name
);
259 if ((nameinfo.id & 0x8000) != 0) continue;
261 dprintf_resource(stddeb
, "FindResourceByName // nameinfo.id=%04X !\n", nameinfo
.id
);
262 old_pos
= lseek(r
->wpnt
->fd
, 0, SEEK_CUR
);
263 new_pos
= rtoff
+ nameinfo
.id
;
264 lseek(r
->wpnt
->fd
, new_pos
, SEEK_SET
);
265 read(r
->wpnt
->fd
, &nbytes
, 1);
266 dprintf_resource(stddeb
, "FindResourceByName // namesize=%d !\n", nbytes
);
267 nbytes
= CONV_CHAR_TO_LONG (nbytes
);
268 read(r
->wpnt
->fd
, name
, nbytes
);
269 lseek(r
->wpnt
->fd
, old_pos
, SEEK_SET
);
271 dprintf_resource(stddeb
, "FindResourceByName type_id=%X (%d of %d) name='%s' resource_name='%s'\n",
272 typeinfo
.type_id
, i
+ 1, typeinfo
.count
,
273 name
, resource_name
);
274 if (strcasecmp(name
, resource_name
) == 0) {
275 r
->size
= nameinfo
.length
<< size_shift
;
276 r
->offset
= nameinfo
.offset
<< size_shift
;
282 lseek(r
->wpnt
->fd
, (typeinfo
.count
* sizeof(nameinfo
)), SEEK_CUR
);
288 /**********************************************************************
289 * GetRsrcCount [internal]
291 int GetRsrcCount(HINSTANCE hInst
, int type_id
)
293 struct w_files
*wpnt
;
294 struct resource_typeinfo_s typeinfo
;
295 struct resource_nameinfo_s nameinfo
;
296 unsigned short size_shift
;
301 dprintf_resource(stddeb
, "GetRsrcCount hInst=%04X typename=%08X\n",
304 if ((wpnt
= GetFileInfo(hInst
)) == NULL
)
307 * Move to beginning of resource table.
309 rtoff
= (wpnt
->mz_header
->ne_offset
+
310 wpnt
->ne
->ne_header
->resource_tab_offset
);
311 lseek(wpnt
->fd
, rtoff
, SEEK_SET
);
315 if (read(wpnt
->fd
, &size_shift
, sizeof(size_shift
)) != sizeof(size_shift
)) {
316 printf("GetRsrcCount // bad block size !\n");
319 size_shift
= CONV_SHORT (size_shift
);
321 if (read(wpnt
->fd
, &typeinfo
, sizeof(typeinfo
)) != sizeof(typeinfo
)) {
322 printf("GetRsrcCount // bad typeinfo size !\n");
325 dprintf_resource(stddeb
, "GetRsrcCount // typeinfo.type_id=%X count=%d type_id=%X\n",
326 typeinfo
.type_id
, typeinfo
.count
, type_id
);
327 if (typeinfo
.type_id
== 0)
329 if (type_match(type_id
, typeinfo
.type_id
, wpnt
->fd
, rtoff
))
330 return typeinfo
.count
;
332 lseek(wpnt
->fd
, (typeinfo
.count
* sizeof(nameinfo
)), SEEK_CUR
);
337 /**********************************************************************
338 * NE_FindResource [KERNEL.60]
340 int NE_FindResource(HANDLE instance
, LPSTR resource_name
, LPSTR type_name
,
345 dprintf_resource(stddeb
, "NE_FindResource hInst=%04X typename=%p resname=%p\n",
346 instance
, type_name
, resource_name
);
348 r
->size
= r
->offset
= 0;
350 /* nametable loaded ? */
351 if (r
->wpnt
->ne
->resnamtab
== NULL
)
352 NE_LoadNameTable(r
->wpnt
);
354 if (((int) type_name
& 0xffff0000) == 0)
355 type
= (int) type_name
;
357 if (type_name
[0] == '\0')
359 if (type_name
[0] == '#')
360 type
= atoi(type_name
+ 1);
362 type
= (int) type_name
;
365 if (((int) resource_name
& 0xffff0000) == 0)
366 x
= FindResourceByNumber(r
, type
, (int) resource_name
| 0x8000);
368 if (resource_name
[0] == '\0')
369 x
= FindResourceByNumber(r
, type
, -1);
370 if (resource_name
[0] == '#')
371 x
= FindResourceByNumber(r
, type
, atoi(resource_name
+ 1));
373 x
= FindResourceByName(r
, type
, resource_name
);
376 printf("NE_FindResource hInst=%04X typename=%08X resname=%08X not found!\n",
377 instance
, (int) type_name
, (int) resource_name
);