|
|
|
@@ -17,10 +17,10 @@ |
|
|
|
#ifndef DATAVISUAL_UTILS_CRC32_CRC32_H_ |
|
|
|
#define DATAVISUAL_UTILS_CRC32_CRC32_H_ |
|
|
|
|
|
|
|
#include "pybind11/pybind11.h" |
|
|
|
#include "securec/include/securec.h" |
|
|
|
#include <cstddef> |
|
|
|
#include <cstdint> |
|
|
|
#include "pybind11/pybind11.h" |
|
|
|
#include "securec.h" |
|
|
|
|
|
|
|
#define CRC_TABLE_SIZE 256 |
|
|
|
#define RIGHT_SHIFT 15 |
|
|
|
@@ -29,28 +29,19 @@ |
|
|
|
// Align n to (1 << m) byte boundary |
|
|
|
#define MEM_ALIGN(n, m) ((n + ((1 << m) - 1)) & ~((1 << m) - 1)) |
|
|
|
|
|
|
|
// check the null point, Only log it in if(): The value is null |
|
|
|
#define EXCEPT_CHECK_NULL(value) \ |
|
|
|
do { \ |
|
|
|
if (value == nullptr) { \ |
|
|
|
break; \ |
|
|
|
} \ |
|
|
|
} while (0) |
|
|
|
|
|
|
|
// implement common define function |
|
|
|
// Get the 32 bits align value |
|
|
|
inline uint32_t DecodeFixed32(const char* ptr) { |
|
|
|
uint32_t result = 0; |
|
|
|
if(EOK != memcpy_s(&result, sizeof(result), ptr, sizeof(result))) { |
|
|
|
return result; |
|
|
|
if (EOK != memcpy_s(&result, sizeof(result), ptr, sizeof(result))) { |
|
|
|
// `0` indicates that something wrong happened |
|
|
|
return 0; |
|
|
|
} |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
// Used to fetch a naturally-aligned 32-bit word in little endian byte-order |
|
|
|
inline uint32_t LE_LOAD32(const uint8_t* p) { |
|
|
|
return DecodeFixed32(reinterpret_cast<const char*>(p)); |
|
|
|
} |
|
|
|
inline uint32_t LE_LOAD32(const uint8_t* p) { return DecodeFixed32(reinterpret_cast<const char*>(p)); } |
|
|
|
|
|
|
|
// Masked for crc. |
|
|
|
static constexpr uint32_t kMaskDelta = 0xA282EAD8U; |
|
|
|
@@ -62,6 +53,10 @@ uint32_t MakeCrc32c(uint32_t init_crc, const char* data, size_t size); |
|
|
|
|
|
|
|
// A function return the crc32c value |
|
|
|
uint32_t GetMaskCrc32cValue(const char* data, size_t n) { |
|
|
|
if (data == nullptr && n > 0) { |
|
|
|
// Return early to prevent MakeCrc32c resulting in segmentfault |
|
|
|
return 0; |
|
|
|
} |
|
|
|
uint32_t crc = MakeCrc32c(0, data, n); |
|
|
|
return ((crc >> RIGHT_SHIFT) | (crc << LEFT_SHIFT)) + kMaskDelta; |
|
|
|
} |
|
|
|
|