configuration parsing and loading code for rsyncd. This is based
[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
ebb0a6f6 22int csum_length=2; /* initial value */
34ccb63e
AT
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 */
0aeee8c1 33uint32 get_checksum1(char *buf1,int len)
c627d613
AT
34{
35 int i;
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) {
1b01b295
AT
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);
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
34ccb63e
AT
52static void sum_put(MDstruct *md,char *sum)
53{
54 SIVAL(sum,0,md->buffer[0]);
55 if (csum_length <= 4) return;
56 SIVAL(sum,4,md->buffer[1]);
57 if (csum_length <= 8) return;
58 SIVAL(sum,8,md->buffer[2]);
59 if (csum_length <= 12) return;
60 SIVAL(sum,12,md->buffer[3]);
61}
62
63
c627d613
AT
64void get_checksum2(char *buf,int len,char *sum)
65{
c627d613
AT
66 int i;
67 MDstruct MD;
3a6a366f
AT
68 static char *buf1;
69 static int len1;
aae43eb3
AT
70
71 if (len > len1) {
72 if (buf1) free(buf1);
57e877a1 73 buf1 = (char *)malloc(len+4);
aae43eb3
AT
74 len1 = len;
75 if (!buf1) out_of_memory("get_checksum2");
76 }
c627d613
AT
77
78 MDbegin(&MD);
34ccb63e 79
aae43eb3
AT
80 bcopy(buf,buf1,len);
81 if (checksum_seed) {
57e877a1
AT
82 SIVAL(buf1,len,checksum_seed);
83 len += 4;
c627d613 84 }
34ccb63e 85
aae43eb3
AT
86 for(i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) {
87 MDupdate(&MD, buf1+i, CSUM_CHUNK*8);
88 }
9e31c482
AT
89 if (len - i > 0)
90 MDupdate(&MD, buf1+i, (len-i)*8);
34ccb63e
AT
91
92 sum_put(&MD,sum);
c627d613
AT
93}
94
34ccb63e 95
bcacc18b 96void file_checksum(char *fname,char *sum,OFF_T size)
c627d613 97{
bcacc18b 98 OFF_T i;
34ccb63e 99 MDstruct MD;
c6e7fcb4 100 struct map_struct *buf;
c627d613 101 int fd;
bcacc18b 102 OFF_T len = size;
aae43eb3 103 char tmpchunk[CSUM_CHUNK];
34ccb63e 104
43a481dc 105 bzero(sum,csum_length);
c627d613
AT
106
107 fd = open(fname,O_RDONLY);
108 if (fd == -1) return;
109
110 buf = map_file(fd,size);
34ccb63e
AT
111
112 MDbegin(&MD);
113
114 for(i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) {
115 bcopy(map_ptr(buf,i,CSUM_CHUNK),tmpchunk,CSUM_CHUNK);
116 MDupdate(&MD, tmpchunk, CSUM_CHUNK*8);
c627d613
AT
117 }
118
9e31c482
AT
119 if (len - i > 0) {
120 bcopy(map_ptr(buf,i,len-i),tmpchunk,len-i);
121 MDupdate(&MD, tmpchunk, (len-i)*8);
122 }
34ccb63e
AT
123
124 sum_put(&MD,sum);
125
c627d613 126 close(fd);
c6e7fcb4 127 unmap_file(buf);
c627d613 128}
34ccb63e
AT
129
130
131void checksum_init(void)
132{
b98c7b81
AT
133 if (remote_version >= 14)
134 csum_length = 2; /* adaptive */
135 else
136 csum_length = SUM_LENGTH;
34ccb63e
AT
137}
138
139
9e31c482
AT
140
141static MDstruct sumMD;
142static int sumresidue;
143static char sumrbuf[CSUM_CHUNK];
144
145void sum_init(void)
146{
73545f2c
AT
147 char s[4];
148 MDbegin(&sumMD);
9e31c482 149 sumresidue=0;
73545f2c
AT
150 SIVAL(s,0,checksum_seed);
151 sum_update(s,4);
9e31c482
AT
152}
153
154void sum_update(char *p,int len)
155{
156 int i;
f94e821c
AT
157 if (len + sumresidue < CSUM_CHUNK) {
158 bcopy(p,sumrbuf+sumresidue,len);
159 sumresidue += len;
160 return;
161 }
162
9e31c482
AT
163 if (sumresidue) {
164 i = MIN(CSUM_CHUNK-sumresidue,len);
165 bcopy(p,sumrbuf+sumresidue,i);
166 MDupdate(&sumMD, sumrbuf, (i+sumresidue)*8);
167 len -= i;
168 p += i;
169 }
170
171 for(i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) {
172 bcopy(p+i,sumrbuf,CSUM_CHUNK);
173 MDupdate(&sumMD, sumrbuf, CSUM_CHUNK*8);
174 }
175
176 if (len - i > 0) {
177 sumresidue = len-i;
178 bcopy(p+i,sumrbuf,sumresidue);
179 } else {
180 sumresidue = 0;
181 }
182}
183
184void sum_end(char *sum)
185{
186 if (sumresidue)
187 MDupdate(&sumMD, sumrbuf, sumresidue*8);
188
189 SIVAL(sum,0,sumMD.buffer[0]);
190 SIVAL(sum,4,sumMD.buffer[1]);
191 SIVAL(sum,8,sumMD.buffer[2]);
192 SIVAL(sum,12,sumMD.buffer[3]);
193}
194
195