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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! Decompresses a Brotli stream written to a `std.Io.Writer`, passing the//! decompressed bytes on to another `std.Io.Writer`.//!//! This is the push-driven counterpart of `DecompressReader`, for when the//! compressed bytes arrive by being written somewhere: copied from a reader//! with `streamRemaining`, handed over by a protocol, or produced by the//! program itself.//!//! ```//! var d: brotli.DecompressWriter = .init(gpa, output_writer, &buffer, .{});//! defer d.deinit();//! try d.writer.writeAll(compressed);//! try d.finish();//! try output_writer.flush();//! ```//!//! `finish` is what says the compressed stream is over. It fails with//! `error.Truncated` if the stream has not ended, and it does not flush the//! output writer.//!//! The `std.Io.Writer` interface has only `error.WriteFailed` to say that//! something went wrong. When the fault is in the stream rather than in the//! output writer, `err` says what it was: a `Decompressor.Error`, or//! `error.TrailingData` if bytes were written after the end of the stream.//!//! The struct holds the interface that refers back to it, so it must not be//! moved once `writer` is in use.
const std = @import("std");const Allocator = std.mem.Allocator;const Io = std.Io;
const Decompressor = @import("Decompressor.zig");
const DecompressWriter = @This();
pub const Error = Decompressor.Error || error{ /// Bytes were written after the end of the stream. TrailingData, /// `finish` was called before the stream ended. Truncated,};
output: *Io.Writer,decompressor: Decompressor,/// Where the compressed stream is written.writer: Io.Writer,/// Why the last write failed, if it was the stream's fault rather than the/// output writer's.err: ?Error = null,
const vtable: Io.Writer.VTable = .{ .drain = drain,};
/// `buffer` becomes the buffer of `writer`, holding compressed bytes until/// there are enough to be worth decompressing; it may be empty.pub fn init(gpa: Allocator, output: *Io.Writer, buffer: []u8, options: Decompressor.Options) DecompressWriter { return .{ .output = output, .decompressor = .init(gpa, options), .writer = .{ .vtable = &vtable, .buffer = buffer, .end = 0 }, };}
pub fn deinit(d: *DecompressWriter) void { d.decompressor.deinit(); d.* = undefined;}
/// Decompresses whatever is still buffered and checks that the stream has/// ended. Everything decompressed has then been written to the output/// writer, which is left for the caller to flush.pub fn finish(d: *DecompressWriter) (Error || Io.Writer.Error)!void { d.writer.flush() catch |e| return d.err orelse e; while (true) { const r = d.decompressor.decompressToWriter(&.{}, d.output, .unlimited) catch |e| switch (e) { error.WriteFailed => return error.WriteFailed, else => |de| { d.err = de; return de; }, }; switch (r.status) { .done => return, .need_output => {}, .need_input => { d.err = error.Truncated; return error.Truncated; }, } }}
fn drain(w: *Io.Writer, data: []const []const u8, splat: usize) Io.Writer.Error!usize { const d: *DecompressWriter = @alignCast(@fieldParentPtr("writer", w)); try d.feed(w.buffered()); w.end = 0; var n: usize = 0; for (data[0 .. data.len - 1]) |bytes| { try d.feed(bytes); n += bytes.len; } const last = data[data.len - 1]; for (0..splat) |_| { try d.feed(last); n += last.len; } return n;}
/// Decompresses all of `bytes`, writing what comes out to the output writer.fn feed(d: *DecompressWriter, bytes: []const u8) Io.Writer.Error!void { var in = bytes; while (in.len > 0) { if (d.decompressor.isDone()) { d.err = error.TrailingData; return error.WriteFailed; } const r = d.decompressor.decompressToWriter(in, d.output, .unlimited) catch |e| switch (e) { error.WriteFailed => return error.WriteFailed, else => |de| { d.err = de; return error.WriteFailed; }, }; in = in[r.read..]; }}