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