SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
Checksum.h
Go to the documentation of this file.
1 /*
2  * SWC-DB© Copyright since 2019 Alex Kashirin <kashirin.alex@gmail.com>
3  * License details at <https://github.com/kashirin-alex/swc-db/#license>
4  */
5 
6 
7 #ifndef swcdb_core_Checksum_h
8 #define swcdb_core_Checksum_h
9 
10 #include "swcdb/core/Compat.h"
12 
13 
14 
15 namespace SWC { namespace Core {
16 
17 
18 
19 // Fletcher 8-bit by 16-bit implementation (32-bit checksum)
20 static
21 uint32_t fletcher32(const uint8_t* data, size_t len) noexcept
22  SWC_ATTRIBS((SWC_ATTRIB_O3));
23 
24 SWC_SHOULD_NOT_INLINE
25 static
26 uint32_t fletcher32(const uint8_t* data, size_t len) noexcept {
27  uint32_t c0 = 0, c1 = 0;
28  for(uint16_t i; len >= 720; len -= 720) {
29  for(i = 360; i; --i, ++data) {
30  c0 += uint16_t(*data) << 8;
31  c1 += (c0 += *++data);
32  }
33  c0 %= 65535;
34  c1 %= 65535;
35  }
36 
37  if(len) {
38  for(;len > 1; ++data, len-=2) {
39  c0 += uint16_t(*data) << 8;
40  c1 += (c0 += *++data);
41  }
42  if(len)
43  c1 += (c0 += *data);
44  c0 %= 65535;
45  c1 %= 65535;
46  }
47  return (c1 << 16 | c0);
48 }
49 
50 
51 uint32_t checksum32(const uint8_t* data8, size_t len8) noexcept
52  SWC_ATTRIBS((SWC_ATTRIB_O3));
53 
54 extern SWC_CAN_INLINE
55 uint32_t checksum32(const uint8_t* data8, size_t len8) noexcept {
56  return fletcher32(data8, len8);
57 }
58 
59 
60 
61 extern SWC_CAN_INLINE
62 void checksum_i32(const uint8_t* start, size_t len,
63  uint8_t** ptr) noexcept {
64  Serialization::encode_i32(ptr, checksum32(start, len));
65 }
66 
67 extern SWC_CAN_INLINE
68 void checksum_i32(const uint8_t* start, const uint8_t* end,
69  uint8_t** ptr) noexcept {
70  Serialization::encode_i32(ptr, checksum32(start, end-start));
71 }
72 
73 
74 extern SWC_CAN_INLINE
75 void checksum_i32(const uint8_t* start, size_t len, uint8_t** ptr,
76  uint32_t& checksum) noexcept {
77  Serialization::encode_i32(ptr, checksum = checksum32(start, len));
78 }
79 
80 extern SWC_CAN_INLINE
81 void checksum_i32(const uint8_t* start, const uint8_t* end, uint8_t** ptr,
82  uint32_t& checksum) noexcept {
83  Serialization::encode_i32(ptr, checksum = checksum32(start, end-start));
84 }
85 
86 
87 
88 bool checksum_i32_log_chk(uint32_t checksum, const uint8_t* base,
89  uint32_t len);
90 
91 
92 
93 extern SWC_CAN_INLINE
94 bool checksum_i32_chk(uint32_t checksum, const uint8_t* base, uint32_t len) {
95  return checksum == checksum32(base, len);
96 }
97 
98 extern SWC_CAN_INLINE
99 bool checksum_i32_chk(uint32_t checksum, const uint8_t* base, uint32_t len,
100  uint32_t* computed) {
101  return checksum == (*computed = checksum32(base, len));
102 }
103 
104 extern SWC_CAN_INLINE
105 bool checksum_i32_chk(uint32_t checksum, const uint8_t* base, uint32_t len,
106  uint32_t offset) {
107  memset(const_cast<uint8_t*>(base + offset), 0, 4);
108  return checksum_i32_chk(checksum, base, len);
109 }
110 
111 
112 
113 }} // namespace SWC::Core
114 
115 
116 #ifdef SWC_IMPL_SOURCE
117 #include "swcdb/core/Checksum.cc"
118 #endif
119 
120 #endif // swcdb_core_Checksum_h
SWC_ATTRIBS
#define SWC_ATTRIBS(attrs)
Definition: Compat.h:53
checksum
uint32_t checksum
Header checksum (excl. it self)
Definition: Header.h:59
data
T data
Definition: BitFieldInt.h:1
SWC::Core::fletcher32
static uint32_t fletcher32(const uint8_t *data, size_t len) noexcept SWC_ATTRIBS((SWC_ATTRIB_O3))
Definition: Checksum.h:26
SWC::Serialization::encode_i32
SWC_CAN_INLINE void encode_i32(uint8_t **bufp, uint32_t val) noexcept
Definition: Serialization.h:138
SWC::Core::checksum_i32_chk
SWC_CAN_INLINE bool checksum_i32_chk(uint32_t checksum, const uint8_t *base, uint32_t len)
Definition: Checksum.h:94
SWC_CAN_INLINE
#define SWC_CAN_INLINE
Definition: Compat.h:102
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
Compat.h
SWC::Core::checksum_i32_log_chk
bool checksum_i32_log_chk(uint32_t checksum, const uint8_t *base, uint32_t len)
Definition: Checksum.cc:14
Serialization.h
SWC::Core::checksum32
uint32_t checksum32(const uint8_t *data8, size_t len8) noexcept SWC_ATTRIBS((SWC_ATTRIB_O3))
Definition: Checksum.h:55
Checksum.cc
SWC::Core::checksum_i32
SWC_CAN_INLINE void checksum_i32(const uint8_t *start, size_t len, uint8_t **ptr) noexcept
Definition: Checksum.h:62