| 1 |
#ifndef CORE_IMPL_HPP |
| 2 |
#define CORE_IMPL_HPP |
| 3 |
|
| 4 |
#include <iterator> |
| 5 |
#include <type_traits> |
| 6 |
#include <climits> |
| 7 |
#include <cstdint> |
| 8 |
|
| 9 |
#include "core.hpp" |
| 10 |
|
| 11 |
namespace bgcode { namespace core { |
| 12 |
|
| 13 |
// Max size of checksum buffer data, in bytes |
| 14 |
// Increase this value if you implement a checksum algorithm needing a bigger buffer |
| 15 |
static constexpr const size_t MAX_CHECKSUM_SIZE = 4; |
| 16 |
|
| 17 |
static constexpr const std::array<char, 4> MAGIC{ 'G', 'C', 'D', 'E' }; |
| 18 |
|
| 19 |
// Highest binary gcode file version supported. |
| 20 |
static constexpr const uint32_t VERSION = 1; |
| 21 |
|
| 22 |
template<class I, class T = I> |
| 23 |
using IntegerOnly = std::enable_if_t<std::is_integral_v<I>, T>; |
| 24 |
|
| 25 |
template<class T> |
| 26 |
using remove_cvref_t = std::remove_cv_t<std::remove_reference_t<T>>; |
| 27 |
|
| 28 |
template<class BufT> constexpr bool IsBufferType = |
| 29 |
std::is_convertible_v<remove_cvref_t<BufT>, std::byte> || |
| 30 |
std::is_convertible_v<remove_cvref_t<BufT>, unsigned char> || |
| 31 |
std::is_convertible_v<remove_cvref_t<BufT>, char>; |
| 32 |
|
| 33 |
template<class It> constexpr bool IsBufferIterator = |
| 34 |
IsBufferType< typename std::iterator_traits<It>::value_type>; |
| 35 |
|
| 36 |
template<class BufT, class T = BufT> |
| 37 |
using BufferTypeOnly = std::enable_if_t<IsBufferType<BufT>, T>; |
| 38 |
|
| 39 |
template<class It, class T = It> |
| 40 |
using BufferIteratorOnly = std::enable_if_t<IsBufferIterator<It>, T>; |
| 41 |
|
| 42 |
// For LE byte sequences only |
| 43 |
template<class IntT, class It, class = BufferIteratorOnly<It>> |
| 44 |
constexpr IntegerOnly<IntT> load_integer(It from, It to) noexcept |
| 45 |
{ |
| 46 |
IntT result{}; |
| 47 |
|
| 48 |
size_t i = 0; |
| 49 |
for (It it = from; it != to && i < sizeof(IntT); ++it) { |
| 50 |
result |= (static_cast<IntT>(*it) << (i++ * sizeof(std::byte) * CHAR_BIT)); |
| 51 |
} |
| 52 |
|
| 53 |
return result; |
| 54 |
} |
| 55 |
|
| 56 |
template<class IntT, class OutIt> |
| 57 |
constexpr BufferIteratorOnly<OutIt, void> |
| 58 |
store_integer_le(IntT value, OutIt out, size_t sz = sizeof(IntT)) |
| 59 |
{ |
| 60 |
for (size_t i = 0; i < std::min(sizeof(IntT), sz); ++i) |
| 61 |
{ |
| 62 |
*out++ = static_cast<typename std::iterator_traits<OutIt>::value_type>( |
| 63 |
(value >> (i * CHAR_BIT)) & UCHAR_MAX |
| 64 |
); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
template<class IntT, class OutIt> |
| 69 |
std::enable_if_t<!IsBufferIterator<OutIt>, void> |
| 70 |
store_integer_le(IntT value, OutIt out, size_t sz = sizeof(IntT)) |
| 71 |
{ |
| 72 |
for (size_t i = 0; i < std::min(sizeof(IntT), sz); ++i) |
| 73 |
{ |
| 74 |
*out++ = (value >> (i * CHAR_BIT)) & UCHAR_MAX; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
template<class It, class = BufferIteratorOnly<It>> |
| 79 |
static constexpr uint32_t crc32_sw(It from, It to, uint32_t crc) |
| 80 |
{ |
| 81 |
constexpr uint32_t ui32Max = 0xFFFFFFFF; |
| 82 |
constexpr uint32_t crcMagic = 0xEDB88320; |
| 83 |
|
| 84 |
uint32_t value = crc ^ ui32Max; |
| 85 |
for (auto it = from; it != to; ++it) { |
| 86 |
value ^= load_integer<uint32_t>(it, std::next(it)); |
| 87 |
for (int bit = 0; bit < CHAR_BIT; bit++) { |
| 88 |
if (value & 1) |
| 89 |
value = (value >> 1) ^ crcMagic; |
| 90 |
else |
| 91 |
value >>= 1; |
| 92 |
} |
| 93 |
} |
| 94 |
value ^= ui32Max; |
| 95 |
|
| 96 |
return value; |
| 97 |
} |
| 98 |
|
| 99 |
template<class Enum> |
| 100 |
constexpr auto to_underlying(Enum enumval) noexcept |
| 101 |
{ |
| 102 |
return static_cast<std::underlying_type_t<Enum>>(enumval); |
| 103 |
} |
| 104 |
|
| 105 |
class BGCODE_CORE_EXPORT Checksum |
| 106 |
{ |
| 107 |
public: |
| 108 |
// Constructs a checksum of the given type. |
| 109 |
// The checksum data are sized accordingly. |
| 110 |
explicit Checksum(EChecksumType type); |
| 111 |
|
| 112 |
EChecksumType get_type() const noexcept { return m_type; } |
| 113 |
|
| 114 |
// Append vector of data to checksum |
| 115 |
void append(const std::vector<std::byte>& data); |
| 116 |
|
| 117 |
// Append data to the checksum |
| 118 |
template<class BufT> |
| 119 |
void append(const BufT* data, size_t size); |
| 120 |
|
| 121 |
// Append any aritmetic data to the checksum (shorthand for aritmetic types) |
| 122 |
template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type> |
| 123 |
void append(T& data) { append(reinterpret_cast<const std::byte*>(&data), sizeof(data)); } |
| 124 |
|
| 125 |
// Returns true if the given checksum is equal to this one |
| 126 |
bool matches(Checksum& other); |
| 127 |
|
| 128 |
EResult write(FILE& file); |
| 129 |
EResult read(FILE& file); |
| 130 |
|
| 131 |
private: |
| 132 |
EChecksumType m_type; |
| 133 |
// actual size of checksum buffer, type dependent |
| 134 |
size_t m_size; |
| 135 |
std::array<std::byte, MAX_CHECKSUM_SIZE> m_checksum; |
| 136 |
}; |
| 137 |
|
| 138 |
// Updates the given checksum with the data of this BlockHeader |
| 139 |
inline void update_checksum(Checksum& checksum, const BlockHeader &block_header) |
| 140 |
{ |
| 141 |
checksum.append(block_header.type); |
| 142 |
checksum.append(block_header.compression); |
| 143 |
checksum.append(block_header.uncompressed_size); |
| 144 |
if (block_header.compression != to_underlying(ECompressionType::None)) |
| 145 |
checksum.append(block_header.compressed_size); |
| 146 |
} |
| 147 |
|
| 148 |
template<class BufT> |
| 149 |
void Checksum::append(const BufT *data, size_t size) |
| 150 |
{ |
| 151 |
if (data == nullptr || size == 0) |
| 152 |
return; |
| 153 |
|
| 154 |
switch (m_type) |
| 155 |
{ |
| 156 |
case EChecksumType::None: |
| 157 |
{ |
| 158 |
break; |
| 159 |
} |
| 160 |
case EChecksumType::CRC32: |
| 161 |
{ |
| 162 |
static_assert(sizeof(m_checksum) >= sizeof(uint32_t), "CRC32 checksum requires at least 4 bytes"); |
| 163 |
const auto old_crc = load_integer<uint32_t>(m_checksum.begin(), m_checksum.end()); //*(uint32_t*)m_checksum.data(); |
| 164 |
const uint32_t new_crc = crc32_sw(data, data + size, old_crc); |
| 165 |
store_integer_le(new_crc, m_checksum.begin(), m_checksum.size()); |
| 166 |
break; |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
static constexpr auto MAGICi32 = load_integer<uint32_t>(std::begin(MAGIC), std::end(MAGIC)); |
| 172 |
|
| 173 |
constexpr auto checksum_types_count() noexcept { auto v = to_underlying(EChecksumType::CRC32); ++v; return v;} |
| 174 |
constexpr auto block_types_count() noexcept { auto v = to_underlying(EBlockType::Thumbnail); ++v; return v; } |
| 175 |
constexpr auto compression_types_count() noexcept { auto v = to_underlying(ECompressionType::Heatshrink_12_4); ++v; return v; } |
| 176 |
|
| 177 |
} // namespace core |
| 178 |
} // namespace bgcode |
| 179 |
|
| 180 |
#endif // CORE_IMPL_HPP |