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 | ||
43a481dc | 22 | extern int csum_length; |
c627d613 AT |
23 | |
24 | /* | |
25 | a simple 32 bit checksum that can be upadted from either end | |
26 | (inspired by Mark Adler's Adler-32 checksum) | |
27 | */ | |
28 | uint32 get_checksum1(char *buf,int len) | |
29 | { | |
30 | int i; | |
31 | uint32 s1, s2; | |
32 | ||
33 | s1 = s2 = 0; | |
34 | for (i = 0; i < len; i++) { | |
35 | s1 += buf[i]; | |
36 | s2 += s1; | |
37 | } | |
38 | return (s1 & 0xffff) + (s2 << 16); | |
39 | } | |
40 | ||
41 | ||
42 | void get_checksum2(char *buf,int len,char *sum) | |
43 | { | |
44 | char buf2[64]; | |
45 | int i; | |
46 | MDstruct MD; | |
47 | ||
48 | MDbegin(&MD); | |
49 | for(i = 0; i + 64 <= len; i += 64) { | |
50 | bcopy(buf+i,buf2,64); | |
51 | MDupdate(&MD, buf2, 512); | |
52 | } | |
53 | bcopy(buf+i,buf2,len-i); | |
54 | MDupdate(&MD, buf2, (len-i)*8); | |
55 | SIVAL(sum,0,MD.buffer[0]); | |
43a481dc | 56 | if (csum_length <= 4) return; |
c627d613 | 57 | SIVAL(sum,4,MD.buffer[1]); |
43a481dc | 58 | if (csum_length <= 8) return; |
c627d613 | 59 | SIVAL(sum,8,MD.buffer[2]); |
43a481dc | 60 | if (csum_length <= 12) return; |
c627d613 AT |
61 | SIVAL(sum,12,MD.buffer[3]); |
62 | } | |
63 | ||
64 | void file_checksum(char *fname,char *sum,off_t size) | |
65 | { | |
66 | char *buf; | |
67 | int fd; | |
43a481dc | 68 | bzero(sum,csum_length); |
c627d613 AT |
69 | |
70 | fd = open(fname,O_RDONLY); | |
71 | if (fd == -1) return; | |
72 | ||
73 | buf = map_file(fd,size); | |
74 | if (!buf) { | |
75 | close(fd); | |
76 | return; | |
77 | } | |
78 | ||
79 | get_checksum2(buf,size,sum); | |
80 | close(fd); | |
81 | unmap_file(buf,size); | |
82 | } |