Type Function Decompress [src]

Prototype

pub fn Decompress(comptime ReaderType: type) type

Parameters

ReaderType: type

Source

pub fn Decompress(comptime ReaderType: type) type { return struct { const Self = @This(); pub const Error = ReaderType.Error || Allocator.Error || error{ CorruptInput, EndOfStream, Overflow }; pub const Reader = std.io.Reader(*Self, Error, read); allocator: Allocator, in_reader: ReaderType, to_read: std.ArrayListUnmanaged(u8), buffer: decode.lzbuffer.LzCircularBuffer, decoder: decode.rangecoder.RangeDecoder, state: decode.DecoderState, pub fn init(allocator: Allocator, source: ReaderType, params: decode.Params, memlimit: ?usize) !Self { return Self{ .allocator = allocator, .in_reader = source, .to_read = .{}, .buffer = decode.lzbuffer.LzCircularBuffer.init(params.dict_size, memlimit orelse math.maxInt(usize)), .decoder = try decode.rangecoder.RangeDecoder.init(source), .state = try decode.DecoderState.init(allocator, params.properties, params.unpacked_size), }; } pub fn reader(self: *Self) Reader { return .{ .context = self }; } pub fn deinit(self: *Self) void { self.to_read.deinit(self.allocator); self.buffer.deinit(self.allocator); self.state.deinit(self.allocator); self.* = undefined; } pub fn read(self: *Self, output: []u8) Error!usize { const writer = self.to_read.writer(self.allocator); while (self.to_read.items.len < output.len) { switch (try self.state.process(self.allocator, self.in_reader, writer, &self.buffer, &self.decoder)) { .continue_ => {}, .finished => { try self.buffer.finish(writer); break; }, } } const input = self.to_read.items; const n = @min(input.len, output.len); @memcpy(output[0..n], input[0..n]); std.mem.copyForwards(u8, input[0 .. input.len - n], input[n..]); self.to_read.shrinkRetainingCapacity(input.len - n); return n; } }; }