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.
22 int csum_length=SUM_LENGTH;
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 *buf,int len)
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]);
43 for (; i < len; i++) {
44 s1 += buf[i]; s2 += s1;
46 return (s1 & 0xffff) + (s2 << 16);
50 static void sum_put(MDstruct *md,char *sum)
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]);
62 void get_checksum2(char *buf,int len,char *sum)
66 static char *buf1 = NULL;
71 buf1 = (char *)malloc(len+4);
73 if (!buf1) out_of_memory("get_checksum2");
80 SIVAL(buf1,len,checksum_seed);
84 for(i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) {
85 MDupdate(&MD, buf1+i, CSUM_CHUNK*8);
88 MDupdate(&MD, buf1+i, (len-i)*8);
94 void file_checksum(char *fname,char *sum,off_t size)
98 struct map_struct *buf;
101 char tmpchunk[CSUM_CHUNK];
103 bzero(sum,csum_length);
105 fd = open(fname,O_RDONLY);
106 if (fd == -1) return;
108 buf = map_file(fd,size);
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);
118 bcopy(map_ptr(buf,i,len-i),tmpchunk,len-i);
119 MDupdate(&MD, tmpchunk, (len-i)*8);
129 void checksum_init(void)
131 if (remote_version >= 14)
132 csum_length = 2; /* adaptive */
134 csum_length = SUM_LENGTH;
139 static MDstruct sumMD;
140 static int sumresidue;
141 static char sumrbuf[CSUM_CHUNK];
148 SIVAL(s,0,checksum_seed);
152 void sum_update(char *p,int len)
156 i = MIN(CSUM_CHUNK-sumresidue,len);
157 bcopy(p,sumrbuf+sumresidue,i);
158 MDupdate(&sumMD, sumrbuf, (i+sumresidue)*8);
163 for(i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) {
164 bcopy(p+i,sumrbuf,CSUM_CHUNK);
165 MDupdate(&sumMD, sumrbuf, CSUM_CHUNK*8);
170 bcopy(p+i,sumrbuf,sumresidue);
176 void sum_end(char *sum)
179 MDupdate(&sumMD, sumrbuf, sumresidue*8);
181 SIVAL(sum,0,sumMD.buffer[0]);
182 SIVAL(sum,4,sumMD.buffer[1]);
183 SIVAL(sum,8,sumMD.buffer[2]);
184 SIVAL(sum,12,sumMD.buffer[3]);