Introduction
Binary G-code is a compact container for G-code, introduced by Prusa Research in 2023 for its printers and written by PrusaSlicer. It holds the same commands as a text G-code file, but compressed and split into blocks, with the print settings and preview images stored in their own blocks so a printer can show them without reading the whole file.
Files are often several times smaller than the text form, which speeds up transfer over a network. Prusa publishes the specification and a library, and the files can be converted back to plain G-code.
Structure
A binary G-code file starts with the four bytes GCDE, a version number
and the type of checksum used. Blocks follow, each with a header giving its type, its
compression and its sizes, then its parameters, its data and an optional CRC-32
checksum. Metadata blocks come first: printer metadata, thumbnails, print metadata and
slicer metadata. The G-code itself follows in a series of blocks, compressed with
Deflate or Heatshrink and optionally packed with MeatPack, which stores common G-code
characters in fewer bits.
0File headerGCDE, version, checksum type10 bytes10Metadata blocks printer, thumbnails, print, slicervaries…G-code blocks the compressed commandsvaries
In this reference
- Header and blocks – the file header, block header, every block type (including the firmware’s identity, key and encrypted blocks for encrypted files), compression, encoding and thumbnail code, checksums, the Heatshrink bit stream, what the printer firmware accepts, and MeatPack with a worked example
Not described
Known, not yet covered
- Viewer: the payload of an encrypted block (type 8) whose key block uses encryption 1 (the AES and HMAC keys RSA-encrypted to one printer) is shown as bytes (“encrypted, not decoded”) and its HMAC is not checked: the keys are inside an RSA-OAEP ciphertext that only that printer’s private key opens, which no file holds. Blocks with keys in the clear (encryption 0) are decrypted, their HMACs checked and the inner G-code listed (what the file alone allows).
- Viewer: the identity block’s RSASSA-PSS signature, the signature of a key block of encryption 1, and the intro and key block hashes are verified from what the file holds (the signatures with the identity’s own public key), but whether the signer’s key is one to trust is the printer’s policy and is not shown as verified: no list of known identities is available to a viewer. The signature check accepts any PSS salt length, as the firmware’s verify call takes none; the salt length writers use is unknown.
- Viewer: opening a file reads all of it once, for the CRC-32 of every block, the hashes and HMACs, and to decode every G-code block (to count its lines and find its errors), so a file of several gigabytes opens slowly. Deflate, Heatshrink and MeatPack data are all decoded in windows with checkpoints, whatever the size.
- Viewer: every block of the file is read the same way, however many there are: the first 100,000 are kept as records and the rest as a compact table (which only bounds the memory a file with very many blocks needs), one pass decodes and checks each block in turn (CRC-32, order, data, MeatPack, Heatshrink, Deflate) and keeps its problems and totals, and a block is read again, in windows, when its node, table row or entry is opened. The G-code, Metadata, Thumbnails and Encryption tabs list late blocks too. Identity and key blocks are checked wherever the order rules allow them: the intro hash and the key block hash are streamed over the late blocks (only the hash states and the key material are kept), and the encrypted blocks after a late key block in the clear are decrypted. Tested at the real limit with a file of 130,007 blocks built page by page in memory, and with a file of 100,001 thumbnails followed by an identity block, a key block and encrypted G-code (a damaged late thumbnail fails the intro hash).
- Viewer: a G-code line over 3,000 characters and a metadata key or value over 4,000 are cut in the lists, with the cut written in the cell and the length given; the whole line opens as an entry (and, in a file that stores the text plainly, in the hex view). JSON metadata over 256 MiB is not listed member by member: its row opens the block as an entry that the JSON viewer reads in windows. A thumbnail over 64 MiB is not decoded to pixels (it opens whole, in windows). Every problem is listed, a page at a time, in the Problems tab (the panel above the tabs lists the first 500 and the rest in pages of 200), and every thumbnail and metadata entry is listed, a page at a time.
Unknown
- Note (does not block the reference): which software writes identity, key and encrypted blocks, and with what policy, is not stated in the sources read (Prusa-Firmware-Buddy only reads them). Block types 6 to 8 exist only in the firmware’s copy of libbgcode, so the firmware reader is the definition.
- Note (does not block the reference): the RSA-PSS salt length writers use is not stated anywhere read; the firmware’s reader accepts any, because
mbedtls_rsa_rsassa_pss_verifycalls the extended verify withMBEDTLS_RSA_SALT_LEN_ANY(mbedtls 2.16.12 and 2.28.8library/rsa.c; the firmware’s exact mbedtls version was not read). - Note (does not block the reference): whether the public libbgcode
mainbranch will define block types 6 to 8 is not known; today only the firmware’s copy of the library does. - Note (does not block the reference): what the printer’s user interface does with the identity flag bit 0 (one-time identity) was not read; the sources read only carry it to the identity check.
Links
Software
- PrusaSlicer – Prusa Research’s free open source slicer, which writes binary G-code (Windows, macOS, Linux)
Developers
- libbgcode – Prusa’s library and command line tool for reading, writing and converting binary G-code, with the specification (C++)
Reference
- Binary G-code specification – Prusa’s description of the header, blocks and codes