zlib 怎样压缩 SVG 并检查还原的图稿?
用 64 条线段生成 SVG,逐字节验证解压结果,再翻转校验字段中的一个位。
示例实测 zlib 1.4 将 1,383 字节的 SVG 压为 257 字节,解码后逐字节一致。改坏校验字段后返回 Z_DATA_ERROR。
图稿压缩后,怎样确认打开的是原内容?
雕刻编辑器可以在本地素材包或自动保存缓存中压缩 SVG 字节,载入后二次解析图稿。zlib 是 Jean-loup Gailly 和 Mark Adler 开发的压缩库。本篇用固定的 1.4 版本生成一张带有 64 条线段的 SVG,对压缩后还原的字节逐个比较,并故意改坏压缩流的校验字段。
重复标记怎样变短,校验在哪一步发生?
compress2 用 DEFLATE 对输入字节编码,再加上 zlib 封装。本例重复出现 <path d= 和 0v16。编码器可在滑动窗口中找相同的字节串,用长度和回溯距离引用旧内容,再把字面量与长度、距离符号交给 Huffman 编码。可以在 deflate.c 从 deflate_slow 和 longest_match 跟读默认策略下级别 6 的路径。级别 6 使用的匹配与输出逻辑
uncompress2 调用解码器,并回报实际消耗的输入长度。inflate.c 的 CHECK 状态将已解码字节的 Adler-32 与 zlib 封装末尾的值比较。不一致时进入错误状态,调用方得到 Z_DATA_ERROR。示例单独打印原 SVG 的 CRC-32 作为复现实验指纹,损坏测试改的是 zlib 封装里的 Adler-32。解压入口与输入计数 · 解码校验状态
压缩比取决于文件内容和级别,重复标签多的 SVG 在这里很容易压缩。产品读取未知图稿时,可以使用 inflate 分批解码,并对单块和整个包同时设置解码字节上限。Adler-32 和 CRC-32 用来检测意外损坏,文件真实性需要额外的可信验证机制。通过字节校验之后,应用仍需要验证 SVG 结构和支持的图形元素。zlib 接口手册
在本地构建并运行
在 macOS 或 Linux 的新工作目录运行。需要 curl、tar、make 和支持 C++17 的 C/C++ 工具链。示例直接链接 zlib 1.4 源码构建生成的 libz.a,SVG 在内存生成与还原。实测环境为 Apple Silicon macOS。
查看示例源码
#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-demo运行结果
zlib 1.4
svg_bytes=1383 compressed_bytes=257
roundtrip=equal crc32=21731948
changed_trailer=Z_DATA_ERROR
使用同一份 SVG,级别 0 的输出是 1,394 字节,级别 9 是 255 字节。两次解码都逐字节还原了原图稿,CRC-32 也一致。