]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/util/crc32c.h
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / rocksdb / util / crc32c.h
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 // 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 namespace rocksdb {
15 namespace crc32c {
16
17 extern bool IsFastCrc32Supported();
18
19 // Return the crc32c of concat(A, data[0,n-1]) where init_crc is the
20 // crc32c of some string A. Extend() is often used to maintain the
21 // crc32c of a stream of data.
22 extern uint32_t Extend(uint32_t init_crc, const char* data, size_t n);
23
24 // Return the crc32c of data[0,n-1]
25 inline uint32_t Value(const char* data, size_t n) {
26 return Extend(0, data, n);
27 }
28
29 static const uint32_t kMaskDelta = 0xa282ead8ul;
30
31 // Return a masked representation of crc.
32 //
33 // Motivation: it is problematic to compute the CRC of a string that
34 // contains embedded CRCs. Therefore we recommend that CRCs stored
35 // somewhere (e.g., in files) should be masked before being stored.
36 inline uint32_t Mask(uint32_t crc) {
37 // Rotate right by 15 bits and add a constant.
38 return ((crc >> 15) | (crc << 17)) + kMaskDelta;
39 }
40
41 // Return the crc whose masked representation is masked_crc.
42 inline uint32_t Unmask(uint32_t masked_crc) {
43 uint32_t rot = masked_crc - kMaskDelta;
44 return ((rot >> 17) | (rot << 15));
45 }
46
47 } // namespace crc32c
48 } // namespace rocksdb