/src/rocksdb/util/crc32c.h
Line | Count | Source |
1 | | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
2 | | // This source code is licensed under both the GPLv2 (found in the |
3 | | // COPYING file in the root directory) and Apache 2.0 License |
4 | | // (found in the LICENSE.Apache file in the root directory). |
5 | | // |
6 | | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. |
7 | | // Use of this source code is governed by a BSD-style license that can be |
8 | | // found in the LICENSE file. See the AUTHORS file for names of contributors. |
9 | | |
10 | | #pragma once |
11 | | #include <stddef.h> |
12 | | #include <stdint.h> |
13 | | |
14 | | #include <string> |
15 | | |
16 | | #include "rocksdb/rocksdb_namespace.h" |
17 | | |
18 | | namespace ROCKSDB_NAMESPACE { |
19 | | namespace crc32c { |
20 | | |
21 | | std::string IsFastCrc32Supported(); |
22 | | |
23 | | // Return the crc32c of concat(A, data[0,n-1]) where init_crc is the |
24 | | // crc32c of some string A. Extend() is often used to maintain the |
25 | | // crc32c of a stream of data. |
26 | | uint32_t Extend(uint32_t init_crc, const char* data, size_t n); |
27 | | |
28 | | // Takes two unmasked crc32c values, and the length of the string from |
29 | | // which `crc2` was computed, and computes a crc32c value for the |
30 | | // concatenation of the original two input strings. Running time is |
31 | | // ~ log(crc2len). |
32 | | uint32_t Crc32cCombine(uint32_t crc1, uint32_t crc2, size_t crc2len); |
33 | | |
34 | | // Return the crc32c of data[0,n-1] |
35 | 6.13M | inline uint32_t Value(const char* data, size_t n) { return Extend(0, data, n); } |
36 | | |
37 | | static const uint32_t kMaskDelta = 0xa282ead8ul; |
38 | | |
39 | | // Return a masked representation of crc. |
40 | | // |
41 | | // Motivation: it is problematic to compute the CRC of a string that |
42 | | // contains embedded CRCs. Therefore we recommend that CRCs stored |
43 | | // somewhere (e.g., in files) should be masked before being stored. |
44 | 221k | inline uint32_t Mask(uint32_t crc) { |
45 | | // Rotate right by 15 bits and add a constant. |
46 | 221k | return ((crc >> 15) | (crc << 17)) + kMaskDelta; |
47 | 221k | } |
48 | | |
49 | | // Return the crc whose masked representation is masked_crc. |
50 | 188k | inline uint32_t Unmask(uint32_t masked_crc) { |
51 | 188k | uint32_t rot = masked_crc - kMaskDelta; |
52 | 188k | return ((rot >> 17) | (rot << 15)); |
53 | 188k | } |
54 | | |
55 | | } // namespace crc32c |
56 | | } // namespace ROCKSDB_NAMESPACE |