added resend logic
[rsync/rsync.git] / checksum.c
index eac41c4..ba81358 100644 (file)
@@ -23,8 +23,7 @@ int csum_length=SUM_LENGTH;
 
 #define CSUM_CHUNK 64
 
-static char *tmpchunk = NULL;
-
+int checksum_seed = 0;
 
 /*
   a simple 32 bit checksum that can be upadted from either end
@@ -63,16 +62,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 +94,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,20 +113,67 @@ 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");
+}
+
+
+
+static MDstruct sumMD;
+static int sumresidue;
+static char sumrbuf[CSUM_CHUNK];
+
+void sum_init(void)
+{
+  MDbegin(&sumMD);
+  sumresidue=0;
+}
+
+void sum_update(char *p,int len)
+{
+  int i;
+  if (sumresidue) {
+    i = MIN(CSUM_CHUNK-sumresidue,len);
+    bcopy(p,sumrbuf+sumresidue,i);
+    MDupdate(&sumMD, sumrbuf, (i+sumresidue)*8);
+    len -= i;
+    p += i;
+  }
+
+  for(i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) {
+    bcopy(p+i,sumrbuf,CSUM_CHUNK);
+    MDupdate(&sumMD, sumrbuf, CSUM_CHUNK*8);
+  }
+
+  if (len - i > 0) {
+    sumresidue = len-i;
+    bcopy(p+i,sumrbuf,sumresidue);
+  } else {
+    sumresidue = 0;    
+  }
+}
+
+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]);  
 }