librsync  2.0.1
rollsum.h
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 #ifndef _ROLLSUM_H_
23 #define _ROLLSUM_H_
24 
25 /* We should make this something other than zero to improve the
26  * checksum algorithm: tridge suggests a prime number. */
27 #define ROLLSUM_CHAR_OFFSET 31
28 
29 /* the Rollsum struct type*/
30 
31 /** \private */
32 typedef struct _Rollsum {
33  unsigned long count; /* count of bytes included in sum */
34  unsigned long s1; /* s1 part of sum */
35  unsigned long s2; /* s2 part of sum */
36 } Rollsum;
37 
38 void RollsumUpdate(Rollsum *sum,const unsigned char *buf,unsigned int len);
39 /* The following are implemented as macros.
40 void RollsumInit(Rollsum *sum);
41 void RollsumRotate(Rollsum *sum,unsigned char out, unsigned char in);
42 void RollsumRollin(Rollsum *sum,unsigned char c);
43 void RollsumRollout(Rollsum *sum,unsigned char c);
44 unsigned long RollsumDigest(Rollsum *sum);
45 */
46 
47 /* macro implementations of simple routines */
48 #define RollsumInit(sum) { \
49  (sum)->count=(sum)->s1=(sum)->s2=0; \
50 }
51 
52 #define RollsumRotate(sum,out,in) { \
53  (sum)->s1 += (unsigned char)(in) - (unsigned char)(out); \
54  (sum)->s2 += (sum)->s1 - (sum)->count*((unsigned char)(out)+ROLLSUM_CHAR_OFFSET); \
55 }
56 
57 #define RollsumRollin(sum,c) { \
58  (sum)->s1 += ((unsigned char)(c)+ROLLSUM_CHAR_OFFSET); \
59  (sum)->s2 += (sum)->s1; \
60  (sum)->count++; \
61 }
62 
63 #define RollsumRollout(sum,c) { \
64  (sum)->s1 -= ((unsigned char)(c)+ROLLSUM_CHAR_OFFSET); \
65  (sum)->s2 -= (sum)->count*((unsigned char)(c)+ROLLSUM_CHAR_OFFSET); \
66  (sum)->count--; \
67 }
68 
69 #define RollsumDigest(sum) (((sum)->s2 << 16) | ((sum)->s1 & 0xffff))
70 
71 #endif /* _ROLLSUM_H_ */