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>
15 #include "wine/winbase16.h"
17 #include "wine/unicode.h"
21 #include "debugtools.h"
23 DEFAULT_DEBUG_CHANNEL(ver);
26 /**********************************************************************
29 * Find an entry by id in a resource directory
30 * Copied from loader/pe_resource.c
32 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
33 WORD id, const void *root )
35 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
38 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
39 min = dir->NumberOfNamedEntries;
40 max = min + dir->NumberOfIdEntries - 1;
43 pos = (min + max) / 2;
44 if (entry[pos].u1.s2.Id == id)
45 return (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry[pos].u2.s3.OffsetToDirectory);
46 if (entry[pos].u1.s2.Id > id) max = pos - 1;
53 /**********************************************************************
56 * Find a default entry in a resource directory
57 * Copied from loader/pe_resource.c
59 static const IMAGE_RESOURCE_DIRECTORY *find_entry_default( const IMAGE_RESOURCE_DIRECTORY *dir,
62 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
64 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
65 return (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry->u2.s3.OffsetToDirectory);
69 /**********************************************************************
72 * Find an entry by name in a resource directory
73 * Copied from loader/pe_resource.c
75 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_name( const IMAGE_RESOURCE_DIRECTORY *dir,
76 LPCSTR name, const void *root )
78 const IMAGE_RESOURCE_DIRECTORY *ret = NULL;
82 if (!HIWORD(name)) return find_entry_by_id( dir, LOWORD(name), root );
85 return find_entry_by_id( dir, atoi(name+1), root );
88 namelen = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
89 if ((nameW = HeapAlloc( GetProcessHeap(), 0, namelen * sizeof(WCHAR) )))
91 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
92 const IMAGE_RESOURCE_DIR_STRING_U *str;
93 int min, max, res, pos;
95 MultiByteToWideChar( CP_ACP, 0, name, -1, nameW, namelen );
96 namelen--; /* remove terminating null */
97 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
99 max = dir->NumberOfNamedEntries - 1;
102 pos = (min + max) / 2;
103 str = (IMAGE_RESOURCE_DIR_STRING_U *)((char *)root + entry[pos].u1.s1.NameOffset);
104 res = strncmpiW( nameW, str->NameString, str->Length );
105 if (!res && namelen == str->Length)
107 ret = (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry[pos].u2.s3.OffsetToDirectory);
110 if (res < 0) max = pos - 1;
113 HeapFree( GetProcessHeap(), 0, nameW );
119 /***********************************************************************
120 * read_xx_header [internal]
122 static int read_xx_header( HFILE lzfd )
124 IMAGE_DOS_HEADER mzh;
127 LZSeek( lzfd, 0, SEEK_SET );
128 if ( sizeof(mzh) != LZRead( lzfd, &mzh, sizeof(mzh) ) )
130 if ( mzh.e_magic != IMAGE_DOS_SIGNATURE )
133 LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
134 if ( 2 != LZRead( lzfd, magic, 2 ) )
137 LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
139 if ( magic[0] == 'N' && magic[1] == 'E' )
140 return IMAGE_OS2_SIGNATURE;
141 if ( magic[0] == 'P' && magic[1] == 'E' )
142 return IMAGE_NT_SIGNATURE;
145 WARN("Can't handle %s files.\n", magic );
149 /***********************************************************************
150 * load_ne_resource [internal]
152 static BOOL find_ne_resource( HFILE lzfd, LPCSTR typeid, LPCSTR resid,
153 DWORD *resLen, DWORD *resOff )
155 IMAGE_OS2_HEADER nehd;
156 NE_TYPEINFO *typeInfo;
157 NE_NAMEINFO *nameInfo;
163 /* Read in NE header */
164 nehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
165 if ( sizeof(nehd) != LZRead( lzfd, &nehd, sizeof(nehd) ) ) return 0;
167 resTabSize = nehd.ne_restab - nehd.ne_rsrctab;
170 TRACE("No resources in NE dll\n" );
174 /* Read in resource table */
175 resTab = HeapAlloc( GetProcessHeap(), 0, resTabSize );
176 if ( !resTab ) return FALSE;
178 LZSeek( lzfd, nehd.ne_rsrctab + nehdoffset, SEEK_SET );
179 if ( resTabSize != LZRead( lzfd, resTab, resTabSize ) )
181 HeapFree( GetProcessHeap(), 0, resTab );
186 typeInfo = (NE_TYPEINFO *)(resTab + 2);
188 if (HIWORD(typeid) != 0) /* named type */
190 BYTE len = strlen( typeid );
191 while (typeInfo->type_id)
193 if (!(typeInfo->type_id & 0x8000))
195 BYTE *p = resTab + typeInfo->type_id;
196 if ((*p == len) && !strncasecmp( p+1, typeid, len )) goto found_type;
198 typeInfo = (NE_TYPEINFO *)((char *)(typeInfo + 1) +
199 typeInfo->count * sizeof(NE_NAMEINFO));
202 else /* numeric type id */
204 WORD id = LOWORD(typeid) | 0x8000;
205 while (typeInfo->type_id)
207 if (typeInfo->type_id == id) goto found_type;
208 typeInfo = (NE_TYPEINFO *)((char *)(typeInfo + 1) +
209 typeInfo->count * sizeof(NE_NAMEINFO));
212 TRACE("No typeid entry found for %p\n", typeid );
213 HeapFree( GetProcessHeap(), 0, resTab );
217 nameInfo = (NE_NAMEINFO *)(typeInfo + 1);
219 if (HIWORD(resid) != 0) /* named resource */
221 BYTE len = strlen( resid );
222 for (count = typeInfo->count; count > 0; count--, nameInfo++)
224 BYTE *p = resTab + nameInfo->id;
225 if (nameInfo->id & 0x8000) continue;
226 if ((*p == len) && !strncasecmp( p+1, resid, len )) goto found_name;
229 else /* numeric resource id */
231 WORD id = LOWORD(resid) | 0x8000;
232 for (count = typeInfo->count; count > 0; count--, nameInfo++)
233 if (nameInfo->id == id) goto found_name;
235 TRACE("No resid entry found for %p\n", typeid );
236 HeapFree( GetProcessHeap(), 0, resTab );
240 /* Return resource data */
241 if ( resLen ) *resLen = nameInfo->length << *(WORD *)resTab;
242 if ( resOff ) *resOff = nameInfo->offset << *(WORD *)resTab;
244 HeapFree( GetProcessHeap(), 0, resTab );
248 /***********************************************************************
249 * load_pe_resource [internal]
251 static BOOL find_pe_resource( HFILE lzfd, LPCSTR typeid, LPCSTR resid,
252 DWORD *resLen, DWORD *resOff )
254 IMAGE_NT_HEADERS pehd;
256 PIMAGE_DATA_DIRECTORY resDataDir;
257 PIMAGE_SECTION_HEADER sections;
259 DWORD resSectionSize;
261 const IMAGE_RESOURCE_DIRECTORY *resPtr;
262 const IMAGE_RESOURCE_DATA_ENTRY *resData;
266 /* Read in PE header */
267 pehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
268 if ( sizeof(pehd) != LZRead( lzfd, &pehd, sizeof(pehd) ) ) return 0;
270 resDataDir = pehd.OptionalHeader.DataDirectory+IMAGE_FILE_RESOURCE_DIRECTORY;
271 if ( !resDataDir->Size )
273 TRACE("No resources in PE dll\n" );
277 /* Read in section table */
278 nSections = pehd.FileHeader.NumberOfSections;
279 sections = HeapAlloc( GetProcessHeap(), 0,
280 nSections * sizeof(IMAGE_SECTION_HEADER) );
281 if ( !sections ) return FALSE;
283 LZSeek( lzfd, pehdoffset +
284 sizeof(DWORD) + /* Signature */
285 sizeof(IMAGE_FILE_HEADER) +
286 pehd.FileHeader.SizeOfOptionalHeader, SEEK_SET );
288 if ( nSections * sizeof(IMAGE_SECTION_HEADER) !=
289 LZRead( lzfd, sections, nSections * sizeof(IMAGE_SECTION_HEADER) ) )
291 HeapFree( GetProcessHeap(), 0, sections );
295 /* Find resource section */
296 for ( i = 0; i < nSections; i++ )
297 if ( resDataDir->VirtualAddress >= sections[i].VirtualAddress
298 && resDataDir->VirtualAddress < sections[i].VirtualAddress +
299 sections[i].SizeOfRawData )
302 if ( i == nSections )
304 HeapFree( GetProcessHeap(), 0, sections );
305 TRACE("Couldn't find resource section\n" );
309 /* Read in resource section */
310 resSectionSize = sections[i].SizeOfRawData;
311 resSection = HeapAlloc( GetProcessHeap(), 0, resSectionSize );
314 HeapFree( GetProcessHeap(), 0, sections );
318 LZSeek( lzfd, sections[i].PointerToRawData, SEEK_SET );
319 if ( resSectionSize != LZRead( lzfd, resSection, resSectionSize ) ) goto done;
322 resDir = resSection + (resDataDir->VirtualAddress - sections[i].VirtualAddress);
324 resPtr = (PIMAGE_RESOURCE_DIRECTORY)resDir;
325 resPtr = find_entry_by_name( resPtr, typeid, resDir );
328 TRACE("No typeid entry found for %p\n", typeid );
331 resPtr = find_entry_by_name( resPtr, resid, resDir );
334 TRACE("No resid entry found for %p\n", resid );
337 resPtr = find_entry_default( resPtr, resDir );
340 TRACE("No default language entry found for %p\n", resid );
344 /* Find resource data section */
345 resData = (PIMAGE_RESOURCE_DATA_ENTRY)resPtr;
346 for ( i = 0; i < nSections; i++ )
347 if ( resData->OffsetToData >= sections[i].VirtualAddress
348 && resData->OffsetToData < sections[i].VirtualAddress +
349 sections[i].SizeOfRawData )
352 if ( i == nSections )
354 TRACE("Couldn't find resource data section\n" );
358 /* Return resource data */
359 if ( resLen ) *resLen = resData->Size;
360 if ( resOff ) *resOff = resData->OffsetToData - sections[i].VirtualAddress
361 + sections[i].PointerToRawData;
365 HeapFree( GetProcessHeap(), 0, resSection );
366 HeapFree( GetProcessHeap(), 0, sections );
371 /*************************************************************************
372 * GetFileResourceSize16 [VER.2]
374 DWORD WINAPI GetFileResourceSize16( LPCSTR lpszFileName, LPCSTR lpszResType,
375 LPCSTR lpszResId, LPDWORD lpdwFileOffset )
382 TRACE("(%s,type=0x%lx,id=0x%lx,off=%p)\n",
383 debugstr_a(lpszFileName), (LONG)lpszResType, (LONG)lpszResId,
386 lzfd = LZOpenFileA( lpszFileName, &ofs, OF_READ );
387 if ( !lzfd ) return 0;
389 switch ( read_xx_header( lzfd ) )
391 case IMAGE_OS2_SIGNATURE:
392 retv = find_ne_resource( lzfd, lpszResType, lpszResId,
393 &reslen, lpdwFileOffset );
396 case IMAGE_NT_SIGNATURE:
397 retv = find_pe_resource( lzfd, lpszResType, lpszResId,
398 &reslen, lpdwFileOffset );
403 return retv? reslen : 0;
407 /*************************************************************************
408 * GetFileResource16 [VER.3]
410 DWORD WINAPI GetFileResource16( LPCSTR lpszFileName, LPCSTR lpszResType,
411 LPCSTR lpszResId, DWORD dwFileOffset,
412 DWORD dwResLen, LPVOID lpvData )
417 DWORD reslen = dwResLen;
419 TRACE("(%s,type=0x%lx,id=0x%lx,off=%ld,len=%ld,data=%p)\n",
420 debugstr_a(lpszFileName), (LONG)lpszResType, (LONG)lpszResId,
421 dwFileOffset, dwResLen, lpvData );
423 lzfd = LZOpenFileA( lpszFileName, &ofs, OF_READ );
424 if ( lzfd == 0 ) return 0;
428 switch ( read_xx_header( lzfd ) )
430 case IMAGE_OS2_SIGNATURE:
431 retv = find_ne_resource( lzfd, lpszResType, lpszResId,
432 &reslen, &dwFileOffset );
435 case IMAGE_NT_SIGNATURE:
436 retv = find_pe_resource( lzfd, lpszResType, lpszResId,
437 &reslen, &dwFileOffset );
448 LZSeek( lzfd, dwFileOffset, SEEK_SET );
449 reslen = LZRead( lzfd, lpvData, min( reslen, dwResLen ) );