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