Fixed typo.
[wine] / loader / ne / resource.c
1 /*
2  * NE resource functions
3  *
4  * Copyright 1993 Robert J. Amstadt
5  * Copyright 1995 Alexandre Julliard
6  * Copyright 1997 Alex Korobka
7  */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
15 #include <unistd.h>
16 #include "windef.h"
17 #include "wine/winbase16.h"
18 #include "global.h"
19 #include "ldt.h"
20 #include "module.h"
21 #include "neexe.h"
22 #include "resource.h"
23 #include "callback.h"
24 #include "debug.h"
25
26 #define NEXT_TYPEINFO(pTypeInfo) ((NE_TYPEINFO *)((char*)((pTypeInfo) + 1) + \
27                                    (pTypeInfo)->count * sizeof(NE_NAMEINFO)))
28
29 /***********************************************************************
30  *           NE_FindNameTableId
31  *
32  * Find the type and resource id from their names.
33  * Return value is MAKELONG( typeId, resId ), or 0 if not found.
34  */
35 static DWORD NE_FindNameTableId( NE_MODULE *pModule, LPCSTR typeId, LPCSTR resId )
36 {
37     NE_TYPEINFO *pTypeInfo = (NE_TYPEINFO *)((char *)pModule + pModule->res_table + 2);
38     NE_NAMEINFO *pNameInfo;
39     HGLOBAL16 handle;
40     WORD *p;
41     DWORD ret = 0;
42     int count;
43
44     for (; pTypeInfo->type_id != 0;
45            pTypeInfo = (NE_TYPEINFO *)((char*)(pTypeInfo+1) +
46                                         pTypeInfo->count * sizeof(NE_NAMEINFO)))
47     {
48         if (pTypeInfo->type_id != 0x800f) continue;
49         pNameInfo = (NE_NAMEINFO *)(pTypeInfo + 1);
50         for (count = pTypeInfo->count; count > 0; count--, pNameInfo++)
51         {
52             TRACE(resource, "NameTable entry: type=%04x id=%04x\n",
53                               pTypeInfo->type_id, pNameInfo->id );
54             handle = LoadResource16( pModule->self, 
55                                    (HRSRC16)((int)pNameInfo - (int)pModule) );
56             for(p = (WORD*)LockResource16(handle); p && *p; p = (WORD *)((char*)p+*p))
57             {
58                 TRACE(resource,"  type=%04x '%s' id=%04x '%s'\n",
59                                   p[1], (char *)(p+3), p[2],
60                                   (char *)(p+3)+strlen((char *)(p+3))+1 );
61                 /* Check for correct type */
62
63                 if (p[1] & 0x8000)
64                 {
65                     if (!HIWORD(typeId)) continue;
66                     if (lstrcmpiA( typeId,
67                                      (char *)(p + 3) )) continue;
68                 }
69                 else if (HIWORD(typeId) || (((DWORD)typeId & ~0x8000)!= p[1]))
70                   continue;
71
72                 /* Now check for the id */
73
74                 if (p[2] & 0x8000)
75                 {
76                     if (!HIWORD(resId)) continue;
77                     if (lstrcmpiA( resId,
78                                (char*)(p+3)+strlen((char*)(p+3))+1 )) continue;
79                     
80                 }
81                 else if (HIWORD(resId) || (((DWORD)resId & ~0x8000) != p[2]))
82                   continue;
83
84                 /* If we get here, we've found the entry */
85
86                 TRACE(resource, "  Found!\n" );
87                 ret = MAKELONG( p[1], p[2] );
88                 break;
89             }
90             FreeResource16( handle );
91             if (ret) return ret;
92         }
93     }
94     return 0;
95 }
96
97 /***********************************************************************
98  *           NE_FindTypeSection
99  *
100  * Find header struct for a particular resource type.
101  */
102 NE_TYPEINFO *NE_FindTypeSection( LPBYTE pResTab, 
103                                  NE_TYPEINFO *pTypeInfo, LPCSTR typeId )
104 {
105     /* start from pTypeInfo */
106
107     if (HIWORD(typeId) != 0)  /* Named type */
108     {
109         LPCSTR str = typeId;
110         BYTE len = strlen( str );
111         while (pTypeInfo->type_id)
112         {
113             if (!(pTypeInfo->type_id & 0x8000))
114             {
115                 BYTE *p = pResTab + pTypeInfo->type_id;
116                 if ((*p == len) && !lstrncmpiA( p+1, str, len ))
117                 {
118                     TRACE(resource, "  Found type '%s'\n", str );
119                     return pTypeInfo;
120                 }
121             }
122             TRACE(resource, "  Skipping type %04x\n", pTypeInfo->type_id );
123             pTypeInfo = NEXT_TYPEINFO(pTypeInfo);
124         }
125     }
126     else  /* Numeric type id */
127     {
128         WORD id = LOWORD(typeId) | 0x8000;
129         while (pTypeInfo->type_id)
130         {
131             if (pTypeInfo->type_id == id)
132             {
133                 TRACE(resource, "  Found type %04x\n", id );
134                 return pTypeInfo;
135             }
136             TRACE(resource, "  Skipping type %04x\n", pTypeInfo->type_id );
137             pTypeInfo = NEXT_TYPEINFO(pTypeInfo);
138         }
139     }
140     return NULL;
141 }
142
143 /***********************************************************************
144  *           NE_FindResourceFromType
145  *
146  * Find a resource once the type info structure has been found.
147  */
148 NE_NAMEINFO *NE_FindResourceFromType( LPBYTE pResTab,
149                                       NE_TYPEINFO *pTypeInfo, LPCSTR resId )
150 {
151     BYTE *p;
152     int count;
153     NE_NAMEINFO *pNameInfo = (NE_NAMEINFO *)(pTypeInfo + 1);
154
155     if (HIWORD(resId) != 0)  /* Named resource */
156     {
157         LPCSTR str = resId;
158         BYTE len = strlen( str );
159         for (count = pTypeInfo->count; count > 0; count--, pNameInfo++)
160         {
161             if (pNameInfo->id & 0x8000) continue;
162             p = pResTab + pNameInfo->id;
163             if ((*p == len) && !lstrncmpiA( p+1, str, len ))
164                 return pNameInfo;
165         }
166     }
167     else  /* Numeric resource id */
168     {
169         WORD id = LOWORD(resId) | 0x8000;
170         for (count = pTypeInfo->count; count > 0; count--, pNameInfo++)
171             if (pNameInfo->id == id) 
172                 return pNameInfo;
173     }
174     return NULL;
175 }
176
177
178 /***********************************************************************
179  *           NE_DefResourceHandler
180  *
181  * This is the default LoadProc() function. 
182  */
183 HGLOBAL16 WINAPI NE_DefResourceHandler( HGLOBAL16 hMemObj, HMODULE16 hModule,
184                                         HRSRC16 hRsrc )
185 {
186     HANDLE fd;
187     NE_MODULE* pModule = NE_GetPtr( hModule );
188     if (pModule && (fd = NE_OpenFile( pModule )) >= 0)
189     {
190         HGLOBAL16 handle;
191         WORD sizeShift = *(WORD *)((char *)pModule + pModule->res_table);
192         NE_NAMEINFO* pNameInfo = (NE_NAMEINFO*)((char*)pModule + hRsrc);
193
194         TRACE(resource, "loading, pos=%d, len=%d\n",
195                      (int)pNameInfo->offset << sizeShift,
196                      (int)pNameInfo->length << sizeShift );
197         if( hMemObj )
198             handle = GlobalReAlloc16( hMemObj, pNameInfo->length << sizeShift, 0 );
199         else
200             handle = AllocResource16( hModule, hRsrc, 0 );
201
202         if( handle )
203         {
204             DWORD res;
205             SetFilePointer( fd, (int)pNameInfo->offset << sizeShift, NULL, SEEK_SET );
206             ReadFile( fd, GlobalLock16( handle ), (int)pNameInfo->length << sizeShift,
207                       &res, NULL );
208         }
209         return handle;
210     }
211     return (HGLOBAL16)0;
212 }
213
214 /***********************************************************************
215  *           NE_InitResourceHandler
216  *
217  * Fill in 'resloader' fields in the resource table.
218  */
219 BOOL NE_InitResourceHandler( HMODULE16 hModule )
220 {
221     NE_MODULE *pModule = NE_GetPtr( hModule );
222     NE_TYPEINFO *pTypeInfo = (NE_TYPEINFO *)((char *)pModule + pModule->res_table + 2);
223
224     FARPROC16 handler = MODULE_GetWndProcEntry16("DefResourceHandler");
225
226     TRACE(resource,"InitResourceHandler[%04x]\n", hModule );
227
228     while(pTypeInfo->type_id)
229     {
230         pTypeInfo->resloader = handler;
231         pTypeInfo = NEXT_TYPEINFO(pTypeInfo);
232     }
233     return TRUE;
234 }
235
236
237 /**********************************************************************
238  *      SetResourceHandler      (KERNEL.43)
239  */
240 FARPROC16 WINAPI SetResourceHandler16( HMODULE16 hModule, SEGPTR typeId,
241                                      FARPROC16 resourceHandler )
242 {
243     FARPROC16 prevHandler = NULL;
244     NE_MODULE *pModule = NE_GetPtr( hModule );
245     LPBYTE pResTab = (LPBYTE)pModule + pModule->res_table;
246     NE_TYPEINFO *pTypeInfo = (NE_TYPEINFO *)(pResTab + 2);
247
248     if (!pModule || !pModule->res_table) return NULL;
249
250     TRACE( resource, "module=%04x type=%s\n",
251            hModule, debugres_a(PTR_SEG_TO_LIN(typeId)) );
252
253     for (;;)
254     {
255         if (!(pTypeInfo = NE_FindTypeSection( pResTab, pTypeInfo, PTR_SEG_TO_LIN(typeId) )))
256             break;
257         prevHandler = pTypeInfo->resloader;
258         pTypeInfo->resloader = resourceHandler;
259         pTypeInfo = NEXT_TYPEINFO(pTypeInfo);
260     }
261     return prevHandler;
262 }
263
264
265 /**********************************************************************
266  *          NE_FindResource
267  */
268 HRSRC16 NE_FindResource( NE_MODULE *pModule, LPCSTR name, LPCSTR type )
269 {
270     NE_TYPEINFO *pTypeInfo;
271     NE_NAMEINFO *pNameInfo;
272     LPBYTE pResTab;
273
274     if (!pModule || !pModule->res_table) return 0;
275
276     TRACE( resource, "module=%04x name=%s type=%s\n", 
277            pModule->self, debugres_a(PTR_SEG_TO_LIN(name)),
278            debugres_a(PTR_SEG_TO_LIN(type)) );
279
280     if (HIWORD(name))  /* Check for '#xxx' name */
281     {
282         LPCSTR ptr = name;
283         if (ptr[0] == '#')
284             if (!(name = (LPCSTR)atoi( ptr + 1 )))
285             {
286                 WARN(resource, "Incorrect resource name: %s\n", ptr);
287                 return 0;
288             }
289     }
290
291     if (HIWORD(type))  /* Check for '#xxx' type */
292     {
293         LPCSTR ptr = type;
294         if (ptr[0] == '#')
295             if (!(type = (LPCSTR)atoi( ptr + 1 )))
296             {
297                 WARN(resource, "Incorrect resource type: %s\n", ptr);
298                 return 0;
299             }
300     }
301
302     if (HIWORD(type) || HIWORD(name))
303     {
304         DWORD id = NE_FindNameTableId( pModule, type, name );
305         if (id)  /* found */
306         {
307             type = (LPCSTR)(int)LOWORD(id);
308             name = (LPCSTR)(int)HIWORD(id);
309         }
310     }
311
312     pResTab = (LPBYTE)pModule + pModule->res_table;
313     pTypeInfo = (NE_TYPEINFO *)( pResTab + 2 );
314
315     for (;;)
316     {
317         if (!(pTypeInfo = NE_FindTypeSection( pResTab, pTypeInfo, type )))
318             break;
319         if ((pNameInfo = NE_FindResourceFromType( pResTab, pTypeInfo, name )))
320         {
321             TRACE(resource, "    Found id %08lx\n", (DWORD)name );
322             return (HRSRC16)( (int)pNameInfo - (int)pModule );
323         }
324         TRACE(resource, "    Not found, going on\n" );
325         pTypeInfo = NEXT_TYPEINFO(pTypeInfo);
326     }
327
328     WARN(resource, "failed!\n");
329     return 0;
330 }
331
332
333 /**********************************************************************
334  *          AllocResource    (KERNEL.66)
335  */
336 HGLOBAL16 WINAPI AllocResource16( HMODULE16 hModule, HRSRC16 hRsrc, DWORD size)
337 {
338     NE_NAMEINFO *pNameInfo=NULL;
339     WORD sizeShift;
340
341     NE_MODULE *pModule = NE_GetPtr( hModule );
342     if (!pModule || !pModule->res_table || !hRsrc) return 0;
343
344     TRACE( resource, "module=%04x res=%04x size=%ld\n", hModule, hRsrc, size );
345
346     sizeShift = *(WORD *)((char *)pModule + pModule->res_table);
347     pNameInfo = (NE_NAMEINFO*)((char*)pModule + hRsrc);
348     if (size < (DWORD)pNameInfo->length << sizeShift)
349         size = (DWORD)pNameInfo->length << sizeShift;
350     return GLOBAL_Alloc( GMEM_FIXED, size, hModule, FALSE, FALSE, FALSE );
351 }
352
353
354 /**********************************************************************
355  *      DirectResAlloc    (KERNEL.168)
356  *
357  * Check Schulman, p. 232 for details
358  */
359 HGLOBAL16 WINAPI DirectResAlloc16( HINSTANCE16 hInstance, WORD wType,
360                                  UINT16 wSize )
361 {
362     TRACE(resource,"(%04x,%04x,%04x)\n",
363                      hInstance, wType, wSize );
364     if (!(hInstance = GetExePtr( hInstance ))) return 0;
365     if(wType != 0x10)   /* 0x10 is the only observed value, passed from
366                            CreateCursorIndirect. */
367         TRACE(resource, "(wType=%x)\n", wType);
368     return GLOBAL_Alloc(GMEM_MOVEABLE, wSize, hInstance, FALSE, FALSE, FALSE);
369 }
370
371
372 /**********************************************************************
373  *          NE_AccessResource
374  */
375 INT16 NE_AccessResource( NE_MODULE *pModule, HRSRC16 hRsrc )
376 {
377     HFILE16 fd;
378
379     if (!pModule || !pModule->res_table || !hRsrc) return -1;
380
381     TRACE(resource, "module=%04x res=%04x\n", pModule->self, hRsrc );
382
383     if ((fd = _lopen16( NE_MODULE_NAME(pModule), OF_READ )) != HFILE_ERROR16)
384     {
385         WORD sizeShift = *(WORD *)((char *)pModule + pModule->res_table);
386         NE_NAMEINFO *pNameInfo = (NE_NAMEINFO*)((char*)pModule + hRsrc);
387         _llseek16( fd, (int)pNameInfo->offset << sizeShift, SEEK_SET );
388     }
389     return fd;
390 }
391
392
393 /**********************************************************************
394  *          NE_SizeofResource
395  */
396 DWORD NE_SizeofResource( NE_MODULE *pModule, HRSRC16 hRsrc )
397 {
398     NE_NAMEINFO *pNameInfo=NULL;
399     WORD sizeShift;
400
401     if (!pModule || !pModule->res_table) return 0;
402
403     TRACE(resource, "module=%04x res=%04x\n", pModule->self, hRsrc );
404
405     sizeShift = *(WORD *)((char *)pModule + pModule->res_table);
406     pNameInfo = (NE_NAMEINFO*)((char*)pModule + hRsrc);
407     return (DWORD)pNameInfo->length << sizeShift;
408 }
409
410
411 /**********************************************************************
412  *          NE_LoadResource
413  */
414 HGLOBAL16 NE_LoadResource( NE_MODULE *pModule, HRSRC16 hRsrc )
415 {
416     NE_TYPEINFO *pTypeInfo;
417     NE_NAMEINFO *pNameInfo = NULL;
418     int d;
419
420     TRACE( resource, "module=%04x res=%04x\n", pModule->self, hRsrc );
421     if (!hRsrc || !pModule || !pModule->res_table) return 0;
422
423     /* First, verify hRsrc (just an offset from pModule to the needed pNameInfo) */
424
425     d = pModule->res_table + 2;
426     pTypeInfo = (NE_TYPEINFO *)((char *)pModule + d);
427     while( hRsrc > d )
428     {
429         if (pTypeInfo->type_id == 0)
430                 break; /* terminal entry */
431         d += sizeof(NE_TYPEINFO) + pTypeInfo->count * sizeof(NE_NAMEINFO);
432         if (hRsrc < d)
433         {
434             if( ((d - hRsrc)%sizeof(NE_NAMEINFO)) == 0 )
435             {
436                 pNameInfo = (NE_NAMEINFO *)(((char *)pModule) + hRsrc);
437                 break;
438             }
439             else 
440                 break; /* NE_NAMEINFO boundary mismatch */
441         }
442         pTypeInfo = (NE_TYPEINFO *)(((char *)pModule) + d);
443     }
444
445     if (pNameInfo)
446     {
447         if (pNameInfo->handle
448             && !(GlobalFlags16(pNameInfo->handle) & GMEM_DISCARDED))
449         {
450             pNameInfo->usage++;
451             TRACE(resource, "  Already loaded, new count=%d\n",
452                               pNameInfo->usage );
453         }
454         else
455         {
456             if (pTypeInfo->resloader)
457                 pNameInfo->handle = Callbacks->CallResourceHandlerProc(
458                     pTypeInfo->resloader, pNameInfo->handle, pModule->self, hRsrc );
459             else /* this is really bad */
460             {
461                 ERR(resource, "[%04x]: Missing resource handler!\n", pModule->self);
462                 pNameInfo->handle = NE_DefResourceHandler(
463                                          pNameInfo->handle, pModule->self, hRsrc );
464             }
465
466             if (pNameInfo->handle)
467             {
468                 pNameInfo->usage++;
469                 pNameInfo->flags |= NE_SEGFLAGS_LOADED;
470             }
471         }
472         return pNameInfo->handle;
473     }
474     return 0;
475 }
476
477
478 /**********************************************************************
479  *          NE_FreeResource
480  */
481 BOOL16 NE_FreeResource( NE_MODULE *pModule, HGLOBAL16 handle )
482 {
483     NE_TYPEINFO *pTypeInfo;
484     NE_NAMEINFO *pNameInfo;
485     WORD count;
486
487     if (!handle || !pModule || !pModule->res_table) return handle;
488
489     TRACE(resource, "handle=%04x\n", handle );
490
491     pTypeInfo = (NE_TYPEINFO *)((char *)pModule + pModule->res_table + 2);
492     while (pTypeInfo->type_id)
493     {
494         pNameInfo = (NE_NAMEINFO *)(pTypeInfo + 1);
495         for (count = pTypeInfo->count; count > 0; count--)
496         {
497             if (pNameInfo->handle == handle)
498             {
499                 if (pNameInfo->usage > 0) pNameInfo->usage--;
500                 if (pNameInfo->usage == 0)
501                 {
502                     GlobalFree16( pNameInfo->handle );
503                     pNameInfo->handle = 0;
504                     pNameInfo->flags &= ~NE_SEGFLAGS_LOADED;
505                 }
506                 return 0;
507             }
508             pNameInfo++;
509         }
510         pTypeInfo = (NE_TYPEINFO *)pNameInfo;
511     }
512
513     return handle;
514 }