| 1 | // -------------------------------------------------------------------------- |
|---|
| 2 | // |
|---|
| 3 | // File |
|---|
| 4 | // Name: RollingChecksum.cpp |
|---|
| 5 | // Purpose: A simple rolling checksum over a block of data |
|---|
| 6 | // Created: 6/12/03 |
|---|
| 7 | // |
|---|
| 8 | // -------------------------------------------------------------------------- |
|---|
| 9 | |
|---|
| 10 | #include "Box.h" |
|---|
| 11 | #include "RollingChecksum.h" |
|---|
| 12 | |
|---|
| 13 | #include "MemLeakFindOn.h" |
|---|
| 14 | |
|---|
| 15 | // -------------------------------------------------------------------------- |
|---|
| 16 | // |
|---|
| 17 | // Function |
|---|
| 18 | // Name: RollingChecksum::RollingChecksum(const void *, unsigned int) |
|---|
| 19 | // Purpose: Constructor -- does initial computation of the checksum. |
|---|
| 20 | // Created: 6/12/03 |
|---|
| 21 | // |
|---|
| 22 | // -------------------------------------------------------------------------- |
|---|
| 23 | RollingChecksum::RollingChecksum(const void * const data, const unsigned int Length) |
|---|
| 24 | : a(0), |
|---|
| 25 | b(0) |
|---|
| 26 | { |
|---|
| 27 | const uint8_t *block = (const uint8_t *)data; |
|---|
| 28 | for(unsigned int x = Length; x >= 1; --x) |
|---|
| 29 | { |
|---|
| 30 | a += (*block); |
|---|
| 31 | b += x * (*block); |
|---|
| 32 | |
|---|
| 33 | ++block; |
|---|
| 34 | } |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | // -------------------------------------------------------------------------- |
|---|
| 38 | // |
|---|
| 39 | // Function |
|---|
| 40 | // Name: RollingChecksum::RollForwardSeveral(uint8_t*, uint8_t*, unsigned int, unsigned int) |
|---|
| 41 | // Purpose: Move the checksum forward a block, given a pointer to the first byte of the current block, |
|---|
| 42 | // and a pointer just after the last byte of the current block and the length of the block and of the skip. |
|---|
| 43 | // Created: 7/14/05 |
|---|
| 44 | // |
|---|
| 45 | // -------------------------------------------------------------------------- |
|---|
| 46 | void RollingChecksum::RollForwardSeveral(const uint8_t * const StartOfThisBlock, const uint8_t * const LastOfNextBlock, const unsigned int Length, const unsigned int Skip) |
|---|
| 47 | { |
|---|
| 48 | // IMPLEMENTATION NOTE: Everything is implicitly mod 2^16 -- uint16_t's will overflow nicely. |
|---|
| 49 | unsigned int i; |
|---|
| 50 | uint16_t sumBegin=0, j,k; |
|---|
| 51 | |
|---|
| 52 | for(i=0; i < Skip; i++) |
|---|
| 53 | { |
|---|
| 54 | j = StartOfThisBlock[i]; |
|---|
| 55 | k = LastOfNextBlock[i]; |
|---|
| 56 | sumBegin += j; |
|---|
| 57 | a += (k - j); |
|---|
| 58 | b += a; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | b -= Length * sumBegin; |
|---|
| 62 | } |
|---|