Fixed the backing up of a device or socket.
[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
d3d07a5e 6 * Copyright (C) 2004-2008 Wayne Davison
0f78b815
WD
7 *
8 * This program is free software; you can redistribute it and/or modify
8e41b68e
WD
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
0f78b815
WD
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 18 * You should have received a copy of the GNU General Public License along
4fd842f9 19 * with this program; if not, visit the http://fsf.org website.
0f78b815 20 */
c627d613
AT
21
22#include "rsync.h"
23
8ee6eb71 24extern int checksum_seed;
d04e9c51 25extern int protocol_version;
c627d613 26
a0456b9c
WD
27int csum_length = SHORT_SUM_LENGTH; /* initial value */
28
c627d613
AT
29/*
30 a simple 32 bit checksum that can be upadted from either end
31 (inspired by Mark Adler's Adler-32 checksum)
32 */
deb5bf1d 33uint32 get_checksum1(char *buf1, int32 len)
c627d613 34{
deb5bf1d 35 int32 i;
c627d613 36 uint32 s1, s2;
debb4505 37 schar *buf = (schar *)buf1;
c627d613
AT
38
39 s1 = s2 = 0;
34ccb63e 40 for (i = 0; i < (len-4); i+=4) {
0d7638ea 41 s2 += 4*(s1 + buf[i]) + 3*buf[i+1] + 2*buf[i+2] + buf[i+3] +
1b01b295 42 10*CHAR_OFFSET;
0d7638ea 43 s1 += (buf[i+0] + buf[i+1] + buf[i+2] + buf[i+3] + 4*CHAR_OFFSET);
34ccb63e
AT
44 }
45 for (; i < len; i++) {
1b01b295 46 s1 += (buf[i]+CHAR_OFFSET); s2 += s1;
c627d613
AT
47 }
48 return (s1 & 0xffff) + (s2 << 16);
49}
50
51
deb5bf1d 52void get_checksum2(char *buf, int32 len, char *sum)
c627d613 53{
a0456b9c
WD
54 md_context m;
55
56 if (protocol_version >= 30) {
57 uchar seedbuf[4];
58 md5_begin(&m);
161fba69 59 md5_update(&m, (uchar *)buf, len);
a0456b9c
WD
60 if (checksum_seed) {
61 SIVAL(seedbuf, 0, checksum_seed);
62 md5_update(&m, seedbuf, 4);
63 }
a0456b9c
WD
64 md5_result(&m, (uchar *)sum);
65 } else {
66 int32 i;
67 static char *buf1;
68 static int32 len1;
69
70 mdfour_begin(&m);
71
72 if (len > len1) {
73 if (buf1)
74 free(buf1);
75 buf1 = new_array(char, len+4);
76 len1 = len;
77 if (!buf1)
78 out_of_memory("get_checksum2");
79 }
80
81 memcpy(buf1, buf, len);
82 if (checksum_seed) {
83 SIVAL(buf1,len,checksum_seed);
84 len += 4;
85 }
86
87 for (i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK)
88 mdfour_update(&m, (uchar *)(buf1+i), CSUM_CHUNK);
89
90 /*
91 * Prior to version 27 an incorrect MD4 checksum was computed
92 * by failing to call mdfour_tail() for block sizes that
93 * are multiples of 64. This is fixed by calling mdfour_update()
94 * even when there are no more bytes.
95 */
96 if (len - i > 0 || protocol_version >= 27)
97 mdfour_update(&m, (uchar *)(buf1+i), len-i);
98
99 mdfour_result(&m, (uchar *)sum);
8de330a3 100 }
c627d613
AT
101}
102
a0456b9c 103void file_checksum(char *fname, char *sum, OFF_T size)
c627d613 104{
8de330a3 105 struct map_struct *buf;
26404276 106 OFF_T i, len = size;
a0456b9c 107 md_context m;
26404276
WD
108 int32 remainder;
109 int fd;
0d7638ea 110
a0456b9c 111 memset(sum, 0, MAX_DIGEST_LEN);
0d7638ea 112
8c9fd200 113 fd = do_open(fname, O_RDONLY, 0);
efa95a18
WD
114 if (fd == -1)
115 return;
0d7638ea 116
bd1a581b 117 buf = map_file(fd, size, MAX_MAP_SIZE, CSUM_CHUNK);
0d7638ea 118
a0456b9c
WD
119 if (protocol_version >= 30) {
120 md5_begin(&m);
8de330a3 121
a0456b9c
WD
122 for (i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) {
123 md5_update(&m, (uchar *)map_ptr(buf, i, CSUM_CHUNK),
124 CSUM_CHUNK);
125 }
8de330a3 126
a0456b9c
WD
127 remainder = (int32)(len - i);
128 if (remainder > 0)
129 md5_update(&m, (uchar *)map_ptr(buf, i, remainder), remainder);
8de330a3 130
a0456b9c
WD
131 md5_result(&m, (uchar *)sum);
132 } else {
133 mdfour_begin(&m);
134
135 for (i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) {
136 mdfour_update(&m, (uchar *)map_ptr(buf, i, CSUM_CHUNK),
137 CSUM_CHUNK);
138 }
139
140 /* Prior to version 27 an incorrect MD4 checksum was computed
141 * by failing to call mdfour_tail() for block sizes that
142 * are multiples of 64. This is fixed by calling mdfour_update()
143 * even when there are no more bytes. */
144 remainder = (int32)(len - i);
145 if (remainder > 0 || protocol_version >= 27)
146 mdfour_update(&m, (uchar *)map_ptr(buf, i, remainder), remainder);
147
148 mdfour_result(&m, (uchar *)sum);
149 }
8de330a3
AT
150
151 close(fd);
152 unmap_file(buf);
c627d613 153}
34ccb63e 154
deb5bf1d 155static int32 sumresidue;
a0456b9c 156static md_context md;
9e31c482 157
ba582f75 158void sum_init(int seed)
9e31c482 159{
8de330a3 160 char s[4];
a0456b9c
WD
161
162 if (protocol_version >= 30)
163 md5_begin(&md);
164 else {
165 mdfour_begin(&md);
166 sumresidue = 0;
9889a34b
WD
167 SIVAL(s, 0, seed);
168 sum_update(s, 4);
a0456b9c 169 }
9e31c482
AT
170}
171
d37d8d7b
MP
172/**
173 * Feed data into an MD4 accumulator, md. The results may be
174 * retrieved using sum_end(). md is used for different purposes at
175 * different points during execution.
176 *
177 * @todo Perhaps get rid of md and just pass in the address each time.
178 * Very slightly clearer and slower.
179 **/
4a19c3b2 180void sum_update(const char *p, int32 len)
9e31c482 181{
a0456b9c
WD
182 if (protocol_version >= 30) {
183 md5_update(&md, (uchar *)p, len);
184 return;
185 }
186
8de330a3 187 if (len + sumresidue < CSUM_CHUNK) {
a0456b9c 188 memcpy(md.buffer + sumresidue, p, len);
8de330a3
AT
189 sumresidue += len;
190 return;
191 }
192
193 if (sumresidue) {
deb5bf1d 194 int32 i = CSUM_CHUNK - sumresidue;
a0456b9c
WD
195 memcpy(md.buffer + sumresidue, p, i);
196 mdfour_update(&md, (uchar *)md.buffer, CSUM_CHUNK);
8de330a3
AT
197 len -= i;
198 p += i;
199 }
200
deb5bf1d
WD
201 while (len >= CSUM_CHUNK) {
202 mdfour_update(&md, (uchar *)p, CSUM_CHUNK);
203 len -= CSUM_CHUNK;
204 p += CSUM_CHUNK;
8de330a3
AT
205 }
206
deb5bf1d
WD
207 sumresidue = len;
208 if (sumresidue)
a0456b9c 209 memcpy(md.buffer, p, sumresidue);
9e31c482
AT
210}
211
a0456b9c 212int sum_end(char *sum)
9e31c482 213{
a0456b9c
WD
214 if (protocol_version >= 30) {
215 md5_result(&md, (uchar *)sum);
216 return MD5_DIGEST_LEN;
217 }
218
deb5bf1d 219 if (sumresidue || protocol_version >= 27)
a0456b9c 220 mdfour_update(&md, (uchar *)md.buffer, sumresidue);
9e31c482 221
7ae359c3 222 mdfour_result(&md, (uchar *)sum);
a0456b9c
WD
223
224 return MD4_DIGEST_LEN;
9e31c482 225}