Commit | Line | Data |
---|---|---|
0d7638ea | 1 | /* |
c627d613 AT |
2 | Copyright (C) Andrew Tridgell 1996 |
3 | Copyright (C) Paul Mackerras 1996 | |
0d7638ea | 4 | |
c627d613 AT |
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. | |
0d7638ea | 9 | |
c627d613 AT |
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. | |
0d7638ea | 14 | |
c627d613 AT |
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 | 22 | int csum_length=2; /* initial value */ |
34ccb63e AT |
23 | |
24 | #define CSUM_CHUNK 64 | |
25 | ||
8ee6eb71 | 26 | extern int checksum_seed; |
d04e9c51 | 27 | extern int protocol_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 | 33 | uint32 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) { |
0d7638ea | 41 | s2 += 4*(s1 + buf[i]) + 3*buf[i+1] + 2*buf[i+2] + buf[i+3] + |
1b01b295 | 42 | 10*CHAR_OFFSET; |
0d7638ea | 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 | ||
52 | void get_checksum2(char *buf,int len,char *sum) | |
53 | { | |
8de330a3 AT |
54 | int i; |
55 | static char *buf1; | |
56 | static int len1; | |
57 | struct mdfour m; | |
58 | ||
59 | if (len > len1) { | |
60 | if (buf1) free(buf1); | |
58cadc86 | 61 | buf1 = new_array(char, len+4); |
8de330a3 AT |
62 | len1 = len; |
63 | if (!buf1) out_of_memory("get_checksum2"); | |
64 | } | |
0d7638ea | 65 | |
8de330a3 | 66 | mdfour_begin(&m); |
0d7638ea | 67 | |
8de330a3 AT |
68 | memcpy(buf1,buf,len); |
69 | if (checksum_seed) { | |
70 | SIVAL(buf1,len,checksum_seed); | |
71 | len += 4; | |
72 | } | |
0d7638ea | 73 | |
8de330a3 | 74 | for(i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) { |
7ae359c3 | 75 | mdfour_update(&m, (uchar *)(buf1+i), CSUM_CHUNK); |
8de330a3 | 76 | } |
fc1ae658 S |
77 | /* |
78 | * Prior to version 27 an incorrect MD4 checksum was computed | |
79 | * by failing to call mdfour_tail() for block sizes that | |
80 | * are multiples of 64. This is fixed by calling mdfour_update() | |
81 | * even when there are no more bytes. | |
82 | */ | |
d04e9c51 | 83 | if (len - i > 0 || protocol_version >= 27) { |
7ae359c3 | 84 | mdfour_update(&m, (uchar *)(buf1+i), (len-i)); |
8de330a3 | 85 | } |
0d7638ea | 86 | |
7ae359c3 | 87 | mdfour_result(&m, (uchar *)sum); |
c627d613 AT |
88 | } |
89 | ||
34ccb63e | 90 | |
bcacc18b | 91 | void file_checksum(char *fname,char *sum,OFF_T size) |
c627d613 | 92 | { |
8de330a3 AT |
93 | OFF_T i; |
94 | struct map_struct *buf; | |
95 | int fd; | |
96 | OFF_T len = size; | |
8de330a3 | 97 | struct mdfour m; |
0d7638ea | 98 | |
f855a7d0 | 99 | memset(sum,0,MD4_SUM_LENGTH); |
0d7638ea | 100 | |
8c9fd200 | 101 | fd = do_open(fname, O_RDONLY, 0); |
efa95a18 WD |
102 | if (fd == -1) |
103 | return; | |
0d7638ea | 104 | |
bd1a581b | 105 | buf = map_file(fd, size, MAX_MAP_SIZE, CSUM_CHUNK); |
0d7638ea | 106 | |
8de330a3 AT |
107 | mdfour_begin(&m); |
108 | ||
109 | for(i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) { | |
afd8bdb9 WD |
110 | mdfour_update(&m, (uchar *)map_ptr(buf, i, CSUM_CHUNK), |
111 | CSUM_CHUNK); | |
8de330a3 AT |
112 | } |
113 | ||
afd8bdb9 | 114 | /* Prior to version 27 an incorrect MD4 checksum was computed |
fc1ae658 S |
115 | * by failing to call mdfour_tail() for block sizes that |
116 | * are multiples of 64. This is fixed by calling mdfour_update() | |
afd8bdb9 WD |
117 | * even when there are no more bytes. */ |
118 | if (len - i > 0 || protocol_version >= 27) | |
119 | mdfour_update(&m, (uchar *)map_ptr(buf, i, len-i), len-i); | |
8de330a3 | 120 | |
7ae359c3 | 121 | mdfour_result(&m, (uchar *)sum); |
8de330a3 AT |
122 | |
123 | close(fd); | |
124 | unmap_file(buf); | |
c627d613 | 125 | } |
34ccb63e AT |
126 | |
127 | ||
9e31c482 AT |
128 | static int sumresidue; |
129 | static char sumrbuf[CSUM_CHUNK]; | |
8de330a3 | 130 | static struct mdfour md; |
9e31c482 | 131 | |
ba582f75 | 132 | void sum_init(int seed) |
9e31c482 | 133 | { |
8de330a3 AT |
134 | char s[4]; |
135 | mdfour_begin(&md); | |
ba582f75 WD |
136 | sumresidue = 0; |
137 | SIVAL(s, 0, seed); | |
138 | sum_update(s, 4); | |
9e31c482 AT |
139 | } |
140 | ||
d37d8d7b MP |
141 | /** |
142 | * Feed data into an MD4 accumulator, md. The results may be | |
143 | * retrieved using sum_end(). md is used for different purposes at | |
144 | * different points during execution. | |
145 | * | |
146 | * @todo Perhaps get rid of md and just pass in the address each time. | |
147 | * Very slightly clearer and slower. | |
148 | **/ | |
149 | void sum_update(char *p, int len) | |
9e31c482 | 150 | { |
8de330a3 AT |
151 | int i; |
152 | if (len + sumresidue < CSUM_CHUNK) { | |
153 | memcpy(sumrbuf+sumresidue, p, len); | |
154 | sumresidue += len; | |
155 | return; | |
156 | } | |
157 | ||
158 | if (sumresidue) { | |
159 | i = MIN(CSUM_CHUNK-sumresidue,len); | |
160 | memcpy(sumrbuf+sumresidue,p,i); | |
7ae359c3 | 161 | mdfour_update(&md, (uchar *)sumrbuf, (i+sumresidue)); |
8de330a3 AT |
162 | len -= i; |
163 | p += i; | |
164 | } | |
165 | ||
166 | for(i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) { | |
167 | memcpy(sumrbuf,p+i,CSUM_CHUNK); | |
7ae359c3 | 168 | mdfour_update(&md, (uchar *)sumrbuf, CSUM_CHUNK); |
8de330a3 AT |
169 | } |
170 | ||
171 | if (len - i > 0) { | |
172 | sumresidue = len-i; | |
173 | memcpy(sumrbuf,p+i,sumresidue); | |
174 | } else { | |
0d7638ea | 175 | sumresidue = 0; |
8de330a3 | 176 | } |
9e31c482 AT |
177 | } |
178 | ||
179 | void sum_end(char *sum) | |
180 | { | |
d04e9c51 | 181 | if (sumresidue || protocol_version >= 27) { |
7ae359c3 | 182 | mdfour_update(&md, (uchar *)sumrbuf, sumresidue); |
8de330a3 | 183 | } |
9e31c482 | 184 | |
7ae359c3 | 185 | mdfour_result(&md, (uchar *)sum); |
9e31c482 | 186 | } |