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