How does zlib compress and check SVG artwork?
Generate an SVG with 64 line segments, verify the decoded bytes, then flip one bit in the checksum field.
Example result zlib 1.4 compresses the 1,383-byte SVG to 257 bytes and restores every byte. A changed checksum field returns Z_DATA_ERROR.
How can an editor check restored artwork?
An engraving editor can compress SVG bytes inside a local asset bundle or an autosave cache, then decode them for parsing. zlib is a compression library written by Jean-loup Gailly and Mark Adler. This study uses version 1.4 to round-trip a generated SVG with 64 line segments, compares the restored bytes with the original, and deliberately alters the compressed stream's checksum field.
zlib project and release information · zlib 1.4 source · zlib license
Compress repeated markup and check the decoded bytes
compress2 encodes the input with DEFLATE inside a zlib wrapper. Strings such as <path d= and 0v16 repeat throughout this SVG. Within its sliding window, the encoder can represent a matching sequence by its length and backward distance. DEFLATE then Huffman-codes literal, length, and distance symbols. Follow deflate_slow and longest_match in deflate.c to see how this works at level 6 with the default strategy. Matching and output at level 6
uncompress2 calls the decoder and reports the actual input bytes consumed. In the CHECK state of inflate.c, the decoder compares the Adler-32 of the decoded bytes against the value in the zlib trailer. A mismatch puts the decoder in an error state and the caller gets Z_DATA_ERROR. The demo prints a separate CRC-32 of the original SVG as a reproduction fingerprint. The deliberately changed field is the Adler-32 in the zlib trailer. Decompression entry point and input accounting · Decoder checksum state
Compression depends on the content and level. The repeated tags make this SVG particularly compressible. A reader for arbitrary assets can use inflate in chunks and cap decoded bytes per chunk and across the bundle. Adler-32 and CRC-32 detect accidental corruption. Authenticity takes a separate verification mechanism that the application trusts. The editor still needs to validate the restored SVG structure and supported elements. zlib API manual
Build and run locally
Use a fresh directory on macOS or Linux with curl, tar, make, and a C/C++ toolchain that supports C++17. Link the demo directly to libz.a built from zlib 1.4. All SVG generation and recovery happen in memory. It was run on Apple Silicon macOS.
Download the C++17 example · Build commands in plain text
View the example source
#include <zlib.h>
#include <iomanip>
#include <iostream>
#include <stdexcept>
#include <string>
#include <string_view>
#include <vector>
int main() try {
if (std::string_view(zlibVersion()) != "1.4") {
throw std::runtime_error("This demo pins zlib 1.4");
}
std::string svg = R"(<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 128 16" stroke="black" fill="none">)";
for (int x = 0; x < 128; x += 2) {
svg += "<path d=\"M" + std::to_string(x) + " 0v16\"/>";
}
svg += "</svg>";
const auto* input = reinterpret_cast<const Bytef*>(svg.data());
const auto inputSize = static_cast<uLong>(svg.size());
uLongf compressedSize = compressBound(inputSize);
std::vector<Bytef> compressed(compressedSize);
if (compress2(compressed.data(), &compressedSize, input, inputSize, 6) != Z_OK) {
throw std::runtime_error("Could not compress SVG");
}
// This local fixture has a known size. Bound the output buffer to that size.
std::string decoded(svg.size(), '\0');
auto* output = reinterpret_cast<Bytef*>(decoded.data());
uLongf decodedSize = static_cast<uLongf>(decoded.size());
uLong consumedSize = compressedSize;
if (uncompress2(output, &decodedSize, compressed.data(), &consumedSize) != Z_OK ||
decodedSize != inputSize || consumedSize != compressedSize || decoded != svg) {
throw std::runtime_error("Round trip mismatch");
}
// Flip one bit in the zlib wrapper's trailing Adler-32 field.
compressed[compressedSize - 1] ^= 1;
decodedSize = static_cast<uLongf>(decoded.size());
consumedSize = compressedSize;
const int status = uncompress2(output, &decodedSize, compressed.data(), &consumedSize);
std::cout << "zlib " << zlibVersion() << '\n'
<< "svg_bytes=" << svg.size() << " compressed_bytes=" << compressedSize << '\n'
<< "roundtrip=equal crc32=" << std::hex << std::setfill('0') << std::setw(8)
<< crc32(0L, input, static_cast<uInt>(inputSize)) << '\n'
<< "changed_trailer=" << (status == Z_DATA_ERROR ? "Z_DATA_ERROR" : "unexpected")
<< '\n';
return status == Z_DATA_ERROR ? 0 : 1;
} catch (const std::exception& error) {
std::cerr << error.what() << '\n';
return 1;
}mkdir zlib-artwork-study && cd zlib-artwork-study
curl -fsSL https://zlib.net/fossils/zlib-1.4.tar.gz -o zlib.tar.gz
tar -xzf zlib.tar.gz
curl -fsSLO https://www.juyaoai.com/examples/zlib-artwork-roundtrip/demo.cpp
(cd zlib-1.4 && ./configure --static && make libz.a)
c++ -std=c++17 -I zlib-1.4 demo.cpp zlib-1.4/libz.a -o zlib-demo
./zlib-demoRun output
zlib 1.4
svg_bytes=1383 compressed_bytes=257
roundtrip=equal crc32=21731948
changed_trailer=Z_DATA_ERROR
With the same SVG, level 0 produced 1,394 bytes and level 9 produced 255 bytes. Both returned identical SVG bytes with the same original fingerprint.