2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
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.
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.
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.
22 extern int csum_length;
25 a simple 32 bit checksum that can be upadted from either end
26 (inspired by Mark Adler's Adler-32 checksum)
28 uint32 get_checksum1(char *buf,int len)
34 for (i = 0; i < len; i++) {
38 return (s1 & 0xffff) + (s2 << 16);
42 void get_checksum2(char *buf,int len,char *sum)
49 for(i = 0; i + 64 <= len; i += 64) {
51 MDupdate(&MD, buf2, 512);
53 bcopy(buf+i,buf2,len-i);
54 MDupdate(&MD, buf2, (len-i)*8);
55 SIVAL(sum,0,MD.buffer[0]);
56 if (csum_length <= 4) return;
57 SIVAL(sum,4,MD.buffer[1]);
58 if (csum_length <= 8) return;
59 SIVAL(sum,8,MD.buffer[2]);
60 if (csum_length <= 12) return;
61 SIVAL(sum,12,MD.buffer[3]);
64 void file_checksum(char *fname,char *sum,off_t size)
68 bzero(sum,csum_length);
70 fd = open(fname,O_RDONLY);
73 buf = map_file(fd,size);
79 get_checksum2(buf,size,sum);