如何解决Zlib adler32滚动校验和问题



我使用zlib中的adler32函数来计算内存块x(长度为4096)的弱校验和。一切都很好,但现在如果来自不同文件的块不匹配,我想执行滚动校验和。然而,我不知道如何编写一个函数来对zlib中adler32返回的值执行该操作。所以,如果校验和不匹配,我如何使用原始校验和x+1字节和x+4096+1来计算滚动校验和?主要是尝试构建rsync实现。

Pysync在zlib的Adler32之上实现了滚动,如下所示:

_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

但正如Peter所说,rsync并不直接使用Adler32,而是它的一个更快的变体

rsync工具的代码有点难以阅读,但请检查librsync。这是一个完全独立的项目,可读性更强。看看rollsum.crollsum.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))

相关内容

  • 没有找到相关文章

最新更新