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