Debian Codegraph @

Contrib/VPatch/Source/Plugin/checksum.c

raw
1 //---------------------------------------------------------------------------
2 // checksum.c
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 // Reviewed for Unicode support by Jim Park -- 08/29/2007
27
28 #include "checksum.h"
29
30 #ifdef _MSC_VER
31 # define FORCE_INLINE __forceinline
32 #else
33 # ifdef __GNUC__
34 # if __GNUC__ < 3
35 # define FORCE_INLINE inline
36 # else
37 # define FORCE_INLINE inline __attribute__ ((always_inline))
38 # endif
39 # else
40 # define FORCE_INLINE inline
41 # endif
42 #endif
43
44 /* ------------------------ CRC32 checksum calculation ----------------- */
45
46 UINT CRCTable[256];
47 BOOL bInitCRC = FALSE;
48
49 FORCE_INLINE void InitCRC() {
50 int i, j; unsigned long c;
51 for (c = i = 0; i < 256; c = ++i) {
52 for (j = 0; j < 8; j++) {
53 if (c & 1) c = (c>>1) ^ 0xEDB88320;
54 else c >>= 1;
55 }
56 CRCTable[i] = c;
57 }
58 bInitCRC = TRUE;
59 }
60
61 #define CRCBLOCKSIZE 4096
62
63 BOOL FileCRC(HANDLE hFile, DWORD *crc) {
64 static BYTE crcblock[CRCBLOCKSIZE];
65 DWORD read;
66 BYTE *p;
67
68 UINT c = 0xFFFFFFFF;
69 if (bInitCRC == FALSE)
70 InitCRC();
71
72 SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
73 do {
74 if (ReadFile(hFile, crcblock, CRCBLOCKSIZE, &read, NULL) == FALSE)
75 return FALSE;
76 for (p = crcblock; p < crcblock + read; p++)
77 c = CRCTable[(c & 0xFF) ^ *p] ^ (c >> 8);
78 } while (read);
79
80 *crc = (c ^ 0xFFFFFFFF);
81
82 return TRUE;
83 }
84
85 void CRC32ToString(char* string, DWORD crc) {
86 int i = 0;
87 int j = 7;
88 int a1, a2;
89 for(i = 0; i < 4; i++) {
90 a1 = (crc >> 4) % 16;
91 a2 = crc % 16;
92 string[j--] = a2 < 10 ? ('0' + a2) : ('A' + a2 - 10);
93 string[j--] = a1 < 10 ? ('0' + a1) : ('A' + a1 - 10);
94 crc = crc >> 8;
95 }
96 }
97
98 /* ------------------------ MD5 checksum calculation ----------------- */
99
100 #define MD5BLOCKSIZE 16384
101
102 BOOL FileMD5(HANDLE hFile, md5_byte_t digest[16]) {
103 static BYTE md5block[MD5BLOCKSIZE];
104 DWORD read;
105
106 md5_state_t state;
107
108 md5_init(&state);
109
110 SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
111 do {
112 if (ReadFile(hFile, md5block, MD5BLOCKSIZE, &read, NULL) == FALSE)
113 return FALSE;
114 md5_append(&state, md5block, read);
115 } while (read);
116
117 md5_finish(&state, digest);
118 return TRUE;
119 }
120
121 void MD5ToString(char* string, md5_byte_t checksum[16]) {
122 int i = 0;
123 int j = 0;
124 int a1, a2;
125 for(i = 0; i < 16; i++) {
126 a1 = (checksum[i] >> 4) % 16;
127 a2 = checksum[i] % 16;
128 string[j++] = a1 < 10 ? ('0' + a1) : ('A' + a1 - 10);
129 string[j++] = a2 < 10 ? ('0' + a2) : ('A' + a2 - 10);
130 }
131 }