summaryrefslogtreecommitdiff
path: root/plugins/PluginUpdater/src/Utils.cpp
diff options
context:
space:
mode:
authorKirill Volinsky <mataes2007@gmail.com>2013-02-11 15:47:08 +0000
committerKirill Volinsky <mataes2007@gmail.com>2013-02-11 15:47:08 +0000
commit6023520b8150f43f68c9a3454b4afecd4ea2ac1a (patch)
tree8f3266dc9a329a52f0afcaf3bd3c71a4bf9f25c2 /plugins/PluginUpdater/src/Utils.cpp
parent3035e706cbac5a03e68617be500e14e2646b966f (diff)
added checking crc sum
git-svn-id: http://svn.miranda-ng.org/main/trunk@3555 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'plugins/PluginUpdater/src/Utils.cpp')
-rw-r--r--plugins/PluginUpdater/src/Utils.cpp86
1 files changed, 82 insertions, 4 deletions
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);