| ... | @@ -14,8 +14,6 @@ fn readVarInt(comptime T: type, bytes: []const u8) T { | ... | @@ -14,8 +14,6 @@ fn readVarInt(comptime T: type, bytes: []const u8) T { |
| 14 | return std.mem.readVarInt(T, bytes, .Little); | 14 | return std.mem.readVarInt(T, bytes, .Little); |
| 15 | } | 15 | } |
| 16 | | 16 | |
| 17 | const log = std.log.scoped(.Decompress); | | |
| 18 | | | |
| 19 | fn isSkippableMagic(magic: u32) bool { | 17 | fn isSkippableMagic(magic: u32) bool { |
| 20 | return frame.Skippable.magic_number_min <= magic and magic <= frame.Skippable.magic_number_max; | 18 | return frame.Skippable.magic_number_min <= magic and magic <= frame.Skippable.magic_number_max; |
| 21 | } | 19 | } |
| ... | @@ -136,11 +134,6 @@ pub const DecodeState = struct { | ... | @@ -136,11 +134,6 @@ pub const DecodeState = struct { |
| 136 | self.literal.state = try bit_reader.readBitsNoEof(u9, self.literal.accuracy_log); | 134 | self.literal.state = try bit_reader.readBitsNoEof(u9, self.literal.accuracy_log); |
| 137 | self.offset.state = try bit_reader.readBitsNoEof(u8, self.offset.accuracy_log); | 135 | self.offset.state = try bit_reader.readBitsNoEof(u8, self.offset.accuracy_log); |
| 138 | self.match.state = try bit_reader.readBitsNoEof(u9, self.match.accuracy_log); | 136 | self.match.state = try bit_reader.readBitsNoEof(u9, self.match.accuracy_log); |
| 139 | log.debug("initial decoder state: literal = {d}, offset = {d} match = {d}", .{ | | |
| 140 | self.literal.state, | | |
| 141 | self.offset.state, | | |
| 142 | self.match.state, | | |
| 143 | }); | | |
| 144 | } | 137 | } |
| 145 | | 138 | |
| 146 | fn updateRepeatOffset(self: *DecodeState, offset: u32) void { | 139 | fn updateRepeatOffset(self: *DecodeState, offset: u32) void { |
| ... | @@ -208,10 +201,6 @@ pub const DecodeState = struct { | ... | @@ -208,10 +201,6 @@ pub const DecodeState = struct { |
| 208 | ); | 201 | ); |
| 209 | @field(self, field_name).table = .{ .fse = @field(self, field_name ++ "_fse_buffer")[0..table_size] }; | 202 | @field(self, field_name).table = .{ .fse = @field(self, field_name ++ "_fse_buffer")[0..table_size] }; |
| 210 | @field(self, field_name).accuracy_log = std.math.log2_int_ceil(usize, table_size); | 203 | @field(self, field_name).accuracy_log = std.math.log2_int_ceil(usize, table_size); |
| 211 | log.debug("decoded fse " ++ field_name ++ " table '{}'", .{ | | |
| 212 | std.fmt.fmtSliceHexUpper(src[0..counting_reader.bytes_read]), | | |
| 213 | }); | | |
| 214 | dumpFseTable(field_name, @field(self, field_name).table.fse); | | |
| 215 | return counting_reader.bytes_read; | 204 | return counting_reader.bytes_read; |
| 216 | }, | 205 | }, |
| 217 | .repeat => return if (self.fse_tables_undefined) error.RepeatModeFirst else 0, | 206 | .repeat => return if (self.fse_tables_undefined) error.RepeatModeFirst else 0, |
| ... | @@ -227,7 +216,6 @@ pub const DecodeState = struct { | ... | @@ -227,7 +216,6 @@ pub const DecodeState = struct { |
| 227 | fn nextSequence(self: *DecodeState, bit_reader: anytype) !Sequence { | 216 | fn nextSequence(self: *DecodeState, bit_reader: anytype) !Sequence { |
| 228 | const raw_code = self.getCode(.offset); | 217 | const raw_code = self.getCode(.offset); |
| 229 | const offset_code = std.math.cast(u5, raw_code) orelse { | 218 | const offset_code = std.math.cast(u5, raw_code) orelse { |
| 230 | log.err("got offset code of {d}", .{raw_code}); | | |
| 231 | return error.OffsetCodeTooLarge; | 219 | return error.OffsetCodeTooLarge; |
| 232 | }; | 220 | }; |
| 233 | const offset_value = (@as(u32, 1) << offset_code) + try bit_reader.readBitsNoEof(u32, offset_code); | 221 | const offset_value = (@as(u32, 1) << offset_code) + try bit_reader.readBitsNoEof(u32, offset_code); |
| ... | @@ -256,7 +244,6 @@ pub const DecodeState = struct { | ... | @@ -256,7 +244,6 @@ pub const DecodeState = struct { |
| 256 | break :offset self.useRepeatOffset(offset_value - 1); | 244 | break :offset self.useRepeatOffset(offset_value - 1); |
| 257 | }; | 245 | }; |
| 258 | | 246 | |
| 259 | log.debug("sequence = ({d}, {d}, {d})", .{ literal_length, offset, match_length }); | | |
| 260 | return .{ | 247 | return .{ |
| 261 | .literal_length = literal_length, | 248 | .literal_length = literal_length, |
| 262 | .match_length = match_length, | 249 | .match_length = match_length, |
| ... | @@ -310,9 +297,6 @@ pub const DecodeState = struct { | ... | @@ -310,9 +297,6 @@ pub const DecodeState = struct { |
| 310 | if (sequence_length > sequence_size_limit) return error.MalformedSequence; | 297 | if (sequence_length > sequence_size_limit) return error.MalformedSequence; |
| 311 | | 298 | |
| 312 | try self.executeSequenceSlice(dest, write_pos, literals, sequence); | 299 | try self.executeSequenceSlice(dest, write_pos, literals, sequence); |
| 313 | log.debug("sequence decompressed into '{x}'", .{ | | |
| 314 | std.fmt.fmtSliceHexUpper(dest[write_pos .. write_pos + sequence.literal_length + sequence.match_length]), | | |
| 315 | }); | | |
| 316 | if (!last_sequence) { | 300 | if (!last_sequence) { |
| 317 | try self.updateState(.literal, bit_reader); | 301 | try self.updateState(.literal, bit_reader); |
| 318 | try self.updateState(.match, bit_reader); | 302 | try self.updateState(.match, bit_reader); |
| ... | @@ -334,13 +318,6 @@ pub const DecodeState = struct { | ... | @@ -334,13 +318,6 @@ pub const DecodeState = struct { |
| 334 | if (sequence_length > sequence_size_limit) return error.MalformedSequence; | 318 | if (sequence_length > sequence_size_limit) return error.MalformedSequence; |
| 335 | | 319 | |
| 336 | try self.executeSequenceRingBuffer(dest, literals, sequence); | 320 | try self.executeSequenceRingBuffer(dest, literals, sequence); |
| 337 | if (std.options.log_level == .debug) { | | |
| 338 | const written_slice = dest.sliceLast(sequence_length); | | |
| 339 | log.debug("sequence decompressed into '{x}{x}'", .{ | | |
| 340 | std.fmt.fmtSliceHexUpper(written_slice.first), | | |
| 341 | std.fmt.fmtSliceHexUpper(written_slice.second), | | |
| 342 | }); | | |
| 343 | } | | |
| 344 | if (!last_sequence) { | 321 | if (!last_sequence) { |
| 345 | try self.updateState(.literal, bit_reader); | 322 | try self.updateState(.literal, bit_reader); |
| 346 | try self.updateState(.match, bit_reader); | 323 | try self.updateState(.match, bit_reader); |
| ... | @@ -355,7 +332,6 @@ pub const DecodeState = struct { | ... | @@ -355,7 +332,6 @@ pub const DecodeState = struct { |
| 355 | } | 332 | } |
| 356 | | 333 | |
| 357 | fn initLiteralStream(self: *DecodeState, bytes: []const u8) !void { | 334 | fn initLiteralStream(self: *DecodeState, bytes: []const u8) !void { |
| 358 | log.debug("initing literal stream: {}", .{std.fmt.fmtSliceHexUpper(bytes)}); | | |
| 359 | try self.literal_stream_reader.init(bytes); | 335 | try self.literal_stream_reader.init(bytes); |
| 360 | } | 336 | } |
| 361 | | 337 | |
| ... | @@ -372,7 +348,6 @@ pub const DecodeState = struct { | ... | @@ -372,7 +348,6 @@ pub const DecodeState = struct { |
| 372 | while (i < len) : (i += 1) { | 348 | while (i < len) : (i += 1) { |
| 373 | dest[i] = literals.streams.one[0]; | 349 | dest[i] = literals.streams.one[0]; |
| 374 | } | 350 | } |
| 375 | log.debug("rle: {}", .{std.fmt.fmtSliceHexUpper(dest[0..len])}); | | |
| 376 | self.literal_written_count += len; | 351 | self.literal_written_count += len; |
| 377 | }, | 352 | }, |
| 378 | .compressed, .treeless => { | 353 | .compressed, .treeless => { |
| ... | @@ -538,7 +513,6 @@ pub fn decodeZStandardFrame(dest: []u8, src: []const u8, verify_checksum: bool) | ... | @@ -538,7 +513,6 @@ pub fn decodeZStandardFrame(dest: []u8, src: []const u8, verify_checksum: bool) |
| 538 | const hash = hash_state.final(); | 513 | const hash = hash_state.final(); |
| 539 | const hash_low_bytes = hash & 0xFFFFFFFF; | 514 | const hash_low_bytes = hash & 0xFFFFFFFF; |
| 540 | if (checksum != hash_low_bytes) { | 515 | if (checksum != hash_low_bytes) { |
| 541 | std.log.err("expected checksum {x}, got {x} (full hash {x})", .{ checksum, hash_low_bytes, hash }); | | |
| 542 | return error.ChecksumFailure; | 516 | return error.ChecksumFailure; |
| 543 | } | 517 | } |
| 544 | } | 518 | } |
| ... | @@ -556,13 +530,11 @@ pub fn decodeZStandardFrameAlloc(allocator: std.mem.Allocator, src: []const u8, | ... | @@ -556,13 +530,11 @@ pub fn decodeZStandardFrameAlloc(allocator: std.mem.Allocator, src: []const u8, |
| 556 | if (frame_header.descriptor.dictionary_id_flag != 0) return error.DictionaryIdFlagUnsupported; | 530 | if (frame_header.descriptor.dictionary_id_flag != 0) return error.DictionaryIdFlagUnsupported; |
| 557 | | 531 | |
| 558 | const window_size = frameWindowSize(frame_header) orelse return error.WindowSizeUnknown; | 532 | const window_size = frameWindowSize(frame_header) orelse return error.WindowSizeUnknown; |
| 559 | log.debug("window size = {d}", .{window_size}); | | |
| 560 | | 533 | |
| 561 | const should_compute_checksum = frame_header.descriptor.content_checksum_flag and verify_checksum; | 534 | const should_compute_checksum = frame_header.descriptor.content_checksum_flag and verify_checksum; |
| 562 | var hash = if (should_compute_checksum) std.hash.XxHash64.init(0) else null; | 535 | var hash = if (should_compute_checksum) std.hash.XxHash64.init(0) else null; |
| 563 | | 536 | |
| 564 | const block_size_maximum = @min(1 << 17, window_size); | 537 | const block_size_maximum = @min(1 << 17, window_size); |
| 565 | log.debug("block size maximum = {d}", .{block_size_maximum}); | | |
| 566 | | 538 | |
| 567 | var window_data = try allocator.alloc(u8, window_size); | 539 | var window_data = try allocator.alloc(u8, window_size); |
| 568 | defer allocator.free(window_data); | 540 | defer allocator.free(window_data); |
| ... | @@ -680,7 +652,6 @@ pub fn decodeFrameBlocks(dest: []u8, src: []const u8, consumed_count: *usize, ha | ... | @@ -680,7 +652,6 @@ pub fn decodeFrameBlocks(dest: []u8, src: []const u8, consumed_count: *usize, ha |
| 680 | | 652 | |
| 681 | fn decodeRawBlock(dest: []u8, src: []const u8, block_size: u21, consumed_count: *usize) !usize { | 653 | fn decodeRawBlock(dest: []u8, src: []const u8, block_size: u21, consumed_count: *usize) !usize { |
| 682 | if (src.len < block_size) return error.MalformedBlockSize; | 654 | if (src.len < block_size) return error.MalformedBlockSize; |
| 683 | log.debug("writing raw block - size {d}", .{block_size}); | | |
| 684 | const data = src[0..block_size]; | 655 | const data = src[0..block_size]; |
| 685 | std.mem.copy(u8, dest, data); | 656 | std.mem.copy(u8, dest, data); |
| 686 | consumed_count.* += block_size; | 657 | consumed_count.* += block_size; |
| ... | @@ -689,7 +660,6 @@ fn decodeRawBlock(dest: []u8, src: []const u8, block_size: u21, consumed_count: | ... | @@ -689,7 +660,6 @@ fn decodeRawBlock(dest: []u8, src: []const u8, block_size: u21, consumed_count: |
| 689 | | 660 | |
| 690 | fn decodeRawBlockRingBuffer(dest: *RingBuffer, src: []const u8, block_size: u21, consumed_count: *usize) !usize { | 661 | fn decodeRawBlockRingBuffer(dest: *RingBuffer, src: []const u8, block_size: u21, consumed_count: *usize) !usize { |
| 691 | if (src.len < block_size) return error.MalformedBlockSize; | 662 | if (src.len < block_size) return error.MalformedBlockSize; |
| 692 | log.debug("writing raw block - size {d}", .{block_size}); | | |
| 693 | const data = src[0..block_size]; | 663 | const data = src[0..block_size]; |
| 694 | dest.writeSliceAssumeCapacity(data); | 664 | dest.writeSliceAssumeCapacity(data); |
| 695 | consumed_count.* += block_size; | 665 | consumed_count.* += block_size; |
| ... | @@ -698,7 +668,6 @@ fn decodeRawBlockRingBuffer(dest: *RingBuffer, src: []const u8, block_size: u21, | ... | @@ -698,7 +668,6 @@ fn decodeRawBlockRingBuffer(dest: *RingBuffer, src: []const u8, block_size: u21, |
| 698 | | 668 | |
| 699 | fn decodeRleBlock(dest: []u8, src: []const u8, block_size: u21, consumed_count: *usize) !usize { | 669 | fn decodeRleBlock(dest: []u8, src: []const u8, block_size: u21, consumed_count: *usize) !usize { |
| 700 | if (src.len < 1) return error.MalformedRleBlock; | 670 | if (src.len < 1) return error.MalformedRleBlock; |
| 701 | log.debug("writing rle block - '{x}'x{d}", .{ src[0], block_size }); | | |
| 702 | var write_pos: usize = 0; | 671 | var write_pos: usize = 0; |
| 703 | while (write_pos < block_size) : (write_pos += 1) { | 672 | while (write_pos < block_size) : (write_pos += 1) { |
| 704 | dest[write_pos] = src[0]; | 673 | dest[write_pos] = src[0]; |
| ... | @@ -709,7 +678,6 @@ fn decodeRleBlock(dest: []u8, src: []const u8, block_size: u21, consumed_count: | ... | @@ -709,7 +678,6 @@ fn decodeRleBlock(dest: []u8, src: []const u8, block_size: u21, consumed_count: |
| 709 | | 678 | |
| 710 | fn decodeRleBlockRingBuffer(dest: *RingBuffer, src: []const u8, block_size: u21, consumed_count: *usize) !usize { | 679 | fn decodeRleBlockRingBuffer(dest: *RingBuffer, src: []const u8, block_size: u21, consumed_count: *usize) !usize { |
| 711 | if (src.len < 1) return error.MalformedRleBlock; | 680 | if (src.len < 1) return error.MalformedRleBlock; |
| 712 | log.debug("writing rle block - '{x}'x{d}", .{ src[0], block_size }); | | |
| 713 | var write_pos: usize = 0; | 681 | var write_pos: usize = 0; |
| 714 | while (write_pos < block_size) : (write_pos += 1) { | 682 | while (write_pos < block_size) : (write_pos += 1) { |
| 715 | dest.writeAssumeCapacity(src[0]); | 683 | dest.writeAssumeCapacity(src[0]); |
| ... | @@ -751,7 +719,6 @@ pub fn decodeBlock( | ... | @@ -751,7 +719,6 @@ pub fn decodeBlock( |
| 751 | var sequence_size_limit = block_size_max; | 719 | var sequence_size_limit = block_size_max; |
| 752 | var i: usize = 0; | 720 | var i: usize = 0; |
| 753 | while (i < sequences_header.sequence_count) : (i += 1) { | 721 | while (i < sequences_header.sequence_count) : (i += 1) { |
| 754 | log.debug("decoding sequence {d}", .{i}); | | |
| 755 | const write_pos = written_count + bytes_written; | 722 | const write_pos = written_count + bytes_written; |
| 756 | const decompressed_size = try decode_state.decodeSequenceSlice( | 723 | const decompressed_size = try decode_state.decodeSequenceSlice( |
| 757 | dest, | 724 | dest, |
| ... | @@ -769,13 +736,8 @@ pub fn decodeBlock( | ... | @@ -769,13 +736,8 @@ pub fn decodeBlock( |
| 769 | } | 736 | } |
| 770 | | 737 | |
| 771 | if (decode_state.literal_written_count < literals.header.regenerated_size) { | 738 | if (decode_state.literal_written_count < literals.header.regenerated_size) { |
| 772 | log.debug("decoding remaining literals", .{}); | | |
| 773 | const len = literals.header.regenerated_size - decode_state.literal_written_count; | 739 | const len = literals.header.regenerated_size - decode_state.literal_written_count; |
| 774 | try decode_state.decodeLiteralsSlice(dest[written_count + bytes_written ..], literals, len); | 740 | try decode_state.decodeLiteralsSlice(dest[written_count + bytes_written ..], literals, len); |
| 775 | log.debug("remaining decoded literals at {d}: {}", .{ | | |
| 776 | written_count, | | |
| 777 | std.fmt.fmtSliceHexUpper(dest[written_count .. written_count + len]), | | |
| 778 | }); | | |
| 779 | bytes_written += len; | 741 | bytes_written += len; |
| 780 | } | 742 | } |
| 781 | | 743 | |
| ... | @@ -820,7 +782,6 @@ pub fn decodeBlockRingBuffer( | ... | @@ -820,7 +782,6 @@ pub fn decodeBlockRingBuffer( |
| 820 | var sequence_size_limit = block_size_max; | 782 | var sequence_size_limit = block_size_max; |
| 821 | var i: usize = 0; | 783 | var i: usize = 0; |
| 822 | while (i < sequences_header.sequence_count) : (i += 1) { | 784 | while (i < sequences_header.sequence_count) : (i += 1) { |
| 823 | log.debug("decoding sequence {d}", .{i}); | | |
| 824 | const decompressed_size = try decode_state.decodeSequenceRingBuffer( | 785 | const decompressed_size = try decode_state.decodeSequenceRingBuffer( |
| 825 | dest, | 786 | dest, |
| 826 | literals, | 787 | literals, |
| ... | @@ -836,15 +797,8 @@ pub fn decodeBlockRingBuffer( | ... | @@ -836,15 +797,8 @@ pub fn decodeBlockRingBuffer( |
| 836 | } | 797 | } |
| 837 | | 798 | |
| 838 | if (decode_state.literal_written_count < literals.header.regenerated_size) { | 799 | if (decode_state.literal_written_count < literals.header.regenerated_size) { |
| 839 | log.debug("decoding remaining literals", .{}); | | |
| 840 | const len = literals.header.regenerated_size - decode_state.literal_written_count; | 800 | const len = literals.header.regenerated_size - decode_state.literal_written_count; |
| 841 | try decode_state.decodeLiteralsRingBuffer(dest, literals, len); | 801 | try decode_state.decodeLiteralsRingBuffer(dest, literals, len); |
| 842 | const written_slice = dest.sliceLast(len); | | |
| 843 | log.debug("remaining decoded literals at {d}: {}{}", .{ | | |
| 844 | bytes_written, | | |
| 845 | std.fmt.fmtSliceHexUpper(written_slice.first), | | |
| 846 | std.fmt.fmtSliceHexUpper(written_slice.second), | | |
| 847 | }); | | |
| 848 | bytes_written += len; | 802 | bytes_written += len; |
| 849 | } | 803 | } |
| 850 | | 804 | |
| ... | @@ -922,22 +876,6 @@ pub fn decodeZStandardHeader(src: []const u8, consumed_count: ?*usize) !frame.ZS | ... | @@ -922,22 +876,6 @@ pub fn decodeZStandardHeader(src: []const u8, consumed_count: ?*usize) !frame.ZS |
| 922 | .dictionary_id = dictionary_id, | 876 | .dictionary_id = dictionary_id, |
| 923 | .content_size = content_size, | 877 | .content_size = content_size, |
| 924 | }; | 878 | }; |
| 925 | log.debug( | | |
| 926 | "decoded ZStandard frame header {x}: " ++ | | |
| 927 | "desc = (d={d},c={},r={},u={},s={},cs={d}), win_desc = {?x}, dict_id = {?x}, content_size = {?d}", | | |
| 928 | .{ | | |
| 929 | std.fmt.fmtSliceHexUpper(src[0..bytes_read_count]), | | |
| 930 | header.descriptor.dictionary_id_flag, | | |
| 931 | header.descriptor.content_checksum_flag, | | |
| 932 | header.descriptor.reserved, | | |
| 933 | header.descriptor.unused, | | |
| 934 | header.descriptor.single_segment_flag, | | |
| 935 | header.descriptor.content_size_flag, | | |
| 936 | header.window_descriptor, | | |
| 937 | header.dictionary_id, | | |
| 938 | header.content_size, | | |
| 939 | }, | | |
| 940 | ); | | |
| 941 | return header; | 879 | return header; |
| 942 | } | 880 | } |
| 943 | | 881 | |
| ... | @@ -945,12 +883,6 @@ pub fn decodeBlockHeader(src: *const [3]u8) frame.ZStandard.Block.Header { | ... | @@ -945,12 +883,6 @@ pub fn decodeBlockHeader(src: *const [3]u8) frame.ZStandard.Block.Header { |
| 945 | const last_block = src[0] & 1 == 1; | 883 | const last_block = src[0] & 1 == 1; |
| 946 | const block_type = @intToEnum(frame.ZStandard.Block.Type, (src[0] & 0b110) >> 1); | 884 | const block_type = @intToEnum(frame.ZStandard.Block.Type, (src[0] & 0b110) >> 1); |
| 947 | const block_size = ((src[0] & 0b11111000) >> 3) + (@as(u21, src[1]) << 5) + (@as(u21, src[2]) << 13); | 885 | const block_size = ((src[0] & 0b11111000) >> 3) + (@as(u21, src[1]) << 5) + (@as(u21, src[2]) << 13); |
| 948 | log.debug("decoded block header {}: last = {}, type = {s}, size = {d}", .{ | | |
| 949 | std.fmt.fmtSliceHexUpper(src), | | |
| 950 | last_block, | | |
| 951 | @tagName(block_type), | | |
| 952 | block_size, | | |
| 953 | }); | | |
| 954 | return .{ | 886 | return .{ |
| 955 | .last_block = last_block, | 887 | .last_block = last_block, |
| 956 | .block_type = block_type, | 888 | .block_type = block_type, |
| ... | @@ -990,8 +922,6 @@ pub fn decodeLiteralsSection(src: []const u8, consumed_count: *usize) !LiteralsS | ... | @@ -990,8 +922,6 @@ pub fn decodeLiteralsSection(src: []const u8, consumed_count: *usize) !LiteralsS |
| 990 | null; | 922 | null; |
| 991 | const huffman_tree_size = bytes_read - huffman_tree_start; | 923 | const huffman_tree_size = bytes_read - huffman_tree_start; |
| 992 | const total_streams_size = @as(usize, header.compressed_size.?) - huffman_tree_size; | 924 | const total_streams_size = @as(usize, header.compressed_size.?) - huffman_tree_size; |
| 993 | log.debug("huffman tree size = {}, total streams size = {}", .{ huffman_tree_size, total_streams_size }); | | |
| 994 | if (huffman_tree) |tree| dumpHuffmanTree(tree); | | |
| 995 | | 925 | |
| 996 | if (src.len < bytes_read + total_streams_size) return error.MalformedLiteralsSection; | 926 | if (src.len < bytes_read + total_streams_size) return error.MalformedLiteralsSection; |
| 997 | const stream_data = src[bytes_read .. bytes_read + total_streams_size]; | 927 | const stream_data = src[bytes_read .. bytes_read + total_streams_size]; |
| ... | @@ -1007,7 +937,6 @@ pub fn decodeLiteralsSection(src: []const u8, consumed_count: *usize) !LiteralsS | ... | @@ -1007,7 +937,6 @@ pub fn decodeLiteralsSection(src: []const u8, consumed_count: *usize) !LiteralsS |
| 1007 | | 937 | |
| 1008 | if (stream_data.len < 6) return error.MalformedLiteralsSection; | 938 | if (stream_data.len < 6) return error.MalformedLiteralsSection; |
| 1009 | | 939 | |
| 1010 | log.debug("jump table: {}", .{std.fmt.fmtSliceHexUpper(stream_data[0..6])}); | | |
| 1011 | const stream_1_length = @as(usize, readInt(u16, stream_data[0..2])); | 940 | const stream_1_length = @as(usize, readInt(u16, stream_data[0..2])); |
| 1012 | const stream_2_length = @as(usize, readInt(u16, stream_data[2..4])); | 941 | const stream_2_length = @as(usize, readInt(u16, stream_data[2..4])); |
| 1013 | const stream_3_length = @as(usize, readInt(u16, stream_data[4..6])); | 942 | const stream_3_length = @as(usize, readInt(u16, stream_data[4..6])); |
| ... | @@ -1059,8 +988,6 @@ fn decodeHuffmanTree(src: []const u8, consumed_count: *usize) !LiteralsSection.H | ... | @@ -1059,8 +988,6 @@ fn decodeHuffmanTree(src: []const u8, consumed_count: *usize) !LiteralsSection.H |
| 1059 | var huff_bits: ReverseBitReader = undefined; | 988 | var huff_bits: ReverseBitReader = undefined; |
| 1060 | try huff_bits.init(huff_data); | 989 | try huff_bits.init(huff_data); |
| 1061 | | 990 | |
| 1062 | dumpFseTable("huffman", entries[0..table_size]); | | |
| 1063 | | | |
| 1064 | var i: usize = 0; | 991 | var i: usize = 0; |
| 1065 | var even_state: u32 = try huff_bits.readBitsNoEof(u32, accuracy_log); | 992 | var even_state: u32 = try huff_bits.readBitsNoEof(u32, accuracy_log); |
| 1066 | var odd_state: u32 = try huff_bits.readBitsNoEof(u32, accuracy_log); | 993 | var odd_state: u32 = try huff_bits.readBitsNoEof(u32, accuracy_log); |
| ... | @@ -1073,7 +1000,6 @@ fn decodeHuffmanTree(src: []const u8, consumed_count: *usize) !LiteralsSection.H | ... | @@ -1073,7 +1000,6 @@ fn decodeHuffmanTree(src: []const u8, consumed_count: *usize) !LiteralsSection.H |
| 1073 | i += 1; | 1000 | i += 1; |
| 1074 | if (read_bits < even_data.bits) { | 1001 | if (read_bits < even_data.bits) { |
| 1075 | weights[i] = std.math.cast(u4, entries[odd_state].symbol) orelse return error.MalformedHuffmanTree; | 1002 | weights[i] = std.math.cast(u4, entries[odd_state].symbol) orelse return error.MalformedHuffmanTree; |
| 1076 | log.debug("overflow condition: setting weights[{d}] = {d}", .{ i, weights[i] }); | | |
| 1077 | i += 1; | 1003 | i += 1; |
| 1078 | break; | 1004 | break; |
| 1079 | } | 1005 | } |
| ... | @@ -1087,7 +1013,6 @@ fn decodeHuffmanTree(src: []const u8, consumed_count: *usize) !LiteralsSection.H | ... | @@ -1087,7 +1013,6 @@ fn decodeHuffmanTree(src: []const u8, consumed_count: *usize) !LiteralsSection.H |
| 1087 | if (read_bits < odd_data.bits) { | 1013 | if (read_bits < odd_data.bits) { |
| 1088 | if (i == 256) return error.MalformedHuffmanTree; | 1014 | if (i == 256) return error.MalformedHuffmanTree; |
| 1089 | weights[i] = std.math.cast(u4, entries[even_state].symbol) orelse return error.MalformedHuffmanTree; | 1015 | weights[i] = std.math.cast(u4, entries[even_state].symbol) orelse return error.MalformedHuffmanTree; |
| 1090 | log.debug("overflow condition: setting weights[{d}] = {d}", .{ i, weights[i] }); | | |
| 1091 | i += 1; | 1016 | i += 1; |
| 1092 | break; | 1017 | break; |
| 1093 | } | 1018 | } |
| ... | @@ -1099,19 +1024,12 @@ fn decodeHuffmanTree(src: []const u8, consumed_count: *usize) !LiteralsSection.H | ... | @@ -1099,19 +1024,12 @@ fn decodeHuffmanTree(src: []const u8, consumed_count: *usize) !LiteralsSection.H |
| 1099 | } else { | 1024 | } else { |
| 1100 | const encoded_symbol_count = header - 127; | 1025 | const encoded_symbol_count = header - 127; |
| 1101 | symbol_count = encoded_symbol_count + 1; | 1026 | symbol_count = encoded_symbol_count + 1; |
| 1102 | log.debug("huffman tree symbol count = {d}", .{symbol_count}); | | |
| 1103 | const weights_byte_count = (encoded_symbol_count + 1) / 2; | 1027 | const weights_byte_count = (encoded_symbol_count + 1) / 2; |
| 1104 | log.debug("decoding direct huffman tree: {}|{}", .{ | | |
| 1105 | std.fmt.fmtSliceHexUpper(src[0..1]), | | |
| 1106 | std.fmt.fmtSliceHexUpper(src[1 .. weights_byte_count + 1]), | | |
| 1107 | }); | | |
| 1108 | if (src.len < weights_byte_count) return error.MalformedHuffmanTree; | 1028 | if (src.len < weights_byte_count) return error.MalformedHuffmanTree; |
| 1109 | var i: usize = 0; | 1029 | var i: usize = 0; |
| 1110 | while (i < weights_byte_count) : (i += 1) { | 1030 | while (i < weights_byte_count) : (i += 1) { |
| 1111 | weights[2 * i] = @intCast(u4, src[i + 1] >> 4); | 1031 | weights[2 * i] = @intCast(u4, src[i + 1] >> 4); |
| 1112 | weights[2 * i + 1] = @intCast(u4, src[i + 1] & 0xF); | 1032 | weights[2 * i + 1] = @intCast(u4, src[i + 1] & 0xF); |
| 1113 | log.debug("weights[{d}] = {d}", .{ 2 * i, weights[2 * i] }); | | |
| 1114 | log.debug("weights[{d}] = {d}", .{ 2 * i + 1, weights[2 * i + 1] }); | | |
| 1115 | } | 1033 | } |
| 1116 | bytes_read += weights_byte_count; | 1034 | bytes_read += weights_byte_count; |
| 1117 | } | 1035 | } |
| ... | @@ -1121,13 +1039,11 @@ fn decodeHuffmanTree(src: []const u8, consumed_count: *usize) !LiteralsSection.H | ... | @@ -1121,13 +1039,11 @@ fn decodeHuffmanTree(src: []const u8, consumed_count: *usize) !LiteralsSection.H |
| 1121 | weight_power_sum += @as(u16, 1) << (value - 1); | 1039 | weight_power_sum += @as(u16, 1) << (value - 1); |
| 1122 | } | 1040 | } |
| 1123 | } | 1041 | } |
| 1124 | log.debug("weight power sum = {d}", .{weight_power_sum}); | | |
| 1125 | | 1042 | |
| 1126 | // advance to next power of two (even if weight_power_sum is a power of 2) | 1043 | // advance to next power of two (even if weight_power_sum is a power of 2) |
| 1127 | max_number_of_bits = std.math.log2_int(u16, weight_power_sum) + 1; | 1044 | max_number_of_bits = std.math.log2_int(u16, weight_power_sum) + 1; |
| 1128 | const next_power_of_two = @as(u16, 1) << max_number_of_bits; | 1045 | const next_power_of_two = @as(u16, 1) << max_number_of_bits; |
| 1129 | weights[symbol_count - 1] = std.math.log2_int(u16, next_power_of_two - weight_power_sum) + 1; | 1046 | weights[symbol_count - 1] = std.math.log2_int(u16, next_power_of_two - weight_power_sum) + 1; |
| 1130 | log.debug("weights[{d}] = {d}", .{ symbol_count - 1, weights[symbol_count - 1] }); | | |
| 1131 | | 1047 | |
| 1132 | var weight_sorted_prefixed_symbols: [256]LiteralsSection.HuffmanTree.PrefixedSymbol = undefined; | 1048 | var weight_sorted_prefixed_symbols: [256]LiteralsSection.HuffmanTree.PrefixedSymbol = undefined; |
| 1133 | for (weight_sorted_prefixed_symbols[0..symbol_count]) |_, i| { | 1049 | for (weight_sorted_prefixed_symbols[0..symbol_count]) |_, i| { |
| ... | @@ -1177,7 +1093,6 @@ fn decodeHuffmanTree(src: []const u8, consumed_count: *usize) !LiteralsSection.H | ... | @@ -1177,7 +1093,6 @@ fn decodeHuffmanTree(src: []const u8, consumed_count: *usize) !LiteralsSection.H |
| 1177 | .symbol_count_minus_one = @intCast(u8, prefixed_symbol_count - 1), | 1093 | .symbol_count_minus_one = @intCast(u8, prefixed_symbol_count - 1), |
| 1178 | .nodes = weight_sorted_prefixed_symbols, | 1094 | .nodes = weight_sorted_prefixed_symbols, |
| 1179 | }; | 1095 | }; |
| 1180 | log.debug("decoded huffman tree {}:", .{std.fmt.fmtSliceHexUpper(src[0..bytes_read])}); | | |
| 1181 | return tree; | 1096 | return tree; |
| 1182 | } | 1097 | } |
| 1183 | | 1098 | |
| ... | @@ -1194,7 +1109,6 @@ fn lessThanByWeight( | ... | @@ -1194,7 +1109,6 @@ fn lessThanByWeight( |
| 1194 | | 1109 | |
| 1195 | pub fn decodeLiteralsHeader(src: []const u8, consumed_count: *usize) !LiteralsSection.Header { | 1110 | pub fn decodeLiteralsHeader(src: []const u8, consumed_count: *usize) !LiteralsSection.Header { |
| 1196 | if (src.len == 0) return error.MalformedLiteralsSection; | 1111 | if (src.len == 0) return error.MalformedLiteralsSection; |
| 1197 | const start = consumed_count.*; | | |
| 1198 | const byte0 = src[0]; | 1112 | const byte0 = src[0]; |
| 1199 | const block_type = @intToEnum(LiteralsSection.BlockType, byte0 & 0b11); | 1113 | const block_type = @intToEnum(LiteralsSection.BlockType, byte0 & 0b11); |
| 1200 | const size_format = @intCast(u2, (byte0 & 0b1100) >> 2); | 1114 | const size_format = @intCast(u2, (byte0 & 0b1100) >> 2); |
| ... | @@ -1250,16 +1164,6 @@ pub fn decodeLiteralsHeader(src: []const u8, consumed_count: *usize) !LiteralsSe | ... | @@ -1250,16 +1164,6 @@ pub fn decodeLiteralsHeader(src: []const u8, consumed_count: *usize) !LiteralsSe |
| 1250 | } | 1164 | } |
| 1251 | }, | 1165 | }, |
| 1252 | } | 1166 | } |
| 1253 | log.debug( | | |
| 1254 | "decoded literals section header '{}': type = {s}, size_format = {}, regen_size = {d}, compressed size = {?d}", | | |
| 1255 | .{ | | |
| 1256 | std.fmt.fmtSliceHexUpper(src[0 .. consumed_count.* - start]), | | |
| 1257 | @tagName(block_type), | | |
| 1258 | size_format, | | |
| 1259 | regenerated_size, | | |
| 1260 | compressed_size, | | |
| 1261 | }, | | |
| 1262 | ); | | |
| 1263 | return LiteralsSection.Header{ | 1167 | return LiteralsSection.Header{ |
| 1264 | .block_type = block_type, | 1168 | .block_type = block_type, |
| 1265 | .size_format = size_format, | 1169 | .size_format = size_format, |
| ... | @@ -1276,7 +1180,6 @@ pub fn decodeSequencesHeader(src: []const u8, consumed_count: *usize) !Sequences | ... | @@ -1276,7 +1180,6 @@ pub fn decodeSequencesHeader(src: []const u8, consumed_count: *usize) !Sequences |
| 1276 | const byte0 = src[0]; | 1180 | const byte0 = src[0]; |
| 1277 | if (byte0 == 0) { | 1181 | if (byte0 == 0) { |
| 1278 | bytes_read += 1; | 1182 | bytes_read += 1; |
| 1279 | log.debug("decoded sequences header '{}': sequence count = 0", .{std.fmt.fmtSliceHexUpper(src[0..bytes_read])}); | | |
| 1280 | consumed_count.* += bytes_read; | 1183 | consumed_count.* += bytes_read; |
| 1281 | return SequencesSection.Header{ | 1184 | return SequencesSection.Header{ |
| 1282 | .sequence_count = 0, | 1185 | .sequence_count = 0, |
| ... | @@ -1305,13 +1208,6 @@ pub fn decodeSequencesHeader(src: []const u8, consumed_count: *usize) !Sequences | ... | @@ -1305,13 +1208,6 @@ pub fn decodeSequencesHeader(src: []const u8, consumed_count: *usize) !Sequences |
| 1305 | const matches_mode = @intToEnum(SequencesSection.Header.Mode, (compression_modes & 0b00001100) >> 2); | 1208 | const matches_mode = @intToEnum(SequencesSection.Header.Mode, (compression_modes & 0b00001100) >> 2); |
| 1306 | const offsets_mode = @intToEnum(SequencesSection.Header.Mode, (compression_modes & 0b00110000) >> 4); | 1209 | const offsets_mode = @intToEnum(SequencesSection.Header.Mode, (compression_modes & 0b00110000) >> 4); |
| 1307 | const literal_mode = @intToEnum(SequencesSection.Header.Mode, (compression_modes & 0b11000000) >> 6); | 1210 | const literal_mode = @intToEnum(SequencesSection.Header.Mode, (compression_modes & 0b11000000) >> 6); |
| 1308 | log.debug("decoded sequences header '{}': (sc={d},o={s},m={s},l={s})", .{ | | |
| 1309 | std.fmt.fmtSliceHexUpper(src[0..bytes_read]), | | |
| 1310 | sequence_count, | | |
| 1311 | @tagName(offsets_mode), | | |
| 1312 | @tagName(matches_mode), | | |
| 1313 | @tagName(literal_mode), | | |
| 1314 | }); | | |
| 1315 | if (compression_modes & 0b11 != 0) return error.ReservedBitSet; | 1211 | if (compression_modes & 0b11 != 0) return error.ReservedBitSet; |
| 1316 | | 1212 | |
| 1317 | return SequencesSection.Header{ | 1213 | return SequencesSection.Header{ |
| ... | @@ -1383,10 +1279,7 @@ fn decodeFseTable( | ... | @@ -1383,10 +1279,7 @@ fn decodeFseTable( |
| 1383 | max_accuracy_log: u4, | 1279 | max_accuracy_log: u4, |
| 1384 | entries: []Table.Fse, | 1280 | entries: []Table.Fse, |
| 1385 | ) !usize { | 1281 | ) !usize { |
| 1386 | log.debug("decoding fse table {d} {d}", .{ max_accuracy_log, expected_symbol_count }); | | |
| 1387 | | | |
| 1388 | const accuracy_log_biased = try bit_reader.readBitsNoEof(u4, 4); | 1282 | const accuracy_log_biased = try bit_reader.readBitsNoEof(u4, 4); |
| 1389 | log.debug("accuracy_log_biased = {d}", .{accuracy_log_biased}); | | |
| 1390 | if (accuracy_log_biased > max_accuracy_log -| 5) return error.MalformedAccuracyLog; | 1283 | if (accuracy_log_biased > max_accuracy_log -| 5) return error.MalformedAccuracyLog; |
| 1391 | const accuracy_log = accuracy_log_biased + 5; | 1284 | const accuracy_log = accuracy_log_biased + 5; |
| 1392 | | 1285 | |
| ... | @@ -1394,7 +1287,6 @@ fn decodeFseTable( | ... | @@ -1394,7 +1287,6 @@ fn decodeFseTable( |
| 1394 | var value_count: usize = 0; | 1287 | var value_count: usize = 0; |
| 1395 | | 1288 | |
| 1396 | const total_probability = @as(u16, 1) << accuracy_log; | 1289 | const total_probability = @as(u16, 1) << accuracy_log; |
| 1397 | log.debug("total probability = {d}", .{total_probability}); | | |
| 1398 | var accumulated_probability: u16 = 0; | 1290 | var accumulated_probability: u16 = 0; |
| 1399 | | 1291 | |
| 1400 | while (accumulated_probability < total_probability) { | 1292 | while (accumulated_probability < total_probability) { |
| ... | @@ -1549,17 +1441,3 @@ test buildFseTable { | ... | @@ -1549,17 +1441,3 @@ test buildFseTable { |
| 1549 | try buildFseTable(&offset_codes_default_values, entries[0..32]); | 1441 | try buildFseTable(&offset_codes_default_values, entries[0..32]); |
| 1550 | try std.testing.expectEqualSlices(Table.Fse, types.compressed_block.predefined_offset_fse_table.fse, entries[0..32]); | 1442 | try std.testing.expectEqualSlices(Table.Fse, types.compressed_block.predefined_offset_fse_table.fse, entries[0..32]); |
| 1551 | } | 1443 | } |
| 1552 | | | |
| 1553 | fn dumpFseTable(prefix: []const u8, table: []const Table.Fse) void { | | |
| 1554 | log.debug("{s} fse table:", .{prefix}); | | |
| 1555 | for (table) |entry, i| { | | |
| 1556 | log.debug("state = {d} symbol = {d} bl = {d}, bits = {d}", .{ i, entry.symbol, entry.baseline, entry.bits }); | | |
| 1557 | } | | |
| 1558 | } | | |
| 1559 | | | |
| 1560 | fn dumpHuffmanTree(tree: LiteralsSection.HuffmanTree) void { | | |
| 1561 | log.debug("Huffman tree: max bit count = {}, symbol count = {}", .{ tree.max_bit_count, tree.symbol_count_minus_one + 1 }); | | |
| 1562 | for (tree.nodes[0 .. tree.symbol_count_minus_one + 1]) |node| { | | |
| 1563 | log.debug("symbol = {[symbol]d}, prefix = {[prefix]d}, weight = {[weight]d}", node); | | |
| 1564 | } | | |
| 1565 | } | | |