// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! Decompresses a Brotli stream read from a `std.Io.Reader`, as a //! `std.Io.Reader` of its own. //! //! Compressed bytes are pulled from `input` as they are needed, and //! `reader` yields the decompressed bytes. Reading past the end of the stream //! gives `error.EndOfStream`; anything after the stream is left unread in //! `input`, so a stream embedded in something larger ends where it should. //! //! ``` //! var in: std.Io.Reader = .fixed(compressed); //! var d: brotli.DecompressReader = .init(gpa, &in, &.{}, .{}); //! defer d.deinit(); //! const n = try d.reader.streamRemaining(output_writer); //! ``` //! //! The `std.Io.Reader` interface has only `error.ReadFailed` to say that //! something went wrong. When the fault is in the stream rather than in //! `input`, `err` says what it was: a `Decompressor.Error`, or //! `error.Truncated` if `input` ended first. //! //! The struct holds the interface that refers back to it, so it must not be //! moved once `reader` is in use. const std = @import("std"); const Allocator = std.mem.Allocator; const Io = std.Io; const Decompressor = @import("Decompressor.zig"); const DecompressReader = @This(); pub const Error = Decompressor.Error || error{ /// `input` ended before the stream did. Truncated, }; input: *Io.Reader, decompressor: Decompressor, /// The decompressed stream. reader: Io.Reader, /// Why the last read failed, if it was the stream's fault rather than /// `input`'s. err: ?Error = null, const vtable: Io.Reader.VTable = .{ .stream = stream, .discard = discard, }; /// `input` is refilled through its own buffer, so it must have one of at least /// a byte, unless, like `std.Io.Reader.fixed`, it holds all of its data from /// the start. `buffer` becomes the buffer of `reader`, and may be empty: the /// decompressor keeps its own window, so `reader` needs a buffer only for the /// parts of the `std.Io.Reader` API that peek, such as `takeByte` or `peek`. pub fn init(gpa: Allocator, input: *Io.Reader, buffer: []u8, options: Decompressor.Options) DecompressReader { return .{ .input = input, .decompressor = .init(gpa, options), .reader = .{ .vtable = &vtable, .buffer = buffer, .seek = 0, .end = 0 }, }; } pub fn deinit(d: *DecompressReader) void { d.decompressor.deinit(); d.* = undefined; } fn stream(r: *Io.Reader, w: *Io.Writer, limit: Io.Limit) Io.Reader.StreamError!usize { const d: *DecompressReader = @alignCast(@fieldParentPtr("reader", r)); if (limit == .nothing) return 0; while (true) { if (d.decompressor.isDone()) return error.EndOfStream; const result = d.decompressor.decompressToWriter(d.input.buffered(), w, limit) catch |e| switch (e) { error.WriteFailed => return error.WriteFailed, else => |de| { d.err = de; return error.ReadFailed; }, }; d.input.toss(result.read); if (result.written > 0) return result.written; switch (result.status) { .done => return error.EndOfStream, // Only possible when `limit` left no room, which it did not. .need_output => return 0, // Every buffered byte has been consumed, so there is room to // fill. .need_input => d.input.fillMore() catch |e| switch (e) { error.EndOfStream => { d.err = error.Truncated; return error.ReadFailed; }, error.ReadFailed => return error.ReadFailed, }, } } } fn discard(r: *Io.Reader, limit: Io.Limit) Io.Reader.Error!usize { var buffer: [4096]u8 = undefined; var w: Io.Writer = .fixed(&buffer); return stream(r, &w, limit) catch |e| switch (e) { // `stream` writes no more than `w` has room for. error.WriteFailed => unreachable, else => |re| return re, }; }