version: Exclude unused headers.
[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
40 #include "wine/unicode.h"
41 #include "wine/winbase16.h"
42 #include "wine/winuser16.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  *  find_entry_by_name
94  *
95  * Find an entry by name in a resource directory
96  * Copied from loader/pe_resource.c
97  */
98 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_name( const IMAGE_RESOURCE_DIRECTORY *dir,
99                                                            LPCSTR name, const void *root )
100 {
101     const IMAGE_RESOURCE_DIRECTORY *ret = NULL;
102     LPWSTR nameW;
103     DWORD namelen;
104
105     if (!HIWORD(name)) return find_entry_by_id( dir, LOWORD(name), root );
106     if (name[0] == '#')
107     {
108         return find_entry_by_id( dir, atoi(name+1), root );
109     }
110
111     namelen = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
112     if ((nameW = HeapAlloc( GetProcessHeap(), 0, namelen * sizeof(WCHAR) )))
113     {
114         const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
115         const IMAGE_RESOURCE_DIR_STRING_U *str;
116         int min, max, res, pos;
117
118         MultiByteToWideChar( CP_ACP, 0, name, -1, nameW, namelen );
119         namelen--;  /* remove terminating null */
120         entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
121         min = 0;
122         max = dir->NumberOfNamedEntries - 1;
123         while (min <= max)
124         {
125             pos = (min + max) / 2;
126             str = (const IMAGE_RESOURCE_DIR_STRING_U *)((const char *)root + entry[pos].u1.s1.NameOffset);
127             res = strncmpiW( nameW, str->NameString, str->Length );
128             if (!res && namelen == str->Length)
129             {
130                 ret = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s3.OffsetToDirectory);
131                 break;
132             }
133             if (res < 0) max = pos - 1;
134             else min = pos + 1;
135         }
136         HeapFree( GetProcessHeap(), 0, nameW );
137     }
138     return ret;
139 }
140
141
142 /***********************************************************************
143  *           read_xx_header         [internal]
144  */
145 static int read_xx_header( HFILE lzfd )
146 {
147     IMAGE_DOS_HEADER mzh;
148     char magic[3];
149
150     LZSeek( lzfd, 0, SEEK_SET );
151     if ( sizeof(mzh) != LZRead( lzfd, (LPSTR)&mzh, sizeof(mzh) ) )
152         return 0;
153     if ( mzh.e_magic != IMAGE_DOS_SIGNATURE )
154         return 0;
155
156     LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
157     if ( 2 != LZRead( lzfd, magic, 2 ) )
158         return 0;
159
160     LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
161
162     if ( magic[0] == 'N' && magic[1] == 'E' )
163         return IMAGE_OS2_SIGNATURE;
164     if ( magic[0] == 'P' && magic[1] == 'E' )
165         return IMAGE_NT_SIGNATURE;
166
167     magic[2] = '\0';
168     WARN("Can't handle %s files.\n", magic );
169     return 0;
170 }
171
172 /***********************************************************************
173  *           load_ne_resource         [internal]
174  */
175 static BOOL find_ne_resource( HFILE lzfd, LPCSTR typeid, LPCSTR resid,
176                                 DWORD *resLen, DWORD *resOff )
177 {
178     IMAGE_OS2_HEADER nehd;
179     NE_TYPEINFO *typeInfo;
180     NE_NAMEINFO *nameInfo;
181     DWORD nehdoffset;
182     LPBYTE resTab;
183     DWORD resTabSize;
184     int count;
185
186     /* Read in NE header */
187     nehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
188     if ( sizeof(nehd) != LZRead( lzfd, (LPSTR)&nehd, sizeof(nehd) ) ) return 0;
189
190     resTabSize = nehd.ne_restab - nehd.ne_rsrctab;
191     if ( !resTabSize )
192     {
193         TRACE("No resources in NE dll\n" );
194         return FALSE;
195     }
196
197     /* Read in resource table */
198     resTab = HeapAlloc( GetProcessHeap(), 0, resTabSize );
199     if ( !resTab ) return FALSE;
200
201     LZSeek( lzfd, nehd.ne_rsrctab + nehdoffset, SEEK_SET );
202     if ( resTabSize != LZRead( lzfd, (char*)resTab, resTabSize ) )
203     {
204         HeapFree( GetProcessHeap(), 0, resTab );
205         return FALSE;
206     }
207
208     /* Find resource */
209     typeInfo = (NE_TYPEINFO *)(resTab + 2);
210
211     if (HIWORD(typeid) != 0)  /* named type */
212     {
213         BYTE len = strlen( typeid );
214         while (typeInfo->type_id)
215         {
216             if (!(typeInfo->type_id & 0x8000))
217             {
218                 BYTE *p = resTab + typeInfo->type_id;
219                 if ((*p == len) && !strncasecmp( (char*)p+1, typeid, len )) goto found_type;
220             }
221             typeInfo = (NE_TYPEINFO *)((char *)(typeInfo + 1) +
222                                        typeInfo->count * sizeof(NE_NAMEINFO));
223         }
224     }
225     else  /* numeric type id */
226     {
227         WORD id = LOWORD(typeid) | 0x8000;
228         while (typeInfo->type_id)
229         {
230             if (typeInfo->type_id == id) goto found_type;
231             typeInfo = (NE_TYPEINFO *)((char *)(typeInfo + 1) +
232                                        typeInfo->count * sizeof(NE_NAMEINFO));
233         }
234     }
235     TRACE("No typeid entry found for %p\n", typeid );
236     HeapFree( GetProcessHeap(), 0, resTab );
237     return FALSE;
238
239  found_type:
240     nameInfo = (NE_NAMEINFO *)(typeInfo + 1);
241
242     if (HIWORD(resid) != 0)  /* named resource */
243     {
244         BYTE len = strlen( resid );
245         for (count = typeInfo->count; count > 0; count--, nameInfo++)
246         {
247             BYTE *p = resTab + nameInfo->id;
248             if (nameInfo->id & 0x8000) continue;
249             if ((*p == len) && !strncasecmp( (char*)p+1, resid, len )) goto found_name;
250         }
251     }
252     else  /* numeric resource id */
253     {
254         WORD id = LOWORD(resid) | 0x8000;
255         for (count = typeInfo->count; count > 0; count--, nameInfo++)
256             if (nameInfo->id == id) goto found_name;
257     }
258     TRACE("No resid entry found for %p\n", typeid );
259     HeapFree( GetProcessHeap(), 0, resTab );
260     return FALSE;
261
262  found_name:
263     /* Return resource data */
264     if ( resLen ) *resLen = nameInfo->length << *(WORD *)resTab;
265     if ( resOff ) *resOff = nameInfo->offset << *(WORD *)resTab;
266
267     HeapFree( GetProcessHeap(), 0, resTab );
268     return TRUE;
269 }
270
271 /***********************************************************************
272  *           load_pe_resource         [internal]
273  */
274 static BOOL find_pe_resource( HFILE lzfd, LPCSTR typeid, LPCSTR resid,
275                                 DWORD *resLen, DWORD *resOff )
276 {
277     IMAGE_NT_HEADERS pehd;
278     DWORD pehdoffset;
279     PIMAGE_DATA_DIRECTORY resDataDir;
280     PIMAGE_SECTION_HEADER sections;
281     LPBYTE resSection;
282     DWORD resSectionSize;
283     const void *resDir;
284     const IMAGE_RESOURCE_DIRECTORY *resPtr;
285     const IMAGE_RESOURCE_DATA_ENTRY *resData;
286     int i, nSections;
287     BOOL ret = FALSE;
288
289     /* Read in PE header */
290     pehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
291     if ( sizeof(pehd) != LZRead( lzfd, (LPSTR)&pehd, sizeof(pehd) ) ) return 0;
292
293     resDataDir = pehd.OptionalHeader.DataDirectory+IMAGE_FILE_RESOURCE_DIRECTORY;
294     if ( !resDataDir->Size )
295     {
296         TRACE("No resources in PE dll\n" );
297         return FALSE;
298     }
299
300     /* Read in section table */
301     nSections = pehd.FileHeader.NumberOfSections;
302     sections = HeapAlloc( GetProcessHeap(), 0,
303                           nSections * sizeof(IMAGE_SECTION_HEADER) );
304     if ( !sections ) return FALSE;
305
306     LZSeek( lzfd, pehdoffset +
307                     sizeof(DWORD) + /* Signature */
308                     sizeof(IMAGE_FILE_HEADER) +
309                     pehd.FileHeader.SizeOfOptionalHeader, SEEK_SET );
310
311     if ( nSections * sizeof(IMAGE_SECTION_HEADER) !=
312          LZRead( lzfd, (LPSTR)sections, nSections * sizeof(IMAGE_SECTION_HEADER) ) )
313     {
314         HeapFree( GetProcessHeap(), 0, sections );
315         return FALSE;
316     }
317
318     /* Find resource section */
319     for ( i = 0; i < nSections; i++ )
320         if (    resDataDir->VirtualAddress >= sections[i].VirtualAddress
321              && resDataDir->VirtualAddress <  sections[i].VirtualAddress +
322                                               sections[i].SizeOfRawData )
323             break;
324
325     if ( i == nSections )
326     {
327         HeapFree( GetProcessHeap(), 0, sections );
328         TRACE("Couldn't find resource section\n" );
329         return FALSE;
330     }
331
332     /* Read in resource section */
333     resSectionSize = sections[i].SizeOfRawData;
334     resSection = HeapAlloc( GetProcessHeap(), 0, resSectionSize );
335     if ( !resSection )
336     {
337         HeapFree( GetProcessHeap(), 0, sections );
338         return FALSE;
339     }
340
341     LZSeek( lzfd, sections[i].PointerToRawData, SEEK_SET );
342     if ( resSectionSize != LZRead( lzfd, (char*)resSection, resSectionSize ) ) goto done;
343
344     /* Find resource */
345     resDir = resSection + (resDataDir->VirtualAddress - sections[i].VirtualAddress);
346
347     resPtr = (const IMAGE_RESOURCE_DIRECTORY*)resDir;
348     resPtr = find_entry_by_name( resPtr, typeid, resDir );
349     if ( !resPtr )
350     {
351         TRACE("No typeid entry found for %p\n", typeid );
352         goto done;
353     }
354     resPtr = find_entry_by_name( resPtr, resid, resDir );
355     if ( !resPtr )
356     {
357         TRACE("No resid entry found for %p\n", resid );
358         goto done;
359     }
360     resPtr = find_entry_default( resPtr, resDir );
361     if ( !resPtr )
362     {
363         TRACE("No default language entry found for %p\n", resid );
364         goto done;
365     }
366
367     /* Find resource data section */
368     resData = (const IMAGE_RESOURCE_DATA_ENTRY*)resPtr;
369     for ( i = 0; i < nSections; i++ )
370         if (    resData->OffsetToData >= sections[i].VirtualAddress
371              && resData->OffsetToData <  sections[i].VirtualAddress +
372                                          sections[i].SizeOfRawData )
373             break;
374
375     if ( i == nSections )
376     {
377         TRACE("Couldn't find resource data section\n" );
378         goto done;
379     }
380
381     /* Return resource data */
382     if ( resLen ) *resLen = resData->Size;
383     if ( resOff ) *resOff = resData->OffsetToData - sections[i].VirtualAddress
384                             + sections[i].PointerToRawData;
385     ret = TRUE;
386
387  done:
388     HeapFree( GetProcessHeap(), 0, resSection );
389     HeapFree( GetProcessHeap(), 0, sections );
390     return ret;
391 }
392
393
394 /*************************************************************************
395  * GetFileResourceSize                     [VER.2]
396  */
397 DWORD WINAPI GetFileResourceSize16( LPCSTR lpszFileName, LPCSTR lpszResType,
398                                     LPCSTR lpszResId, LPDWORD lpdwFileOffset )
399 {
400     BOOL retv = FALSE;
401     HFILE lzfd;
402     OFSTRUCT ofs;
403     DWORD reslen;
404
405     TRACE("(%s,type=0x%x,id=0x%x,off=%p)\n",
406                 debugstr_a(lpszFileName), (LONG)lpszResType, (LONG)lpszResId,
407                 lpszResId );
408
409     lzfd = LZOpenFileA( (LPSTR)lpszFileName, &ofs, OF_READ );
410     if ( lzfd < 0 ) return 0;
411
412     switch ( read_xx_header( lzfd ) )
413     {
414     case IMAGE_OS2_SIGNATURE:
415         retv = find_ne_resource( lzfd, lpszResType, lpszResId,
416                                  &reslen, lpdwFileOffset );
417         break;
418
419     case IMAGE_NT_SIGNATURE:
420         retv = find_pe_resource( lzfd, lpszResType, lpszResId,
421                                  &reslen, lpdwFileOffset );
422         break;
423     }
424
425     LZClose( lzfd );
426     return retv? reslen : 0;
427 }
428
429
430 /*************************************************************************
431  * GetFileResource                         [VER.3]
432  */
433 DWORD WINAPI GetFileResource16( LPCSTR lpszFileName, LPCSTR lpszResType,
434                                 LPCSTR lpszResId, DWORD dwFileOffset,
435                                 DWORD dwResLen, LPVOID lpvData )
436 {
437     BOOL retv = FALSE;
438     HFILE lzfd;
439     OFSTRUCT ofs;
440     DWORD reslen = dwResLen;
441
442     TRACE("(%s,type=%p,id=%p,off=%d,len=%d,data=%p)\n",
443                 debugstr_a(lpszFileName), lpszResType, lpszResId,
444                 dwFileOffset, dwResLen, lpvData );
445
446     lzfd = LZOpenFileA( (LPSTR)lpszFileName, &ofs, OF_READ );
447     if ( lzfd < 0 ) return 0;
448
449     if ( !dwFileOffset )
450     {
451         switch ( read_xx_header( lzfd ) )
452         {
453         case IMAGE_OS2_SIGNATURE:
454             retv = find_ne_resource( lzfd, lpszResType, lpszResId,
455                                      &reslen, &dwFileOffset );
456             break;
457
458         case IMAGE_NT_SIGNATURE:
459             retv = find_pe_resource( lzfd, lpszResType, lpszResId,
460                                      &reslen, &dwFileOffset );
461             break;
462         }
463
464         if ( !retv )
465         {
466             LZClose( lzfd );
467             return 0;
468         }
469     }
470
471     LZSeek( lzfd, dwFileOffset, SEEK_SET );
472     reslen = LZRead( lzfd, lpvData, min( reslen, dwResLen ) );
473     LZClose( lzfd );
474
475     return reslen;
476 }