cmd: Turn off echo, add visual separator between testcases.
[wine/multimedia.git] / dlls / version / resource.c
blob68cda3fd579700f9da6e69bd5d27c88fa2865122
1 /*
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
23 #include "config.h"
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
34 #define NONAMELESSUNION
35 #define NONAMELESSSTRUCT
36 #include "windef.h"
37 #include "winbase.h"
38 #include "lzexpand.h"
39 #include "winver.h"
41 #include "wine/unicode.h"
42 #include "wine/winbase16.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(ver);
49 /**********************************************************************
50 * find_entry_by_id
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;
59 int min, max, pos;
61 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
62 min = dir->NumberOfNamedEntries;
63 max = min + dir->NumberOfIdEntries - 1;
64 while (min <= max)
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;
70 else min = pos + 1;
72 return NULL;
76 /**********************************************************************
77 * find_entry_default
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,
83 const void *root )
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 /***********************************************************************
93 * read_xx_header [internal]
95 static int read_xx_header( HFILE lzfd )
97 IMAGE_DOS_HEADER mzh;
98 char magic[3];
100 LZSeek( lzfd, 0, SEEK_SET );
101 if ( sizeof(mzh) != LZRead( lzfd, (LPSTR)&mzh, sizeof(mzh) ) )
102 return 0;
103 if ( mzh.e_magic != IMAGE_DOS_SIGNATURE )
105 if (!memcmp( &mzh, "\177ELF", 4 )) return 1; /* ELF */
106 if (*(UINT *)&mzh == 0xfeedface || *(UINT *)&mzh == 0xcefaedfe) return 1; /* Mach-O */
107 return 0;
110 LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
111 if ( 2 != LZRead( lzfd, magic, 2 ) )
112 return 0;
114 LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
116 if ( magic[0] == 'N' && magic[1] == 'E' )
117 return IMAGE_OS2_SIGNATURE;
118 if ( magic[0] == 'P' && magic[1] == 'E' )
119 return IMAGE_NT_SIGNATURE;
121 magic[2] = '\0';
122 WARN("Can't handle %s files.\n", magic );
123 return 0;
126 /***********************************************************************
127 * find_ne_resource [internal]
129 static BOOL find_ne_resource( HFILE lzfd, DWORD *resLen, DWORD *resOff )
131 const WORD typeid = VS_FILE_INFO | 0x8000;
132 const WORD resid = VS_VERSION_INFO | 0x8000;
133 IMAGE_OS2_HEADER nehd;
134 NE_TYPEINFO *typeInfo;
135 NE_NAMEINFO *nameInfo;
136 DWORD nehdoffset;
137 LPBYTE resTab;
138 DWORD resTabSize;
139 int count;
141 /* Read in NE header */
142 nehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
143 if ( sizeof(nehd) != LZRead( lzfd, (LPSTR)&nehd, sizeof(nehd) ) ) return 0;
145 resTabSize = nehd.ne_restab - nehd.ne_rsrctab;
146 if ( !resTabSize )
148 TRACE("No resources in NE dll\n" );
149 return FALSE;
152 /* Read in resource table */
153 resTab = HeapAlloc( GetProcessHeap(), 0, resTabSize );
154 if ( !resTab ) return FALSE;
156 LZSeek( lzfd, nehd.ne_rsrctab + nehdoffset, SEEK_SET );
157 if ( resTabSize != LZRead( lzfd, (char*)resTab, resTabSize ) )
159 HeapFree( GetProcessHeap(), 0, resTab );
160 return FALSE;
163 /* Find resource */
164 typeInfo = (NE_TYPEINFO *)(resTab + 2);
165 while (typeInfo->type_id)
167 if (typeInfo->type_id == typeid) goto found_type;
168 typeInfo = (NE_TYPEINFO *)((char *)(typeInfo + 1) +
169 typeInfo->count * sizeof(NE_NAMEINFO));
171 TRACE("No typeid entry found\n" );
172 HeapFree( GetProcessHeap(), 0, resTab );
173 return FALSE;
175 found_type:
176 nameInfo = (NE_NAMEINFO *)(typeInfo + 1);
178 for (count = typeInfo->count; count > 0; count--, nameInfo++)
179 if (nameInfo->id == resid) goto found_name;
181 TRACE("No resid entry found\n" );
182 HeapFree( GetProcessHeap(), 0, resTab );
183 return FALSE;
185 found_name:
186 /* Return resource data */
187 if ( resLen ) *resLen = nameInfo->length << *(WORD *)resTab;
188 if ( resOff ) *resOff = nameInfo->offset << *(WORD *)resTab;
190 HeapFree( GetProcessHeap(), 0, resTab );
191 return TRUE;
194 /***********************************************************************
195 * find_pe_resource [internal]
197 static BOOL find_pe_resource( HFILE lzfd, DWORD *resLen, DWORD *resOff )
199 IMAGE_NT_HEADERS pehd;
200 DWORD pehdoffset;
201 PIMAGE_DATA_DIRECTORY resDataDir;
202 PIMAGE_SECTION_HEADER sections;
203 LPBYTE resSection;
204 DWORD resSectionSize;
205 const void *resDir;
206 const IMAGE_RESOURCE_DIRECTORY *resPtr;
207 const IMAGE_RESOURCE_DATA_ENTRY *resData;
208 int i, nSections;
209 BOOL ret = FALSE;
211 /* Read in PE header */
212 pehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
213 if ( sizeof(pehd) != LZRead( lzfd, (LPSTR)&pehd, sizeof(pehd) ) ) return 0;
215 resDataDir = pehd.OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_RESOURCE;
216 if ( !resDataDir->Size )
218 TRACE("No resources in PE dll\n" );
219 return FALSE;
222 /* Read in section table */
223 nSections = pehd.FileHeader.NumberOfSections;
224 sections = HeapAlloc( GetProcessHeap(), 0,
225 nSections * sizeof(IMAGE_SECTION_HEADER) );
226 if ( !sections ) return FALSE;
228 LZSeek( lzfd, pehdoffset +
229 sizeof(DWORD) + /* Signature */
230 sizeof(IMAGE_FILE_HEADER) +
231 pehd.FileHeader.SizeOfOptionalHeader, SEEK_SET );
233 if ( nSections * sizeof(IMAGE_SECTION_HEADER) !=
234 LZRead( lzfd, (LPSTR)sections, nSections * sizeof(IMAGE_SECTION_HEADER) ) )
236 HeapFree( GetProcessHeap(), 0, sections );
237 return FALSE;
240 /* Find resource section */
241 for ( i = 0; i < nSections; i++ )
242 if ( resDataDir->VirtualAddress >= sections[i].VirtualAddress
243 && resDataDir->VirtualAddress < sections[i].VirtualAddress +
244 sections[i].SizeOfRawData )
245 break;
247 if ( i == nSections )
249 HeapFree( GetProcessHeap(), 0, sections );
250 TRACE("Couldn't find resource section\n" );
251 return FALSE;
254 /* Read in resource section */
255 resSectionSize = sections[i].SizeOfRawData;
256 resSection = HeapAlloc( GetProcessHeap(), 0, resSectionSize );
257 if ( !resSection )
259 HeapFree( GetProcessHeap(), 0, sections );
260 return FALSE;
263 LZSeek( lzfd, sections[i].PointerToRawData, SEEK_SET );
264 if ( resSectionSize != LZRead( lzfd, (char*)resSection, resSectionSize ) ) goto done;
266 /* Find resource */
267 resDir = resSection + (resDataDir->VirtualAddress - sections[i].VirtualAddress);
269 resPtr = resDir;
270 resPtr = find_entry_by_id( resPtr, VS_FILE_INFO, resDir );
271 if ( !resPtr )
273 TRACE("No typeid entry found\n" );
274 goto done;
276 resPtr = find_entry_by_id( resPtr, VS_VERSION_INFO, resDir );
277 if ( !resPtr )
279 TRACE("No resid entry found\n" );
280 goto done;
282 resPtr = find_entry_default( resPtr, resDir );
283 if ( !resPtr )
285 TRACE("No default language entry found\n" );
286 goto done;
289 /* Find resource data section */
290 resData = (const IMAGE_RESOURCE_DATA_ENTRY*)resPtr;
291 for ( i = 0; i < nSections; i++ )
292 if ( resData->OffsetToData >= sections[i].VirtualAddress
293 && resData->OffsetToData < sections[i].VirtualAddress +
294 sections[i].SizeOfRawData )
295 break;
297 if ( i == nSections )
299 TRACE("Couldn't find resource data section\n" );
300 goto done;
303 /* Return resource data */
304 if ( resLen ) *resLen = resData->Size;
305 if ( resOff ) *resOff = resData->OffsetToData - sections[i].VirtualAddress
306 + sections[i].PointerToRawData;
307 ret = TRUE;
309 done:
310 HeapFree( GetProcessHeap(), 0, resSection );
311 HeapFree( GetProcessHeap(), 0, sections );
312 return ret;
316 /***********************************************************************
317 * find_version_resource [internal]
319 DWORD find_version_resource( HFILE lzfd, DWORD *reslen, DWORD *offset )
321 DWORD magic = read_xx_header( lzfd );
323 switch (magic)
325 case IMAGE_OS2_SIGNATURE:
326 if (!find_ne_resource( lzfd, reslen, offset )) magic = 0;
327 break;
328 case IMAGE_NT_SIGNATURE:
329 if (!find_pe_resource( lzfd, reslen, offset )) magic = 0;
330 break;
332 return magic;