Changed some names since "depth" wasn't really the right term.
[rsync.git] / checksum.c
blob7484ca58d8b7352998ca982da000296109f1cd6c
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 int checksum_seed = 0;
27 extern int remote_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,int len)
35 int 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,int len,char *sum)
54 int i;
55 static char *buf1;
56 static int len1;
57 struct mdfour m;
59 if (len > len1) {
60 if (buf1) free(buf1);
61 buf1 = (char *)malloc(len+4);
62 len1 = len;
63 if (!buf1) out_of_memory("get_checksum2");
66 mdfour_begin(&m);
68 memcpy(buf1,buf,len);
69 if (checksum_seed) {
70 SIVAL(buf1,len,checksum_seed);
71 len += 4;
74 for(i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) {
75 mdfour_update(&m, (uchar *)(buf1+i), CSUM_CHUNK);
78 * Prior to version 27 an incorrect MD4 checksum was computed
79 * by failing to call mdfour_tail() for block sizes that
80 * are multiples of 64. This is fixed by calling mdfour_update()
81 * even when there are no more bytes.
83 if (len - i > 0 || remote_version >= 27) {
84 mdfour_update(&m, (uchar *)(buf1+i), (len-i));
87 mdfour_result(&m, (uchar *)sum);
91 void file_checksum(char *fname,char *sum,OFF_T size)
93 OFF_T i;
94 struct map_struct *buf;
95 int fd;
96 OFF_T len = size;
97 char tmpchunk[CSUM_CHUNK];
98 struct mdfour m;
100 memset(sum,0,MD4_SUM_LENGTH);
102 fd = do_open(fname, O_RDONLY, 0);
103 if (fd == -1) return;
105 buf = map_file(fd,size);
107 mdfour_begin(&m);
109 for(i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) {
110 memcpy(tmpchunk, map_ptr(buf,i,CSUM_CHUNK), CSUM_CHUNK);
111 mdfour_update(&m, (uchar *)tmpchunk, CSUM_CHUNK);
115 * Prior to version 27 an incorrect MD4 checksum was computed
116 * by failing to call mdfour_tail() for block sizes that
117 * are multiples of 64. This is fixed by calling mdfour_update()
118 * even when there are no more bytes.
120 if (len - i > 0) {
121 memcpy(tmpchunk, map_ptr(buf,i,len-i), len-i);
123 if (len - i > 0 || remote_version >= 27) {
124 mdfour_update(&m, (uchar *)tmpchunk, (len-i));
127 mdfour_result(&m, (uchar *)sum);
129 close(fd);
130 unmap_file(buf);
134 static int sumresidue;
135 static char sumrbuf[CSUM_CHUNK];
136 static struct mdfour md;
138 void sum_init(void)
140 char s[4];
141 mdfour_begin(&md);
142 sumresidue=0;
143 SIVAL(s,0,checksum_seed);
144 sum_update(s,4);
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.
152 * @todo Perhaps get rid of md and just pass in the address each time.
153 * Very slightly clearer and slower.
155 void sum_update(char *p, int len)
157 int i;
158 if (len + sumresidue < CSUM_CHUNK) {
159 memcpy(sumrbuf+sumresidue, p, len);
160 sumresidue += len;
161 return;
164 if (sumresidue) {
165 i = MIN(CSUM_CHUNK-sumresidue,len);
166 memcpy(sumrbuf+sumresidue,p,i);
167 mdfour_update(&md, (uchar *)sumrbuf, (i+sumresidue));
168 len -= i;
169 p += i;
172 for(i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) {
173 memcpy(sumrbuf,p+i,CSUM_CHUNK);
174 mdfour_update(&md, (uchar *)sumrbuf, CSUM_CHUNK);
177 if (len - i > 0) {
178 sumresidue = len-i;
179 memcpy(sumrbuf,p+i,sumresidue);
180 } else {
181 sumresidue = 0;
185 void sum_end(char *sum)
187 if (sumresidue || remote_version >= 27) {
188 mdfour_update(&md, (uchar *)sumrbuf, sumresidue);
191 mdfour_result(&md, (uchar *)sum);