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
27 #include <sys/types.h>
32 #define NONAMELESSUNION
33 #define NONAMELESSSTRUCT
37 #include "wine/unicode.h"
38 #include "wine/winbase16.h"
39 #include "wine/winuser16.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(ver);
47 /**********************************************************************
50 * Find an entry by id in a resource directory
51 * Copied from loader/pe_resource.c
53 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
54 WORD id, const void *root )
56 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
59 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
60 min = dir->NumberOfNamedEntries;
61 max = min + dir->NumberOfIdEntries - 1;
64 pos = (min + max) / 2;
65 if (entry[pos].u1.s2.Id == id)
66 return (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry[pos].u2.s3.OffsetToDirectory);
67 if (entry[pos].u1.s2.Id > id) max = pos - 1;
74 /**********************************************************************
77 * Find a default entry in a resource directory
78 * Copied from loader/pe_resource.c
80 static const IMAGE_RESOURCE_DIRECTORY *find_entry_default( const IMAGE_RESOURCE_DIRECTORY *dir,
83 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
85 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
86 return (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry->u2.s3.OffsetToDirectory);
90 /**********************************************************************
93 * Find an entry by name in a resource directory
94 * Copied from loader/pe_resource.c
96 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_name( const IMAGE_RESOURCE_DIRECTORY *dir,
97 LPCSTR name, const void *root )
99 const IMAGE_RESOURCE_DIRECTORY *ret = NULL;
103 if (!HIWORD(name)) return find_entry_by_id( dir, LOWORD(name), root );
106 return find_entry_by_id( dir, atoi(name+1), root );
109 namelen = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
110 if ((nameW = HeapAlloc( GetProcessHeap(), 0, namelen * sizeof(WCHAR) )))
112 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
113 const IMAGE_RESOURCE_DIR_STRING_U *str;
114 int min, max, res, pos;
116 MultiByteToWideChar( CP_ACP, 0, name, -1, nameW, namelen );
117 namelen--; /* remove terminating null */
118 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
120 max = dir->NumberOfNamedEntries - 1;
123 pos = (min + max) / 2;
124 str = (IMAGE_RESOURCE_DIR_STRING_U *)((char *)root + entry[pos].u1.s1.NameOffset);
125 res = strncmpiW( nameW, str->NameString, str->Length );
126 if (!res && namelen == str->Length)
128 ret = (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry[pos].u2.s3.OffsetToDirectory);
131 if (res < 0) max = pos - 1;
134 HeapFree( GetProcessHeap(), 0, nameW );
140 /***********************************************************************
141 * read_xx_header [internal]
143 static int read_xx_header( HFILE lzfd )
145 IMAGE_DOS_HEADER mzh;
148 LZSeek( lzfd, 0, SEEK_SET );
149 if ( sizeof(mzh) != LZRead( lzfd, &mzh, sizeof(mzh) ) )
151 if ( mzh.e_magic != IMAGE_DOS_SIGNATURE )
154 LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
155 if ( 2 != LZRead( lzfd, magic, 2 ) )
158 LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
160 if ( magic[0] == 'N' && magic[1] == 'E' )
161 return IMAGE_OS2_SIGNATURE;
162 if ( magic[0] == 'P' && magic[1] == 'E' )
163 return IMAGE_NT_SIGNATURE;
166 WARN("Can't handle %s files.\n", magic );
170 /***********************************************************************
171 * load_ne_resource [internal]
173 static BOOL find_ne_resource( HFILE lzfd, LPCSTR typeid, LPCSTR resid,
174 DWORD *resLen, DWORD *resOff )
176 IMAGE_OS2_HEADER nehd;
177 NE_TYPEINFO *typeInfo;
178 NE_NAMEINFO *nameInfo;
184 /* Read in NE header */
185 nehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
186 if ( sizeof(nehd) != LZRead( lzfd, &nehd, sizeof(nehd) ) ) return 0;
188 resTabSize = nehd.ne_restab - nehd.ne_rsrctab;
191 TRACE("No resources in NE dll\n" );
195 /* Read in resource table */
196 resTab = HeapAlloc( GetProcessHeap(), 0, resTabSize );
197 if ( !resTab ) return FALSE;
199 LZSeek( lzfd, nehd.ne_rsrctab + nehdoffset, SEEK_SET );
200 if ( resTabSize != LZRead( lzfd, resTab, resTabSize ) )
202 HeapFree( GetProcessHeap(), 0, resTab );
207 typeInfo = (NE_TYPEINFO *)(resTab + 2);
209 if (HIWORD(typeid) != 0) /* named type */
211 BYTE len = strlen( typeid );
212 while (typeInfo->type_id)
214 if (!(typeInfo->type_id & 0x8000))
216 BYTE *p = resTab + typeInfo->type_id;
217 if ((*p == len) && !strncasecmp( p+1, typeid, len )) goto found_type;
219 typeInfo = (NE_TYPEINFO *)((char *)(typeInfo + 1) +
220 typeInfo->count * sizeof(NE_NAMEINFO));
223 else /* numeric type id */
225 WORD id = LOWORD(typeid) | 0x8000;
226 while (typeInfo->type_id)
228 if (typeInfo->type_id == id) goto found_type;
229 typeInfo = (NE_TYPEINFO *)((char *)(typeInfo + 1) +
230 typeInfo->count * sizeof(NE_NAMEINFO));
233 TRACE("No typeid entry found for %p\n", typeid );
234 HeapFree( GetProcessHeap(), 0, resTab );
238 nameInfo = (NE_NAMEINFO *)(typeInfo + 1);
240 if (HIWORD(resid) != 0) /* named resource */
242 BYTE len = strlen( resid );
243 for (count = typeInfo->count; count > 0; count--, nameInfo++)
245 BYTE *p = resTab + nameInfo->id;
246 if (nameInfo->id & 0x8000) continue;
247 if ((*p == len) && !strncasecmp( p+1, resid, len )) goto found_name;
250 else /* numeric resource id */
252 WORD id = LOWORD(resid) | 0x8000;
253 for (count = typeInfo->count; count > 0; count--, nameInfo++)
254 if (nameInfo->id == id) goto found_name;
256 TRACE("No resid entry found for %p\n", typeid );
257 HeapFree( GetProcessHeap(), 0, resTab );
261 /* Return resource data */
262 if ( resLen ) *resLen = nameInfo->length << *(WORD *)resTab;
263 if ( resOff ) *resOff = nameInfo->offset << *(WORD *)resTab;
265 HeapFree( GetProcessHeap(), 0, resTab );
269 /***********************************************************************
270 * load_pe_resource [internal]
272 static BOOL find_pe_resource( HFILE lzfd, LPCSTR typeid, LPCSTR resid,
273 DWORD *resLen, DWORD *resOff )
275 IMAGE_NT_HEADERS pehd;
277 PIMAGE_DATA_DIRECTORY resDataDir;
278 PIMAGE_SECTION_HEADER sections;
280 DWORD resSectionSize;
282 const IMAGE_RESOURCE_DIRECTORY *resPtr;
283 const IMAGE_RESOURCE_DATA_ENTRY *resData;
287 /* Read in PE header */
288 pehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
289 if ( sizeof(pehd) != LZRead( lzfd, &pehd, sizeof(pehd) ) ) return 0;
291 resDataDir = pehd.OptionalHeader.DataDirectory+IMAGE_FILE_RESOURCE_DIRECTORY;
292 if ( !resDataDir->Size )
294 TRACE("No resources in PE dll\n" );
298 /* Read in section table */
299 nSections = pehd.FileHeader.NumberOfSections;
300 sections = HeapAlloc( GetProcessHeap(), 0,
301 nSections * sizeof(IMAGE_SECTION_HEADER) );
302 if ( !sections ) return FALSE;
304 LZSeek( lzfd, pehdoffset +
305 sizeof(DWORD) + /* Signature */
306 sizeof(IMAGE_FILE_HEADER) +
307 pehd.FileHeader.SizeOfOptionalHeader, SEEK_SET );
309 if ( nSections * sizeof(IMAGE_SECTION_HEADER) !=
310 LZRead( lzfd, sections, nSections * sizeof(IMAGE_SECTION_HEADER) ) )
312 HeapFree( GetProcessHeap(), 0, sections );
316 /* Find resource section */
317 for ( i = 0; i < nSections; i++ )
318 if ( resDataDir->VirtualAddress >= sections[i].VirtualAddress
319 && resDataDir->VirtualAddress < sections[i].VirtualAddress +
320 sections[i].SizeOfRawData )
323 if ( i == nSections )
325 HeapFree( GetProcessHeap(), 0, sections );
326 TRACE("Couldn't find resource section\n" );
330 /* Read in resource section */
331 resSectionSize = sections[i].SizeOfRawData;
332 resSection = HeapAlloc( GetProcessHeap(), 0, resSectionSize );
335 HeapFree( GetProcessHeap(), 0, sections );
339 LZSeek( lzfd, sections[i].PointerToRawData, SEEK_SET );
340 if ( resSectionSize != LZRead( lzfd, resSection, resSectionSize ) ) goto done;
343 resDir = resSection + (resDataDir->VirtualAddress - sections[i].VirtualAddress);
345 resPtr = (PIMAGE_RESOURCE_DIRECTORY)resDir;
346 resPtr = find_entry_by_name( resPtr, typeid, resDir );
349 TRACE("No typeid entry found for %p\n", typeid );
352 resPtr = find_entry_by_name( resPtr, resid, resDir );
355 TRACE("No resid entry found for %p\n", resid );
358 resPtr = find_entry_default( resPtr, resDir );
361 TRACE("No default language entry found for %p\n", resid );
365 /* Find resource data section */
366 resData = (PIMAGE_RESOURCE_DATA_ENTRY)resPtr;
367 for ( i = 0; i < nSections; i++ )
368 if ( resData->OffsetToData >= sections[i].VirtualAddress
369 && resData->OffsetToData < sections[i].VirtualAddress +
370 sections[i].SizeOfRawData )
373 if ( i == nSections )
375 TRACE("Couldn't find resource data section\n" );
379 /* Return resource data */
380 if ( resLen ) *resLen = resData->Size;
381 if ( resOff ) *resOff = resData->OffsetToData - sections[i].VirtualAddress
382 + sections[i].PointerToRawData;
386 HeapFree( GetProcessHeap(), 0, resSection );
387 HeapFree( GetProcessHeap(), 0, sections );
392 /*************************************************************************
393 * GetFileResourceSize [VER.2]
395 DWORD WINAPI GetFileResourceSize16( LPCSTR lpszFileName, LPCSTR lpszResType,
396 LPCSTR lpszResId, LPDWORD lpdwFileOffset )
403 TRACE("(%s,type=0x%lx,id=0x%lx,off=%p)\n",
404 debugstr_a(lpszFileName), (LONG)lpszResType, (LONG)lpszResId,
407 lzfd = LZOpenFileA( lpszFileName, &ofs, OF_READ );
408 if ( lzfd < 0 ) return 0;
410 switch ( read_xx_header( lzfd ) )
412 case IMAGE_OS2_SIGNATURE:
413 retv = find_ne_resource( lzfd, lpszResType, lpszResId,
414 &reslen, lpdwFileOffset );
417 case IMAGE_NT_SIGNATURE:
418 retv = find_pe_resource( lzfd, lpszResType, lpszResId,
419 &reslen, lpdwFileOffset );
424 return retv? reslen : 0;
428 /*************************************************************************
429 * GetFileResource [VER.3]
431 DWORD WINAPI GetFileResource16( LPCSTR lpszFileName, LPCSTR lpszResType,
432 LPCSTR lpszResId, DWORD dwFileOffset,
433 DWORD dwResLen, LPVOID lpvData )
438 DWORD reslen = dwResLen;
440 TRACE("(%s,type=0x%lx,id=0x%lx,off=%ld,len=%ld,data=%p)\n",
441 debugstr_a(lpszFileName), (LONG)lpszResType, (LONG)lpszResId,
442 dwFileOffset, dwResLen, lpvData );
444 lzfd = LZOpenFileA( lpszFileName, &ofs, OF_READ );
445 if ( lzfd < 0 ) return 0;
449 switch ( read_xx_header( lzfd ) )
451 case IMAGE_OS2_SIGNATURE:
452 retv = find_ne_resource( lzfd, lpszResType, lpszResId,
453 &reslen, &dwFileOffset );
456 case IMAGE_NT_SIGNATURE:
457 retv = find_pe_resource( lzfd, lpszResType, lpszResId,
458 &reslen, &dwFileOffset );
469 LZSeek( lzfd, dwFileOffset, SEEK_SET );
470 reslen = LZRead( lzfd, lpvData, min( reslen, dwResLen ) );