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>
33 #include "wine/unicode.h"
34 #include "wine/winbase16.h"
35 #include "wine/winuser16.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(ver
);
43 /**********************************************************************
46 * Find an entry by id in a resource directory
47 * Copied from loader/pe_resource.c
49 static const IMAGE_RESOURCE_DIRECTORY
*find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY
*dir
,
50 WORD id
, const void *root
)
52 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
;
55 entry
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(dir
+ 1);
56 min
= dir
->NumberOfNamedEntries
;
57 max
= min
+ dir
->NumberOfIdEntries
- 1;
60 pos
= (min
+ max
) / 2;
61 if (entry
[pos
].u1
.s2
.Id
== id
)
62 return (IMAGE_RESOURCE_DIRECTORY
*)((char *)root
+ entry
[pos
].u2
.s3
.OffsetToDirectory
);
63 if (entry
[pos
].u1
.s2
.Id
> id
) max
= pos
- 1;
70 /**********************************************************************
73 * Find a default entry in a resource directory
74 * Copied from loader/pe_resource.c
76 static const IMAGE_RESOURCE_DIRECTORY
*find_entry_default( const IMAGE_RESOURCE_DIRECTORY
*dir
,
79 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
;
81 entry
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(dir
+ 1);
82 return (IMAGE_RESOURCE_DIRECTORY
*)((char *)root
+ entry
->u2
.s3
.OffsetToDirectory
);
86 /**********************************************************************
89 * Find an entry by name in a resource directory
90 * Copied from loader/pe_resource.c
92 static const IMAGE_RESOURCE_DIRECTORY
*find_entry_by_name( const IMAGE_RESOURCE_DIRECTORY
*dir
,
93 LPCSTR name
, const void *root
)
95 const IMAGE_RESOURCE_DIRECTORY
*ret
= NULL
;
99 if (!HIWORD(name
)) return find_entry_by_id( dir
, LOWORD(name
), root
);
102 return find_entry_by_id( dir
, atoi(name
+1), root
);
105 namelen
= MultiByteToWideChar( CP_ACP
, 0, name
, -1, NULL
, 0 );
106 if ((nameW
= HeapAlloc( GetProcessHeap(), 0, namelen
* sizeof(WCHAR
) )))
108 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
;
109 const IMAGE_RESOURCE_DIR_STRING_U
*str
;
110 int min
, max
, res
, pos
;
112 MultiByteToWideChar( CP_ACP
, 0, name
, -1, nameW
, namelen
);
113 namelen
--; /* remove terminating null */
114 entry
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(dir
+ 1);
116 max
= dir
->NumberOfNamedEntries
- 1;
119 pos
= (min
+ max
) / 2;
120 str
= (IMAGE_RESOURCE_DIR_STRING_U
*)((char *)root
+ entry
[pos
].u1
.s1
.NameOffset
);
121 res
= strncmpiW( nameW
, str
->NameString
, str
->Length
);
122 if (!res
&& namelen
== str
->Length
)
124 ret
= (IMAGE_RESOURCE_DIRECTORY
*)((char *)root
+ entry
[pos
].u2
.s3
.OffsetToDirectory
);
127 if (res
< 0) max
= pos
- 1;
130 HeapFree( GetProcessHeap(), 0, nameW
);
136 /***********************************************************************
137 * read_xx_header [internal]
139 static int read_xx_header( HFILE lzfd
)
141 IMAGE_DOS_HEADER mzh
;
144 LZSeek( lzfd
, 0, SEEK_SET
);
145 if ( sizeof(mzh
) != LZRead( lzfd
, &mzh
, sizeof(mzh
) ) )
147 if ( mzh
.e_magic
!= IMAGE_DOS_SIGNATURE
)
150 LZSeek( lzfd
, mzh
.e_lfanew
, SEEK_SET
);
151 if ( 2 != LZRead( lzfd
, magic
, 2 ) )
154 LZSeek( lzfd
, mzh
.e_lfanew
, SEEK_SET
);
156 if ( magic
[0] == 'N' && magic
[1] == 'E' )
157 return IMAGE_OS2_SIGNATURE
;
158 if ( magic
[0] == 'P' && magic
[1] == 'E' )
159 return IMAGE_NT_SIGNATURE
;
162 WARN("Can't handle %s files.\n", magic
);
166 /***********************************************************************
167 * load_ne_resource [internal]
169 static BOOL
find_ne_resource( HFILE lzfd
, LPCSTR
typeid, LPCSTR resid
,
170 DWORD
*resLen
, DWORD
*resOff
)
172 IMAGE_OS2_HEADER nehd
;
173 NE_TYPEINFO
*typeInfo
;
174 NE_NAMEINFO
*nameInfo
;
180 /* Read in NE header */
181 nehdoffset
= LZSeek( lzfd
, 0, SEEK_CUR
);
182 if ( sizeof(nehd
) != LZRead( lzfd
, &nehd
, sizeof(nehd
) ) ) return 0;
184 resTabSize
= nehd
.ne_restab
- nehd
.ne_rsrctab
;
187 TRACE("No resources in NE dll\n" );
191 /* Read in resource table */
192 resTab
= HeapAlloc( GetProcessHeap(), 0, resTabSize
);
193 if ( !resTab
) return FALSE
;
195 LZSeek( lzfd
, nehd
.ne_rsrctab
+ nehdoffset
, SEEK_SET
);
196 if ( resTabSize
!= LZRead( lzfd
, resTab
, resTabSize
) )
198 HeapFree( GetProcessHeap(), 0, resTab
);
203 typeInfo
= (NE_TYPEINFO
*)(resTab
+ 2);
205 if (HIWORD(typeid) != 0) /* named type */
207 BYTE len
= strlen( typeid );
208 while (typeInfo
->type_id
)
210 if (!(typeInfo
->type_id
& 0x8000))
212 BYTE
*p
= resTab
+ typeInfo
->type_id
;
213 if ((*p
== len
) && !strncasecmp( p
+1, typeid, len
)) goto found_type
;
215 typeInfo
= (NE_TYPEINFO
*)((char *)(typeInfo
+ 1) +
216 typeInfo
->count
* sizeof(NE_NAMEINFO
));
219 else /* numeric type id */
221 WORD id
= LOWORD(typeid) | 0x8000;
222 while (typeInfo
->type_id
)
224 if (typeInfo
->type_id
== id
) goto found_type
;
225 typeInfo
= (NE_TYPEINFO
*)((char *)(typeInfo
+ 1) +
226 typeInfo
->count
* sizeof(NE_NAMEINFO
));
229 TRACE("No typeid entry found for %p\n", typeid );
230 HeapFree( GetProcessHeap(), 0, resTab
);
234 nameInfo
= (NE_NAMEINFO
*)(typeInfo
+ 1);
236 if (HIWORD(resid
) != 0) /* named resource */
238 BYTE len
= strlen( resid
);
239 for (count
= typeInfo
->count
; count
> 0; count
--, nameInfo
++)
241 BYTE
*p
= resTab
+ nameInfo
->id
;
242 if (nameInfo
->id
& 0x8000) continue;
243 if ((*p
== len
) && !strncasecmp( p
+1, resid
, len
)) goto found_name
;
246 else /* numeric resource id */
248 WORD id
= LOWORD(resid
) | 0x8000;
249 for (count
= typeInfo
->count
; count
> 0; count
--, nameInfo
++)
250 if (nameInfo
->id
== id
) goto found_name
;
252 TRACE("No resid entry found for %p\n", typeid );
253 HeapFree( GetProcessHeap(), 0, resTab
);
257 /* Return resource data */
258 if ( resLen
) *resLen
= nameInfo
->length
<< *(WORD
*)resTab
;
259 if ( resOff
) *resOff
= nameInfo
->offset
<< *(WORD
*)resTab
;
261 HeapFree( GetProcessHeap(), 0, resTab
);
265 /***********************************************************************
266 * load_pe_resource [internal]
268 static BOOL
find_pe_resource( HFILE lzfd
, LPCSTR
typeid, LPCSTR resid
,
269 DWORD
*resLen
, DWORD
*resOff
)
271 IMAGE_NT_HEADERS pehd
;
273 PIMAGE_DATA_DIRECTORY resDataDir
;
274 PIMAGE_SECTION_HEADER sections
;
276 DWORD resSectionSize
;
278 const IMAGE_RESOURCE_DIRECTORY
*resPtr
;
279 const IMAGE_RESOURCE_DATA_ENTRY
*resData
;
283 /* Read in PE header */
284 pehdoffset
= LZSeek( lzfd
, 0, SEEK_CUR
);
285 if ( sizeof(pehd
) != LZRead( lzfd
, &pehd
, sizeof(pehd
) ) ) return 0;
287 resDataDir
= pehd
.OptionalHeader
.DataDirectory
+IMAGE_FILE_RESOURCE_DIRECTORY
;
288 if ( !resDataDir
->Size
)
290 TRACE("No resources in PE dll\n" );
294 /* Read in section table */
295 nSections
= pehd
.FileHeader
.NumberOfSections
;
296 sections
= HeapAlloc( GetProcessHeap(), 0,
297 nSections
* sizeof(IMAGE_SECTION_HEADER
) );
298 if ( !sections
) return FALSE
;
300 LZSeek( lzfd
, pehdoffset
+
301 sizeof(DWORD
) + /* Signature */
302 sizeof(IMAGE_FILE_HEADER
) +
303 pehd
.FileHeader
.SizeOfOptionalHeader
, SEEK_SET
);
305 if ( nSections
* sizeof(IMAGE_SECTION_HEADER
) !=
306 LZRead( lzfd
, sections
, nSections
* sizeof(IMAGE_SECTION_HEADER
) ) )
308 HeapFree( GetProcessHeap(), 0, sections
);
312 /* Find resource section */
313 for ( i
= 0; i
< nSections
; i
++ )
314 if ( resDataDir
->VirtualAddress
>= sections
[i
].VirtualAddress
315 && resDataDir
->VirtualAddress
< sections
[i
].VirtualAddress
+
316 sections
[i
].SizeOfRawData
)
319 if ( i
== nSections
)
321 HeapFree( GetProcessHeap(), 0, sections
);
322 TRACE("Couldn't find resource section\n" );
326 /* Read in resource section */
327 resSectionSize
= sections
[i
].SizeOfRawData
;
328 resSection
= HeapAlloc( GetProcessHeap(), 0, resSectionSize
);
331 HeapFree( GetProcessHeap(), 0, sections
);
335 LZSeek( lzfd
, sections
[i
].PointerToRawData
, SEEK_SET
);
336 if ( resSectionSize
!= LZRead( lzfd
, resSection
, resSectionSize
) ) goto done
;
339 resDir
= resSection
+ (resDataDir
->VirtualAddress
- sections
[i
].VirtualAddress
);
341 resPtr
= (PIMAGE_RESOURCE_DIRECTORY
)resDir
;
342 resPtr
= find_entry_by_name( resPtr
, typeid, resDir
);
345 TRACE("No typeid entry found for %p\n", typeid );
348 resPtr
= find_entry_by_name( resPtr
, resid
, resDir
);
351 TRACE("No resid entry found for %p\n", resid
);
354 resPtr
= find_entry_default( resPtr
, resDir
);
357 TRACE("No default language entry found for %p\n", resid
);
361 /* Find resource data section */
362 resData
= (PIMAGE_RESOURCE_DATA_ENTRY
)resPtr
;
363 for ( i
= 0; i
< nSections
; i
++ )
364 if ( resData
->OffsetToData
>= sections
[i
].VirtualAddress
365 && resData
->OffsetToData
< sections
[i
].VirtualAddress
+
366 sections
[i
].SizeOfRawData
)
369 if ( i
== nSections
)
371 TRACE("Couldn't find resource data section\n" );
375 /* Return resource data */
376 if ( resLen
) *resLen
= resData
->Size
;
377 if ( resOff
) *resOff
= resData
->OffsetToData
- sections
[i
].VirtualAddress
378 + sections
[i
].PointerToRawData
;
382 HeapFree( GetProcessHeap(), 0, resSection
);
383 HeapFree( GetProcessHeap(), 0, sections
);
388 /*************************************************************************
389 * GetFileResourceSize [VER.2]
391 DWORD WINAPI
GetFileResourceSize16( LPCSTR lpszFileName
, LPCSTR lpszResType
,
392 LPCSTR lpszResId
, LPDWORD lpdwFileOffset
)
399 TRACE("(%s,type=0x%lx,id=0x%lx,off=%p)\n",
400 debugstr_a(lpszFileName
), (LONG
)lpszResType
, (LONG
)lpszResId
,
403 lzfd
= LZOpenFileA( lpszFileName
, &ofs
, OF_READ
);
404 if ( lzfd
< 0 ) return 0;
406 switch ( read_xx_header( lzfd
) )
408 case IMAGE_OS2_SIGNATURE
:
409 retv
= find_ne_resource( lzfd
, lpszResType
, lpszResId
,
410 &reslen
, lpdwFileOffset
);
413 case IMAGE_NT_SIGNATURE
:
414 retv
= find_pe_resource( lzfd
, lpszResType
, lpszResId
,
415 &reslen
, lpdwFileOffset
);
420 return retv
? reslen
: 0;
424 /*************************************************************************
425 * GetFileResource [VER.3]
427 DWORD WINAPI
GetFileResource16( LPCSTR lpszFileName
, LPCSTR lpszResType
,
428 LPCSTR lpszResId
, DWORD dwFileOffset
,
429 DWORD dwResLen
, LPVOID lpvData
)
434 DWORD reslen
= dwResLen
;
436 TRACE("(%s,type=0x%lx,id=0x%lx,off=%ld,len=%ld,data=%p)\n",
437 debugstr_a(lpszFileName
), (LONG
)lpszResType
, (LONG
)lpszResId
,
438 dwFileOffset
, dwResLen
, lpvData
);
440 lzfd
= LZOpenFileA( lpszFileName
, &ofs
, OF_READ
);
441 if ( lzfd
< 0 ) return 0;
445 switch ( read_xx_header( lzfd
) )
447 case IMAGE_OS2_SIGNATURE
:
448 retv
= find_ne_resource( lzfd
, lpszResType
, lpszResId
,
449 &reslen
, &dwFileOffset
);
452 case IMAGE_NT_SIGNATURE
:
453 retv
= find_pe_resource( lzfd
, lpszResType
, lpszResId
,
454 &reslen
, &dwFileOffset
);
465 LZSeek( lzfd
, dwFileOffset
, SEEK_SET
);
466 reslen
= LZRead( lzfd
, lpvData
, min( reslen
, dwResLen
) );