Commit | Line | Data |
---|---|---|
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 |
22 | int csum_length=SUM_LENGTH; |
23 | ||
24 | #define CSUM_CHUNK 64 | |
25 | ||
26 | static char *tmpchunk = NULL; | |
27 | ||
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 | */ | |
33 | uint32 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 |
50 | static 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 |
62 | void get_checksum2(char *buf,int len,char *sum) |
63 | { | |
c627d613 AT |
64 | int i; |
65 | MDstruct MD; | |
66 | ||
67 | MDbegin(&MD); | |
34ccb63e AT |
68 | |
69 | for(i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) { | |
70 | bcopy(buf+i,tmpchunk,CSUM_CHUNK); | |
71 | MDupdate(&MD, tmpchunk, CSUM_CHUNK*8); | |
c627d613 | 72 | } |
34ccb63e AT |
73 | |
74 | bcopy(buf+i,tmpchunk,len-i); | |
75 | MDupdate(&MD, tmpchunk, (len-i)*8); | |
76 | ||
77 | sum_put(&MD,sum); | |
c627d613 AT |
78 | } |
79 | ||
34ccb63e | 80 | |
c627d613 AT |
81 | void file_checksum(char *fname,char *sum,off_t size) |
82 | { | |
34ccb63e AT |
83 | int i; |
84 | MDstruct MD; | |
c627d613 AT |
85 | char *buf; |
86 | int fd; | |
34ccb63e AT |
87 | int len = size; |
88 | ||
43a481dc | 89 | bzero(sum,csum_length); |
c627d613 AT |
90 | |
91 | fd = open(fname,O_RDONLY); | |
92 | if (fd == -1) return; | |
93 | ||
94 | buf = map_file(fd,size); | |
34ccb63e AT |
95 | |
96 | MDbegin(&MD); | |
97 | ||
98 | for(i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) { | |
99 | bcopy(map_ptr(buf,i,CSUM_CHUNK),tmpchunk,CSUM_CHUNK); | |
100 | MDupdate(&MD, tmpchunk, CSUM_CHUNK*8); | |
c627d613 AT |
101 | } |
102 | ||
34ccb63e AT |
103 | bcopy(map_ptr(buf,i,len-i),tmpchunk,len-i); |
104 | MDupdate(&MD, tmpchunk, (len-i)*8); | |
105 | ||
106 | sum_put(&MD,sum); | |
107 | ||
c627d613 AT |
108 | close(fd); |
109 | unmap_file(buf,size); | |
110 | } | |
34ccb63e AT |
111 | |
112 | ||
113 | void checksum_init(void) | |
114 | { | |
115 | tmpchunk = (char *)malloc(CSUM_CHUNK); | |
116 | if (!tmpchunk) out_of_memory("checksum_init"); | |
117 | } | |
118 | ||
119 | ||
120 | #ifdef CHECKSUM_MAIN | |
121 | int main(int argc,char *argv[]) | |
122 | { | |
123 | char sum[SUM_LENGTH]; | |
124 | int i,j; | |
125 | ||
126 | checksum_init(); | |
127 | ||
128 | for (i=1;i<argc;i++) { | |
129 | struct stat st; | |
130 | if (stat(argv[i],&st) == 0) { | |
131 | file_checksum(argv[i],sum,st.st_size); | |
132 | for (j=0;j<SUM_LENGTH;j++) | |
133 | printf("%02X",(unsigned char)sum[j]); | |
134 | printf(" %s\n",argv[i]); | |
135 | } | |
136 | } | |
137 | } | |
138 | #endif |