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>
35 #include "wine/unicode.h"
36 #include "wine/winbase16.h"
37 #include "wine/winuser16.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(ver
);
45 /**********************************************************************
48 * Find an entry by id in a resource directory
49 * Copied from loader/pe_resource.c
51 static const IMAGE_RESOURCE_DIRECTORY
*find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY
*dir
,
52 WORD id
, const void *root
)
54 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
;
57 entry
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(dir
+ 1);
58 min
= dir
->NumberOfNamedEntries
;
59 max
= min
+ dir
->NumberOfIdEntries
- 1;
62 pos
= (min
+ max
) / 2;
63 if (entry
[pos
].u1
.s2
.Id
== id
)
64 return (IMAGE_RESOURCE_DIRECTORY
*)((char *)root
+ entry
[pos
].u2
.s3
.OffsetToDirectory
);
65 if (entry
[pos
].u1
.s2
.Id
> id
) max
= pos
- 1;
72 /**********************************************************************
75 * Find a default entry in a resource directory
76 * Copied from loader/pe_resource.c
78 static const IMAGE_RESOURCE_DIRECTORY
*find_entry_default( const IMAGE_RESOURCE_DIRECTORY
*dir
,
81 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
;
83 entry
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(dir
+ 1);
84 return (IMAGE_RESOURCE_DIRECTORY
*)((char *)root
+ entry
->u2
.s3
.OffsetToDirectory
);
88 /**********************************************************************
91 * Find an entry by name in a resource directory
92 * Copied from loader/pe_resource.c
94 static const IMAGE_RESOURCE_DIRECTORY
*find_entry_by_name( const IMAGE_RESOURCE_DIRECTORY
*dir
,
95 LPCSTR name
, const void *root
)
97 const IMAGE_RESOURCE_DIRECTORY
*ret
= NULL
;
101 if (!HIWORD(name
)) return find_entry_by_id( dir
, LOWORD(name
), root
);
104 return find_entry_by_id( dir
, atoi(name
+1), root
);
107 namelen
= MultiByteToWideChar( CP_ACP
, 0, name
, -1, NULL
, 0 );
108 if ((nameW
= HeapAlloc( GetProcessHeap(), 0, namelen
* sizeof(WCHAR
) )))
110 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
;
111 const IMAGE_RESOURCE_DIR_STRING_U
*str
;
112 int min
, max
, res
, pos
;
114 MultiByteToWideChar( CP_ACP
, 0, name
, -1, nameW
, namelen
);
115 namelen
--; /* remove terminating null */
116 entry
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(dir
+ 1);
118 max
= dir
->NumberOfNamedEntries
- 1;
121 pos
= (min
+ max
) / 2;
122 str
= (IMAGE_RESOURCE_DIR_STRING_U
*)((char *)root
+ entry
[pos
].u1
.s1
.NameOffset
);
123 res
= strncmpiW( nameW
, str
->NameString
, str
->Length
);
124 if (!res
&& namelen
== str
->Length
)
126 ret
= (IMAGE_RESOURCE_DIRECTORY
*)((char *)root
+ entry
[pos
].u2
.s3
.OffsetToDirectory
);
129 if (res
< 0) max
= pos
- 1;
132 HeapFree( GetProcessHeap(), 0, nameW
);
138 /***********************************************************************
139 * read_xx_header [internal]
141 static int read_xx_header( HFILE lzfd
)
143 IMAGE_DOS_HEADER mzh
;
146 LZSeek( lzfd
, 0, SEEK_SET
);
147 if ( sizeof(mzh
) != LZRead( lzfd
, &mzh
, sizeof(mzh
) ) )
149 if ( mzh
.e_magic
!= IMAGE_DOS_SIGNATURE
)
152 LZSeek( lzfd
, mzh
.e_lfanew
, SEEK_SET
);
153 if ( 2 != LZRead( lzfd
, magic
, 2 ) )
156 LZSeek( lzfd
, mzh
.e_lfanew
, SEEK_SET
);
158 if ( magic
[0] == 'N' && magic
[1] == 'E' )
159 return IMAGE_OS2_SIGNATURE
;
160 if ( magic
[0] == 'P' && magic
[1] == 'E' )
161 return IMAGE_NT_SIGNATURE
;
164 WARN("Can't handle %s files.\n", magic
);
168 /***********************************************************************
169 * load_ne_resource [internal]
171 static BOOL
find_ne_resource( HFILE lzfd
, LPCSTR
typeid, LPCSTR resid
,
172 DWORD
*resLen
, DWORD
*resOff
)
174 IMAGE_OS2_HEADER nehd
;
175 NE_TYPEINFO
*typeInfo
;
176 NE_NAMEINFO
*nameInfo
;
182 /* Read in NE header */
183 nehdoffset
= LZSeek( lzfd
, 0, SEEK_CUR
);
184 if ( sizeof(nehd
) != LZRead( lzfd
, &nehd
, sizeof(nehd
) ) ) return 0;
186 resTabSize
= nehd
.ne_restab
- nehd
.ne_rsrctab
;
189 TRACE("No resources in NE dll\n" );
193 /* Read in resource table */
194 resTab
= HeapAlloc( GetProcessHeap(), 0, resTabSize
);
195 if ( !resTab
) return FALSE
;
197 LZSeek( lzfd
, nehd
.ne_rsrctab
+ nehdoffset
, SEEK_SET
);
198 if ( resTabSize
!= LZRead( lzfd
, resTab
, resTabSize
) )
200 HeapFree( GetProcessHeap(), 0, resTab
);
205 typeInfo
= (NE_TYPEINFO
*)(resTab
+ 2);
207 if (HIWORD(typeid) != 0) /* named type */
209 BYTE len
= strlen( typeid );
210 while (typeInfo
->type_id
)
212 if (!(typeInfo
->type_id
& 0x8000))
214 BYTE
*p
= resTab
+ typeInfo
->type_id
;
215 if ((*p
== len
) && !strncasecmp( p
+1, typeid, len
)) goto found_type
;
217 typeInfo
= (NE_TYPEINFO
*)((char *)(typeInfo
+ 1) +
218 typeInfo
->count
* sizeof(NE_NAMEINFO
));
221 else /* numeric type id */
223 WORD id
= LOWORD(typeid) | 0x8000;
224 while (typeInfo
->type_id
)
226 if (typeInfo
->type_id
== id
) goto found_type
;
227 typeInfo
= (NE_TYPEINFO
*)((char *)(typeInfo
+ 1) +
228 typeInfo
->count
* sizeof(NE_NAMEINFO
));
231 TRACE("No typeid entry found for %p\n", typeid );
232 HeapFree( GetProcessHeap(), 0, resTab
);
236 nameInfo
= (NE_NAMEINFO
*)(typeInfo
+ 1);
238 if (HIWORD(resid
) != 0) /* named resource */
240 BYTE len
= strlen( resid
);
241 for (count
= typeInfo
->count
; count
> 0; count
--, nameInfo
++)
243 BYTE
*p
= resTab
+ nameInfo
->id
;
244 if (nameInfo
->id
& 0x8000) continue;
245 if ((*p
== len
) && !strncasecmp( p
+1, resid
, len
)) goto found_name
;
248 else /* numeric resource id */
250 WORD id
= LOWORD(resid
) | 0x8000;
251 for (count
= typeInfo
->count
; count
> 0; count
--, nameInfo
++)
252 if (nameInfo
->id
== id
) goto found_name
;
254 TRACE("No resid entry found for %p\n", typeid );
255 HeapFree( GetProcessHeap(), 0, resTab
);
259 /* Return resource data */
260 if ( resLen
) *resLen
= nameInfo
->length
<< *(WORD
*)resTab
;
261 if ( resOff
) *resOff
= nameInfo
->offset
<< *(WORD
*)resTab
;
263 HeapFree( GetProcessHeap(), 0, resTab
);
267 /***********************************************************************
268 * load_pe_resource [internal]
270 static BOOL
find_pe_resource( HFILE lzfd
, LPCSTR
typeid, LPCSTR resid
,
271 DWORD
*resLen
, DWORD
*resOff
)
273 IMAGE_NT_HEADERS pehd
;
275 PIMAGE_DATA_DIRECTORY resDataDir
;
276 PIMAGE_SECTION_HEADER sections
;
278 DWORD resSectionSize
;
280 const IMAGE_RESOURCE_DIRECTORY
*resPtr
;
281 const IMAGE_RESOURCE_DATA_ENTRY
*resData
;
285 /* Read in PE header */
286 pehdoffset
= LZSeek( lzfd
, 0, SEEK_CUR
);
287 if ( sizeof(pehd
) != LZRead( lzfd
, &pehd
, sizeof(pehd
) ) ) return 0;
289 resDataDir
= pehd
.OptionalHeader
.DataDirectory
+IMAGE_FILE_RESOURCE_DIRECTORY
;
290 if ( !resDataDir
->Size
)
292 TRACE("No resources in PE dll\n" );
296 /* Read in section table */
297 nSections
= pehd
.FileHeader
.NumberOfSections
;
298 sections
= HeapAlloc( GetProcessHeap(), 0,
299 nSections
* sizeof(IMAGE_SECTION_HEADER
) );
300 if ( !sections
) return FALSE
;
302 LZSeek( lzfd
, pehdoffset
+
303 sizeof(DWORD
) + /* Signature */
304 sizeof(IMAGE_FILE_HEADER
) +
305 pehd
.FileHeader
.SizeOfOptionalHeader
, SEEK_SET
);
307 if ( nSections
* sizeof(IMAGE_SECTION_HEADER
) !=
308 LZRead( lzfd
, sections
, nSections
* sizeof(IMAGE_SECTION_HEADER
) ) )
310 HeapFree( GetProcessHeap(), 0, sections
);
314 /* Find resource section */
315 for ( i
= 0; i
< nSections
; i
++ )
316 if ( resDataDir
->VirtualAddress
>= sections
[i
].VirtualAddress
317 && resDataDir
->VirtualAddress
< sections
[i
].VirtualAddress
+
318 sections
[i
].SizeOfRawData
)
321 if ( i
== nSections
)
323 HeapFree( GetProcessHeap(), 0, sections
);
324 TRACE("Couldn't find resource section\n" );
328 /* Read in resource section */
329 resSectionSize
= sections
[i
].SizeOfRawData
;
330 resSection
= HeapAlloc( GetProcessHeap(), 0, resSectionSize
);
333 HeapFree( GetProcessHeap(), 0, sections
);
337 LZSeek( lzfd
, sections
[i
].PointerToRawData
, SEEK_SET
);
338 if ( resSectionSize
!= LZRead( lzfd
, resSection
, resSectionSize
) ) goto done
;
341 resDir
= resSection
+ (resDataDir
->VirtualAddress
- sections
[i
].VirtualAddress
);
343 resPtr
= (PIMAGE_RESOURCE_DIRECTORY
)resDir
;
344 resPtr
= find_entry_by_name( resPtr
, typeid, resDir
);
347 TRACE("No typeid entry found for %p\n", typeid );
350 resPtr
= find_entry_by_name( resPtr
, resid
, resDir
);
353 TRACE("No resid entry found for %p\n", resid
);
356 resPtr
= find_entry_default( resPtr
, resDir
);
359 TRACE("No default language entry found for %p\n", resid
);
363 /* Find resource data section */
364 resData
= (PIMAGE_RESOURCE_DATA_ENTRY
)resPtr
;
365 for ( i
= 0; i
< nSections
; i
++ )
366 if ( resData
->OffsetToData
>= sections
[i
].VirtualAddress
367 && resData
->OffsetToData
< sections
[i
].VirtualAddress
+
368 sections
[i
].SizeOfRawData
)
371 if ( i
== nSections
)
373 TRACE("Couldn't find resource data section\n" );
377 /* Return resource data */
378 if ( resLen
) *resLen
= resData
->Size
;
379 if ( resOff
) *resOff
= resData
->OffsetToData
- sections
[i
].VirtualAddress
380 + sections
[i
].PointerToRawData
;
384 HeapFree( GetProcessHeap(), 0, resSection
);
385 HeapFree( GetProcessHeap(), 0, sections
);
390 /*************************************************************************
391 * GetFileResourceSize [VER.2]
393 DWORD WINAPI
GetFileResourceSize16( LPCSTR lpszFileName
, LPCSTR lpszResType
,
394 LPCSTR lpszResId
, LPDWORD lpdwFileOffset
)
401 TRACE("(%s,type=0x%lx,id=0x%lx,off=%p)\n",
402 debugstr_a(lpszFileName
), (LONG
)lpszResType
, (LONG
)lpszResId
,
405 lzfd
= LZOpenFileA( lpszFileName
, &ofs
, OF_READ
);
406 if ( lzfd
< 0 ) return 0;
408 switch ( read_xx_header( lzfd
) )
410 case IMAGE_OS2_SIGNATURE
:
411 retv
= find_ne_resource( lzfd
, lpszResType
, lpszResId
,
412 &reslen
, lpdwFileOffset
);
415 case IMAGE_NT_SIGNATURE
:
416 retv
= find_pe_resource( lzfd
, lpszResType
, lpszResId
,
417 &reslen
, lpdwFileOffset
);
422 return retv
? reslen
: 0;
426 /*************************************************************************
427 * GetFileResource [VER.3]
429 DWORD WINAPI
GetFileResource16( LPCSTR lpszFileName
, LPCSTR lpszResType
,
430 LPCSTR lpszResId
, DWORD dwFileOffset
,
431 DWORD dwResLen
, LPVOID lpvData
)
436 DWORD reslen
= dwResLen
;
438 TRACE("(%s,type=0x%lx,id=0x%lx,off=%ld,len=%ld,data=%p)\n",
439 debugstr_a(lpszFileName
), (LONG
)lpszResType
, (LONG
)lpszResId
,
440 dwFileOffset
, dwResLen
, lpvData
);
442 lzfd
= LZOpenFileA( lpszFileName
, &ofs
, OF_READ
);
443 if ( lzfd
< 0 ) return 0;
447 switch ( read_xx_header( lzfd
) )
449 case IMAGE_OS2_SIGNATURE
:
450 retv
= find_ne_resource( lzfd
, lpszResType
, lpszResId
,
451 &reslen
, &dwFileOffset
);
454 case IMAGE_NT_SIGNATURE
:
455 retv
= find_pe_resource( lzfd
, lpszResType
, lpszResId
,
456 &reslen
, &dwFileOffset
);
467 LZSeek( lzfd
, dwFileOffset
, SEEK_SET
);
468 reslen
= LZRead( lzfd
, lpvData
, min( reslen
, dwResLen
) );