source: box/trunk/lib/crypto/RollingChecksum.h @ 217

Revision 217, 3.7 KB checked in by martin, 6 years ago (diff)

Set svn:eol-style as appropriate for all files

  • Property svn:eol-style set to native
Line 
1// --------------------------------------------------------------------------
2//
3// File
4//              Name:    RollingChecksum.h
5//              Purpose: A simple rolling checksum over a block of data
6//              Created: 6/12/03
7//
8// --------------------------------------------------------------------------
9
10#ifndef ROLLINGCHECKSUM__H
11#define ROLLINGCHECKSUM__H
12
13// --------------------------------------------------------------------------
14//
15// Class
16//              Name:    RollingChecksum
17//              Purpose: A simple rolling checksum over a block of data -- can move the block
18//                               "forwards" in memory and get the next checksum efficiently.
19//
20//                               Implementation of http://rsync.samba.org/tech_report/node3.html
21//              Created: 6/12/03
22//
23// --------------------------------------------------------------------------
24class RollingChecksum
25{
26public:
27        RollingChecksum(const void * const data, const unsigned int Length);
28
29        // --------------------------------------------------------------------------
30        //
31        // Function
32        //              Name:    RollingChecksum::RollForward(uint8_t, uint8_t, unsigned int)
33        //              Purpose: Move the checksum forward a block, given the first byte of the current block,
34        //                               last byte of the next block (it's rolling forward to) and the length of the block.
35        //              Created: 6/12/03
36        //
37        // --------------------------------------------------------------------------
38        inline void RollForward(const uint8_t StartOfThisBlock, const uint8_t LastOfNextBlock, const unsigned int Length)
39        {
40                // IMPLEMENTATION NOTE: Everything is implicitly mod 2^16 -- uint16_t's will overflow nicely.
41                a -= StartOfThisBlock;
42                a += LastOfNextBlock;
43                b -= Length * StartOfThisBlock;
44                b += a;
45        }
46
47        // --------------------------------------------------------------------------
48        //
49        // Function
50        //              Name:    RollingChecksum::RollForwardSeveral(uint8_t*, uint8_t*, unsigned int, unsigned int)
51        //              Purpose: Move the checksum forward a block, given a pointer to the first byte of the current block,
52        //                               and a pointer just after the last byte of the current block and the length of the block and of the skip.
53        //              Created: 7/14/05
54        //
55        // --------------------------------------------------------------------------
56        void RollForwardSeveral(const uint8_t * const StartOfThisBlock, const uint8_t * const LastOfNextBlock, const unsigned int Length, const unsigned int Skip);
57
58        // --------------------------------------------------------------------------
59        //
60        // Function
61        //              Name:    RollingChecksum::GetChecksum()
62        //              Purpose: Returns the checksum
63        //              Created: 6/12/03
64        //
65        // --------------------------------------------------------------------------   
66        inline uint32_t GetChecksum() const
67        {
68                return ((uint32_t)a) | (((uint32_t)b) << 16);
69        }
70       
71        // Components, just in case they're handy
72        inline uint16_t GetComponent1() const {return a;}
73        inline uint16_t GetComponent2() const {return b;}
74       
75        // --------------------------------------------------------------------------
76        //
77        // Function
78        //              Name:    RollingChecksum::GetComponentForHashing()
79        //              Purpose: Return the 16 bit component used for hashing and/or quick checks
80        //              Created: 6/12/03
81        //
82        // --------------------------------------------------------------------------
83        inline uint16_t GetComponentForHashing() const
84        {
85                return b;
86        }
87       
88        // --------------------------------------------------------------------------
89        //
90        // Function
91        //              Name:    RollingChecksum::ExtractHashingComponent(uint32_t)
92        //              Purpose: Static. Given a full checksum, extract the component used in the hashing table.
93        //              Created: 14/1/04
94        //
95        // --------------------------------------------------------------------------
96        static inline uint16_t ExtractHashingComponent(const uint32_t Checksum)
97        {
98                return Checksum >> 16;
99        }
100       
101private:
102        uint16_t a;
103        uint16_t b;
104};
105
106#endif // ROLLINGCHECKSUM__H
107
Note: See TracBrowser for help on using the repository browser.