Release 980503
[wine] / misc / ver.c
1 /* 
2  * Implementation of VER.DLL
3  * 
4  * Copyright 1996,1997 Marcus Meissner
5  * Copyright 1997 David Cuthbert
6  */
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <ctype.h>
11 #include <unistd.h>
12 #include "windows.h"
13 #include "win.h"
14 #include "winerror.h"
15 #include "heap.h"
16 #include "ver.h"
17 #include "lzexpand.h"
18 #include "module.h"
19 #include "neexe.h"
20 #include "debug.h"
21 #include "xmalloc.h"
22 #include "winreg.h"
23
24 #define LZREAD(what) \
25   if (sizeof(*what)!=LZRead32(lzfd,what,sizeof(*what))) return 0;
26 #define LZTELL(lzfd) LZSeek32(lzfd, 0, SEEK_CUR);
27
28 /******************************************************************************
29  *
30  *   void  ver_dstring(
31  *      char const * prologue,
32  *      char const * teststring,
33  *      char const * epilogue )
34  *
35  *   This function will print via dprintf[_]ver to stddeb the prologue string,
36  *   followed by the address of teststring and the string it contains if
37  *   teststring is non-null or "(null)" otherwise, and then the epilogue
38  *   string followed by a new line.
39  *
40  *   Revision history
41  *      30-May-1997 Dave Cuthbert (dacut@ece.cmu.edu)
42  *         Original implementation as dprintf[_]ver_string
43  *      05-Jul-1997 Dave Cuthbert (dacut@ece.cmu.edu)
44  *         Fixed problem that caused bug with tools/make_debug -- renaming
45  *         this function should fix the problem.
46  *      15-Feb-1998 Dimitrie Paun (dimi@cs.toronto.edu)
47  *         Modified it to make it print the message using only one
48  *         dprintf[_]ver call.
49  *
50  *****************************************************************************/
51
52 static void  ver_dstring(
53     char const * prologue,
54     char const * teststring,
55     char const * epilogue )
56 {
57     TRACE(ver, "%s %p (\"%s\") %s\n", prologue, 
58                 (void const *) teststring, 
59                 teststring ? teststring : "(null)",
60                 epilogue);
61 }
62
63 /******************************************************************************
64  *
65  *   This function will print via dprintf[_]ver to stddeb debug info regarding
66  *   the file info structure vffi.
67  *      15-Feb-1998 Dimitrie Paun (dimi@cs.toronto.edu)
68  *      Added this function to clean up the code.
69  *
70  *****************************************************************************/
71 static void print_vffi_debug(VS_FIXEDFILEINFO *vffi)
72 {
73         dbg_decl_str(ver, 1024);
74
75         TRACE(ver," structversion=0x%lx.0x%lx, fileversion=0x%lx.0x%lx, productversion=0x%lx.0x%lx, flagmask=0x%lx, flags=%s%s%s%s%s%s\n",
76                     (vffi->dwStrucVersion>>16),vffi->dwStrucVersion&0xFFFF,
77                     vffi->dwFileVersionMS,vffi->dwFileVersionLS,
78                     vffi->dwProductVersionMS,vffi->dwProductVersionLS,
79                     vffi->dwFileFlagsMask,
80                     (vffi->dwFileFlags & VS_FF_DEBUG) ? "DEBUG," : "",
81                     (vffi->dwFileFlags & VS_FF_PRERELEASE) ? "PRERELEASE," : "",
82                     (vffi->dwFileFlags & VS_FF_PATCHED) ? "PATCHED," : "",
83                     (vffi->dwFileFlags & VS_FF_PRIVATEBUILD) ? "PRIVATEBUILD," : "",
84                     (vffi->dwFileFlags & VS_FF_INFOINFERRED) ? "INFOINFERRED," : "",
85                     (vffi->dwFileFlags & VS_FF_SPECIALBUILD) ? "SPECIALBUILD," : ""
86                     );
87
88         dsprintf(ver," OS=0x%lx.0x%lx ",
89                 (vffi->dwFileOS&0xFFFF0000)>>16,
90                 vffi->dwFileOS&0x0000FFFF
91         );
92         switch (vffi->dwFileOS&0xFFFF0000) {
93         case VOS_DOS:dsprintf(ver,"DOS,");break;
94         case VOS_OS216:dsprintf(ver,"OS/2-16,");break;
95         case VOS_OS232:dsprintf(ver,"OS/2-32,");break;
96         case VOS_NT:dsprintf(ver,"NT,");break;
97         case VOS_UNKNOWN:
98         default:
99                 dsprintf(ver,"UNKNOWN(0x%lx),",vffi->dwFileOS&0xFFFF0000);break;
100         }
101         switch (vffi->dwFileOS & 0xFFFF) {
102         case VOS__BASE:dsprintf(ver,"BASE");break;
103         case VOS__WINDOWS16:dsprintf(ver,"WIN16");break;
104         case VOS__WINDOWS32:dsprintf(ver,"WIN32");break;
105         case VOS__PM16:dsprintf(ver,"PM16");break;
106         case VOS__PM32:dsprintf(ver,"PM32");break;
107         default:dsprintf(ver,"UNKNOWN(0x%lx)",vffi->dwFileOS&0xFFFF);break;
108         }
109         TRACE(ver, "(%s)\n", dbg_str(ver));
110
111         dbg_reset_str(ver);
112         switch (vffi->dwFileType) {
113         default:
114         case VFT_UNKNOWN:
115                 dsprintf(ver,"filetype=Unknown(0x%lx)",vffi->dwFileType);
116                 break;
117         case VFT_APP:dsprintf(ver,"filetype=APP,");break;
118         case VFT_DLL:dsprintf(ver,"filetype=DLL,");break;
119         case VFT_DRV:
120                 dsprintf(ver,"filetype=DRV,");
121                 switch(vffi->dwFileSubtype) {
122                 default:
123                 case VFT2_UNKNOWN:
124                         dsprintf(ver,"UNKNOWN(0x%lx)",vffi->dwFileSubtype);
125                         break;
126                 case VFT2_DRV_PRINTER:
127                         dsprintf(ver,"PRINTER");
128                         break;
129                 case VFT2_DRV_KEYBOARD:
130                         dsprintf(ver,"KEYBOARD");
131                         break;
132                 case VFT2_DRV_LANGUAGE:
133                         dsprintf(ver,"LANGUAGE");
134                         break;
135                 case VFT2_DRV_DISPLAY:
136                         dsprintf(ver,"DISPLAY");
137                         break;
138                 case VFT2_DRV_MOUSE:
139                         dsprintf(ver,"MOUSE");
140                         break;
141                 case VFT2_DRV_NETWORK:
142                         dsprintf(ver,"NETWORK");
143                         break;
144                 case VFT2_DRV_SYSTEM:
145                         dsprintf(ver,"SYSTEM");
146                         break;
147                 case VFT2_DRV_INSTALLABLE:
148                         dsprintf(ver,"INSTALLABLE");
149                         break;
150                 case VFT2_DRV_SOUND:
151                         dsprintf(ver,"SOUND");
152                         break;
153                 case VFT2_DRV_COMM:
154                         dsprintf(ver,"COMM");
155                         break;
156                 case VFT2_DRV_INPUTMETHOD:
157                         dsprintf(ver,"INPUTMETHOD");
158                         break;
159                 }
160                 break;
161         case VFT_FONT:
162                 dsprintf(ver,"filetype=FONT.");
163                 switch (vffi->dwFileSubtype) {
164                 default:
165                         dsprintf(ver,"UNKNOWN(0x%lx)",vffi->dwFileSubtype);
166                         break;
167                 case VFT2_FONT_RASTER:dsprintf(ver,"RASTER");break;
168                 case VFT2_FONT_VECTOR:dsprintf(ver,"VECTOR");break;
169                 case VFT2_FONT_TRUETYPE:dsprintf(ver,"TRUETYPE");break;
170                 }
171                 break;
172         case VFT_VXD:dsprintf(ver,"filetype=VXD");break;
173         case VFT_STATIC_LIB:dsprintf(ver,"filetype=STATIC_LIB");break;
174         }
175         TRACE(ver, "%s\n", dbg_str(ver));
176
177         TRACE(ver, "  filedata=0x%lx.0x%lx\n",
178                     vffi->dwFileDateMS,vffi->dwFileDateLS);
179 }
180
181 /******************************************************************************
182  *
183  *   int  testFileExistence(
184  *      char const * path,
185  *      char const * file )
186  *
187  *   Tests whether a given path/file combination exists.  If the file does
188  *   not exist, the return value is zero.  If it does exist, the return
189  *   value is non-zero.
190  *
191  *   Revision history
192  *      30-May-1997 Dave Cuthbert (dacut@ece.cmu.edu)
193  *         Original implementation
194  *
195  *****************************************************************************/
196
197 static int  testFileExistence(
198    char const * path,
199    char const * file )
200 {
201     char  filename[1024];
202     int  filenamelen;
203     OFSTRUCT  fileinfo;
204     int  retval;
205
206     fileinfo.cBytes = sizeof(OFSTRUCT);
207
208     strcpy(filename, path);
209     filenamelen = strlen(filename);
210
211     /* Add a trailing \ if necessary */
212     if(filenamelen) {
213         if(filename[filenamelen - 1] != '\\')
214             strcat(filename, "\\");
215     }
216     else /* specify the current directory */
217         strcpy(filename, ".\\");
218
219     /* Create the full pathname */
220     strcat(filename, file);
221
222     if(OpenFile32(filename, &fileinfo, OF_EXIST) == HFILE_ERROR32)
223         retval = 0;
224     else
225         retval = 1;
226
227     return  retval;
228 }
229
230 /******************************************************************************
231  *
232  *   int  testFileExclusiveExistence(
233  *      char const * path,
234  *      char const * file )
235  *
236  *   Tests whether a given path/file combination exists and ensures that no
237  *   other programs have handles to the given file.  If the file does not
238  *   exist or is open, the return value is zero.  If it does exist, the
239  *   return value is non-zero.
240  *
241  *   Revision history
242  *      30-May-1997 Dave Cuthbert (dacut@ece.cmu.edu)
243  *         Original implementation
244  *
245  *****************************************************************************/
246
247 static int  testFileExclusiveExistence(
248    char const * path,
249    char const * file )
250 {
251     char  filename[1024];
252     int  filenamelen;
253     OFSTRUCT  fileinfo;
254     int  retval;
255
256     fileinfo.cBytes = sizeof(OFSTRUCT);
257
258     strcpy(filename, path);
259     filenamelen = strlen(filename);
260
261     /* Add a trailing \ if necessary */
262     if(filenamelen) {
263         if(filename[filenamelen - 1] != '\\')
264             strcat(filename, "\\");
265     }
266     else /* specify the current directory */
267         strcpy(filename, ".\\");
268
269     /* Create the full pathname */
270     strcat(filename, file);
271
272     if(OpenFile32(filename, &fileinfo, OF_EXIST | OF_SHARE_EXCLUSIVE) ==
273        HFILE_ERROR32)
274         retval = 0;
275     else
276         retval = 1;
277
278     return retval;
279 }
280
281
282 static int read_xx_header(HFILE32 lzfd) {
283         IMAGE_DOS_HEADER        mzh;
284         char                    magic[3];
285
286         LZSeek32(lzfd,0,SEEK_SET);
287         if (sizeof(mzh)!=LZRead32(lzfd,&mzh,sizeof(mzh)))
288                 return 0;
289         if (mzh.e_magic!=IMAGE_DOS_SIGNATURE)
290                 return 0;
291         LZSeek32(lzfd,mzh.e_lfanew,SEEK_SET);
292         if (2!=LZRead32(lzfd,magic,2))
293                 return 0;
294         LZSeek32(lzfd,mzh.e_lfanew,SEEK_SET);
295         if (magic[0] == 'N' && magic[1] == 'E')
296                 return IMAGE_OS2_SIGNATURE;
297         if (magic[0] == 'P' && magic[1] == 'E')
298                 return IMAGE_NT_SIGNATURE;
299         magic[2]='\0';
300         WARN(ver,"Can't handle %s files.\n",magic);
301         return 0;
302 }
303
304
305 static int find_ne_resource(
306         HFILE32 lzfd,SEGPTR typeid,SEGPTR resid,
307         BYTE **resdata,int *reslen,DWORD *off
308 ) {
309         IMAGE_OS2_HEADER nehd;
310         NE_TYPEINFO     ti;
311         NE_NAMEINFO     ni;
312         int             i;
313         WORD            shiftcount;
314         DWORD           nehdoffset;
315
316         nehdoffset = LZTELL(lzfd);
317         LZREAD(&nehd);
318         if (nehd.resource_tab_offset==nehd.rname_tab_offset) {
319                 TRACE(ver,"no resources in NE dll\n");
320                 return 0;
321         }
322         LZSeek32(lzfd,nehd.resource_tab_offset+nehdoffset,SEEK_SET);
323         LZREAD(&shiftcount);
324         TRACE(ver,"shiftcount is %d\n",shiftcount);
325         TRACE(ver,"reading resource typeinfo dir.\n");
326
327         if (!HIWORD(typeid)) typeid = (SEGPTR)(LOWORD(typeid) | 0x8000);
328         if (!HIWORD(resid))  resid  = (SEGPTR)(LOWORD(resid) | 0x8000);
329         while (1) {
330                 int     skipflag;
331
332                 LZREAD(&ti);
333                 if (!ti.type_id)
334                         return 0;
335                 TRACE(ver,"    ti.typeid =%04x,count=%d\n",ti.type_id,ti.count);
336
337                 skipflag=0;
338                 if (!HIWORD(typeid)) {
339                         if ((ti.type_id&0x8000)&&(typeid!=ti.type_id))
340                                 skipflag=1;
341                 } else {
342                         if (ti.type_id & 0x8000) {
343                                 skipflag=1; 
344                         } else {
345                                 BYTE    len;
346                                 char    *str;
347                                 DWORD   whereleft;
348
349                                 whereleft = LZTELL(lzfd);
350                                 LZSeek32(
351                                         lzfd,
352                                         nehdoffset+nehd.resource_tab_offset+ti.type_id,
353                                         SEEK_SET
354                                 );
355                                 LZREAD(&len);
356                                 str=xmalloc(len);
357                                 if (len!=LZRead32(lzfd,str,len))
358                                         return 0;
359                                 TRACE(ver,"read %s to compare it with %s\n",
360                                         str,(char*)PTR_SEG_TO_LIN(typeid)
361                                 );
362                                 if (lstrcmpi32A(str,(char*)PTR_SEG_TO_LIN(typeid)))
363                                         skipflag=1;
364                                 free(str);
365                                 LZSeek32(lzfd,whereleft,SEEK_SET);
366                         }
367                 }
368                 if (skipflag) {
369                         LZSeek32(lzfd,ti.count*sizeof(ni),SEEK_CUR);
370                         continue;
371                 }
372                 for (i=0;i<ti.count;i++) {
373                         WORD    *rdata;
374                         int     len;
375
376                         LZREAD(&ni);
377                         TRACE(ver,"     ni.id=%4x,offset=%d,length=%d\n",
378                                 ni.id,ni.offset,ni.length
379                         );
380                         skipflag=1;
381                         if (!HIWORD(resid)) {
382                                 if (ni.id == resid)
383                                         skipflag=0;
384                         } else {
385                                 if (!(ni.id & 0x8000)) {
386                                         BYTE    len;
387                                         char    *str;
388                                         DWORD   whereleft;
389
390                                         whereleft = LZTELL(lzfd);
391                                           LZSeek32(
392                                                 lzfd,
393                                                 nehdoffset+nehd.resource_tab_offset+ni.id,
394                                                 SEEK_SET
395                                         );
396                                         LZREAD(&len);
397                                         str=xmalloc(len);
398                                         if (len!=LZRead32(lzfd,str,len))
399                                                 return 0;
400                                         TRACE(ver,"read %s to compare it with %s\n",
401                                                 str,(char*)PTR_SEG_TO_LIN(typeid)
402                                         );
403                                         if (!lstrcmpi32A(str,(char*)PTR_SEG_TO_LIN(typeid)))
404                                                 skipflag=0;
405                                         free(str);
406                                         LZSeek32(lzfd,whereleft,SEEK_SET);
407                                 }
408                         }
409                         if (skipflag)
410                                 continue;
411                         LZSeek32(lzfd,((int)ni.offset<<shiftcount),SEEK_SET);
412                         *off    = (int)ni.offset<<shiftcount;
413                         len     = ni.length<<shiftcount;
414                         rdata=(WORD*)xmalloc(len);
415                         if (len!=LZRead32(lzfd,rdata,len)) {
416                                 free(rdata);
417                                 return 0;
418                         }
419                         TRACE(ver,"resource found.\n");
420                         *resdata= (BYTE*)rdata;
421                         *reslen = len;
422                         return 1;
423                 }
424         }
425 }
426
427 /* Loads the specified PE resource.
428  * FIXME: shouldn't load the whole image
429  */
430 static int
431 find_pe_resource(
432         HFILE32 lzfd,LPWSTR typeid,LPWSTR resid,
433         BYTE **resdata,int *reslen,DWORD *off
434 ) {
435         IMAGE_NT_HEADERS pehd;
436         int             i;
437         UINT32          nrofsections;
438         DWORD           imagesize,pehdoffset;
439         BYTE            *image;
440         IMAGE_DATA_DIRECTORY            resdir;
441         LPIMAGE_RESOURCE_DIRECTORY      resourcedir,xresdir;
442         LPIMAGE_RESOURCE_DATA_ENTRY     xresdata;
443         LPIMAGE_SECTION_HEADER          sections;
444
445         pehdoffset = LZTELL(lzfd);
446         LZREAD(&pehd);
447         resdir = pehd.OptionalHeader.DataDirectory[IMAGE_FILE_RESOURCE_DIRECTORY];
448         TRACE(ver,"(.,%p,%p,....)\n",typeid,resid);
449         if (!resdir.Size) {
450                 WARN(ver,"No resource directory found in PE file.\n");
451                 return 0;
452         }
453         imagesize = pehd.OptionalHeader.SizeOfImage;
454         image = HeapAlloc(GetProcessHeap(),0,imagesize);
455         nrofsections = pehd.FileHeader.NumberOfSections;
456
457         sections = (LPIMAGE_SECTION_HEADER)HeapAlloc(GetProcessHeap(),0,pehd.FileHeader.NumberOfSections*sizeof(IMAGE_SECTION_HEADER));
458         LZSeek32(lzfd,
459                 pehdoffset+
460                 sizeof(DWORD)+  /* Signature */
461                 sizeof(IMAGE_FILE_HEADER)+      
462                 pehd.FileHeader.SizeOfOptionalHeader,
463                 SEEK_SET
464         );
465         if (    nrofsections*sizeof(IMAGE_SECTION_HEADER)!=
466                 LZRead32(lzfd,sections,nrofsections*sizeof(IMAGE_SECTION_HEADER))
467         ) {
468                 HeapFree(GetProcessHeap(),0,image);
469                 return 0;
470         }
471         for (i=0;i<nrofsections;i++) {
472                 if (sections[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
473                         continue;
474                 LZSeek32(lzfd,sections[i].PointerToRawData,SEEK_SET);
475                 if (    sections[i].SizeOfRawData!=
476                         LZRead32(lzfd,image+sections[i].VirtualAddress,sections[i].SizeOfRawData)
477                 ) {
478                         HeapFree(GetProcessHeap(),0,image);
479                         return 0;
480                 }
481         }
482         resourcedir = (LPIMAGE_RESOURCE_DIRECTORY)(image+resdir.VirtualAddress);
483         xresdir = GetResDirEntryW(resourcedir,typeid,(DWORD)resourcedir,FALSE);
484         if (!xresdir) {
485                 TRACE(ver,"...no typeid entry found for %p\n",typeid);
486                 HeapFree(GetProcessHeap(),0,image);
487                 return 0;
488         }
489         xresdir = GetResDirEntryW(xresdir,resid,(DWORD)resourcedir,FALSE);
490         if (!xresdir) {
491                 TRACE(ver,"...no resid entry found for %p\n",resid);
492                 HeapFree(GetProcessHeap(),0,image);
493                 return 0;
494         }
495         
496         xresdir = GetResDirEntryW(xresdir,0,(DWORD)resourcedir,TRUE);
497         if (!xresdir) {
498                 TRACE(ver,"...no 0 (default language) entry found for %p\n",resid);
499                 HeapFree(GetProcessHeap(),0,image);
500                 return 0;
501         }
502         xresdata = (LPIMAGE_RESOURCE_DATA_ENTRY)xresdir;
503         *reslen = xresdata->Size;
504         *resdata= (LPBYTE)xmalloc(*reslen);
505         memcpy(*resdata,image+xresdata->OffsetToData,*reslen);
506         /* find physical address for virtual offset */
507         for (i=0;i<nrofsections;i++) {
508                 if (sections[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
509                         continue;
510                 if (    (xresdata->OffsetToData >= sections[i].VirtualAddress)&&
511                         (xresdata->OffsetToData < sections[i].VirtualAddress+sections[i].SizeOfRawData)
512                 ) {
513                         *off = (DWORD)(xresdata->OffsetToData)-(DWORD)(sections[i].VirtualAddress)+(DWORD)(sections[i].PointerToRawData);
514                         break;
515                 }
516         }
517         HeapFree(GetProcessHeap(),0,image);
518         HeapFree(GetProcessHeap(),0,sections);
519         return 1;
520 }
521
522 /* GetFileResourceSize                          [VER.2] */
523 DWORD WINAPI GetFileResourceSize(LPCSTR filename,SEGPTR restype,SEGPTR resid,
524                                  LPDWORD off)
525 {
526         HFILE32                 lzfd;
527         OFSTRUCT                ofs;
528         BYTE                    *resdata = NULL;
529         int                     reslen=0;
530         int                     res=0;
531
532         TRACE(ver,"(%s,%lx,%lx,%p)\n",
533                 filename,(LONG)restype,(LONG)resid,off
534         );
535         lzfd=LZOpenFile32A(filename,&ofs,OF_READ);
536         if (!lzfd)
537                 return 0;
538         switch (read_xx_header(lzfd)) {
539         case 0:
540             res=0;
541             break;
542         case IMAGE_OS2_SIGNATURE:
543             res=find_ne_resource(lzfd,restype,resid,&resdata,&reslen,off);
544             break;
545         case IMAGE_NT_SIGNATURE:
546             res=find_pe_resource(lzfd,(LPWSTR)restype,(LPWSTR)resid,&resdata,&reslen,off);
547             break;
548         }
549         if (!res) {
550             LZClose32(lzfd);
551             return 0;
552         }
553         if (resdata)
554                 free(resdata);
555         LZClose32(lzfd);
556         return reslen;
557 }
558
559 /* GetFileResource                              [VER.3] */
560 DWORD WINAPI GetFileResource(LPCSTR filename,SEGPTR restype,SEGPTR resid,
561                              DWORD off,DWORD datalen,LPVOID data )
562 {
563         HFILE32                 lzfd;
564         OFSTRUCT                ofs;
565         BYTE                    *resdata=NULL;
566         int                     res=0;
567         int                     reslen=datalen;
568
569         TRACE(ver,"(%s,%lx,%lx,%ld,%ld,%p)\n",
570                 filename,(LONG)restype,(LONG)resid,off,datalen,data
571         );
572
573         lzfd=LZOpenFile32A(filename,&ofs,OF_READ);
574         if (lzfd==0)
575                 return 0;
576         if (!off) {
577                 switch (read_xx_header(lzfd)) {
578                 case 0: res=0;
579                         break;
580                 case IMAGE_OS2_SIGNATURE:
581                         res= find_ne_resource(lzfd,restype,resid,&resdata,&reslen,&off);
582                         break;
583                 case IMAGE_NT_SIGNATURE:
584                         res= find_pe_resource(lzfd,(LPWSTR)restype,(LPWSTR)resid,&resdata,&reslen,&off);
585                         break;
586                 }
587                 LZClose32(lzfd);
588                 if (!res)
589                         return 0;
590                 if (reslen>datalen) reslen = datalen;
591                 memcpy(data,resdata,reslen);
592                 free(resdata);
593                 return reslen;
594         }
595         LZSeek32(lzfd,off,SEEK_SET);
596         reslen = LZRead32(lzfd,data,datalen);
597         LZClose32(lzfd);
598         return reslen;
599 }
600
601 /* GetFileVersionInfoSize                       [VER.6] */
602 DWORD WINAPI GetFileVersionInfoSize16(LPCSTR filename,LPDWORD handle)
603 {
604         DWORD   len,ret,isuni=0;
605         BYTE    buf[144];
606         VS_FIXEDFILEINFO *vffi;
607
608         TRACE(ver,"(%s,%p)\n",filename,handle);
609         len=GetFileResourceSize(filename,VS_FILE_INFO,VS_VERSION_INFO,handle);
610         if (!len)
611                 return 0;
612         ret=GetFileResource(
613                 filename,VS_FILE_INFO,VS_VERSION_INFO,*handle,sizeof(buf),buf
614         );
615         if (!ret)
616                 return 0;
617
618         vffi=(VS_FIXEDFILEINFO*)(buf+0x14);
619         if (vffi->dwSignature != VS_FFI_SIGNATURE) {
620                 /* unicode resource */
621                 if (vffi->dwSignature == 0x004f0049) {
622                         isuni = 1;
623                         vffi = (VS_FIXEDFILEINFO*)(buf+0x28);
624                 } else {
625                         WARN(ver,"vffi->dwSignature is 0x%08lx, but not 0x%08lx!\n",
626                                 vffi->dwSignature,VS_FFI_SIGNATURE
627                         );
628                         return 0;
629                 }
630         }
631         if (*(WORD*)buf < len)
632                 len = *(WORD*)buf;
633
634         if(TRACE_ON(ver))
635           print_vffi_debug(vffi);
636
637         return len;
638 }
639
640 /* GetFileVersionInfoSize32A                    [VERSION.1] */
641 DWORD WINAPI GetFileVersionInfoSize32A(LPCSTR filename,LPDWORD handle)
642 {
643         TRACE(ver,"(%s,%p)\n",filename,handle);
644         return GetFileVersionInfoSize16(filename,handle);
645 }
646
647 /* GetFileVersionInfoSize32W                    [VERSION.2] */
648 DWORD WINAPI GetFileVersionInfoSize32W( LPCWSTR filename, LPDWORD handle )
649 {
650     LPSTR xfn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
651     DWORD ret = GetFileVersionInfoSize16( xfn, handle );
652     HeapFree( GetProcessHeap(), 0, xfn );
653     return ret;
654 }
655
656 /* GetFileVersionInfo                           [VER.7] */
657 DWORD  WINAPI GetFileVersionInfo16(LPCSTR filename,DWORD handle,DWORD datasize,
658                                    LPVOID data)
659 {
660         TRACE(ver,"(%s,%ld,%ld,%p)\n",
661                 filename,handle,datasize,data
662         );
663         return GetFileResource(
664                 filename,VS_FILE_INFO,VS_VERSION_INFO,handle,datasize,data
665         );
666 }
667
668 /* GetFileVersionInfoA                          [VERSION.0] */
669 DWORD  WINAPI GetFileVersionInfo32A(LPCSTR filename,DWORD handle,
670                                     DWORD datasize,LPVOID data)
671 {
672         return GetFileVersionInfo16(filename,handle,datasize,data);
673 }
674
675 /* GetFileVersionInfoW                          [VERSION.3] */
676 DWORD WINAPI GetFileVersionInfo32W( LPCWSTR filename, DWORD handle,
677                                     DWORD datasize, LPVOID data)
678 {
679     LPSTR fn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
680     DWORD ret = GetFileVersionInfo16( fn, handle, datasize, data );
681     HeapFree( GetProcessHeap(), 0, fn );
682     return ret;
683 }
684
685 /*****************************************************************************
686  *
687  *   VerFindFile() [VER.8]
688  *   Determines where to install a file based on whether it locates another
689  *   version of the file in the system.  The values VerFindFile returns are
690  *   used in a subsequent call to the VerInstallFile function.
691  *
692  *   Revision history:
693  *      30-May-1997   Dave Cuthbert (dacut@ece.cmu.edu)
694  *         Reimplementation of VerFindFile from original stub.
695  *
696  ****************************************************************************/
697
698 DWORD WINAPI VerFindFile16(
699     UINT16 flags,
700     LPCSTR lpszFilename,
701     LPCSTR lpszWinDir,
702     LPCSTR lpszAppDir,
703     LPSTR lpszCurDir,
704     UINT16 *lpuCurDirLen,
705     LPSTR lpszDestDir,
706     UINT16 *lpuDestDirLen )
707 {
708     DWORD  retval;
709     char  curDir[256];
710     char  destDir[256];
711     unsigned int  curDirSizeReq;
712     unsigned int  destDirSizeReq;
713
714     retval = 0;
715
716     /* Print out debugging information */
717     TRACE(ver, "called with parameters:\n"
718                  "\tflags = %x", flags);
719     if(flags & VFFF_ISSHAREDFILE)
720         TRACE(ver, " (VFFF_ISSHAREDFILE)\n");
721     else
722         TRACE(ver, "\n");
723
724     ver_dstring("\tlpszFilename = ", lpszFilename, "");
725     ver_dstring("\tlpszWinDir = ", lpszWinDir, "");
726     ver_dstring("\tlpszAppDir = ", lpszAppDir, "");
727
728     TRACE(ver, "\tlpszCurDir = %p\n", lpszCurDir);
729     if(lpuCurDirLen)
730         TRACE(ver, "\tlpuCurDirLen = %p (%u)\n",
731                     lpuCurDirLen, *lpuCurDirLen);
732     else
733         TRACE(ver, "\tlpuCurDirLen = (null)\n");
734
735     TRACE(ver, "\tlpszDestDir = %p\n", lpszDestDir);
736     if(lpuDestDirLen)
737         TRACE(ver, "\tlpuDestDirLen = %p (%u)\n",
738                     lpuDestDirLen, *lpuDestDirLen);
739
740     /* Figure out where the file should go; shared files default to the
741        system directory */
742
743     strcpy(curDir, "");
744     strcpy(destDir, "");
745
746     if(flags & VFFF_ISSHAREDFILE) {
747         GetSystemDirectory32A(destDir, 256);
748
749         /* Were we given a filename?  If so, try to find the file. */
750         if(lpszFilename) {
751             if(testFileExistence(destDir, lpszFilename)) {
752                 strcpy(curDir, destDir);
753
754                 if(!testFileExclusiveExistence(destDir, lpszFilename))
755                     retval |= VFF_FILEINUSE;
756             }
757             else if(lpszAppDir && testFileExistence(lpszAppDir,
758                                                     lpszFilename)) {
759                 strcpy(curDir, lpszAppDir);
760                 retval |= VFF_CURNEDEST;
761
762                 if(!testFileExclusiveExistence(lpszAppDir, lpszFilename))
763                     retval |= VFF_FILEINUSE;
764             }
765         }
766     }
767     else if(!(flags & VFFF_ISSHAREDFILE)) { /* not a shared file */
768         if(lpszAppDir) {
769             char  systemDir[256];
770             GetSystemDirectory32A(systemDir, 256);
771
772             strcpy(destDir, lpszAppDir);
773
774             if(lpszFilename) {
775                 if(testFileExistence(lpszAppDir, lpszFilename)) {
776                     strcpy(curDir, lpszAppDir);
777
778                     if(!testFileExclusiveExistence(lpszAppDir, lpszFilename))
779                         retval |= VFF_FILEINUSE;
780                 }
781                 else if(testFileExistence(systemDir, lpszFilename)) {
782                     strcpy(curDir, systemDir);
783                     retval |= VFF_CURNEDEST;
784
785                     if(!testFileExclusiveExistence(systemDir, lpszFilename))
786                         retval |= VFF_FILEINUSE;
787                 }
788             }
789         }
790     }
791
792     curDirSizeReq = strlen(curDir) + 1;
793     destDirSizeReq = strlen(destDir) + 1;
794
795
796
797     /* Make sure that the pointers to the size of the buffers are
798        valid; if not, do NOTHING with that buffer.  If that pointer
799        is valid, then make sure that the buffer pointer is valid, too! */
800
801     if(lpuDestDirLen && lpszDestDir) {
802         if(*lpuDestDirLen < destDirSizeReq) {
803             retval |= VFF_BUFFTOOSMALL;
804             strncpy(lpszDestDir, destDir, *lpuDestDirLen - 1);
805             lpszDestDir[*lpuDestDirLen - 1] = '\0';
806         }
807         else
808             strcpy(lpszDestDir, destDir);
809
810         *lpuDestDirLen = destDirSizeReq;
811     }
812     
813     if(lpuCurDirLen && lpszCurDir) {
814         if(*lpuCurDirLen < curDirSizeReq) {
815             retval |= VFF_BUFFTOOSMALL;
816             strncpy(lpszCurDir, curDir, *lpuCurDirLen - 1);
817             lpszCurDir[*lpuCurDirLen - 1] = '\0';
818         }
819         else
820             strcpy(lpszCurDir, curDir);
821
822         *lpuCurDirLen = curDirSizeReq;
823     }
824
825     TRACE(ver, "ret = %lu (%s%s%s)\n", retval,
826                  (retval & VFF_CURNEDEST) ? "VFF_CURNEDEST " : "",
827                  (retval & VFF_FILEINUSE) ? "VFF_FILEINUSE " : "",
828                  (retval & VFF_BUFFTOOSMALL) ? "VFF_BUFFTOOSMALL " : "");
829
830     ver_dstring("\t(Exit) lpszCurDir = ", lpszCurDir, "");
831     if(lpuCurDirLen)
832         TRACE(ver, "\t(Exit) lpuCurDirLen = %p (%u)\n",
833                     lpuCurDirLen, *lpuCurDirLen);
834     else
835         TRACE(ver, "\t(Exit) lpuCurDirLen = (null)\n");
836
837     ver_dstring("\t(Exit) lpszDestDir = ", lpszDestDir, "");
838     if(lpuDestDirLen)
839         TRACE(ver, "\t(Exit) lpuDestDirLen = %p (%u)\n",
840                     lpuDestDirLen, *lpuDestDirLen);
841
842     return retval;
843 }
844
845 /* VerFindFileA                                         [VERSION.5] */
846 DWORD WINAPI VerFindFile32A(
847         UINT32 flags,LPCSTR filename,LPCSTR windir,LPCSTR appdir,
848         LPSTR curdir,UINT32 *pcurdirlen,LPSTR destdir,UINT32 *pdestdirlen )
849 {
850     UINT16 curdirlen, destdirlen;
851     DWORD ret;
852     
853     curdirlen = (UINT16)*pcurdirlen;
854     destdirlen= (UINT16)*pdestdirlen;
855
856     ret = VerFindFile16(flags,filename,windir,appdir,
857                         curdir,&curdirlen,destdir,&destdirlen);
858     *pcurdirlen = curdirlen;
859     *pdestdirlen = destdirlen;
860     return ret;
861 }
862
863 /* VerFindFileW                                         [VERSION.6] */
864 DWORD WINAPI VerFindFile32W(
865         UINT32 flags,LPCWSTR filename,LPCWSTR windir,LPCWSTR appdir,
866         LPWSTR curdir,UINT32 *pcurdirlen,LPWSTR destdir,UINT32 *pdestdirlen )
867 {
868     UINT16 curdirlen, destdirlen;
869     LPSTR wfn,wwd,wad,wdd,wcd;
870     DWORD ret;
871
872     wfn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
873     wwd = HEAP_strdupWtoA( GetProcessHeap(), 0, windir );
874     wad = HEAP_strdupWtoA( GetProcessHeap(), 0, appdir );
875     wcd = HeapAlloc( GetProcessHeap(), 0, *pcurdirlen );
876     wdd = HeapAlloc( GetProcessHeap(), 0, *pdestdirlen );
877     ret = VerFindFile16(flags,wfn,wwd,wad,wcd,&curdirlen,wdd,&destdirlen);
878     lstrcpynAtoW(curdir,wcd,*pcurdirlen);
879     lstrcpynAtoW(destdir,wdd,*pdestdirlen);
880     *pcurdirlen = strlen(wcd);
881     *pdestdirlen = strlen(wdd);
882     HeapFree( GetProcessHeap(), 0, wfn );
883     HeapFree( GetProcessHeap(), 0, wwd );
884     HeapFree( GetProcessHeap(), 0, wad );
885     HeapFree( GetProcessHeap(), 0, wcd );
886     HeapFree( GetProcessHeap(), 0, wdd );
887     return ret;
888 }
889
890 /* VerInstallFile                                       [VER.9] */
891 DWORD WINAPI VerInstallFile16(
892         UINT16 flags,LPCSTR srcfilename,LPCSTR destfilename,LPCSTR srcdir,
893         LPCSTR destdir,LPCSTR curdir,LPSTR tmpfile,UINT16 *tmpfilelen )
894 {
895     UINT32      filelen;
896     DWORD ret= VerInstallFile32A(flags,srcfilename,destfilename,srcdir,
897                                  destdir,curdir,tmpfile,&filelen);
898
899     *tmpfilelen = filelen;
900     return ret;
901 }
902
903 static LPBYTE
904 _fetch_versioninfo(LPSTR fn,VS_FIXEDFILEINFO **vffi) {
905     DWORD       alloclen;
906     LPBYTE      buf;
907     DWORD       ret;
908
909     alloclen = 1000;
910     buf= xmalloc(alloclen);
911     while (1) {
912         ret = GetFileVersionInfo32A(fn,0,alloclen,buf);
913         if (!ret) {
914             free(buf);
915             return 0;
916         }
917         if (alloclen<*(WORD*)buf) {
918             free(buf);
919             alloclen = *(WORD*)buf;
920             buf = xmalloc(alloclen);
921         } else {
922             *vffi = (VS_FIXEDFILEINFO*)(buf+0x14);
923             if ((*vffi)->dwSignature == 0x004f0049) /* hack to detect unicode */
924                 *vffi = (VS_FIXEDFILEINFO*)(buf+0x28);
925             if ((*vffi)->dwSignature != VS_FFI_SIGNATURE)
926                 WARN(ver,"Bad VS_FIXEDFILEINFO signature 0x%08lx\n",(*vffi)->dwSignature);
927             return buf;
928         }
929     }
930 }
931
932 static DWORD
933 _error2vif(DWORD error) {
934     switch (error) {
935     case ERROR_ACCESS_DENIED:
936         return VIF_ACCESSVIOLATION;
937     case ERROR_SHARING_VIOLATION:
938         return VIF_SHARINGVIOLATION;
939     default:
940         return 0;
941     }
942 }
943
944
945 /******************************************************************************
946  * VerInstallFile32A [VERSION.7]
947  */
948 DWORD WINAPI VerInstallFile32A(
949         UINT32 flags,LPCSTR srcfilename,LPCSTR destfilename,LPCSTR srcdir,
950         LPCSTR destdir,LPCSTR curdir,LPSTR tmpfile,UINT32 *tmpfilelen )
951 {
952     LPCSTR pdest;
953     char        destfn[260],tmpfn[260],srcfn[260];
954     HFILE32     hfsrc,hfdst;
955     DWORD       attr,ret,xret,tmplast;
956     LPBYTE      buf1,buf2;
957     OFSTRUCT    ofs;
958
959     TRACE(ver,"(%x,%s,%s,%s,%s,%s,%p,%d)\n",
960             flags,srcfilename,destfilename,srcdir,destdir,curdir,tmpfile,*tmpfilelen
961     );
962     xret = 0;
963     sprintf(srcfn,"%s\\%s",srcdir,srcfilename);
964     if (!destdir || !*destdir) pdest = srcdir;
965     else pdest = destdir;
966     sprintf(destfn,"%s\\%s",pdest,destfilename);
967     hfsrc=LZOpenFile32A(srcfn,&ofs,OF_READ);
968     if (hfsrc==HFILE_ERROR32)
969         return VIF_CANNOTREADSRC;
970     sprintf(tmpfn,"%s\\%s",pdest,destfilename);
971     tmplast=strlen(pdest)+1;
972     attr = GetFileAttributes32A(tmpfn);
973     if (attr!=-1) {
974         if (attr & FILE_ATTRIBUTE_READONLY) {
975             LZClose32(hfsrc);
976             return VIF_WRITEPROT;
977         }
978         /* FIXME: check if file currently in use and return VIF_FILEINUSE */
979     }
980     attr = -1;
981     if (flags & VIFF_FORCEINSTALL) {
982         if (tmpfile[0]) {
983             sprintf(tmpfn,"%s\\%s",pdest,tmpfile);
984             tmplast = strlen(pdest)+1;
985             attr = GetFileAttributes32A(tmpfn);
986             /* if it exists, it has been copied by the call before.
987              * we jump over the copy part... 
988              */
989         }
990     }
991     if (attr == -1) {
992         char    *s;
993
994         GetTempFileName32A(pdest,"ver",0,tmpfn); /* should not fail ... */
995         s=strrchr(tmpfn,'\\');
996         if (s)
997             tmplast = s-tmpfn;
998         else
999             tmplast = 0;
1000         hfdst = OpenFile32(tmpfn,&ofs,OF_CREATE);
1001         if (hfdst == HFILE_ERROR32) {
1002             LZClose32(hfsrc);
1003             return VIF_CANNOTCREATE; /* | translated dos error */
1004         }
1005         ret = LZCopy32(hfsrc,hfdst);
1006         _lclose32(hfdst);
1007         if (((long) ret) < 0) {
1008             /* translate LZ errors into VIF_xxx */
1009             switch (ret) {
1010             case LZERROR_BADINHANDLE:
1011             case LZERROR_READ:
1012             case LZERROR_BADVALUE:
1013             case LZERROR_UNKNOWNALG:
1014                 ret = VIF_CANNOTREADSRC;
1015                 break;
1016             case LZERROR_BADOUTHANDLE:
1017             case LZERROR_WRITE:
1018                 ret = VIF_OUTOFMEMORY; /* FIXME: correct? */
1019                 break;
1020             case LZERROR_GLOBALLOC:
1021             case LZERROR_GLOBLOCK:
1022                 ret = VIF_OUTOFSPACE;
1023                 break;
1024             default: /* unknown error, should not happen */
1025                 ret = 0;
1026                 break;
1027             }
1028             if (ret) {
1029                 LZClose32(hfsrc);
1030                 return ret;
1031             }
1032         }
1033     }
1034     xret = 0;
1035     if (!(flags & VIFF_FORCEINSTALL)) {
1036         VS_FIXEDFILEINFO *destvffi,*tmpvffi;
1037         buf1 = _fetch_versioninfo(destfn,&destvffi);
1038         if (buf1) {
1039             buf2 = _fetch_versioninfo(tmpfn,&tmpvffi);
1040             if (buf2) {
1041                 char    *tbuf1,*tbuf2;
1042                 UINT32  len1,len2;
1043
1044                 len1=len2=40;
1045
1046                 /* compare file versions */
1047                 if ((destvffi->dwFileVersionMS > tmpvffi->dwFileVersionMS)||
1048                     ((destvffi->dwFileVersionMS==tmpvffi->dwFileVersionMS)&&
1049                      (destvffi->dwFileVersionLS > tmpvffi->dwFileVersionLS)
1050                     )
1051                 )
1052                     xret |= VIF_MISMATCH|VIF_SRCOLD;
1053                 /* compare filetypes and filesubtypes */
1054                 if ((destvffi->dwFileType!=tmpvffi->dwFileType) ||
1055                     (destvffi->dwFileSubtype!=tmpvffi->dwFileSubtype)
1056                 )
1057                     xret |= VIF_MISMATCH|VIF_DIFFTYPE;
1058                 if (VerQueryValue32A(buf1,"\\VarFileInfo\\Translation",(LPVOID*)&tbuf1,&len1) &&
1059                     VerQueryValue32A(buf2,"\\VarFileInfo\\Translation",(LPVOID*)&tbuf2,&len2)
1060                 ) {
1061                     /* irgendwas mit tbuf1 und tbuf2 machen 
1062                      * generiert DIFFLANG|MISMATCH
1063                      */
1064                 }
1065                 free(buf2);
1066             } else
1067                 xret=VIF_MISMATCH|VIF_SRCOLD;
1068             free(buf1);
1069         }
1070     }
1071     if (xret) {
1072         if (*tmpfilelen<strlen(tmpfn+tmplast)) {
1073             xret|=VIF_BUFTOSMALL;
1074             DeleteFile32A(tmpfn);
1075         } else {
1076             strcpy(tmpfile,tmpfn+tmplast);
1077             *tmpfilelen = strlen(tmpfn+tmplast)+1;
1078             xret|=VIF_TEMPFILE;
1079         }
1080     } else {
1081         if (-1!=GetFileAttributes32A(destfn))
1082             if (!DeleteFile32A(destfn)) {
1083                 xret|=_error2vif(GetLastError())|VIF_CANNOTDELETE;
1084                 DeleteFile32A(tmpfn);
1085                 LZClose32(hfsrc);
1086                 return xret;
1087             }
1088         if ((!(flags & VIFF_DONTDELETEOLD))     && 
1089             curdir                              && 
1090             *curdir                             &&
1091             lstrcmpi32A(curdir,pdest)
1092         ) {
1093             char curfn[260];
1094
1095             sprintf(curfn,"%s\\%s",curdir,destfilename);
1096             if (-1!=GetFileAttributes32A(curfn)) {
1097                 /* FIXME: check if in use ... if it is, VIF_CANNOTDELETECUR */
1098                 if (!DeleteFile32A(curfn))
1099                     xret|=_error2vif(GetLastError())|VIF_CANNOTDELETECUR;
1100             }
1101         }
1102         if (!MoveFile32A(tmpfn,destfn)) {
1103             xret|=_error2vif(GetLastError())|VIF_CANNOTRENAME;
1104             DeleteFile32A(tmpfn);
1105         }
1106     }
1107     LZClose32(hfsrc);
1108     return xret;
1109 }
1110
1111
1112 /* VerInstallFileW                              [VERSION.8] */
1113 DWORD WINAPI VerInstallFile32W(
1114         UINT32 flags,LPCWSTR srcfilename,LPCWSTR destfilename,LPCWSTR srcdir,
1115         LPCWSTR destdir,LPCWSTR curdir,LPWSTR tmpfile,UINT32 *tmpfilelen )
1116 {
1117     LPSTR wsrcf,wsrcd,wdestf,wdestd,wtmpf,wcurd;
1118     DWORD ret;
1119
1120     wsrcf  = HEAP_strdupWtoA( GetProcessHeap(), 0, srcfilename );
1121     wsrcd  = HEAP_strdupWtoA( GetProcessHeap(), 0, srcdir );
1122     wdestf = HEAP_strdupWtoA( GetProcessHeap(), 0, destfilename );
1123     wdestd = HEAP_strdupWtoA( GetProcessHeap(), 0, destdir );
1124     wtmpf  = HEAP_strdupWtoA( GetProcessHeap(), 0, tmpfile );
1125     wcurd  = HEAP_strdupWtoA( GetProcessHeap(), 0, curdir );
1126     ret = VerInstallFile32A(flags,wsrcf,wdestf,wsrcd,wdestd,wcurd,wtmpf,tmpfilelen);
1127     if (!ret)
1128         lstrcpynAtoW(tmpfile,wtmpf,*tmpfilelen);
1129     HeapFree( GetProcessHeap(), 0, wsrcf );
1130     HeapFree( GetProcessHeap(), 0, wsrcd );
1131     HeapFree( GetProcessHeap(), 0, wdestf );
1132     HeapFree( GetProcessHeap(), 0, wdestd );
1133     HeapFree( GetProcessHeap(), 0, wtmpf );
1134     if (wcurd) 
1135         HeapFree( GetProcessHeap(), 0, wcurd );
1136     return ret;
1137 }
1138
1139
1140 struct dbA {
1141         WORD    nextoff;
1142         WORD    datalen;
1143 /* in memory structure... */
1144         char    name[1];        /* padded to dword alignment */
1145 /* .... 
1146         char    data[datalen];     padded to dword alignment
1147         BYTE    subdirdata[];      until nextoff
1148  */
1149 };
1150
1151 #define DATA_OFFSET_A(db) ((4+(strlen((db)->name)+4))&~3)
1152
1153 struct dbW {
1154         WORD    nextoff;
1155         WORD    datalen;
1156         WORD    btext;          /* type of data */
1157 /* in memory structure... */
1158         WCHAR   name[1];        /* padded to dword alignment */
1159 /* .... 
1160         WCHAR   data[datalen];     padded to dword alignment
1161         BYTE    subdirdata[];      until nextoff
1162  */
1163 };
1164
1165 /* WORD nextoffset;
1166  * WORD datalength;
1167  * WORD btype;
1168  * WCHAR szKey[]; (zero terminated)
1169  * PADDING (round up to nearest 32bit boundary)
1170  */
1171 #define DATA_OFFSET_W(db) ((2+2+2+((lstrlen32W((db)->name)+1)*2+3))&~3)
1172
1173 /* this one used for Win16 resources, which are always in ASCII format */
1174 static BYTE*
1175 _find_dataA(BYTE *block,LPCSTR str, int buff_remain) {
1176         char    *nextslash;
1177         int     substrlen, inc_size;
1178         struct  dbA     *db;
1179
1180         while (*str && *str=='\\')
1181                 str++;
1182         if (NULL!=(nextslash=strchr(str,'\\')))
1183                 substrlen=nextslash-str;
1184         else
1185                 substrlen=strlen(str);
1186         if (nextslash!=NULL) {
1187                 while (*nextslash && *nextslash=='\\')
1188                         nextslash++;
1189                 if (!*nextslash)
1190                         nextslash=NULL;
1191         } else if (*str == 0)
1192                 return NULL;
1193
1194
1195         while (1) {
1196                 db=(struct dbA*)block;
1197                 TRACE(ver,"db=%p,db->nextoff=%d,db->datalen=%d,db->name=%s\n",
1198                         db,db->nextoff,db->datalen,db->name
1199                 );
1200                 if ((!db->nextoff) || (buff_remain<=0)) /* no more entries ? */
1201                         return NULL;
1202
1203                 TRACE(ver,"comparing with %s\n",db->name);
1204                 if (!lstrncmpi32A(db->name,str,substrlen)) {
1205                         if (nextslash) {
1206                                 inc_size=DATA_OFFSET_A(db)+((db->datalen+3)&~3);
1207                                 return _find_dataA(block+inc_size,nextslash,
1208                                                         db->nextoff-inc_size);
1209                         } else
1210                                 return block;
1211                 }
1212                 inc_size         = ((db->nextoff+3)&~3);
1213                 block           += inc_size;
1214                 buff_remain     -= inc_size;
1215         }
1216 }
1217
1218 /* this one used for Win32 resources, which are always in UNICODE format */
1219 extern LPWSTR __cdecl CRTDLL_wcschr(LPCWSTR str,WCHAR xchar);
1220 static BYTE*
1221 _find_dataW(BYTE *block,LPCWSTR str, int buff_remain) {
1222         LPWSTR  nextslash;
1223         int     substrlen, inc_size;
1224         struct  dbW     *db;
1225
1226
1227         while (*str && *str=='\\')
1228                 str++;
1229         if (NULL!=(nextslash=CRTDLL_wcschr(str,'\\')))
1230                 substrlen=nextslash-str;
1231         else
1232                 substrlen=lstrlen32W(str);
1233         if (nextslash!=NULL) {
1234                 while (*nextslash && *nextslash=='\\')
1235                         nextslash++;
1236                 if (!*nextslash)
1237                         nextslash=NULL;
1238         } else if (*str == 0)
1239                 return NULL;
1240
1241
1242         while (1) {
1243                 char    *xs,*vs;
1244                 db=(struct dbW*)block;
1245                 xs= HEAP_strdupWtoA(GetProcessHeap(),0,db->name);
1246                 if (db->datalen) {
1247                         if (db->btext)
1248                                 vs = HEAP_strdupWtoA(GetProcessHeap(),0,(WCHAR*)((block+DATA_OFFSET_W(db))));
1249                         else
1250                                 vs = HEAP_strdupA(GetProcessHeap(),0,"not a string");
1251                 } else
1252                         vs = HEAP_strdupA(GetProcessHeap(),0,"no data");
1253
1254                 TRACE(ver,"db->nextoff=%d,db->name=%s,db->data=\"%s\"\n",
1255                         db->nextoff,xs,vs
1256                 );
1257                 HeapFree(GetProcessHeap(),0,vs);
1258                 HeapFree(GetProcessHeap(),0,xs);
1259                 if ((!db->nextoff) || (buff_remain<=0)) /* no more entries ? */
1260                         return NULL;
1261
1262                 if (!lstrncmpi32W(db->name,str,substrlen)) {
1263                         if (nextslash) {
1264                                 /* DATA_OFFSET_W(db) (padded to 32bit already)
1265                                  * DATA[datalength]
1266                                  * PADDING (round up to nearest 32bit boundary)
1267                                  * -->  next level structs
1268                                  */
1269                                 inc_size=DATA_OFFSET_W(db)+((db->datalen+3)&~3);
1270                                 return _find_dataW( block+inc_size ,nextslash,
1271                                                         db->nextoff-inc_size);
1272                         } else
1273                                 return block;
1274                 }
1275                 /* skip over this block, round up to nearest 32bit boundary */
1276                 inc_size        =  ((db->nextoff+3)&~3);
1277                 block           += inc_size;
1278                 buff_remain     -= inc_size;
1279         }
1280 }
1281
1282 /* VerQueryValue                        [VER.11] */
1283 /* take care, 'buffer' is NOT a SEGPTR, it just points to one */
1284 DWORD WINAPI VerQueryValue16(SEGPTR segblock,LPCSTR subblock,SEGPTR *buffer,
1285                              UINT16 *buflen)
1286 {
1287         LPSTR   s;
1288         BYTE    *block=PTR_SEG_TO_LIN(segblock),*b;
1289
1290         TRACE(ver,"(%p,%s,%p,%d)\n",
1291                 block,subblock,buffer,*buflen
1292         );
1293
1294         s=(char*)xmalloc(strlen("VS_VERSION_INFO\\")+strlen(subblock)+1);
1295         strcpy(s,"VS_VERSION_INFO\\");strcat(s,subblock);
1296         /* check for UNICODE version */
1297         if (    (*(DWORD*)(block+0x14) != VS_FFI_SIGNATURE) && 
1298                 (*(DWORD*)(block+0x28) == VS_FFI_SIGNATURE)
1299         ) {
1300                 struct  dbW     *db;
1301                 LPWSTR  wstr;
1302                 LPSTR   xs;
1303
1304                 wstr = HEAP_strdupAtoW(GetProcessHeap(),0,s);
1305                 b=_find_dataW(block,wstr,*(WORD*)block);
1306                 HeapFree(GetProcessHeap(),0,wstr);
1307                 if (!b) {
1308                         WARN(ver,"key %s not found in versionresource.\n",s);
1309                         *buflen=0;
1310                         free (s);
1311                         return 0;
1312                 }
1313                 db=(struct dbW*)b;
1314                 b       = b+DATA_OFFSET_W(db);
1315                 *buflen = db->datalen;
1316                 if (db->btext) {
1317                     xs = HEAP_strdupWtoA(GetProcessHeap(),0,(WCHAR*)b);
1318                     TRACE(ver,"->%s\n",xs);
1319                     HeapFree(GetProcessHeap(),0,xs);
1320                 } else
1321                     TRACE(ver,"->%p\n",b);
1322         } else {
1323                 struct  dbA     *db;
1324                 b=_find_dataA(block,s,*(WORD*)block);
1325                 if (!b) {
1326                         WARN(ver,"key %s not found in versionresource.\n",s);
1327                         *buflen=0;
1328                         free (s);
1329                         return 0;
1330                 }
1331                 db=(struct dbA*)b;
1332                 b       = b+DATA_OFFSET_A(db);
1333                 *buflen = db->datalen;
1334                 /* the string is only printable, if it is below \\StringFileInfo*/
1335                 if (!lstrncmpi32A("VS_VERSION_INFO\\StringFileInfo\\",s,strlen("VS_VERSION_INFO\\StringFileInfo\\")))
1336                     TRACE(ver," -> %s=%s\n",subblock,b);
1337                 else
1338                     TRACE(ver," -> %s=%p\n",subblock,b);
1339         }
1340         *buffer = (b-block)+segblock;
1341         free(s);
1342         return 1;
1343 }
1344
1345 DWORD WINAPI VerQueryValue32A(LPVOID vblock,LPCSTR subblock,
1346                               LPVOID *vbuffer,UINT32 *buflen)
1347 {
1348         BYTE    *b,*block=(LPBYTE)vblock,**buffer=(LPBYTE*)vbuffer;
1349         LPSTR   s;
1350
1351         TRACE(ver,"(%p,%s,%p,%d)\n",
1352                 block,subblock,buffer,*buflen
1353         );
1354
1355         s=(char*)xmalloc(strlen("VS_VERSION_INFO\\")+strlen(subblock)+1);
1356         strcpy(s,"VS_VERSION_INFO\\");strcat(s,subblock);
1357
1358         /* check for UNICODE version */
1359         if (    (*(DWORD*)(block+0x14) != VS_FFI_SIGNATURE) && 
1360                 (*(DWORD*)(block+0x28) == VS_FFI_SIGNATURE)
1361         ) {
1362                 LPWSTR  wstr;
1363                 LPSTR   xs;
1364                 struct  dbW     *db;
1365
1366                 wstr = HEAP_strdupAtoW(GetProcessHeap(),0,s);
1367                 b=_find_dataW(block,wstr,*(WORD*)block);
1368                 HeapFree(GetProcessHeap(),0,wstr);
1369                 if (!b) {
1370                         WARN(ver,"key %s not found in versionresource.\n",s);
1371                         *buflen=0;
1372                         free (s);
1373                         return 0;
1374                 }
1375                 db      = (struct dbW*)b;
1376                 *buflen = db->datalen;
1377                 b       = b+DATA_OFFSET_W(db);
1378                 if (db->btext) {
1379                     xs = HEAP_strdupWtoA(GetProcessHeap(),0,(WCHAR*)b);
1380                     TRACE(ver,"->%s\n",xs);
1381                     HeapFree(GetProcessHeap(),0,xs);
1382                 } else
1383                     TRACE(ver,"->%p\n",b);
1384                 /* This is a leak.  */
1385                 b = HEAP_strdupWtoA(GetProcessHeap(),0,(WCHAR*)b);
1386         } else {
1387                 struct  dbA     *db;
1388                 b=_find_dataA(block,s,*(WORD*)block);
1389                 if (!b) {
1390                         WARN(ver,"key %s not found in versionresource.\n",subblock);
1391                         *buflen=0;
1392                         free (s);
1393                         return 0;
1394                 }
1395                 db=(struct dbA*)b;
1396                 *buflen = db->datalen;
1397                 b       = b+DATA_OFFSET_A(db);
1398
1399                 /* the string is only printable, if it is below \\StringFileInfo*/
1400                 if (!lstrncmpi32A("VS_VERSION_INFO\\StringFileInfo\\",s,strlen("VS_VERSION_INFO\\StringFileInfo\\")))
1401                     TRACE(ver," -> %s=%s\n",subblock,b);
1402                 else
1403                     TRACE(ver," -> %s=%p\n",subblock,b);
1404         }
1405         *buffer = b;
1406         free(s);
1407         return 1;
1408 }
1409
1410 DWORD WINAPI VerQueryValue32W(LPVOID vblock,LPCWSTR subblock,LPVOID *vbuffer,
1411                               UINT32 *buflen)
1412 {
1413         LPSTR           sb;
1414         DWORD           ret;
1415
1416         sb = HEAP_strdupWtoA( GetProcessHeap(), 0, subblock );
1417         ret = VerQueryValue32A(vblock,sb,vbuffer,buflen);
1418         HeapFree( GetProcessHeap(), 0, sb );
1419         return 1;
1420 }
1421 /* 20 GETFILEVERSIONINFORAW */