// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // 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..]; } }