getfilesversion.cpp
Upload User: dzyhzl
Upload Date: 2019-04-29
Package Size: 56270k
Code Size: 15k
Development Platform:

C/C++

  1. //////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. //  FileName    :   GetFilesVersion.cpp
  4. //  Version     :   1.0
  5. //  Creater     :   Cheng Bitao
  6. //  Date        :   2002-2-1 15:50:02
  7. //  Comment     :   The function for get duba files version
  8. //
  9. //////////////////////////////////////////////////////////////////////////////////////
  10. #include "stdafx.h"
  11. #include "GetFilesVersion.h"
  12. #include "Global.h"
  13. #include "GetVersion.h"
  14. #include "KAVStrTranslate.h"    
  15. #include "UpdateData.h"
  16. #include "PublicFun.h"
  17. extern KPATH_TABLE      g_PathTable;
  18. extern KUPDATE_DATA     g_UpdateData;
  19. // Get engine file version, such as KAEngine.dat DrWeb32.dat
  20. // Get kingsoft antivirus file version by check version method
  21. int GetKAVFileVersion(
  22.     LPCTSTR lpszFileName, 
  23.     KCHECKVERSIONMETHOD CheckVersionMethod, 
  24.     DWORD *pdwMajorVersion, 
  25.     DWORD *pdwMinorVersion
  26. )
  27. {
  28.     int nResult         = false;
  29.     
  30.     *pdwMajorVersion    = 0;
  31.     *pdwMinorVersion    = 0;
  32.     
  33.     //if this file does not exist in user's disk
  34.     //then won't download it...
  35.     nResult = FileExists(lpszFileName);
  36.     KAV_PROCESS_ERROR(nResult);
  37.     
  38.     switch (CheckVersionMethod)
  39.     {
  40.     case enumCVM_NORMAL:     // Normal file, such as KAV9X.exe, KAEPlat.dll
  41.         nResult = GetFileVersion(lpszFileName, pdwMajorVersion, pdwMinorVersion);
  42.         break;
  43.     case enumCVM_ENGINE:     // Engine file
  44.         nResult = GetEngineFileVersion(lpszFileName, pdwMajorVersion, pdwMinorVersion);
  45.         break;
  46.     case enumCVM_SIGN:       // Sign file
  47.         nResult = GetSignFileVersion(lpszFileName, pdwMajorVersion, pdwMinorVersion);
  48.         break;
  49.     default:      // CheckVersionMethod == enumCVM_DATA
  50.         break;
  51.     }
  52.     
  53. Exit0:
  54.     return nResult;
  55. }
  56. // Get engine file version, such as KAEngine.dat DrWeb32.dat
  57. int GetEngineFileVersion(
  58.     const char cszFileName[], 
  59.     DWORD *pdwMajorVersion, 
  60.     DWORD *pdwMinorVersion
  61. )
  62. {
  63. int nResult                  = false;
  64.     int nRetCode                 = 0;
  65.     int nReadSize                = 0;
  66.     int nKAVEngineFileHeaderSize = 0;
  67.     HANDLE hFile                 = INVALID_HANDLE_VALUE;
  68.     KAVEngineFileHeader Header;
  69.     ASSERT(pdwMajorVersion);
  70.     ASSERT(pdwMinorVersion);
  71.     
  72. *pdwMajorVersion = 0;
  73. *pdwMinorVersion = 0;
  74.     hFile = ::CreateFile(
  75.         cszFileName,  
  76.         GENERIC_READ, 
  77.         FILE_SHARE_READ, 
  78.         NULL, 
  79.         OPEN_EXISTING, 
  80.         FILE_ATTRIBUTE_NORMAL, 
  81.         NULL
  82.     );
  83.     KAV_PROCESS_ERROR(INVALID_HANDLE_VALUE != hFile);
  84.     nKAVEngineFileHeaderSize = sizeof(KAVEngineFileHeader);
  85.     
  86.     nRetCode = ::ReadFile(hFile, (void *)&Header, nKAVEngineFileHeaderSize, (DWORD *)&nReadSize, NULL);
  87.     KAV_PROCESS_ERROR(!(!nRetCode || (nReadSize != nKAVEngineFileHeaderSize)));
  88.         
  89.     *pdwMajorVersion = Header.dwOrignMajorVersion;
  90.     *pdwMinorVersion = Header.dwOrignMinorVersion;
  91.         
  92.     nResult = true;
  93. Exit0:
  94.     if (INVALID_HANDLE_VALUE != hFile)
  95.     {
  96.         ::CloseHandle(hFile);
  97.         hFile = INVALID_HANDLE_VALUE;
  98.     }
  99. return nResult;
  100. }
  101. // Get sign file version, such as KAV00000.dat KAV00000.upd
  102. int GetSignFileVersion(
  103.     const char cszFileName[], 
  104.     DWORD *pdwMajorVersion, 
  105.     DWORD *pdwMinorVersion
  106. )
  107. {
  108.     int nResult                  = false;
  109.     int nRetCode                 = 0;
  110.     int nReadSize                = 0;
  111.     int  nKAVSignFileHeaderSize  = 0;
  112.     HANDLE hFile                 = INVALID_HANDLE_VALUE;
  113.     KAVSIGNFILEHEADER Header;
  114.     WORD wTemp                   = 0;
  115.     ASSERT(pdwMajorVersion);
  116.     ASSERT(pdwMinorVersion);
  117.     *pdwMajorVersion  = 0;
  118.     *pdwMinorVersion  = 0;
  119.     hFile = ::CreateFile(
  120.         cszFileName,  
  121.         GENERIC_READ, 
  122.         FILE_SHARE_READ, 
  123.         NULL, 
  124.         OPEN_EXISTING, 
  125.         FILE_ATTRIBUTE_NORMAL, 
  126.         NULL
  127.     );
  128.     KAV_PROCESS_ERROR(INVALID_HANDLE_VALUE != hFile);
  129.         
  130.     nKAVSignFileHeaderSize = sizeof(KAVSIGNFILEHEADER);
  131.     
  132.     nRetCode = ::ReadFile(hFile, (void *)&Header, nKAVSignFileHeaderSize, (DWORD *)&nReadSize, NULL);
  133.     KAV_PROCESS_ERROR(!(!nRetCode || (nReadSize != nKAVSignFileHeaderSize)));
  134.     //dwMajorVersion
  135.     wTemp = ((WORD)(Header.dwDateStamp >> 8)) & 0x00FF;
  136.     *pdwMajorVersion = (Header.dwDateStamp & 0xFFFF0000) + wTemp;
  137.         
  138.     //dwMinorVersion
  139.     wTemp = ((WORD)Header.dwDateStamp) & 0x00FF;
  140.     *pdwMinorVersion = wTemp;
  141.     *pdwMinorVersion = *pdwMinorVersion << 16;
  142.     nResult = true;
  143.    
  144. Exit0:
  145.     if (INVALID_HANDLE_VALUE != hFile)
  146.     {
  147.         ::CloseHandle(hFile);
  148.         hFile = INVALID_HANDLE_VALUE;
  149.     }
  150.     return nResult;
  151. }
  152. // Get local max sign DAT file number
  153. int GetLocalDatFileNumber()
  154. {
  155.     char szLocalPathName[MAX_PATH + 1];
  156.     char szSignFileName[13]     = defBASE_SIGN_FILENAME;    
  157.     int  nSignFileNameNum       = 0;
  158.     WIN32_FIND_DATA *pFindData  = NULL;
  159.     int  nRetCode               = 0;
  160.     HANDLE hFind                = INVALID_HANDLE_VALUE;
  161.     
  162.     strcpy(szLocalPathName, g_PathTable.szUpdateDestPath);
  163.     strcat(szLocalPathName, "KAV?????.DAT");
  164.     
  165.     pFindData = new WIN32_FIND_DATA;
  166.     KAV_PROCESS_ERROR(pFindData);
  167.     hFind = ::FindFirstFile(szLocalPathName, pFindData);
  168.     if (hFind != INVALID_HANDLE_VALUE)
  169.     {
  170.         do    // Get max number file name
  171.         {
  172.             if (IsSignFileName(pFindData->cFileName))
  173.                 if (stricmp(pFindData->cFileName, szSignFileName) > 0)
  174.                     strcpy(szSignFileName, pFindData->cFileName);
  175.        
  176.         } while (::FindNextFile(hFind, pFindData));
  177.     
  178.         ::FindClose(hFind);
  179.         hFind = INVALID_HANDLE_VALUE;
  180.     }
  181.     if (szSignFileName[8] == '.')   // Get file number
  182.     {
  183.         szSignFileName[8] = '';
  184.         nSignFileNameNum  = _StrToInt(szSignFileName + 3);
  185.         szSignFileName[8] = '.';
  186.     }
  187. Exit0:
  188.     KAV_DELETE(pFindData);
  189.     return nSignFileNameNum;
  190. }
  191. int GetLocalSignVersion(DWORD *pdwMajorVersion, DWORD *pdwMinorVersion)
  192. {
  193.     char szSignFileName[MAX_PATH];
  194.     char szFileName[MAX_PATH];
  195.     int  nSignFileNameSize      = 0;
  196.     int  nBaseSignNum           = 0;
  197.     int  nUpdateSignNum         = 0;
  198.     int  nLen                   = 0;
  199.     int  nRetCode               = false;
  200.     int  nResult                = false;
  201.     unsigned uMajorVersion      = 0;
  202.     unsigned uMinorVersion      = 0;
  203.     // Get KAVXXXXX.DAT Version
  204.     nBaseSignNum = GetLocalDatFileNumber();
  205.     sprintf(szFileName, "KAV%05d.DAT", nBaseSignNum);
  206.     strcpy(szSignFileName, g_PathTable.szUpdateDestPath);
  207.     strcat(szSignFileName, szFileName);
  208.     nRetCode = GetSignFileVersion(szSignFileName, (unsigned long *)&uMajorVersion, (unsigned long *)&uMinorVersion);
  209.     if ((uMajorVersion == 0) && (uMinorVersion == 0))
  210.         return false;
  211.     *pdwMajorVersion = uMajorVersion;
  212.     *pdwMinorVersion = uMinorVersion;
  213.     nSignFileNameSize = strlen(szSignFileName) + 1;
  214.     // Find the update sign data file.
  215.     nUpdateSignNum = nBaseSignNum;
  216.     strcpy(szFileName, g_PathTable.szUpdateDestPath);
  217.     strcat(szFileName, defBASE_SIGN_UPDNAME);
  218.     while (true)
  219.     {
  220.         nUpdateSignNum++;
  221.         _IntToNStr(nUpdateSignNum, szFileName + nSignFileNameSize - 10, 5);
  222.         nRetCode = FileExists(szFileName);
  223.         if (!nRetCode)
  224.         {
  225.             break;
  226.         }
  227.         else
  228.         {
  229.             nRetCode = GetSignFileVersion(szFileName, (unsigned long *)&uMajorVersion, (unsigned long *)&uMinorVersion);
  230.             if ((uMajorVersion == 0) && (uMinorVersion == 0))
  231.                 break;
  232.             
  233.             *pdwMajorVersion = uMajorVersion;
  234.             *pdwMinorVersion = uMinorVersion;
  235.         }
  236.     }
  237.     
  238.     return true;
  239. }                                   
  240. int GetLocalSignVersion(char szVersion[])
  241. {
  242.     unsigned uMajorVersion      = 0;
  243.     unsigned uMinorVersion      = 0;
  244.     GetLocalSignVersion((unsigned long *)&uMajorVersion, (unsigned long *)&uMinorVersion);
  245.     GetVersionStringFromInt(
  246.             uMajorVersion, uMinorVersion, 
  247.             szVersion, defVERSION_SIZE
  248.     );
  249.     return true;
  250. }
  251. // Get Max number of sign file name
  252. int GetLocalSignFileNumber()
  253. {
  254.     int  nSignFileNameNum       = 0;
  255.     int  nRetCode               = 0;
  256.     
  257.     nSignFileNameNum = GetLocalDatFileNumber();
  258.     nRetCode = ExistUpdFileByNum(nSignFileNameNum + 1);
  259.     while (nRetCode)
  260.     {
  261.         nSignFileNameNum++;
  262.         nRetCode = ExistUpdFileByNum(nSignFileNameNum + 1);
  263.     }
  264.     return nSignFileNameNum;
  265. }
  266. // Check Upd file exists by number
  267. int ExistUpdFileByNum(int nNum)
  268. {
  269.     int nRetCode            = false;
  270.     char szSignVersion[6];
  271.     char szLocalPathName[MAX_PATH + 1];
  272.     int nLocalPathNameLen   = 0;
  273.     
  274.     strcpy(szLocalPathName, g_PathTable.szUpdateDestPath);
  275.     strcat(szLocalPathName, defBASE_SIGN_UPDNAME);
  276.     nLocalPathNameLen = strlen(szLocalPathName);
  277.     nRetCode = _IntToStr(nNum, 6, szSignVersion);
  278.     memcpy(szLocalPathName + nLocalPathNameLen - 4 - nRetCode, szSignVersion, nRetCode);
  279.     
  280.     if (FileExists(szLocalPathName))
  281.         return true;
  282.     return false;
  283. }
  284. // Check sign file by file header
  285. int IsSignFile(const char szFileName[])
  286. {
  287.     int nResult                 = false;
  288.     KAVSIGNFILEHEADER KAVSignFileHeader;
  289.     int nKAVSignFileHeaderSize  = 0;
  290.     int nRetCode                = 0;
  291.     int nReadSize               = 0;
  292.     HANDLE hFile                = INVALID_HANDLE_VALUE;
  293.     hFile = ::CreateFile(
  294.         szFileName,  
  295.         GENERIC_READ, 
  296.         FILE_SHARE_READ, 
  297.         NULL, 
  298.         OPEN_EXISTING, 
  299.         FILE_ATTRIBUTE_NORMAL, 
  300.         NULL
  301.     );
  302.     KAV_PROCESS_ERROR(INVALID_HANDLE_VALUE != hFile);
  303.     nKAVSignFileHeaderSize = sizeof(KAVSIGNFILEHEADER);
  304.     
  305.     nRetCode = ::ReadFile(hFile, (void *)&KAVSignFileHeader, nKAVSignFileHeaderSize, (DWORD *)&nReadSize, NULL);
  306.     KAV_PROCESS_ERROR(!(!nRetCode || (nReadSize != nKAVSignFileHeaderSize)));
  307.     
  308.     nRetCode = strnicmp(KAVSignFileHeader.cszDescription, "Kingsoft AntiVirus Database", 27);
  309.     if (!nRetCode)
  310.         nResult = true;
  311. Exit0:
  312.     if (INVALID_HANDLE_VALUE != hFile)
  313.     {
  314.         ::CloseHandle(hFile);
  315.         hFile = INVALID_HANDLE_VALUE;
  316.     }
  317.  
  318.     return nResult;
  319. }
  320. int GetSignFileNumber(const char cszSignName[])
  321. {
  322.     int nResult         = -1;
  323.     int nSignNameLen    = strlen(cszSignName);
  324.     char szFileNameNum[13];
  325.     KAV_PROCESS_ERROR(nSignNameLen == 12);
  326.     
  327.     KAV_PROCESS_ERROR(IsNumeric(cszSignName + 3, 5));
  328.     strncpy(szFileNameNum, cszSignName + 3, 5);
  329.     szFileNameNum[5] = '';
  330.     nResult = _StrToInt(szFileNameNum);    
  331. Exit0:
  332.     return nResult;
  333. }
  334. int IsUpdFileName(const char cszFileName[])
  335. {
  336.     int nResult         = false;
  337.     int nFileNameLen    = strlen(cszFileName);
  338.     KAV_PROCESS_ERROR(nFileNameLen == 12);
  339.         
  340.     KAV_PROCESS_ERROR(strnicmp(cszFileName, "KAV", 3) == 0);
  341.     
  342.     KAV_PROCESS_ERROR(IsNumeric(cszFileName + 3, 5));
  343.         
  344.     if (strnicmp(cszFileName + nFileNameLen - 4, ".UPD", 4) == 0)
  345.         nResult = true;
  346. Exit0:
  347.     return nResult;    
  348. }
  349. int IsSignFileName(const char cszFileName[])
  350. {
  351.     int nResult         = false;
  352.     int nFileNameLen    = strlen(cszFileName);
  353.     KAV_PROCESS_ERROR(nFileNameLen == 12);
  354.         
  355.     KAV_PROCESS_ERROR(strnicmp(cszFileName, "KAV", 3) == 0);
  356.     
  357.     KAV_PROCESS_ERROR(IsNumeric(cszFileName + 3, 5));
  358.     if (strnicmp(cszFileName + nFileNameLen - 4, ".DAT", 4) == 0)
  359.         nResult = true;
  360. Exit0:
  361.     return nResult;    
  362. }
  363.     
  364. int GetUpdFileNumber(const char cszUpdFileName[])
  365. {
  366.     return GetSignFileNumber(cszUpdFileName);
  367. }
  368. // Check is exist the same version sign file
  369. // 0: no
  370. // 1: Exist the same Version file
  371. // -1: Exist the same file
  372. int CheckExistSameVersionFile(const char cszFileName[])
  373. {
  374.     int nRetCode = 0;
  375.     char szFileName[MAX_PATH] = {0};
  376.     char *pszExtName = NULL;
  377.     char szFullFileName[MAX_PATH] = {0};
  378.     strcpy(szFileName, cszFileName);
  379.     pszExtName = szFileName + strlen(szFileName) - 3;
  380.     if (IsUpdFileName(cszFileName))   
  381. strcpy(pszExtName, "DAT");
  382.     else if (IsSignFileName(cszFileName))
  383.         strcpy(pszExtName, "UPD");
  384.     strcpy(szFullFileName, g_PathTable.szUpdateDestPath);
  385.     strcat(szFullFileName, szFileName);
  386.     nRetCode = FileExists(szFullFileName);
  387. if (nRetCode)
  388. return 1;
  389. strcpy(szFullFileName, g_PathTable.szUpdateDestPath);
  390. strcat(szFullFileName, cszFileName);
  391. nRetCode = FileExists(szFullFileName);
  392. if (nRetCode)
  393. return -1;        
  394. return 0;
  395. }
  396. int GetNormalVersion(const char cszPathName[], char szVersion[])
  397. {
  398.     int nRetCode = 0;
  399.     unsigned uMajorVersion = 0;
  400.     unsigned uMinorVersion = 0;
  401.     nRetCode = GetFileVersion(cszPathName, (DWORD *)&uMajorVersion, (DWORD *)&uMinorVersion);
  402.     if (nRetCode)
  403.         nRetCode = GetVersionStringFromInt(
  404.             uMajorVersion, uMinorVersion, 
  405.             szVersion, defVERSION_SIZE
  406.         );
  407.     else 
  408.         nRetCode = GetVersionStringFromInt(1, 0, szVersion, defVERSION_SIZE);
  409.     return nRetCode;
  410. }
  411. int GetEngineVersion(const char cszPathName[], char szVersion[])
  412. {
  413.     int nRetCode = 0;
  414.     unsigned uMajorVersion = 0;
  415.     unsigned uMinorVersion = 0;
  416.     nRetCode = GetEngineFileVersion(cszPathName, (DWORD *)&uMajorVersion, (DWORD *)&uMinorVersion);
  417.     if (!nRetCode)
  418.         return nRetCode;
  419.     nRetCode = GetVersionStringFromInt(
  420.             uMajorVersion, uMinorVersion, 
  421.             szVersion, defVERSION_SIZE
  422.     );    
  423.     return nRetCode;
  424. }
  425. int GetSignVersion(const char cszPathName[], char szVersion[])
  426. {
  427.     int nRetCode = 0;
  428.     unsigned uMajorVersion = 0;
  429.     unsigned uMinorVersion = 0;
  430.     nRetCode = GetSignFileVersion(cszPathName, (DWORD *)&uMajorVersion, (DWORD *)&uMinorVersion);
  431.     
  432.     nRetCode = GetVersionStringFromInt(
  433.             uMajorVersion, uMinorVersion, 
  434.             szVersion, defVERSION_SIZE
  435.     );  
  436.     return nRetCode;
  437. }
  438. int GetVirusCountFromSignFile(const char cszPathName[])
  439. {
  440.     HANDLE hFile    = INVALID_HANDLE_VALUE;
  441.     int nVirusCount = 0;
  442.     KAE_KVSIGN_HEADER SignHeader;
  443.     int nRetCode    = 0;
  444.     DWORD dwRead    = 0;
  445.     nRetCode = IsSignFile(cszPathName);
  446.     KAV_PROCESS_ERROR(nRetCode);
  447.         
  448.     hFile = ::CreateFile(cszPathName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_ARCHIVE, NULL);
  449.     KAV_PROCESS_ERROR(INVALID_HANDLE_VALUE != hFile);
  450.     nRetCode = ::ReadFile(hFile, &SignHeader, sizeof(SignHeader), &dwRead, NULL);
  451.     KAV_PROCESS_ERROR(!(!nRetCode || (dwRead != sizeof(SignHeader))));
  452.     
  453.     nVirusCount = SignHeader.uProcessVirusNum;
  454. Exit0:
  455.     if (INVALID_HANDLE_VALUE != hFile)
  456.     {
  457.         ::CloseHandle(hFile);
  458.         hFile = INVALID_HANDLE_VALUE;
  459.     }
  460.     return nVirusCount;
  461. }