1*9356374aSAndroid Build Coastguard Worker // Copyright 2022 The Abseil Authors. 2*9356374aSAndroid Build Coastguard Worker // 3*9356374aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License"); 4*9356374aSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License. 5*9356374aSAndroid Build Coastguard Worker // You may obtain a copy of the License at 6*9356374aSAndroid Build Coastguard Worker // 7*9356374aSAndroid Build Coastguard Worker // https://www.apache.org/licenses/LICENSE-2.0 8*9356374aSAndroid Build Coastguard Worker // 9*9356374aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software 10*9356374aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS, 11*9356374aSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*9356374aSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and 13*9356374aSAndroid Build Coastguard Worker // limitations under the License. 14*9356374aSAndroid Build Coastguard Worker 15*9356374aSAndroid Build Coastguard Worker #ifndef ABSL_CRC_INTERNAL_CRC_H_ 16*9356374aSAndroid Build Coastguard Worker #define ABSL_CRC_INTERNAL_CRC_H_ 17*9356374aSAndroid Build Coastguard Worker 18*9356374aSAndroid Build Coastguard Worker #include <cstdint> 19*9356374aSAndroid Build Coastguard Worker 20*9356374aSAndroid Build Coastguard Worker #include "absl/base/config.h" 21*9356374aSAndroid Build Coastguard Worker 22*9356374aSAndroid Build Coastguard Worker // This class implements CRCs (aka Rabin Fingerprints). 23*9356374aSAndroid Build Coastguard Worker // Treats the input as a polynomial with coefficients in Z(2), 24*9356374aSAndroid Build Coastguard Worker // and finds the remainder when divided by an primitive polynomial 25*9356374aSAndroid Build Coastguard Worker // of the appropriate length. 26*9356374aSAndroid Build Coastguard Worker 27*9356374aSAndroid Build Coastguard Worker // A polynomial is represented by the bit pattern formed by its coefficients, 28*9356374aSAndroid Build Coastguard Worker // but with the highest order bit not stored. 29*9356374aSAndroid Build Coastguard Worker // The highest degree coefficient is stored in the lowest numbered bit 30*9356374aSAndroid Build Coastguard Worker // in the lowest addressed byte. Thus, in what follows, the highest degree 31*9356374aSAndroid Build Coastguard Worker // coefficient that is stored is in the low order bit of "lo" or "*lo". 32*9356374aSAndroid Build Coastguard Worker 33*9356374aSAndroid Build Coastguard Worker // Hardware acceleration is used when available. 34*9356374aSAndroid Build Coastguard Worker 35*9356374aSAndroid Build Coastguard Worker namespace absl { 36*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN 37*9356374aSAndroid Build Coastguard Worker namespace crc_internal { 38*9356374aSAndroid Build Coastguard Worker 39*9356374aSAndroid Build Coastguard Worker class CRC { 40*9356374aSAndroid Build Coastguard Worker public: 41*9356374aSAndroid Build Coastguard Worker virtual ~CRC(); 42*9356374aSAndroid Build Coastguard Worker 43*9356374aSAndroid Build Coastguard Worker // If "*crc" is the CRC of bytestring A, place the CRC of 44*9356374aSAndroid Build Coastguard Worker // the bytestring formed from the concatenation of A and the "length" 45*9356374aSAndroid Build Coastguard Worker // bytes at "bytes" into "*crc". 46*9356374aSAndroid Build Coastguard Worker virtual void Extend(uint32_t* crc, const void* bytes, 47*9356374aSAndroid Build Coastguard Worker size_t length) const = 0; 48*9356374aSAndroid Build Coastguard Worker 49*9356374aSAndroid Build Coastguard Worker // Equivalent to Extend(crc, bytes, length) where "bytes" 50*9356374aSAndroid Build Coastguard Worker // points to an array of "length" zero bytes. 51*9356374aSAndroid Build Coastguard Worker virtual void ExtendByZeroes(uint32_t* crc, size_t length) const = 0; 52*9356374aSAndroid Build Coastguard Worker 53*9356374aSAndroid Build Coastguard Worker // Inverse operation of ExtendByZeroes. If `crc` is the CRC value of a string 54*9356374aSAndroid Build Coastguard Worker // ending in `length` zero bytes, this returns a CRC value of that string 55*9356374aSAndroid Build Coastguard Worker // with those zero bytes removed. 56*9356374aSAndroid Build Coastguard Worker virtual void UnextendByZeroes(uint32_t* crc, size_t length) const = 0; 57*9356374aSAndroid Build Coastguard Worker 58*9356374aSAndroid Build Coastguard Worker // Apply a non-linear transformation to "*crc" so that 59*9356374aSAndroid Build Coastguard Worker // it is safe to CRC the result with the same polynomial without 60*9356374aSAndroid Build Coastguard Worker // any reduction of error-detection ability in the outer CRC. 61*9356374aSAndroid Build Coastguard Worker // Unscramble() performs the inverse transformation. 62*9356374aSAndroid Build Coastguard Worker // It is strongly recommended that CRCs be scrambled before storage or 63*9356374aSAndroid Build Coastguard Worker // transmission, and unscrambled at the other end before further manipulation. 64*9356374aSAndroid Build Coastguard Worker virtual void Scramble(uint32_t* crc) const = 0; 65*9356374aSAndroid Build Coastguard Worker virtual void Unscramble(uint32_t* crc) const = 0; 66*9356374aSAndroid Build Coastguard Worker 67*9356374aSAndroid Build Coastguard Worker // Crc32c() returns the singleton implementation of CRC for the CRC32C 68*9356374aSAndroid Build Coastguard Worker // polynomial. Returns a handle that MUST NOT be destroyed with delete. 69*9356374aSAndroid Build Coastguard Worker static CRC* Crc32c(); 70*9356374aSAndroid Build Coastguard Worker 71*9356374aSAndroid Build Coastguard Worker protected: 72*9356374aSAndroid Build Coastguard Worker CRC(); // Clients may not call constructor; use Crc32c() instead. 73*9356374aSAndroid Build Coastguard Worker 74*9356374aSAndroid Build Coastguard Worker private: 75*9356374aSAndroid Build Coastguard Worker CRC(const CRC&) = delete; 76*9356374aSAndroid Build Coastguard Worker CRC& operator=(const CRC&) = delete; 77*9356374aSAndroid Build Coastguard Worker }; 78*9356374aSAndroid Build Coastguard Worker 79*9356374aSAndroid Build Coastguard Worker } // namespace crc_internal 80*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END 81*9356374aSAndroid Build Coastguard Worker } // namespace absl 82*9356374aSAndroid Build Coastguard Worker 83*9356374aSAndroid Build Coastguard Worker #endif // ABSL_CRC_INTERNAL_CRC_H_ 84