//ref: //http://www.ieyebrain.com:8080/pts/src/rsync/ type Rollsum struct { a, b uint16 Xk byte count int } func New(count int) *Rollsum { return &Rollsum{ a:0, b:0, count: count} } func (r *Rollsum) Init(a uint16, b uint16, xk uint8) { r.a = a r.b = b r.Xk = xk } func (r *Rollsum) InitByBuf(buf []byte) { r.a,r.b = calc_weaksum(buf) r.Xk = buf[0] } func (r *Rollsum) Update(Xk_1 byte, Xl_one byte) { var a,b int a , b = int(r.a),int(r.b) a = a - int(r.Xk) + int(Xl_one) b = a + b - r.count * int(r.Xk) r.Xk = Xk_1 r.a = uint16(a) r.b = uint16(b) } func (r *Rollsum) GetValues() (uint16,uint16) { return r.a,r.b } type Weaksum struct { a,b uint16 } func calc_weaksum(buf []byte) (uint16, uint16) { var a,b,c int; i := 0 len := len(buf) a,b = 0,0 for len > 0 { c = int(buf[i]) a = a + c b = b + (len * c) len-- i++ } //return Weaksum{a:uint16(a),b:uint16(b)} return uint16(a), uint16(b) } func calc_strongsum(buf []byte) ([]byte) { ctx := md4.New() ctx.Write(buf) return ctx.Sum(nil) }
package rsync type Rollsum struct { a, b uint16 Xk byte count int } func New(count int) *Rollsum { return &Rollsum{ a:0, b:0, count: count} } func (r *Rollsum) Init(a uint16, b uint16, xk uint8) { r.a = a r.b = b r.Xk = xk } func (r *Rollsum) InitByBuf(buf []byte) { r.a,r.b = calc_weaksum(buf) r.Xk = buf[0] } func (r *Rollsum) Update(Xk_1 byte, Xl_one byte) { var a,b int a , b = int(r.a),int(r.b) a = a - int(r.Xk) + int(Xl_one) b = a + b - r.count * int(r.Xk) r.Xk = Xk_1 r.a = uint16(a) r.b = uint16(b) } func (r *Rollsum) GetValues() (uint16,uint16) { return r.a,r.b }
有疑问加站长微信联系(非本文作者)