1
/* Copyright 2017 The CRC32C Authors. All rights reserved.
2
   Use of this source code is governed by a BSD-style license that can be
3
   found in the LICENSE file. See the AUTHORS file for names of contributors. */
4

            
5
#ifndef CRC32C_CRC32C_H_
6
#define CRC32C_CRC32C_H_
7

            
8
/* The API exported by the CRC32C project. */
9

            
10
#if defined(__cplusplus)
11

            
12
#include <cstddef>
13
#include <cstdint>
14
#include <string>
15

            
16
#else  /* !defined(__cplusplus) */
17

            
18
#include <stddef.h>
19
#include <stdint.h>
20

            
21
#endif  /* !defined(__cplusplus) */
22

            
23

            
24
/* The C API. */
25

            
26
#if defined(__cplusplus)
27
extern "C" {
28
#endif  /* defined(__cplusplus) */
29

            
30
/* Extends "crc" with the CRC32C of "count" bytes in the buffer pointed by
31
   "data" */
32
uint32_t crc32c_extend(uint32_t crc, const uint8_t* data, size_t count);
33

            
34
/* Computes the CRC32C of "count" bytes in the buffer pointed by "data". */
35
uint32_t crc32c_value(const uint8_t* data, size_t count);
36

            
37
#ifdef __cplusplus
38
}  /* end extern "C" */
39
#endif  /* defined(__cplusplus) */
40

            
41

            
42
/* The C++ API. */
43

            
44
#if defined(__cplusplus)
45

            
46
namespace crc32c {
47

            
48
// Extends "crc" with the CRC32C of "count" bytes in the buffer pointed by
49
// "data".
50
uint32_t Extend(uint32_t crc, const uint8_t* data, size_t count);
51

            
52
// Computes the CRC32C of "count" bytes in the buffer pointed by "data".
53
inline uint32_t Crc32c(const uint8_t* data, size_t count) {
54
  return Extend(0, data, count);
55
}
56

            
57
// Computes the CRC32C of "count" bytes in the buffer pointed by "data".
58
inline uint32_t Crc32c(const char* data, size_t count) {
59
  return Extend(0, reinterpret_cast<const uint8_t*>(data), count);
60
}
61

            
62
// Computes the CRC32C of the string's content.
63
inline uint32_t Crc32c(const std::string& string) {
64
  return Crc32c(reinterpret_cast<const uint8_t*>(string.data()),
65
                string.size());
66
}
67

            
68
}  // namespace crc32c
69

            
70
#if __cplusplus > 201402L
71
#if __has_include(<string_view>)
72
#include <string_view>
73

            
74
namespace crc32c {
75

            
76
// Computes the CRC32C of the bytes in the string_view.
77
inline uint32_t Crc32c(const std::string_view& string_view) {
78
  return Crc32c(reinterpret_cast<const uint8_t*>(string_view.data()),
79
                string_view.size());
80
}
81

            
82
}  // namespace crc32c
83

            
84
#endif  // __has_include(<string_view>)
85
#endif  // __cplusplus > 201402L
86

            
87
#endif  /* defined(__cplusplus) */
88

            
89
#endif  // CRC32C_CRC32C_H_