2 * Implementation of VER.DLL
4 * Copyright 1996,1997 Marcus Meissner
5 * Copyright 1997 David Cuthbert
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 /******************************************************************************
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 followed by a new line.
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
50 *****************************************************************************/
52 static void ver_dstring(
53 char const * prologue,
54 char const * teststring,
55 char const * epilogue )
57 TRACE(ver, "%s %p (\"%s\") %s\n", prologue,
58 (void const *) teststring,
59 teststring ? teststring : "(null)",
63 /******************************************************************************
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.
70 *****************************************************************************/
71 static void print_vffi_debug(VS_FIXEDFILEINFO *vffi)
73 dbg_decl_str(ver, 1024);
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," : ""
88 dsprintf(ver," OS=0x%lx.0x%lx ",
89 (vffi->dwFileOS&0xFFFF0000)>>16,
90 vffi->dwFileOS&0x0000FFFF
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;
99 dsprintf(ver,"UNKNOWN(0x%lx),",vffi->dwFileOS&0xFFFF0000);break;
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;
109 TRACE(ver, "(%s)\n", dbg_str(ver));
112 switch (vffi->dwFileType) {
115 dsprintf(ver,"filetype=Unknown(0x%lx)",vffi->dwFileType);
117 case VFT_APP:dsprintf(ver,"filetype=APP,");break;
118 case VFT_DLL:dsprintf(ver,"filetype=DLL,");break;
120 dsprintf(ver,"filetype=DRV,");
121 switch(vffi->dwFileSubtype) {
124 dsprintf(ver,"UNKNOWN(0x%lx)",vffi->dwFileSubtype);
126 case VFT2_DRV_PRINTER:
127 dsprintf(ver,"PRINTER");
129 case VFT2_DRV_KEYBOARD:
130 dsprintf(ver,"KEYBOARD");
132 case VFT2_DRV_LANGUAGE:
133 dsprintf(ver,"LANGUAGE");
135 case VFT2_DRV_DISPLAY:
136 dsprintf(ver,"DISPLAY");
139 dsprintf(ver,"MOUSE");
141 case VFT2_DRV_NETWORK:
142 dsprintf(ver,"NETWORK");
144 case VFT2_DRV_SYSTEM:
145 dsprintf(ver,"SYSTEM");
147 case VFT2_DRV_INSTALLABLE:
148 dsprintf(ver,"INSTALLABLE");
151 dsprintf(ver,"SOUND");
154 dsprintf(ver,"COMM");
156 case VFT2_DRV_INPUTMETHOD:
157 dsprintf(ver,"INPUTMETHOD");
162 dsprintf(ver,"filetype=FONT.");
163 switch (vffi->dwFileSubtype) {
165 dsprintf(ver,"UNKNOWN(0x%lx)",vffi->dwFileSubtype);
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;
172 case VFT_VXD:dsprintf(ver,"filetype=VXD");break;
173 case VFT_STATIC_LIB:dsprintf(ver,"filetype=STATIC_LIB");break;
175 TRACE(ver, "%s\n", dbg_str(ver));
177 TRACE(ver, " filedata=0x%lx.0x%lx\n",
178 vffi->dwFileDateMS,vffi->dwFileDateLS);
181 /******************************************************************************
183 * int testFileExistence(
185 * char const * file )
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
192 * 30-May-1997 Dave Cuthbert (dacut@ece.cmu.edu)
193 * Original implementation
195 *****************************************************************************/
197 static int testFileExistence(
206 fileinfo.cBytes = sizeof(OFSTRUCT);
208 strcpy(filename, path);
209 filenamelen = strlen(filename);
211 /* Add a trailing \ if necessary */
213 if(filename[filenamelen - 1] != '\\')
214 strcat(filename, "\\");
216 else /* specify the current directory */
217 strcpy(filename, ".\\");
219 /* Create the full pathname */
220 strcat(filename, file);
222 if(OpenFile32(filename, &fileinfo, OF_EXIST) == HFILE_ERROR32)
230 /******************************************************************************
232 * int testFileExclusiveExistence(
234 * char const * file )
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.
242 * 30-May-1997 Dave Cuthbert (dacut@ece.cmu.edu)
243 * Original implementation
245 *****************************************************************************/
247 static int testFileExclusiveExistence(
256 fileinfo.cBytes = sizeof(OFSTRUCT);
258 strcpy(filename, path);
259 filenamelen = strlen(filename);
261 /* Add a trailing \ if necessary */
263 if(filename[filenamelen - 1] != '\\')
264 strcat(filename, "\\");
266 else /* specify the current directory */
267 strcpy(filename, ".\\");
269 /* Create the full pathname */
270 strcat(filename, file);
272 if(OpenFile32(filename, &fileinfo, OF_EXIST | OF_SHARE_EXCLUSIVE) ==
282 static int read_xx_header(HFILE32 lzfd) {
283 IMAGE_DOS_HEADER mzh;
286 LZSeek32(lzfd,0,SEEK_SET);
287 if (sizeof(mzh)!=LZRead32(lzfd,&mzh,sizeof(mzh)))
289 if (mzh.e_magic!=IMAGE_DOS_SIGNATURE)
291 LZSeek32(lzfd,mzh.e_lfanew,SEEK_SET);
292 if (2!=LZRead32(lzfd,magic,2))
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;
300 WARN(ver,"Can't handle %s files.\n",magic);
305 static int find_ne_resource(
306 HFILE32 lzfd,SEGPTR typeid,SEGPTR resid,
307 BYTE **resdata,int *reslen,DWORD *off
309 IMAGE_OS2_HEADER nehd;
316 nehdoffset = LZTELL(lzfd);
318 if (nehd.resource_tab_offset==nehd.rname_tab_offset) {
319 TRACE(ver,"no resources in NE dll\n");
322 LZSeek32(lzfd,nehd.resource_tab_offset+nehdoffset,SEEK_SET);
324 TRACE(ver,"shiftcount is %d\n",shiftcount);
325 TRACE(ver,"reading resource typeinfo dir.\n");
327 if (!HIWORD(typeid)) typeid = (SEGPTR)(LOWORD(typeid) | 0x8000);
328 if (!HIWORD(resid)) resid = (SEGPTR)(LOWORD(resid) | 0x8000);
335 TRACE(ver," ti.typeid =%04x,count=%d\n",ti.type_id,ti.count);
338 if (!HIWORD(typeid)) {
339 if ((ti.type_id&0x8000)&&(typeid!=ti.type_id))
342 if (ti.type_id & 0x8000) {
349 whereleft = LZTELL(lzfd);
352 nehdoffset+nehd.resource_tab_offset+ti.type_id,
357 if (len!=LZRead32(lzfd,str,len))
359 TRACE(ver,"read %s to compare it with %s\n",
360 str,(char*)PTR_SEG_TO_LIN(typeid)
362 if (lstrcmpi32A(str,(char*)PTR_SEG_TO_LIN(typeid)))
365 LZSeek32(lzfd,whereleft,SEEK_SET);
369 LZSeek32(lzfd,ti.count*sizeof(ni),SEEK_CUR);
372 for (i=0;i<ti.count;i++) {
377 TRACE(ver," ni.id=%4x,offset=%d,length=%d\n",
378 ni.id,ni.offset,ni.length
381 if (!HIWORD(resid)) {
385 if (!(ni.id & 0x8000)) {
390 whereleft = LZTELL(lzfd);
393 nehdoffset+nehd.resource_tab_offset+ni.id,
398 if (len!=LZRead32(lzfd,str,len))
400 TRACE(ver,"read %s to compare it with %s\n",
401 str,(char*)PTR_SEG_TO_LIN(typeid)
403 if (!lstrcmpi32A(str,(char*)PTR_SEG_TO_LIN(typeid)))
406 LZSeek32(lzfd,whereleft,SEEK_SET);
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)) {
419 TRACE(ver,"resource found.\n");
420 *resdata= (BYTE*)rdata;
427 /* Loads the specified PE resource.
428 * FIXME: shouldn't load the whole image
432 HFILE32 lzfd,LPWSTR typeid,LPWSTR resid,
433 BYTE **resdata,int *reslen,DWORD *off
435 IMAGE_NT_HEADERS pehd;
438 DWORD imagesize,pehdoffset;
440 IMAGE_DATA_DIRECTORY resdir;
441 LPIMAGE_RESOURCE_DIRECTORY resourcedir,xresdir;
442 LPIMAGE_RESOURCE_DATA_ENTRY xresdata;
443 LPIMAGE_SECTION_HEADER sections;
445 pehdoffset = LZTELL(lzfd);
447 resdir = pehd.OptionalHeader.DataDirectory[IMAGE_FILE_RESOURCE_DIRECTORY];
448 TRACE(ver,"(.,%p,%p,....)\n",typeid,resid);
450 WARN(ver,"No resource directory found in PE file.\n");
453 imagesize = pehd.OptionalHeader.SizeOfImage;
454 image = HeapAlloc(GetProcessHeap(),0,imagesize);
455 nrofsections = pehd.FileHeader.NumberOfSections;
457 sections = (LPIMAGE_SECTION_HEADER)HeapAlloc(GetProcessHeap(),0,pehd.FileHeader.NumberOfSections*sizeof(IMAGE_SECTION_HEADER));
460 sizeof(DWORD)+ /* Signature */
461 sizeof(IMAGE_FILE_HEADER)+
462 pehd.FileHeader.SizeOfOptionalHeader,
465 if ( nrofsections*sizeof(IMAGE_SECTION_HEADER)!=
466 LZRead32(lzfd,sections,nrofsections*sizeof(IMAGE_SECTION_HEADER))
468 HeapFree(GetProcessHeap(),0,image);
471 for (i=0;i<nrofsections;i++) {
472 if (sections[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
474 LZSeek32(lzfd,sections[i].PointerToRawData,SEEK_SET);
475 if ( sections[i].SizeOfRawData!=
476 LZRead32(lzfd,image+sections[i].VirtualAddress,sections[i].SizeOfRawData)
478 HeapFree(GetProcessHeap(),0,image);
482 resourcedir = (LPIMAGE_RESOURCE_DIRECTORY)(image+resdir.VirtualAddress);
483 xresdir = GetResDirEntryW(resourcedir,typeid,(DWORD)resourcedir,FALSE);
485 TRACE(ver,"...no typeid entry found for %p\n",typeid);
486 HeapFree(GetProcessHeap(),0,image);
489 xresdir = GetResDirEntryW(xresdir,resid,(DWORD)resourcedir,FALSE);
491 TRACE(ver,"...no resid entry found for %p\n",resid);
492 HeapFree(GetProcessHeap(),0,image);
496 xresdir = GetResDirEntryW(xresdir,0,(DWORD)resourcedir,TRUE);
498 TRACE(ver,"...no 0 (default language) entry found for %p\n",resid);
499 HeapFree(GetProcessHeap(),0,image);
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)
510 if ( (xresdata->OffsetToData >= sections[i].VirtualAddress)&&
511 (xresdata->OffsetToData < sections[i].VirtualAddress+sections[i].SizeOfRawData)
513 *off = (DWORD)(xresdata->OffsetToData)-(DWORD)(sections[i].VirtualAddress)+(DWORD)(sections[i].PointerToRawData);
517 HeapFree(GetProcessHeap(),0,image);
518 HeapFree(GetProcessHeap(),0,sections);
522 /* GetFileResourceSize [VER.2] */
523 DWORD WINAPI GetFileResourceSize(LPCSTR filename,SEGPTR restype,SEGPTR resid,
528 BYTE *resdata = NULL;
532 TRACE(ver,"(%s,%lx,%lx,%p)\n",
533 filename,(LONG)restype,(LONG)resid,off
535 lzfd=LZOpenFile32A(filename,&ofs,OF_READ);
538 switch (read_xx_header(lzfd)) {
542 case IMAGE_OS2_SIGNATURE:
543 res=find_ne_resource(lzfd,restype,resid,&resdata,&reslen,off);
545 case IMAGE_NT_SIGNATURE:
546 res=find_pe_resource(lzfd,(LPWSTR)restype,(LPWSTR)resid,&resdata,&reslen,off);
559 /* GetFileResource [VER.3] */
560 DWORD WINAPI GetFileResource(LPCSTR filename,SEGPTR restype,SEGPTR resid,
561 DWORD off,DWORD datalen,LPVOID data )
569 TRACE(ver,"(%s,%lx,%lx,%ld,%ld,%p)\n",
570 filename,(LONG)restype,(LONG)resid,off,datalen,data
573 lzfd=LZOpenFile32A(filename,&ofs,OF_READ);
577 switch (read_xx_header(lzfd)) {
580 case IMAGE_OS2_SIGNATURE:
581 res= find_ne_resource(lzfd,restype,resid,&resdata,&reslen,&off);
583 case IMAGE_NT_SIGNATURE:
584 res= find_pe_resource(lzfd,(LPWSTR)restype,(LPWSTR)resid,&resdata,&reslen,&off);
590 if (reslen>datalen) reslen = datalen;
591 memcpy(data,resdata,reslen);
595 LZSeek32(lzfd,off,SEEK_SET);
596 reslen = LZRead32(lzfd,data,datalen);
601 /* GetFileVersionInfoSize [VER.6] */
602 DWORD WINAPI GetFileVersionInfoSize16(LPCSTR filename,LPDWORD handle)
604 DWORD len,ret,isuni=0;
606 VS_FIXEDFILEINFO *vffi;
608 TRACE(ver,"(%s,%p)\n",filename,handle);
609 len=GetFileResourceSize(filename,VS_FILE_INFO,VS_VERSION_INFO,handle);
613 filename,VS_FILE_INFO,VS_VERSION_INFO,*handle,sizeof(buf),buf
618 vffi=(VS_FIXEDFILEINFO*)(buf+0x14);
619 if (vffi->dwSignature != VS_FFI_SIGNATURE) {
620 /* unicode resource */
621 if (vffi->dwSignature == 0x004f0049) {
623 vffi = (VS_FIXEDFILEINFO*)(buf+0x28);
625 WARN(ver,"vffi->dwSignature is 0x%08lx, but not 0x%08lx!\n",
626 vffi->dwSignature,VS_FFI_SIGNATURE
631 if (*(WORD*)buf < len)
635 print_vffi_debug(vffi);
640 /* GetFileVersionInfoSize32A [VERSION.1] */
641 DWORD WINAPI GetFileVersionInfoSize32A(LPCSTR filename,LPDWORD handle)
643 TRACE(ver,"(%s,%p)\n",filename,handle);
644 return GetFileVersionInfoSize16(filename,handle);
647 /* GetFileVersionInfoSize32W [VERSION.2] */
648 DWORD WINAPI GetFileVersionInfoSize32W( LPCWSTR filename, LPDWORD handle )
650 LPSTR xfn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
651 DWORD ret = GetFileVersionInfoSize16( xfn, handle );
652 HeapFree( GetProcessHeap(), 0, xfn );
656 /* GetFileVersionInfo [VER.7] */
657 DWORD WINAPI GetFileVersionInfo16(LPCSTR filename,DWORD handle,DWORD datasize,
660 TRACE(ver,"(%s,%ld,%ld,%p)\n",
661 filename,handle,datasize,data
663 return GetFileResource(
664 filename,VS_FILE_INFO,VS_VERSION_INFO,handle,datasize,data
668 /* GetFileVersionInfoA [VERSION.0] */
669 DWORD WINAPI GetFileVersionInfo32A(LPCSTR filename,DWORD handle,
670 DWORD datasize,LPVOID data)
672 return GetFileVersionInfo16(filename,handle,datasize,data);
675 /* GetFileVersionInfoW [VERSION.3] */
676 DWORD WINAPI GetFileVersionInfo32W( LPCWSTR filename, DWORD handle,
677 DWORD datasize, LPVOID data)
679 LPSTR fn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
680 DWORD ret = GetFileVersionInfo16( fn, handle, datasize, data );
681 HeapFree( GetProcessHeap(), 0, fn );
685 /*****************************************************************************
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.
693 * 30-May-1997 Dave Cuthbert (dacut@ece.cmu.edu)
694 * Reimplementation of VerFindFile from original stub.
696 ****************************************************************************/
698 DWORD WINAPI VerFindFile16(
704 UINT16 *lpuCurDirLen,
706 UINT16 *lpuDestDirLen )
711 unsigned int curDirSizeReq;
712 unsigned int destDirSizeReq;
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");
724 ver_dstring("\tlpszFilename = ", lpszFilename, "");
725 ver_dstring("\tlpszWinDir = ", lpszWinDir, "");
726 ver_dstring("\tlpszAppDir = ", lpszAppDir, "");
728 TRACE(ver, "\tlpszCurDir = %p\n", lpszCurDir);
730 TRACE(ver, "\tlpuCurDirLen = %p (%u)\n",
731 lpuCurDirLen, *lpuCurDirLen);
733 TRACE(ver, "\tlpuCurDirLen = (null)\n");
735 TRACE(ver, "\tlpszDestDir = %p\n", lpszDestDir);
737 TRACE(ver, "\tlpuDestDirLen = %p (%u)\n",
738 lpuDestDirLen, *lpuDestDirLen);
740 /* Figure out where the file should go; shared files default to the
746 if(flags & VFFF_ISSHAREDFILE) {
747 GetSystemDirectory32A(destDir, 256);
749 /* Were we given a filename? If so, try to find the file. */
751 if(testFileExistence(destDir, lpszFilename)) {
752 strcpy(curDir, destDir);
754 if(!testFileExclusiveExistence(destDir, lpszFilename))
755 retval |= VFF_FILEINUSE;
757 else if(lpszAppDir && testFileExistence(lpszAppDir,
759 strcpy(curDir, lpszAppDir);
760 retval |= VFF_CURNEDEST;
762 if(!testFileExclusiveExistence(lpszAppDir, lpszFilename))
763 retval |= VFF_FILEINUSE;
767 else if(!(flags & VFFF_ISSHAREDFILE)) { /* not a shared file */
770 GetSystemDirectory32A(systemDir, 256);
772 strcpy(destDir, lpszAppDir);
775 if(testFileExistence(lpszAppDir, lpszFilename)) {
776 strcpy(curDir, lpszAppDir);
778 if(!testFileExclusiveExistence(lpszAppDir, lpszFilename))
779 retval |= VFF_FILEINUSE;
781 else if(testFileExistence(systemDir, lpszFilename)) {
782 strcpy(curDir, systemDir);
783 retval |= VFF_CURNEDEST;
785 if(!testFileExclusiveExistence(systemDir, lpszFilename))
786 retval |= VFF_FILEINUSE;
792 curDirSizeReq = strlen(curDir) + 1;
793 destDirSizeReq = strlen(destDir) + 1;
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! */
801 if(lpuDestDirLen && lpszDestDir) {
802 if(*lpuDestDirLen < destDirSizeReq) {
803 retval |= VFF_BUFFTOOSMALL;
804 strncpy(lpszDestDir, destDir, *lpuDestDirLen - 1);
805 lpszDestDir[*lpuDestDirLen - 1] = '\0';
808 strcpy(lpszDestDir, destDir);
810 *lpuDestDirLen = destDirSizeReq;
813 if(lpuCurDirLen && lpszCurDir) {
814 if(*lpuCurDirLen < curDirSizeReq) {
815 retval |= VFF_BUFFTOOSMALL;
816 strncpy(lpszCurDir, curDir, *lpuCurDirLen - 1);
817 lpszCurDir[*lpuCurDirLen - 1] = '\0';
820 strcpy(lpszCurDir, curDir);
822 *lpuCurDirLen = curDirSizeReq;
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 " : "");
830 ver_dstring("\t(Exit) lpszCurDir = ", lpszCurDir, "");
832 TRACE(ver, "\t(Exit) lpuCurDirLen = %p (%u)\n",
833 lpuCurDirLen, *lpuCurDirLen);
835 TRACE(ver, "\t(Exit) lpuCurDirLen = (null)\n");
837 ver_dstring("\t(Exit) lpszDestDir = ", lpszDestDir, "");
839 TRACE(ver, "\t(Exit) lpuDestDirLen = %p (%u)\n",
840 lpuDestDirLen, *lpuDestDirLen);
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 )
850 UINT16 curdirlen, destdirlen;
853 curdirlen = (UINT16)*pcurdirlen;
854 destdirlen= (UINT16)*pdestdirlen;
856 ret = VerFindFile16(flags,filename,windir,appdir,
857 curdir,&curdirlen,destdir,&destdirlen);
858 *pcurdirlen = curdirlen;
859 *pdestdirlen = destdirlen;
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 )
868 UINT16 curdirlen, destdirlen;
869 LPSTR wfn,wwd,wad,wdd,wcd;
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 );
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 )
896 DWORD ret= VerInstallFile32A(flags,srcfilename,destfilename,srcdir,
897 destdir,curdir,tmpfile,&filelen);
899 *tmpfilelen = filelen;
904 _fetch_versioninfo(LPSTR fn,VS_FIXEDFILEINFO **vffi) {
910 buf= xmalloc(alloclen);
912 ret = GetFileVersionInfo32A(fn,0,alloclen,buf);
917 if (alloclen<*(WORD*)buf) {
919 alloclen = *(WORD*)buf;
920 buf = xmalloc(alloclen);
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);
933 _error2vif(DWORD error) {
935 case ERROR_ACCESS_DENIED:
936 return VIF_ACCESSVIOLATION;
937 case ERROR_SHARING_VIOLATION:
938 return VIF_SHARINGVIOLATION;
945 /******************************************************************************
946 * VerInstallFile32A [VERSION.7]
948 DWORD WINAPI VerInstallFile32A(
949 UINT32 flags,LPCSTR srcfilename,LPCSTR destfilename,LPCSTR srcdir,
950 LPCSTR destdir,LPCSTR curdir,LPSTR tmpfile,UINT32 *tmpfilelen )
953 char destfn[260],tmpfn[260],srcfn[260];
955 DWORD attr,ret,xret,tmplast;
959 TRACE(ver,"(%x,%s,%s,%s,%s,%s,%p,%d)\n",
960 flags,srcfilename,destfilename,srcdir,destdir,curdir,tmpfile,*tmpfilelen
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);
974 if (attr & FILE_ATTRIBUTE_READONLY) {
976 return VIF_WRITEPROT;
978 /* FIXME: check if file currently in use and return VIF_FILEINUSE */
981 if (flags & VIFF_FORCEINSTALL) {
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...
994 GetTempFileName32A(pdest,"ver",0,tmpfn); /* should not fail ... */
995 s=strrchr(tmpfn,'\\');
1000 hfdst = OpenFile32(tmpfn,&ofs,OF_CREATE);
1001 if (hfdst == HFILE_ERROR32) {
1003 return VIF_CANNOTCREATE; /* | translated dos error */
1005 ret = LZCopy32(hfsrc,hfdst);
1007 if (((long) ret) < 0) {
1008 /* translate LZ errors into VIF_xxx */
1010 case LZERROR_BADINHANDLE:
1012 case LZERROR_BADVALUE:
1013 case LZERROR_UNKNOWNALG:
1014 ret = VIF_CANNOTREADSRC;
1016 case LZERROR_BADOUTHANDLE:
1018 ret = VIF_OUTOFMEMORY; /* FIXME: correct? */
1020 case LZERROR_GLOBALLOC:
1021 case LZERROR_GLOBLOCK:
1022 ret = VIF_OUTOFSPACE;
1024 default: /* unknown error, should not happen */
1035 if (!(flags & VIFF_FORCEINSTALL)) {
1036 VS_FIXEDFILEINFO *destvffi,*tmpvffi;
1037 buf1 = _fetch_versioninfo(destfn,&destvffi);
1039 buf2 = _fetch_versioninfo(tmpfn,&tmpvffi);
1046 /* compare file versions */
1047 if ((destvffi->dwFileVersionMS > tmpvffi->dwFileVersionMS)||
1048 ((destvffi->dwFileVersionMS==tmpvffi->dwFileVersionMS)&&
1049 (destvffi->dwFileVersionLS > tmpvffi->dwFileVersionLS)
1052 xret |= VIF_MISMATCH|VIF_SRCOLD;
1053 /* compare filetypes and filesubtypes */
1054 if ((destvffi->dwFileType!=tmpvffi->dwFileType) ||
1055 (destvffi->dwFileSubtype!=tmpvffi->dwFileSubtype)
1057 xret |= VIF_MISMATCH|VIF_DIFFTYPE;
1058 if (VerQueryValue32A(buf1,"\\VarFileInfo\\Translation",(LPVOID*)&tbuf1,&len1) &&
1059 VerQueryValue32A(buf2,"\\VarFileInfo\\Translation",(LPVOID*)&tbuf2,&len2)
1061 /* irgendwas mit tbuf1 und tbuf2 machen
1062 * generiert DIFFLANG|MISMATCH
1067 xret=VIF_MISMATCH|VIF_SRCOLD;
1072 if (*tmpfilelen<strlen(tmpfn+tmplast)) {
1073 xret|=VIF_BUFTOSMALL;
1074 DeleteFile32A(tmpfn);
1076 strcpy(tmpfile,tmpfn+tmplast);
1077 *tmpfilelen = strlen(tmpfn+tmplast)+1;
1081 if (-1!=GetFileAttributes32A(destfn))
1082 if (!DeleteFile32A(destfn)) {
1083 xret|=_error2vif(GetLastError())|VIF_CANNOTDELETE;
1084 DeleteFile32A(tmpfn);
1088 if ((!(flags & VIFF_DONTDELETEOLD)) &&
1091 lstrcmpi32A(curdir,pdest)
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;
1102 if (!MoveFile32A(tmpfn,destfn)) {
1103 xret|=_error2vif(GetLastError())|VIF_CANNOTRENAME;
1104 DeleteFile32A(tmpfn);
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 )
1117 LPSTR wsrcf,wsrcd,wdestf,wdestd,wtmpf,wcurd;
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);
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 );
1135 HeapFree( GetProcessHeap(), 0, wcurd );
1143 /* in memory structure... */
1144 char name[1]; /* padded to dword alignment */
1146 char data[datalen]; padded to dword alignment
1147 BYTE subdirdata[]; until nextoff
1151 #define DATA_OFFSET_A(db) ((4+(strlen((db)->name)+4))&~3)
1156 WORD btext; /* type of data */
1157 /* in memory structure... */
1158 WCHAR name[1]; /* padded to dword alignment */
1160 WCHAR data[datalen]; padded to dword alignment
1161 BYTE subdirdata[]; until nextoff
1168 * WCHAR szKey[]; (zero terminated)
1169 * PADDING (round up to nearest 32bit boundary)
1171 #define DATA_OFFSET_W(db) ((2+2+2+((lstrlen32W((db)->name)+1)*2+3))&~3)
1173 /* this one used for Win16 resources, which are always in ASCII format */
1175 _find_dataA(BYTE *block,LPCSTR str, int buff_remain) {
1177 int substrlen, inc_size;
1180 while (*str && *str=='\\')
1182 if (NULL!=(nextslash=strchr(str,'\\')))
1183 substrlen=nextslash-str;
1185 substrlen=strlen(str);
1186 if (nextslash!=NULL) {
1187 while (*nextslash && *nextslash=='\\')
1191 } else if (*str == 0)
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
1200 if ((!db->nextoff) || (buff_remain<=0)) /* no more entries ? */
1203 TRACE(ver,"comparing with %s\n",db->name);
1204 if (!lstrncmpi32A(db->name,str,substrlen)) {
1206 inc_size=DATA_OFFSET_A(db)+((db->datalen+3)&~3);
1207 return _find_dataA(block+inc_size,nextslash,
1208 db->nextoff-inc_size);
1212 inc_size = ((db->nextoff+3)&~3);
1214 buff_remain -= inc_size;
1218 /* this one used for Win32 resources, which are always in UNICODE format */
1219 extern LPWSTR __cdecl CRTDLL_wcschr(LPCWSTR str,WCHAR xchar);
1221 _find_dataW(BYTE *block,LPCWSTR str, int buff_remain) {
1223 int substrlen, inc_size;
1227 while (*str && *str=='\\')
1229 if (NULL!=(nextslash=CRTDLL_wcschr(str,'\\')))
1230 substrlen=nextslash-str;
1232 substrlen=lstrlen32W(str);
1233 if (nextslash!=NULL) {
1234 while (*nextslash && *nextslash=='\\')
1238 } else if (*str == 0)
1244 db=(struct dbW*)block;
1245 xs= HEAP_strdupWtoA(GetProcessHeap(),0,db->name);
1248 vs = HEAP_strdupWtoA(GetProcessHeap(),0,(WCHAR*)((block+DATA_OFFSET_W(db))));
1250 vs = HEAP_strdupA(GetProcessHeap(),0,"not a string");
1252 vs = HEAP_strdupA(GetProcessHeap(),0,"no data");
1254 TRACE(ver,"db->nextoff=%d,db->name=%s,db->data=\"%s\"\n",
1257 HeapFree(GetProcessHeap(),0,vs);
1258 HeapFree(GetProcessHeap(),0,xs);
1259 if ((!db->nextoff) || (buff_remain<=0)) /* no more entries ? */
1262 if (!lstrncmpi32W(db->name,str,substrlen)) {
1264 /* DATA_OFFSET_W(db) (padded to 32bit already)
1266 * PADDING (round up to nearest 32bit boundary)
1267 * --> next level structs
1269 inc_size=DATA_OFFSET_W(db)+((db->datalen+3)&~3);
1270 return _find_dataW( block+inc_size ,nextslash,
1271 db->nextoff-inc_size);
1275 /* skip over this block, round up to nearest 32bit boundary */
1276 inc_size = ((db->nextoff+3)&~3);
1278 buff_remain -= inc_size;
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,
1288 BYTE *block=PTR_SEG_TO_LIN(segblock),*b;
1290 TRACE(ver,"(%p,%s,%p,%d)\n",
1291 block,subblock,buffer,*buflen
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)
1304 wstr = HEAP_strdupAtoW(GetProcessHeap(),0,s);
1305 b=_find_dataW(block,wstr,*(WORD*)block);
1306 HeapFree(GetProcessHeap(),0,wstr);
1308 WARN(ver,"key %s not found in versionresource.\n",s);
1314 b = b+DATA_OFFSET_W(db);
1315 *buflen = db->datalen;
1317 xs = HEAP_strdupWtoA(GetProcessHeap(),0,(WCHAR*)b);
1318 TRACE(ver,"->%s\n",xs);
1319 HeapFree(GetProcessHeap(),0,xs);
1321 TRACE(ver,"->%p\n",b);
1324 b=_find_dataA(block,s,*(WORD*)block);
1326 WARN(ver,"key %s not found in versionresource.\n",s);
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);
1338 TRACE(ver," -> %s=%p\n",subblock,b);
1340 *buffer = (b-block)+segblock;
1345 DWORD WINAPI VerQueryValue32A(LPVOID vblock,LPCSTR subblock,
1346 LPVOID *vbuffer,UINT32 *buflen)
1348 BYTE *b,*block=(LPBYTE)vblock,**buffer=(LPBYTE*)vbuffer;
1351 TRACE(ver,"(%p,%s,%p,%d)\n",
1352 block,subblock,buffer,*buflen
1355 s=(char*)xmalloc(strlen("VS_VERSION_INFO\\")+strlen(subblock)+1);
1356 strcpy(s,"VS_VERSION_INFO\\");strcat(s,subblock);
1358 /* check for UNICODE version */
1359 if ( (*(DWORD*)(block+0x14) != VS_FFI_SIGNATURE) &&
1360 (*(DWORD*)(block+0x28) == VS_FFI_SIGNATURE)
1366 wstr = HEAP_strdupAtoW(GetProcessHeap(),0,s);
1367 b=_find_dataW(block,wstr,*(WORD*)block);
1368 HeapFree(GetProcessHeap(),0,wstr);
1370 WARN(ver,"key %s not found in versionresource.\n",s);
1375 db = (struct dbW*)b;
1376 *buflen = db->datalen;
1377 b = b+DATA_OFFSET_W(db);
1379 xs = HEAP_strdupWtoA(GetProcessHeap(),0,(WCHAR*)b);
1380 TRACE(ver,"->%s\n",xs);
1381 HeapFree(GetProcessHeap(),0,xs);
1383 TRACE(ver,"->%p\n",b);
1384 /* This is a leak. */
1385 b = HEAP_strdupWtoA(GetProcessHeap(),0,(WCHAR*)b);
1388 b=_find_dataA(block,s,*(WORD*)block);
1390 WARN(ver,"key %s not found in versionresource.\n",subblock);
1396 *buflen = db->datalen;
1397 b = b+DATA_OFFSET_A(db);
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);
1403 TRACE(ver," -> %s=%p\n",subblock,b);
1410 DWORD WINAPI VerQueryValue32W(LPVOID vblock,LPCWSTR subblock,LPVOID *vbuffer,
1416 sb = HEAP_strdupWtoA( GetProcessHeap(), 0, subblock );
1417 ret = VerQueryValue32A(vblock,sb,vbuffer,buflen);
1418 HeapFree( GetProcessHeap(), 0, sb );
1421 /* 20 GETFILEVERSIONINFORAW */