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.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! Writes the Brotli bit stream, least significant bit of each byte first,//! into a growing buffer.//!//! Whole bytes go to `bytes` as soon as they are complete; the bits of a//! byte not yet finished wait in `acc`, and carry over into whatever is//! written next. A `Mark` taken before writing something lets it be taken//! back, which is how a meta-block that came out larger than its input is//! replaced with an uncompressed one.
const std = @import("std");const Allocator = std.mem.Allocator;const BitWriter = @This();
bytes: std.ArrayList(u8) = .empty,/// Bits not yet making a whole byte, the first in the least significant/// position.acc: u64 = 0,count: u6 = 0,
pub fn deinit(bw: *BitWriter, gpa: Allocator) void { bw.bytes.deinit(gpa); bw.* = undefined;}
/// Writes the low `n` bits of `value`, at most 56 of them.pub fn writeBits(bw: *BitWriter, gpa: Allocator, n: u6, value: u64) Allocator.Error!void { std.debug.assert(n <= 56); std.debug.assert(n == 0 or value >> n == 0); try bw.bytes.ensureUnusedCapacity(gpa, 8); bw.acc |= value << bw.count; var count: u32 = @as(u32, bw.count) + n; while (count >= 8) : (count -= 8) { bw.bytes.appendAssumeCapacity(@truncate(bw.acc)); bw.acc >>= 8; } bw.count = @intCast(count);}
/// Writes zero bits up to the next byte boundary.pub fn alignToByte(bw: *BitWriter, gpa: Allocator) Allocator.Error!void { if (bw.count != 0) try bw.writeBits(gpa, 8 - @as(u6, bw.count), 0);}
/// Appends whole bytes. Only valid on a byte boundary.pub fn writeBytes(bw: *BitWriter, gpa: Allocator, bytes: []const u8) Allocator.Error!void { std.debug.assert(bw.count == 0); try bw.bytes.appendSlice(gpa, bytes);}
/// How many bits have been written.pub fn bitLength(bw: *const BitWriter) u64 { return @as(u64, bw.bytes.items.len) * 8 + bw.count;}
pub const Mark = struct { len: usize, acc: u64, count: u6 };
pub fn mark(bw: *const BitWriter) Mark { return .{ .len = bw.bytes.items.len, .acc = bw.acc, .count = bw.count };}
/// Takes back everything written since `m`.pub fn restore(bw: *BitWriter, m: Mark) void { bw.bytes.shrinkRetainingCapacity(m.len); bw.acc = m.acc; bw.count = m.count;}
test "bits go in least significant first" { const gpa = std.testing.allocator; var bw: BitWriter = .{}; defer bw.deinit(gpa); try bw.writeBits(gpa, 1, 0); try bw.writeBits(gpa, 3, 0b011); try bw.writeBits(gpa, 5, 0b1_1010); try bw.writeBits(gpa, 7, 0b111_1111); try std.testing.expectEqualSlices(u8, &.{ 0b1010_0110, 0xff }, bw.bytes.items); try bw.writeBits(gpa, 3, 0b101); const m = bw.mark(); try bw.writeBits(gpa, 40, 0xab_cdef_0123); try std.testing.expectEqual(16 + 3 + 40, bw.bitLength()); bw.restore(m); try bw.alignToByte(gpa); try std.testing.expectEqualSlices(u8, &.{ 0b1010_0110, 0xff, 0b101 }, bw.bytes.items);}