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/Utils.cpp | 86 +++++++++++++++++++++++++++++++++++-- 1 file changed, 82 insertions(+), 4 deletions(-) (limited to 'plugins/PluginUpdater/src/Utils.cpp') 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