librsync  2.0.1
rollsum.c
1 /*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*-
2  *
3  * rollsum -- the librsync rolling checksum
4  *
5  * Copyright (C) 2003 by Donovan Baarda <[email protected]>
6  * based on work, Copyright (C) 2000, 2001 by Martin Pool <[email protected]>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22 #include "rollsum.h"
23 
24 #define DO1(buf,i) {s1 += buf[i]; s2 += s1;}
25 #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
26 #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
27 #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
28 #define DO16(buf) DO8(buf,0); DO8(buf,8);
29 #define OF16(off) {s1 += 16*off; s2 += 136*off;}
30 
31 void RollsumUpdate(Rollsum *sum,const unsigned char *buf,unsigned int len) {
32  /* ANSI C says no overflow for unsigned.
33  zlib's adler 32 goes to extra effort to avoid overflow*/
34  unsigned long s1 = sum->s1;
35  unsigned long s2 = sum->s2;
36 
37  sum->count+=len; /* increment sum count */
38  while (len >= 16) {
39  DO16(buf);
40  OF16(ROLLSUM_CHAR_OFFSET);
41  buf += 16;
42  len -= 16;
43  }
44  while (len != 0) {
45  s1 += (*buf++ + ROLLSUM_CHAR_OFFSET);
46  s2 += s1;
47  len--;
48  }
49  sum->s1=s1;
50  sum->s2=s2;
51 }