Zig 0.17.0-dev (Split by item)

This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig library itself.

ChunkState

blake3.ChunkState
const ChunkState = struct

File

lib/std/crypto/blake3.zig:837

Code

const ChunkState = struct {
    cv: [8]u32 align(16),
    chunk_counter: u64,
    buf: [Blake3.block_length]u8 align(16),
    buf_len: u8,
    blocks_compressed: u8,
    flags: Flags,

    fn init(key: [8]u32, flags: Flags) ChunkState {
        return ChunkState{
            .cv = key,
            .chunk_counter = 0,
            .buf = @splat(0),
            .buf_len = 0,
            .blocks_compressed = 0,
            .flags = flags,
        };
    }

    fn reset(self: *ChunkState, key: [8]u32, chunk_counter: u64) void {
        self.cv = key;
        self.chunk_counter = chunk_counter;
        self.blocks_compressed = 0;
        self.buf = @splat(0);
        self.buf_len = 0;
    }

    fn len(self: *const ChunkState) usize {
        return (Blake3.block_length * @as(usize, self.blocks_compressed)) + @as(usize, self.buf_len);
    }

    fn fillBuf(self: *ChunkState, input: []const u8) usize {
        const take = @min(Blake3.block_length - @as(usize, self.buf_len), input.len);
        @memcpy(self.buf[self.buf_len..][0..take], input[0..take]);
        self.buf_len += @intCast(take);
        return take;
    }

    fn maybeStartFlag(self: *const ChunkState) Flags {
        return if (self.blocks_compressed == 0) .{ .chunk_start = true } else .{};
    }

    fn update(self: *ChunkState, input: []const u8) void {
        var inp = input;

        while (inp.len > 0) {
            if (self.buf_len == Blake3.block_length) {
                compressInPlace(&self.cv, &self.buf, Blake3.block_length, self.chunk_counter, self.flags.with(self.maybeStartFlag()));
                self.blocks_compressed += 1;
                self.buf = @splat(0);
                self.buf_len = 0;
            }

            const take = self.fillBuf(inp);
            inp = inp[take..];
        }
    }

    fn output(self: *const ChunkState) Output {
        const block_flags = self.flags.with(self.maybeStartFlag()).with(.{ .chunk_end = true });
        return Output{
            .input_cv = self.cv,
            .block = self.buf,
            .block_len = self.buf_len,
            .counter = self.chunk_counter,
            .flags = block_flags,
        };
    }
}