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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! Compresses what is written to a `std.Io.Writer`, passing the Brotli//! stream on to another `std.Io.Writer`.//!//! ```//! var c: brotli.CompressWriter = .init(gpa, output_writer, &buffer, .{});//! defer c.deinit();//! try c.writer.writeAll(data);//! try c.finish();//! try output_writer.flush();//! ```//!//! `finish` ends the stream; without it, what is written is not a complete//! Brotli stream. Flushing `writer` compresses everything written so far//! and carries the stream to a byte boundary, so that whoever is reading the//! output can decompress all of it; that costs a few bytes each time, so//! flush when the reader needs it rather than out of habit. Neither flushes//! the output writer.//!//! The `std.Io.Writer` interface has only `error.WriteFailed` to say that//! something went wrong. When the fault is the compressor's rather than the//! output writer's, `err` says what it was.//!//! 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 Compressor = @import("Compressor.zig");
const CompressWriter = @This();
output: *Io.Writer,compressor: Compressor,/// Where the uncompressed data is written.writer: Io.Writer,/// Why the last write failed, if it was the compressor's fault rather than/// the output writer's.err: ?Compressor.Error = null,
const vtable: Io.Writer.VTable = .{ .drain = drain, .flush = flush,};
/// `buffer` becomes the buffer of `writer`; it may be empty. The compressor/// gathers its input into blocks of its own, so a large buffer here saves/// only calls.pub fn init(gpa: Allocator, output: *Io.Writer, buffer: []u8, options: Compressor.Options) CompressWriter { return .{ .output = output, .compressor = .init(gpa, options), .writer = .{ .vtable = &vtable, .buffer = buffer, .end = 0 }, };}
pub fn deinit(c: *CompressWriter) void { c.compressor.deinit(); c.* = undefined;}
/// Compresses whatever is still buffered and ends the stream. Everything is/// then written to the output writer, which is left for the caller to/// flush.pub fn finish(c: *CompressWriter) (Compressor.Error || Io.Writer.Error)!void { try c.run(c.writer.buffered(), .finish); c.writer.end = 0;}
fn drain(w: *Io.Writer, data: []const []const u8, splat: usize) Io.Writer.Error!usize { const c: *CompressWriter = @alignCast(@fieldParentPtr("writer", w)); try c.feed(w.buffered(), .process); w.end = 0; var n: usize = 0; for (data[0 .. data.len - 1]) |bytes| { try c.feed(bytes, .process); n += bytes.len; } const last = data[data.len - 1]; for (0..splat) |_| { try c.feed(last, .process); n += last.len; } return n;}
fn flush(w: *Io.Writer) Io.Writer.Error!void { const c: *CompressWriter = @alignCast(@fieldParentPtr("writer", w)); try c.feed(w.buffered(), .flush); w.end = 0;}
/// `run`, with the compressor's own errors kept in `err`.fn feed(c: *CompressWriter, bytes: []const u8, op: Compressor.Operation) Io.Writer.Error!void { c.run(bytes, op) catch |e| switch (e) { error.WriteFailed => return error.WriteFailed, else => |ce| { c.err = ce; return error.WriteFailed; }, };}
/// Compresses all of `bytes` and carries out `op`, writing what comes out to/// the output writer.fn run(c: *CompressWriter, bytes: []const u8, op: Compressor.Operation) (Compressor.Error || Io.Writer.Error)!void { var in = bytes; while (true) { const r = try c.compressor.compressToWriter(in, c.output, .unlimited, op); in = in[r.read..]; switch (r.status) { .need_output => {}, .need_input, .done => return, } }}