Mention the latest bug-fix.
[rsync.git] / checksum.c
blobec7ec13ce73691a2437c05f4f9a8cb32c3cb84df
1 /*
2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "rsync.h"
22 int csum_length=2; /* initial value */
24 #define CSUM_CHUNK 64
26 extern int checksum_seed;
27 extern int protocol_version;
30 a simple 32 bit checksum that can be upadted from either end
31 (inspired by Mark Adler's Adler-32 checksum)
33 uint32 get_checksum1(char *buf1, int32 len)
35 int32 i;
36 uint32 s1, s2;
37 schar *buf = (schar *)buf1;
39 s1 = s2 = 0;
40 for (i = 0; i < (len-4); i+=4) {
41 s2 += 4*(s1 + buf[i]) + 3*buf[i+1] + 2*buf[i+2] + buf[i+3] +
42 10*CHAR_OFFSET;
43 s1 += (buf[i+0] + buf[i+1] + buf[i+2] + buf[i+3] + 4*CHAR_OFFSET);
45 for (; i < len; i++) {
46 s1 += (buf[i]+CHAR_OFFSET); s2 += s1;
48 return (s1 & 0xffff) + (s2 << 16);
52 void get_checksum2(char *buf, int32 len, char *sum)
54 int32 i;
55 static char *buf1;
56 static int32 len1;
57 struct mdfour m;
59 if (len > len1) {
60 if (buf1)
61 free(buf1);
62 buf1 = new_array(char, len+4);
63 len1 = len;
64 if (!buf1)
65 out_of_memory("get_checksum2");
68 mdfour_begin(&m);
70 memcpy(buf1,buf,len);
71 if (checksum_seed) {
72 SIVAL(buf1,len,checksum_seed);
73 len += 4;
76 for(i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) {
77 mdfour_update(&m, (uchar *)(buf1+i), CSUM_CHUNK);
80 * Prior to version 27 an incorrect MD4 checksum was computed
81 * by failing to call mdfour_tail() for block sizes that
82 * are multiples of 64. This is fixed by calling mdfour_update()
83 * even when there are no more bytes.
85 if (len - i > 0 || protocol_version >= 27) {
86 mdfour_update(&m, (uchar *)(buf1+i), (len-i));
89 mdfour_result(&m, (uchar *)sum);
93 void file_checksum(char *fname,char *sum,OFF_T size)
95 OFF_T i;
96 struct map_struct *buf;
97 int fd;
98 OFF_T len = size;
99 struct mdfour m;
101 memset(sum,0,MD4_SUM_LENGTH);
103 fd = do_open(fname, O_RDONLY, 0);
104 if (fd == -1)
105 return;
107 buf = map_file(fd, size, MAX_MAP_SIZE, CSUM_CHUNK);
109 mdfour_begin(&m);
111 for(i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) {
112 mdfour_update(&m, (uchar *)map_ptr(buf, i, CSUM_CHUNK),
113 CSUM_CHUNK);
116 /* Prior to version 27 an incorrect MD4 checksum was computed
117 * by failing to call mdfour_tail() for block sizes that
118 * are multiples of 64. This is fixed by calling mdfour_update()
119 * even when there are no more bytes. */
120 if (len - i > 0 || protocol_version >= 27)
121 mdfour_update(&m, (uchar *)map_ptr(buf, i, len-i), len-i);
123 mdfour_result(&m, (uchar *)sum);
125 close(fd);
126 unmap_file(buf);
130 static int32 sumresidue;
131 static char sumrbuf[CSUM_CHUNK];
132 static struct mdfour md;
134 void sum_init(int seed)
136 char s[4];
137 mdfour_begin(&md);
138 sumresidue = 0;
139 SIVAL(s, 0, seed);
140 sum_update(s, 4);
144 * Feed data into an MD4 accumulator, md. The results may be
145 * retrieved using sum_end(). md is used for different purposes at
146 * different points during execution.
148 * @todo Perhaps get rid of md and just pass in the address each time.
149 * Very slightly clearer and slower.
151 void sum_update(char *p, int32 len)
153 if (len + sumresidue < CSUM_CHUNK) {
154 memcpy(sumrbuf + sumresidue, p, len);
155 sumresidue += len;
156 return;
159 if (sumresidue) {
160 int32 i = CSUM_CHUNK - sumresidue;
161 memcpy(sumrbuf + sumresidue, p, i);
162 mdfour_update(&md, (uchar *)sumrbuf, CSUM_CHUNK);
163 len -= i;
164 p += i;
167 while (len >= CSUM_CHUNK) {
168 mdfour_update(&md, (uchar *)p, CSUM_CHUNK);
169 len -= CSUM_CHUNK;
170 p += CSUM_CHUNK;
173 sumresidue = len;
174 if (sumresidue)
175 memcpy(sumrbuf, p, sumresidue);
178 void sum_end(char *sum)
180 if (sumresidue || protocol_version >= 27)
181 mdfour_update(&md, (uchar *)sumrbuf, sumresidue);
183 mdfour_result(&md, (uchar *)sum);