stdassertDecompressdefault_window_lenblock_size_maxliterals_length_default_distributionmatch_lengths_default_distributionoffset_codes_default_distributionstart_repeated_offset_1start_repeated_offset_2start_repeated_offset_3literals_length_code_tablematch_length_code_tabletable_accuracy_log_maxtable_symbol_count_maxdefault_accuracy_logtable_size_maxtestDecompresstestDiscardtestExpectDecompresstestExpectDecompressError
std.compress.zstd.assert
别名
别名目标 std.debug.assert
std.compress.zstd.block_size_max
常量
std.compress.zstd.Decompress
别名
std.compress.zstd.default_accuracy_log
命名空间
std.compress.zstd.default_accuracy_log.literal
常量
std.compress.zstd.default_accuracy_log.match
常量
std.compress.zstd.default_accuracy_log.offset
常量
std.compress.zstd.default_window_len
常量
标准建议的大小。低于此值可能导致无法 解压常见的数据流。
std.compress.zstd.literals_length_code_table
常量
std.compress.zstd.literals_length_default_distribution
常量
std.compress.zstd.match_length_code_table
常量
std.compress.zstd.match_lengths_default_distribution
常量
std.compress.zstd.offset_codes_default_distribution
常量
std.compress.zstd.start_repeated_offset_1
常量
std.compress.zstd.start_repeated_offset_2
常量
std.compress.zstd.start_repeated_offset_3
常量
std.compress.zstd.std
别名
别名目标 std
std.compress.zstd.table_accuracy_log_max
命名空间
std.compress.zstd.table_accuracy_log_max.literal
常量
std.compress.zstd.table_accuracy_log_max.match
常量
std.compress.zstd.table_accuracy_log_max.offset
常量
std.compress.zstd.table_size_max
命名空间
std.compress.zstd.table_size_max.literal
常量
std.compress.zstd.table_size_max.match
常量
std.compress.zstd.table_size_max.offset
常量
std.compress.zstd.table_symbol_count_max
命名空间
std.compress.zstd.table_symbol_count_max.literal
常量
std.compress.zstd.table_symbol_count_max.match
常量
std.compress.zstd.table_symbol_count_max.offset
常量
std.compress.zstd.testDecompress
函数
fn testDecompress(gpa: std.mem.Allocator, compressed: []const u8) ![]u8
参数
std.compress.zstd.testDiscard
函数
由 compressed 创建 Decompress,并立即丢弃全部输出。返回
丢弃的字节数。
fn testDiscard(gpa: std.mem.Allocator, compressed: []const u8) !usize
参数
std.compress.zstd.testExpectDecompress
函数
fn testExpectDecompress(uncompressed: []const u8, compressed: []const u8) !void
参数
uncompressed: []const u8
compressed: []const u8
std.compress.zstd.testExpectDecompressError
函数
fn testExpectDecompressError(err: anyerror, compressed: []const u8) !void
参数
err: anyerror
compressed: []const u8
源码
std/compress/zstd.zig
const std = @import("../std.zig");
const assert = std.debug.assert;
pub const Decompress = @import("zstd/Decompress.zig");
/// 标准建议的大小。低于此值可能导致无法
/// 解压常见的数据流。
pub const default_window_len = 8 * 1024 * 1024;
pub const block_size_max = 1 << 17;
pub const literals_length_default_distribution = [36]i16{
4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 1, 1, 1, 1, 1,
-1, -1, -1, -1,
};
pub const match_lengths_default_distribution = [53]i16{
1, 4, 3, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1,
-1, -1, -1, -1, -1,
};
pub const offset_codes_default_distribution = [29]i16{
1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
};
pub const start_repeated_offset_1 = 1;
pub const start_repeated_offset_2 = 4;
pub const start_repeated_offset_3 = 8;
pub const literals_length_code_table = [36]struct { u32, u5 }{
.{ 0, 0 }, .{ 1, 0 }, .{ 2, 0 }, .{ 3, 0 },
.{ 4, 0 }, .{ 5, 0 }, .{ 6, 0 }, .{ 7, 0 },
.{ 8, 0 }, .{ 9, 0 }, .{ 10, 0 }, .{ 11, 0 },
.{ 12, 0 }, .{ 13, 0 }, .{ 14, 0 }, .{ 15, 0 },
.{ 16, 1 }, .{ 18, 1 }, .{ 20, 1 }, .{ 22, 1 },
.{ 24, 2 }, .{ 28, 2 }, .{ 32, 3 }, .{ 40, 3 },
.{ 48, 4 }, .{ 64, 6 }, .{ 128, 7 }, .{ 256, 8 },
.{ 512, 9 }, .{ 1024, 10 }, .{ 2048, 11 }, .{ 4096, 12 },
.{ 8192, 13 }, .{ 16384, 14 }, .{ 32768, 15 }, .{ 65536, 16 },
};
pub const match_length_code_table = [53]struct { u32, u5 }{
.{ 3, 0 }, .{ 4, 0 }, .{ 5, 0 }, .{ 6, 0 }, .{ 7, 0 }, .{ 8, 0 },
.{ 9, 0 }, .{ 10, 0 }, .{ 11, 0 }, .{ 12, 0 }, .{ 13, 0 }, .{ 14, 0 },
.{ 15, 0 }, .{ 16, 0 }, .{ 17, 0 }, .{ 18, 0 }, .{ 19, 0 }, .{ 20, 0 },
.{ 21, 0 }, .{ 22, 0 }, .{ 23, 0 }, .{ 24, 0 }, .{ 25, 0 }, .{ 26, 0 },
.{ 27, 0 }, .{ 28, 0 }, .{ 29, 0 }, .{ 30, 0 }, .{ 31, 0 }, .{ 32, 0 },
.{ 33, 0 }, .{ 34, 0 }, .{ 35, 1 }, .{ 37, 1 }, .{ 39, 1 }, .{ 41, 1 },
.{ 43, 2 }, .{ 47, 2 }, .{ 51, 3 }, .{ 59, 3 }, .{ 67, 4 }, .{ 83, 4 },
.{ 99, 5 }, .{ 131, 7 }, .{ 259, 8 }, .{ 515, 9 }, .{ 1027, 10 }, .{ 2051, 11 },
.{ 4099, 12 }, .{ 8195, 13 }, .{ 16387, 14 }, .{ 32771, 15 }, .{ 65539, 16 },
};
pub const table_accuracy_log_max = struct {
pub const literal = 9;
pub const match = 9;
pub const offset = 8;
};
pub const table_symbol_count_max = struct {
pub const literal = 36;
pub const match = 53;
pub const offset = 32;
};
pub const default_accuracy_log = struct {
pub const literal = 6;
pub const match = 6;
pub const offset = 5;
};
pub const table_size_max = struct {
pub const literal = 1 << table_accuracy_log_max.literal;
pub const match = 1 << table_accuracy_log_max.match;
pub const offset = 1 << table_accuracy_log_max.offset;
};
fn testDecompress(gpa: std.mem.Allocator, compressed: []const u8) ![]u8 {
var out: std.Io.Writer.Allocating = .init(gpa);
defer out.deinit();
var in: std.Io.Reader = .fixed(compressed);
var zstd_stream: Decompress = .init(&in, &.{}, .{});
_ = try zstd_stream.reader.streamRemaining(&out.writer);
return out.toOwnedSlice();
}
/// 由 `compressed` 创建 `Decompress`,并立即丢弃全部输出。返回
/// 丢弃的字节数。
fn testDiscard(gpa: std.mem.Allocator, compressed: []const u8) !usize {
const buf: []u8 = try gpa.alloc(u8, default_window_len + block_size_max);
defer gpa.free(buf);
var in: std.Io.Reader = .fixed(compressed);
var zstd_stream: Decompress = .init(&in, buf, .{});
return try zstd_stream.reader.discardRemaining();
}
fn testExpectDecompress(uncompressed: []const u8, compressed: []const u8) !void {
const gpa = std.testing.allocator;
const result = try testDecompress(gpa, compressed);
defer gpa.free(result);
try std.testing.expectEqualSlices(u8, uncompressed, result);
}
fn testExpectDecompressError(err: anyerror, compressed: []const u8) !void {
const gpa = std.testing.allocator;
var out: std.Io.Writer.Allocating = .init(gpa);
defer out.deinit();
var in: std.Io.Reader = .fixed(compressed);
var zstd_stream: Decompress = .init(&in, &.{}, .{});
try std.testing.expectError(
error.ReadFailed,
zstd_stream.reader.streamRemaining(&out.writer),
);
try std.testing.expectError(err, zstd_stream.err orelse {});
}
test "partial magic number" {
const input_raw =
"\x28\xb5\x2f"; // 4 字节 zstandard 帧魔数中的 3 字节
try testExpectDecompressError(error.BadMagic, input_raw);
}
test "zero sized raw block" {
const input_raw =
"\x28\xb5\x2f\xfd" ++ // zstandard 帧魔数
"\x20\x00" ++ // 帧头部:仅设置 single_segment_flag,frame_content_size 为零
"\x01\x00\x00"; // 块头:已设置 last_block,block_type 为 raw,block_size 为零。
try testExpectDecompress("", input_raw);
}
test "zero sized rle block" {
const input_rle =
"\x28\xb5\x2f\xfd" ++ // zstandard 帧魔数
"\x20\x00" ++ // 帧头部:仅设置 single_segment_flag,frame_content_size 为零
"\x03\x00\x00" ++ // 块头设置为:last_block 已设置,block_type 为 rle,block_size 为零
"\xaa"; // block_content
try testExpectDecompress("", input_rle);
}
test "declared raw literals size too large" {
const input_raw =
"\x28\xb5\x2f\xfd" ++ // zstandard 帧魔数
"\x00\x00" ++ // 帧头:所有标志未设置,窗口描述符为零
"\x95\x00\x00" ++ // 块头设置为:last_block 已设置,block_type 为 compressed,block_size 为 18
"\xbc\xf3\xae" ++ // 字面量段头:类型为 raw,size_format 为 3,regenerated_size 为 716603。
"\xa5\x9f\xe3"; // 若干字节的字面量内容——内容短于 regenerated_size
// 注意,上述输入的 regenerated_size 超过块的最大大小,因此
// 该块作为原始字面量块不可能有效。
try testExpectDecompressError(error.MalformedLiteralsSection, input_raw);
}
test "skippable frame" {
const input_raw =
"\x50\x2a\x4d\x18" ++ // 可跳过帧的最小魔数
"\x02\x00\x00\x00" ++ // 要跳过的字节数
"\xFF\xFF"; // 跳过的字节
try testExpectDecompress("", input_raw);
}