/src/brpc/src/butil/crc32c.h
Line | Count | Source |
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 | | #ifndef BUTIL_CRC32C_H |
11 | | #define BUTIL_CRC32C_H |
12 | | |
13 | | #include <stddef.h> |
14 | | #include <stdint.h> |
15 | | |
16 | | namespace butil { |
17 | | namespace crc32c { |
18 | | |
19 | | extern bool IsFastCrc32Supported(); |
20 | | |
21 | | // Return the crc32c of concat(A, data[0,n-1]) where init_crc is the |
22 | | // crc32c of some string A. Extend() is often used to maintain the |
23 | | // crc32c of a stream of data. |
24 | | extern uint32_t Extend(uint32_t init_crc, const char* data, size_t n); |
25 | | |
26 | | // Return the crc32c of data[0,n-1] |
27 | 0 | inline uint32_t Value(const char* data, size_t n) { |
28 | 0 | return Extend(0, data, n); |
29 | 0 | } |
30 | | |
31 | | static const uint32_t kMaskDelta = 0xa282ead8ul; |
32 | | |
33 | | // Return a masked representation of crc. |
34 | | // |
35 | | // Motivation: it is problematic to compute the CRC of a string that |
36 | | // contains embedded CRCs. Therefore we recommend that CRCs stored |
37 | | // somewhere (e.g., in files) should be masked before being stored. |
38 | 0 | inline uint32_t Mask(uint32_t crc) { |
39 | | // Rotate right by 15 bits and add a constant. |
40 | 0 | return ((crc >> 15) | (crc << 17)) + kMaskDelta; |
41 | 0 | } |
42 | | |
43 | | // Return the crc whose masked representation is masked_crc. |
44 | 0 | inline uint32_t Unmask(uint32_t masked_crc) { |
45 | 0 | uint32_t rot = masked_crc - kMaskDelta; |
46 | 0 | return ((rot >> 17) | (rot << 15)); |
47 | 0 | } |
48 | | |
49 | | } // namespace crc32c |
50 | | } // namespace butil |
51 | | |
52 | | #endif // BUTIL_CRC32C_H |