2 * Implementation of VERSION.DLL - Resource Access routines
4 * Copyright 1996,1997 Marcus Meissner
5 * Copyright 1997 David Cuthbert
6 * Copyright 1999 Ulrich Weigand
13 #include <sys/types.h>
19 #include "wine/unicode.h"
20 #include "wine/winbase16.h"
21 #include "wine/winuser16.h"
24 #include "debugtools.h"
26 DEFAULT_DEBUG_CHANNEL(ver
);
29 /**********************************************************************
32 * Find an entry by id in a resource directory
33 * Copied from loader/pe_resource.c
35 static const IMAGE_RESOURCE_DIRECTORY
*find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY
*dir
,
36 WORD id
, const void *root
)
38 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
;
41 entry
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(dir
+ 1);
42 min
= dir
->NumberOfNamedEntries
;
43 max
= min
+ dir
->NumberOfIdEntries
- 1;
46 pos
= (min
+ max
) / 2;
47 if (entry
[pos
].u1
.s2
.Id
== id
)
48 return (IMAGE_RESOURCE_DIRECTORY
*)((char *)root
+ entry
[pos
].u2
.s3
.OffsetToDirectory
);
49 if (entry
[pos
].u1
.s2
.Id
> id
) max
= pos
- 1;
56 /**********************************************************************
59 * Find a default entry in a resource directory
60 * Copied from loader/pe_resource.c
62 static const IMAGE_RESOURCE_DIRECTORY
*find_entry_default( const IMAGE_RESOURCE_DIRECTORY
*dir
,
65 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
;
67 entry
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(dir
+ 1);
68 return (IMAGE_RESOURCE_DIRECTORY
*)((char *)root
+ entry
->u2
.s3
.OffsetToDirectory
);
72 /**********************************************************************
75 * Find an entry by name in a resource directory
76 * Copied from loader/pe_resource.c
78 static const IMAGE_RESOURCE_DIRECTORY
*find_entry_by_name( const IMAGE_RESOURCE_DIRECTORY
*dir
,
79 LPCSTR name
, const void *root
)
81 const IMAGE_RESOURCE_DIRECTORY
*ret
= NULL
;
85 if (!HIWORD(name
)) return find_entry_by_id( dir
, LOWORD(name
), root
);
88 return find_entry_by_id( dir
, atoi(name
+1), root
);
91 namelen
= MultiByteToWideChar( CP_ACP
, 0, name
, -1, NULL
, 0 );
92 if ((nameW
= HeapAlloc( GetProcessHeap(), 0, namelen
* sizeof(WCHAR
) )))
94 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
;
95 const IMAGE_RESOURCE_DIR_STRING_U
*str
;
96 int min
, max
, res
, pos
;
98 MultiByteToWideChar( CP_ACP
, 0, name
, -1, nameW
, namelen
);
99 namelen
--; /* remove terminating null */
100 entry
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(dir
+ 1);
102 max
= dir
->NumberOfNamedEntries
- 1;
105 pos
= (min
+ max
) / 2;
106 str
= (IMAGE_RESOURCE_DIR_STRING_U
*)((char *)root
+ entry
[pos
].u1
.s1
.NameOffset
);
107 res
= strncmpiW( nameW
, str
->NameString
, str
->Length
);
108 if (!res
&& namelen
== str
->Length
)
110 ret
= (IMAGE_RESOURCE_DIRECTORY
*)((char *)root
+ entry
[pos
].u2
.s3
.OffsetToDirectory
);
113 if (res
< 0) max
= pos
- 1;
116 HeapFree( GetProcessHeap(), 0, nameW
);
122 /***********************************************************************
123 * read_xx_header [internal]
125 static int read_xx_header( HFILE lzfd
)
127 IMAGE_DOS_HEADER mzh
;
130 LZSeek( lzfd
, 0, SEEK_SET
);
131 if ( sizeof(mzh
) != LZRead( lzfd
, &mzh
, sizeof(mzh
) ) )
133 if ( mzh
.e_magic
!= IMAGE_DOS_SIGNATURE
)
136 LZSeek( lzfd
, mzh
.e_lfanew
, SEEK_SET
);
137 if ( 2 != LZRead( lzfd
, magic
, 2 ) )
140 LZSeek( lzfd
, mzh
.e_lfanew
, SEEK_SET
);
142 if ( magic
[0] == 'N' && magic
[1] == 'E' )
143 return IMAGE_OS2_SIGNATURE
;
144 if ( magic
[0] == 'P' && magic
[1] == 'E' )
145 return IMAGE_NT_SIGNATURE
;
148 WARN("Can't handle %s files.\n", magic
);
152 /***********************************************************************
153 * load_ne_resource [internal]
155 static BOOL
find_ne_resource( HFILE lzfd
, LPCSTR
typeid, LPCSTR resid
,
156 DWORD
*resLen
, DWORD
*resOff
)
158 IMAGE_OS2_HEADER nehd
;
159 NE_TYPEINFO
*typeInfo
;
160 NE_NAMEINFO
*nameInfo
;
166 /* Read in NE header */
167 nehdoffset
= LZSeek( lzfd
, 0, SEEK_CUR
);
168 if ( sizeof(nehd
) != LZRead( lzfd
, &nehd
, sizeof(nehd
) ) ) return 0;
170 resTabSize
= nehd
.ne_restab
- nehd
.ne_rsrctab
;
173 TRACE("No resources in NE dll\n" );
177 /* Read in resource table */
178 resTab
= HeapAlloc( GetProcessHeap(), 0, resTabSize
);
179 if ( !resTab
) return FALSE
;
181 LZSeek( lzfd
, nehd
.ne_rsrctab
+ nehdoffset
, SEEK_SET
);
182 if ( resTabSize
!= LZRead( lzfd
, resTab
, resTabSize
) )
184 HeapFree( GetProcessHeap(), 0, resTab
);
189 typeInfo
= (NE_TYPEINFO
*)(resTab
+ 2);
191 if (HIWORD(typeid) != 0) /* named type */
193 BYTE len
= strlen( typeid );
194 while (typeInfo
->type_id
)
196 if (!(typeInfo
->type_id
& 0x8000))
198 BYTE
*p
= resTab
+ typeInfo
->type_id
;
199 if ((*p
== len
) && !strncasecmp( p
+1, typeid, len
)) goto found_type
;
201 typeInfo
= (NE_TYPEINFO
*)((char *)(typeInfo
+ 1) +
202 typeInfo
->count
* sizeof(NE_NAMEINFO
));
205 else /* numeric type id */
207 WORD id
= LOWORD(typeid) | 0x8000;
208 while (typeInfo
->type_id
)
210 if (typeInfo
->type_id
== id
) goto found_type
;
211 typeInfo
= (NE_TYPEINFO
*)((char *)(typeInfo
+ 1) +
212 typeInfo
->count
* sizeof(NE_NAMEINFO
));
215 TRACE("No typeid entry found for %p\n", typeid );
216 HeapFree( GetProcessHeap(), 0, resTab
);
220 nameInfo
= (NE_NAMEINFO
*)(typeInfo
+ 1);
222 if (HIWORD(resid
) != 0) /* named resource */
224 BYTE len
= strlen( resid
);
225 for (count
= typeInfo
->count
; count
> 0; count
--, nameInfo
++)
227 BYTE
*p
= resTab
+ nameInfo
->id
;
228 if (nameInfo
->id
& 0x8000) continue;
229 if ((*p
== len
) && !strncasecmp( p
+1, resid
, len
)) goto found_name
;
232 else /* numeric resource id */
234 WORD id
= LOWORD(resid
) | 0x8000;
235 for (count
= typeInfo
->count
; count
> 0; count
--, nameInfo
++)
236 if (nameInfo
->id
== id
) goto found_name
;
238 TRACE("No resid entry found for %p\n", typeid );
239 HeapFree( GetProcessHeap(), 0, resTab
);
243 /* Return resource data */
244 if ( resLen
) *resLen
= nameInfo
->length
<< *(WORD
*)resTab
;
245 if ( resOff
) *resOff
= nameInfo
->offset
<< *(WORD
*)resTab
;
247 HeapFree( GetProcessHeap(), 0, resTab
);
251 /***********************************************************************
252 * load_pe_resource [internal]
254 static BOOL
find_pe_resource( HFILE lzfd
, LPCSTR
typeid, LPCSTR resid
,
255 DWORD
*resLen
, DWORD
*resOff
)
257 IMAGE_NT_HEADERS pehd
;
259 PIMAGE_DATA_DIRECTORY resDataDir
;
260 PIMAGE_SECTION_HEADER sections
;
262 DWORD resSectionSize
;
264 const IMAGE_RESOURCE_DIRECTORY
*resPtr
;
265 const IMAGE_RESOURCE_DATA_ENTRY
*resData
;
269 /* Read in PE header */
270 pehdoffset
= LZSeek( lzfd
, 0, SEEK_CUR
);
271 if ( sizeof(pehd
) != LZRead( lzfd
, &pehd
, sizeof(pehd
) ) ) return 0;
273 resDataDir
= pehd
.OptionalHeader
.DataDirectory
+IMAGE_FILE_RESOURCE_DIRECTORY
;
274 if ( !resDataDir
->Size
)
276 TRACE("No resources in PE dll\n" );
280 /* Read in section table */
281 nSections
= pehd
.FileHeader
.NumberOfSections
;
282 sections
= HeapAlloc( GetProcessHeap(), 0,
283 nSections
* sizeof(IMAGE_SECTION_HEADER
) );
284 if ( !sections
) return FALSE
;
286 LZSeek( lzfd
, pehdoffset
+
287 sizeof(DWORD
) + /* Signature */
288 sizeof(IMAGE_FILE_HEADER
) +
289 pehd
.FileHeader
.SizeOfOptionalHeader
, SEEK_SET
);
291 if ( nSections
* sizeof(IMAGE_SECTION_HEADER
) !=
292 LZRead( lzfd
, sections
, nSections
* sizeof(IMAGE_SECTION_HEADER
) ) )
294 HeapFree( GetProcessHeap(), 0, sections
);
298 /* Find resource section */
299 for ( i
= 0; i
< nSections
; i
++ )
300 if ( resDataDir
->VirtualAddress
>= sections
[i
].VirtualAddress
301 && resDataDir
->VirtualAddress
< sections
[i
].VirtualAddress
+
302 sections
[i
].SizeOfRawData
)
305 if ( i
== nSections
)
307 HeapFree( GetProcessHeap(), 0, sections
);
308 TRACE("Couldn't find resource section\n" );
312 /* Read in resource section */
313 resSectionSize
= sections
[i
].SizeOfRawData
;
314 resSection
= HeapAlloc( GetProcessHeap(), 0, resSectionSize
);
317 HeapFree( GetProcessHeap(), 0, sections
);
321 LZSeek( lzfd
, sections
[i
].PointerToRawData
, SEEK_SET
);
322 if ( resSectionSize
!= LZRead( lzfd
, resSection
, resSectionSize
) ) goto done
;
325 resDir
= resSection
+ (resDataDir
->VirtualAddress
- sections
[i
].VirtualAddress
);
327 resPtr
= (PIMAGE_RESOURCE_DIRECTORY
)resDir
;
328 resPtr
= find_entry_by_name( resPtr
, typeid, resDir
);
331 TRACE("No typeid entry found for %p\n", typeid );
334 resPtr
= find_entry_by_name( resPtr
, resid
, resDir
);
337 TRACE("No resid entry found for %p\n", resid
);
340 resPtr
= find_entry_default( resPtr
, resDir
);
343 TRACE("No default language entry found for %p\n", resid
);
347 /* Find resource data section */
348 resData
= (PIMAGE_RESOURCE_DATA_ENTRY
)resPtr
;
349 for ( i
= 0; i
< nSections
; i
++ )
350 if ( resData
->OffsetToData
>= sections
[i
].VirtualAddress
351 && resData
->OffsetToData
< sections
[i
].VirtualAddress
+
352 sections
[i
].SizeOfRawData
)
355 if ( i
== nSections
)
357 TRACE("Couldn't find resource data section\n" );
361 /* Return resource data */
362 if ( resLen
) *resLen
= resData
->Size
;
363 if ( resOff
) *resOff
= resData
->OffsetToData
- sections
[i
].VirtualAddress
364 + sections
[i
].PointerToRawData
;
368 HeapFree( GetProcessHeap(), 0, resSection
);
369 HeapFree( GetProcessHeap(), 0, sections
);
374 /*************************************************************************
375 * GetFileResourceSize [VER.2]
377 DWORD WINAPI
GetFileResourceSize16( LPCSTR lpszFileName
, LPCSTR lpszResType
,
378 LPCSTR lpszResId
, LPDWORD lpdwFileOffset
)
385 TRACE("(%s,type=0x%lx,id=0x%lx,off=%p)\n",
386 debugstr_a(lpszFileName
), (LONG
)lpszResType
, (LONG
)lpszResId
,
389 lzfd
= LZOpenFileA( lpszFileName
, &ofs
, OF_READ
);
390 if ( lzfd
< 0 ) return 0;
392 switch ( read_xx_header( lzfd
) )
394 case IMAGE_OS2_SIGNATURE
:
395 retv
= find_ne_resource( lzfd
, lpszResType
, lpszResId
,
396 &reslen
, lpdwFileOffset
);
399 case IMAGE_NT_SIGNATURE
:
400 retv
= find_pe_resource( lzfd
, lpszResType
, lpszResId
,
401 &reslen
, lpdwFileOffset
);
406 return retv
? reslen
: 0;
410 /*************************************************************************
411 * GetFileResource [VER.3]
413 DWORD WINAPI
GetFileResource16( LPCSTR lpszFileName
, LPCSTR lpszResType
,
414 LPCSTR lpszResId
, DWORD dwFileOffset
,
415 DWORD dwResLen
, LPVOID lpvData
)
420 DWORD reslen
= dwResLen
;
422 TRACE("(%s,type=0x%lx,id=0x%lx,off=%ld,len=%ld,data=%p)\n",
423 debugstr_a(lpszFileName
), (LONG
)lpszResType
, (LONG
)lpszResId
,
424 dwFileOffset
, dwResLen
, lpvData
);
426 lzfd
= LZOpenFileA( lpszFileName
, &ofs
, OF_READ
);
427 if ( lzfd
< 0 ) return 0;
431 switch ( read_xx_header( lzfd
) )
433 case IMAGE_OS2_SIGNATURE
:
434 retv
= find_ne_resource( lzfd
, lpszResType
, lpszResId
,
435 &reslen
, &dwFileOffset
);
438 case IMAGE_NT_SIGNATURE
:
439 retv
= find_pe_resource( lzfd
, lpszResType
, lpszResId
,
440 &reslen
, &dwFileOffset
);
451 LZSeek( lzfd
, dwFileOffset
, SEEK_SET
);
452 reslen
= LZRead( lzfd
, lpvData
, min( reslen
, dwResLen
) );