Make use of the SHORT_SUM_LENGTH define.
[rsync/rsync.git] / checksum.c
CommitLineData
0d7638ea 1/*
0f78b815
WD
2 * Routines to support checksumming of bytes.
3 *
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2004, 2005 Wayne Davison
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
e7c67065
WD
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
0f78b815 21 */
c627d613
AT
22
23#include "rsync.h"
24
794a0b6c 25int csum_length = SHORT_SUM_LENGTH; /* initial value */
34ccb63e
AT
26
27#define CSUM_CHUNK 64
28
8ee6eb71 29extern int checksum_seed;
d04e9c51 30extern int protocol_version;
c627d613
AT
31
32/*
33 a simple 32 bit checksum that can be upadted from either end
34 (inspired by Mark Adler's Adler-32 checksum)
35 */
deb5bf1d 36uint32 get_checksum1(char *buf1, int32 len)
c627d613 37{
deb5bf1d 38 int32 i;
c627d613 39 uint32 s1, s2;
debb4505 40 schar *buf = (schar *)buf1;
c627d613
AT
41
42 s1 = s2 = 0;
34ccb63e 43 for (i = 0; i < (len-4); i+=4) {
0d7638ea 44 s2 += 4*(s1 + buf[i]) + 3*buf[i+1] + 2*buf[i+2] + buf[i+3] +
1b01b295 45 10*CHAR_OFFSET;
0d7638ea 46 s1 += (buf[i+0] + buf[i+1] + buf[i+2] + buf[i+3] + 4*CHAR_OFFSET);
34ccb63e
AT
47 }
48 for (; i < len; i++) {
1b01b295 49 s1 += (buf[i]+CHAR_OFFSET); s2 += s1;
c627d613
AT
50 }
51 return (s1 & 0xffff) + (s2 << 16);
52}
53
54
deb5bf1d 55void get_checksum2(char *buf, int32 len, char *sum)
c627d613 56{
deb5bf1d 57 int32 i;
8de330a3 58 static char *buf1;
deb5bf1d 59 static int32 len1;
8de330a3
AT
60 struct mdfour m;
61
62 if (len > len1) {
deb5bf1d
WD
63 if (buf1)
64 free(buf1);
58cadc86 65 buf1 = new_array(char, len+4);
8de330a3 66 len1 = len;
deb5bf1d
WD
67 if (!buf1)
68 out_of_memory("get_checksum2");
8de330a3 69 }
0d7638ea 70
8de330a3 71 mdfour_begin(&m);
0d7638ea 72
8de330a3
AT
73 memcpy(buf1,buf,len);
74 if (checksum_seed) {
75 SIVAL(buf1,len,checksum_seed);
76 len += 4;
77 }
0d7638ea 78
8de330a3 79 for(i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) {
7ae359c3 80 mdfour_update(&m, (uchar *)(buf1+i), CSUM_CHUNK);
8de330a3 81 }
fc1ae658
S
82 /*
83 * Prior to version 27 an incorrect MD4 checksum was computed
84 * by failing to call mdfour_tail() for block sizes that
85 * are multiples of 64. This is fixed by calling mdfour_update()
86 * even when there are no more bytes.
87 */
d04e9c51 88 if (len - i > 0 || protocol_version >= 27) {
7ae359c3 89 mdfour_update(&m, (uchar *)(buf1+i), (len-i));
8de330a3 90 }
0d7638ea 91
7ae359c3 92 mdfour_result(&m, (uchar *)sum);
c627d613
AT
93}
94
34ccb63e 95
bcacc18b 96void file_checksum(char *fname,char *sum,OFF_T size)
c627d613 97{
8de330a3 98 struct map_struct *buf;
26404276 99 OFF_T i, len = size;
8de330a3 100 struct mdfour m;
26404276
WD
101 int32 remainder;
102 int fd;
0d7638ea 103
f855a7d0 104 memset(sum,0,MD4_SUM_LENGTH);
0d7638ea 105
8c9fd200 106 fd = do_open(fname, O_RDONLY, 0);
efa95a18
WD
107 if (fd == -1)
108 return;
0d7638ea 109
bd1a581b 110 buf = map_file(fd, size, MAX_MAP_SIZE, CSUM_CHUNK);
0d7638ea 111
8de330a3
AT
112 mdfour_begin(&m);
113
114 for(i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) {
afd8bdb9
WD
115 mdfour_update(&m, (uchar *)map_ptr(buf, i, CSUM_CHUNK),
116 CSUM_CHUNK);
8de330a3
AT
117 }
118
afd8bdb9 119 /* Prior to version 27 an incorrect MD4 checksum was computed
fc1ae658
S
120 * by failing to call mdfour_tail() for block sizes that
121 * are multiples of 64. This is fixed by calling mdfour_update()
afd8bdb9 122 * even when there are no more bytes. */
26404276
WD
123 remainder = (int32)(len - i);
124 if (remainder > 0 || protocol_version >= 27)
125 mdfour_update(&m, (uchar *)map_ptr(buf, i, remainder), remainder);
8de330a3 126
7ae359c3 127 mdfour_result(&m, (uchar *)sum);
8de330a3
AT
128
129 close(fd);
130 unmap_file(buf);
c627d613 131}
34ccb63e
AT
132
133
deb5bf1d 134static int32 sumresidue;
9e31c482 135static char sumrbuf[CSUM_CHUNK];
8de330a3 136static struct mdfour md;
9e31c482 137
ba582f75 138void sum_init(int seed)
9e31c482 139{
8de330a3
AT
140 char s[4];
141 mdfour_begin(&md);
ba582f75
WD
142 sumresidue = 0;
143 SIVAL(s, 0, seed);
144 sum_update(s, 4);
9e31c482
AT
145}
146
d37d8d7b
MP
147/**
148 * Feed data into an MD4 accumulator, md. The results may be
149 * retrieved using sum_end(). md is used for different purposes at
150 * different points during execution.
151 *
152 * @todo Perhaps get rid of md and just pass in the address each time.
153 * Very slightly clearer and slower.
154 **/
4a19c3b2 155void sum_update(const char *p, int32 len)
9e31c482 156{
8de330a3 157 if (len + sumresidue < CSUM_CHUNK) {
deb5bf1d 158 memcpy(sumrbuf + sumresidue, p, len);
8de330a3
AT
159 sumresidue += len;
160 return;
161 }
162
163 if (sumresidue) {
deb5bf1d
WD
164 int32 i = CSUM_CHUNK - sumresidue;
165 memcpy(sumrbuf + sumresidue, p, i);
166 mdfour_update(&md, (uchar *)sumrbuf, CSUM_CHUNK);
8de330a3
AT
167 len -= i;
168 p += i;
169 }
170
deb5bf1d
WD
171 while (len >= CSUM_CHUNK) {
172 mdfour_update(&md, (uchar *)p, CSUM_CHUNK);
173 len -= CSUM_CHUNK;
174 p += CSUM_CHUNK;
8de330a3
AT
175 }
176
deb5bf1d
WD
177 sumresidue = len;
178 if (sumresidue)
179 memcpy(sumrbuf, p, sumresidue);
9e31c482
AT
180}
181
182void sum_end(char *sum)
183{
deb5bf1d 184 if (sumresidue || protocol_version >= 27)
7ae359c3 185 mdfour_update(&md, (uchar *)sumrbuf, sumresidue);
9e31c482 186
7ae359c3 187 mdfour_result(&md, (uchar *)sum);
9e31c482 188}