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"
42 #include "wine/winuser16.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(ver
);
49 /**********************************************************************
52 * Find an entry by id in a resource directory
53 * Copied from loader/pe_resource.c
55 static const IMAGE_RESOURCE_DIRECTORY
*find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY
*dir
,
56 WORD id
, const void *root
)
58 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
;
61 entry
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(dir
+ 1);
62 min
= dir
->NumberOfNamedEntries
;
63 max
= min
+ dir
->NumberOfIdEntries
- 1;
66 pos
= (min
+ max
) / 2;
67 if (entry
[pos
].u1
.s2
.Id
== id
)
68 return (const IMAGE_RESOURCE_DIRECTORY
*)((const char *)root
+ entry
[pos
].u2
.s3
.OffsetToDirectory
);
69 if (entry
[pos
].u1
.s2
.Id
> id
) max
= pos
- 1;
76 /**********************************************************************
79 * Find a default entry in a resource directory
80 * Copied from loader/pe_resource.c
82 static const IMAGE_RESOURCE_DIRECTORY
*find_entry_default( const IMAGE_RESOURCE_DIRECTORY
*dir
,
85 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
;
87 entry
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(dir
+ 1);
88 return (const IMAGE_RESOURCE_DIRECTORY
*)((const char *)root
+ entry
->u2
.s3
.OffsetToDirectory
);
92 /**********************************************************************
95 * Find an entry by name in a resource directory
96 * Copied from loader/pe_resource.c
98 static const IMAGE_RESOURCE_DIRECTORY
*find_entry_by_name( const IMAGE_RESOURCE_DIRECTORY
*dir
,
99 LPCSTR name
, const void *root
)
101 const IMAGE_RESOURCE_DIRECTORY
*ret
= NULL
;
105 if (!HIWORD(name
)) return find_entry_by_id( dir
, LOWORD(name
), root
);
108 return find_entry_by_id( dir
, atoi(name
+1), root
);
111 namelen
= MultiByteToWideChar( CP_ACP
, 0, name
, -1, NULL
, 0 );
112 if ((nameW
= HeapAlloc( GetProcessHeap(), 0, namelen
* sizeof(WCHAR
) )))
114 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
;
115 const IMAGE_RESOURCE_DIR_STRING_U
*str
;
116 int min
, max
, res
, pos
;
118 MultiByteToWideChar( CP_ACP
, 0, name
, -1, nameW
, namelen
);
119 namelen
--; /* remove terminating null */
120 entry
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(dir
+ 1);
122 max
= dir
->NumberOfNamedEntries
- 1;
125 pos
= (min
+ max
) / 2;
126 str
= (const IMAGE_RESOURCE_DIR_STRING_U
*)((const char *)root
+ entry
[pos
].u1
.s1
.NameOffset
);
127 res
= strncmpiW( nameW
, str
->NameString
, str
->Length
);
128 if (!res
&& namelen
== str
->Length
)
130 ret
= (const IMAGE_RESOURCE_DIRECTORY
*)((const char *)root
+ entry
[pos
].u2
.s3
.OffsetToDirectory
);
133 if (res
< 0) max
= pos
- 1;
136 HeapFree( GetProcessHeap(), 0, nameW
);
142 /***********************************************************************
143 * read_xx_header [internal]
145 static int read_xx_header( HFILE lzfd
)
147 IMAGE_DOS_HEADER mzh
;
150 LZSeek( lzfd
, 0, SEEK_SET
);
151 if ( sizeof(mzh
) != LZRead( lzfd
, (LPSTR
)&mzh
, sizeof(mzh
) ) )
153 if ( mzh
.e_magic
!= IMAGE_DOS_SIGNATURE
)
156 LZSeek( lzfd
, mzh
.e_lfanew
, SEEK_SET
);
157 if ( 2 != LZRead( lzfd
, magic
, 2 ) )
160 LZSeek( lzfd
, mzh
.e_lfanew
, SEEK_SET
);
162 if ( magic
[0] == 'N' && magic
[1] == 'E' )
163 return IMAGE_OS2_SIGNATURE
;
164 if ( magic
[0] == 'P' && magic
[1] == 'E' )
165 return IMAGE_NT_SIGNATURE
;
168 WARN("Can't handle %s files.\n", magic
);
172 /***********************************************************************
173 * load_ne_resource [internal]
175 static BOOL
find_ne_resource( HFILE lzfd
, LPCSTR
typeid, LPCSTR resid
,
176 DWORD
*resLen
, DWORD
*resOff
)
178 IMAGE_OS2_HEADER nehd
;
179 NE_TYPEINFO
*typeInfo
;
180 NE_NAMEINFO
*nameInfo
;
186 /* Read in NE header */
187 nehdoffset
= LZSeek( lzfd
, 0, SEEK_CUR
);
188 if ( sizeof(nehd
) != LZRead( lzfd
, (LPSTR
)&nehd
, sizeof(nehd
) ) ) return 0;
190 resTabSize
= nehd
.ne_restab
- nehd
.ne_rsrctab
;
193 TRACE("No resources in NE dll\n" );
197 /* Read in resource table */
198 resTab
= HeapAlloc( GetProcessHeap(), 0, resTabSize
);
199 if ( !resTab
) return FALSE
;
201 LZSeek( lzfd
, nehd
.ne_rsrctab
+ nehdoffset
, SEEK_SET
);
202 if ( resTabSize
!= LZRead( lzfd
, (char*)resTab
, resTabSize
) )
204 HeapFree( GetProcessHeap(), 0, resTab
);
209 typeInfo
= (NE_TYPEINFO
*)(resTab
+ 2);
211 if (HIWORD(typeid) != 0) /* named type */
213 BYTE len
= strlen( typeid );
214 while (typeInfo
->type_id
)
216 if (!(typeInfo
->type_id
& 0x8000))
218 BYTE
*p
= resTab
+ typeInfo
->type_id
;
219 if ((*p
== len
) && !strncasecmp( (char*)p
+1, typeid, len
)) goto found_type
;
221 typeInfo
= (NE_TYPEINFO
*)((char *)(typeInfo
+ 1) +
222 typeInfo
->count
* sizeof(NE_NAMEINFO
));
225 else /* numeric type id */
227 WORD id
= LOWORD(typeid) | 0x8000;
228 while (typeInfo
->type_id
)
230 if (typeInfo
->type_id
== id
) goto found_type
;
231 typeInfo
= (NE_TYPEINFO
*)((char *)(typeInfo
+ 1) +
232 typeInfo
->count
* sizeof(NE_NAMEINFO
));
235 TRACE("No typeid entry found for %p\n", typeid );
236 HeapFree( GetProcessHeap(), 0, resTab
);
240 nameInfo
= (NE_NAMEINFO
*)(typeInfo
+ 1);
242 if (HIWORD(resid
) != 0) /* named resource */
244 BYTE len
= strlen( resid
);
245 for (count
= typeInfo
->count
; count
> 0; count
--, nameInfo
++)
247 BYTE
*p
= resTab
+ nameInfo
->id
;
248 if (nameInfo
->id
& 0x8000) continue;
249 if ((*p
== len
) && !strncasecmp( (char*)p
+1, resid
, len
)) goto found_name
;
252 else /* numeric resource id */
254 WORD id
= LOWORD(resid
) | 0x8000;
255 for (count
= typeInfo
->count
; count
> 0; count
--, nameInfo
++)
256 if (nameInfo
->id
== id
) goto found_name
;
258 TRACE("No resid entry found for %p\n", typeid );
259 HeapFree( GetProcessHeap(), 0, resTab
);
263 /* Return resource data */
264 if ( resLen
) *resLen
= nameInfo
->length
<< *(WORD
*)resTab
;
265 if ( resOff
) *resOff
= nameInfo
->offset
<< *(WORD
*)resTab
;
267 HeapFree( GetProcessHeap(), 0, resTab
);
271 /***********************************************************************
272 * load_pe_resource [internal]
274 static BOOL
find_pe_resource( HFILE lzfd
, LPCSTR
typeid, LPCSTR resid
,
275 DWORD
*resLen
, DWORD
*resOff
)
277 IMAGE_NT_HEADERS pehd
;
279 PIMAGE_DATA_DIRECTORY resDataDir
;
280 PIMAGE_SECTION_HEADER sections
;
282 DWORD resSectionSize
;
284 const IMAGE_RESOURCE_DIRECTORY
*resPtr
;
285 const IMAGE_RESOURCE_DATA_ENTRY
*resData
;
289 /* Read in PE header */
290 pehdoffset
= LZSeek( lzfd
, 0, SEEK_CUR
);
291 if ( sizeof(pehd
) != LZRead( lzfd
, (LPSTR
)&pehd
, sizeof(pehd
) ) ) return 0;
293 resDataDir
= pehd
.OptionalHeader
.DataDirectory
+IMAGE_FILE_RESOURCE_DIRECTORY
;
294 if ( !resDataDir
->Size
)
296 TRACE("No resources in PE dll\n" );
300 /* Read in section table */
301 nSections
= pehd
.FileHeader
.NumberOfSections
;
302 sections
= HeapAlloc( GetProcessHeap(), 0,
303 nSections
* sizeof(IMAGE_SECTION_HEADER
) );
304 if ( !sections
) return FALSE
;
306 LZSeek( lzfd
, pehdoffset
+
307 sizeof(DWORD
) + /* Signature */
308 sizeof(IMAGE_FILE_HEADER
) +
309 pehd
.FileHeader
.SizeOfOptionalHeader
, SEEK_SET
);
311 if ( nSections
* sizeof(IMAGE_SECTION_HEADER
) !=
312 LZRead( lzfd
, (LPSTR
)sections
, nSections
* sizeof(IMAGE_SECTION_HEADER
) ) )
314 HeapFree( GetProcessHeap(), 0, sections
);
318 /* Find resource section */
319 for ( i
= 0; i
< nSections
; i
++ )
320 if ( resDataDir
->VirtualAddress
>= sections
[i
].VirtualAddress
321 && resDataDir
->VirtualAddress
< sections
[i
].VirtualAddress
+
322 sections
[i
].SizeOfRawData
)
325 if ( i
== nSections
)
327 HeapFree( GetProcessHeap(), 0, sections
);
328 TRACE("Couldn't find resource section\n" );
332 /* Read in resource section */
333 resSectionSize
= sections
[i
].SizeOfRawData
;
334 resSection
= HeapAlloc( GetProcessHeap(), 0, resSectionSize
);
337 HeapFree( GetProcessHeap(), 0, sections
);
341 LZSeek( lzfd
, sections
[i
].PointerToRawData
, SEEK_SET
);
342 if ( resSectionSize
!= LZRead( lzfd
, (char*)resSection
, resSectionSize
) ) goto done
;
345 resDir
= resSection
+ (resDataDir
->VirtualAddress
- sections
[i
].VirtualAddress
);
347 resPtr
= (const IMAGE_RESOURCE_DIRECTORY
*)resDir
;
348 resPtr
= find_entry_by_name( resPtr
, typeid, resDir
);
351 TRACE("No typeid entry found for %p\n", typeid );
354 resPtr
= find_entry_by_name( resPtr
, resid
, resDir
);
357 TRACE("No resid entry found for %p\n", resid
);
360 resPtr
= find_entry_default( resPtr
, resDir
);
363 TRACE("No default language entry found for %p\n", resid
);
367 /* Find resource data section */
368 resData
= (const IMAGE_RESOURCE_DATA_ENTRY
*)resPtr
;
369 for ( i
= 0; i
< nSections
; i
++ )
370 if ( resData
->OffsetToData
>= sections
[i
].VirtualAddress
371 && resData
->OffsetToData
< sections
[i
].VirtualAddress
+
372 sections
[i
].SizeOfRawData
)
375 if ( i
== nSections
)
377 TRACE("Couldn't find resource data section\n" );
381 /* Return resource data */
382 if ( resLen
) *resLen
= resData
->Size
;
383 if ( resOff
) *resOff
= resData
->OffsetToData
- sections
[i
].VirtualAddress
384 + sections
[i
].PointerToRawData
;
388 HeapFree( GetProcessHeap(), 0, resSection
);
389 HeapFree( GetProcessHeap(), 0, sections
);
394 /*************************************************************************
395 * GetFileResourceSize [VER.2]
397 DWORD WINAPI
GetFileResourceSize16( LPCSTR lpszFileName
, LPCSTR lpszResType
,
398 LPCSTR lpszResId
, LPDWORD lpdwFileOffset
)
405 TRACE("(%s,type=0x%x,id=0x%x,off=%p)\n",
406 debugstr_a(lpszFileName
), (LONG
)lpszResType
, (LONG
)lpszResId
,
409 lzfd
= LZOpenFileA( (LPSTR
)lpszFileName
, &ofs
, OF_READ
);
410 if ( lzfd
< 0 ) return 0;
412 switch ( read_xx_header( lzfd
) )
414 case IMAGE_OS2_SIGNATURE
:
415 retv
= find_ne_resource( lzfd
, lpszResType
, lpszResId
,
416 &reslen
, lpdwFileOffset
);
419 case IMAGE_NT_SIGNATURE
:
420 retv
= find_pe_resource( lzfd
, lpszResType
, lpszResId
,
421 &reslen
, lpdwFileOffset
);
426 return retv
? reslen
: 0;
430 /*************************************************************************
431 * GetFileResource [VER.3]
433 DWORD WINAPI
GetFileResource16( LPCSTR lpszFileName
, LPCSTR lpszResType
,
434 LPCSTR lpszResId
, DWORD dwFileOffset
,
435 DWORD dwResLen
, LPVOID lpvData
)
440 DWORD reslen
= dwResLen
;
442 TRACE("(%s,type=%p,id=%p,off=%d,len=%d,data=%p)\n",
443 debugstr_a(lpszFileName
), lpszResType
, lpszResId
,
444 dwFileOffset
, dwResLen
, lpvData
);
446 lzfd
= LZOpenFileA( (LPSTR
)lpszFileName
, &ofs
, OF_READ
);
447 if ( lzfd
< 0 ) return 0;
451 switch ( read_xx_header( lzfd
) )
453 case IMAGE_OS2_SIGNATURE
:
454 retv
= find_ne_resource( lzfd
, lpszResType
, lpszResId
,
455 &reslen
, &dwFileOffset
);
458 case IMAGE_NT_SIGNATURE
:
459 retv
= find_pe_resource( lzfd
, lpszResType
, lpszResId
,
460 &reslen
, &dwFileOffset
);
471 LZSeek( lzfd
, dwFileOffset
, SEEK_SET
);
472 reslen
= LZRead( lzfd
, lpvData
, min( reslen
, dwResLen
) );