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