Function decodeLiteralsSectionSlice [src]
Decode a LiteralsSection from src, incrementing consumed_count by the
number of bytes the section uses.
Errors returned:
error.MalformedLiteralsHeader if the header is invalid
error.MalformedLiteralsSection if there are decoding errors
error.MalformedAccuracyLog if compressed literals have invalid
accuracy
error.MalformedFseTable if compressed literals have invalid FSE table
error.MalformedHuffmanTree if there are errors decoding a Huffamn tree
error.EndOfStream if there are not enough bytes in src
Prototype
pub fn decodeLiteralsSectionSlice( src: []const u8, consumed_count: *usize, ) (error{ MalformedLiteralsHeader, MalformedLiteralsSection, EndOfStream } || huffman.Error)!LiteralsSection
Parameters
src: []const u8
consumed_count: *usize
Source
pub fn decodeLiteralsSectionSlice(
src: []const u8,
consumed_count: *usize,
) (error{ MalformedLiteralsHeader, MalformedLiteralsSection, EndOfStream } || huffman.Error)!LiteralsSection {
var bytes_read: usize = 0;
const header = header: {
var fbs = std.io.fixedBufferStream(src);
defer bytes_read = fbs.pos;
break :header decodeLiteralsHeader(fbs.reader()) catch return error.MalformedLiteralsHeader;
};
switch (header.block_type) {
.raw => {
if (src.len < bytes_read + header.regenerated_size) return error.MalformedLiteralsSection;
const stream = src[bytes_read..][0..header.regenerated_size];
consumed_count.* += header.regenerated_size + bytes_read;
return LiteralsSection{
.header = header,
.huffman_tree = null,
.streams = .{ .one = stream },
};
},
.rle => {
if (src.len < bytes_read + 1) return error.MalformedLiteralsSection;
const stream = src[bytes_read..][0..1];
consumed_count.* += 1 + bytes_read;
return LiteralsSection{
.header = header,
.huffman_tree = null,
.streams = .{ .one = stream },
};
},
.compressed, .treeless => {
const huffman_tree_start = bytes_read;
const huffman_tree = if (header.block_type == .compressed)
try huffman.decodeHuffmanTreeSlice(src[bytes_read..], &bytes_read)
else
null;
const huffman_tree_size = bytes_read - huffman_tree_start;
const total_streams_size = std.math.sub(usize, header.compressed_size.?, huffman_tree_size) catch
return error.MalformedLiteralsSection;
if (src.len < bytes_read + total_streams_size) return error.MalformedLiteralsSection;
const stream_data = src[bytes_read .. bytes_read + total_streams_size];
const streams = try decodeStreams(header.size_format, stream_data);
consumed_count.* += bytes_read + total_streams_size;
return LiteralsSection{
.header = header,
.huffman_tree = huffman_tree,
.streams = streams,
};
},
}
}