]>
git.ipfire.org Git - thirdparty/rsync.git/blob - checksum.c
2 * Routines to support checksumming of bytes.
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2004, 2005 Wayne Davison
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 int csum_length
=2; /* initial value */
29 extern int checksum_seed
;
30 extern int protocol_version
;
33 a simple 32 bit checksum that can be upadted from either end
34 (inspired by Mark Adler's Adler-32 checksum)
36 uint32
get_checksum1(char *buf1
, int32 len
)
40 schar
*buf
= (schar
*)buf1
;
43 for (i
= 0; i
< (len
-4); i
+=4) {
44 s2
+= 4*(s1
+ buf
[i
]) + 3*buf
[i
+1] + 2*buf
[i
+2] + buf
[i
+3] +
46 s1
+= (buf
[i
+0] + buf
[i
+1] + buf
[i
+2] + buf
[i
+3] + 4*CHAR_OFFSET
);
48 for (; i
< len
; i
++) {
49 s1
+= (buf
[i
]+CHAR_OFFSET
); s2
+= s1
;
51 return (s1
& 0xffff) + (s2
<< 16);
55 void get_checksum2(char *buf
, int32 len
, char *sum
)
65 buf1
= new_array(char, len
+4);
68 out_of_memory("get_checksum2");
75 SIVAL(buf1
,len
,checksum_seed
);
79 for(i
= 0; i
+ CSUM_CHUNK
<= len
; i
+= CSUM_CHUNK
) {
80 mdfour_update(&m
, (uchar
*)(buf1
+i
), CSUM_CHUNK
);
83 * Prior to version 27 an incorrect MD4 checksum was computed
84 * by failing to call mdfour_tail() for block sizes that
85 * are multiples of 64. This is fixed by calling mdfour_update()
86 * even when there are no more bytes.
88 if (len
- i
> 0 || protocol_version
>= 27) {
89 mdfour_update(&m
, (uchar
*)(buf1
+i
), (len
-i
));
92 mdfour_result(&m
, (uchar
*)sum
);
96 void file_checksum(char *fname
,char *sum
,OFF_T size
)
99 struct map_struct
*buf
;
104 memset(sum
,0,MD4_SUM_LENGTH
);
106 fd
= do_open(fname
, O_RDONLY
, 0);
110 buf
= map_file(fd
, size
, MAX_MAP_SIZE
, CSUM_CHUNK
);
114 for(i
= 0; i
+ CSUM_CHUNK
<= len
; i
+= CSUM_CHUNK
) {
115 mdfour_update(&m
, (uchar
*)map_ptr(buf
, i
, CSUM_CHUNK
),
119 /* Prior to version 27 an incorrect MD4 checksum was computed
120 * by failing to call mdfour_tail() for block sizes that
121 * are multiples of 64. This is fixed by calling mdfour_update()
122 * even when there are no more bytes. */
123 if (len
- i
> 0 || protocol_version
>= 27)
124 mdfour_update(&m
, (uchar
*)map_ptr(buf
, i
, len
-i
), len
-i
);
126 mdfour_result(&m
, (uchar
*)sum
);
133 static int32 sumresidue
;
134 static char sumrbuf
[CSUM_CHUNK
];
135 static struct mdfour md
;
137 void sum_init(int seed
)
147 * Feed data into an MD4 accumulator, md. The results may be
148 * retrieved using sum_end(). md is used for different purposes at
149 * different points during execution.
151 * @todo Perhaps get rid of md and just pass in the address each time.
152 * Very slightly clearer and slower.
154 void sum_update(char *p
, int32 len
)
156 if (len
+ sumresidue
< CSUM_CHUNK
) {
157 memcpy(sumrbuf
+ sumresidue
, p
, len
);
163 int32 i
= CSUM_CHUNK
- sumresidue
;
164 memcpy(sumrbuf
+ sumresidue
, p
, i
);
165 mdfour_update(&md
, (uchar
*)sumrbuf
, CSUM_CHUNK
);
170 while (len
>= CSUM_CHUNK
) {
171 mdfour_update(&md
, (uchar
*)p
, CSUM_CHUNK
);
178 memcpy(sumrbuf
, p
, sumresidue
);
181 void sum_end(char *sum
)
183 if (sumresidue
|| protocol_version
>= 27)
184 mdfour_update(&md
, (uchar
*)sumrbuf
, sumresidue
);
186 mdfour_result(&md
, (uchar
*)sum
);