Something went wrong. Try again.
Brotli (RFC 7932) compression and decompression for Zig 0.17, with sans-I/O streaming cores and std.Io adapters.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128#!/usr/bin/env python3# SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us># SPDX-License-Identifier: MIT
"""Converts the reference implementation's synthetic decoder tests to Zig.
The Brotli repository's go/brotli/synth_test.go holds several dozen streamswritten by hand to reach corners of the format that real compressors never do:every transform, every window size, padding and reserved bits set where theymust not be, codes that are not quite complete. Each comes with whether adecoder must accept it and, if so, what it decodes to.
This reads that file and writes the same cases as a Zig table:
python3 tools/convert_synth.py path/to/synth_test.go > tests/synth.zig
Expected outputs are written the way the Go file writes them, as literalpieces and repetitions, since one of them is 16 MiB of asterisks."""
import reimport sys
STRING = r'"(?:[^"\\]|\\.)*"'
def go_string(lit): """The bytes a Go interpreted string literal stands for.""" body = lit[1:-1] out = bytearray() i = 0 while i < len(body): c = body[i] if c != "\\": out += c.encode("utf-8") i += 1 continue e = body[i + 1] if e == "x": out.append(int(body[i + 2 : i + 4], 16)) i += 4 elif e in "01234567": out.append(int(body[i + 1 : i + 4], 8)) i += 4 else: out.append({"n": 10, "t": 9, "r": 13, "\\": 92, '"': 34, "'": 39, "a": 7, "b": 8, "f": 12, "v": 11}[e]) i += 2 return bytes(out)
def zig_string(b): s = "" for c in b: if c == 0x22: s += '\\"' elif c == 0x5C: s += "\\\\" elif c == 0x0A: s += "\\n" elif c == 0x09: s += "\\t" elif 0x20 <= c < 0x7F: s += chr(c) else: s += "\\x%02x" % c return '"' + s + '"'
def main(path): src = open(path).read() funcs = re.split(r"\nfunc Test", src)[1:] # REUSE-IgnoreStart print("// SPDX-FileCopyrightText: 2025 Google Inc.") print("// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>") print("// SPDX-License-Identifier: MIT") # REUSE-IgnoreEnd print() print("//! The synthetic decoder tests of the Brotli reference implementation,") print("//! converted from go/brotli/synth_test.go by tools/convert_synth.py.") print("//! Do not edit; regenerate.") print() print('const Case = @import("synth_case.zig").Case;') print() print("pub const cases = [_]Case{") for f in funcs: name = re.match(r"(\w+)\(", f).group(1) body = f[f.index("compressed := []uint8{") :] data = body[: body.index("}")] nums = [int(x, 16) for x in re.findall(r"0x([0-9a-fA-F]{2})", data)] rest = body[body.index("verify(t,") :] # Drop the comment describing the stream; what follows is the # expectation and then the expected output. rest = re.sub(r"/\*.*?\*/", "", rest, flags=re.S) rest = rest.split("/* GENERATED CODE END */")[0] m = re.match(r"verify\(t,\s*compressed,\s*(true|false),\s*(.*?),?\s*\);?\s*\}\s*$", rest.strip(), re.S) ok = m.group(1) == "true" expr = m.group(2).strip() pieces = [] for t in re.finditer(r"times\((\d+),\s*(" + STRING + r")\)|(" + STRING + r")", expr): if t.group(1): pieces.append(".{ .repeat = .{ .count = %s, .pattern = %s } }" % (t.group(1), zig_string(go_string(t.group(2))))) else: s = go_string(t.group(3)) if s: pieces.append(".{ .text = %s }" % zig_string(s)) print(" .{") print(' .name = "%s",' % name) print(" .ok = %s," % ("true" if ok else "false")) if "t.Skip(" in f: print(" .skipped_upstream = true,") print(" .compressed = &.{") for i in range(0, len(nums), 12): print(" " + " ".join("0x%02x," % n for n in nums[i : i + 12])) print(" },") if pieces: print(" .output = &.{") for p in pieces: print(" %s," % p) print(" },") else: print(" .output = &.{},") print(" },") print("};")
if __name__ == "__main__": main(sys.argv[1])