dsound: Reset device playpos/mixpos on buflen change.
[wine/hacks.git] / dlls / version / resource.c
blob63311756f2e9d218f03c0b4d034784b961b7b484
1 /*
2 * Implementation of VERSION.DLL - Resource Access routines
4 * Copyright 1996,1997 Marcus Meissner
5 * Copyright 1997 David Cuthbert
6 * Copyright 1999 Ulrich Weigand
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
34 #define NONAMELESSUNION
35 #define NONAMELESSSTRUCT
36 #include "windef.h"
37 #include "winbase.h"
38 #include "lzexpand.h"
40 #include "wine/unicode.h"
41 #include "wine/winbase16.h"
42 #include "wine/winuser16.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(ver);
49 /**********************************************************************
50 * find_entry_by_id
52 * Find an entry by id in a resource directory
53 * Copied from loader/pe_resource.c
55 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
56 WORD id, const void *root )
58 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
59 int min, max, pos;
61 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
62 min = dir->NumberOfNamedEntries;
63 max = min + dir->NumberOfIdEntries - 1;
64 while (min <= max)
66 pos = (min + max) / 2;
67 if (entry[pos].u1.s2.Id == id)
68 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s3.OffsetToDirectory);
69 if (entry[pos].u1.s2.Id > id) max = pos - 1;
70 else min = pos + 1;
72 return NULL;
76 /**********************************************************************
77 * find_entry_default
79 * Find a default entry in a resource directory
80 * Copied from loader/pe_resource.c
82 static const IMAGE_RESOURCE_DIRECTORY *find_entry_default( const IMAGE_RESOURCE_DIRECTORY *dir,
83 const void *root )
85 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
87 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
88 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry->u2.s3.OffsetToDirectory);
92 /**********************************************************************
93 * find_entry_by_name
95 * Find an entry by name in a resource directory
96 * Copied from loader/pe_resource.c
98 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_name( const IMAGE_RESOURCE_DIRECTORY *dir,
99 LPCSTR name, const void *root )
101 const IMAGE_RESOURCE_DIRECTORY *ret = NULL;
102 LPWSTR nameW;
103 DWORD namelen;
105 if (!HIWORD(name)) return find_entry_by_id( dir, LOWORD(name), root );
106 if (name[0] == '#')
108 return find_entry_by_id( dir, atoi(name+1), root );
111 namelen = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
112 if ((nameW = HeapAlloc( GetProcessHeap(), 0, namelen * sizeof(WCHAR) )))
114 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
115 const IMAGE_RESOURCE_DIR_STRING_U *str;
116 int min, max, res, pos;
118 MultiByteToWideChar( CP_ACP, 0, name, -1, nameW, namelen );
119 namelen--; /* remove terminating null */
120 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
121 min = 0;
122 max = dir->NumberOfNamedEntries - 1;
123 while (min <= max)
125 pos = (min + max) / 2;
126 str = (const IMAGE_RESOURCE_DIR_STRING_U *)((const char *)root + entry[pos].u1.s1.NameOffset);
127 res = strncmpiW( nameW, str->NameString, str->Length );
128 if (!res && namelen == str->Length)
130 ret = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s3.OffsetToDirectory);
131 break;
133 if (res < 0) max = pos - 1;
134 else min = pos + 1;
136 HeapFree( GetProcessHeap(), 0, nameW );
138 return ret;
142 /***********************************************************************
143 * read_xx_header [internal]
145 static int read_xx_header( HFILE lzfd )
147 IMAGE_DOS_HEADER mzh;
148 char magic[3];
150 LZSeek( lzfd, 0, SEEK_SET );
151 if ( sizeof(mzh) != LZRead( lzfd, (LPSTR)&mzh, sizeof(mzh) ) )
152 return 0;
153 if ( mzh.e_magic != IMAGE_DOS_SIGNATURE )
154 return 0;
156 LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
157 if ( 2 != LZRead( lzfd, magic, 2 ) )
158 return 0;
160 LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
162 if ( magic[0] == 'N' && magic[1] == 'E' )
163 return IMAGE_OS2_SIGNATURE;
164 if ( magic[0] == 'P' && magic[1] == 'E' )
165 return IMAGE_NT_SIGNATURE;
167 magic[2] = '\0';
168 WARN("Can't handle %s files.\n", magic );
169 return 0;
172 /***********************************************************************
173 * load_ne_resource [internal]
175 static BOOL find_ne_resource( HFILE lzfd, LPCSTR typeid, LPCSTR resid,
176 DWORD *resLen, DWORD *resOff )
178 IMAGE_OS2_HEADER nehd;
179 NE_TYPEINFO *typeInfo;
180 NE_NAMEINFO *nameInfo;
181 DWORD nehdoffset;
182 LPBYTE resTab;
183 DWORD resTabSize;
184 int count;
186 /* Read in NE header */
187 nehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
188 if ( sizeof(nehd) != LZRead( lzfd, (LPSTR)&nehd, sizeof(nehd) ) ) return 0;
190 resTabSize = nehd.ne_restab - nehd.ne_rsrctab;
191 if ( !resTabSize )
193 TRACE("No resources in NE dll\n" );
194 return FALSE;
197 /* Read in resource table */
198 resTab = HeapAlloc( GetProcessHeap(), 0, resTabSize );
199 if ( !resTab ) return FALSE;
201 LZSeek( lzfd, nehd.ne_rsrctab + nehdoffset, SEEK_SET );
202 if ( resTabSize != LZRead( lzfd, (char*)resTab, resTabSize ) )
204 HeapFree( GetProcessHeap(), 0, resTab );
205 return FALSE;
208 /* Find resource */
209 typeInfo = (NE_TYPEINFO *)(resTab + 2);
211 if (HIWORD(typeid) != 0) /* named type */
213 BYTE len = strlen( typeid );
214 while (typeInfo->type_id)
216 if (!(typeInfo->type_id & 0x8000))
218 BYTE *p = resTab + typeInfo->type_id;
219 if ((*p == len) && !strncasecmp( (char*)p+1, typeid, len )) goto found_type;
221 typeInfo = (NE_TYPEINFO *)((char *)(typeInfo + 1) +
222 typeInfo->count * sizeof(NE_NAMEINFO));
225 else /* numeric type id */
227 WORD id = LOWORD(typeid) | 0x8000;
228 while (typeInfo->type_id)
230 if (typeInfo->type_id == id) goto found_type;
231 typeInfo = (NE_TYPEINFO *)((char *)(typeInfo + 1) +
232 typeInfo->count * sizeof(NE_NAMEINFO));
235 TRACE("No typeid entry found for %p\n", typeid );
236 HeapFree( GetProcessHeap(), 0, resTab );
237 return FALSE;
239 found_type:
240 nameInfo = (NE_NAMEINFO *)(typeInfo + 1);
242 if (HIWORD(resid) != 0) /* named resource */
244 BYTE len = strlen( resid );
245 for (count = typeInfo->count; count > 0; count--, nameInfo++)
247 BYTE *p = resTab + nameInfo->id;
248 if (nameInfo->id & 0x8000) continue;
249 if ((*p == len) && !strncasecmp( (char*)p+1, resid, len )) goto found_name;
252 else /* numeric resource id */
254 WORD id = LOWORD(resid) | 0x8000;
255 for (count = typeInfo->count; count > 0; count--, nameInfo++)
256 if (nameInfo->id == id) goto found_name;
258 TRACE("No resid entry found for %p\n", typeid );
259 HeapFree( GetProcessHeap(), 0, resTab );
260 return FALSE;
262 found_name:
263 /* Return resource data */
264 if ( resLen ) *resLen = nameInfo->length << *(WORD *)resTab;
265 if ( resOff ) *resOff = nameInfo->offset << *(WORD *)resTab;
267 HeapFree( GetProcessHeap(), 0, resTab );
268 return TRUE;
271 /***********************************************************************
272 * load_pe_resource [internal]
274 static BOOL find_pe_resource( HFILE lzfd, LPCSTR typeid, LPCSTR resid,
275 DWORD *resLen, DWORD *resOff )
277 IMAGE_NT_HEADERS pehd;
278 DWORD pehdoffset;
279 PIMAGE_DATA_DIRECTORY resDataDir;
280 PIMAGE_SECTION_HEADER sections;
281 LPBYTE resSection;
282 DWORD resSectionSize;
283 const void *resDir;
284 const IMAGE_RESOURCE_DIRECTORY *resPtr;
285 const IMAGE_RESOURCE_DATA_ENTRY *resData;
286 int i, nSections;
287 BOOL ret = FALSE;
289 /* Read in PE header */
290 pehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
291 if ( sizeof(pehd) != LZRead( lzfd, (LPSTR)&pehd, sizeof(pehd) ) ) return 0;
293 resDataDir = pehd.OptionalHeader.DataDirectory+IMAGE_FILE_RESOURCE_DIRECTORY;
294 if ( !resDataDir->Size )
296 TRACE("No resources in PE dll\n" );
297 return FALSE;
300 /* Read in section table */
301 nSections = pehd.FileHeader.NumberOfSections;
302 sections = HeapAlloc( GetProcessHeap(), 0,
303 nSections * sizeof(IMAGE_SECTION_HEADER) );
304 if ( !sections ) return FALSE;
306 LZSeek( lzfd, pehdoffset +
307 sizeof(DWORD) + /* Signature */
308 sizeof(IMAGE_FILE_HEADER) +
309 pehd.FileHeader.SizeOfOptionalHeader, SEEK_SET );
311 if ( nSections * sizeof(IMAGE_SECTION_HEADER) !=
312 LZRead( lzfd, (LPSTR)sections, nSections * sizeof(IMAGE_SECTION_HEADER) ) )
314 HeapFree( GetProcessHeap(), 0, sections );
315 return FALSE;
318 /* Find resource section */
319 for ( i = 0; i < nSections; i++ )
320 if ( resDataDir->VirtualAddress >= sections[i].VirtualAddress
321 && resDataDir->VirtualAddress < sections[i].VirtualAddress +
322 sections[i].SizeOfRawData )
323 break;
325 if ( i == nSections )
327 HeapFree( GetProcessHeap(), 0, sections );
328 TRACE("Couldn't find resource section\n" );
329 return FALSE;
332 /* Read in resource section */
333 resSectionSize = sections[i].SizeOfRawData;
334 resSection = HeapAlloc( GetProcessHeap(), 0, resSectionSize );
335 if ( !resSection )
337 HeapFree( GetProcessHeap(), 0, sections );
338 return FALSE;
341 LZSeek( lzfd, sections[i].PointerToRawData, SEEK_SET );
342 if ( resSectionSize != LZRead( lzfd, (char*)resSection, resSectionSize ) ) goto done;
344 /* Find resource */
345 resDir = resSection + (resDataDir->VirtualAddress - sections[i].VirtualAddress);
347 resPtr = (const IMAGE_RESOURCE_DIRECTORY*)resDir;
348 resPtr = find_entry_by_name( resPtr, typeid, resDir );
349 if ( !resPtr )
351 TRACE("No typeid entry found for %p\n", typeid );
352 goto done;
354 resPtr = find_entry_by_name( resPtr, resid, resDir );
355 if ( !resPtr )
357 TRACE("No resid entry found for %p\n", resid );
358 goto done;
360 resPtr = find_entry_default( resPtr, resDir );
361 if ( !resPtr )
363 TRACE("No default language entry found for %p\n", resid );
364 goto done;
367 /* Find resource data section */
368 resData = (const IMAGE_RESOURCE_DATA_ENTRY*)resPtr;
369 for ( i = 0; i < nSections; i++ )
370 if ( resData->OffsetToData >= sections[i].VirtualAddress
371 && resData->OffsetToData < sections[i].VirtualAddress +
372 sections[i].SizeOfRawData )
373 break;
375 if ( i == nSections )
377 TRACE("Couldn't find resource data section\n" );
378 goto done;
381 /* Return resource data */
382 if ( resLen ) *resLen = resData->Size;
383 if ( resOff ) *resOff = resData->OffsetToData - sections[i].VirtualAddress
384 + sections[i].PointerToRawData;
385 ret = TRUE;
387 done:
388 HeapFree( GetProcessHeap(), 0, resSection );
389 HeapFree( GetProcessHeap(), 0, sections );
390 return ret;
394 /*************************************************************************
395 * GetFileResourceSize [VER.2]
397 DWORD WINAPI GetFileResourceSize16( LPCSTR lpszFileName, LPCSTR lpszResType,
398 LPCSTR lpszResId, LPDWORD lpdwFileOffset )
400 BOOL retv = FALSE;
401 HFILE lzfd;
402 OFSTRUCT ofs;
403 DWORD reslen;
405 TRACE("(%s,type=0x%x,id=0x%x,off=%p)\n",
406 debugstr_a(lpszFileName), (LONG)lpszResType, (LONG)lpszResId,
407 lpszResId );
409 lzfd = LZOpenFileA( (LPSTR)lpszFileName, &ofs, OF_READ );
410 if ( lzfd < 0 ) return 0;
412 switch ( read_xx_header( lzfd ) )
414 case IMAGE_OS2_SIGNATURE:
415 retv = find_ne_resource( lzfd, lpszResType, lpszResId,
416 &reslen, lpdwFileOffset );
417 break;
419 case IMAGE_NT_SIGNATURE:
420 retv = find_pe_resource( lzfd, lpszResType, lpszResId,
421 &reslen, lpdwFileOffset );
422 break;
425 LZClose( lzfd );
426 return retv? reslen : 0;
430 /*************************************************************************
431 * GetFileResource [VER.3]
433 DWORD WINAPI GetFileResource16( LPCSTR lpszFileName, LPCSTR lpszResType,
434 LPCSTR lpszResId, DWORD dwFileOffset,
435 DWORD dwResLen, LPVOID lpvData )
437 BOOL retv = FALSE;
438 HFILE lzfd;
439 OFSTRUCT ofs;
440 DWORD reslen = dwResLen;
442 TRACE("(%s,type=%p,id=%p,off=%d,len=%d,data=%p)\n",
443 debugstr_a(lpszFileName), lpszResType, lpszResId,
444 dwFileOffset, dwResLen, lpvData );
446 lzfd = LZOpenFileA( (LPSTR)lpszFileName, &ofs, OF_READ );
447 if ( lzfd < 0 ) return 0;
449 if ( !dwFileOffset )
451 switch ( read_xx_header( lzfd ) )
453 case IMAGE_OS2_SIGNATURE:
454 retv = find_ne_resource( lzfd, lpszResType, lpszResId,
455 &reslen, &dwFileOffset );
456 break;
458 case IMAGE_NT_SIGNATURE:
459 retv = find_pe_resource( lzfd, lpszResType, lpszResId,
460 &reslen, &dwFileOffset );
461 break;
464 if ( !retv )
466 LZClose( lzfd );
467 return 0;
471 LZSeek( lzfd, dwFileOffset, SEEK_SET );
472 reslen = LZRead( lzfd, lpvData, min( reslen, dwResLen ) );
473 LZClose( lzfd );
475 return reslen;