| 1 |
#include "binarize.hpp" |
| 2 |
#include "meatpack.hpp" |
| 3 |
|
| 4 |
#include "core/core_impl.hpp" |
| 5 |
|
| 6 |
extern "C" { |
| 7 |
#include <heatshrink/heatshrink_encoder.h> |
| 8 |
#include <heatshrink/heatshrink_decoder.h> |
| 9 |
} |
| 10 |
#include <zlib.h> |
| 11 |
|
| 12 |
#include <cstring> |
| 13 |
#include <cassert> |
| 14 |
|
| 15 |
namespace bgcode { |
| 16 |
|
| 17 |
using namespace core; |
| 18 |
|
| 19 |
namespace binarize { |
| 20 |
|
| 21 |
template<class T> |
| 22 |
static bool write_to_file(FILE& file, const T* data, size_t data_size) |
| 23 |
{ |
| 24 |
const size_t wsize = fwrite(static_cast<const void*>(data), 1, data_size, &file); |
| 25 |
return !ferror(&file) && wsize == data_size; |
| 26 |
} |
| 27 |
|
| 28 |
template<class T> |
| 29 |
static bool read_from_file(FILE& file, T *data, size_t data_size) |
| 30 |
{ |
| 31 |
static_assert(!std::is_const_v<T>, "Type of output buffer cannot be const!"); |
| 32 |
|
| 33 |
const size_t rsize = fread(static_cast<void *>(data), 1, data_size, &file); |
| 34 |
return !ferror(&file) && rsize == data_size; |
| 35 |
} |
| 36 |
|
| 37 |
void update_checksum(Checksum& checksum, const ThumbnailBlock &th) |
| 38 |
{ |
| 39 |
checksum.append(th.params.format); |
| 40 |
checksum.append(th.params.width); |
| 41 |
checksum.append(th.params.height); |
| 42 |
checksum.append(th.data); |
| 43 |
} |
| 44 |
|
| 45 |
static std::vector<uint8_t> encode(const std::byte* data, size_t data_size) |
| 46 |
{ |
| 47 |
std::vector<uint8_t> ret(data_size); |
| 48 |
memcpy(ret.data(), data, data_size); |
| 49 |
return ret; |
| 50 |
} |
| 51 |
|
| 52 |
static uint16_t metadata_encoding_types_count() { return 1 + (uint16_t)EMetadataEncodingType::JSON; } |
| 53 |
static uint16_t thumbnail_formats_count() { return 1 + (uint16_t)EThumbnailFormat::QOI; } |
| 54 |
static uint16_t gcode_encoding_types_count() { return 1 + (uint16_t)EGCodeEncodingType::MeatPackComments; } |
| 55 |
|
| 56 |
static bool encode_metadata(const std::vector<std::pair<std::string, std::string>>& src, std::vector<uint8_t>& dst, |
| 57 |
EMetadataEncodingType encoding_type) |
| 58 |
{ |
| 59 |
|
| 60 |
switch (encoding_type) |
| 61 |
{ |
| 62 |
case EMetadataEncodingType::INI: |
| 63 |
{ |
| 64 |
for (const auto& [key, value] : src) { |
| 65 |
dst.insert(dst.end(), key.begin(), key.end()); |
| 66 |
dst.emplace_back('='); |
| 67 |
dst.insert(dst.end(), value.begin(), value.end()); |
| 68 |
dst.emplace_back('\n'); |
| 69 |
} |
| 70 |
break; |
| 71 |
} |
| 72 |
case EMetadataEncodingType::JSON: |
| 73 |
{ |
| 74 |
if (src.size() != 1) |
| 75 |
return false; |
| 76 |
const auto& [k, v] = src.at(0); |
| 77 |
dst.insert(dst.end(), v.begin(), v.end()); |
| 78 |
break; |
| 79 |
} |
| 80 |
} |
| 81 |
return true; |
| 82 |
} |
| 83 |
|
| 84 |
static bool decode_metadata(const std::vector<uint8_t>& src, std::vector<std::pair<std::string, std::string>>& dst, |
| 85 |
EMetadataEncodingType encoding_type) |
| 86 |
{ |
| 87 |
switch (encoding_type) |
| 88 |
{ |
| 89 |
case EMetadataEncodingType::INI: |
| 90 |
{ |
| 91 |
auto begin_it = src.begin(); |
| 92 |
auto end_it = src.begin(); |
| 93 |
while (end_it != src.end()) { |
| 94 |
while (end_it != src.end() && *end_it != '\n') { |
| 95 |
++end_it; |
| 96 |
} |
| 97 |
const std::string item(begin_it, end_it); |
| 98 |
const size_t pos = item.find_first_of('='); |
| 99 |
if (pos != std::string::npos) { |
| 100 |
dst.emplace_back(item.substr(0, pos), item.substr(pos + 1)); |
| 101 |
begin_it = ++end_it; |
| 102 |
} |
| 103 |
} |
| 104 |
break; |
| 105 |
} |
| 106 |
case EMetadataEncodingType::JSON: |
| 107 |
{ |
| 108 |
std::string v; |
| 109 |
v.insert(v.end(), src.begin(), src.end()); |
| 110 |
dst.emplace_back("", v); |
| 111 |
break; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
return true; |
| 116 |
} |
| 117 |
|
| 118 |
static bool encode_gcode(const std::string& src, std::vector<uint8_t>& dst, EGCodeEncodingType encoding_type) |
| 119 |
{ |
| 120 |
switch (encoding_type) |
| 121 |
{ |
| 122 |
case EGCodeEncodingType::None: |
| 123 |
{ |
| 124 |
dst.insert(dst.end(), src.begin(), src.end()); |
| 125 |
break; |
| 126 |
} |
| 127 |
case EGCodeEncodingType::MeatPack: |
| 128 |
case EGCodeEncodingType::MeatPackComments: |
| 129 |
{ |
| 130 |
uint8_t binarizer_flags = (encoding_type == EGCodeEncodingType::MeatPack) ? MeatPack::Flag_RemoveComments : 0; |
| 131 |
binarizer_flags |= MeatPack::Flag_OmitWhitespaces; |
| 132 |
MeatPack::MPBinarizer binarizer(binarizer_flags); |
| 133 |
binarizer.initialize(dst); |
| 134 |
auto begin_it = src.begin(); |
| 135 |
auto end_it = src.begin(); |
| 136 |
while (end_it != src.end()) { |
| 137 |
while (end_it != src.end() && *end_it != '\n') { |
| 138 |
++end_it; |
| 139 |
} |
| 140 |
const std::string line(begin_it, ++end_it); |
| 141 |
binarizer.binarize_line(line, dst); |
| 142 |
begin_it = end_it; |
| 143 |
} |
| 144 |
binarizer.finalize(dst); |
| 145 |
break; |
| 146 |
} |
| 147 |
} |
| 148 |
return true; |
| 149 |
} |
| 150 |
|
| 151 |
static bool decode_gcode(const std::vector<uint8_t>& src, std::string& dst, EGCodeEncodingType encoding_type) |
| 152 |
{ |
| 153 |
switch (encoding_type) |
| 154 |
{ |
| 155 |
case EGCodeEncodingType::None: |
| 156 |
{ |
| 157 |
dst.insert(dst.end(), src.begin(), src.end()); |
| 158 |
break; |
| 159 |
} |
| 160 |
case EGCodeEncodingType::MeatPack: |
| 161 |
case EGCodeEncodingType::MeatPackComments: |
| 162 |
{ |
| 163 |
MeatPack::unbinarize(src, dst); |
| 164 |
break; |
| 165 |
} |
| 166 |
} |
| 167 |
return true; |
| 168 |
} |
| 169 |
|
| 170 |
static bool compress(std::vector<uint8_t>& src, std::vector<uint8_t>& dst, ECompressionType compression_type) |
| 171 |
{ |
| 172 |
switch (compression_type) |
| 173 |
{ |
| 174 |
case ECompressionType::Deflate: |
| 175 |
{ |
| 176 |
dst.clear(); |
| 177 |
|
| 178 |
const size_t BUFSIZE = 2048; |
| 179 |
std::vector<uint8_t> temp_buffer(BUFSIZE); |
| 180 |
|
| 181 |
z_stream strm{}; |
| 182 |
strm.next_in = static_cast<Bytef*>(src.data()); |
| 183 |
strm.avail_in = static_cast<uInt>(src.size()); |
| 184 |
strm.next_out = temp_buffer.data(); |
| 185 |
strm.avail_out = BUFSIZE; |
| 186 |
|
| 187 |
const int level = Z_DEFAULT_COMPRESSION; |
| 188 |
int res = deflateInit(&strm, level); |
| 189 |
if (res != Z_OK) |
| 190 |
return false; |
| 191 |
|
| 192 |
while (strm.avail_in > 0) { |
| 193 |
res = deflate(&strm, Z_NO_FLUSH); |
| 194 |
if (res != Z_OK) { |
| 195 |
deflateEnd(&strm); |
| 196 |
return false; |
| 197 |
} |
| 198 |
if (strm.avail_out == 0) { |
| 199 |
dst.insert(dst.end(), temp_buffer.data(), temp_buffer.data() + BUFSIZE); |
| 200 |
strm.next_out = temp_buffer.data(); |
| 201 |
strm.avail_out = BUFSIZE; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
int deflate_res = Z_OK; |
| 206 |
while (deflate_res == Z_OK) { |
| 207 |
if (strm.avail_out == 0) { |
| 208 |
dst.insert(dst.end(), temp_buffer.data(), temp_buffer.data() + BUFSIZE); |
| 209 |
strm.next_out = temp_buffer.data(); |
| 210 |
strm.avail_out = BUFSIZE; |
| 211 |
} |
| 212 |
deflate_res = deflate(&strm, Z_FINISH); |
| 213 |
} |
| 214 |
|
| 215 |
if (deflate_res != Z_STREAM_END) { |
| 216 |
deflateEnd(&strm); |
| 217 |
return false; |
| 218 |
} |
| 219 |
|
| 220 |
dst.insert(dst.end(), temp_buffer.data(), temp_buffer.data() + BUFSIZE - strm.avail_out); |
| 221 |
deflateEnd(&strm); |
| 222 |
break; |
| 223 |
} |
| 224 |
case ECompressionType::Heatshrink_11_4: |
| 225 |
case ECompressionType::Heatshrink_12_4: |
| 226 |
{ |
| 227 |
const uint8_t window_sz = (compression_type == ECompressionType::Heatshrink_11_4) ? 11 : 12; |
| 228 |
const uint8_t lookahead_sz = 4; |
| 229 |
heatshrink_encoder* encoder = heatshrink_encoder_alloc(window_sz, lookahead_sz); |
| 230 |
if (encoder == nullptr) |
| 231 |
return false; |
| 232 |
|
| 233 |
// calculate the maximum compressed size (assuming a conservative estimate) |
| 234 |
const size_t src_size = src.size(); |
| 235 |
const size_t max_compressed_size = src_size + (src_size >> 2); |
| 236 |
dst.resize(max_compressed_size); |
| 237 |
|
| 238 |
uint8_t* buf = src.data(); |
| 239 |
uint8_t* outbuf = dst.data(); |
| 240 |
|
| 241 |
// compress data |
| 242 |
size_t tosink = src_size; |
| 243 |
size_t output_size = 0; |
| 244 |
while (tosink > 0) { |
| 245 |
size_t sunk = 0; |
| 246 |
const HSE_sink_res sink_res = heatshrink_encoder_sink(encoder, buf, tosink, &sunk); |
| 247 |
if (sink_res != HSER_SINK_OK) { |
| 248 |
heatshrink_encoder_free(encoder); |
| 249 |
return false; |
| 250 |
} |
| 251 |
if (sunk == 0) |
| 252 |
// all input data processed |
| 253 |
break; |
| 254 |
|
| 255 |
tosink -= sunk; |
| 256 |
buf += sunk; |
| 257 |
|
| 258 |
size_t polled = 0; |
| 259 |
const HSE_poll_res poll_res = heatshrink_encoder_poll(encoder, outbuf + output_size, max_compressed_size - output_size, &polled); |
| 260 |
if (poll_res < 0) { |
| 261 |
heatshrink_encoder_free(encoder); |
| 262 |
return false; |
| 263 |
} |
| 264 |
output_size += polled; |
| 265 |
} |
| 266 |
|
| 267 |
// input data finished |
| 268 |
const HSE_finish_res finish_res = heatshrink_encoder_finish(encoder); |
| 269 |
if (finish_res < 0) { |
| 270 |
heatshrink_encoder_free(encoder); |
| 271 |
return false; |
| 272 |
} |
| 273 |
|
| 274 |
// poll for final output |
| 275 |
size_t polled = 0; |
| 276 |
const HSE_poll_res poll_res = heatshrink_encoder_poll(encoder, outbuf + output_size, max_compressed_size - output_size, &polled); |
| 277 |
if (poll_res < 0) { |
| 278 |
heatshrink_encoder_free(encoder); |
| 279 |
return false; |
| 280 |
} |
| 281 |
dst.resize(output_size + polled); |
| 282 |
heatshrink_encoder_free(encoder); |
| 283 |
break; |
| 284 |
} |
| 285 |
case ECompressionType::None: |
| 286 |
default: |
| 287 |
{ |
| 288 |
break; |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
return true; |
| 293 |
} |
| 294 |
|
| 295 |
static bool uncompress(const std::vector<uint8_t>& src, std::vector<uint8_t>& dst, ECompressionType compression_type, size_t uncompressed_size) |
| 296 |
{ |
| 297 |
switch (compression_type) |
| 298 |
{ |
| 299 |
case ECompressionType::Deflate: |
| 300 |
{ |
| 301 |
dst.clear(); |
| 302 |
dst.reserve(uncompressed_size); |
| 303 |
|
| 304 |
const size_t BUFSIZE = 2048; |
| 305 |
std::vector<uint8_t> temp_buffer(BUFSIZE); |
| 306 |
|
| 307 |
z_stream strm{}; |
| 308 |
strm.next_in = const_cast<uint8_t*>(src.data()); |
| 309 |
strm.avail_in = (uInt)src.size(); |
| 310 |
strm.next_out = temp_buffer.data(); |
| 311 |
strm.avail_out = BUFSIZE; |
| 312 |
int res = inflateInit(&strm); |
| 313 |
if (res != Z_OK) |
| 314 |
return false; |
| 315 |
|
| 316 |
while (strm.avail_in > 0) { |
| 317 |
res = inflate(&strm, Z_NO_FLUSH); |
| 318 |
if (res != Z_OK && res != Z_STREAM_END) { |
| 319 |
inflateEnd(&strm); |
| 320 |
return false; |
| 321 |
} |
| 322 |
if (strm.avail_out == 0) { |
| 323 |
dst.insert(dst.end(), temp_buffer.data(), temp_buffer.data() + BUFSIZE); |
| 324 |
strm.next_out = temp_buffer.data(); |
| 325 |
strm.avail_out = BUFSIZE; |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
int inflate_res = Z_OK; |
| 330 |
while (inflate_res == Z_OK) { |
| 331 |
if (strm.avail_out == 0) { |
| 332 |
dst.insert(dst.end(), temp_buffer.data(), temp_buffer.data() + BUFSIZE); |
| 333 |
strm.next_out = temp_buffer.data(); |
| 334 |
strm.avail_out = BUFSIZE; |
| 335 |
} |
| 336 |
inflate_res = inflate(&strm, Z_FINISH); |
| 337 |
} |
| 338 |
|
| 339 |
if (inflate_res != Z_STREAM_END) { |
| 340 |
inflateEnd(&strm); |
| 341 |
return false; |
| 342 |
} |
| 343 |
|
| 344 |
dst.insert(dst.end(), temp_buffer.data(), temp_buffer.data() + BUFSIZE - strm.avail_out); |
| 345 |
inflateEnd(&strm); |
| 346 |
break; |
| 347 |
} |
| 348 |
case ECompressionType::Heatshrink_11_4: |
| 349 |
case ECompressionType::Heatshrink_12_4: |
| 350 |
{ |
| 351 |
const uint8_t window_sz = (compression_type == ECompressionType::Heatshrink_11_4) ? 11 : 12; |
| 352 |
const uint8_t lookahead_sz = 4; |
| 353 |
const uint16_t input_buffer_size = 2048; |
| 354 |
heatshrink_decoder* decoder = heatshrink_decoder_alloc(input_buffer_size, window_sz, lookahead_sz); |
| 355 |
if (decoder == nullptr) |
| 356 |
return false; |
| 357 |
|
| 358 |
dst.resize(uncompressed_size); |
| 359 |
|
| 360 |
uint8_t* buf = const_cast<uint8_t*>(src.data()); |
| 361 |
uint8_t* outbuf = dst.data(); |
| 362 |
|
| 363 |
uint32_t sunk = 0; |
| 364 |
uint32_t polled = 0; |
| 365 |
|
| 366 |
const size_t compressed_size = src.size(); |
| 367 |
while (sunk < compressed_size) { |
| 368 |
size_t count = 0; |
| 369 |
const HSD_sink_res sink_res = heatshrink_decoder_sink(decoder, &buf[sunk], compressed_size - sunk, &count); |
| 370 |
if (sink_res < 0) { |
| 371 |
heatshrink_decoder_free(decoder); |
| 372 |
return false; |
| 373 |
} |
| 374 |
|
| 375 |
sunk += (uint32_t)count; |
| 376 |
|
| 377 |
HSD_poll_res poll_res; |
| 378 |
do { |
| 379 |
poll_res = heatshrink_decoder_poll(decoder, &outbuf[polled], uncompressed_size - polled, &count); |
| 380 |
if (poll_res < 0) { |
| 381 |
heatshrink_decoder_free(decoder); |
| 382 |
return false; |
| 383 |
} |
| 384 |
polled += (uint32_t)count; |
| 385 |
} while (polled < uncompressed_size && poll_res == HSDR_POLL_MORE); |
| 386 |
} |
| 387 |
|
| 388 |
const HSD_finish_res finish_res = heatshrink_decoder_finish(decoder); |
| 389 |
if (finish_res < 0) { |
| 390 |
heatshrink_decoder_free(decoder); |
| 391 |
return false; |
| 392 |
} |
| 393 |
|
| 394 |
heatshrink_decoder_free(decoder); |
| 395 |
break; |
| 396 |
} |
| 397 |
case ECompressionType::None: |
| 398 |
default: |
| 399 |
{ |
| 400 |
break; |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
return true; |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
// write block header and data in encoded format |
| 409 |
core::EResult write(const BaseMetadataBlock &block, FILE& file, core::EBlockType block_type, core::ECompressionType compression_type, core::Checksum &checksum) |
| 410 |
{ |
| 411 |
if (block.encoding_type > metadata_encoding_types_count()) |
| 412 |
return EResult::InvalidMetadataEncodingType; |
| 413 |
|
| 414 |
BlockHeader block_header((uint16_t)block_type, (uint16_t)compression_type, (uint32_t)0); |
| 415 |
std::vector<uint8_t> out_data; |
| 416 |
if (!block.raw_data.empty()) { |
| 417 |
// process payload encoding |
| 418 |
std::vector<uint8_t> uncompressed_data; |
| 419 |
if (!encode_metadata(block.raw_data, uncompressed_data, (EMetadataEncodingType)block.encoding_type)) |
| 420 |
return EResult::MetadataEncodingError; |
| 421 |
// process payload compression |
| 422 |
block_header.uncompressed_size = (uint32_t)uncompressed_data.size(); |
| 423 |
std::vector<uint8_t> compressed_data; |
| 424 |
if (compression_type != ECompressionType::None) { |
| 425 |
if (!compress(uncompressed_data, compressed_data, compression_type)) |
| 426 |
return EResult::DataCompressionError; |
| 427 |
block_header.compressed_size = (uint32_t)compressed_data.size(); |
| 428 |
} |
| 429 |
out_data.swap((compression_type == ECompressionType::None) ? uncompressed_data : compressed_data); |
| 430 |
} |
| 431 |
|
| 432 |
// write block header |
| 433 |
EResult res = block_header.write(file); |
| 434 |
if (res != EResult::Success) |
| 435 |
// propagate error |
| 436 |
return res; |
| 437 |
|
| 438 |
// write block payload |
| 439 |
if (!write_to_file(file, &block.encoding_type, sizeof(block.encoding_type))) |
| 440 |
return EResult::WriteError; |
| 441 |
if (!out_data.empty()) { |
| 442 |
if (!write_to_file(file, out_data.data(), out_data.size())) |
| 443 |
return EResult::WriteError; |
| 444 |
} |
| 445 |
|
| 446 |
if (checksum.get_type() != EChecksumType::None) { |
| 447 |
// update checksum with block header |
| 448 |
update_checksum(checksum, block_header); |
| 449 |
// update checksum with block payload |
| 450 |
checksum.append(block.encoding_type); |
| 451 |
if (!out_data.empty()) |
| 452 |
checksum.append(static_cast<unsigned char*>(out_data.data()), out_data.size()); |
| 453 |
} |
| 454 |
|
| 455 |
return EResult::Success; |
| 456 |
} |
| 457 |
|
| 458 |
EResult BaseMetadataBlock::read_data(FILE& file, const BlockHeader& block_header) |
| 459 |
{ |
| 460 |
const ECompressionType compression_type = (ECompressionType)block_header.compression; |
| 461 |
|
| 462 |
if (!read_from_file(file, (void*)&encoding_type, sizeof(encoding_type))) |
| 463 |
return EResult::ReadError; |
| 464 |
if (encoding_type > metadata_encoding_types_count()) |
| 465 |
return EResult::InvalidMetadataEncodingType; |
| 466 |
|
| 467 |
std::vector<uint8_t> data; |
| 468 |
const size_t data_size = (compression_type == ECompressionType::None) ? block_header.uncompressed_size : block_header.compressed_size; |
| 469 |
if (data_size > 0) { |
| 470 |
data.resize(data_size); |
| 471 |
if (!read_from_file(file, (void*)data.data(), data_size)) |
| 472 |
return EResult::ReadError; |
| 473 |
} |
| 474 |
|
| 475 |
std::vector<uint8_t> uncompressed_data; |
| 476 |
if (compression_type != ECompressionType::None) { |
| 477 |
if (!uncompress(data, uncompressed_data, compression_type, block_header.uncompressed_size)) |
| 478 |
return EResult::DataUncompressionError; |
| 479 |
} |
| 480 |
|
| 481 |
if (!decode_metadata((compression_type == ECompressionType::None) ? data : uncompressed_data, raw_data, (EMetadataEncodingType)encoding_type)) |
| 482 |
return EResult::MetadataDecodingError; |
| 483 |
|
| 484 |
return EResult::Success; |
| 485 |
} |
| 486 |
|
| 487 |
EResult FileMetadataBlock::write(FILE& file, ECompressionType compression_type, EChecksumType checksum_type) const |
| 488 |
{ |
| 489 |
Checksum cs(checksum_type); |
| 490 |
|
| 491 |
// write block header, payload |
| 492 |
EResult res = binarize::write(*this, file, EBlockType::FileMetadata, compression_type, cs); |
| 493 |
if (res != EResult::Success) |
| 494 |
// propagate error |
| 495 |
return res; |
| 496 |
|
| 497 |
// write block checksum |
| 498 |
if (checksum_type != EChecksumType::None) |
| 499 |
return cs.write(file); |
| 500 |
|
| 501 |
return EResult::Success; |
| 502 |
} |
| 503 |
|
| 504 |
EResult FileMetadataBlock::read_data(FILE& file, const FileHeader& file_header, const BlockHeader& block_header) |
| 505 |
{ |
| 506 |
// read block payload |
| 507 |
EResult res = BaseMetadataBlock::read_data(file, block_header); |
| 508 |
if (res != EResult::Success) |
| 509 |
// propagate error |
| 510 |
return res; |
| 511 |
|
| 512 |
const EChecksumType checksum_type = (EChecksumType)file_header.checksum_type; |
| 513 |
if (checksum_type != EChecksumType::None) { |
| 514 |
// read block checksum |
| 515 |
Checksum cs(checksum_type); |
| 516 |
res = cs.read(file); |
| 517 |
if (res != EResult::Success) |
| 518 |
// propagate error |
| 519 |
return res; |
| 520 |
} |
| 521 |
return EResult::Success; |
| 522 |
} |
| 523 |
|
| 524 |
EResult PrintMetadataBlock::write(FILE& file, ECompressionType compression_type, EChecksumType checksum_type) const |
| 525 |
{ |
| 526 |
Checksum cs(checksum_type); |
| 527 |
|
| 528 |
// write block header, payload |
| 529 |
EResult res = binarize::write(*this, file, EBlockType::PrintMetadata, compression_type, cs); |
| 530 |
if (res != EResult::Success) |
| 531 |
// propagate error |
| 532 |
return res; |
| 533 |
|
| 534 |
// write block checksum |
| 535 |
if (checksum_type != EChecksumType::None) |
| 536 |
return cs.write(file); |
| 537 |
|
| 538 |
return EResult::Success; |
| 539 |
} |
| 540 |
|
| 541 |
EResult PrintMetadataBlock::read_data(FILE& file, const FileHeader& file_header, const BlockHeader& block_header) |
| 542 |
{ |
| 543 |
// read block payload |
| 544 |
EResult res = BaseMetadataBlock::read_data(file, block_header); |
| 545 |
if (res != EResult::Success) |
| 546 |
// propagate error |
| 547 |
return res; |
| 548 |
|
| 549 |
const EChecksumType checksum_type = (EChecksumType)file_header.checksum_type; |
| 550 |
if (checksum_type != EChecksumType::None) { |
| 551 |
// read block checksum |
| 552 |
Checksum cs(checksum_type); |
| 553 |
res = cs.read(file); |
| 554 |
if (res != EResult::Success) |
| 555 |
// propagate error |
| 556 |
return res; |
| 557 |
} |
| 558 |
return EResult::Success; |
| 559 |
} |
| 560 |
|
| 561 |
EResult PrinterMetadataBlock::write(FILE& file, ECompressionType compression_type, EChecksumType checksum_type) const |
| 562 |
{ |
| 563 |
Checksum cs(checksum_type); |
| 564 |
|
| 565 |
// write block header, payload |
| 566 |
EResult res = binarize::write(*this, file, EBlockType::PrinterMetadata, compression_type, cs); |
| 567 |
if (res != EResult::Success) |
| 568 |
// propagate error |
| 569 |
return res; |
| 570 |
|
| 571 |
// write block checksum |
| 572 |
if (checksum_type != EChecksumType::None) |
| 573 |
return cs.write(file); |
| 574 |
|
| 575 |
return EResult::Success; |
| 576 |
} |
| 577 |
|
| 578 |
EResult PrinterMetadataBlock::read_data(FILE& file, const FileHeader& file_header, const BlockHeader& block_header) |
| 579 |
{ |
| 580 |
// read block payload |
| 581 |
EResult res = BaseMetadataBlock::read_data(file, block_header); |
| 582 |
if (res != EResult::Success) |
| 583 |
// propagate error |
| 584 |
return res; |
| 585 |
|
| 586 |
const EChecksumType checksum_type = (EChecksumType)file_header.checksum_type; |
| 587 |
if (checksum_type != EChecksumType::None) { |
| 588 |
// read block checksum |
| 589 |
Checksum cs(checksum_type); |
| 590 |
res = cs.read(file); |
| 591 |
if (res != EResult::Success) |
| 592 |
// propagate error |
| 593 |
return res; |
| 594 |
} |
| 595 |
return EResult::Success; |
| 596 |
} |
| 597 |
|
| 598 |
EResult ThumbnailBlock::write(FILE& file, EChecksumType checksum_type) |
| 599 |
{ |
| 600 |
if (params.format >= thumbnail_formats_count()) |
| 601 |
return EResult::InvalidThumbnailFormat; |
| 602 |
if (params.width == 0) |
| 603 |
return EResult::InvalidThumbnailWidth; |
| 604 |
if (params.height == 0) |
| 605 |
return EResult::InvalidThumbnailHeight; |
| 606 |
if (data.size() == 0) |
| 607 |
return EResult::InvalidThumbnailDataSize; |
| 608 |
|
| 609 |
// write block header |
| 610 |
BlockHeader block_header((uint16_t)EBlockType::Thumbnail, (uint16_t)ECompressionType::None, (uint32_t)data.size()); |
| 611 |
EResult res = block_header.write(file); |
| 612 |
if (res != EResult::Success) |
| 613 |
// propagate error |
| 614 |
return res; |
| 615 |
|
| 616 |
res = params.write(file); |
| 617 |
if (res != EResult::Success){ |
| 618 |
// propagate error |
| 619 |
return res; |
| 620 |
} |
| 621 |
|
| 622 |
if (!write_to_file(file, data.data(), data.size())) |
| 623 |
return EResult::WriteError; |
| 624 |
|
| 625 |
if (checksum_type != EChecksumType::None) { |
| 626 |
Checksum cs(checksum_type); |
| 627 |
// update checksum with block header |
| 628 |
update_checksum(cs, block_header); |
| 629 |
// update checksum with block payload |
| 630 |
update_checksum(cs, *this); |
| 631 |
// write block checksum |
| 632 |
res = cs.write(file); |
| 633 |
if (res != EResult::Success) |
| 634 |
// propagate error |
| 635 |
return res; |
| 636 |
} |
| 637 |
return EResult::Success; |
| 638 |
} |
| 639 |
|
| 640 |
EResult ThumbnailBlock::read_data(FILE& file, const FileHeader& file_header, const BlockHeader& block_header) |
| 641 |
{ |
| 642 |
// read block payload |
| 643 |
EResult res = params.read(file); |
| 644 |
if (res != EResult::Success) |
| 645 |
// propagate error |
| 646 |
return res; |
| 647 |
if (params.format >= thumbnail_formats_count()) |
| 648 |
return EResult::InvalidThumbnailFormat; |
| 649 |
if (params.width == 0) |
| 650 |
return EResult::InvalidThumbnailWidth; |
| 651 |
if (params.height == 0) |
| 652 |
return EResult::InvalidThumbnailHeight; |
| 653 |
if (block_header.uncompressed_size == 0) |
| 654 |
return EResult::InvalidThumbnailDataSize; |
| 655 |
|
| 656 |
data.resize(block_header.uncompressed_size); |
| 657 |
if (!read_from_file(file, (void*)data.data(), block_header.uncompressed_size)) |
| 658 |
return EResult::ReadError; |
| 659 |
|
| 660 |
const EChecksumType checksum_type = (EChecksumType)file_header.checksum_type; |
| 661 |
if (checksum_type != EChecksumType::None) { |
| 662 |
// read block checksum |
| 663 |
Checksum cs(checksum_type); |
| 664 |
const EResult res = cs.read(file); |
| 665 |
if (res != EResult::Success) |
| 666 |
// propagate error |
| 667 |
return res; |
| 668 |
} |
| 669 |
return EResult::Success; |
| 670 |
} |
| 671 |
|
| 672 |
EResult GCodeBlock::write(FILE& file, ECompressionType compression_type, EChecksumType checksum_type) const |
| 673 |
{ |
| 674 |
if (encoding_type > gcode_encoding_types_count()) |
| 675 |
return EResult::InvalidGCodeEncodingType; |
| 676 |
|
| 677 |
BlockHeader block_header((uint16_t)EBlockType::GCode, (uint16_t)compression_type, (uint32_t)0); |
| 678 |
std::vector<uint8_t> out_data; |
| 679 |
if (!raw_data.empty()) { |
| 680 |
// process payload encoding |
| 681 |
std::vector<uint8_t> uncompressed_data; |
| 682 |
if (!encode_gcode(raw_data, uncompressed_data, (EGCodeEncodingType)encoding_type)) |
| 683 |
return EResult::GCodeEncodingError; |
| 684 |
// process payload compression |
| 685 |
block_header.uncompressed_size = (uint32_t)uncompressed_data.size(); |
| 686 |
std::vector<uint8_t> compressed_data; |
| 687 |
if (compression_type != ECompressionType::None) { |
| 688 |
if (!compress(uncompressed_data, compressed_data, compression_type)) |
| 689 |
return EResult::DataCompressionError; |
| 690 |
block_header.compressed_size = (uint32_t)compressed_data.size(); |
| 691 |
} |
| 692 |
out_data.swap((compression_type == ECompressionType::None) ? uncompressed_data : compressed_data); |
| 693 |
} |
| 694 |
|
| 695 |
// write block header |
| 696 |
EResult res = block_header.write(file); |
| 697 |
if (res != EResult::Success) |
| 698 |
// propagate error |
| 699 |
return res; |
| 700 |
|
| 701 |
// write block payload |
| 702 |
if (!write_to_file(file, &encoding_type, sizeof(encoding_type))) |
| 703 |
return EResult::WriteError; |
| 704 |
if (!out_data.empty()) { |
| 705 |
if (!write_to_file(file, out_data.data(), out_data.size())) |
| 706 |
return EResult::WriteError; |
| 707 |
} |
| 708 |
|
| 709 |
// write checksum |
| 710 |
if (checksum_type != EChecksumType::None) { |
| 711 |
Checksum cs(checksum_type); |
| 712 |
// update checksum with block header |
| 713 |
update_checksum(cs, block_header); |
| 714 |
// update checksum with block payload |
| 715 |
std::vector<uint8_t> data_to_encode = |
| 716 |
encode(reinterpret_cast<const std::byte*>(&encoding_type), sizeof(encoding_type)); |
| 717 |
cs.append(data_to_encode.data(), data_to_encode.size()); |
| 718 |
if (!out_data.empty()) |
| 719 |
cs.append(static_cast<unsigned char *>(out_data.data()), out_data.size()); |
| 720 |
res = cs.write(file); |
| 721 |
if (res != EResult::Success) |
| 722 |
// propagate error |
| 723 |
return res; |
| 724 |
} |
| 725 |
return EResult::Success; |
| 726 |
} |
| 727 |
|
| 728 |
EResult GCodeBlock::read_data(FILE& file, const FileHeader& file_header, const BlockHeader& block_header) |
| 729 |
{ |
| 730 |
const ECompressionType compression_type = (ECompressionType)block_header.compression; |
| 731 |
|
| 732 |
if (!read_from_file(file, (void*)&encoding_type, sizeof(encoding_type))) |
| 733 |
return EResult::ReadError; |
| 734 |
if (encoding_type > gcode_encoding_types_count()) |
| 735 |
return EResult::InvalidGCodeEncodingType; |
| 736 |
|
| 737 |
std::vector<uint8_t> data; |
| 738 |
const size_t data_size = (compression_type == ECompressionType::None) ? block_header.uncompressed_size : block_header.compressed_size; |
| 739 |
if (data_size > 0) { |
| 740 |
data.resize(data_size); |
| 741 |
if (!read_from_file(file, (void*)data.data(), data_size)) |
| 742 |
return EResult::ReadError; |
| 743 |
} |
| 744 |
|
| 745 |
std::vector<uint8_t> uncompressed_data; |
| 746 |
if (compression_type != ECompressionType::None) { |
| 747 |
if (!uncompress(data, uncompressed_data, compression_type, block_header.uncompressed_size)) |
| 748 |
return EResult::DataUncompressionError; |
| 749 |
} |
| 750 |
|
| 751 |
if (!decode_gcode((compression_type == ECompressionType::None) ? data : uncompressed_data, raw_data, (EGCodeEncodingType)encoding_type)) |
| 752 |
return EResult::GCodeDecodingError; |
| 753 |
|
| 754 |
const EChecksumType checksum_type = (EChecksumType)file_header.checksum_type; |
| 755 |
if (checksum_type != EChecksumType::None) { |
| 756 |
// read block checksum |
| 757 |
Checksum cs(checksum_type); |
| 758 |
const EResult res = cs.read(file); |
| 759 |
if (res != EResult::Success) |
| 760 |
// propagate error |
| 761 |
return res; |
| 762 |
} |
| 763 |
return EResult::Success; |
| 764 |
} |
| 765 |
|
| 766 |
EResult SlicerMetadataBlock::write(FILE& file, ECompressionType compression_type, EChecksumType checksum_type) const |
| 767 |
{ |
| 768 |
Checksum cs(checksum_type); |
| 769 |
|
| 770 |
// write block header, payload |
| 771 |
EResult res = binarize::write(*this, file, EBlockType::SlicerMetadata, compression_type, cs); |
| 772 |
if (res != EResult::Success) |
| 773 |
// propagate error |
| 774 |
return res; |
| 775 |
|
| 776 |
// write block checksum |
| 777 |
if (checksum_type != EChecksumType::None) |
| 778 |
return cs.write(file); |
| 779 |
|
| 780 |
return EResult::Success; |
| 781 |
} |
| 782 |
|
| 783 |
EResult SlicerMetadataBlock::read_data(FILE& file, const FileHeader& file_header, const BlockHeader& block_header) |
| 784 |
{ |
| 785 |
// read block payload |
| 786 |
EResult res = BaseMetadataBlock::read_data(file, block_header); |
| 787 |
if (res != EResult::Success) |
| 788 |
// propagate error |
| 789 |
return res; |
| 790 |
|
| 791 |
const EChecksumType checksum_type = (EChecksumType)file_header.checksum_type; |
| 792 |
if (checksum_type != EChecksumType::None) { |
| 793 |
// read block checksum |
| 794 |
Checksum cs(checksum_type); |
| 795 |
res = cs.read(file); |
| 796 |
if (res != EResult::Success) |
| 797 |
// propagate error |
| 798 |
return res; |
| 799 |
} |
| 800 |
return EResult::Success; |
| 801 |
} |
| 802 |
|
| 803 |
void Slicer3MetadataBlock::set_json(std::string_view json) |
| 804 |
{ |
| 805 |
if (raw_data.empty()) |
| 806 |
raw_data.emplace_back("", json); |
| 807 |
else |
| 808 |
raw_data.front().second = json; |
| 809 |
} |
| 810 |
|
| 811 |
const std::string& Slicer3MetadataBlock::json() const |
| 812 |
{ |
| 813 |
if (raw_data.empty()) { |
| 814 |
static std::string EMPTY; |
| 815 |
return EMPTY; |
| 816 |
} |
| 817 |
return raw_data.front().second; |
| 818 |
} |
| 819 |
|
| 820 |
|
| 821 |
EResult Slicer3MetadataBlock::write(FILE& file, ECompressionType compression_type, EChecksumType checksum_type) const |
| 822 |
{ |
| 823 |
Checksum cs(checksum_type); |
| 824 |
|
| 825 |
// write block header, payload |
| 826 |
EResult res = binarize::write(*this, file, EBlockType::SlicerMetadata, compression_type, cs); |
| 827 |
if (res != EResult::Success) |
| 828 |
// propagate error |
| 829 |
return res; |
| 830 |
|
| 831 |
// write block checksum |
| 832 |
if (checksum_type != EChecksumType::None) |
| 833 |
return cs.write(file); |
| 834 |
|
| 835 |
return EResult::Success; |
| 836 |
} |
| 837 |
|
| 838 |
EResult Slicer3MetadataBlock::read_data(FILE& file, const FileHeader& file_header, const BlockHeader& block_header) |
| 839 |
{ |
| 840 |
// read block payload |
| 841 |
EResult res = BaseMetadataBlock::read_data(file, block_header); |
| 842 |
if (res != EResult::Success) |
| 843 |
// propagate error |
| 844 |
return res; |
| 845 |
|
| 846 |
const EChecksumType checksum_type = (EChecksumType)file_header.checksum_type; |
| 847 |
if (checksum_type != EChecksumType::None) { |
| 848 |
// read block checksum |
| 849 |
Checksum cs(checksum_type); |
| 850 |
res = cs.read(file); |
| 851 |
if (res != EResult::Success) |
| 852 |
// propagate error |
| 853 |
return res; |
| 854 |
} |
| 855 |
return EResult::Success; |
| 856 |
} |
| 857 |
|
| 858 |
EPeekSlicerMetadataResult peek_slicer_metadata_block(FILE& file, const core::BlockHeader& block_header) |
| 859 |
{ |
| 860 |
if (EBlockType{block_header.type} != EBlockType::SlicerMetadata) |
| 861 |
return EPeekSlicerMetadataResult::OtherBlockFound; |
| 862 |
decltype(Slicer3MetadataBlock::encoding_type) encoding_type; |
| 863 |
auto pos = ftell(&file); |
| 864 |
if (pos < 0) |
| 865 |
return EPeekSlicerMetadataResult::ReadError; |
| 866 |
if (!read_from_file(file, (void*)&encoding_type, sizeof(encoding_type))) |
| 867 |
return EPeekSlicerMetadataResult::ReadError; |
| 868 |
// rewind back the file like we didn't read it |
| 869 |
fseek(&file, pos, SEEK_SET); |
| 870 |
return EMetadataEncodingType{encoding_type} == EMetadataEncodingType::JSON ? EPeekSlicerMetadataResult::Slicer3MetadataFound : EPeekSlicerMetadataResult::SlicerMetadataFound; |
| 871 |
} |
| 872 |
|
| 873 |
bool Binarizer::is_enabled() const { return m_enabled; } |
| 874 |
void Binarizer::set_enabled(bool enable) { m_enabled = enable; } |
| 875 |
BinaryData& Binarizer::get_binary_data() { return m_binary_data; } |
| 876 |
const BinaryData& Binarizer::get_binary_data() const { return m_binary_data; } |
| 877 |
size_t Binarizer::get_max_gcode_cache_size() const { return m_gcode_cache_size; } |
| 878 |
void Binarizer::set_max_gcode_cache_size(size_t size) { m_gcode_cache_size = size; } |
| 879 |
|
| 880 |
EResult Binarizer::initialize(FILE& file, const BinarizerConfig& config) |
| 881 |
{ |
| 882 |
if (!m_enabled) |
| 883 |
return EResult::Success; |
| 884 |
|
| 885 |
m_file = &file; |
| 886 |
m_config = config; |
| 887 |
|
| 888 |
// save header |
| 889 |
FileHeader file_header; |
| 890 |
file_header.checksum_type = (uint16_t)m_config.checksum; |
| 891 |
EResult res = file_header.write(*m_file); |
| 892 |
if (res != EResult::Success) |
| 893 |
// propagate error |
| 894 |
return res; |
| 895 |
|
| 896 |
// save file metadata block, if present |
| 897 |
if (!m_binary_data.file_metadata.raw_data.empty()) { |
| 898 |
m_binary_data.file_metadata.encoding_type = (uint16_t)config.metadata_encoding; |
| 899 |
res = m_binary_data.file_metadata.write(*m_file, m_config.compression.file_metadata, m_config.checksum); |
| 900 |
if (res != EResult::Success) |
| 901 |
// propagate error |
| 902 |
return res; |
| 903 |
} |
| 904 |
|
| 905 |
// save printer metadata block |
| 906 |
if (m_binary_data.printer_metadata.raw_data.empty()) |
| 907 |
return EResult::MissingPrinterMetadata; |
| 908 |
m_binary_data.printer_metadata.encoding_type = (uint16_t)config.metadata_encoding; |
| 909 |
res = m_binary_data.printer_metadata.write(*m_file, m_config.compression.printer_metadata, m_config.checksum); |
| 910 |
if (res != EResult::Success) |
| 911 |
// propagate error |
| 912 |
return res; |
| 913 |
|
| 914 |
// save thumbnail blocks |
| 915 |
for (ThumbnailBlock& block : m_binary_data.thumbnails) { |
| 916 |
res = block.write(*m_file, m_config.checksum); |
| 917 |
if (res != EResult::Success) |
| 918 |
// propagate error |
| 919 |
return res; |
| 920 |
} |
| 921 |
|
| 922 |
// save print metadata block |
| 923 |
if (m_binary_data.print_metadata.raw_data.empty()) |
| 924 |
return EResult::MissingPrintMetadata; |
| 925 |
m_binary_data.print_metadata.encoding_type = (uint16_t)config.metadata_encoding; |
| 926 |
res = m_binary_data.print_metadata.write(*m_file, m_config.compression.print_metadata, m_config.checksum); |
| 927 |
if (res != EResult::Success) |
| 928 |
// propagate error |
| 929 |
return res; |
| 930 |
|
| 931 |
// save slicer metadata blocks |
| 932 |
if (m_binary_data.slicer_metadata.raw_data.empty() && m_binary_data.slicer3_metadata.raw_data.empty()) |
| 933 |
return EResult::MissingSlicerMetadata; |
| 934 |
|
| 935 |
if (!m_binary_data.slicer_metadata.raw_data.empty()) { |
| 936 |
m_binary_data.slicer_metadata.encoding_type = (uint16_t)config.metadata_encoding; |
| 937 |
res = m_binary_data.slicer_metadata.write(*m_file, m_config.compression.slicer_metadata, m_config.checksum); |
| 938 |
if (res != EResult::Success) { |
| 939 |
// propagate error |
| 940 |
return res; |
| 941 |
} |
| 942 |
} |
| 943 |
|
| 944 |
if (!m_binary_data.slicer3_metadata.raw_data.empty()) { |
| 945 |
res = m_binary_data.slicer3_metadata.write(*m_file, m_config.compression.slicer3_metadata, m_config.checksum); |
| 946 |
if (res != EResult::Success) { |
| 947 |
// propagate error |
| 948 |
return res; |
| 949 |
} |
| 950 |
} |
| 951 |
|
| 952 |
return EResult::Success; |
| 953 |
} |
| 954 |
|
| 955 |
static EResult write_gcode_block(FILE& file, const std::string& raw_data, const BinarizerConfig& config) |
| 956 |
{ |
| 957 |
GCodeBlock block; |
| 958 |
block.encoding_type = (uint16_t)config.gcode_encoding; |
| 959 |
block.raw_data = raw_data; |
| 960 |
return block.write(file, config.compression.gcode, config.checksum); |
| 961 |
} |
| 962 |
|
| 963 |
EResult Binarizer::append_gcode(const std::string& gcode) |
| 964 |
{ |
| 965 |
if (gcode.empty()) |
| 966 |
return EResult::Success; |
| 967 |
|
| 968 |
assert(m_file != nullptr); |
| 969 |
if (m_file == nullptr) |
| 970 |
return EResult::WriteError; |
| 971 |
|
| 972 |
auto it_begin = gcode.begin(); |
| 973 |
do { |
| 974 |
const size_t begin_pos = std::distance(gcode.begin(), it_begin); |
| 975 |
const size_t end_line_pos = gcode.find_first_of('\n', begin_pos); |
| 976 |
if (end_line_pos == std::string::npos) |
| 977 |
return EResult::WriteError; |
| 978 |
|
| 979 |
const size_t line_size = 1 + end_line_pos - begin_pos; |
| 980 |
if (line_size + m_gcode_cache.length() > m_gcode_cache_size) { |
| 981 |
if (!m_gcode_cache.empty()) { |
| 982 |
const EResult res = write_gcode_block(*m_file, m_gcode_cache, m_config); |
| 983 |
if (res != EResult::Success) |
| 984 |
// propagate error |
| 985 |
return res; |
| 986 |
m_gcode_cache.clear(); |
| 987 |
} |
| 988 |
} |
| 989 |
|
| 990 |
if (line_size > m_gcode_cache_size) |
| 991 |
return EResult::WriteError; |
| 992 |
|
| 993 |
m_gcode_cache.insert(m_gcode_cache.end(), it_begin, it_begin + line_size); |
| 994 |
it_begin += line_size; |
| 995 |
} while (it_begin != gcode.end()); |
| 996 |
|
| 997 |
return EResult::Success; |
| 998 |
} |
| 999 |
|
| 1000 |
EResult Binarizer::finalize() |
| 1001 |
{ |
| 1002 |
if (!m_enabled) |
| 1003 |
return EResult::Success; |
| 1004 |
|
| 1005 |
// save gcode cache, if not empty |
| 1006 |
if (!m_gcode_cache.empty()) { |
| 1007 |
const EResult res = write_gcode_block(*m_file, m_gcode_cache, m_config); |
| 1008 |
if (res != EResult::Success) |
| 1009 |
// propagate error |
| 1010 |
return res; |
| 1011 |
} |
| 1012 |
|
| 1013 |
return EResult::Success; |
| 1014 |
} |
| 1015 |
|
| 1016 |
}} // namespace bgcode |