1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #ifndef TENSORFLOW_CORE_LIB_HASH_CRC32C_H_
17 #define TENSORFLOW_CORE_LIB_HASH_CRC32C_H_
18
19 #include <stddef.h>
20 #include "tensorflow/core/platform/types.h"
21
22 namespace tensorflow {
23 namespace crc32c {
24
25 // Return the crc32c of concat(A, data[0,n-1]) where init_crc is the
26 // crc32c of some string A. Extend() is often used to maintain the
27 // crc32c of a stream of data.
28 extern uint32 Extend(uint32 init_crc, const char* data, size_t n);
29
30 // Return the crc32c of data[0,n-1]
Value(const char * data,size_t n)31 inline uint32 Value(const char* data, size_t n) { return Extend(0, data, n); }
32
33 static const uint32 kMaskDelta = 0xa282ead8ul;
34
35 // Return a masked representation of crc.
36 //
37 // Motivation: it is problematic to compute the CRC of a string that
38 // contains embedded CRCs. Therefore we recommend that CRCs stored
39 // somewhere (e.g., in files) should be masked before being stored.
Mask(uint32 crc)40 inline uint32 Mask(uint32 crc) {
41 // Rotate right by 15 bits and add a constant.
42 return ((crc >> 15) | (crc << 17)) + kMaskDelta;
43 }
44
45 // Return the crc whose masked representation is masked_crc.
Unmask(uint32 masked_crc)46 inline uint32 Unmask(uint32 masked_crc) {
47 uint32 rot = masked_crc - kMaskDelta;
48 return ((rot >> 17) | (rot << 15));
49 }
50
51 } // namespace crc32c
52 } // namespace tensorflow
53
54 #endif // TENSORFLOW_CORE_LIB_HASH_CRC32C_H_
55