*** empty log message ***
[rsync/rsync.git] / checksum.c
CommitLineData
c627d613
AT
1/*
2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
4
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.
9
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.
14
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.
18*/
19
20#include "rsync.h"
21
34ccb63e
AT
22int csum_length=SUM_LENGTH;
23
24#define CSUM_CHUNK 64
25
aae43eb3 26int checksum_seed = 0;
b98c7b81 27extern int remote_version;
c627d613
AT
28
29/*
30 a simple 32 bit checksum that can be upadted from either end
31 (inspired by Mark Adler's Adler-32 checksum)
32 */
33uint32 get_checksum1(char *buf,int len)
34{
35 int i;
36 uint32 s1, s2;
37
38 s1 = s2 = 0;
34ccb63e
AT
39 for (i = 0; i < (len-4); i+=4) {
40 s2 += 4*(s1 + buf[i]) + 3*buf[i+1] + 2*buf[i+2] + buf[i+3];
41 s1 += (buf[i+0] + buf[i+1] + buf[i+2] + buf[i+3]);
42 }
43 for (; i < len; i++) {
44 s1 += buf[i]; s2 += s1;
c627d613
AT
45 }
46 return (s1 & 0xffff) + (s2 << 16);
47}
48
49
34ccb63e
AT
50static void sum_put(MDstruct *md,char *sum)
51{
52 SIVAL(sum,0,md->buffer[0]);
53 if (csum_length <= 4) return;
54 SIVAL(sum,4,md->buffer[1]);
55 if (csum_length <= 8) return;
56 SIVAL(sum,8,md->buffer[2]);
57 if (csum_length <= 12) return;
58 SIVAL(sum,12,md->buffer[3]);
59}
60
61
c627d613
AT
62void get_checksum2(char *buf,int len,char *sum)
63{
c627d613
AT
64 int i;
65 MDstruct MD;
aae43eb3
AT
66 static char *buf1 = NULL;
67 static int len1 = 0;
68
69 if (len > len1) {
70 if (buf1) free(buf1);
57e877a1 71 buf1 = (char *)malloc(len+4);
aae43eb3
AT
72 len1 = len;
73 if (!buf1) out_of_memory("get_checksum2");
74 }
c627d613
AT
75
76 MDbegin(&MD);
34ccb63e 77
aae43eb3
AT
78 bcopy(buf,buf1,len);
79 if (checksum_seed) {
57e877a1
AT
80 SIVAL(buf1,len,checksum_seed);
81 len += 4;
c627d613 82 }
34ccb63e 83
aae43eb3
AT
84 for(i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) {
85 MDupdate(&MD, buf1+i, CSUM_CHUNK*8);
86 }
9e31c482
AT
87 if (len - i > 0)
88 MDupdate(&MD, buf1+i, (len-i)*8);
34ccb63e
AT
89
90 sum_put(&MD,sum);
c627d613
AT
91}
92
34ccb63e 93
c627d613
AT
94void file_checksum(char *fname,char *sum,off_t size)
95{
34ccb63e
AT
96 int i;
97 MDstruct MD;
c6e7fcb4 98 struct map_struct *buf;
c627d613 99 int fd;
34ccb63e 100 int len = size;
aae43eb3 101 char tmpchunk[CSUM_CHUNK];
34ccb63e 102
43a481dc 103 bzero(sum,csum_length);
c627d613
AT
104
105 fd = open(fname,O_RDONLY);
106 if (fd == -1) return;
107
108 buf = map_file(fd,size);
34ccb63e
AT
109
110 MDbegin(&MD);
111
112 for(i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) {
113 bcopy(map_ptr(buf,i,CSUM_CHUNK),tmpchunk,CSUM_CHUNK);
114 MDupdate(&MD, tmpchunk, CSUM_CHUNK*8);
c627d613
AT
115 }
116
9e31c482
AT
117 if (len - i > 0) {
118 bcopy(map_ptr(buf,i,len-i),tmpchunk,len-i);
119 MDupdate(&MD, tmpchunk, (len-i)*8);
120 }
34ccb63e
AT
121
122 sum_put(&MD,sum);
123
c627d613 124 close(fd);
c6e7fcb4 125 unmap_file(buf);
c627d613 126}
34ccb63e
AT
127
128
129void checksum_init(void)
130{
b98c7b81
AT
131 if (remote_version >= 14)
132 csum_length = 2; /* adaptive */
133 else
134 csum_length = SUM_LENGTH;
34ccb63e
AT
135}
136
137
9e31c482
AT
138
139static MDstruct sumMD;
140static int sumresidue;
141static char sumrbuf[CSUM_CHUNK];
142
143void sum_init(void)
144{
73545f2c
AT
145 char s[4];
146 MDbegin(&sumMD);
9e31c482 147 sumresidue=0;
73545f2c
AT
148 SIVAL(s,0,checksum_seed);
149 sum_update(s,4);
9e31c482
AT
150}
151
152void sum_update(char *p,int len)
153{
154 int i;
f94e821c
AT
155 if (len + sumresidue < CSUM_CHUNK) {
156 bcopy(p,sumrbuf+sumresidue,len);
157 sumresidue += len;
158 return;
159 }
160
9e31c482
AT
161 if (sumresidue) {
162 i = MIN(CSUM_CHUNK-sumresidue,len);
163 bcopy(p,sumrbuf+sumresidue,i);
164 MDupdate(&sumMD, sumrbuf, (i+sumresidue)*8);
165 len -= i;
166 p += i;
167 }
168
169 for(i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) {
170 bcopy(p+i,sumrbuf,CSUM_CHUNK);
171 MDupdate(&sumMD, sumrbuf, CSUM_CHUNK*8);
172 }
173
174 if (len - i > 0) {
175 sumresidue = len-i;
176 bcopy(p+i,sumrbuf,sumresidue);
177 } else {
178 sumresidue = 0;
179 }
180}
181
182void sum_end(char *sum)
183{
184 if (sumresidue)
185 MDupdate(&sumMD, sumrbuf, sumresidue*8);
186
187 SIVAL(sum,0,sumMD.buffer[0]);
188 SIVAL(sum,4,sumMD.buffer[1]);
189 SIVAL(sum,8,sumMD.buffer[2]);
190 SIVAL(sum,12,sumMD.buffer[3]);
191}
192
193