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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include <sys/types.h>
34 #define NONAMELESSUNION
35 #define NONAMELESSSTRUCT
40 #include "wine/unicode.h"
41 #include "wine/winbase16.h"
42 #include "wine/winuser16.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(ver);
50 /**********************************************************************
53 * Find an entry by id in a resource directory
54 * Copied from loader/pe_resource.c
56 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
57 WORD id, const void *root )
59 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
62 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
63 min = dir->NumberOfNamedEntries;
64 max = min + dir->NumberOfIdEntries - 1;
67 pos = (min + max) / 2;
68 if (entry[pos].u1.s2.Id == id)
69 return (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry[pos].u2.s3.OffsetToDirectory);
70 if (entry[pos].u1.s2.Id > id) max = pos - 1;
77 /**********************************************************************
80 * Find a default entry in a resource directory
81 * Copied from loader/pe_resource.c
83 static const IMAGE_RESOURCE_DIRECTORY *find_entry_default( const IMAGE_RESOURCE_DIRECTORY *dir,
86 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
88 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
89 return (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry->u2.s3.OffsetToDirectory);
93 /**********************************************************************
96 * Find an entry by name in a resource directory
97 * Copied from loader/pe_resource.c
99 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_name( const IMAGE_RESOURCE_DIRECTORY *dir,
100 LPCSTR name, const void *root )
102 const IMAGE_RESOURCE_DIRECTORY *ret = NULL;
106 if (!HIWORD(name)) return find_entry_by_id( dir, LOWORD(name), root );
109 return find_entry_by_id( dir, atoi(name+1), root );
112 namelen = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
113 if ((nameW = HeapAlloc( GetProcessHeap(), 0, namelen * sizeof(WCHAR) )))
115 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
116 const IMAGE_RESOURCE_DIR_STRING_U *str;
117 int min, max, res, pos;
119 MultiByteToWideChar( CP_ACP, 0, name, -1, nameW, namelen );
120 namelen--; /* remove terminating null */
121 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
123 max = dir->NumberOfNamedEntries - 1;
126 pos = (min + max) / 2;
127 str = (IMAGE_RESOURCE_DIR_STRING_U *)((char *)root + entry[pos].u1.s1.NameOffset);
128 res = strncmpiW( nameW, str->NameString, str->Length );
129 if (!res && namelen == str->Length)
131 ret = (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry[pos].u2.s3.OffsetToDirectory);
134 if (res < 0) max = pos - 1;
137 HeapFree( GetProcessHeap(), 0, nameW );
143 /***********************************************************************
144 * read_xx_header [internal]
146 static int read_xx_header( HFILE lzfd )
148 IMAGE_DOS_HEADER mzh;
151 LZSeek( lzfd, 0, SEEK_SET );
152 if ( sizeof(mzh) != LZRead( lzfd, (LPSTR)&mzh, sizeof(mzh) ) )
154 if ( mzh.e_magic != IMAGE_DOS_SIGNATURE )
157 LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
158 if ( 2 != LZRead( lzfd, magic, 2 ) )
161 LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
163 if ( magic[0] == 'N' && magic[1] == 'E' )
164 return IMAGE_OS2_SIGNATURE;
165 if ( magic[0] == 'P' && magic[1] == 'E' )
166 return IMAGE_NT_SIGNATURE;
169 WARN("Can't handle %s files.\n", magic );
173 /***********************************************************************
174 * load_ne_resource [internal]
176 static BOOL find_ne_resource( HFILE lzfd, LPCSTR typeid, LPCSTR resid,
177 DWORD *resLen, DWORD *resOff )
179 IMAGE_OS2_HEADER nehd;
180 NE_TYPEINFO *typeInfo;
181 NE_NAMEINFO *nameInfo;
187 /* Read in NE header */
188 nehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
189 if ( sizeof(nehd) != LZRead( lzfd, (LPSTR)&nehd, sizeof(nehd) ) ) return 0;
191 resTabSize = nehd.ne_restab - nehd.ne_rsrctab;
194 TRACE("No resources in NE dll\n" );
198 /* Read in resource table */
199 resTab = HeapAlloc( GetProcessHeap(), 0, resTabSize );
200 if ( !resTab ) return FALSE;
202 LZSeek( lzfd, nehd.ne_rsrctab + nehdoffset, SEEK_SET );
203 if ( resTabSize != LZRead( lzfd, resTab, resTabSize ) )
205 HeapFree( GetProcessHeap(), 0, resTab );
210 typeInfo = (NE_TYPEINFO *)(resTab + 2);
212 if (HIWORD(typeid) != 0) /* named type */
214 BYTE len = strlen( typeid );
215 while (typeInfo->type_id)
217 if (!(typeInfo->type_id & 0x8000))
219 BYTE *p = resTab + typeInfo->type_id;
220 if ((*p == len) && !strncasecmp( p+1, typeid, len )) goto found_type;
222 typeInfo = (NE_TYPEINFO *)((char *)(typeInfo + 1) +
223 typeInfo->count * sizeof(NE_NAMEINFO));
226 else /* numeric type id */
228 WORD id = LOWORD(typeid) | 0x8000;
229 while (typeInfo->type_id)
231 if (typeInfo->type_id == id) goto found_type;
232 typeInfo = (NE_TYPEINFO *)((char *)(typeInfo + 1) +
233 typeInfo->count * sizeof(NE_NAMEINFO));
236 TRACE("No typeid entry found for %p\n", typeid );
237 HeapFree( GetProcessHeap(), 0, resTab );
241 nameInfo = (NE_NAMEINFO *)(typeInfo + 1);
243 if (HIWORD(resid) != 0) /* named resource */
245 BYTE len = strlen( resid );
246 for (count = typeInfo->count; count > 0; count--, nameInfo++)
248 BYTE *p = resTab + nameInfo->id;
249 if (nameInfo->id & 0x8000) continue;
250 if ((*p == len) && !strncasecmp( p+1, resid, len )) goto found_name;
253 else /* numeric resource id */
255 WORD id = LOWORD(resid) | 0x8000;
256 for (count = typeInfo->count; count > 0; count--, nameInfo++)
257 if (nameInfo->id == id) goto found_name;
259 TRACE("No resid entry found for %p\n", typeid );
260 HeapFree( GetProcessHeap(), 0, resTab );
264 /* Return resource data */
265 if ( resLen ) *resLen = nameInfo->length << *(WORD *)resTab;
266 if ( resOff ) *resOff = nameInfo->offset << *(WORD *)resTab;
268 HeapFree( GetProcessHeap(), 0, resTab );
272 /***********************************************************************
273 * load_pe_resource [internal]
275 static BOOL find_pe_resource( HFILE lzfd, LPCSTR typeid, LPCSTR resid,
276 DWORD *resLen, DWORD *resOff )
278 IMAGE_NT_HEADERS pehd;
280 PIMAGE_DATA_DIRECTORY resDataDir;
281 PIMAGE_SECTION_HEADER sections;
283 DWORD resSectionSize;
285 const IMAGE_RESOURCE_DIRECTORY *resPtr;
286 const IMAGE_RESOURCE_DATA_ENTRY *resData;
290 /* Read in PE header */
291 pehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
292 if ( sizeof(pehd) != LZRead( lzfd, (LPSTR)&pehd, sizeof(pehd) ) ) return 0;
294 resDataDir = pehd.OptionalHeader.DataDirectory+IMAGE_FILE_RESOURCE_DIRECTORY;
295 if ( !resDataDir->Size )
297 TRACE("No resources in PE dll\n" );
301 /* Read in section table */
302 nSections = pehd.FileHeader.NumberOfSections;
303 sections = HeapAlloc( GetProcessHeap(), 0,
304 nSections * sizeof(IMAGE_SECTION_HEADER) );
305 if ( !sections ) return FALSE;
307 LZSeek( lzfd, pehdoffset +
308 sizeof(DWORD) + /* Signature */
309 sizeof(IMAGE_FILE_HEADER) +
310 pehd.FileHeader.SizeOfOptionalHeader, SEEK_SET );
312 if ( nSections * sizeof(IMAGE_SECTION_HEADER) !=
313 LZRead( lzfd, (LPSTR)sections, nSections * sizeof(IMAGE_SECTION_HEADER) ) )
315 HeapFree( GetProcessHeap(), 0, sections );
319 /* Find resource section */
320 for ( i = 0; i < nSections; i++ )
321 if ( resDataDir->VirtualAddress >= sections[i].VirtualAddress
322 && resDataDir->VirtualAddress < sections[i].VirtualAddress +
323 sections[i].SizeOfRawData )
326 if ( i == nSections )
328 HeapFree( GetProcessHeap(), 0, sections );
329 TRACE("Couldn't find resource section\n" );
333 /* Read in resource section */
334 resSectionSize = sections[i].SizeOfRawData;
335 resSection = HeapAlloc( GetProcessHeap(), 0, resSectionSize );
338 HeapFree( GetProcessHeap(), 0, sections );
342 LZSeek( lzfd, sections[i].PointerToRawData, SEEK_SET );
343 if ( resSectionSize != LZRead( lzfd, resSection, resSectionSize ) ) goto done;
346 resDir = resSection + (resDataDir->VirtualAddress - sections[i].VirtualAddress);
348 resPtr = (PIMAGE_RESOURCE_DIRECTORY)resDir;
349 resPtr = find_entry_by_name( resPtr, typeid, resDir );
352 TRACE("No typeid entry found for %p\n", typeid );
355 resPtr = find_entry_by_name( resPtr, resid, resDir );
358 TRACE("No resid entry found for %p\n", resid );
361 resPtr = find_entry_default( resPtr, resDir );
364 TRACE("No default language entry found for %p\n", resid );
368 /* Find resource data section */
369 resData = (PIMAGE_RESOURCE_DATA_ENTRY)resPtr;
370 for ( i = 0; i < nSections; i++ )
371 if ( resData->OffsetToData >= sections[i].VirtualAddress
372 && resData->OffsetToData < sections[i].VirtualAddress +
373 sections[i].SizeOfRawData )
376 if ( i == nSections )
378 TRACE("Couldn't find resource data section\n" );
382 /* Return resource data */
383 if ( resLen ) *resLen = resData->Size;
384 if ( resOff ) *resOff = resData->OffsetToData - sections[i].VirtualAddress
385 + sections[i].PointerToRawData;
389 HeapFree( GetProcessHeap(), 0, resSection );
390 HeapFree( GetProcessHeap(), 0, sections );
395 /*************************************************************************
396 * GetFileResourceSize [VER.2]
398 DWORD WINAPI GetFileResourceSize16( LPCSTR lpszFileName, LPCSTR lpszResType,
399 LPCSTR lpszResId, LPDWORD lpdwFileOffset )
406 TRACE("(%s,type=0x%lx,id=0x%lx,off=%p)\n",
407 debugstr_a(lpszFileName), (LONG)lpszResType, (LONG)lpszResId,
410 lzfd = LZOpenFileA( (LPSTR)lpszFileName, &ofs, OF_READ );
411 if ( lzfd < 0 ) return 0;
413 switch ( read_xx_header( lzfd ) )
415 case IMAGE_OS2_SIGNATURE:
416 retv = find_ne_resource( lzfd, lpszResType, lpszResId,
417 &reslen, lpdwFileOffset );
420 case IMAGE_NT_SIGNATURE:
421 retv = find_pe_resource( lzfd, lpszResType, lpszResId,
422 &reslen, lpdwFileOffset );
427 return retv? reslen : 0;
431 /*************************************************************************
432 * GetFileResource [VER.3]
434 DWORD WINAPI GetFileResource16( LPCSTR lpszFileName, LPCSTR lpszResType,
435 LPCSTR lpszResId, DWORD dwFileOffset,
436 DWORD dwResLen, LPVOID lpvData )
441 DWORD reslen = dwResLen;
443 TRACE("(%s,type=0x%lx,id=0x%lx,off=%ld,len=%ld,data=%p)\n",
444 debugstr_a(lpszFileName), (LONG)lpszResType, (LONG)lpszResId,
445 dwFileOffset, dwResLen, lpvData );
447 lzfd = LZOpenFileA( (LPSTR)lpszFileName, &ofs, OF_READ );
448 if ( lzfd < 0 ) return 0;
452 switch ( read_xx_header( lzfd ) )
454 case IMAGE_OS2_SIGNATURE:
455 retv = find_ne_resource( lzfd, lpszResType, lpszResId,
456 &reslen, &dwFileOffset );
459 case IMAGE_NT_SIGNATURE:
460 retv = find_pe_resource( lzfd, lpszResType, lpszResId,
461 &reslen, &dwFileOffset );
472 LZSeek( lzfd, dwFileOffset, SEEK_SET );
473 reslen = LZRead( lzfd, lpvData, min( reslen, dwResLen ) );