| 1 |
#include "core_impl.hpp" |
| 2 |
#include <cstring> |
| 3 |
|
| 4 |
namespace bgcode { namespace core { |
| 5 |
|
| 6 |
template<class T> |
| 7 |
static bool write_to_file(FILE& file, const T* data, size_t data_size) |
| 8 |
{ |
| 9 |
const size_t wsize = fwrite(static_cast<const void*>(data), 1, data_size, &file); |
| 10 |
return !ferror(&file) && wsize == data_size; |
| 11 |
} |
| 12 |
|
| 13 |
template<class T> |
| 14 |
static bool read_from_file(FILE& file, T *data, size_t data_size) |
| 15 |
{ |
| 16 |
static_assert(!std::is_const_v<T>, "Type of output buffer cannot be const!"); |
| 17 |
|
| 18 |
const size_t rsize = fread(static_cast<void *>(data), 1, data_size, &file); |
| 19 |
return !ferror(&file) && rsize == data_size; |
| 20 |
} |
| 21 |
|
| 22 |
EResult verify_block_checksum(FILE& file, const FileHeader& file_header, |
| 23 |
const BlockHeader& block_header, std::byte* buffer, size_t buffer_size) |
| 24 |
{ |
| 25 |
if (buffer == nullptr || buffer_size == 0) |
| 26 |
return EResult::InvalidBuffer; |
| 27 |
|
| 28 |
// No checksum in file, no checking, just return success |
| 29 |
if (file_header.checksum_type == (uint16_t)EChecksumType::None) |
| 30 |
return EResult::Success; |
| 31 |
|
| 32 |
// seek after header, where payload starts |
| 33 |
if (fseek(&file, block_header.get_position() + (long)block_header.get_size(), SEEK_SET) != 0) |
| 34 |
return EResult::ReadError; |
| 35 |
|
| 36 |
Checksum curr_cs((EChecksumType)file_header.checksum_type); |
| 37 |
// update block checksum block header |
| 38 |
update_checksum(curr_cs, block_header); |
| 39 |
|
| 40 |
// read block payload |
| 41 |
size_t remaining_payload_size = block_payload_size(block_header); |
| 42 |
while (remaining_payload_size > 0) { |
| 43 |
const size_t size_to_read = std::min(remaining_payload_size, buffer_size); |
| 44 |
if (!read_from_file(file, buffer, size_to_read)) |
| 45 |
return EResult::ReadError; |
| 46 |
curr_cs.append(buffer, size_to_read); |
| 47 |
remaining_payload_size -= size_to_read; |
| 48 |
} |
| 49 |
|
| 50 |
// read checksum |
| 51 |
Checksum read_cs((EChecksumType)file_header.checksum_type); |
| 52 |
EResult res = read_cs.read(file); |
| 53 |
if (res != EResult::Success) |
| 54 |
// propagate error |
| 55 |
return res; |
| 56 |
|
| 57 |
// Verify checksum |
| 58 |
if (!curr_cs.matches(read_cs)) |
| 59 |
return EResult::InvalidChecksum; |
| 60 |
|
| 61 |
return EResult::Success; |
| 62 |
} |
| 63 |
|
| 64 |
Checksum::Checksum(EChecksumType type) |
| 65 |
: m_type(type), m_size(checksum_size(type)) |
| 66 |
{ |
| 67 |
m_checksum.fill(std::byte{0}); |
| 68 |
} |
| 69 |
|
| 70 |
void Checksum::append(const std::vector<std::byte>& data) |
| 71 |
{ |
| 72 |
append(data.data(), data.size()); |
| 73 |
} |
| 74 |
|
| 75 |
bool Checksum::matches(Checksum& other) |
| 76 |
{ |
| 77 |
return m_checksum == other.m_checksum; |
| 78 |
} |
| 79 |
|
| 80 |
EResult Checksum::write(FILE& file) |
| 81 |
{ |
| 82 |
if (m_type != EChecksumType::None) { |
| 83 |
if (!write_to_file(file, m_checksum.data(), m_size)) |
| 84 |
return EResult::WriteError; |
| 85 |
} |
| 86 |
return EResult::Success; |
| 87 |
} |
| 88 |
|
| 89 |
EResult Checksum::read(FILE& file) |
| 90 |
{ |
| 91 |
if (m_type != EChecksumType::None) { |
| 92 |
if (!read_from_file(file, m_checksum.data(), m_size)) |
| 93 |
return EResult::ReadError; |
| 94 |
} |
| 95 |
return EResult::Success; |
| 96 |
} |
| 97 |
|
| 98 |
FileHeader::FileHeader() |
| 99 |
: magic{MAGICi32} |
| 100 |
, version{VERSION} |
| 101 |
, checksum_type{static_cast<uint16_t>(EChecksumType::None)} |
| 102 |
{} |
| 103 |
|
| 104 |
FileHeader::FileHeader(uint32_t mg, uint32_t ver, uint16_t chk_type) |
| 105 |
: magic{mg}, version{ver}, checksum_type{chk_type} |
| 106 |
{} |
| 107 |
|
| 108 |
EResult FileHeader::write(FILE& file) const |
| 109 |
{ |
| 110 |
if (magic != MAGICi32) |
| 111 |
return EResult::InvalidMagicNumber; |
| 112 |
if (checksum_type >= checksum_types_count()) |
| 113 |
return EResult::InvalidChecksumType; |
| 114 |
|
| 115 |
if (!write_to_file(file, &magic, sizeof(magic))) |
| 116 |
return EResult::WriteError; |
| 117 |
if (!write_to_file(file, &version, sizeof(version))) |
| 118 |
return EResult::WriteError; |
| 119 |
if (!write_to_file(file, &checksum_type, sizeof(checksum_type))) |
| 120 |
return EResult::WriteError; |
| 121 |
|
| 122 |
return EResult::Success; |
| 123 |
} |
| 124 |
|
| 125 |
EResult FileHeader::read(FILE& file, const uint32_t* const max_version) |
| 126 |
{ |
| 127 |
if (!read_from_file(file, &magic, sizeof(magic))) |
| 128 |
return EResult::ReadError; |
| 129 |
if (magic != MAGICi32) |
| 130 |
return EResult::InvalidMagicNumber; |
| 131 |
|
| 132 |
if (!read_from_file(file, &version, sizeof(version))) |
| 133 |
return EResult::ReadError; |
| 134 |
if (max_version != nullptr && version > *max_version) |
| 135 |
return EResult::InvalidVersionNumber; |
| 136 |
|
| 137 |
if (!read_from_file(file, &checksum_type, sizeof(checksum_type))) |
| 138 |
return EResult::ReadError; |
| 139 |
if (checksum_type >= checksum_types_count()) |
| 140 |
return EResult::InvalidChecksumType; |
| 141 |
|
| 142 |
return EResult::Success; |
| 143 |
} |
| 144 |
|
| 145 |
BlockHeader::BlockHeader(uint16_t type, uint16_t compression, uint32_t uncompressed_size, uint32_t compressed_size) |
| 146 |
: type(type) |
| 147 |
, compression(compression) |
| 148 |
, uncompressed_size(uncompressed_size) |
| 149 |
, compressed_size(compressed_size) |
| 150 |
{} |
| 151 |
|
| 152 |
long BlockHeader::get_position() const |
| 153 |
{ |
| 154 |
return m_position; |
| 155 |
} |
| 156 |
|
| 157 |
EResult BlockHeader::write(FILE& file) |
| 158 |
{ |
| 159 |
m_position = ftell(&file); |
| 160 |
if (!write_to_file(file, &type, sizeof(type))) |
| 161 |
return EResult::WriteError; |
| 162 |
if (!write_to_file(file, &compression, sizeof(compression))) |
| 163 |
return EResult::WriteError; |
| 164 |
if (!write_to_file(file, &uncompressed_size, sizeof(uncompressed_size))) |
| 165 |
return EResult::WriteError; |
| 166 |
if (compression != (uint16_t)ECompressionType::None) { |
| 167 |
if (!write_to_file(file, &compressed_size, sizeof(compressed_size))) |
| 168 |
return EResult::WriteError; |
| 169 |
} |
| 170 |
return EResult::Success; |
| 171 |
} |
| 172 |
|
| 173 |
EResult BlockHeader::read(FILE& file) |
| 174 |
{ |
| 175 |
m_position = ftell(&file); |
| 176 |
if (!read_from_file(file, &type, sizeof(type))) |
| 177 |
return EResult::ReadError; |
| 178 |
if (type >= block_types_count()) |
| 179 |
return EResult::InvalidBlockType; |
| 180 |
|
| 181 |
if (!read_from_file(file, &compression, sizeof(compression))) |
| 182 |
return EResult::ReadError; |
| 183 |
if (compression >= compression_types_count()) |
| 184 |
return EResult::InvalidCompressionType; |
| 185 |
|
| 186 |
if (!read_from_file(file, &uncompressed_size, sizeof(uncompressed_size))) |
| 187 |
return EResult::ReadError; |
| 188 |
if (compression != (uint16_t)ECompressionType::None) { |
| 189 |
if (!read_from_file(file, &compressed_size, sizeof(compressed_size))) |
| 190 |
return EResult::ReadError; |
| 191 |
} |
| 192 |
|
| 193 |
return EResult::Success; |
| 194 |
} |
| 195 |
|
| 196 |
size_t BlockHeader::get_size() const { |
| 197 |
return sizeof(type) + sizeof(compression) + sizeof(uncompressed_size) + |
| 198 |
((compression == (uint16_t)ECompressionType::None)? 0 : sizeof(compressed_size)); |
| 199 |
} |
| 200 |
|
| 201 |
EResult ThumbnailParams::write(FILE& file) const { |
| 202 |
if (!write_to_file(file, &format, sizeof(format))) |
| 203 |
return EResult::WriteError; |
| 204 |
if (!write_to_file(file, &width, sizeof(width))) |
| 205 |
return EResult::WriteError; |
| 206 |
if (!write_to_file(file, &height, sizeof(height))) |
| 207 |
return EResult::WriteError; |
| 208 |
return EResult::Success; |
| 209 |
} |
| 210 |
|
| 211 |
EResult ThumbnailParams::read(FILE& file){ |
| 212 |
if (!read_from_file(file, &format, sizeof(format))) |
| 213 |
return EResult::ReadError; |
| 214 |
if (!read_from_file(file, &width, sizeof(width))) |
| 215 |
return EResult::ReadError; |
| 216 |
if (!read_from_file(file, &height, sizeof(height))) |
| 217 |
return EResult::ReadError; |
| 218 |
return EResult::Success; |
| 219 |
} |
| 220 |
|
| 221 |
BGCODE_CORE_EXPORT std::string_view translate_result(EResult result) |
| 222 |
{ |
| 223 |
using namespace std::literals; |
| 224 |
switch (result) |
| 225 |
{ |
| 226 |
case EResult::Success: { return "Success"sv; } |
| 227 |
case EResult::ReadError: { return "Read error"sv; } |
| 228 |
case EResult::WriteError: { return "Write error"sv; } |
| 229 |
case EResult::InvalidMagicNumber: { return "Invalid magic number"sv; } |
| 230 |
case EResult::InvalidVersionNumber: { return "Invalid version number"sv; } |
| 231 |
case EResult::InvalidChecksumType: { return "Invalid checksum type"sv; } |
| 232 |
case EResult::InvalidBlockType: { return "Invalid block type"sv; } |
| 233 |
case EResult::InvalidCompressionType: { return "Invalid compression type"sv; } |
| 234 |
case EResult::InvalidMetadataEncodingType: { return "Invalid metadata encoding type"sv; } |
| 235 |
case EResult::InvalidGCodeEncodingType: { return "Invalid gcode encoding type"sv; } |
| 236 |
case EResult::DataCompressionError: { return "Data compression error"sv; } |
| 237 |
case EResult::DataUncompressionError: { return "Data uncompression error"sv; } |
| 238 |
case EResult::MetadataEncodingError: { return "Metadata encoding error"sv; } |
| 239 |
case EResult::MetadataDecodingError: { return "Metadata decoding error"sv; } |
| 240 |
case EResult::GCodeEncodingError: { return "GCode encoding error"sv; } |
| 241 |
case EResult::GCodeDecodingError: { return "GCode decoding error"sv; } |
| 242 |
case EResult::BlockNotFound: { return "Block not found"sv; } |
| 243 |
case EResult::InvalidChecksum: { return "Invalid checksum"sv; } |
| 244 |
case EResult::InvalidThumbnailFormat: { return "Invalid thumbnail format"sv; } |
| 245 |
case EResult::InvalidThumbnailWidth: { return "Invalid thumbnail width"sv; } |
| 246 |
case EResult::InvalidThumbnailHeight: { return "Invalid thumbnail height"sv; } |
| 247 |
case EResult::InvalidThumbnailDataSize: { return "Invalid thumbnail data size"sv; } |
| 248 |
case EResult::InvalidBinaryGCodeFile: { return "Invalid binary GCode file"sv; } |
| 249 |
case EResult::InvalidAsciiGCodeFile: { return "Invalid ascii GCode file"sv; } |
| 250 |
case EResult::InvalidSequenceOfBlocks: { return "Invalid sequence of blocks"sv; } |
| 251 |
case EResult::InvalidBuffer: { return "Invalid buffer"sv; } |
| 252 |
case EResult::AlreadyBinarized: { return "Already binarized"sv; } |
| 253 |
case EResult::MissingPrinterMetadata: { return "Missing printer metadata"sv; } |
| 254 |
case EResult::MissingPrintMetadata: { return "Missing print metadata"sv; } |
| 255 |
case EResult::MissingSlicerMetadata: { return "Missing slicer metadata"sv; } |
| 256 |
} |
| 257 |
return std::string_view(); |
| 258 |
} |
| 259 |
|
| 260 |
BGCODE_CORE_EXPORT EResult is_valid_binary_gcode(FILE& file, bool check_contents, std::byte* cs_buffer, size_t cs_buffer_size) |
| 261 |
{ |
| 262 |
// cache file position |
| 263 |
const long curr_pos = ftell(&file); |
| 264 |
rewind(&file); |
| 265 |
|
| 266 |
// check magic number |
| 267 |
std::array<char, 4> magic; |
| 268 |
const size_t rsize = fread((void*)magic.data(), 1, magic.size(), &file); |
| 269 |
if (ferror(&file) && rsize != magic.size()) |
| 270 |
return EResult::ReadError; |
| 271 |
else if (magic != MAGIC) { |
| 272 |
// restore file position |
| 273 |
fseek(&file, curr_pos, SEEK_SET); |
| 274 |
return EResult::InvalidMagicNumber; |
| 275 |
} |
| 276 |
|
| 277 |
// check contents |
| 278 |
if (check_contents) { |
| 279 |
fseek(&file, 0, SEEK_END); |
| 280 |
const long file_size = ftell(&file); |
| 281 |
rewind(&file); |
| 282 |
|
| 283 |
// read header |
| 284 |
FileHeader file_header; |
| 285 |
EResult res = read_header(file, file_header, nullptr); |
| 286 |
if (res != EResult::Success) { |
| 287 |
// restore file position |
| 288 |
fseek(&file, curr_pos, SEEK_SET); |
| 289 |
// propagate error |
| 290 |
return res; |
| 291 |
} |
| 292 |
BlockHeader block_header; |
| 293 |
// read file metadata block header, if present |
| 294 |
res = read_next_block_header(file, file_header, block_header, cs_buffer, cs_buffer_size); |
| 295 |
if (res != EResult::Success) { |
| 296 |
// restore file position |
| 297 |
fseek(&file, curr_pos, SEEK_SET); |
| 298 |
// propagate error |
| 299 |
return res; |
| 300 |
} |
| 301 |
if ((EBlockType)block_header.type != EBlockType::FileMetadata && |
| 302 |
(EBlockType)block_header.type != EBlockType::PrinterMetadata) { |
| 303 |
// restore file position |
| 304 |
fseek(&file, curr_pos, SEEK_SET); |
| 305 |
return EResult::InvalidBlockType; |
| 306 |
} |
| 307 |
|
| 308 |
// read printer metadata block header, if file metadata block is present |
| 309 |
if ((EBlockType)block_header.type == EBlockType::FileMetadata) { |
| 310 |
res = skip_block(file, file_header, block_header); |
| 311 |
if (res != EResult::Success) { |
| 312 |
// restore file position |
| 313 |
fseek(&file, curr_pos, SEEK_SET); |
| 314 |
// propagate error |
| 315 |
return res; |
| 316 |
} |
| 317 |
res = read_next_block_header(file, file_header, block_header, cs_buffer, cs_buffer_size); |
| 318 |
if (res != EResult::Success) { |
| 319 |
// restore file position |
| 320 |
fseek(&file, curr_pos, SEEK_SET); |
| 321 |
// propagate error |
| 322 |
return res; |
| 323 |
} |
| 324 |
} |
| 325 |
if ((EBlockType)block_header.type != EBlockType::PrinterMetadata) { |
| 326 |
// restore file position |
| 327 |
fseek(&file, curr_pos, SEEK_SET); |
| 328 |
return EResult::InvalidBlockType; |
| 329 |
} |
| 330 |
|
| 331 |
// read thumbnails block headers, if present |
| 332 |
res = skip_block(file, file_header, block_header); |
| 333 |
if (res != EResult::Success) { |
| 334 |
// restore file position |
| 335 |
fseek(&file, curr_pos, SEEK_SET); |
| 336 |
// propagate error |
| 337 |
return res; |
| 338 |
} |
| 339 |
res = read_next_block_header(file, file_header, block_header, cs_buffer, cs_buffer_size); |
| 340 |
if (res != EResult::Success) { |
| 341 |
// restore file position |
| 342 |
fseek(&file, curr_pos, SEEK_SET); |
| 343 |
// propagate error |
| 344 |
return res; |
| 345 |
} |
| 346 |
while ((EBlockType)block_header.type == EBlockType::Thumbnail) { |
| 347 |
res = skip_block(file, file_header, block_header); |
| 348 |
if (res != EResult::Success) { |
| 349 |
// restore file position |
| 350 |
fseek(&file, curr_pos, SEEK_SET); |
| 351 |
// propagate error |
| 352 |
return res; |
| 353 |
} |
| 354 |
res = read_next_block_header(file, file_header, block_header, cs_buffer, cs_buffer_size); |
| 355 |
if (res != EResult::Success) { |
| 356 |
// restore file position |
| 357 |
fseek(&file, curr_pos, SEEK_SET); |
| 358 |
// propagate error |
| 359 |
return res; |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
// read print metadata block header |
| 364 |
if ((EBlockType)block_header.type != EBlockType::PrintMetadata) { |
| 365 |
// restore file position |
| 366 |
fseek(&file, curr_pos, SEEK_SET); |
| 367 |
return EResult::InvalidBlockType; |
| 368 |
} |
| 369 |
|
| 370 |
for (size_t i = 0; i < 2; i++) { |
| 371 |
// read slicer metadata block header |
| 372 |
res = skip_block(file, file_header, block_header); |
| 373 |
if (res != EResult::Success) { |
| 374 |
// restore file position |
| 375 |
fseek(&file, curr_pos, SEEK_SET); |
| 376 |
// propagate error |
| 377 |
return res; |
| 378 |
} |
| 379 |
res = read_next_block_header(file, file_header, block_header, cs_buffer, cs_buffer_size); |
| 380 |
if (res != EResult::Success) { |
| 381 |
// restore file position |
| 382 |
fseek(&file, curr_pos, SEEK_SET); |
| 383 |
// propagate error |
| 384 |
return res; |
| 385 |
} |
| 386 |
if ((EBlockType)block_header.type != EBlockType::SlicerMetadata && i == 0) { |
| 387 |
// restore file position |
| 388 |
fseek(&file, curr_pos, SEEK_SET); |
| 389 |
return EResult::InvalidBlockType; |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
// read gcode block headers |
| 394 |
do { |
| 395 |
res = skip_block(file, file_header, block_header); |
| 396 |
if (res != EResult::Success) { |
| 397 |
// restore file position |
| 398 |
fseek(&file, curr_pos, SEEK_SET); |
| 399 |
// propagate error |
| 400 |
return res; |
| 401 |
} |
| 402 |
if (ftell(&file) == file_size) |
| 403 |
break; |
| 404 |
res = read_next_block_header(file, file_header, block_header, cs_buffer, cs_buffer_size); |
| 405 |
if (res != EResult::Success) { |
| 406 |
// restore file position |
| 407 |
fseek(&file, curr_pos, SEEK_SET); |
| 408 |
// propagate error |
| 409 |
return res; |
| 410 |
} |
| 411 |
if ((EBlockType)block_header.type != EBlockType::GCode) { |
| 412 |
// restore file position |
| 413 |
fseek(&file, curr_pos, SEEK_SET); |
| 414 |
return EResult::InvalidBlockType; |
| 415 |
} |
| 416 |
} while (!feof(&file)); |
| 417 |
} |
| 418 |
|
| 419 |
fseek(&file, curr_pos, SEEK_SET); |
| 420 |
return EResult::Success; |
| 421 |
} |
| 422 |
|
| 423 |
BGCODE_CORE_EXPORT EResult read_header(FILE& file, FileHeader& header, const uint32_t* const max_version) |
| 424 |
{ |
| 425 |
rewind(&file); |
| 426 |
return header.read(file, max_version); |
| 427 |
} |
| 428 |
|
| 429 |
BGCODE_CORE_EXPORT EResult read_next_block_header(FILE& file, const FileHeader& file_header, BlockHeader& block_header, |
| 430 |
std::byte* cs_buffer, size_t cs_buffer_size) |
| 431 |
{ |
| 432 |
EResult res = block_header.read(file); |
| 433 |
if (res == EResult::Success && cs_buffer != nullptr && cs_buffer_size > 0) { |
| 434 |
res = verify_block_checksum(file, file_header, block_header, cs_buffer, cs_buffer_size); |
| 435 |
// return to payload position after checksum verification |
| 436 |
if (fseek(&file, block_header.get_position() + static_cast<long>(block_header.get_size()), SEEK_SET) != 0) |
| 437 |
res = EResult::ReadError; |
| 438 |
} |
| 439 |
|
| 440 |
return res; |
| 441 |
} |
| 442 |
|
| 443 |
BGCODE_CORE_EXPORT EResult read_next_block_header(FILE& file, const FileHeader& file_header, BlockHeader& block_header, EBlockType type, |
| 444 |
std::byte* cs_buffer, size_t cs_buffer_size) |
| 445 |
{ |
| 446 |
// cache file position |
| 447 |
const long curr_pos = ftell(&file); |
| 448 |
|
| 449 |
do { |
| 450 |
EResult res = read_next_block_header(file, file_header, block_header, nullptr, 0); // intentionally skip checksum verification |
| 451 |
if (res != EResult::Success) |
| 452 |
// propagate error |
| 453 |
return res; |
| 454 |
else if (feof(&file)) { |
| 455 |
// block not found |
| 456 |
// restore file position |
| 457 |
fseek(&file, curr_pos, SEEK_SET); |
| 458 |
return EResult::BlockNotFound; |
| 459 |
} |
| 460 |
else if ((EBlockType)block_header.type == type) { |
| 461 |
// block found |
| 462 |
if (cs_buffer != nullptr && cs_buffer_size > 0) { |
| 463 |
// checksum verification requested |
| 464 |
res = verify_block_checksum(file, file_header, block_header, cs_buffer, cs_buffer_size); |
| 465 |
// return to payload position after checksum verification |
| 466 |
if (fseek(&file, block_header.get_position() + (long)block_header.get_size(), SEEK_SET) != 0) |
| 467 |
res = EResult::ReadError; |
| 468 |
return res; // propagate error or success |
| 469 |
} |
| 470 |
return EResult::Success; |
| 471 |
} |
| 472 |
|
| 473 |
if (!feof(&file)) { |
| 474 |
res = skip_block(file, file_header, block_header); |
| 475 |
if (res != EResult::Success) |
| 476 |
// propagate error |
| 477 |
return res; |
| 478 |
} |
| 479 |
} while (true); |
| 480 |
} |
| 481 |
|
| 482 |
BGCODE_CORE_EXPORT size_t block_parameters_size(EBlockType type) |
| 483 |
{ |
| 484 |
switch (type) |
| 485 |
{ |
| 486 |
case EBlockType::FileMetadata: { return sizeof(uint16_t); } /* encoding_type */ |
| 487 |
case EBlockType::GCode: { return sizeof(uint16_t); } /* encoding_type */ |
| 488 |
case EBlockType::SlicerMetadata: { return sizeof(uint16_t); } /* encoding_type */ |
| 489 |
case EBlockType::PrinterMetadata: { return sizeof(uint16_t); } /* encoding_type */ |
| 490 |
case EBlockType::PrintMetadata: { return sizeof(uint16_t); } /* encoding_type */ |
| 491 |
case EBlockType::Thumbnail: { return sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint16_t); } /* format, width, height */ |
| 492 |
} |
| 493 |
return 0; |
| 494 |
} |
| 495 |
|
| 496 |
BGCODE_CORE_EXPORT EResult skip_block_content(FILE& file, const FileHeader& file_header, const BlockHeader& block_header) |
| 497 |
{ |
| 498 |
fseek(&file, (long)block_content_size(file_header, block_header), SEEK_CUR); |
| 499 |
return ferror(&file) ? EResult::ReadError : EResult::Success; |
| 500 |
} |
| 501 |
|
| 502 |
BGCODE_CORE_EXPORT EResult skip_block(FILE& file, const FileHeader& file_header, const BlockHeader& block_header) |
| 503 |
{ |
| 504 |
fseek(&file, block_header.get_position() + (long)block_header.get_size() + (long)block_content_size(file_header, block_header), SEEK_SET); |
| 505 |
return ferror(&file) ? EResult::ReadError : EResult::Success; |
| 506 |
} |
| 507 |
|
| 508 |
BGCODE_CORE_EXPORT size_t block_payload_size(const BlockHeader& block_header) |
| 509 |
{ |
| 510 |
size_t ret = block_parameters_size((EBlockType)block_header.type); |
| 511 |
ret += ((ECompressionType)block_header.compression == ECompressionType::None) ? |
| 512 |
block_header.uncompressed_size : block_header.compressed_size; |
| 513 |
return ret; |
| 514 |
} |
| 515 |
|
| 516 |
BGCODE_CORE_EXPORT size_t checksum_size(EChecksumType type) |
| 517 |
{ |
| 518 |
switch (type) |
| 519 |
{ |
| 520 |
case EChecksumType::None: { return 0; } |
| 521 |
case EChecksumType::CRC32: { return 4; } |
| 522 |
} |
| 523 |
return 0; |
| 524 |
} |
| 525 |
|
| 526 |
BGCODE_CORE_EXPORT size_t block_content_size(const FileHeader& file_header, const BlockHeader& block_header) |
| 527 |
{ |
| 528 |
return block_payload_size(block_header) + checksum_size((EChecksumType)file_header.checksum_type); |
| 529 |
} |
| 530 |
|
| 531 |
uint32_t bgcode_version() noexcept |
| 532 |
{ |
| 533 |
return VERSION; |
| 534 |
} |
| 535 |
|
| 536 |
const char *version() noexcept |
| 537 |
{ |
| 538 |
return LibBGCode_VERSION; |
| 539 |
} |
| 540 |
|
| 541 |
} // namespace core |
| 542 |
} // namespace bgcode |