librsync  2.3.4
rollsum.h
Go to the documentation of this file.
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 
23 /** \file rollsum.h
24  * The Rollsum class implementation of the original rsync rollsum. */
25 #ifndef ROLLSUM_H
26 # define ROLLSUM_H
27 
28 # include <stddef.h>
29 # include <stdint.h>
30 
31 /* We should make this something other than zero to improve the checksum
32  algorithm: tridge suggests a prime number. */
33 # define ROLLSUM_CHAR_OFFSET 31
34 
35 /** The Rollsum state type. */
36 typedef struct Rollsum {
37  size_t count; /**< count of bytes included in sum */
38  uint_fast16_t s1; /**< s1 part of sum */
39  uint_fast16_t s2; /**< s2 part of sum */
41 
42 void RollsumUpdate(Rollsum *sum, const unsigned char *buf, size_t len);
43 
44 /* static inline implementations of simple routines */
45 
46 static inline void RollsumInit(Rollsum *sum)
47 {
48  sum->count = sum->s1 = sum->s2 = 0;
49 }
50 
51 static inline void RollsumRotate(Rollsum *sum, unsigned char out,
52  unsigned char in)
53 {
54  sum->s1 += in - out;
55  sum->s2 += sum->s1 - (uint_fast16_t)sum->count * (out + ROLLSUM_CHAR_OFFSET);
56 }
57 
58 static inline void RollsumRollin(Rollsum *sum, unsigned char in)
59 {
60  sum->s1 += in + ROLLSUM_CHAR_OFFSET;
61  sum->s2 += sum->s1;
62  sum->count++;
63 }
64 
65 static inline void RollsumRollout(Rollsum *sum, unsigned char out)
66 {
67  sum->s1 -= out + ROLLSUM_CHAR_OFFSET;
68  sum->s2 -= (uint_fast16_t)sum->count * (out + ROLLSUM_CHAR_OFFSET);
69  sum->count--;
70 }
71 
72 static inline uint32_t RollsumDigest(Rollsum *sum)
73 {
74  return ((uint32_t)sum->s2 << 16) | ((uint32_t)sum->s1 & 0xffff);
75 }
76 
77 #endif /* !ROLLSUM_H */
struct Rollsum Rollsum
The Rollsum state type.
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