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.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! Compresses what is read from a `std.Io.Reader`, as a `std.Io.Reader` of//! the compressed stream.//!//! Uncompressed bytes are pulled from `input` as they are needed, and//! `reader` yields the Brotli stream. The end of `input` is the end of the//! stream: the stream is finished then, and `reader` reports//! `error.EndOfStream` once all of it has been read.//!//! ```//! var in: std.Io.Reader = .fixed(data);//! var c: brotli.CompressReader = .init(gpa, &in, &.{}, .{});//! defer c.deinit();//! const n = try c.reader.streamRemaining(output_writer);//! ```//!//! 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 Compressor = @import("Compressor.zig");
const CompressReader = @This();
input: *Io.Reader,compressor: Compressor,/// The compressed stream.reader: Io.Reader,/// Whether `input` has ended, so that what is left is to finish the stream.input_ended: bool = false,/// Why the last read failed, if it was the compressor's fault rather than/// `input`'s: it can only run out of memory.err: ?Compressor.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: it is/// needed only for the parts of the `std.Io.Reader` API that peek.pub fn init(gpa: Allocator, input: *Io.Reader, buffer: []u8, options: Compressor.Options) CompressReader { return .{ .input = input, .compressor = .init(gpa, options), .reader = .{ .vtable = &vtable, .buffer = buffer, .seek = 0, .end = 0 }, };}
pub fn deinit(c: *CompressReader) void { c.compressor.deinit(); c.* = undefined;}
fn stream(r: *Io.Reader, w: *Io.Writer, limit: Io.Limit) Io.Reader.StreamError!usize { const c: *CompressReader = @alignCast(@fieldParentPtr("reader", r)); if (limit == .nothing) return 0; while (true) { if (c.compressor.isDone()) return error.EndOfStream; const op: Compressor.Operation = if (c.input_ended) .finish else .process; const result = c.compressor.compressToWriter(c.input.buffered(), w, limit, op) catch |e| switch (e) { error.WriteFailed => return error.WriteFailed, else => |ce| { c.err = ce; return error.ReadFailed; }, }; c.input.toss(result.read); if (result.written > 0) return result.written; switch (result.status) { .done => return error.EndOfStream, .need_output => return 0, .need_input => c.input.fillMore() catch |e| switch (e) { error.EndOfStream => c.input_ended = true, 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, };}