OLD | NEW |
| (Empty) |
1 // Copyright 2003-2009 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 | |
16 #ifndef _CRC_H_ | |
17 #define _CRC_H_ | |
18 | |
19 #include "base/basictypes.h" | |
20 | |
21 namespace omaha { | |
22 | |
23 // This class implements CRCs (aka Rabin Fingerprints). | |
24 // Treats the input as a polynomial with coefficients in Z(2), | |
25 // and finds the remainder when divided by an irreducible polynomial | |
26 // of the appropriate length. | |
27 // It handles all CRC sizes from 8 to 128 bits. | |
28 // The input string is prefixed with a "1" bit, and has "degree" "0" bits | |
29 // appended to it before the remainder is found. This ensures that | |
30 // short strings are scrambled somewhat. | |
31 | |
32 // A polynomial is represented by the bit pattern formed by its coefficients, | |
33 // but with the highest order bit not stored. | |
34 // The highest degree coefficient is stored in the lowest numbered bit | |
35 // in the the lowest adderessed byte. Thus, in what follows, | |
36 // the highest degree coeficient that is stored is in the low order bit | |
37 // of "lo" or "*lo". | |
38 | |
39 // Typical usage: | |
40 // | |
41 // // prepare to do 32-bit CRCs using the default polynomial. No rolling hash. | |
42 // scoped_ptr<CRC> crc(CRC::Default(32, 0)); | |
43 // ... | |
44 // uint64 lo; // declare a lo,hi pair to hold the CRC | |
45 // uint64 hi; | |
46 // crc->Empty(&lo, &hi); // Initialize to CRC of empty string | |
47 // crc->Extend(&lo, &hi, "hello", 5); // Get CRC of "hello" | |
48 // ... | |
49 // | |
50 // // prepare to use a 32-bit rolling hash over 6 bytes | |
51 // scoped_ptr<CRC> crc(CRC::Default(32, 6)); | |
52 // ... | |
53 // uint64 lo; // declare a lo,hi pair to hold the CRC | |
54 // uint64 hi; | |
55 // crc->Empty(&lo, &hi); // Initialize to CRC of empty string | |
56 // crc->Extend(&lo, &hi, data, 6); // Get CRC of first 6 bytes | |
57 // for (int i = 6; i != sizeof (data); i++) { | |
58 // crc->Roll(&lo, &hi, data[i-6], data[i]); // Move window by one byte | |
59 // // lo,hi is CRC of bytes data[i-5...i] | |
60 // } | |
61 // ... | |
62 // | |
63 | |
64 class CRC { | |
65 public: | |
66 // Initialize all the tables for CRC's of a given bit length "degree" | |
67 // using a default polynomial of the given length. | |
68 // | |
69 // The argument "roll_length" is used by subsequent calls to | |
70 // Roll(). | |
71 // Returns a handle that MUST NOT be destroyed with delete. | |
72 // The default polynomials are those in POLYS[8...128]. | |
73 // Handles returned by Default() MUST NOT be deleted. | |
74 // Identical calls to Default() yield identical handles. | |
75 static CRC *Default(int degree, size_t roll_length); | |
76 | |
77 // Initialize all the tables for CRC's of a given bit length "degree" | |
78 // using an arbitrary CRC polynomial. | |
79 // Normally, you would use Default() instead of New()---see above. | |
80 // | |
81 // Requires that "lo,hi" contain an irreducible polynomial of degree "degree" | |
82 // Requires 8 <= degree && degree <= 128 | |
83 // Any irreducible polynomial of the correct degree will work. | |
84 // See the POLYS array for suitable irredicible polynomials. | |
85 // | |
86 // The argument "roll_length" is used by subsequent calls to | |
87 // Roll(). | |
88 // Each call to New() yeilds a pointer to a new object | |
89 // that may be deallocated with delete. | |
90 static CRC *New(uint64 lo, uint64 hi, int degree, size_t roll_length); | |
91 | |
92 virtual ~CRC(); | |
93 | |
94 // Place the CRC of the empty string in "*lo,*hi" | |
95 virtual void Empty(uint64 *lo, uint64 *hi) const = 0; | |
96 | |
97 // If "*lo,*hi" is the CRC of bytestring A, place the CRC of | |
98 // the bytestring formed from the concatenation of A and the "length" | |
99 // bytes at "bytes" into "*lo,*hi". | |
100 virtual void Extend(/*INOUT*/ uint64 *lo, /*INOUT*/ uint64 *hi, | |
101 const void *bytes, size_t length) const = 0; | |
102 | |
103 // Equivalent to Extend(lo, hi, bytes, length) where "bytes" | |
104 // points to an array of "length" zero bytes. | |
105 virtual void ExtendByZeroes(/*INOUT*/ uint64 *lo, /*INOUT*/ uint64 *hi, | |
106 size_t length) const = 0; | |
107 | |
108 // If "*lo,*hi" is the CRC of a byte string of length "roll_length" | |
109 // (which is an argument to New() and Default()) that consists of | |
110 // byte "o_byte" followed by string S, set "*lo,*hi" to the CRC of | |
111 // the string that consists of S followed by the byte "i_byte". | |
112 virtual void Roll(/*INOUT*/ uint64 *lo, /*INOUT*/ uint64 *hi, | |
113 uint8 o_byte, uint8 i_byte) const = 0; | |
114 | |
115 // POLYS[] is an array of valid triples that may be given to New() | |
116 static const struct Poly { | |
117 uint64 lo; // first half suitable CRC polynomial | |
118 uint64 hi; // second half of suitable CRC polynomial | |
119 int degree; // degree of suitable CRC polynomial | |
120 } *const POLYS; | |
121 // It is guaranteed that no two entries in POLYS[] are identical, | |
122 // that POLYS[i] cnotains a polynomial of degree i for 8 <= i <= 128, | |
123 // that POLYS[0] and POLYS[1] contains polynomials of degree 32, | |
124 // that POLYS[2] and POLYS[3] contains polynomials of degree 64, | |
125 // that POLYS[4] and POLYS[5] contains polynomials of degree 96, and | |
126 // that POLYS[6] and POLYS[7] contains polynomials of degree 128. | |
127 | |
128 static const int N_POLYS; // Number of elements in POLYS array. | |
129 | |
130 protected: | |
131 CRC(); // Clients may not call constructor; | |
132 // use Default() or New() instead. | |
133 | |
134 private: | |
135 DISALLOW_EVIL_CONSTRUCTORS(CRC); | |
136 }; | |
137 | |
138 } // namespace omaha | |
139 | |
140 #endif | |
OLD | NEW |