advapi32: Add stub for EnableTrace.
[wine] / dlls / version / resource.c
1 /*
2  * Implementation of VERSION.DLL - Resource Access routines
3  *
4  * Copyright 1996,1997 Marcus Meissner
5  * Copyright 1997 David Cuthbert
6  * Copyright 1999 Ulrich Weigand
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include "config.h"
24
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
33
34 #define NONAMELESSUNION
35 #define NONAMELESSSTRUCT
36 #include "windef.h"
37 #include "winbase.h"
38 #include "lzexpand.h"
39 #include "winver.h"
40
41 #include "wine/unicode.h"
42 #include "wine/winbase16.h"
43
44 #include "wine/debug.h"
45
46 WINE_DEFAULT_DEBUG_CHANNEL(ver);
47
48
49 /**********************************************************************
50  *  find_entry_by_id
51  *
52  * Find an entry by id in a resource directory
53  * Copied from loader/pe_resource.c
54  */
55 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
56                                                          WORD id, const void *root )
57 {
58     const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
59     int min, max, pos;
60
61     entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
62     min = dir->NumberOfNamedEntries;
63     max = min + dir->NumberOfIdEntries - 1;
64     while (min <= max)
65     {
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;
71     }
72     return NULL;
73 }
74
75
76 /**********************************************************************
77  *  find_entry_default
78  *
79  * Find a default entry in a resource directory
80  * Copied from loader/pe_resource.c
81  */
82 static const IMAGE_RESOURCE_DIRECTORY *find_entry_default( const IMAGE_RESOURCE_DIRECTORY *dir,
83                                                            const void *root )
84 {
85     const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
86
87     entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
88     return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry->u2.s3.OffsetToDirectory);
89 }
90
91
92 /***********************************************************************
93  *           read_xx_header         [internal]
94  */
95 static int read_xx_header( HFILE lzfd )
96 {
97     IMAGE_DOS_HEADER mzh;
98     char magic[3];
99
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 )
104         return 0;
105
106     LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
107     if ( 2 != LZRead( lzfd, magic, 2 ) )
108         return 0;
109
110     LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
111
112     if ( magic[0] == 'N' && magic[1] == 'E' )
113         return IMAGE_OS2_SIGNATURE;
114     if ( magic[0] == 'P' && magic[1] == 'E' )
115         return IMAGE_NT_SIGNATURE;
116
117     magic[2] = '\0';
118     WARN("Can't handle %s files.\n", magic );
119     return 0;
120 }
121
122 /***********************************************************************
123  *           find_ne_resource         [internal]
124  */
125 static BOOL find_ne_resource( HFILE lzfd, DWORD *resLen, DWORD *resOff )
126 {
127     const WORD typeid = VS_FILE_INFO | 0x8000;
128     const WORD resid = VS_VERSION_INFO | 0x8000;
129     IMAGE_OS2_HEADER nehd;
130     NE_TYPEINFO *typeInfo;
131     NE_NAMEINFO *nameInfo;
132     DWORD nehdoffset;
133     LPBYTE resTab;
134     DWORD resTabSize;
135     int count;
136
137     /* Read in NE header */
138     nehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
139     if ( sizeof(nehd) != LZRead( lzfd, (LPSTR)&nehd, sizeof(nehd) ) ) return 0;
140
141     resTabSize = nehd.ne_restab - nehd.ne_rsrctab;
142     if ( !resTabSize )
143     {
144         TRACE("No resources in NE dll\n" );
145         return FALSE;
146     }
147
148     /* Read in resource table */
149     resTab = HeapAlloc( GetProcessHeap(), 0, resTabSize );
150     if ( !resTab ) return FALSE;
151
152     LZSeek( lzfd, nehd.ne_rsrctab + nehdoffset, SEEK_SET );
153     if ( resTabSize != LZRead( lzfd, (char*)resTab, resTabSize ) )
154     {
155         HeapFree( GetProcessHeap(), 0, resTab );
156         return FALSE;
157     }
158
159     /* Find resource */
160     typeInfo = (NE_TYPEINFO *)(resTab + 2);
161     while (typeInfo->type_id)
162     {
163         if (typeInfo->type_id == typeid) goto found_type;
164         typeInfo = (NE_TYPEINFO *)((char *)(typeInfo + 1) +
165                                    typeInfo->count * sizeof(NE_NAMEINFO));
166     }
167     TRACE("No typeid entry found\n" );
168     HeapFree( GetProcessHeap(), 0, resTab );
169     return FALSE;
170
171  found_type:
172     nameInfo = (NE_NAMEINFO *)(typeInfo + 1);
173
174     for (count = typeInfo->count; count > 0; count--, nameInfo++)
175         if (nameInfo->id == resid) goto found_name;
176
177     TRACE("No resid entry found\n" );
178     HeapFree( GetProcessHeap(), 0, resTab );
179     return FALSE;
180
181  found_name:
182     /* Return resource data */
183     if ( resLen ) *resLen = nameInfo->length << *(WORD *)resTab;
184     if ( resOff ) *resOff = nameInfo->offset << *(WORD *)resTab;
185
186     HeapFree( GetProcessHeap(), 0, resTab );
187     return TRUE;
188 }
189
190 /***********************************************************************
191  *           find_pe_resource         [internal]
192  */
193 static BOOL find_pe_resource( HFILE lzfd, DWORD *resLen, DWORD *resOff )
194 {
195     IMAGE_NT_HEADERS pehd;
196     DWORD pehdoffset;
197     PIMAGE_DATA_DIRECTORY resDataDir;
198     PIMAGE_SECTION_HEADER sections;
199     LPBYTE resSection;
200     DWORD resSectionSize;
201     const void *resDir;
202     const IMAGE_RESOURCE_DIRECTORY *resPtr;
203     const IMAGE_RESOURCE_DATA_ENTRY *resData;
204     int i, nSections;
205     BOOL ret = FALSE;
206
207     /* Read in PE header */
208     pehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
209     if ( sizeof(pehd) != LZRead( lzfd, (LPSTR)&pehd, sizeof(pehd) ) ) return 0;
210
211     resDataDir = pehd.OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_RESOURCE;
212     if ( !resDataDir->Size )
213     {
214         TRACE("No resources in PE dll\n" );
215         return FALSE;
216     }
217
218     /* Read in section table */
219     nSections = pehd.FileHeader.NumberOfSections;
220     sections = HeapAlloc( GetProcessHeap(), 0,
221                           nSections * sizeof(IMAGE_SECTION_HEADER) );
222     if ( !sections ) return FALSE;
223
224     LZSeek( lzfd, pehdoffset +
225                     sizeof(DWORD) + /* Signature */
226                     sizeof(IMAGE_FILE_HEADER) +
227                     pehd.FileHeader.SizeOfOptionalHeader, SEEK_SET );
228
229     if ( nSections * sizeof(IMAGE_SECTION_HEADER) !=
230          LZRead( lzfd, (LPSTR)sections, nSections * sizeof(IMAGE_SECTION_HEADER) ) )
231     {
232         HeapFree( GetProcessHeap(), 0, sections );
233         return FALSE;
234     }
235
236     /* Find resource section */
237     for ( i = 0; i < nSections; i++ )
238         if (    resDataDir->VirtualAddress >= sections[i].VirtualAddress
239              && resDataDir->VirtualAddress <  sections[i].VirtualAddress +
240                                               sections[i].SizeOfRawData )
241             break;
242
243     if ( i == nSections )
244     {
245         HeapFree( GetProcessHeap(), 0, sections );
246         TRACE("Couldn't find resource section\n" );
247         return FALSE;
248     }
249
250     /* Read in resource section */
251     resSectionSize = sections[i].SizeOfRawData;
252     resSection = HeapAlloc( GetProcessHeap(), 0, resSectionSize );
253     if ( !resSection )
254     {
255         HeapFree( GetProcessHeap(), 0, sections );
256         return FALSE;
257     }
258
259     LZSeek( lzfd, sections[i].PointerToRawData, SEEK_SET );
260     if ( resSectionSize != LZRead( lzfd, (char*)resSection, resSectionSize ) ) goto done;
261
262     /* Find resource */
263     resDir = resSection + (resDataDir->VirtualAddress - sections[i].VirtualAddress);
264
265     resPtr = resDir;
266     resPtr = find_entry_by_id( resPtr, VS_FILE_INFO, resDir );
267     if ( !resPtr )
268     {
269         TRACE("No typeid entry found\n" );
270         goto done;
271     }
272     resPtr = find_entry_by_id( resPtr, VS_VERSION_INFO, resDir );
273     if ( !resPtr )
274     {
275         TRACE("No resid entry found\n" );
276         goto done;
277     }
278     resPtr = find_entry_default( resPtr, resDir );
279     if ( !resPtr )
280     {
281         TRACE("No default language entry found\n" );
282         goto done;
283     }
284
285     /* Find resource data section */
286     resData = (const IMAGE_RESOURCE_DATA_ENTRY*)resPtr;
287     for ( i = 0; i < nSections; i++ )
288         if (    resData->OffsetToData >= sections[i].VirtualAddress
289              && resData->OffsetToData <  sections[i].VirtualAddress +
290                                          sections[i].SizeOfRawData )
291             break;
292
293     if ( i == nSections )
294     {
295         TRACE("Couldn't find resource data section\n" );
296         goto done;
297     }
298
299     /* Return resource data */
300     if ( resLen ) *resLen = resData->Size;
301     if ( resOff ) *resOff = resData->OffsetToData - sections[i].VirtualAddress
302                             + sections[i].PointerToRawData;
303     ret = TRUE;
304
305  done:
306     HeapFree( GetProcessHeap(), 0, resSection );
307     HeapFree( GetProcessHeap(), 0, sections );
308     return ret;
309 }
310
311
312 /***********************************************************************
313  *           find_version_resource         [internal]
314  */
315 DWORD find_version_resource( HFILE lzfd, DWORD *reslen, DWORD *offset )
316 {
317     DWORD magic = read_xx_header( lzfd );
318
319     switch (magic)
320     {
321     case IMAGE_OS2_SIGNATURE:
322         if (!find_ne_resource( lzfd, reslen, offset )) magic = 0;
323         break;
324     case IMAGE_NT_SIGNATURE:
325         if (!find_pe_resource( lzfd, reslen, offset )) magic = 0;
326         break;
327     }
328     return magic;
329 }