/Users/deen/code/yugabyte-db/src/yb/rocksdb/util/crc32c.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
2 | | // This source code is licensed under the BSD-style license found in the |
3 | | // LICENSE file in the root directory of this source tree. An additional grant |
4 | | // of patent rights can be found in the PATENTS file in the same directory. |
5 | | // |
6 | | // The following only applies to changes made to this file as part of YugaByte development. |
7 | | // |
8 | | // Portions Copyright (c) YugaByte, Inc. |
9 | | // |
10 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
11 | | // in compliance with the License. You may obtain a copy of the License at |
12 | | // |
13 | | // http://www.apache.org/licenses/LICENSE-2.0 |
14 | | // |
15 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
16 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
17 | | // or implied. See the License for the specific language governing permissions and limitations |
18 | | // under the License. |
19 | | // |
20 | | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. |
21 | | // Use of this source code is governed by a BSD-style license that can be |
22 | | // found in the LICENSE file. See the AUTHORS file for names of contributors. |
23 | | #ifndef ROCKSDB_UTIL_CRC32C_H |
24 | | #define ROCKSDB_UTIL_CRC32C_H |
25 | | |
26 | | #pragma once |
27 | | #include <stddef.h> |
28 | | #include <stdint.h> |
29 | | |
30 | | namespace rocksdb { |
31 | | namespace crc32c { |
32 | | |
33 | | extern bool IsFastCrc32Supported(); |
34 | | |
35 | | // Return the crc32c of concat(A, data[0,n-1]) where init_crc is the |
36 | | // crc32c of some string A. Extend() is often used to maintain the |
37 | | // crc32c of a stream of data. |
38 | | uint32_t Extend(uint32_t init_crc, const char* data, size_t n); |
39 | | |
40 | 0 | inline uint32_t Extend(uint32_t crc, const uint8_t* buf, size_t size) { |
41 | 0 | return Extend(crc, reinterpret_cast<const char*>(buf), size); |
42 | 0 | } |
43 | | |
44 | | // Return the crc32c of data[0,n-1] |
45 | 21.3M | inline uint32_t Value(const char* data, size_t n) { |
46 | 21.3M | return Extend(0, data, n); |
47 | 21.3M | } |
48 | | |
49 | 7.26M | inline uint32_t Value(const uint8_t* data, size_t n) { |
50 | 7.26M | return Value(reinterpret_cast<const char*>(data), n); |
51 | 7.26M | } |
52 | | |
53 | | static const uint32_t kMaskDelta = 0xa282ead8ul; |
54 | | |
55 | | // Return a masked representation of crc. |
56 | | // |
57 | | // Motivation: it is problematic to compute the CRC of a string that |
58 | | // contains embedded CRCs. Therefore we recommend that CRCs stored |
59 | | // somewhere (e.g., in files) should be masked before being stored. |
60 | 23.9M | inline uint32_t Mask(uint32_t crc) { |
61 | | // Rotate right by 15 bits and add a constant. |
62 | 23.9M | return ((crc >> 15) | (crc << 17)) + kMaskDelta; |
63 | 23.9M | } |
64 | | |
65 | | // Return the crc whose masked representation is masked_crc. |
66 | 7.82M | inline uint32_t Unmask(uint32_t masked_crc) { |
67 | 7.82M | uint32_t rot = masked_crc - kMaskDelta; |
68 | 7.82M | return ((rot >> 17) | (rot << 15)); |
69 | 7.82M | } |
70 | | |
71 | | } // namespace crc32c |
72 | | } // namespace rocksdb |
73 | | |
74 | | #endif // ROCKSDB_UTIL_CRC32C_H |