| 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 | int csum_length=SUM_LENGTH; |
| 23 | |
| 24 | #define CSUM_CHUNK 64 |
| 25 | |
| 26 | int checksum_seed = 0; |
| 27 | |
| 28 | /* |
| 29 | a simple 32 bit checksum that can be upadted from either end |
| 30 | (inspired by Mark Adler's Adler-32 checksum) |
| 31 | */ |
| 32 | uint32 get_checksum1(char *buf,int len) |
| 33 | { |
| 34 | int i; |
| 35 | uint32 s1, s2; |
| 36 | |
| 37 | s1 = s2 = 0; |
| 38 | for (i = 0; i < (len-4); i+=4) { |
| 39 | s2 += 4*(s1 + buf[i]) + 3*buf[i+1] + 2*buf[i+2] + buf[i+3]; |
| 40 | s1 += (buf[i+0] + buf[i+1] + buf[i+2] + buf[i+3]); |
| 41 | } |
| 42 | for (; i < len; i++) { |
| 43 | s1 += buf[i]; s2 += s1; |
| 44 | } |
| 45 | return (s1 & 0xffff) + (s2 << 16); |
| 46 | } |
| 47 | |
| 48 | |
| 49 | static void sum_put(MDstruct *md,char *sum) |
| 50 | { |
| 51 | SIVAL(sum,0,md->buffer[0]); |
| 52 | if (csum_length <= 4) return; |
| 53 | SIVAL(sum,4,md->buffer[1]); |
| 54 | if (csum_length <= 8) return; |
| 55 | SIVAL(sum,8,md->buffer[2]); |
| 56 | if (csum_length <= 12) return; |
| 57 | SIVAL(sum,12,md->buffer[3]); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | void get_checksum2(char *buf,int len,char *sum) |
| 62 | { |
| 63 | int i; |
| 64 | MDstruct MD; |
| 65 | static char *buf1 = NULL; |
| 66 | static int len1 = 0; |
| 67 | |
| 68 | if (len > len1) { |
| 69 | if (buf1) free(buf1); |
| 70 | buf1 = (char *)malloc(len+4); |
| 71 | len1 = len; |
| 72 | if (!buf1) out_of_memory("get_checksum2"); |
| 73 | } |
| 74 | |
| 75 | MDbegin(&MD); |
| 76 | |
| 77 | bcopy(buf,buf1,len); |
| 78 | if (checksum_seed) { |
| 79 | SIVAL(buf1,len,checksum_seed); |
| 80 | len += 4; |
| 81 | } |
| 82 | |
| 83 | for(i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) { |
| 84 | MDupdate(&MD, buf1+i, CSUM_CHUNK*8); |
| 85 | } |
| 86 | MDupdate(&MD, buf1+i, (len-i)*8); |
| 87 | |
| 88 | sum_put(&MD,sum); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | void file_checksum(char *fname,char *sum,off_t size) |
| 93 | { |
| 94 | int i; |
| 95 | MDstruct MD; |
| 96 | struct map_struct *buf; |
| 97 | int fd; |
| 98 | int len = size; |
| 99 | char tmpchunk[CSUM_CHUNK]; |
| 100 | |
| 101 | bzero(sum,csum_length); |
| 102 | |
| 103 | fd = open(fname,O_RDONLY); |
| 104 | if (fd == -1) return; |
| 105 | |
| 106 | buf = map_file(fd,size); |
| 107 | |
| 108 | MDbegin(&MD); |
| 109 | |
| 110 | for(i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) { |
| 111 | bcopy(map_ptr(buf,i,CSUM_CHUNK),tmpchunk,CSUM_CHUNK); |
| 112 | MDupdate(&MD, tmpchunk, CSUM_CHUNK*8); |
| 113 | } |
| 114 | |
| 115 | bcopy(map_ptr(buf,i,len-i),tmpchunk,len-i); |
| 116 | MDupdate(&MD, tmpchunk, (len-i)*8); |
| 117 | |
| 118 | sum_put(&MD,sum); |
| 119 | |
| 120 | close(fd); |
| 121 | unmap_file(buf); |
| 122 | } |
| 123 | |
| 124 | |
| 125 | void checksum_init(void) |
| 126 | { |
| 127 | } |
| 128 | |
| 129 | |
| 130 | #ifdef CHECKSUM_MAIN |
| 131 | int main(int argc,char *argv[]) |
| 132 | { |
| 133 | char sum[SUM_LENGTH]; |
| 134 | int i,j; |
| 135 | |
| 136 | checksum_init(); |
| 137 | |
| 138 | for (i=1;i<argc;i++) { |
| 139 | struct stat st; |
| 140 | if (stat(argv[i],&st) == 0) { |
| 141 | file_checksum(argv[i],sum,st.st_size); |
| 142 | for (j=0;j<SUM_LENGTH;j++) |
| 143 | printf("%02X",(unsigned char)sum[j]); |
| 144 | printf(" %s\n",argv[i]); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | #endif |