Zlib adler32 checksum problem

I am using the adler32 function from zlib to calculate the weak checksum of a piece of memory x (4096 in length). Everything is fine, but now I would like to execute a rolling checksum if the pieces from another file do not match, however I'm not sure how to write a function to execute this value returned by adler32 in zlib. Therefore, if the checksum does not match, how can I calculate the checksum using the original checksum, x + 1 byte and x + 4096 + 1? basically trying to build an rsync implementation.

Thank.

+3
source share
1 answer

Pysync implemented casting on top of zlib Adler32 as follows:

_BASE=65521      # largest prime smaller than 65536
_NMAX=5552       # largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
_OFFS=1          # default initial s1 offset   
import zlib
class adler32:
    def __init__(self,data=''):
        value = zlib.adler32(data,_OFFS)
        self.s2, self.s1 = (value >> 16) & 0xffff, value & 0xffff
        self.count=len(data)
    def update(self,data):
        value = zlib.adler32(data, (self.s2<<16) | self.s1)
        self.s2, self.s1 = (value >> 16) & 0xffff, value & 0xffff
        self.count = self.count+len(data)
    def rotate(self,x1,xn):
        x1,xn=ord(x1),ord(xn)
        self.s1=(self.s1 - x1 + xn) % _BASE
        self.s2=(self.s2 - self.count*x1 + self.s1 - _OFFS) % _BASE
    def digest(self):
        return (self.s2<<16) | self.s1
    def copy(self):
        n=adler32()
        n.count,n.s1,n.s2=self.count,self.s1,self.s2
        return n

, , rsync Adler32 , .

rsync , librsync. , . rollsum.c rollsum.h. C:

/* the Rollsum struct type*/
typedef struct _Rollsum {
    unsigned long count;               /* count of bytes included in sum */
    unsigned long s1;                  /* s1 part of sum */
    unsigned long s2;                  /* s2 part of sum */
} Rollsum;

#define ROLLSUM_CHAR_OFFSET 31

#define RollsumInit(sum) { \
    (sum)->count=(sum)->s1=(sum)->s2=0; \
}

#define RollsumRotate(sum,out,in) { \
    (sum)->s1 += (unsigned char)(in) - (unsigned char)(out); \
    (sum)->s2 += (sum)->s1 - (sum)->count*((unsigned char)(out)+ROLLSUM_CHAR_OFFSET); \
}

#define RollsumRollin(sum,c) { \
    (sum)->s1 += ((unsigned char)(c)+ROLLSUM_CHAR_OFFSET); \
    (sum)->s2 += (sum)->s1; \
    (sum)->count++; \
}

#define RollsumRollout(sum,c) { \
    (sum)->s1 -= ((unsigned char)(c)+ROLLSUM_CHAR_OFFSET); \
    (sum)->s2 -= (sum)->count*((unsigned char)(c)+ROLLSUM_CHAR_OFFSET); \
    (sum)->count--; \
}

#define RollsumDigest(sum) (((sum)->s2 << 16) | ((sum)->s1 & 0xffff))
+5

All Articles