structstd.compress.lzma.decode.lzbuffer.LzAccumBuffer[src]

An accumulating buffer for LZ sequences

Fields

buf: ArrayListUnmanaged(u8)

Buffer

memlimit: usize

Buffer memory limit

len: usize

Total number of bytes sent through the buffer

Functions

Functioninit[src]

pub fn init(memlimit: usize) Self

Parameters

memlimit: usize

Source Code

Source code
pub fn init(memlimit: usize) Self {
    return Self{
        .buf = .{},
        .memlimit = memlimit,
        .len = 0,
    };
}

FunctionappendByte[src]

pub fn appendByte(self: *Self, allocator: Allocator, byte: u8) !void

Parameters

self: *Self
allocator: Allocator
byte: u8

Source Code

Source code
pub fn appendByte(self: *Self, allocator: Allocator, byte: u8) !void {
    try self.buf.append(allocator, byte);
    self.len += 1;
}

Functionreset[src]

pub fn reset(self: *Self, writer: anytype) !void

Reset the internal dictionary

Parameters

self: *Self

Source Code

Source code
pub fn reset(self: *Self, writer: anytype) !void {
    try writer.writeAll(self.buf.items);
    self.buf.clearRetainingCapacity();
    self.len = 0;
}

FunctionlastOr[src]

pub fn lastOr(self: Self, lit: u8) u8

Retrieve the last byte or return a default

Parameters

self: Self
lit: u8

Source Code

Source code
pub fn lastOr(self: Self, lit: u8) u8 {
    const buf_len = self.buf.items.len;
    return if (buf_len == 0)
        lit
    else
        self.buf.items[buf_len - 1];
}

FunctionlastN[src]

pub fn lastN(self: Self, dist: usize) !u8

Retrieve the n-th last byte

Parameters

self: Self
dist: usize

Source Code

Source code
pub fn lastN(self: Self, dist: usize) !u8 {
    const buf_len = self.buf.items.len;
    if (dist > buf_len) {
        return error.CorruptInput;
    }

    return self.buf.items[buf_len - dist];
}

FunctionappendLiteral[src]

pub fn appendLiteral( self: *Self, allocator: Allocator, lit: u8, writer: anytype, ) !void

Append a literal

Parameters

self: *Self
allocator: Allocator
lit: u8

Source Code

Source code
pub fn appendLiteral(
    self: *Self,
    allocator: Allocator,
    lit: u8,
    writer: anytype,
) !void {
    _ = writer;
    if (self.len >= self.memlimit) {
        return error.CorruptInput;
    }
    try self.buf.append(allocator, lit);
    self.len += 1;
}

FunctionappendLz[src]

pub fn appendLz( self: *Self, allocator: Allocator, len: usize, dist: usize, writer: anytype, ) !void

Fetch an LZ sequence (length, distance) from inside the buffer

Parameters

self: *Self
allocator: Allocator
len: usize
dist: usize

Source Code

Source code
pub fn appendLz(
    self: *Self,
    allocator: Allocator,
    len: usize,
    dist: usize,
    writer: anytype,
) !void {
    _ = writer;

    const buf_len = self.buf.items.len;
    if (dist > buf_len) {
        return error.CorruptInput;
    }

    var offset = buf_len - dist;
    var i: usize = 0;
    while (i < len) : (i += 1) {
        const x = self.buf.items[offset];
        try self.buf.append(allocator, x);
        offset += 1;
    }
    self.len += len;
}

Functionfinish[src]

pub fn finish(self: *Self, writer: anytype) !void

Parameters

self: *Self

Source Code

Source code
pub fn finish(self: *Self, writer: anytype) !void {
    try writer.writeAll(self.buf.items);
    self.buf.clearRetainingCapacity();
}

Functiondeinit[src]

pub fn deinit(self: *Self, allocator: Allocator) void

Parameters

self: *Self
allocator: Allocator

Source Code

Source code
pub fn deinit(self: *Self, allocator: Allocator) void {
    self.buf.deinit(allocator);
    self.* = undefined;
}

Source Code

Source code
pub const LzAccumBuffer = struct {
    /// Buffer
    buf: ArrayListUnmanaged(u8),

    /// Buffer memory limit
    memlimit: usize,

    /// Total number of bytes sent through the buffer
    len: usize,

    const Self = @This();

    pub fn init(memlimit: usize) Self {
        return Self{
            .buf = .{},
            .memlimit = memlimit,
            .len = 0,
        };
    }

    pub fn appendByte(self: *Self, allocator: Allocator, byte: u8) !void {
        try self.buf.append(allocator, byte);
        self.len += 1;
    }

    /// Reset the internal dictionary
    pub fn reset(self: *Self, writer: anytype) !void {
        try writer.writeAll(self.buf.items);
        self.buf.clearRetainingCapacity();
        self.len = 0;
    }

    /// Retrieve the last byte or return a default
    pub fn lastOr(self: Self, lit: u8) u8 {
        const buf_len = self.buf.items.len;
        return if (buf_len == 0)
            lit
        else
            self.buf.items[buf_len - 1];
    }

    /// Retrieve the n-th last byte
    pub fn lastN(self: Self, dist: usize) !u8 {
        const buf_len = self.buf.items.len;
        if (dist > buf_len) {
            return error.CorruptInput;
        }

        return self.buf.items[buf_len - dist];
    }

    /// Append a literal
    pub fn appendLiteral(
        self: *Self,
        allocator: Allocator,
        lit: u8,
        writer: anytype,
    ) !void {
        _ = writer;
        if (self.len >= self.memlimit) {
            return error.CorruptInput;
        }
        try self.buf.append(allocator, lit);
        self.len += 1;
    }

    /// Fetch an LZ sequence (length, distance) from inside the buffer
    pub fn appendLz(
        self: *Self,
        allocator: Allocator,
        len: usize,
        dist: usize,
        writer: anytype,
    ) !void {
        _ = writer;

        const buf_len = self.buf.items.len;
        if (dist > buf_len) {
            return error.CorruptInput;
        }

        var offset = buf_len - dist;
        var i: usize = 0;
        while (i < len) : (i += 1) {
            const x = self.buf.items[offset];
            try self.buf.append(allocator, x);
            offset += 1;
        }
        self.len += len;
    }

    pub fn finish(self: *Self, writer: anytype) !void {
        try writer.writeAll(self.buf.items);
        self.buf.clearRetainingCapacity();
    }

    pub fn deinit(self: *Self, allocator: Allocator) void {
        self.buf.deinit(allocator);
        self.* = undefined;
    }
}