monotone

monotone Mtn Source Tree

Root/adler32.hh

1#ifndef __ADLER32_HH__
2#define __ADLER32_HH__
3
4// copyright (C) 2002, 2003 graydon hoare <graydon@pobox.com>
5// all rights reserved.
6// licensed to the public under the terms of the GNU GPL (>= 2)
7// see the file COPYING for details
8
9// this is a pseudo-adler32. it does not use a prime modulus. it is not
10// entirely clear that this matters; it is what rsync and xdelta both do
11// and it seems to work.
12
13#include <string>
14#include "numeric_vocab.hh"
15
16struct
17adler32
18{
19 u32 s1, s2, len;
20 u32 const mask;
21
22 inline u32 sum() const
23 {
24 return (s2 << 16) | s1;
25 }
26
27 inline void in(u8 c)
28 {
29 s1 += widen<u32,u8>(c);
30 s1 &= mask;
31 s2 += s1;
32 s2 &= mask;
33 ++len;
34 }
35
36 inline void out(u8 c)
37 {
38 s1 -= widen<u32,u8>(c);
39 s1 &= mask;
40 s2 -= (len * widen<u32,u8>(c)) + 1;
41 s2 &= mask;
42 --len;
43 }
44
45 adler32()
46 : s1(1), s2(0), len(0), mask(widen<u32,u16>(0xffff))
47 {}
48
49 adler32(u8 const * ch, std::string::size_type count)
50 : s1(1), s2(0), len(0), mask(widen<u32,u16>(0xffff))
51 {
52 while(count--)
53 in(*(ch++));
54 }
55};
56
57#endif // __ADLER32_HH__

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status