2 * Implementation of VERSION.DLL - Resource Access routines
4 * Copyright 1996,1997 Marcus Meissner
5 * Copyright 1997 David Cuthbert
6 * Copyright 1999 Ulrich Weigand
11 #include <sys/types.h>
16 #include "wine/unicode.h"
22 #include "debugtools.h"
24 DEFAULT_DEBUG_CHANNEL(ver);
27 /**********************************************************************
30 * Find an entry by id in a resource directory
31 * Copied from loader/pe_resource.c
33 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
34 WORD id, const void *root )
36 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
39 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
40 min = dir->NumberOfNamedEntries;
41 max = min + dir->NumberOfIdEntries - 1;
44 pos = (min + max) / 2;
45 if (entry[pos].u1.Id == id)
46 return (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry[pos].u2.s2.OffsetToDirectory);
47 if (entry[pos].u1.Id > id) max = pos - 1;
54 /**********************************************************************
57 * Find a default entry in a resource directory
58 * Copied from loader/pe_resource.c
60 static const IMAGE_RESOURCE_DIRECTORY *find_entry_default( const IMAGE_RESOURCE_DIRECTORY *dir,
63 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
65 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
66 return (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry->u2.s2.OffsetToDirectory);
70 /**********************************************************************
73 * Find an entry by name in a resource directory
74 * Copied from loader/pe_resource.c
76 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_name( const IMAGE_RESOURCE_DIRECTORY *dir,
77 LPCSTR name, const void *root )
79 const IMAGE_RESOURCE_DIRECTORY *ret = NULL;
83 if (!HIWORD(name)) return find_entry_by_id( dir, LOWORD(name), root );
86 return find_entry_by_id( dir, atoi(name+1), root );
89 namelen = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
90 if ((nameW = HeapAlloc( GetProcessHeap(), 0, namelen * sizeof(WCHAR) )))
92 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
93 const IMAGE_RESOURCE_DIR_STRING_U *str;
94 int min, max, res, pos;
96 MultiByteToWideChar( CP_ACP, 0, name, -1, nameW, namelen );
97 namelen--; /* remove terminating null */
98 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
100 max = dir->NumberOfNamedEntries - 1;
103 pos = (min + max) / 2;
104 str = (IMAGE_RESOURCE_DIR_STRING_U *)((char *)root + entry[pos].u1.s1.NameOffset);
105 res = strncmpiW( nameW, str->NameString, str->Length );
106 if (!res && namelen == str->Length)
108 ret = (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry[pos].u2.s2.OffsetToDirectory);
111 if (res < 0) max = pos - 1;
114 HeapFree( GetProcessHeap(), 0, nameW );
120 /***********************************************************************
121 * read_xx_header [internal]
123 static int read_xx_header( HFILE lzfd )
125 IMAGE_DOS_HEADER mzh;
128 LZSeek( lzfd, 0, SEEK_SET );
129 if ( sizeof(mzh) != LZRead( lzfd, &mzh, sizeof(mzh) ) )
131 if ( mzh.e_magic != IMAGE_DOS_SIGNATURE )
134 LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
135 if ( 2 != LZRead( lzfd, magic, 2 ) )
138 LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
140 if ( magic[0] == 'N' && magic[1] == 'E' )
141 return IMAGE_OS2_SIGNATURE;
142 if ( magic[0] == 'P' && magic[1] == 'E' )
143 return IMAGE_NT_SIGNATURE;
146 WARN("Can't handle %s files.\n", magic );
150 /***********************************************************************
151 * load_ne_resource [internal]
153 static BOOL find_ne_resource( HFILE lzfd, LPCSTR typeid, LPCSTR resid,
154 DWORD *resLen, DWORD *resOff )
156 IMAGE_OS2_HEADER nehd;
157 NE_TYPEINFO *typeInfo;
158 NE_NAMEINFO *nameInfo;
164 /* Read in NE header */
165 nehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
166 if ( sizeof(nehd) != LZRead( lzfd, &nehd, sizeof(nehd) ) ) return 0;
168 resTabSize = nehd.ne_restab - nehd.ne_rsrctab;
171 TRACE("No resources in NE dll\n" );
175 /* Read in resource table */
176 resTab = HeapAlloc( GetProcessHeap(), 0, resTabSize );
177 if ( !resTab ) return FALSE;
179 LZSeek( lzfd, nehd.ne_rsrctab + nehdoffset, SEEK_SET );
180 if ( resTabSize != LZRead( lzfd, resTab, resTabSize ) )
182 HeapFree( GetProcessHeap(), 0, resTab );
187 typeInfo = (NE_TYPEINFO *)(resTab + 2);
189 if (HIWORD(typeid) != 0) /* named type */
191 BYTE len = strlen( typeid );
192 while (typeInfo->type_id)
194 if (!(typeInfo->type_id & 0x8000))
196 BYTE *p = resTab + typeInfo->type_id;
197 if ((*p == len) && !strncasecmp( p+1, typeid, len )) goto found_type;
199 typeInfo = (NE_TYPEINFO *)((char *)(typeInfo + 1) +
200 typeInfo->count * sizeof(NE_NAMEINFO));
203 else /* numeric type id */
205 WORD id = LOWORD(typeid) | 0x8000;
206 while (typeInfo->type_id)
208 if (typeInfo->type_id == id) goto found_type;
209 typeInfo = (NE_TYPEINFO *)((char *)(typeInfo + 1) +
210 typeInfo->count * sizeof(NE_NAMEINFO));
213 TRACE("No typeid entry found for %p\n", typeid );
214 HeapFree( GetProcessHeap(), 0, resTab );
218 nameInfo = (NE_NAMEINFO *)(typeInfo + 1);
220 if (HIWORD(resid) != 0) /* named resource */
222 BYTE len = strlen( resid );
223 for (count = typeInfo->count; count > 0; count--, nameInfo++)
225 BYTE *p = resTab + nameInfo->id;
226 if (nameInfo->id & 0x8000) continue;
227 if ((*p == len) && !strncasecmp( p+1, resid, len )) goto found_name;
230 else /* numeric resource id */
232 WORD id = LOWORD(resid) | 0x8000;
233 for (count = typeInfo->count; count > 0; count--, nameInfo++)
234 if (nameInfo->id == id) goto found_name;
236 TRACE("No resid entry found for %p\n", typeid );
237 HeapFree( GetProcessHeap(), 0, resTab );
241 /* Return resource data */
242 if ( resLen ) *resLen = nameInfo->length << *(WORD *)resTab;
243 if ( resOff ) *resOff = nameInfo->offset << *(WORD *)resTab;
245 HeapFree( GetProcessHeap(), 0, resTab );
249 /***********************************************************************
250 * load_pe_resource [internal]
252 static BOOL find_pe_resource( HFILE lzfd, LPCSTR typeid, LPCSTR resid,
253 DWORD *resLen, DWORD *resOff )
255 IMAGE_NT_HEADERS pehd;
257 PIMAGE_DATA_DIRECTORY resDataDir;
258 PIMAGE_SECTION_HEADER sections;
260 DWORD resSectionSize;
262 const IMAGE_RESOURCE_DIRECTORY *resPtr;
263 const IMAGE_RESOURCE_DATA_ENTRY *resData;
267 /* Read in PE header */
268 pehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
269 if ( sizeof(pehd) != LZRead( lzfd, &pehd, sizeof(pehd) ) ) return 0;
271 resDataDir = pehd.OptionalHeader.DataDirectory+IMAGE_FILE_RESOURCE_DIRECTORY;
272 if ( !resDataDir->Size )
274 TRACE("No resources in PE dll\n" );
278 /* Read in section table */
279 nSections = pehd.FileHeader.NumberOfSections;
280 sections = HeapAlloc( GetProcessHeap(), 0,
281 nSections * sizeof(IMAGE_SECTION_HEADER) );
282 if ( !sections ) return FALSE;
284 LZSeek( lzfd, pehdoffset +
285 sizeof(DWORD) + /* Signature */
286 sizeof(IMAGE_FILE_HEADER) +
287 pehd.FileHeader.SizeOfOptionalHeader, SEEK_SET );
289 if ( nSections * sizeof(IMAGE_SECTION_HEADER) !=
290 LZRead( lzfd, sections, nSections * sizeof(IMAGE_SECTION_HEADER) ) )
292 HeapFree( GetProcessHeap(), 0, sections );
296 /* Find resource section */
297 for ( i = 0; i < nSections; i++ )
298 if ( resDataDir->VirtualAddress >= sections[i].VirtualAddress
299 && resDataDir->VirtualAddress < sections[i].VirtualAddress +
300 sections[i].SizeOfRawData )
303 if ( i == nSections )
305 HeapFree( GetProcessHeap(), 0, sections );
306 TRACE("Couldn't find resource section\n" );
310 /* Read in resource section */
311 resSectionSize = sections[i].SizeOfRawData;
312 resSection = HeapAlloc( GetProcessHeap(), 0, resSectionSize );
315 HeapFree( GetProcessHeap(), 0, sections );
319 LZSeek( lzfd, sections[i].PointerToRawData, SEEK_SET );
320 if ( resSectionSize != LZRead( lzfd, resSection, resSectionSize ) ) goto done;
323 resDir = resSection + (resDataDir->VirtualAddress - sections[i].VirtualAddress);
325 resPtr = (PIMAGE_RESOURCE_DIRECTORY)resDir;
326 resPtr = find_entry_by_name( resPtr, typeid, resDir );
329 TRACE("No typeid entry found for %p\n", typeid );
332 resPtr = find_entry_by_name( resPtr, resid, resDir );
335 TRACE("No resid entry found for %p\n", resid );
338 resPtr = find_entry_default( resPtr, resDir );
341 TRACE("No default language entry found for %p\n", resid );
345 /* Find resource data section */
346 resData = (PIMAGE_RESOURCE_DATA_ENTRY)resPtr;
347 for ( i = 0; i < nSections; i++ )
348 if ( resData->OffsetToData >= sections[i].VirtualAddress
349 && resData->OffsetToData < sections[i].VirtualAddress +
350 sections[i].SizeOfRawData )
353 if ( i == nSections )
355 TRACE("Couldn't find resource data section\n" );
359 /* Return resource data */
360 if ( resLen ) *resLen = resData->Size;
361 if ( resOff ) *resOff = resData->OffsetToData - sections[i].VirtualAddress
362 + sections[i].PointerToRawData;
366 HeapFree( GetProcessHeap(), 0, resSection );
367 HeapFree( GetProcessHeap(), 0, sections );
372 /*************************************************************************
373 * GetFileResourceSize16 [VER.2]
375 DWORD WINAPI GetFileResourceSize16( LPCSTR lpszFileName, LPCSTR lpszResType,
376 LPCSTR lpszResId, LPDWORD lpdwFileOffset )
383 TRACE("(%s,type=0x%lx,id=0x%lx,off=%p)\n",
384 debugstr_a(lpszFileName), (LONG)lpszResType, (LONG)lpszResId,
387 lzfd = LZOpenFileA( lpszFileName, &ofs, OF_READ );
388 if ( !lzfd ) return 0;
390 switch ( read_xx_header( lzfd ) )
392 case IMAGE_OS2_SIGNATURE:
393 retv = find_ne_resource( lzfd, lpszResType, lpszResId,
394 &reslen, lpdwFileOffset );
397 case IMAGE_NT_SIGNATURE:
398 retv = find_pe_resource( lzfd, lpszResType, lpszResId,
399 &reslen, lpdwFileOffset );
404 return retv? reslen : 0;
408 /*************************************************************************
409 * GetFileResource16 [VER.3]
411 DWORD WINAPI GetFileResource16( LPCSTR lpszFileName, LPCSTR lpszResType,
412 LPCSTR lpszResId, DWORD dwFileOffset,
413 DWORD dwResLen, LPVOID lpvData )
418 DWORD reslen = dwResLen;
420 TRACE("(%s,type=0x%lx,id=0x%lx,off=%ld,len=%ld,data=%p)\n",
421 debugstr_a(lpszFileName), (LONG)lpszResType, (LONG)lpszResId,
422 dwFileOffset, dwResLen, lpvData );
424 lzfd = LZOpenFileA( lpszFileName, &ofs, OF_READ );
425 if ( lzfd == 0 ) return 0;
429 switch ( read_xx_header( lzfd ) )
431 case IMAGE_OS2_SIGNATURE:
432 retv = find_ne_resource( lzfd, lpszResType, lpszResId,
433 &reslen, &dwFileOffset );
436 case IMAGE_NT_SIGNATURE:
437 retv = find_pe_resource( lzfd, lpszResType, lpszResId,
438 &reslen, &dwFileOffset );
449 LZSeek( lzfd, dwFileOffset, SEEK_SET );
450 reslen = LZRead( lzfd, lpvData, min( reslen, dwResLen ) );