xref: /aosp_15_r20/external/leveldb/util/crc32c.h (revision 9507f98c5f32dee4b5f9e4a38cd499f3ff5c4490)
1*9507f98cSAndroid Build Coastguard Worker // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2*9507f98cSAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*9507f98cSAndroid Build Coastguard Worker // found in the LICENSE file. See the AUTHORS file for names of contributors.
4*9507f98cSAndroid Build Coastguard Worker 
5*9507f98cSAndroid Build Coastguard Worker #ifndef STORAGE_LEVELDB_UTIL_CRC32C_H_
6*9507f98cSAndroid Build Coastguard Worker #define STORAGE_LEVELDB_UTIL_CRC32C_H_
7*9507f98cSAndroid Build Coastguard Worker 
8*9507f98cSAndroid Build Coastguard Worker #include <cstddef>
9*9507f98cSAndroid Build Coastguard Worker #include <cstdint>
10*9507f98cSAndroid Build Coastguard Worker 
11*9507f98cSAndroid Build Coastguard Worker namespace leveldb {
12*9507f98cSAndroid Build Coastguard Worker namespace crc32c {
13*9507f98cSAndroid Build Coastguard Worker 
14*9507f98cSAndroid Build Coastguard Worker // Return the crc32c of concat(A, data[0,n-1]) where init_crc is the
15*9507f98cSAndroid Build Coastguard Worker // crc32c of some string A.  Extend() is often used to maintain the
16*9507f98cSAndroid Build Coastguard Worker // crc32c of a stream of data.
17*9507f98cSAndroid Build Coastguard Worker uint32_t Extend(uint32_t init_crc, const char* data, size_t n);
18*9507f98cSAndroid Build Coastguard Worker 
19*9507f98cSAndroid Build Coastguard Worker // Return the crc32c of data[0,n-1]
Value(const char * data,size_t n)20*9507f98cSAndroid Build Coastguard Worker inline uint32_t Value(const char* data, size_t n) { return Extend(0, data, n); }
21*9507f98cSAndroid Build Coastguard Worker 
22*9507f98cSAndroid Build Coastguard Worker static const uint32_t kMaskDelta = 0xa282ead8ul;
23*9507f98cSAndroid Build Coastguard Worker 
24*9507f98cSAndroid Build Coastguard Worker // Return a masked representation of crc.
25*9507f98cSAndroid Build Coastguard Worker //
26*9507f98cSAndroid Build Coastguard Worker // Motivation: it is problematic to compute the CRC of a string that
27*9507f98cSAndroid Build Coastguard Worker // contains embedded CRCs.  Therefore we recommend that CRCs stored
28*9507f98cSAndroid Build Coastguard Worker // somewhere (e.g., in files) should be masked before being stored.
Mask(uint32_t crc)29*9507f98cSAndroid Build Coastguard Worker inline uint32_t Mask(uint32_t crc) {
30*9507f98cSAndroid Build Coastguard Worker   // Rotate right by 15 bits and add a constant.
31*9507f98cSAndroid Build Coastguard Worker   return ((crc >> 15) | (crc << 17)) + kMaskDelta;
32*9507f98cSAndroid Build Coastguard Worker }
33*9507f98cSAndroid Build Coastguard Worker 
34*9507f98cSAndroid Build Coastguard Worker // Return the crc whose masked representation is masked_crc.
Unmask(uint32_t masked_crc)35*9507f98cSAndroid Build Coastguard Worker inline uint32_t Unmask(uint32_t masked_crc) {
36*9507f98cSAndroid Build Coastguard Worker   uint32_t rot = masked_crc - kMaskDelta;
37*9507f98cSAndroid Build Coastguard Worker   return ((rot >> 17) | (rot << 15));
38*9507f98cSAndroid Build Coastguard Worker }
39*9507f98cSAndroid Build Coastguard Worker 
40*9507f98cSAndroid Build Coastguard Worker }  // namespace crc32c
41*9507f98cSAndroid Build Coastguard Worker }  // namespace leveldb
42*9507f98cSAndroid Build Coastguard Worker 
43*9507f98cSAndroid Build Coastguard Worker #endif  // STORAGE_LEVELDB_UTIL_CRC32C_H_
44