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
29 #include <sys/types.h>
34 #define NONAMELESSUNION
35 #define NONAMELESSSTRUCT
40 #include "wine/unicode.h"
41 #include "wine/winbase16.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(ver);
48 /**********************************************************************
51 * Find an entry by id in a resource directory
52 * Copied from loader/pe_resource.c
54 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
55 WORD id, const void *root )
57 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
60 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
61 min = dir->NumberOfNamedEntries;
62 max = min + dir->NumberOfIdEntries - 1;
65 pos = (min + max) / 2;
66 if (entry[pos].u1.s2.Id == id)
67 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s3.OffsetToDirectory);
68 if (entry[pos].u1.s2.Id > id) max = pos - 1;
75 /**********************************************************************
78 * Find a default entry in a resource directory
79 * Copied from loader/pe_resource.c
81 static const IMAGE_RESOURCE_DIRECTORY *find_entry_default( const IMAGE_RESOURCE_DIRECTORY *dir,
84 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
86 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
87 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry->u2.s3.OffsetToDirectory);
91 /**********************************************************************
94 * Find an entry by name in a resource directory
95 * Copied from loader/pe_resource.c
97 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_name( const IMAGE_RESOURCE_DIRECTORY *dir,
98 LPCSTR name, const void *root )
100 const IMAGE_RESOURCE_DIRECTORY *ret = NULL;
104 if (!HIWORD(name)) return find_entry_by_id( dir, LOWORD(name), root );
107 return find_entry_by_id( dir, atoi(name+1), root );
110 namelen = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
111 if ((nameW = HeapAlloc( GetProcessHeap(), 0, namelen * sizeof(WCHAR) )))
113 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
114 const IMAGE_RESOURCE_DIR_STRING_U *str;
115 int min, max, res, pos;
117 MultiByteToWideChar( CP_ACP, 0, name, -1, nameW, namelen );
118 namelen--; /* remove terminating null */
119 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
121 max = dir->NumberOfNamedEntries - 1;
124 pos = (min + max) / 2;
125 str = (const IMAGE_RESOURCE_DIR_STRING_U *)((const char *)root + entry[pos].u1.s1.NameOffset);
126 res = strncmpiW( nameW, str->NameString, str->Length );
127 if (!res && namelen == str->Length)
129 ret = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s3.OffsetToDirectory);
132 if (res < 0) max = pos - 1;
135 HeapFree( GetProcessHeap(), 0, nameW );
141 /***********************************************************************
142 * read_xx_header [internal]
144 static int read_xx_header( HFILE lzfd )
146 IMAGE_DOS_HEADER mzh;
149 LZSeek( lzfd, 0, SEEK_SET );
150 if ( sizeof(mzh) != LZRead( lzfd, (LPSTR)&mzh, sizeof(mzh) ) )
152 if ( mzh.e_magic != IMAGE_DOS_SIGNATURE )
155 LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
156 if ( 2 != LZRead( lzfd, magic, 2 ) )
159 LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
161 if ( magic[0] == 'N' && magic[1] == 'E' )
162 return IMAGE_OS2_SIGNATURE;
163 if ( magic[0] == 'P' && magic[1] == 'E' )
164 return IMAGE_NT_SIGNATURE;
167 WARN("Can't handle %s files.\n", magic );
171 /***********************************************************************
172 * load_ne_resource [internal]
174 static BOOL find_ne_resource( HFILE lzfd, LPCSTR typeid, LPCSTR resid,
175 DWORD *resLen, DWORD *resOff )
177 IMAGE_OS2_HEADER nehd;
178 NE_TYPEINFO *typeInfo;
179 NE_NAMEINFO *nameInfo;
185 /* Read in NE header */
186 nehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
187 if ( sizeof(nehd) != LZRead( lzfd, (LPSTR)&nehd, sizeof(nehd) ) ) return 0;
189 resTabSize = nehd.ne_restab - nehd.ne_rsrctab;
192 TRACE("No resources in NE dll\n" );
196 /* Read in resource table */
197 resTab = HeapAlloc( GetProcessHeap(), 0, resTabSize );
198 if ( !resTab ) return FALSE;
200 LZSeek( lzfd, nehd.ne_rsrctab + nehdoffset, SEEK_SET );
201 if ( resTabSize != LZRead( lzfd, (char*)resTab, resTabSize ) )
203 HeapFree( GetProcessHeap(), 0, resTab );
208 typeInfo = (NE_TYPEINFO *)(resTab + 2);
210 if (HIWORD(typeid) != 0) /* named type */
212 BYTE len = strlen( typeid );
213 while (typeInfo->type_id)
215 if (!(typeInfo->type_id & 0x8000))
217 BYTE *p = resTab + typeInfo->type_id;
218 if ((*p == len) && !strncasecmp( (char*)p+1, typeid, len )) goto found_type;
220 typeInfo = (NE_TYPEINFO *)((char *)(typeInfo + 1) +
221 typeInfo->count * sizeof(NE_NAMEINFO));
224 else /* numeric type id */
226 WORD id = LOWORD(typeid) | 0x8000;
227 while (typeInfo->type_id)
229 if (typeInfo->type_id == id) goto found_type;
230 typeInfo = (NE_TYPEINFO *)((char *)(typeInfo + 1) +
231 typeInfo->count * sizeof(NE_NAMEINFO));
234 TRACE("No typeid entry found for %p\n", typeid );
235 HeapFree( GetProcessHeap(), 0, resTab );
239 nameInfo = (NE_NAMEINFO *)(typeInfo + 1);
241 if (HIWORD(resid) != 0) /* named resource */
243 BYTE len = strlen( resid );
244 for (count = typeInfo->count; count > 0; count--, nameInfo++)
246 BYTE *p = resTab + nameInfo->id;
247 if (nameInfo->id & 0x8000) continue;
248 if ((*p == len) && !strncasecmp( (char*)p+1, resid, len )) goto found_name;
251 else /* numeric resource id */
253 WORD id = LOWORD(resid) | 0x8000;
254 for (count = typeInfo->count; count > 0; count--, nameInfo++)
255 if (nameInfo->id == id) goto found_name;
257 TRACE("No resid entry found for %p\n", typeid );
258 HeapFree( GetProcessHeap(), 0, resTab );
262 /* Return resource data */
263 if ( resLen ) *resLen = nameInfo->length << *(WORD *)resTab;
264 if ( resOff ) *resOff = nameInfo->offset << *(WORD *)resTab;
266 HeapFree( GetProcessHeap(), 0, resTab );
270 /***********************************************************************
271 * load_pe_resource [internal]
273 static BOOL find_pe_resource( HFILE lzfd, LPCSTR typeid, LPCSTR resid,
274 DWORD *resLen, DWORD *resOff )
276 IMAGE_NT_HEADERS pehd;
278 PIMAGE_DATA_DIRECTORY resDataDir;
279 PIMAGE_SECTION_HEADER sections;
281 DWORD resSectionSize;
283 const IMAGE_RESOURCE_DIRECTORY *resPtr;
284 const IMAGE_RESOURCE_DATA_ENTRY *resData;
288 /* Read in PE header */
289 pehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
290 if ( sizeof(pehd) != LZRead( lzfd, (LPSTR)&pehd, sizeof(pehd) ) ) return 0;
292 resDataDir = pehd.OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_RESOURCE;
293 if ( !resDataDir->Size )
295 TRACE("No resources in PE dll\n" );
299 /* Read in section table */
300 nSections = pehd.FileHeader.NumberOfSections;
301 sections = HeapAlloc( GetProcessHeap(), 0,
302 nSections * sizeof(IMAGE_SECTION_HEADER) );
303 if ( !sections ) return FALSE;
305 LZSeek( lzfd, pehdoffset +
306 sizeof(DWORD) + /* Signature */
307 sizeof(IMAGE_FILE_HEADER) +
308 pehd.FileHeader.SizeOfOptionalHeader, SEEK_SET );
310 if ( nSections * sizeof(IMAGE_SECTION_HEADER) !=
311 LZRead( lzfd, (LPSTR)sections, nSections * sizeof(IMAGE_SECTION_HEADER) ) )
313 HeapFree( GetProcessHeap(), 0, sections );
317 /* Find resource section */
318 for ( i = 0; i < nSections; i++ )
319 if ( resDataDir->VirtualAddress >= sections[i].VirtualAddress
320 && resDataDir->VirtualAddress < sections[i].VirtualAddress +
321 sections[i].SizeOfRawData )
324 if ( i == nSections )
326 HeapFree( GetProcessHeap(), 0, sections );
327 TRACE("Couldn't find resource section\n" );
331 /* Read in resource section */
332 resSectionSize = sections[i].SizeOfRawData;
333 resSection = HeapAlloc( GetProcessHeap(), 0, resSectionSize );
336 HeapFree( GetProcessHeap(), 0, sections );
340 LZSeek( lzfd, sections[i].PointerToRawData, SEEK_SET );
341 if ( resSectionSize != LZRead( lzfd, (char*)resSection, resSectionSize ) ) goto done;
344 resDir = resSection + (resDataDir->VirtualAddress - sections[i].VirtualAddress);
346 resPtr = (const IMAGE_RESOURCE_DIRECTORY*)resDir;
347 resPtr = find_entry_by_name( resPtr, typeid, resDir );
350 TRACE("No typeid entry found for %p\n", typeid );
353 resPtr = find_entry_by_name( resPtr, resid, resDir );
356 TRACE("No resid entry found for %p\n", resid );
359 resPtr = find_entry_default( resPtr, resDir );
362 TRACE("No default language entry found for %p\n", resid );
366 /* Find resource data section */
367 resData = (const IMAGE_RESOURCE_DATA_ENTRY*)resPtr;
368 for ( i = 0; i < nSections; i++ )
369 if ( resData->OffsetToData >= sections[i].VirtualAddress
370 && resData->OffsetToData < sections[i].VirtualAddress +
371 sections[i].SizeOfRawData )
374 if ( i == nSections )
376 TRACE("Couldn't find resource data section\n" );
380 /* Return resource data */
381 if ( resLen ) *resLen = resData->Size;
382 if ( resOff ) *resOff = resData->OffsetToData - sections[i].VirtualAddress
383 + sections[i].PointerToRawData;
387 HeapFree( GetProcessHeap(), 0, resSection );
388 HeapFree( GetProcessHeap(), 0, sections );
393 /*************************************************************************
394 * GetFileResourceSize [VER.2]
396 DWORD WINAPI GetFileResourceSize16( LPCSTR lpszFileName, LPCSTR lpszResType,
397 LPCSTR lpszResId, LPDWORD lpdwFileOffset )
404 TRACE("(%s,type=0x%x,id=0x%x,off=%p)\n",
405 debugstr_a(lpszFileName), (LONG)lpszResType, (LONG)lpszResId,
408 lzfd = LZOpenFileA( (LPSTR)lpszFileName, &ofs, OF_READ );
409 if ( lzfd < 0 ) return 0;
411 switch ( read_xx_header( lzfd ) )
413 case IMAGE_OS2_SIGNATURE:
414 retv = find_ne_resource( lzfd, lpszResType, lpszResId,
415 &reslen, lpdwFileOffset );
418 case IMAGE_NT_SIGNATURE:
419 retv = find_pe_resource( lzfd, lpszResType, lpszResId,
420 &reslen, lpdwFileOffset );
425 return retv? reslen : 0;
429 /*************************************************************************
430 * GetFileResource [VER.3]
432 DWORD WINAPI GetFileResource16( LPCSTR lpszFileName, LPCSTR lpszResType,
433 LPCSTR lpszResId, DWORD dwFileOffset,
434 DWORD dwResLen, LPVOID lpvData )
439 DWORD reslen = dwResLen;
441 TRACE("(%s,type=%p,id=%p,off=%d,len=%d,data=%p)\n",
442 debugstr_a(lpszFileName), lpszResType, lpszResId,
443 dwFileOffset, dwResLen, lpvData );
445 lzfd = LZOpenFileA( (LPSTR)lpszFileName, &ofs, OF_READ );
446 if ( lzfd < 0 ) return 0;
450 switch ( read_xx_header( lzfd ) )
452 case IMAGE_OS2_SIGNATURE:
453 retv = find_ne_resource( lzfd, lpszResType, lpszResId,
454 &reslen, &dwFileOffset );
457 case IMAGE_NT_SIGNATURE:
458 retv = find_pe_resource( lzfd, lpszResType, lpszResId,
459 &reslen, &dwFileOffset );
470 LZSeek( lzfd, dwFileOffset, SEEK_SET );
471 reslen = LZRead( lzfd, lpvData, min( reslen, dwResLen ) );