| 1 | #ifndef BGCODE_CORE_HPP |
| 2 | #define BGCODE_CORE_HPP |
| 3 | |
| 4 | #include "core/export.h" |
| 5 | |
| 6 | #include <cstdio> |
| 7 | #include <cstdint> |
| 8 | #include <cstddef> |
| 9 | #include <climits> |
| 10 | #include <array> |
| 11 | #include <vector> |
| 12 | #include <string> |
| 13 | #include <string_view> |
| 14 | |
| 15 | namespace bgcode { namespace core { |
| 16 | |
| 17 | enum class EResult : uint16_t |
| 18 | { |
| 19 | Success, |
| 20 | ReadError, |
| 21 | WriteError, |
| 22 | InvalidMagicNumber, |
| 23 | InvalidVersionNumber, |
| 24 | InvalidChecksumType, |
| 25 | InvalidBlockType, |
| 26 | InvalidCompressionType, |
| 27 | InvalidMetadataEncodingType, |
| 28 | InvalidGCodeEncodingType, |
| 29 | DataCompressionError, |
| 30 | DataUncompressionError, |
| 31 | MetadataEncodingError, |
| 32 | MetadataDecodingError, |
| 33 | GCodeEncodingError, |
| 34 | GCodeDecodingError, |
| 35 | BlockNotFound, |
| 36 | InvalidChecksum, |
| 37 | InvalidThumbnailFormat, |
| 38 | InvalidThumbnailWidth, |
| 39 | InvalidThumbnailHeight, |
| 40 | InvalidThumbnailDataSize, |
| 41 | InvalidBinaryGCodeFile, |
| 42 | InvalidAsciiGCodeFile, |
| 43 | InvalidSequenceOfBlocks, |
| 44 | InvalidBuffer, |
| 45 | AlreadyBinarized, |
| 46 | MissingPrinterMetadata, |
| 47 | MissingPrintMetadata, |
| 48 | MissingSlicerMetadata, |
| 49 | }; |
| 50 | |
| 51 | enum class EChecksumType : uint16_t |
| 52 | { |
| 53 | None, |
| 54 | CRC32 |
| 55 | }; |
| 56 | |
| 57 | enum class EBlockType : uint16_t |
| 58 | { |
| 59 | FileMetadata, |
| 60 | GCode, |
| 61 | SlicerMetadata, |
| 62 | PrinterMetadata, |
| 63 | PrintMetadata, |
| 64 | Thumbnail |
| 65 | }; |
| 66 | |
| 67 | enum class ECompressionType : uint16_t |
| 68 | { |
| 69 | None, |
| 70 | Deflate, |
| 71 | Heatshrink_11_4, |
| 72 | Heatshrink_12_4 |
| 73 | }; |
| 74 | |
| 75 | enum class EMetadataEncodingType : uint16_t |
| 76 | { |
| 77 | INI, |
| 78 | JSON |
| 79 | }; |
| 80 | |
| 81 | enum class EGCodeEncodingType : uint16_t |
| 82 | { |
| 83 | None, |
| 84 | MeatPack, |
| 85 | MeatPackComments |
| 86 | }; |
| 87 | |
| 88 | enum class EThumbnailFormat : uint16_t |
| 89 | { |
| 90 | PNG, |
| 91 | JPG, |
| 92 | QOI |
| 93 | }; |
| 94 | |
| 95 | struct BGCODE_CORE_EXPORT FileHeader |
| 96 | { |
| 97 | uint32_t magic; |
| 98 | uint32_t version; |
| 99 | uint16_t checksum_type; |
| 100 | |
| 101 | FileHeader(); |
| 102 | FileHeader(uint32_t mg, uint32_t ver, uint16_t chk_type); |
| 103 | |
| 104 | EResult write(FILE& file) const; |
| 105 | EResult read(FILE& file, const uint32_t* const max_version); |
| 106 | }; |
| 107 | |
| 108 | struct BGCODE_CORE_EXPORT BlockHeader |
| 109 | { |
| 110 | uint16_t type{ 0 }; |
| 111 | uint16_t compression{ 0 }; |
| 112 | uint32_t uncompressed_size{ 0 }; |
| 113 | uint32_t compressed_size{ 0 }; |
| 114 | |
| 115 | BlockHeader() = default; |
| 116 | BlockHeader(uint16_t type, uint16_t compression, uint32_t uncompressed_size, uint32_t compressed_size = 0); |
| 117 | |
| 118 | // Returns the position of this block in the file. |
| 119 | // Position is set by calling write() and read() methods. |
| 120 | long get_position() const; |
| 121 | |
| 122 | EResult write(FILE& file); |
| 123 | EResult read(FILE& file); |
| 124 | |
| 125 | // Returs the size of this BlockHeader, in bytes |
| 126 | size_t get_size() const; |
| 127 | |
| 128 | private: |
| 129 | long m_position{ 0 }; |
| 130 | }; |
| 131 | |
| 132 | struct BGCODE_CORE_EXPORT ThumbnailParams |
| 133 | { |
| 134 | uint16_t format; |
| 135 | uint16_t width; |
| 136 | uint16_t height; |
| 137 | |
| 138 | EResult write(FILE& file) const; |
| 139 | EResult read(FILE& file); |
| 140 | }; |
| 141 | |
| 142 | // Returns a string description of the given result |
| 143 | extern BGCODE_CORE_EXPORT std::string_view translate_result(EResult result); |
| 144 | |
| 145 | // Returns EResult::Success if the given file is a valid binary gcode |
| 146 | // If check_contents is set to true, the order of the blocks is checked |
| 147 | // Does not modify the file position |
| 148 | // Caller is responsible for providing buffer for checksum calculation, if needed. |
| 149 | extern BGCODE_CORE_EXPORT EResult is_valid_binary_gcode(FILE& file, bool check_contents = false, std::byte* cs_buffer = nullptr, |
| 150 | size_t cs_buffer_size = 0); |
| 151 | |
| 152 | // Reads the file header. |
| 153 | // If max_version is not null, version is checked against the passed value. |
| 154 | // If return == EResult::Success: |
| 155 | // - header will contain the file header. |
| 156 | // - file position will be set at the start of the 1st block header. |
| 157 | extern BGCODE_CORE_EXPORT EResult read_header(FILE& file, FileHeader& header, const uint32_t* const max_version); |
| 158 | |
| 159 | // Reads next block header from the current file position. |
| 160 | // File position must be at the start of a block header. |
| 161 | // If return == EResult::Success: |
| 162 | // - block_header will contain the header of the block. |
| 163 | // - file position will be set at the start of the block parameters data. |
| 164 | // Caller is responsible for providing buffer for checksum calculation, if needed. |
| 165 | extern BGCODE_CORE_EXPORT EResult read_next_block_header(FILE& file, const FileHeader& file_header, BlockHeader& block_header, |
| 166 | std::byte* cs_buffer = nullptr, size_t cs_buffer_size = 0); |
| 167 | |
| 168 | // Searches and reads next block header with the given type from the current file position. |
| 169 | // File position must be at the start of a block header. |
| 170 | // If return == EResult::Success: |
| 171 | // - block_header will contain the header of the block with the required type. |
| 172 | // - file position will be set at the start of the block parameters data. |
| 173 | // otherwise: |
| 174 | // - file position will keep the current value. |
| 175 | // Caller is responsible for providing buffer for checksum calculation, if needed. |
| 176 | extern BGCODE_CORE_EXPORT EResult read_next_block_header(FILE& file, const FileHeader& file_header, BlockHeader& block_header, EBlockType type, |
| 177 | std::byte* cs_buffer = nullptr, size_t cs_buffer_size = 0); |
| 178 | |
| 179 | // Calculates block checksum and verify it against checksum stored in file. |
| 180 | // Caller is responsible for providing buffer for checksum calculation, bigger buffer means faster calculation and vice versa. |
| 181 | // If return == EResult::Success: |
| 182 | // - file position will be set at the start of the next block header. |
| 183 | extern BGCODE_CORE_EXPORT EResult verify_block_checksum(FILE& file, const FileHeader& file_header, const BlockHeader& block_header, std::byte* buffer, |
| 184 | size_t buffer_size); |
| 185 | |
| 186 | // Skips the content (parameters + data + checksum) of the block with the given block header. |
| 187 | // File position must be at the start of the block parameters. |
| 188 | // If return == EResult::Success: |
| 189 | // - file position will be set at the start of the next block header. |
| 190 | extern BGCODE_CORE_EXPORT EResult skip_block_content(FILE& file, const FileHeader& file_header, const BlockHeader& block_header); |
| 191 | |
| 192 | // Skips the block with the given block header. |
| 193 | // File position must be set by a previous call to BlockHeader::write() or BlockHeader::read(). |
| 194 | // If return == EResult::Success: |
| 195 | // - file position will be set at the start of the next block header. |
| 196 | extern BGCODE_CORE_EXPORT EResult skip_block(FILE& file, const FileHeader& file_header, const BlockHeader& block_header); |
| 197 | |
| 198 | // Returns the size of the parameters of the given block type, in bytes. |
| 199 | extern BGCODE_CORE_EXPORT size_t block_parameters_size(EBlockType type); |
| 200 | |
| 201 | // Returns the size of the payload (parameters + data) of the block with the given header, in bytes. |
| 202 | extern BGCODE_CORE_EXPORT size_t block_payload_size(const BlockHeader& block_header); |
| 203 | |
| 204 | // Returns the size of the checksum of the given type, in bytes. |
| 205 | extern BGCODE_CORE_EXPORT size_t checksum_size(EChecksumType type); |
| 206 | |
| 207 | // Returns the size of the content (parameters + data + checksum) of the block with the given header, in bytes. |
| 208 | extern BGCODE_CORE_EXPORT size_t block_content_size(const FileHeader& file_header, const BlockHeader& block_header); |
| 209 | |
| 210 | // Highest version of the binary format supported by this library instance |
| 211 | extern BGCODE_CORE_EXPORT uint32_t bgcode_version() noexcept; |
| 212 | |
| 213 | // Version of the library |
| 214 | extern BGCODE_CORE_EXPORT const char* version() noexcept; |
| 215 | |
| 216 | }} // bgcode::core |
| 217 | |
| 218 | #endif // BGCODE_CORE_HPP |