Skip to content

Commit

Permalink
Hide templated stuff from core.hpp
Browse files Browse the repository at this point in the history
Remove Checkum class from public API

Hard to maintain binary compatibility

Fix includes

Update python bindings, add cmake presets, fix GH builds

Fix python tests
  • Loading branch information
tamasmeszaros committed Nov 1, 2023
1 parent df911a7 commit f2ef752
Show file tree
Hide file tree
Showing 11 changed files with 256 additions and 219 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ deps/build*
deps/.pkg_cache
build*
CMakeLists.txt.user
CMakeUserPresets.json
pybgcode.egg-info
dist/
Testing
Expand Down
4 changes: 2 additions & 2 deletions deps/autobuild.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ endif ()

if (${PROJECT_NAME}_DEPS_PRESET STREQUAL "wasm")
list(APPEND CMAKE_FIND_ROOT_PATH ${_build_dir}/destdir/usr/local)
set(CMAKE_FIND_ROOT_PATH "${CMAKE_FIND_ROOT_PATH}" CACHE STRING "")
set(CMAKE_FIND_ROOT_PATH "${CMAKE_FIND_ROOT_PATH}" CACHE STRING "" FORCE)
else ()
list(APPEND CMAKE_PREFIX_PATH ${_build_dir}/destdir/usr/local)
set(CMAKE_PREFIX_PATH "${CMAKE_PREFIX_PATH}" CACHE STRING "")
set(CMAKE_PREFIX_PATH "${CMAKE_PREFIX_PATH}" CACHE STRING "" FORCE)
endif ()
16 changes: 16 additions & 0 deletions pybgcode/CMakePresets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"version": 3,
"configurePresets": [
{
"name": "default",
"displayName": "Default",
"description": "Default build for any desktop OS",
"binaryDir": "${sourceDir}/build-default",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release",
"CMAKE_INSTALL_PREFIX": "${sourceDir}/build/dist",
"PyBGCode_LINK_SYSTEM_LIBBGCODE": false
}
}
]
}
18 changes: 0 additions & 18 deletions pybgcode/pybgcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,20 +163,6 @@ PYBIND11_MODULE(pybgcode, m) {
.value("JPG", core::EThumbnailFormat::JPG)
.value("QOI", core::EThumbnailFormat::QOI);

py::class_<core::Checksum>(m, "Checksum")
.def(py::init<core::EChecksumType>())
.def("get_type", &core::Checksum::get_type)
.def("append", [](core::Checksum &self, const std::string &buf) {
self.append(buf.data(), buf.size());
})
.def("matches", &core::Checksum::matches)
.def("read", [](core::Checksum &self, FILEWrapper &file) {
return self.read(*file.fptr);
})
.def("write", [](core::Checksum &self, FILEWrapper &file) {
return self.write(*file.fptr);
});

py::class_<core::FileHeader>(m, "FileHeader")
.def(py::init<>())
.def_readonly("magic", &core::FileHeader::magic)
Expand All @@ -195,7 +181,6 @@ PYBIND11_MODULE(pybgcode, m) {
.def_readonly("compression", &core::BlockHeader::compression)
.def_readonly("uncompressed_size", &core::BlockHeader::type)
.def_readonly("compressed_size", &core::BlockHeader::compressed_size)
.def("update_checksum", &core::BlockHeader::update_checksum)
.def("get_size()", &core::BlockHeader::get_size)
.def("read", [](core::BlockHeader &self, FILEWrapper &file) {
return self.read(*file.fptr);
Expand Down Expand Up @@ -379,9 +364,6 @@ PYBIND11_MODULE(pybgcode, m) {
.def(py::init<>())
.def_readonly("encoding_type", &binarize::BaseMetadataBlock::encoding_type)
.def_readonly("raw_data", &binarize::BaseMetadataBlock::raw_data)
.def("write", [](binarize::BaseMetadataBlock &self, FILEWrapper &file, core::EBlockType block_type, core::ECompressionType compression_type, core::Checksum& checksum){
return self.write(*file.fptr, block_type, compression_type, checksum);
}, R"pbdoc(write block header and data in encoded format)pbdoc", py::arg("file"), py::arg("block_type"), py::arg("compression_type"), py::arg("checksum"))
.def("read_data", [](binarize::BaseMetadataBlock &self, FILEWrapper &file, const core::BlockHeader& block_header) {
return self.read_data(*file.fptr, block_header);
}, R"pbdoc(read block data in encoded format)pbdoc", py::arg("file"), py::arg("block_header"));
Expand Down
3 changes: 0 additions & 3 deletions pybgcode/tests/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ def test_main():

assert(res == pybgcode.EResult.Success)

checksum = pybgcode.Checksum(pybgcode.ChecksumType.CRC32)
assert(checksum.get_type() == pybgcode.ChecksumType.CRC32)

pybgcode.close(out_f)
pybgcode.close(in_f)

Expand Down
50 changes: 27 additions & 23 deletions src/LibBGCode/binarize/binarize.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "binarize.hpp"
#include "meatpack.hpp"

#include "core/core_impl.hpp"

extern "C" {
#include <heatshrink/heatshrink_encoder.h>
#include <heatshrink/heatshrink_decoder.h>
Expand Down Expand Up @@ -32,6 +34,14 @@ static bool read_from_file(FILE& file, T *data, size_t data_size)
return !ferror(&file) && rsize == data_size;
}

void update_checksum(Checksum& checksum, const ThumbnailBlock &th)
{
checksum.append(th.params.format);
checksum.append(th.params.width);
checksum.append(th.params.height);
checksum.append(th.data);
}

static std::vector<uint8_t> encode(const std::byte* data, size_t data_size)
{
std::vector<uint8_t> ret(data_size);
Expand Down Expand Up @@ -378,18 +388,19 @@ static bool uncompress(const std::vector<uint8_t>& src, std::vector<uint8_t>& ds
return true;
}

EResult BaseMetadataBlock::write(FILE& file, EBlockType block_type, ECompressionType compression_type,
Checksum& checksum) const

// write block header and data in encoded format
core::EResult write(const BaseMetadataBlock &block, FILE& file, core::EBlockType block_type, core::ECompressionType compression_type, core::Checksum &checksum)
{
if (encoding_type > metadata_encoding_types_count())
if (block.encoding_type > metadata_encoding_types_count())
return EResult::InvalidMetadataEncodingType;

BlockHeader block_header((uint16_t)block_type, (uint16_t)compression_type, (uint32_t)0);
std::vector<uint8_t> out_data;
if (!raw_data.empty()) {
if (!block.raw_data.empty()) {
// process payload encoding
std::vector<uint8_t> uncompressed_data;
if (!encode_metadata(raw_data, uncompressed_data, (EMetadataEncodingType)encoding_type))
if (!encode_metadata(block.raw_data, uncompressed_data, (EMetadataEncodingType)block.encoding_type))
return EResult::MetadataEncodingError;
// process payload compression
block_header.uncompressed_size = (uint32_t)uncompressed_data.size();
Expand All @@ -409,7 +420,7 @@ EResult BaseMetadataBlock::write(FILE& file, EBlockType block_type, ECompression
return res;

// write block payload
if (!write_to_file(file, &encoding_type, sizeof(encoding_type)))
if (!write_to_file(file, &block.encoding_type, sizeof(block.encoding_type)))
return EResult::WriteError;
if (!out_data.empty()) {
if (!write_to_file(file, out_data.data(), out_data.size()))
Expand All @@ -418,12 +429,13 @@ EResult BaseMetadataBlock::write(FILE& file, EBlockType block_type, ECompression

if (checksum.get_type() != EChecksumType::None) {
// update checksum with block header
block_header.update_checksum(checksum);
update_checksum(checksum, block_header);
// update checksum with block payload
checksum.append(encoding_type);
checksum.append(block.encoding_type);
if (!out_data.empty())
checksum.append(static_cast<unsigned char*>(out_data.data()), out_data.size());
}

return EResult::Success;
}

Expand Down Expand Up @@ -461,7 +473,7 @@ EResult FileMetadataBlock::write(FILE& file, ECompressionType compression_type,
Checksum cs(checksum_type);

// write block header, payload
EResult res = BaseMetadataBlock::write(file, EBlockType::FileMetadata, compression_type, cs);
EResult res = binarize::write(*this, file, EBlockType::FileMetadata, compression_type, cs);
if (res != EResult::Success)
// propagate error
return res;
Expand Down Expand Up @@ -498,7 +510,7 @@ EResult PrintMetadataBlock::write(FILE& file, ECompressionType compression_type,
Checksum cs(checksum_type);

// write block header, payload
EResult res = BaseMetadataBlock::write(file, EBlockType::PrintMetadata, compression_type, cs);
EResult res = binarize::write(*this, file, EBlockType::PrintMetadata, compression_type, cs);
if (res != EResult::Success)
// propagate error
return res;
Expand Down Expand Up @@ -535,7 +547,7 @@ EResult PrinterMetadataBlock::write(FILE& file, ECompressionType compression_typ
Checksum cs(checksum_type);

// write block header, payload
EResult res = BaseMetadataBlock::write(file, EBlockType::PrinterMetadata, compression_type, cs);
EResult res = binarize::write(*this, file, EBlockType::PrinterMetadata, compression_type, cs);
if (res != EResult::Success)
// propagate error
return res;
Expand Down Expand Up @@ -597,9 +609,9 @@ EResult ThumbnailBlock::write(FILE& file, EChecksumType checksum_type)
if (checksum_type != EChecksumType::None) {
Checksum cs(checksum_type);
// update checksum with block header
block_header.update_checksum(cs);
update_checksum(cs, block_header);
// update checksum with block payload
update_checksum(cs);
update_checksum(cs, *this);
// write block checksum
res = cs.write(file);
if (res != EResult::Success)
Expand Down Expand Up @@ -641,14 +653,6 @@ EResult ThumbnailBlock::read_data(FILE& file, const FileHeader& file_header, con
return EResult::Success;
}

void ThumbnailBlock::update_checksum(Checksum& checksum) const
{
checksum.append(params.format);
checksum.append(params.width);
checksum.append(params.height);
checksum.append(data);
}

EResult GCodeBlock::write(FILE& file, ECompressionType compression_type, EChecksumType checksum_type) const
{
if (encoding_type > gcode_encoding_types_count())
Expand Down Expand Up @@ -690,7 +694,7 @@ EResult GCodeBlock::write(FILE& file, ECompressionType compression_type, EChecks
if (checksum_type != EChecksumType::None) {
Checksum cs(checksum_type);
// update checksum with block header
block_header.update_checksum(cs);
update_checksum(cs, block_header);
// update checksum with block payload
std::vector<uint8_t> data_to_encode =
encode(reinterpret_cast<const std::byte*>(&encoding_type), sizeof(encoding_type));
Expand Down Expand Up @@ -748,7 +752,7 @@ EResult SlicerMetadataBlock::write(FILE& file, ECompressionType compression_type
Checksum cs(checksum_type);

// write block header, payload
EResult res = BaseMetadataBlock::write(file, EBlockType::SlicerMetadata, compression_type, cs);
EResult res = binarize::write(*this, file, EBlockType::SlicerMetadata, compression_type, cs);
if (res != EResult::Success)
// propagate error
return res;
Expand Down
5 changes: 0 additions & 5 deletions src/LibBGCode/binarize/binarize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ struct BGCODE_BINARIZE_EXPORT BaseMetadataBlock
// data in key/value form
std::vector<std::pair<std::string, std::string>> raw_data;

// write block header and data in encoded format
core::EResult write(FILE& file, core::EBlockType block_type, core::ECompressionType compression_type, core::Checksum& checksum) const;
// read block data in encoded format
core::EResult read_data(FILE& file, const core::BlockHeader& block_header);
};
Expand Down Expand Up @@ -52,9 +50,6 @@ struct BGCODE_BINARIZE_EXPORT ThumbnailBlock
core::EResult write(FILE& file, core::EChecksumType checksum_type);
// read block data
core::EResult read_data(FILE& file, const core::FileHeader& file_header, const core::BlockHeader& block_header);

private:
void update_checksum(core::Checksum& checksum) const;
};

struct BGCODE_BINARIZE_EXPORT GCodeBlock
Expand Down
1 change: 1 addition & 0 deletions src/LibBGCode/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set(Core_DOWNSTREAM_DEPS "")
add_library(${_libname}_core
core.cpp
core.hpp
core_impl.hpp
# Add more source files here if needed
)

Expand Down
36 changes: 18 additions & 18 deletions src/LibBGCode/core/core.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "core.hpp"
#include "core_impl.hpp"
#include <cstring>

namespace bgcode { namespace core {
Expand Down Expand Up @@ -35,7 +35,7 @@ EResult verify_block_checksum(FILE& file, const FileHeader& file_header,

Checksum curr_cs((EChecksumType)file_header.checksum_type);
// update block checksum block header
block_header.update_checksum(curr_cs);
update_checksum(curr_cs, block_header);

// read block payload
size_t remaining_payload_size = block_payload_size(block_header);
Expand All @@ -61,18 +61,12 @@ EResult verify_block_checksum(FILE& file, const FileHeader& file_header,
return EResult::Success;
}

static uint16_t checksum_types_count() { return 1 + (uint16_t)EChecksumType::CRC32; }
static uint16_t block_types_count() { return 1 + (uint16_t)EBlockType::Thumbnail; }
static uint16_t compression_types_count() { return 1 + (uint16_t)ECompressionType::Heatshrink_12_4; }

Checksum::Checksum(EChecksumType type)
: m_type(type), m_size(checksum_size(type))
{
m_checksum.fill(std::byte{0});
}

EChecksumType Checksum::get_type() const { return m_type; }

void Checksum::append(const std::vector<std::byte>& data)
{
append(data.data(), data.size());
Expand Down Expand Up @@ -101,6 +95,16 @@ EResult Checksum::read(FILE& file)
return EResult::Success;
}

FileHeader::FileHeader()
: magic{MAGICi32}
, version{VERSION}
, checksum_type{static_cast<uint16_t>(EChecksumType::None)}
{}

FileHeader::FileHeader(uint32_t mg, uint32_t ver, uint16_t chk_type)
: magic{mg}, version{ver}, checksum_type{chk_type}
{}

EResult FileHeader::write(FILE& file) const
{
if (magic != MAGICi32)
Expand Down Expand Up @@ -145,15 +149,6 @@ BlockHeader::BlockHeader(uint16_t type, uint16_t compression, uint32_t uncompres
, compressed_size(compressed_size)
{}

void BlockHeader::update_checksum(Checksum& checksum) const
{
checksum.append(type);
checksum.append(compression);
checksum.append(uncompressed_size);
if (compression != (uint16_t)ECompressionType::None)
checksum.append(compressed_size);
}

long BlockHeader::get_position() const
{
return m_position;
Expand Down Expand Up @@ -528,7 +523,12 @@ BGCODE_CORE_EXPORT size_t checksum_size(EChecksumType type)

BGCODE_CORE_EXPORT size_t block_content_size(const FileHeader& file_header, const BlockHeader& block_header)
{
return block_payload_size(block_header) + checksum_size((EChecksumType)file_header.checksum_type);
return block_payload_size(block_header) + checksum_size((EChecksumType)file_header.checksum_type);
}

uint32_t version() noexcept
{
return VERSION;
}

} // namespace core
Expand Down
Loading

0 comments on commit f2ef752

Please sign in to comment.