Release 970804
[wine/multimedia.git] / misc / ver.c
blob0a747bf75d602b56314390c10edbc2beb872b6b2
1 /*
2 * Implementation of VER.DLL
3 *
4 * Copyright 1996 Marcus Meissner
5 */
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <ctype.h>
10 #include <unistd.h>
11 #include "windows.h"
12 #include "win.h"
13 #include "winerror.h"
14 #include "heap.h"
15 #include "ver.h"
16 #include "lzexpand.h"
17 #include "module.h"
18 #include "neexe.h"
19 #include "stddebug.h"
20 #include "debug.h"
21 #include "xmalloc.h"
22 #include "winreg.h"
24 #define LZREAD(what) \
25 if (sizeof(*what)!=LZRead32(lzfd,what,sizeof(*what))) return 0;
26 #define LZTELL(lzfd) LZSeek32(lzfd, 0, SEEK_CUR);
28 /******************************************************************************
30 * void ver_dstring(
31 * char const * prologue,
32 * char const * teststring,
33 * char const * epilogue )
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.
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.
47 *****************************************************************************/
49 static void ver_dstring(
50 char const * prologue,
51 char const * teststring,
52 char const * epilogue )
54 dprintf_ver(stddeb, "%s", prologue);
56 if(teststring)
57 dprintf_ver(stddeb, "%p (\"%s\")", (void const *) teststring,
58 teststring);
59 else
60 dprintf_ver(stddeb, "(null)");
62 dprintf_ver(stddeb, "%s", epilogue);
64 return;
67 /******************************************************************************
69 * int testFileExistence(
70 * char const * path,
71 * char const * file )
73 * Tests whether a given path/file combination exists. If the file does
74 * not exist, the return value is zero. If it does exist, the return
75 * value is non-zero.
77 * Revision history
78 * 30-May-1997 Dave Cuthbert (dacut@ece.cmu.edu)
79 * Original implementation
81 *****************************************************************************/
83 static int testFileExistence(
84 char const * path,
85 char const * file )
87 char filename[1024];
88 int filenamelen;
89 OFSTRUCT fileinfo;
90 int retval;
92 fileinfo.cBytes = sizeof(OFSTRUCT);
94 strcpy(filename, path);
95 filenamelen = strlen(filename);
97 /* Add a trailing \ if necessary */
98 if(filenamelen) {
99 if(filename[filenamelen - 1] != '\\')
100 strcat(filename, "\\");
102 else /* specify the current directory */
103 strcpy(filename, ".\\");
105 /* Create the full pathname */
106 strcat(filename, file);
108 if(OpenFile32(filename, &fileinfo, OF_EXIST) == HFILE_ERROR32)
109 retval = 0;
110 else
111 retval = 1;
113 return retval;
116 /******************************************************************************
118 * int testFileExclusiveExistence(
119 * char const * path,
120 * char const * file )
122 * Tests whether a given path/file combination exists and ensures that no
123 * other programs have handles to the given file. If the file does not
124 * exist or is open, the return value is zero. If it does exist, the
125 * return value is non-zero.
127 * Revision history
128 * 30-May-1997 Dave Cuthbert (dacut@ece.cmu.edu)
129 * Original implementation
131 *****************************************************************************/
133 static int testFileExclusiveExistence(
134 char const * path,
135 char const * file )
137 char filename[1024];
138 int filenamelen;
139 OFSTRUCT fileinfo;
140 int retval;
142 fileinfo.cBytes = sizeof(OFSTRUCT);
144 strcpy(filename, path);
145 filenamelen = strlen(filename);
147 /* Add a trailing \ if necessary */
148 if(filenamelen) {
149 if(filename[filenamelen - 1] != '\\')
150 strcat(filename, "\\");
152 else /* specify the current directory */
153 strcpy(filename, ".\\");
155 /* Create the full pathname */
156 strcat(filename, file);
158 if(OpenFile32(filename, &fileinfo, OF_EXIST | OF_SHARE_EXCLUSIVE) ==
159 HFILE_ERROR32)
160 retval = 0;
161 else
162 retval = 1;
164 return retval;
169 read_ne_header(HFILE32 lzfd,LPIMAGE_OS2_HEADER nehd) {
170 IMAGE_DOS_HEADER mzh;
172 LZSeek32(lzfd,0,SEEK_SET);
173 if (sizeof(mzh)!=LZRead32(lzfd,&mzh,sizeof(mzh)))
174 return 0;
175 if (mzh.e_magic!=IMAGE_DOS_SIGNATURE)
176 return 0;
177 LZSeek32(lzfd,mzh.e_lfanew,SEEK_SET);
178 LZREAD(nehd);
179 if (nehd->ne_magic == IMAGE_OS2_SIGNATURE) {
180 LZSeek32(lzfd,mzh.e_lfanew,SEEK_SET);
181 return 1;
183 fprintf(stderr,"misc/ver.c:read_ne_header:can't handle PE files yet.\n");
184 /* must handle PE files too. Later. */
185 return 0;
190 find_ne_resource(
191 HFILE32 lzfd,LPIMAGE_OS2_HEADER nehd,SEGPTR typeid,SEGPTR resid,
192 BYTE **resdata,int *reslen,DWORD *off
194 NE_TYPEINFO ti;
195 NE_NAMEINFO ni;
196 int i;
197 WORD shiftcount;
198 DWORD nehdoffset;
200 nehdoffset = LZTELL(lzfd);
201 LZSeek32(lzfd,nehd->resource_tab_offset,SEEK_CUR);
202 LZREAD(&shiftcount);
203 dprintf_ver(stddeb,"shiftcount is %d\n",shiftcount);
204 dprintf_ver(stddeb,"reading resource typeinfo dir.\n");
206 if (!HIWORD(typeid)) typeid = (SEGPTR)(LOWORD(typeid) | 0x8000);
207 if (!HIWORD(resid)) resid = (SEGPTR)(LOWORD(resid) | 0x8000);
208 while (1) {
209 int skipflag;
211 LZREAD(&ti);
212 if (!ti.type_id)
213 return 0;
214 dprintf_ver(stddeb," ti.typeid =%04x,count=%d\n",ti.type_id,ti.count);
215 skipflag=0;
216 if (!HIWORD(typeid)) {
217 if ((ti.type_id&0x8000)&&(typeid!=ti.type_id))
218 skipflag=1;
219 } else {
220 if (ti.type_id & 0x8000) {
221 skipflag=1;
222 } else {
223 BYTE len;
224 char *str;
225 DWORD whereleft;
227 whereleft = LZTELL(lzfd);
228 LZSeek32(
229 lzfd,
230 nehdoffset+nehd->resource_tab_offset+ti.type_id,
231 SEEK_SET
233 LZREAD(&len);
234 str=xmalloc(len);
235 if (len!=LZRead32(lzfd,str,len))
236 return 0;
237 dprintf_ver(stddeb,"read %s to compare it with %s\n",
238 str,(char*)PTR_SEG_TO_LIN(typeid)
240 if (lstrcmpi32A(str,(char*)PTR_SEG_TO_LIN(typeid)))
241 skipflag=1;
242 free(str);
243 LZSeek32(lzfd,whereleft,SEEK_SET);
246 if (skipflag) {
247 LZSeek32(lzfd,ti.count*sizeof(ni),SEEK_CUR);
248 continue;
250 for (i=0;i<ti.count;i++) {
251 WORD *rdata;
252 int len;
254 LZREAD(&ni);
255 dprintf_ver(stddeb," ni.id=%4x,offset=%d,length=%d\n",
256 ni.id,ni.offset,ni.length
258 skipflag=1;
259 if (!HIWORD(resid)) {
260 if (ni.id == resid)
261 skipflag=0;
262 } else {
263 if (!(ni.id & 0x8000)) {
264 BYTE len;
265 char *str;
266 DWORD whereleft;
268 whereleft = LZTELL(lzfd);
269 LZSeek32(
270 lzfd,
271 nehdoffset+nehd->resource_tab_offset+ni.id,
272 SEEK_SET
274 LZREAD(&len);
275 str=xmalloc(len);
276 if (len!=LZRead32(lzfd,str,len))
277 return 0;
278 dprintf_ver(stddeb,"read %s to compare it with %s\n",
279 str,(char*)PTR_SEG_TO_LIN(typeid)
281 if (!lstrcmpi32A(str,(char*)PTR_SEG_TO_LIN(typeid)))
282 skipflag=0;
283 free(str);
284 LZSeek32(lzfd,whereleft,SEEK_SET);
287 if (skipflag)
288 continue;
289 LZSeek32(lzfd,((int)ni.offset<<shiftcount),SEEK_SET);
290 *off = (int)ni.offset<<shiftcount;
291 len = ni.length<<shiftcount;
292 rdata=(WORD*)xmalloc(len);
293 if (len!=LZRead32(lzfd,rdata,len)) {
294 free(rdata);
295 return 0;
297 dprintf_ver(stddeb,"resource found.\n");
298 *resdata= (BYTE*)rdata;
299 *reslen = len;
300 return 1;
305 /* GetFileResourceSize [VER.2] */
306 DWORD
307 GetFileResourceSize(LPCSTR filename,SEGPTR restype,SEGPTR resid,LPDWORD off) {
308 HFILE32 lzfd;
309 OFSTRUCT ofs;
310 BYTE *resdata;
311 int reslen;
312 IMAGE_OS2_HEADER nehd;
314 dprintf_ver(stddeb,"GetFileResourceSize(%s,%lx,%lx,%p)\n",
315 filename,(LONG)restype,(LONG)resid,off
317 lzfd=LZOpenFile32A(filename,&ofs,OF_READ);
318 if (lzfd==0)
319 return 0;
320 if (!read_ne_header(lzfd,&nehd)) {
321 LZClose32(lzfd);
322 return 0;
324 if (!find_ne_resource(lzfd,&nehd,restype,resid,&resdata,&reslen,off)) {
325 LZClose32(lzfd);
326 return 0;
328 free(resdata);
329 LZClose32(lzfd);
330 return reslen;
333 /* GetFileResource [VER.3] */
334 DWORD
335 GetFileResource(LPCSTR filename,SEGPTR restype,SEGPTR resid,
336 DWORD off,DWORD datalen,LPVOID data
338 HFILE32 lzfd;
339 OFSTRUCT ofs;
340 BYTE *resdata;
341 int reslen=datalen;
342 IMAGE_OS2_HEADER nehd;
343 dprintf_ver(stddeb,"GetFileResource(%s,%lx,%lx,%ld,%ld,%p)\n",
344 filename,(LONG)restype,(LONG)resid,off,datalen,data
347 lzfd=LZOpenFile32A(filename,&ofs,OF_READ);
348 if (lzfd==0)
349 return 0;
350 if (!off) {
351 if (!read_ne_header(lzfd,&nehd)) {
352 LZClose32(lzfd);
353 return 0;
355 if (!find_ne_resource(lzfd,&nehd,restype,resid,&resdata,&reslen,&off)) {
356 LZClose32(lzfd);
357 return 0;
359 free(resdata);
361 LZSeek32(lzfd,off,SEEK_SET);
362 if (reslen>datalen)
363 reslen=datalen;
364 LZRead32(lzfd,data,reslen);
365 LZClose32(lzfd);
366 return reslen;
369 /* GetFileVersionInfoSize [VER.6] */
370 DWORD
371 GetFileVersionInfoSize16(LPCSTR filename,LPDWORD handle) {
372 DWORD len,ret;
373 BYTE buf[72];
374 VS_FIXEDFILEINFO *vffi;
376 dprintf_ver(stddeb,"GetFileVersionInfoSize16(%s,%p)\n",filename,handle);
377 len=GetFileResourceSize(filename,VS_FILE_INFO,VS_VERSION_INFO,handle);
378 if (!len)
379 return 0;
380 ret=GetFileResource(
381 filename,VS_FILE_INFO,VS_VERSION_INFO,*handle,sizeof(buf),buf
383 if (!ret)
384 return 0;
386 vffi=(VS_FIXEDFILEINFO*)(buf+0x14);
387 if (vffi->dwSignature != VS_FFI_SIGNATURE)
388 return 0;
389 if (*(WORD*)buf < len)
390 len = *(WORD*)buf;
391 dprintf_ver(stddeb,"->strucver=%ld.%ld,filever=%ld.%ld,productver=%ld.%ld,flagmask=%lx,flags=%lx,OS=",
392 (vffi->dwStrucVersion>>16),vffi->dwStrucVersion&0xFFFF,
393 vffi->dwFileVersionMS,vffi->dwFileVersionLS,
394 vffi->dwProductVersionMS,vffi->dwProductVersionLS,
395 vffi->dwFileFlagsMask,vffi->dwFileFlags
397 switch (vffi->dwFileOS&0xFFFF0000) {
398 case VOS_DOS:dprintf_ver(stddeb,"DOS,");break;
399 case VOS_OS216:dprintf_ver(stddeb,"OS/2-16,");break;
400 case VOS_OS232:dprintf_ver(stddeb,"OS/2-32,");break;
401 case VOS_NT:dprintf_ver(stddeb,"NT,");break;
402 case VOS_UNKNOWN:
403 default:
404 dprintf_ver(stddeb,"UNKNOWN(%ld),",vffi->dwFileOS&0xFFFF0000);break;
406 switch (vffi->dwFileOS & 0xFFFF) {
407 case VOS__BASE:dprintf_ver(stddeb,"BASE");break;
408 case VOS__WINDOWS16:dprintf_ver(stddeb,"WIN16");break;
409 case VOS__WINDOWS32:dprintf_ver(stddeb,"WIN32");break;
410 case VOS__PM16:dprintf_ver(stddeb,"PM16");break;
411 case VOS__PM32:dprintf_ver(stddeb,"PM32");break;
412 default:dprintf_ver(stddeb,"UNKNOWN(%ld)",vffi->dwFileOS&0xFFFF);break;
414 switch (vffi->dwFileType) {
415 default:
416 case VFT_UNKNOWN:
417 dprintf_ver(stddeb,"filetype=Unknown(%ld)",vffi->dwFileType);
418 break;
419 case VFT_APP:dprintf_ver(stddeb,"filetype=APP");break;
420 case VFT_DLL:dprintf_ver(stddeb,"filetype=DLL");break;
421 case VFT_DRV:
422 dprintf_ver(stddeb,"filetype=DRV,");
423 switch(vffi->dwFileSubtype) {
424 default:
425 case VFT2_UNKNOWN:
426 dprintf_ver(stddeb,"UNKNOWN(%ld)",vffi->dwFileSubtype);
427 break;
428 case VFT2_DRV_PRINTER:
429 dprintf_ver(stddeb,"PRINTER");
430 break;
431 case VFT2_DRV_KEYBOARD:
432 dprintf_ver(stddeb,"KEYBOARD");
433 break;
434 case VFT2_DRV_LANGUAGE:
435 dprintf_ver(stddeb,"LANGUAGE");
436 break;
437 case VFT2_DRV_DISPLAY:
438 dprintf_ver(stddeb,"DISPLAY");
439 break;
440 case VFT2_DRV_MOUSE:
441 dprintf_ver(stddeb,"MOUSE");
442 break;
443 case VFT2_DRV_NETWORK:
444 dprintf_ver(stddeb,"NETWORK");
445 break;
446 case VFT2_DRV_SYSTEM:
447 dprintf_ver(stddeb,"SYSTEM");
448 break;
449 case VFT2_DRV_INSTALLABLE:
450 dprintf_ver(stddeb,"INSTALLABLE");
451 break;
452 case VFT2_DRV_SOUND:
453 dprintf_ver(stddeb,"SOUND");
454 break;
455 case VFT2_DRV_COMM:
456 dprintf_ver(stddeb,"COMM");
457 break;
458 case VFT2_DRV_INPUTMETHOD:
459 dprintf_ver(stddeb,"INPUTMETHOD");
460 break;
462 break;
463 case VFT_FONT:
464 dprintf_ver(stddeb,"filetype=FONT.");
465 switch (vffi->dwFileSubtype) {
466 default:
467 dprintf_ver(stddeb,"UNKNOWN(%ld)",vffi->dwFileSubtype);
468 break;
469 case VFT2_FONT_RASTER:dprintf_ver(stddeb,"RASTER");break;
470 case VFT2_FONT_VECTOR:dprintf_ver(stddeb,"VECTOR");break;
471 case VFT2_FONT_TRUETYPE:dprintf_ver(stddeb,"TRUETYPE");break;
473 break;
474 case VFT_VXD:dprintf_ver(stddeb,"filetype=VXD");break;
475 case VFT_STATIC_LIB:dprintf_ver(stddeb,"filetype=STATIC_LIB");break;
477 dprintf_ver(stddeb,"filedata=%lx.%lx\n",vffi->dwFileDateMS,vffi->dwFileDateLS);
478 return len;
481 /* GetFileVersionInfoSize32A [VERSION.1] */
482 DWORD
483 GetFileVersionInfoSize32A(LPCSTR filename,LPDWORD handle) {
484 dprintf_ver(stddeb,"GetFileVersionInfoSize32A(%s,%p)\n",filename,handle);
485 return GetFileVersionInfoSize16(filename,handle);
488 /* GetFileVersionInfoSize32W [VERSION.2] */
489 DWORD GetFileVersionInfoSize32W( LPCWSTR filename, LPDWORD handle )
491 LPSTR xfn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
492 DWORD ret = GetFileVersionInfoSize16( xfn, handle );
493 HeapFree( GetProcessHeap(), 0, xfn );
494 return ret;
497 /* GetFileVersionInfo [VER.7] */
498 DWORD
499 GetFileVersionInfo16(LPCSTR filename,DWORD handle,DWORD datasize,LPVOID data) {
500 dprintf_ver(stddeb,"GetFileVersionInfo16(%s,%ld,%ld,%p)\n->",
501 filename,handle,datasize,data
503 return GetFileResource(
504 filename,VS_FILE_INFO,VS_VERSION_INFO,handle,datasize,data
508 /* GetFileVersionInfoA [VERSION.0] */
509 DWORD
510 GetFileVersionInfo32A(LPCSTR filename,DWORD handle,DWORD datasize,LPVOID data) {
511 return GetFileVersionInfo16(filename,handle,datasize,data);
514 /* GetFileVersionInfoW [VERSION.3] */
515 DWORD GetFileVersionInfo32W( LPCWSTR filename, DWORD handle, DWORD datasize,
516 LPVOID data)
518 LPSTR fn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
519 DWORD ret = GetFileVersionInfo16( fn, handle, datasize, data );
520 HeapFree( GetProcessHeap(), 0, fn );
521 return ret;
524 /*****************************************************************************
526 * VerFindFile() [VER.8]
527 * Determines where to install a file based on whether it locates another
528 * version of the file in the system. The values VerFindFile returns are
529 * used in a subsequent call to the VerInstallFile function.
531 * Revision history:
532 * 30-May-1997 Dave Cuthbert (dacut@ece.cmu.edu)
533 * Reimplementation of VerFindFile from original stub.
535 ****************************************************************************/
537 DWORD VerFindFile16(
538 UINT16 flags,
539 LPCSTR lpszFilename,
540 LPCSTR lpszWinDir,
541 LPCSTR lpszAppDir,
542 LPSTR lpszCurDir,
543 UINT16 *lpuCurDirLen,
544 LPSTR lpszDestDir,
545 UINT16 *lpuDestDirLen )
547 DWORD retval;
548 char curDir[256];
549 char destDir[256];
550 unsigned int curDirSizeReq;
551 unsigned int destDirSizeReq;
553 retval = 0;
555 /* Print out debugging information */
556 dprintf_ver(stddeb, "VerFindFile() called with parameters:\n"
557 "\tflags = %x", flags);
558 if(flags & VFFF_ISSHAREDFILE)
559 dprintf_ver(stddeb, " (VFFF_ISSHAREDFILE)\n");
560 else
561 dprintf_ver(stddeb, "\n");
563 ver_dstring("\tlpszFilename = ", lpszFilename, "\n");
564 ver_dstring("\tlpszWinDir = ", lpszWinDir, "\n");
565 ver_dstring("\tlpszAppDir = ", lpszAppDir, "\n");
567 dprintf_ver(stddeb, "\tlpszCurDir = %p\n", lpszCurDir);
568 if(lpuCurDirLen)
569 dprintf_ver(stddeb, "\tlpuCurDirLen = %p (%u)\n",
570 lpuCurDirLen, *lpuCurDirLen);
571 else
572 dprintf_ver(stddeb, "\tlpuCurDirLen = (null)\n");
574 dprintf_ver(stddeb, "\tlpszDestDir = %p\n", lpszDestDir);
575 if(lpuDestDirLen)
576 dprintf_ver(stddeb, "\tlpuDestDirLen = %p (%u)\n",
577 lpuDestDirLen, *lpuDestDirLen);
579 /* Figure out where the file should go; shared files default to the
580 system directory */
582 strcpy(curDir, "");
583 strcpy(destDir, "");
585 if(flags & VFFF_ISSHAREDFILE && !getuid()) {
586 GetSystemDirectory32A(destDir, 256);
588 /* Were we given a filename? If so, try to find the file. */
589 if(lpszFilename) {
590 if(testFileExistence(destDir, lpszFilename)) {
591 strcpy(curDir, destDir);
593 if(!testFileExclusiveExistence(destDir, lpszFilename))
594 retval |= VFF_FILEINUSE;
596 else if(lpszAppDir && testFileExistence(lpszAppDir,
597 lpszFilename)) {
598 strcpy(curDir, lpszAppDir);
599 retval |= VFF_CURNEDEST;
601 if(!testFileExclusiveExistence(lpszAppDir, lpszFilename))
602 retval |= VFF_FILEINUSE;
606 else if(!(flags & VFFF_ISSHAREDFILE)) { /* not a shared file */
607 if(lpszAppDir) {
608 char systemDir[256];
609 GetSystemDirectory32A(systemDir, 256);
611 strcpy(destDir, lpszAppDir);
613 if(lpszFilename) {
614 if(testFileExistence(lpszAppDir, lpszFilename)) {
615 strcpy(curDir, lpszAppDir);
617 if(!testFileExclusiveExistence(lpszAppDir, lpszFilename))
618 retval |= VFF_FILEINUSE;
620 else if(testFileExistence(systemDir, lpszFilename)) {
621 strcpy(curDir, systemDir);
622 retval |= VFF_CURNEDEST;
624 if(!testFileExclusiveExistence(systemDir, lpszFilename))
625 retval |= VFF_FILEINUSE;
631 curDirSizeReq = strlen(curDir) + 1;
632 destDirSizeReq = strlen(destDir) + 1;
636 /* Make sure that the pointers to the size of the buffers are
637 valid; if not, do NOTHING with that buffer. If that pointer
638 is valid, then make sure that the buffer pointer is valid, too! */
640 if(lpuDestDirLen && lpszDestDir) {
641 if(*lpuDestDirLen < destDirSizeReq) {
642 retval |= VFF_BUFFTOOSMALL;
643 strncpy(lpszDestDir, destDir, *lpuDestDirLen - 1);
644 lpszDestDir[*lpuDestDirLen - 1] = '\0';
646 else
647 strcpy(lpszDestDir, destDir);
649 *lpuDestDirLen = destDirSizeReq;
652 if(lpuCurDirLen && lpszCurDir) {
653 if(*lpuCurDirLen < curDirSizeReq) {
654 retval |= VFF_BUFFTOOSMALL;
655 strncpy(lpszCurDir, curDir, *lpuCurDirLen - 1);
656 lpszCurDir[*lpuCurDirLen - 1] = '\0';
658 else
659 strcpy(lpszCurDir, curDir);
661 *lpuCurDirLen = curDirSizeReq;
664 dprintf_ver(stddeb, "VerFindFile() ret = %lu ",
665 retval);
667 if(retval) {
668 dprintf_ver(stddeb, "( ");
670 if(retval & VFF_CURNEDEST)
671 dprintf_ver(stddeb, "VFF_CURNEDEST ");
672 if(retval & VFF_FILEINUSE)
673 dprintf_ver(stddeb, "VFF_FILEINUSE ");
674 if(retval & VFF_BUFFTOOSMALL)
675 dprintf_ver(stddeb, "VFF_BUFFTOOSMALL ");
677 dprintf_ver(stddeb, ")");
680 ver_dstring("\n\t(Exit) lpszCurDir = ", lpszCurDir, "\n");
681 if(lpuCurDirLen)
682 dprintf_ver(stddeb, "\t(Exit) lpuCurDirLen = %p (%u)\n",
683 lpuCurDirLen, *lpuCurDirLen);
684 else
685 dprintf_ver(stddeb, "\t(Exit) lpuCurDirLen = (null)\n");
687 ver_dstring("\t(Exit) lpszDestDir = ", lpszDestDir, "\n");
688 if(lpuDestDirLen)
689 dprintf_ver(stddeb, "\t(Exit) lpuDestDirLen = %p (%u)\n",
690 lpuDestDirLen, *lpuDestDirLen);
692 return retval;
695 /* VerFindFileA [VERSION.5] */
696 DWORD
697 VerFindFile32A(
698 UINT32 flags,LPCSTR filename,LPCSTR windir,LPCSTR appdir,
699 LPSTR curdir,UINT32 *pcurdirlen,LPSTR destdir,UINT32 *pdestdirlen )
701 UINT16 curdirlen, destdirlen;
702 DWORD ret = VerFindFile16(flags,filename,windir,appdir,
703 curdir,&curdirlen,destdir,&destdirlen);
704 *pcurdirlen = curdirlen;
705 *pdestdirlen = destdirlen;
706 return ret;
709 /* VerFindFileW [VERSION.6] */
710 DWORD
711 VerFindFile32W(
712 UINT32 flags,LPCWSTR filename,LPCWSTR windir,LPCWSTR appdir,
713 LPWSTR curdir,UINT32 *pcurdirlen,LPWSTR destdir,UINT32 *pdestdirlen )
715 UINT16 curdirlen, destdirlen;
716 LPSTR wfn,wwd,wad,wdd,wcd;
717 DWORD ret;
719 wfn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
720 wwd = HEAP_strdupWtoA( GetProcessHeap(), 0, windir );
721 wad = HEAP_strdupWtoA( GetProcessHeap(), 0, appdir );
722 wcd = HeapAlloc( GetProcessHeap(), 0, *pcurdirlen );
723 wdd = HeapAlloc( GetProcessHeap(), 0, *pdestdirlen );
724 ret = VerFindFile16(flags,wfn,wwd,wad,wcd,&curdirlen,wdd,&destdirlen);
725 lstrcpynAtoW(curdir,wcd,*pcurdirlen);
726 lstrcpynAtoW(destdir,wdd,*pdestdirlen);
727 *pcurdirlen = strlen(wcd);
728 *pdestdirlen = strlen(wdd);
729 HeapFree( GetProcessHeap(), 0, wfn );
730 HeapFree( GetProcessHeap(), 0, wwd );
731 HeapFree( GetProcessHeap(), 0, wad );
732 HeapFree( GetProcessHeap(), 0, wcd );
733 HeapFree( GetProcessHeap(), 0, wdd );
734 return ret;
737 /* VerInstallFile [VER.9] */
738 DWORD
739 VerInstallFile16(
740 UINT16 flags,LPCSTR srcfilename,LPCSTR destfilename,LPCSTR srcdir,
741 LPCSTR destdir,LPCSTR curdir,LPSTR tmpfile,UINT16 *tmpfilelen )
743 UINT32 filelen;
744 DWORD ret= VerInstallFile32A(flags,srcfilename,destfilename,srcdir,
745 destdir,curdir,tmpfile,&filelen);
747 *tmpfilelen = filelen;
748 return ret;
751 /* VerInstallFileA [VERSION.7] */
752 static LPBYTE
753 _fetch_versioninfo(LPSTR fn) {
754 DWORD alloclen;
755 LPBYTE buf;
756 DWORD ret;
758 alloclen = 1000;
759 buf= xmalloc(alloclen);
760 while (1) {
761 ret = GetFileVersionInfo32A(fn,0,alloclen,buf);
762 if (!ret) {
763 free(buf);
764 return 0;
766 if (alloclen<*(WORD*)buf) {
767 free(buf);
768 alloclen = *(WORD*)buf;
769 buf = xmalloc(alloclen);
770 } else
771 return buf;
775 static DWORD
776 _error2vif(DWORD error) {
777 switch (error) {
778 case ERROR_ACCESS_DENIED:
779 return VIF_ACCESSVIOLATION;
780 case ERROR_SHARING_VIOLATION:
781 return VIF_SHARINGVIOLATION;
782 default:
783 return 0;
787 DWORD
788 VerInstallFile32A(
789 UINT32 flags,LPCSTR srcfilename,LPCSTR destfilename,LPCSTR srcdir,
790 LPCSTR destdir,LPCSTR curdir,LPSTR tmpfile,UINT32 *tmpfilelen )
792 char destfn[260],tmpfn[260],srcfn[260];
793 HFILE32 hfsrc,hfdst;
794 DWORD attr,ret,xret,tmplast;
795 LPBYTE buf1,buf2;
796 OFSTRUCT ofs;
798 fprintf(stddeb,"VerInstallFile(%x,%s,%s,%s,%s,%s,%p,%d)\n",
799 flags,srcfilename,destfilename,srcdir,destdir,curdir,tmpfile,*tmpfilelen
801 xret = 0;
802 sprintf(srcfn,"%s\\%s",srcdir,srcfilename);
803 sprintf(destfn,"%s\\%s",destdir,destfilename);
804 hfsrc=LZOpenFile32A(srcfn,&ofs,OF_READ);
805 if (hfsrc==HFILE_ERROR32)
806 return VIF_CANNOTREADSRC;
807 sprintf(tmpfn,"%s\\%s",destdir,destfilename);
808 tmplast=strlen(destdir)+1;
809 attr = GetFileAttributes32A(tmpfn);
810 if (attr!=-1) {
811 if (attr & FILE_ATTRIBUTE_READONLY) {
812 LZClose32(hfsrc);
813 return VIF_WRITEPROT;
815 /* FIXME: check if file currently in use and return VIF_FILEINUSE */
817 attr = -1;
818 if (flags & VIFF_FORCEINSTALL) {
819 if (tmpfile[0]) {
820 sprintf(tmpfn,"%s\\%s",destdir,tmpfile);
821 tmplast = strlen(destdir)+1;
822 attr = GetFileAttributes32A(tmpfn);
823 /* if it exists, it has been copied by the call before.
824 * we jump over the copy part...
828 if (attr == -1) {
829 char *s;
831 GetTempFileName32A(destdir,"ver",0,tmpfn); /* should not fail ... */
832 s=strrchr(tmpfn,'\\');
833 if (s)
834 tmplast = s-tmpfn;
835 else
836 tmplast = 0;
837 hfdst = OpenFile32(tmpfn,&ofs,OF_CREATE);
838 if (hfdst == HFILE_ERROR32) {
839 LZClose32(hfsrc);
840 return VIF_CANNOTCREATE; /* | translated dos error */
842 ret = LZCopy32(hfsrc,hfdst);
843 _lclose32(hfdst);
844 if (((long) ret) < 0) {
845 /* translate LZ errors into VIF_xxx */
846 switch (ret) {
847 case LZERROR_BADINHANDLE:
848 case LZERROR_READ:
849 case LZERROR_BADVALUE:
850 case LZERROR_UNKNOWNALG:
851 ret = VIF_CANNOTREADSRC;
852 break;
853 case LZERROR_BADOUTHANDLE:
854 case LZERROR_WRITE:
855 ret = VIF_OUTOFMEMORY; /* FIXME: correct? */
856 break;
857 case LZERROR_GLOBALLOC:
858 case LZERROR_GLOBLOCK:
859 ret = VIF_OUTOFSPACE;
860 break;
861 default: /* unknown error, should not happen */
862 ret = 0;
863 break;
865 if (ret) {
866 LZClose32(hfsrc);
867 return ret;
871 xret = 0;
872 if (!(flags & VIFF_FORCEINSTALL)) {
873 buf1 = _fetch_versioninfo(destfn);
874 if (buf1) {
875 buf2 = _fetch_versioninfo(tmpfn);
876 if (buf2) {
877 char *tbuf1,*tbuf2;
878 VS_FIXEDFILEINFO *destvffi,*tmpvffi;
879 UINT32 len1,len2;
881 destvffi= (VS_FIXEDFILEINFO*)(buf1+0x14);
882 tmpvffi = (VS_FIXEDFILEINFO*)(buf2+0x14);
883 len1=len2=40;
885 /* compare file versions */
886 if ((destvffi->dwFileVersionMS > tmpvffi->dwFileVersionMS)||
887 ((destvffi->dwFileVersionMS==tmpvffi->dwFileVersionMS)&&
888 (destvffi->dwFileVersionLS > tmpvffi->dwFileVersionLS)
891 xret |= VIF_MISMATCH|VIF_SRCOLD;
892 /* compare filetypes and filesubtypes */
893 if ((destvffi->dwFileType!=tmpvffi->dwFileType) ||
894 (destvffi->dwFileSubtype!=tmpvffi->dwFileSubtype)
896 xret |= VIF_MISMATCH|VIF_DIFFTYPE;
897 if (VerQueryValue32A(buf1,"\\VarFileInfo\\Translation",(LPVOID*)&tbuf1,&len1) &&
898 VerQueryValue32A(buf2,"\\VarFileInfo\\Translation",(LPVOID*)&tbuf2,&len2)
900 /* irgendwas mit tbuf1 und tbuf2 machen
901 * generiert DIFFLANG|MISMATCH
904 free(buf2);
905 } else
906 xret=VIF_MISMATCH|VIF_SRCOLD;
907 free(buf1);
910 if (xret) {
911 if (*tmpfilelen<strlen(tmpfn+tmplast)) {
912 xret|=VIF_BUFTOSMALL;
913 DeleteFile32A(tmpfn);
914 } else {
915 strcpy(tmpfile,tmpfn+tmplast);
916 *tmpfilelen = strlen(tmpfn+tmplast)+1;
917 xret|=VIF_TEMPFILE;
919 } else {
920 if (-1!=GetFileAttributes32A(destfn))
921 if (!DeleteFile32A(destfn)) {
922 xret|=_error2vif(GetLastError())|VIF_CANNOTDELETE;
923 DeleteFile32A(tmpfn);
924 LZClose32(hfsrc);
925 return xret;
927 if ((!(flags & VIFF_DONTDELETEOLD)) &&
928 curdir &&
929 *curdir &&
930 lstrcmpi32A(curdir,destdir)
932 char curfn[260];
934 sprintf(curfn,"%s\\%s",curdir,destfilename);
935 if (-1!=GetFileAttributes32A(curfn)) {
936 /* FIXME: check if in use ... if it is, VIF_CANNOTDELETECUR */
937 if (!DeleteFile32A(curfn))
938 xret|=_error2vif(GetLastError())|VIF_CANNOTDELETECUR;
941 if (!MoveFile32A(tmpfn,destfn)) {
942 xret|=_error2vif(GetLastError())|VIF_CANNOTRENAME;
943 DeleteFile32A(tmpfn);
946 LZClose32(hfsrc);
947 return xret;
950 /* VerInstallFileW [VERSION.8] */
951 DWORD
952 VerInstallFile32W(
953 UINT32 flags,LPCWSTR srcfilename,LPCWSTR destfilename,LPCWSTR srcdir,
954 LPCWSTR destdir,LPCWSTR curdir,LPWSTR tmpfile,UINT32 *tmpfilelen )
956 LPSTR wsrcf,wsrcd,wdestf,wdestd,wtmpf,wcurd;
957 DWORD ret;
959 wsrcf = HEAP_strdupWtoA( GetProcessHeap(), 0, srcfilename );
960 wsrcd = HEAP_strdupWtoA( GetProcessHeap(), 0, srcdir );
961 wdestf = HEAP_strdupWtoA( GetProcessHeap(), 0, destfilename );
962 wdestd = HEAP_strdupWtoA( GetProcessHeap(), 0, destdir );
963 wtmpf = HEAP_strdupWtoA( GetProcessHeap(), 0, tmpfile );
964 wcurd = HEAP_strdupWtoA( GetProcessHeap(), 0, curdir );
965 ret = VerInstallFile32A(flags,wsrcf,wdestf,wsrcd,wdestd,wcurd,wtmpf,tmpfilelen);
966 if (!ret)
967 lstrcpynAtoW(tmpfile,wtmpf,*tmpfilelen);
968 HeapFree( GetProcessHeap(), 0, wsrcf );
969 HeapFree( GetProcessHeap(), 0, wsrcd );
970 HeapFree( GetProcessHeap(), 0, wdestf );
971 HeapFree( GetProcessHeap(), 0, wdestd );
972 HeapFree( GetProcessHeap(), 0, wtmpf );
973 if (wcurd)
974 HeapFree( GetProcessHeap(), 0, wcurd );
975 return ret;
979 /* FIXME: UNICODE? */
980 struct db {
981 WORD nextoff;
982 WORD datalen;
983 /* in memory structure... */
984 char name[1]; /* padded to dword alignment */
985 /* ....
986 char data[datalen]; padded to dword alignment
987 BYTE subdirdata[]; until nextoff
991 static BYTE*
992 _find_data(BYTE *block,LPCSTR str, WORD buff_remain) {
993 char *nextslash;
994 int substrlen, inc_size;
995 struct db *db;
997 while (*str && *str=='\\')
998 str++;
999 if (NULL!=(nextslash=strchr(str,'\\')))
1000 substrlen=nextslash-str;
1001 else
1002 substrlen=strlen(str);
1003 if (nextslash!=NULL) {
1004 while (*nextslash && *nextslash=='\\')
1005 nextslash++;
1006 if (!*nextslash)
1007 nextslash=NULL;
1011 while (1) {
1012 db=(struct db*)block;
1013 dprintf_ver(stddeb,"db=%p,db->nextoff=%d,db->datalen=%d,db->name=%s,db->data=%s\n",
1014 db,db->nextoff,db->datalen,db->name,(char*)((char*)db+4+((strlen(db->name)+4)&~3))
1016 if ((!db->nextoff) || (!buff_remain)) /* no more entries ? */
1017 return NULL;
1019 dprintf_ver(stddeb,"comparing with %s\n",db->name);
1020 if (!strncmp(db->name,str,substrlen)) {
1021 if (nextslash) {
1022 inc_size = 4+((strlen(db->name)+4)&~3)+((db->datalen+3)&~3);
1024 return _find_data( block+inc_size ,nextslash,
1025 buff_remain - inc_size);
1027 else
1028 return block;
1030 inc_size=((db->nextoff+3)&~3);
1031 block=block+inc_size;
1032 buff_remain=buff_remain-inc_size;
1036 /* VerQueryValue [VER.11] */
1037 /* take care, 'buffer' is NOT a SEGPTR, it just points to one */
1038 DWORD
1039 VerQueryValue16(SEGPTR segblock,LPCSTR subblock,SEGPTR *buffer,UINT16 *buflen)
1041 BYTE *block=PTR_SEG_TO_LIN(segblock),*b;
1042 struct db *db;
1043 char *s;
1045 dprintf_ver(stddeb,"VerQueryValue16(%p,%s,%p,%d)\n",
1046 block,subblock,buffer,*buflen
1048 s=(char*)xmalloc(strlen("VS_VERSION_INFO\\")+strlen(subblock)+1);
1049 strcpy(s,"VS_VERSION_INFO\\");strcat(s,subblock);
1050 b=_find_data(block, s, *(WORD *)block);
1051 if (b==NULL) {
1052 *buflen=0;
1053 return 0;
1055 db=(struct db*)b;
1056 *buflen = db->datalen;
1057 /* let b point to data area */
1058 b = b+4+((strlen(db->name)+4)&~3);
1059 /* now look up what the resp. SEGPTR would be ... */
1060 *buffer = (b-block)+segblock;
1061 dprintf_ver(stddeb," -> %s=%s\n",subblock,b);
1062 return 1;
1065 DWORD
1066 VerQueryValue32A(LPVOID vblock,LPCSTR subblock,LPVOID *vbuffer,UINT32 *buflen)
1068 BYTE *b,*block=(LPBYTE)vblock,**buffer=(LPBYTE*)vbuffer;
1069 struct db *db;
1070 char *s;
1072 dprintf_ver(stddeb,"VerQueryValue32A(%p,%s,%p,%d)\n",
1073 block,subblock,buffer,*buflen
1075 s=(char*)xmalloc(strlen("VS_VERSION_INFO\\")+strlen(subblock)+1);
1076 strcpy(s,"VS_VERSION_INFO\\");strcat(s,subblock);
1077 b=_find_data(block, s, *(WORD *)block);
1078 if (b==NULL) {
1079 *buflen=0;
1080 return 0;
1082 db=(struct db*)b;
1083 *buflen = db->datalen;
1084 /* let b point to data area */
1085 b = b+4+((strlen(db->name)+4)&~3);
1086 *buffer = b;
1087 dprintf_ver(stddeb," -> %s=%s\n",subblock,b);
1088 return 1;
1091 DWORD
1092 VerQueryValue32W(LPVOID vblock,LPCWSTR subblock,LPVOID *vbuffer,UINT32 *buflen)
1094 /* FIXME: hmm, we not only need to convert subblock, but also
1095 * the content...or?
1096 * And what about UNICODE version info?
1097 * And the NAMES of the values?
1099 BYTE *b,**buffer=(LPBYTE*)vbuffer,*block=(LPBYTE)vblock;
1100 struct db *db;
1101 char *s,*sb;
1103 sb = HEAP_strdupWtoA( GetProcessHeap(), 0, subblock );
1104 s=(char*)xmalloc(strlen("VS_VERSION_INFO\\")+strlen(sb)+1);
1105 strcpy(s,"VS_VERSION_INFO\\");strcat(s,sb);
1106 b=_find_data(block, s, *(WORD *)block);
1107 if (b==NULL) {
1108 *buflen=0;
1109 HeapFree( GetProcessHeap(), 0, sb );
1110 return 0;
1112 db=(struct db*)b;
1113 *buflen = db->datalen;
1114 /* let b point to data area */
1115 b = b+4+((strlen(db->name)+4)&~3);
1116 *buffer = b;
1117 dprintf_ver(stddeb," -> %s=%s\n",sb,b);
1118 HeapFree( GetProcessHeap(), 0, sb );
1119 return 1;
1121 /* 20 GETFILEVERSIONINFORAW */