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 adler32
17{
18 u32 s1, s2, len;
19
20 inline u32 sum() const
21 {
22 return (s2 << 16) | s1;
23 }
24
25 inline void in(char c)
26 {
27 s1 += static_cast<u32>(c);
28 s1 &= 0xffff;
29 s2 += s1;
30 s2 &= 0xffff;
31 ++len;
32 }
33
34 inline void out(char c)
35 {
36 s1 -= static_cast<u32>(c);
37 s1 &= 0xffff;
38 s2 -= (len * static_cast<u32>(c)) + 1;
39 s2 &= 0xffff;
40 --len;
41 }
42
43 adler32() : s1(1), s2(0), len(0) {}
44 adler32(char const * ch, std::string::size_type count)
45 : s1(1), s2(0), len(0)
46 {
47 while(count--)
48 in(*(ch++));
49 }
50};
51
52#endif // __ADLER32_HH__

Archive Download this file

Branches

Tags

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