Debian Codegraph @

Contrib/VPatch/Source/GenPat/Checksums.cpp

raw
1 //---------------------------------------------------------------------------
2 // Checksums.cpp
3 //---------------------------------------------------------------------------
4 // -=* VPatch *=-
5 //---------------------------------------------------------------------------
6 // Copyright (C) 2001-2005 Koen van de Sande / Van de Sande Productions
7 //---------------------------------------------------------------------------
8 // Website: http://www.tibed.net/vpatch
9 //
10 // This software is provided 'as-is', without any express or implied
11 // warranty. In no event will the authors be held liable for any damages
12 // arising from the use of this software.
13 //
14 // Permission is granted to anyone to use this software for any purpose,
15 // including commercial applications, and to alter it and redistribute it
16 // freely, subject to the following restrictions:
17 //
18 // 1. The origin of this software must not be misrepresented; you must not
19 // claim that you wrote the original software. If you use this software
20 // in a product, an acknowledgment in the product documentation would be
21 // appreciated but is not required.
22 // 2. Altered source versions must be plainly marked as such, and must not be
23 // misrepresented as being the original software.
24 // 3. This notice may not be removed or altered from any source distribution.
25 //
26 // Unicode support by Jim Park -- 08/29/2007
27
28 #include "Checksums.h"
29
30 /* ------------------------ CRC32 checksum calculation ----------------- */
31
32 uint32_t CRCTable[256];
33 bool bInitCRC = false;
34
35 void InitCRC() {
36 int i, j; unsigned long c;
37 for (c = i = 0; i < 256; c = ++i) {
38 for (j = 0; j < 8; j++) {
39 if (c & 1) c = (c>>1) ^ 0xEDB88320;
40 else c >>= 1;
41 }
42 CRCTable[i] = c;
43 }
44 bInitCRC = true;
45 }
46
47 crc32_t streamCRC32(bistream& data) {
48 if(!bInitCRC) InitCRC();
49
50 const int CRCBLOCKSIZE = 16384;
51
52 uint8_t block[CRCBLOCKSIZE];
53 unsigned int read;
54 uint8_t *p;
55
56 crc32_t crc = 0xFFFFFFFF;
57 while(data.good()) {
58 data.read(reinterpret_cast<char*>(block), CRCBLOCKSIZE);
59 read = (unsigned int) data.gcount(); // The cast is safe because we never read more than CRCBLOCKSIZE
60 for (p = block; p < block + read; p++)
61 crc = CRCTable[(crc & 0xFF) ^ *p] ^ (crc >> 8);
62 }
63 crc = (crc ^ 0xFFFFFFFF);
64 return crc;
65 }
66
67 /* ------------------------ MD5 checksum calculation ----------------- */
68
69 void streamMD5(bistream& data, md5_byte_t digest[16]) {
70 const int MD5BLOCKSIZE = 16384;
71 uint8_t md5block[MD5BLOCKSIZE];
72 unsigned int read;
73
74 md5_state_t state;
75
76 md5_init(&state);
77
78 while(data.good()) {
79 data.read(reinterpret_cast<char*>(md5block), MD5BLOCKSIZE);
80 read = (unsigned int) data.gcount(); // The cast is safe because we never read more than MD5BLOCKSIZE
81 md5_append(&state, md5block, read);
82 }
83
84 md5_finish(&state, digest);
85 }
86
87 // Jim Park: string -> tstring.
88 TChecksum::TChecksum(tstring& fileName) : mode(MD5) {
89 bifstream data;
90 data.open(fileName.c_str(), ios::binary | ios::in);
91 data.seekg(0, ios::beg);
92 crc = streamCRC32(data);
93 data.close();
94
95 bifstream data2;
96 data2.open(fileName.c_str(), ios::binary | ios::in);
97 data2.seekg(0, ios::beg);
98 streamMD5(data2, digest);
99 data2.close();
100 }
101
102 void TChecksum::loadMD5(md5_byte_t newdigest[16]) {
103 mode = MD5;
104 for(int i = 0; i < 16; i++) {
105 digest[i] = newdigest[i];
106 }
107 }
108 void TChecksum::loadCRC32(crc32_t newcrc) {
109 mode = CRC32;
110 crc = newcrc;
111 }
112 bool TChecksum::operator==(const TChecksum& b) {
113 if(mode != b.mode) throw "Checksums in different mode: MD5/CRC32";
114 if(mode == MD5) {
115 for(int md5index = 0; md5index < 16; md5index++) {
116 if(digest[md5index] != b.digest[md5index]) break;
117 if(md5index == 15) return true;
118 }
119 return false;
120 } else {
121 return (crc == b.crc);
122 }
123 }