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