X-Git-Url: https://mattmccutchen.net/rsync/rsync.git/blobdiff_plain/34ccb63e71367fb1f93530f60147ef6e4ac4e3e4..038406fdfef4945bfa18f760377cc45cd6221982:/checksum.c diff --git a/checksum.c b/checksum.c index eac41c4e..2344b632 100644 --- a/checksum.c +++ b/checksum.c @@ -23,8 +23,8 @@ int csum_length=SUM_LENGTH; #define CSUM_CHUNK 64 -static char *tmpchunk = NULL; - +int checksum_seed = 0; +extern int remote_version; /* a simple 32 bit checksum that can be upadted from either end @@ -63,16 +63,29 @@ void get_checksum2(char *buf,int len,char *sum) { int i; MDstruct MD; + static char *buf1 = NULL; + static int len1 = 0; + + if (len > len1) { + if (buf1) free(buf1); + buf1 = (char *)malloc(len+4); + len1 = len; + if (!buf1) out_of_memory("get_checksum2"); + } MDbegin(&MD); - for(i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) { - bcopy(buf+i,tmpchunk,CSUM_CHUNK); - MDupdate(&MD, tmpchunk, CSUM_CHUNK*8); + bcopy(buf,buf1,len); + if (checksum_seed) { + SIVAL(buf1,len,checksum_seed); + len += 4; } - bcopy(buf+i,tmpchunk,len-i); - MDupdate(&MD, tmpchunk, (len-i)*8); + for(i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) { + MDupdate(&MD, buf1+i, CSUM_CHUNK*8); + } + if (len - i > 0) + MDupdate(&MD, buf1+i, (len-i)*8); sum_put(&MD,sum); } @@ -82,9 +95,10 @@ void file_checksum(char *fname,char *sum,off_t size) { int i; MDstruct MD; - char *buf; + struct map_struct *buf; int fd; int len = size; + char tmpchunk[CSUM_CHUNK]; bzero(sum,csum_length); @@ -100,39 +114,74 @@ void file_checksum(char *fname,char *sum,off_t size) MDupdate(&MD, tmpchunk, CSUM_CHUNK*8); } - bcopy(map_ptr(buf,i,len-i),tmpchunk,len-i); - MDupdate(&MD, tmpchunk, (len-i)*8); + if (len - i > 0) { + bcopy(map_ptr(buf,i,len-i),tmpchunk,len-i); + MDupdate(&MD, tmpchunk, (len-i)*8); + } sum_put(&MD,sum); close(fd); - unmap_file(buf,size); + unmap_file(buf); } void checksum_init(void) { - tmpchunk = (char *)malloc(CSUM_CHUNK); - if (!tmpchunk) out_of_memory("checksum_init"); + if (remote_version >= 14) + csum_length = 2; /* adaptive */ + else + csum_length = SUM_LENGTH; } -#ifdef CHECKSUM_MAIN - int main(int argc,char *argv[]) + +static MDstruct sumMD; +static int sumresidue; +static char sumrbuf[CSUM_CHUNK]; + +void sum_init(void) { - char sum[SUM_LENGTH]; - int i,j; - - checksum_init(); - - for (i=1;i 0) { + sumresidue = len-i; + bcopy(p+i,sumrbuf,sumresidue); + } else { + sumresidue = 0; } } -#endif + +void sum_end(char *sum) +{ + if (sumresidue) + MDupdate(&sumMD, sumrbuf, sumresidue*8); + + SIVAL(sum,0,sumMD.buffer[0]); + SIVAL(sum,4,sumMD.buffer[1]); + SIVAL(sum,8,sumMD.buffer[2]); + SIVAL(sum,12,sumMD.buffer[3]); +} + +