Release 951226
[wine/gsoc-2012-control.git] / loader / ne_resource.c
blobcaf1e0da99786a626c2a0ad4347b436599ccd90e
1 #ifndef WINELIB
2 /*
4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995 Alexandre Julliard
6 */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <unistd.h>
15 #include "windows.h"
16 #include "arch.h"
17 #include "dos_fs.h"
18 #include "global.h"
19 #include "ldt.h"
20 #include "module.h"
21 #include "neexe.h"
22 #include "resource.h"
23 #include "stddebug.h"
24 #include "debug.h"
26 typedef struct resource_typeinfo_s NE_TYPEINFO;
27 typedef struct resource_nameinfo_s NE_NAMEINFO;
30 /***********************************************************************
31 * NE_FindNameTableId
33 * Find the type and resource id from their names.
34 * Return value is MAKELONG( typeId, resId ), or 0 if not found.
36 static DWORD NE_FindNameTableId( HMODULE hModule, SEGPTR typeId, SEGPTR resId )
38 NE_MODULE *pModule;
39 NE_TYPEINFO *pTypeInfo;
40 NE_NAMEINFO *pNameInfo;
41 HGLOBAL handle;
42 WORD *p;
43 DWORD ret = 0;
44 int count;
46 pModule = (NE_MODULE *)GlobalLock( hModule );
47 pTypeInfo = (NE_TYPEINFO *)((char *)pModule + pModule->res_table + 2);
48 for (; pTypeInfo->type_id != 0;
49 pTypeInfo = (NE_TYPEINFO *)((char*)(pTypeInfo+1) +
50 pTypeInfo->count * sizeof(NE_NAMEINFO)))
52 if (pTypeInfo->type_id != 0x800f) continue;
53 pNameInfo = (NE_NAMEINFO *)(pTypeInfo + 1);
54 for (count = pTypeInfo->count; count > 0; count--, pNameInfo++)
56 dprintf_resource( stddeb, "NameTable entry: type=%04x id=%04x\n",
57 pTypeInfo->type_id, pNameInfo->id );
58 handle = LoadResource( hModule,
59 (HANDLE)((int)pNameInfo - (int)pModule) );
60 for(p = (WORD*)LockResource(handle); p && *p; p = (WORD *)((char*)p+*p))
62 dprintf_resource( stddeb," type=%04x '%s' id=%04x '%s'\n",
63 p[1], (char *)(p+3), p[2],
64 (char *)(p+3)+strlen((char *)(p+3))+1 );
65 /* Check for correct type */
67 if (p[1] & 0x8000)
69 if (!HIWORD(typeId)) continue;
70 if (strcasecmp( (char *)PTR_SEG_TO_LIN(typeId),
71 (char *)(p + 3) )) continue;
73 else if (HIWORD(typeId) || ((typeId & ~0x8000)!= p[1]))
74 continue;
76 /* Now check for the id */
78 if (p[2] & 0x8000)
80 if (!HIWORD(resId)) continue;
81 if (strcasecmp( (char *)PTR_SEG_TO_LIN(resId),
82 (char*)(p+3)+strlen((char*)(p+3))+1 )) continue;
85 else if (HIWORD(resId) || ((resId & ~0x8000) != p[2]))
86 continue;
88 /* If we get here, we've found the entry */
90 dprintf_resource( stddeb, " Found!\n" );
91 ret = MAKELONG( p[1], p[2] );
92 break;
94 FreeResource( handle );
95 if (ret) return ret;
98 return 0;
102 /***********************************************************************
103 * NE_FindResourceFromType
105 * Find a resource once the type info structure has been found.
107 static HRSRC NE_FindResourceFromType( NE_MODULE *pModule,
108 NE_TYPEINFO *pTypeInfo, SEGPTR resId )
110 BYTE *p;
111 int count;
112 NE_NAMEINFO *pNameInfo = (NE_NAMEINFO *)(pTypeInfo + 1);
114 if (HIWORD(resId) != 0) /* Named resource */
116 char *str = (char *)PTR_SEG_TO_LIN( resId );
117 BYTE len = strlen( str );
118 for (count = pTypeInfo->count; count > 0; count--, pNameInfo++)
120 if (pNameInfo->id & 0x8000) continue;
121 p = (BYTE *)pModule + pModule->res_table + pNameInfo->id;
122 if ((*p == len) && !strncasecmp( p+1, str, len ))
123 return (HRSRC)((int)pNameInfo - (int)pModule);
126 else /* Numeric resource id */
128 WORD id = LOWORD(resId) | 0x8000;
129 for (count = pTypeInfo->count; count > 0; count--, pNameInfo++)
130 if (pNameInfo->id == id)
131 return (HRSRC)((int)pNameInfo - (int)pModule);
133 return 0;
137 /***********************************************************************
138 * NE_FindResource
140 HRSRC NE_FindResource( HMODULE hModule, SEGPTR typeId, SEGPTR resId )
142 NE_MODULE *pModule;
143 NE_TYPEINFO *pTypeInfo;
144 HRSRC hRsrc;
146 pModule = (NE_MODULE *)GlobalLock( hModule );
147 if (!pModule || !pModule->res_table) return 0;
148 pTypeInfo = (NE_TYPEINFO *)((char *)pModule + pModule->res_table + 2);
150 if (HIWORD(typeId) || HIWORD(resId))
152 /* Search the names in the nametable */
153 DWORD id = NE_FindNameTableId( hModule, typeId, resId );
154 if (id) /* found */
156 typeId = LOWORD(id);
157 resId = HIWORD(id);
161 if (HIWORD(typeId) != 0) /* Named type */
163 char *str = (char *)PTR_SEG_TO_LIN( typeId );
164 BYTE len = strlen( str );
165 while (pTypeInfo->type_id)
167 if (!(pTypeInfo->type_id & 0x8000))
169 BYTE *p = (BYTE*)pModule+pModule->res_table+pTypeInfo->type_id;
170 if ((*p == len) && !strncasecmp( p+1, str, len ))
172 dprintf_resource( stddeb, " Found type '%s'\n", str );
173 hRsrc = NE_FindResourceFromType(pModule, pTypeInfo, resId);
174 if (hRsrc)
176 dprintf_resource( stddeb, " Found id %08lx\n", resId );
177 return hRsrc;
179 dprintf_resource( stddeb, " Not found, going on\n" );
182 dprintf_resource( stddeb, " Skipping type %04x\n",
183 pTypeInfo->type_id );
184 pTypeInfo = (NE_TYPEINFO *)((char*)(pTypeInfo+1) +
185 pTypeInfo->count * sizeof(NE_NAMEINFO));
188 else /* Numeric type id */
190 WORD id = LOWORD(typeId) | 0x8000;
191 while (pTypeInfo->type_id)
193 if (pTypeInfo->type_id == id)
195 dprintf_resource( stddeb, " Found type %04x\n", id );
196 hRsrc = NE_FindResourceFromType( pModule, pTypeInfo, resId );
197 if (hRsrc)
199 dprintf_resource( stddeb, " Found id %08lx\n", resId );
200 return hRsrc;
202 dprintf_resource( stddeb, " Not found, going on\n" );
204 dprintf_resource( stddeb, " Skipping type %04x\n",
205 pTypeInfo->type_id );
206 pTypeInfo = (NE_TYPEINFO *)((char*)(pTypeInfo+1) +
207 pTypeInfo->count * sizeof(NE_NAMEINFO));
210 return 0;
215 /***********************************************************************
216 * NE_AllocResource
218 HGLOBAL NE_AllocResource( HMODULE hModule, HRSRC hRsrc, DWORD size )
220 NE_MODULE *pModule;
221 NE_NAMEINFO *pNameInfo=NULL;
222 WORD sizeShift;
224 pModule = (NE_MODULE *)GlobalLock( hModule );
225 if (!pModule || !pModule->res_table) return 0;
226 sizeShift = *(WORD *)((char *)pModule + pModule->res_table);
227 #ifndef WINELIB
228 pNameInfo = (NE_NAMEINFO*)((char*)pModule + hRsrc);
229 #endif
230 if (size < (DWORD)pNameInfo->length << sizeShift)
231 size = (DWORD)pNameInfo->length << sizeShift;
232 return GLOBAL_Alloc( GMEM_FIXED, size, hModule, FALSE, FALSE, FALSE );
236 /***********************************************************************
237 * NE_AccessResource
239 int NE_AccessResource( HMODULE hModule, HRSRC hRsrc )
241 NE_MODULE *pModule;
242 NE_NAMEINFO *pNameInfo=NULL;
243 WORD sizeShift;
244 char *name;
245 int fd;
247 pModule = (NE_MODULE *)GlobalLock( hModule );
248 if (!pModule || !pModule->res_table) return 0;
249 #ifndef WINELIB
250 pNameInfo = (NE_NAMEINFO*)((char*)pModule + hRsrc);
251 #endif
253 name = ((LOADEDFILEINFO*)((char*)pModule + pModule->fileinfo))->filename;
254 fd = open( DOS_GetUnixFileName(name), O_RDONLY );
255 sizeShift = *(WORD *)((char *)pModule + pModule->res_table);
256 lseek( fd, (int)pNameInfo->offset << sizeShift, SEEK_SET );
257 return fd;
261 /***********************************************************************
262 * NE_SizeofResource
264 DWORD NE_SizeofResource( HMODULE hModule, HRSRC hRsrc )
266 NE_MODULE *pModule;
267 NE_NAMEINFO *pNameInfo=NULL;
268 WORD sizeShift;
270 pModule = (NE_MODULE *)GlobalLock( hModule );
271 if (!pModule || !pModule->res_table) return 0;
272 sizeShift = *(WORD *)((char *)pModule + pModule->res_table);
273 #ifndef WINELIB
274 pNameInfo = (NE_NAMEINFO*)((char*)pModule + hRsrc);
275 #endif
276 return (DWORD)pNameInfo->length << sizeShift;
280 /***********************************************************************
281 * NE_LoadResource
283 HGLOBAL NE_LoadResource( HMODULE hModule, HRSRC hRsrc )
285 NE_MODULE *pModule;
286 NE_NAMEINFO *pNameInfo=NULL;
287 WORD sizeShift;
288 int fd;
290 pModule = (NE_MODULE *)GlobalLock( hModule );
291 if (!pModule || !pModule->res_table) return 0;
292 #ifndef WINELIB
293 pNameInfo = (NE_NAMEINFO*)((char*)pModule + hRsrc);
294 #endif
295 if (pNameInfo->handle)
297 pNameInfo->usage++;
298 dprintf_resource( stddeb, " Already loaded, new count=%d\n",
299 pNameInfo->usage );
300 return pNameInfo->handle;
302 sizeShift = *(WORD *)((char *)pModule + pModule->res_table);
303 dprintf_resource( stddeb, " Loading, pos=%d, len=%d\n",
304 (int)pNameInfo->offset << sizeShift,
305 (int)pNameInfo->length << sizeShift );
306 if ((fd = MODULE_OpenFile( hModule )) == -1) return 0;
307 pNameInfo->handle = NE_AllocResource( hModule, hRsrc, 0 );
308 pNameInfo->usage = 1;
309 lseek( fd, (int)pNameInfo->offset << sizeShift, SEEK_SET );
310 read( fd, GlobalLock( pNameInfo->handle ),
311 (int)pNameInfo->length << sizeShift );
312 return pNameInfo->handle;
316 /***********************************************************************
317 * NE_LockResource
319 SEGPTR NE_LockResource( HMODULE hModule, HGLOBAL handle )
321 /* May need to reload the resource if discarded */
323 return (SEGPTR)WIN16_GlobalLock( handle );
327 /***********************************************************************
328 * NE_FreeResource
330 BOOL NE_FreeResource( HMODULE hModule, HGLOBAL handle )
332 NE_MODULE *pModule;
333 NE_TYPEINFO *pTypeInfo;
334 NE_NAMEINFO *pNameInfo;
335 WORD count;
337 pModule = (NE_MODULE *)GlobalLock( hModule );
338 if (!pModule || !pModule->res_table) return FALSE;
339 pTypeInfo = (NE_TYPEINFO *)((char *)pModule + pModule->res_table + 2);
340 while (pTypeInfo->type_id)
342 pNameInfo = (NE_NAMEINFO *)(pTypeInfo + 1);
343 for (count = pTypeInfo->count; count > 0; count--)
345 if (pNameInfo->handle == handle)
347 if (pNameInfo->usage > 0) pNameInfo->usage--;
348 if (pNameInfo->usage == 0)
350 GlobalFree( pNameInfo->handle );
351 pNameInfo->handle = 0;
353 return TRUE;
355 pNameInfo++;
357 pTypeInfo = (NE_TYPEINFO *)pNameInfo;
359 fprintf( stderr, "FreeResource: "NPFMT" "NPFMT" not found!\n", hModule, handle );
360 return FALSE;
362 #endif /* WINELIB */