atl80: Added AtlComModuleRegisterServer implementation (based on AtlModuleRegisterSer...
[wine] / dlls / ver.dll16 / version.c
1 /*
2  * Implementation of VER.DLL
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 <stdarg.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/types.h>
27
28 #define NONAMELESSUNION
29 #define NONAMELESSSTRUCT
30 #include "windef.h"
31 #include "wine/winbase16.h"
32 #include "winver.h"
33 #include "lzexpand.h"
34 #include "wine/unicode.h"
35 #include "wine/debug.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(ver);
38
39 #ifndef SEEK_SET
40 #define SEEK_SET   0
41 #define SEEK_CUR   1
42 #define SEEK_END   2
43 #endif
44
45 /**********************************************************************
46  *  find_entry_by_id
47  *
48  * Find an entry by id in a resource directory
49  * Copied from loader/pe_resource.c
50  */
51 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
52                                                          WORD id, const void *root )
53 {
54     const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
55     int min, max, pos;
56
57     entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
58     min = dir->NumberOfNamedEntries;
59     max = min + dir->NumberOfIdEntries - 1;
60     while (min <= max)
61     {
62         pos = (min + max) / 2;
63         if (entry[pos].u1.s2.Id == id)
64             return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s3.OffsetToDirectory);
65         if (entry[pos].u1.s2.Id > id) max = pos - 1;
66         else min = pos + 1;
67     }
68     return NULL;
69 }
70
71
72 /**********************************************************************
73  *  find_entry_default
74  *
75  * Find a default entry in a resource directory
76  * Copied from loader/pe_resource.c
77  */
78 static const IMAGE_RESOURCE_DIRECTORY *find_entry_default( const IMAGE_RESOURCE_DIRECTORY *dir,
79                                                            const void *root )
80 {
81     const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
82
83     entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
84     return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry->u2.s3.OffsetToDirectory);
85 }
86
87
88 /**********************************************************************
89  *  find_entry_by_name
90  *
91  * Find an entry by name in a resource directory
92  * Copied from loader/pe_resource.c
93  */
94 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_name( const IMAGE_RESOURCE_DIRECTORY *dir,
95                                                            LPCSTR name, const void *root )
96 {
97     const IMAGE_RESOURCE_DIRECTORY *ret = NULL;
98     LPWSTR nameW;
99     DWORD namelen;
100
101     if (!HIWORD(name)) return find_entry_by_id( dir, LOWORD(name), root );
102     if (name[0] == '#')
103     {
104         return find_entry_by_id( dir, atoi(name+1), root );
105     }
106
107     namelen = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
108     if ((nameW = HeapAlloc( GetProcessHeap(), 0, namelen * sizeof(WCHAR) )))
109     {
110         const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
111         const IMAGE_RESOURCE_DIR_STRING_U *str;
112         int min, max, res, pos;
113
114         MultiByteToWideChar( CP_ACP, 0, name, -1, nameW, namelen );
115         namelen--;  /* remove terminating null */
116         entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
117         min = 0;
118         max = dir->NumberOfNamedEntries - 1;
119         while (min <= max)
120         {
121             pos = (min + max) / 2;
122             str = (const IMAGE_RESOURCE_DIR_STRING_U *)((const char *)root + entry[pos].u1.s1.NameOffset);
123             res = strncmpiW( nameW, str->NameString, str->Length );
124             if (!res && namelen == str->Length)
125             {
126                 ret = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s3.OffsetToDirectory);
127                 break;
128             }
129             if (res < 0) max = pos - 1;
130             else min = pos + 1;
131         }
132         HeapFree( GetProcessHeap(), 0, nameW );
133     }
134     return ret;
135 }
136
137
138 /***********************************************************************
139  *           read_xx_header         [internal]
140  */
141 static int read_xx_header( HFILE lzfd )
142 {
143     IMAGE_DOS_HEADER mzh;
144     char magic[3];
145
146     LZSeek( lzfd, 0, SEEK_SET );
147     if ( sizeof(mzh) != LZRead( lzfd, (LPSTR)&mzh, sizeof(mzh) ) )
148         return 0;
149     if ( mzh.e_magic != IMAGE_DOS_SIGNATURE )
150         return 0;
151
152     LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
153     if ( 2 != LZRead( lzfd, magic, 2 ) )
154         return 0;
155
156     LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
157
158     if ( magic[0] == 'N' && magic[1] == 'E' )
159         return IMAGE_OS2_SIGNATURE;
160     if ( magic[0] == 'P' && magic[1] == 'E' )
161         return IMAGE_NT_SIGNATURE;
162
163     magic[2] = '\0';
164     WARN("Can't handle %s files.\n", magic );
165     return 0;
166 }
167
168 /***********************************************************************
169  *           find_ne_resource         [internal]
170  */
171 static BOOL find_ne_resource( HFILE lzfd, LPCSTR typeid, LPCSTR resid,
172                                 DWORD *resLen, DWORD *resOff )
173 {
174     IMAGE_OS2_HEADER nehd;
175     NE_TYPEINFO *typeInfo;
176     NE_NAMEINFO *nameInfo;
177     DWORD nehdoffset;
178     LPBYTE resTab;
179     DWORD resTabSize;
180     int count;
181
182     /* Read in NE header */
183     nehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
184     if ( sizeof(nehd) != LZRead( lzfd, (LPSTR)&nehd, sizeof(nehd) ) ) return 0;
185
186     resTabSize = nehd.ne_restab - nehd.ne_rsrctab;
187     if ( !resTabSize )
188     {
189         TRACE("No resources in NE dll\n" );
190         return FALSE;
191     }
192
193     /* Read in resource table */
194     resTab = HeapAlloc( GetProcessHeap(), 0, resTabSize );
195     if ( !resTab ) return FALSE;
196
197     LZSeek( lzfd, nehd.ne_rsrctab + nehdoffset, SEEK_SET );
198     if ( resTabSize != LZRead( lzfd, (char*)resTab, resTabSize ) )
199     {
200         HeapFree( GetProcessHeap(), 0, resTab );
201         return FALSE;
202     }
203
204     /* Find resource */
205     typeInfo = (NE_TYPEINFO *)(resTab + 2);
206
207     if (HIWORD(typeid) != 0)  /* named type */
208     {
209         BYTE len = strlen( typeid );
210         while (typeInfo->type_id)
211         {
212             if (!(typeInfo->type_id & 0x8000))
213             {
214                 BYTE *p = resTab + typeInfo->type_id;
215                 if ((*p == len) && !strncasecmp( (char*)p+1, typeid, len )) goto found_type;
216             }
217             typeInfo = (NE_TYPEINFO *)((char *)(typeInfo + 1) +
218                                        typeInfo->count * sizeof(NE_NAMEINFO));
219         }
220     }
221     else  /* numeric type id */
222     {
223         WORD id = LOWORD(typeid) | 0x8000;
224         while (typeInfo->type_id)
225         {
226             if (typeInfo->type_id == id) goto found_type;
227             typeInfo = (NE_TYPEINFO *)((char *)(typeInfo + 1) +
228                                        typeInfo->count * sizeof(NE_NAMEINFO));
229         }
230     }
231     TRACE("No typeid entry found for %p\n", typeid );
232     HeapFree( GetProcessHeap(), 0, resTab );
233     return FALSE;
234
235  found_type:
236     nameInfo = (NE_NAMEINFO *)(typeInfo + 1);
237
238     if (HIWORD(resid) != 0)  /* named resource */
239     {
240         BYTE len = strlen( resid );
241         for (count = typeInfo->count; count > 0; count--, nameInfo++)
242         {
243             BYTE *p = resTab + nameInfo->id;
244             if (nameInfo->id & 0x8000) continue;
245             if ((*p == len) && !strncasecmp( (char*)p+1, resid, len )) goto found_name;
246         }
247     }
248     else  /* numeric resource id */
249     {
250         WORD id = LOWORD(resid) | 0x8000;
251         for (count = typeInfo->count; count > 0; count--, nameInfo++)
252             if (nameInfo->id == id) goto found_name;
253     }
254     TRACE("No resid entry found for %p\n", typeid );
255     HeapFree( GetProcessHeap(), 0, resTab );
256     return FALSE;
257
258  found_name:
259     /* Return resource data */
260     if ( resLen ) *resLen = nameInfo->length << *(WORD *)resTab;
261     if ( resOff ) *resOff = nameInfo->offset << *(WORD *)resTab;
262
263     HeapFree( GetProcessHeap(), 0, resTab );
264     return TRUE;
265 }
266
267 /***********************************************************************
268  *           find_pe_resource         [internal]
269  */
270 static BOOL find_pe_resource( HFILE lzfd, LPCSTR typeid, LPCSTR resid,
271                                 DWORD *resLen, DWORD *resOff )
272 {
273     IMAGE_NT_HEADERS pehd;
274     DWORD pehdoffset;
275     PIMAGE_DATA_DIRECTORY resDataDir;
276     PIMAGE_SECTION_HEADER sections;
277     LPBYTE resSection;
278     DWORD resSectionSize;
279     const void *resDir;
280     const IMAGE_RESOURCE_DIRECTORY *resPtr;
281     const IMAGE_RESOURCE_DATA_ENTRY *resData;
282     int i, nSections;
283     BOOL ret = FALSE;
284
285     /* Read in PE header */
286     pehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
287     if ( sizeof(pehd) != LZRead( lzfd, (LPSTR)&pehd, sizeof(pehd) ) ) return 0;
288
289     resDataDir = pehd.OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_RESOURCE;
290     if ( !resDataDir->Size )
291     {
292         TRACE("No resources in PE dll\n" );
293         return FALSE;
294     }
295
296     /* Read in section table */
297     nSections = pehd.FileHeader.NumberOfSections;
298     sections = HeapAlloc( GetProcessHeap(), 0,
299                           nSections * sizeof(IMAGE_SECTION_HEADER) );
300     if ( !sections ) return FALSE;
301
302     LZSeek( lzfd, pehdoffset +
303                     sizeof(DWORD) + /* Signature */
304                     sizeof(IMAGE_FILE_HEADER) +
305                     pehd.FileHeader.SizeOfOptionalHeader, SEEK_SET );
306
307     if ( nSections * sizeof(IMAGE_SECTION_HEADER) !=
308          LZRead( lzfd, (LPSTR)sections, nSections * sizeof(IMAGE_SECTION_HEADER) ) )
309     {
310         HeapFree( GetProcessHeap(), 0, sections );
311         return FALSE;
312     }
313
314     /* Find resource section */
315     for ( i = 0; i < nSections; i++ )
316         if (    resDataDir->VirtualAddress >= sections[i].VirtualAddress
317              && resDataDir->VirtualAddress <  sections[i].VirtualAddress +
318                                               sections[i].SizeOfRawData )
319             break;
320
321     if ( i == nSections )
322     {
323         HeapFree( GetProcessHeap(), 0, sections );
324         TRACE("Couldn't find resource section\n" );
325         return FALSE;
326     }
327
328     /* Read in resource section */
329     resSectionSize = sections[i].SizeOfRawData;
330     resSection = HeapAlloc( GetProcessHeap(), 0, resSectionSize );
331     if ( !resSection )
332     {
333         HeapFree( GetProcessHeap(), 0, sections );
334         return FALSE;
335     }
336
337     LZSeek( lzfd, sections[i].PointerToRawData, SEEK_SET );
338     if ( resSectionSize != LZRead( lzfd, (char*)resSection, resSectionSize ) ) goto done;
339
340     /* Find resource */
341     resDir = resSection + (resDataDir->VirtualAddress - sections[i].VirtualAddress);
342
343     resPtr = resDir;
344     resPtr = find_entry_by_name( resPtr, typeid, resDir );
345     if ( !resPtr )
346     {
347         TRACE("No typeid entry found for %p\n", typeid );
348         goto done;
349     }
350     resPtr = find_entry_by_name( resPtr, resid, resDir );
351     if ( !resPtr )
352     {
353         TRACE("No resid entry found for %p\n", resid );
354         goto done;
355     }
356     resPtr = find_entry_default( resPtr, resDir );
357     if ( !resPtr )
358     {
359         TRACE("No default language entry found for %p\n", resid );
360         goto done;
361     }
362
363     /* Find resource data section */
364     resData = (const IMAGE_RESOURCE_DATA_ENTRY*)resPtr;
365     for ( i = 0; i < nSections; i++ )
366         if (    resData->OffsetToData >= sections[i].VirtualAddress
367              && resData->OffsetToData <  sections[i].VirtualAddress +
368                                          sections[i].SizeOfRawData )
369             break;
370
371     if ( i == nSections )
372     {
373         TRACE("Couldn't find resource data section\n" );
374         goto done;
375     }
376
377     /* Return resource data */
378     if ( resLen ) *resLen = resData->Size;
379     if ( resOff ) *resOff = resData->OffsetToData - sections[i].VirtualAddress
380                             + sections[i].PointerToRawData;
381     ret = TRUE;
382
383  done:
384     HeapFree( GetProcessHeap(), 0, resSection );
385     HeapFree( GetProcessHeap(), 0, sections );
386     return ret;
387 }
388
389
390 /***********************************************************************
391  *           find_resource         [internal]
392  */
393 static DWORD find_resource( HFILE lzfd, LPCSTR type, LPCSTR id, DWORD *reslen, DWORD *offset )
394 {
395     DWORD magic = read_xx_header( lzfd );
396
397     switch (magic)
398     {
399     case IMAGE_OS2_SIGNATURE:
400         if (!find_ne_resource( lzfd, type, id, reslen, offset )) magic = 0;
401         break;
402     case IMAGE_NT_SIGNATURE:
403         if (!find_pe_resource( lzfd, type, id, reslen, offset )) magic = 0;
404         break;
405     }
406     return magic;
407 }
408
409
410 /*************************************************************************
411  * GetFileResourceSize                     [VER.2]
412  */
413 DWORD WINAPI GetFileResourceSize16( LPCSTR lpszFileName, LPCSTR lpszResType,
414                                     LPCSTR lpszResId, LPDWORD lpdwFileOffset )
415 {
416     HFILE lzfd;
417     OFSTRUCT ofs;
418     DWORD reslen = 0;
419
420     TRACE("(%s,type=%p,id=%p,off=%p)\n",
421           debugstr_a(lpszFileName), lpszResType, lpszResId, lpszResId );
422
423     lzfd = LZOpenFileA( (LPSTR)lpszFileName, &ofs, OF_READ );
424     if (lzfd >= 0)
425     {
426         if (!find_resource( lzfd, lpszResType, lpszResId, &reslen, lpdwFileOffset )) reslen = 0;
427         LZClose( lzfd );
428     }
429     return reslen;
430 }
431
432
433 /*************************************************************************
434  * GetFileResource                         [VER.3]
435  */
436 DWORD WINAPI GetFileResource16( LPCSTR lpszFileName, LPCSTR lpszResType,
437                                 LPCSTR lpszResId, DWORD dwFileOffset,
438                                 DWORD dwResLen, LPVOID lpvData )
439 {
440     HFILE lzfd;
441     OFSTRUCT ofs;
442     DWORD reslen = dwResLen;
443
444     TRACE("(%s,type=%p,id=%p,off=%d,len=%d,data=%p)\n",
445                 debugstr_a(lpszFileName), lpszResType, lpszResId,
446                 dwFileOffset, dwResLen, lpvData );
447
448     lzfd = LZOpenFileA( (LPSTR)lpszFileName, &ofs, OF_READ );
449     if ( lzfd < 0 ) return 0;
450
451     if ( !dwFileOffset )
452     {
453         if (!find_resource( lzfd, lpszResType, lpszResId, &reslen, &dwFileOffset ))
454         {
455             LZClose( lzfd );
456             return 0;
457         }
458     }
459
460     LZSeek( lzfd, dwFileOffset, SEEK_SET );
461     reslen = LZRead( lzfd, lpvData, min( reslen, dwResLen ) );
462     LZClose( lzfd );
463
464     return reslen;
465 }
466
467 /*************************************************************************
468  * GetFileVersionInfoSize                  [VER.6]
469  */
470 DWORD WINAPI GetFileVersionInfoSize16( LPCSTR lpszFileName, LPDWORD lpdwHandle )
471 {
472     TRACE("(%s, %p)\n", debugstr_a(lpszFileName), lpdwHandle );
473     return GetFileVersionInfoSizeA( lpszFileName, lpdwHandle );
474 }
475
476 /*************************************************************************
477  * GetFileVersionInfo                      [VER.7]
478  */
479 DWORD WINAPI GetFileVersionInfo16( LPCSTR lpszFileName, DWORD handle,
480                                    DWORD cbBuf, LPVOID lpvData )
481 {
482     TRACE("(%s, %08x, %d, %p)\n",
483                 debugstr_a(lpszFileName), handle, cbBuf, lpvData );
484
485     return GetFileVersionInfoA( lpszFileName, handle, cbBuf, lpvData );
486 }
487
488 /*************************************************************************
489  * VerFindFile                             [VER.8]
490  */
491 DWORD WINAPI VerFindFile16( UINT16 flags, LPSTR lpszFilename,
492                             LPSTR lpszWinDir, LPSTR lpszAppDir,
493                             LPSTR lpszCurDir, UINT16 *lpuCurDirLen,
494                             LPSTR lpszDestDir, UINT16 *lpuDestDirLen )
495 {
496     UINT curDirLen, destDirLen;
497     DWORD retv = VerFindFileA( flags, lpszFilename, lpszWinDir, lpszAppDir,
498                                  lpszCurDir, &curDirLen, lpszDestDir, &destDirLen );
499
500     *lpuCurDirLen = (UINT16)curDirLen;
501     *lpuDestDirLen = (UINT16)destDirLen;
502     return retv;
503 }
504
505 /*************************************************************************
506  * VerInstallFile                          [VER.9]
507  */
508 DWORD WINAPI VerInstallFile16( UINT16 flags,
509                                LPSTR lpszSrcFilename, LPSTR lpszDestFilename,
510                                LPSTR lpszSrcDir, LPSTR lpszDestDir, LPSTR lpszCurDir,
511                                LPSTR lpszTmpFile, UINT16 *lpwTmpFileLen )
512 {
513     UINT filelen;
514     DWORD retv = VerInstallFileA( flags, lpszSrcFilename, lpszDestFilename,
515                                     lpszSrcDir, lpszDestDir, lpszCurDir,
516                                     lpszTmpFile, &filelen);
517
518     *lpwTmpFileLen = (UINT16)filelen;
519     return retv;
520 }
521
522 /*************************************************************************
523  * VerLanguageName                        [VER.10]
524  */
525 DWORD WINAPI VerLanguageName16( UINT16 uLang, LPSTR lpszLang, UINT16 cbLang )
526 {
527     return VerLanguageNameA( uLang, lpszLang, cbLang );
528 }
529
530 /*************************************************************************
531  * VerQueryValue                          [VER.11]
532  */
533 DWORD WINAPI VerQueryValue16( SEGPTR spvBlock, LPSTR lpszSubBlock,
534                               SEGPTR *lpspBuffer, UINT16 *lpcb )
535 {
536     LPVOID lpvBlock = MapSL( spvBlock );
537     LPVOID buffer = lpvBlock;
538     UINT buflen;
539     DWORD retv;
540
541     TRACE("(%p, %s, %p, %p)\n",
542                 lpvBlock, debugstr_a(lpszSubBlock), lpspBuffer, lpcb );
543
544     retv = VerQueryValueA( lpvBlock, lpszSubBlock, &buffer, &buflen );
545     if ( !retv ) return FALSE;
546
547     if ( OFFSETOF( spvBlock ) + ((char *) buffer - (char *) lpvBlock) >= 0x10000 )
548     {
549         FIXME("offset %08X too large relative to %04X:%04X\n",
550                (char *) buffer - (char *) lpvBlock, SELECTOROF( spvBlock ), OFFSETOF( spvBlock ) );
551         return FALSE;
552     }
553
554     if (lpcb) *lpcb = buflen;
555     *lpspBuffer = (SEGPTR) ((char *) spvBlock + ((char *) buffer - (char *) lpvBlock));
556
557     return retv;
558 }