From 6023520b8150f43f68c9a3454b4afecd4ea2ac1a Mon Sep 17 00:00:00 2001 From: Kirill Volinsky Date: Mon, 11 Feb 2013 15:47:08 +0000 Subject: added checking crc sum git-svn-id: http://svn.miranda-ng.org/main/trunk@3555 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/PluginUpdater/src/Common.h | 3 +- plugins/PluginUpdater/src/Notifications.cpp | 2 +- plugins/PluginUpdater/src/Scanner.cpp | 11 ++-- plugins/PluginUpdater/src/Utils.cpp | 86 +++++++++++++++++++++++++++-- 4 files changed, 92 insertions(+), 10 deletions(-) (limited to 'plugins') diff --git a/plugins/PluginUpdater/src/Common.h b/plugins/PluginUpdater/src/Common.h index 5969dee2a3..e27d68a2ec 100644 --- a/plugins/PluginUpdater/src/Common.h +++ b/plugins/PluginUpdater/src/Common.h @@ -72,6 +72,7 @@ struct FILEURL { TCHAR tszDownloadURL[2048]; TCHAR tszDiskPath[MAX_PATH]; + int CRCsum; }; struct FILEINFO @@ -139,7 +140,7 @@ int OptInit(WPARAM, LPARAM); void BackupFile(TCHAR *ptszSrcFileName, TCHAR *ptszBackFileName); void DoCheck(int iFlag); -BOOL DownloadFile(LPCTSTR tszURL, LPCTSTR tszLocal); +BOOL DownloadFile(LPCTSTR tszURL, LPCTSTR tszLocal, int CRCsum); void ShowPopup(HWND hDlg, LPCTSTR Title, LPCTSTR Text, int Number, int ActType); void __stdcall RestartMe(void*); BOOL AllowUpdateOnStartup(); diff --git a/plugins/PluginUpdater/src/Notifications.cpp b/plugins/PluginUpdater/src/Notifications.cpp index f631a1b8be..a45303d957 100644 --- a/plugins/PluginUpdater/src/Notifications.cpp +++ b/plugins/PluginUpdater/src/Notifications.cpp @@ -319,7 +319,7 @@ static void ApplyUpdates(void *param) SetStringText(hwndList, i, TranslateT("Downloading...")); FILEURL *pFileUrl = &todo[i].File; - if ( !DownloadFile(pFileUrl->tszDownloadURL, pFileUrl->tszDiskPath)) + if ( !DownloadFile(pFileUrl->tszDownloadURL, pFileUrl->tszDiskPath, pFileUrl->CRCsum)) SetStringText(hwndList, i, TranslateT("Failed!")); else SetStringText(hwndList, i, TranslateT("Succeeded.")); diff --git a/plugins/PluginUpdater/src/Scanner.cpp b/plugins/PluginUpdater/src/Scanner.cpp index 2c3e662f71..fe0a027b42 100644 --- a/plugins/PluginUpdater/src/Scanner.cpp +++ b/plugins/PluginUpdater/src/Scanner.cpp @@ -35,7 +35,7 @@ static bool Exists(LPCTSTR strName) struct ServListEntry { - ServListEntry(const char* _name, const char* _hash, DWORD _crc) : + ServListEntry(const char* _name, const char* _hash, int _crc) : m_name( mir_a2t(_name)), m_bNeedFree(true), m_crc(_crc) @@ -58,7 +58,7 @@ struct ServListEntry TCHAR *m_name; char m_szHash[32+1]; bool m_bNeedFree; - DWORD m_crc; + int m_crc; }; static int CompareHashes(const ServListEntry *p1, const ServListEntry *p2) @@ -187,6 +187,7 @@ static void ScanFolder(const TCHAR *tszFolder, size_t cbBaseLen, int level, cons bool bHasNewVersion = false; TCHAR *ptszUrl; + int MyCRC = 0; mir_sntprintf(tszBuf, SIZEOF(tszBuf), _T("%s\\%s"), tszFolder, ffd.cFileName); // this file is not marked for deletion @@ -211,6 +212,7 @@ static void ScanFolder(const TCHAR *tszFolder, size_t cbBaseLen, int level, cons char szMyHash[33]; CalculateModuleHash(tszBuf, szMyHash); + MyCRC = item->m_crc; bHasNewVersion = strcmp(szMyHash, item->m_szHash) != 0; } else { // file was marked for deletion, add it to the list anyway @@ -242,6 +244,7 @@ static void ScanFolder(const TCHAR *tszFolder, size_t cbBaseLen, int level, cons mir_sntprintf(FileInfo->File.tszDownloadURL, SIZEOF(FileInfo->File.tszDownloadURL), _T("%s/%s.zip"), tszBaseUrl, tszBuf); for (p = _tcschr(FileInfo->File.tszDownloadURL, '\\'); p != 0; p = _tcschr(p, '\\')) *p++ = '/'; + FileInfo->File.CRCsum = MyCRC; UpdateFiles->insert(FileInfo); } // end compare versions } @@ -292,7 +295,7 @@ static void CheckUpdates(void *) FILEURL pFileUrl; mir_sntprintf(pFileUrl.tszDownloadURL, SIZEOF(pFileUrl.tszDownloadURL), _T("%s/hashes.zip"), (TCHAR*)tszBaseUrl); mir_sntprintf(pFileUrl.tszDiskPath, SIZEOF(pFileUrl.tszDiskPath), _T("%s\\hashes.zip"), tszTempPath); - if (!DownloadFile(pFileUrl.tszDownloadURL, pFileUrl.tszDiskPath)) { + if (!DownloadFile(pFileUrl.tszDownloadURL, pFileUrl.tszDiskPath, 0)) { ShowPopup(0, LPGENT("Plugin Updater"), LPGENT("An error occured while downloading the update."), 1, 0); CheckThread = NULL; return; @@ -323,7 +326,7 @@ static void CheckUpdates(void *) _strlwr(p); - DWORD dwCrc32; + int dwCrc32; char *p1 = strchr(p, ' '); if (p1 == NULL) dwCrc32 = 0; diff --git a/plugins/PluginUpdater/src/Utils.cpp b/plugins/PluginUpdater/src/Utils.cpp index 1946babfca..38d46a6c75 100644 --- a/plugins/PluginUpdater/src/Utils.cpp +++ b/plugins/PluginUpdater/src/Utils.cpp @@ -122,7 +122,73 @@ void LoadOptions() opts.bUpdateIcons = DBGetContactSettingByte(NULL, MODNAME, "UpdateIcons", DEFAULT_UPDATEICONS); } -BOOL DownloadFile(LPCTSTR tszURL, LPCTSTR tszLocal) +ULONG crc32_table[256]; +ULONG ulPolynomial = 0x04c11db7; + +////////////////////////////////////////////////////////////// +// Reflection is a requirement for the official CRC-32 standard. +// You can create CRCs without it, but they won't conform to the standard. +////////////////////////////////////////////////////////////////////////// + +ULONG Reflect(ULONG ref, char ch) +{ + // Used only by Init_CRC32_Table() + ULONG value(0); + + // Swap bit 0 for bit 7 + // bit 1 for bit 6, etc. + for(int i = 1; i < (ch + 1); i++) + { + if(ref & 1) + value |= 1 << (ch - i); + ref >>= 1; + } + return value; +} + +void InitCrcTable() +{ + // 256 values representing ASCII character codes. + for(int i = 0; i <= 0xFF; i++) + { + crc32_table[i] = Reflect(i, 8) << 24; + for (int j = 0; j < 8; j++) + crc32_table[i] = (crc32_table[i] << 1) ^ (crc32_table[i] & (1 << 31) ? ulPolynomial : 0); + crc32_table[i] = Reflect(crc32_table[i], 32); + } +} + +int Get_CRC(unsigned char* buffer, ULONG bufsize) +{ + ULONG crc(0xffffffff); + int len; + len = bufsize; + // Save the text in the buffer. + + // Perform the algorithm on each character + // in the string, using the lookup table values. + + for(int i = 0; i < len; i++) + crc = (crc >> 8) ^ crc32_table[(crc & 0xFF) ^ buffer[i]]; + + // Exclusive OR the result with the beginning value. + return crc^0xffffffff; +} + +long FileSize(FILE *input) +{ + + long fileSizeBytes; + fseek(input, 0, SEEK_END); + fileSizeBytes = ftell(input); + fseek(input, 0, SEEK_SET); + + return fileSizeBytes; + + +} + +BOOL DownloadFile(LPCTSTR tszURL, LPCTSTR tszLocal, int CRCsum) { DWORD dwBytes; @@ -156,7 +222,13 @@ BOOL DownloadFile(LPCTSTR tszURL, LPCTSTR tszLocal) // write the downloaded file firectly WriteFile(hFile, pReply->pData, (DWORD)pReply->dataLength, &dwBytes, NULL); CloseHandle(hFile); - ret = true; + if (CRCsum) { + InitCrcTable(); + int crc = Get_CRC((unsigned char*)pReply->pData, (long)dwBytes); + if (crc == CRCsum) + ret = true; + } else + ret = true; } else { // try to write it via PU stub @@ -167,11 +239,17 @@ BOOL DownloadFile(LPCTSTR tszURL, LPCTSTR tszLocal) WriteFile(hFile, pReply->pData, (DWORD)pReply->dataLength, &dwBytes, NULL); CloseHandle(hFile); SafeMoveFile(tszTempFile, tszLocal); - ret = true; + if (CRCsum) { + InitCrcTable(); + int crc = Get_CRC((unsigned char*)pReply->pData, (long)dwBytes); + if (crc == CRCsum) + ret = true; + } else + ret = true; } } } - CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT,0,(LPARAM)pReply); + CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)pReply); } mir_free(szUrl); -- cgit v1.2.3