#ifndef CORE_IMPL_HPP #define CORE_IMPL_HPP #include #include #include #include #include "core.hpp" namespace bgcode { namespace core { // Max size of checksum buffer data, in bytes // Increase this value if you implement a checksum algorithm needing a bigger buffer static constexpr const size_t MAX_CHECKSUM_SIZE = 4; static constexpr const std::array MAGIC{ 'G', 'C', 'D', 'E' }; // Highest binary gcode file version supported. static constexpr const uint32_t VERSION = 1; template using IntegerOnly = std::enable_if_t, T>; template using remove_cvref_t = std::remove_cv_t>; template constexpr bool IsBufferType = std::is_convertible_v, std::byte> || std::is_convertible_v, unsigned char> || std::is_convertible_v, char>; template constexpr bool IsBufferIterator = IsBufferType< typename std::iterator_traits::value_type>; template using BufferTypeOnly = std::enable_if_t, T>; template using BufferIteratorOnly = std::enable_if_t, T>; // For LE byte sequences only template> constexpr IntegerOnly load_integer(It from, It to) noexcept { IntT result{}; size_t i = 0; for (It it = from; it != to && i < sizeof(IntT); ++it) { result |= (static_cast(*it) << (i++ * sizeof(std::byte) * CHAR_BIT)); } return result; } template constexpr BufferIteratorOnly store_integer_le(IntT value, OutIt out, size_t sz = sizeof(IntT)) { for (size_t i = 0; i < std::min(sizeof(IntT), sz); ++i) { *out++ = static_cast::value_type>( (value >> (i * CHAR_BIT)) & UCHAR_MAX ); } } template std::enable_if_t, void> store_integer_le(IntT value, OutIt out, size_t sz = sizeof(IntT)) { for (size_t i = 0; i < std::min(sizeof(IntT), sz); ++i) { *out++ = (value >> (i * CHAR_BIT)) & UCHAR_MAX; } } template> static constexpr uint32_t crc32_sw(It from, It to, uint32_t crc) { constexpr uint32_t ui32Max = 0xFFFFFFFF; constexpr uint32_t crcMagic = 0xEDB88320; uint32_t value = crc ^ ui32Max; for (auto it = from; it != to; ++it) { value ^= load_integer(it, std::next(it)); for (int bit = 0; bit < CHAR_BIT; bit++) { if (value & 1) value = (value >> 1) ^ crcMagic; else value >>= 1; } } value ^= ui32Max; return value; } template constexpr auto to_underlying(Enum enumval) noexcept { return static_cast>(enumval); } class BGCODE_CORE_EXPORT Checksum { public: // Constructs a checksum of the given type. // The checksum data are sized accordingly. explicit Checksum(EChecksumType type); EChecksumType get_type() const noexcept { return m_type; } // Append vector of data to checksum void append(const std::vector& data); // Append data to the checksum template void append(const BufT* data, size_t size); // Append any aritmetic data to the checksum (shorthand for aritmetic types) template::value, T>::type> void append(T& data); // Returns true if the given checksum is equal to this one bool matches(Checksum& other); EResult write(FILE& file); EResult read(FILE& file); private: EChecksumType m_type; // actual size of checksum buffer, type dependent size_t m_size; std::array m_checksum; }; // Updates the given checksum with the data of this BlockHeader inline void update_checksum(Checksum& checksum, const BlockHeader &block_header) { checksum.append(block_header.type); checksum.append(block_header.compression); checksum.append(block_header.uncompressed_size); if (block_header.compression != to_underlying(ECompressionType::None)) checksum.append(block_header.compressed_size); } template void Checksum::append(const BufT *data, size_t size) { if (data == nullptr || size == 0) return; switch (m_type) { case EChecksumType::None: { break; } case EChecksumType::CRC32: { static_assert(sizeof(m_checksum) >= sizeof(uint32_t), "CRC32 checksum requires at least 4 bytes"); const auto old_crc = load_integer(m_checksum.begin(), m_checksum.end()); //*(uint32_t*)m_checksum.data(); const uint32_t new_crc = crc32_sw(data, data + size, old_crc); store_integer_le(new_crc, m_checksum.begin(), m_checksum.size()); break; } } } template void Checksum::append(T& data) { std::array temp; store_integer_le(data, temp.begin(), temp.size()); append(temp.data(), temp.size()); } static constexpr auto MAGICi32 = load_integer(std::begin(MAGIC), std::end(MAGIC)); constexpr auto checksum_types_count() noexcept { auto v = to_underlying(EChecksumType::CRC32); ++v; return v;} constexpr auto block_types_count() noexcept { auto v = to_underlying(EBlockType::Thumbnail); ++v; return v; } constexpr auto compression_types_count() noexcept { auto v = to_underlying(ECompressionType::Heatshrink_12_4); ++v; return v; } template static bool write_to_file(FILE& file, const BufT* data, size_t data_size) { const size_t wsize = fwrite(static_cast(data), 1, data_size, &file); return !ferror(&file) && wsize == data_size; } template static bool write_to_file_le(FILE& file, const T &data) { std::array temp; store_integer_le(data, temp.begin(), temp.size()); return write_to_file(file, temp.data(), temp.size()); } template static bool read_from_file(FILE& file, BufT *data, size_t data_size) { const size_t rsize = fread(static_cast(data), 1, data_size, &file); return !ferror(&file) && rsize == data_size; } template static bool read_from_file_le(FILE& file, T &data) { std::array temp; if (!read_from_file(file, temp.data(), temp.size())) { return false; } data = load_integer(temp.begin(), temp.end()); return true; } } // namespace core } // namespace bgcode #endif // CORE_IMPL_HPP