librsync  2.3.4
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 <abo@minkirri.apana.org.au>
6 * based on work, Copyright (C) 2000, 2001 by Martin Pool <mbp@sourcefrog.net>
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 "config.h" /* IWYU pragma: keep */
23#include "rollsum.h"
24
25#define DO1(buf,i) {s1 += buf[i]; s2 += s1;}
26#define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
27#define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
28#define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
29#define DO16(buf) DO8(buf,0); DO8(buf,8);
30
31void RollsumUpdate(Rollsum *sum, const unsigned char *buf, size_t len)
32{
33 /* ANSI C says no overflow for unsigned. zlib's adler32 goes to extra
34 effort to avoid overflow for its mod prime, which we don't have. */
35 size_t n = len;
36 uint_fast16_t s1 = sum->s1;
37 uint_fast16_t s2 = sum->s2;
38
39 while (n >= 16) {
40 DO16(buf);
41 buf += 16;
42 n -= 16;
43 }
44 while (n != 0) {
45 s1 += *buf++;
46 s2 += s1;
47 n--;
48 }
49 /* Increment s1 and s2 by the amounts added by the char offset. */
50 s1 += (uint_fast16_t)len * ROLLSUM_CHAR_OFFSET;
51 s2 += (uint_fast16_t)((len * (len + 1)) / 2) * ROLLSUM_CHAR_OFFSET;
52 sum->count += len; /* Increment sum count. */
53 sum->s1 = s1;
54 sum->s2 = s2;
55}
The Rollsum class implementation of the original rsync rollsum.
The Rollsum state type.
Definition: rollsum.h:36
size_t count
count of bytes included in sum
Definition: rollsum.h:37
uint_fast16_t s1
s1 part of sum
Definition: rollsum.h:38
uint_fast16_t s2
s2 part of sum
Definition: rollsum.h:39