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