| ... | @@ -864,7 +864,7 @@ test "dynamic block (type 2)" { | ... | @@ -864,7 +864,7 @@ test "dynamic block (type 2)" { |
| 864 | fn testBasicCase(in: []const u8, out: []const u8) !void { | 864 | fn testBasicCase(in: []const u8, out: []const u8) !void { |
| 865 | var reader: Reader = .fixed(in); | 865 | var reader: Reader = .fixed(in); |
| 866 | var aw: Writer.Allocating = .init(testing.allocator); | 866 | var aw: Writer.Allocating = .init(testing.allocator); |
| 867 | try aw.ensureUnusedCapacity(flate.history_len + 1); | 867 | try aw.ensureUnusedCapacity(flate.history_len); |
| 868 | defer aw.deinit(); | 868 | defer aw.deinit(); |
| 869 | | 869 | |
| 870 | var decompress: Decompress = .init(&reader, .raw, &.{}); | 870 | var decompress: Decompress = .init(&reader, .raw, &.{}); |
| ... | @@ -873,63 +873,53 @@ fn testBasicCase(in: []const u8, out: []const u8) !void { | ... | @@ -873,63 +873,53 @@ fn testBasicCase(in: []const u8, out: []const u8) !void { |
| 873 | try testing.expectEqualStrings(out, aw.getWritten()); | 873 | try testing.expectEqualStrings(out, aw.getWritten()); |
| 874 | } | 874 | } |
| 875 | | 875 | |
| 876 | test "gzip decompress" { | 876 | test "gzip non compressed block (type 0)" { |
| 877 | const cases = [_]struct { | 877 | try testGzipDecompress(&[_]u8{ |
| 878 | in: []const u8, | 878 | 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, // gzip header (10 bytes) |
| 879 | out: []const u8, | 879 | 0b0000_0001, 0b0000_1100, 0x00, 0b1111_0011, 0xff, // deflate fixed buffer header len, nlen |
| 880 | }{ | 880 | 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 0x0a, // non compressed data |
| 881 | // non compressed block (type 0) | 881 | 0xd5, 0xe0, 0x39, 0xb7, // gzip footer: checksum |
| 882 | .{ | 882 | 0x0c, 0x00, 0x00, 0x00, // gzip footer: size |
| 883 | .in = &[_]u8{ | 883 | }, "Hello world\n"); |
| 884 | 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, // gzip header (10 bytes) | 884 | } |
| 885 | 0b0000_0001, 0b0000_1100, 0x00, 0b1111_0011, 0xff, // deflate fixed buffer header len, nlen | | |
| 886 | 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 0x0a, // non compressed data | | |
| 887 | 0xd5, 0xe0, 0x39, 0xb7, // gzip footer: checksum | | |
| 888 | 0x0c, 0x00, 0x00, 0x00, // gzip footer: size | | |
| 889 | }, | | |
| 890 | .out = "Hello world\n", | | |
| 891 | }, | | |
| 892 | // fixed code block (type 1) | | |
| 893 | .{ | | |
| 894 | .in = &[_]u8{ | | |
| 895 | 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x03, // gzip header (10 bytes) | | |
| 896 | 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0x57, 0x28, 0xcf, // deflate data block type 1 | | |
| 897 | 0x2f, 0xca, 0x49, 0xe1, 0x02, 0x00, | | |
| 898 | 0xd5, 0xe0, 0x39, 0xb7, 0x0c, 0x00, 0x00, 0x00, // gzip footer (chksum, len) | | |
| 899 | }, | | |
| 900 | .out = "Hello world\n", | | |
| 901 | }, | | |
| 902 | // dynamic block (type 2) | | |
| 903 | .{ | | |
| 904 | .in = &[_]u8{ | | |
| 905 | 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, // gzip header (10 bytes) | | |
| 906 | 0x3d, 0xc6, 0x39, 0x11, 0x00, 0x00, 0x0c, 0x02, // deflate data block type 2 | | |
| 907 | 0x30, 0x2b, 0xb5, 0x52, 0x1e, 0xff, 0x96, 0x38, | | |
| 908 | 0x16, 0x96, 0x5c, 0x1e, 0x94, 0xcb, 0x6d, 0x01, | | |
| 909 | 0x17, 0x1c, 0x39, 0xb4, 0x13, 0x00, 0x00, 0x00, // gzip footer (chksum, len) | | |
| 910 | }, | | |
| 911 | .out = "ABCDEABCD ABCDEABCD", | | |
| 912 | }, | | |
| 913 | // gzip header with name | | |
| 914 | .{ | | |
| 915 | .in = &[_]u8{ | | |
| 916 | 0x1f, 0x8b, 0x08, 0x08, 0xe5, 0x70, 0xb1, 0x65, 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e, | | |
| 917 | 0x74, 0x78, 0x74, 0x00, 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1, | | |
| 918 | 0x02, 0x00, 0xd5, 0xe0, 0x39, 0xb7, 0x0c, 0x00, 0x00, 0x00, | | |
| 919 | }, | | |
| 920 | .out = "Hello world\n", | | |
| 921 | }, | | |
| 922 | }; | | |
| 923 | for (cases) |c| { | | |
| 924 | var fb: Reader = .fixed(c.in); | | |
| 925 | var aw: Writer.Allocating = .init(testing.allocator); | | |
| 926 | defer aw.deinit(); | | |
| 927 | | 885 | |
| 928 | var decompress: Decompress = .init(&fb, .gzip, &.{}); | 886 | test "gzip fixed code block (type 1)" { |
| 929 | const r = &decompress.reader; | 887 | try testGzipDecompress(&[_]u8{ |
| 930 | _ = try r.streamRemaining(&aw.writer); | 888 | 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x03, // gzip header (10 bytes) |
| 931 | try testing.expectEqualStrings(c.out, aw.getWritten()); | 889 | 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0x57, 0x28, 0xcf, // deflate data block type 1 |
| 932 | } | 890 | 0x2f, 0xca, 0x49, 0xe1, 0x02, 0x00, |
| | 891 | 0xd5, 0xe0, 0x39, 0xb7, 0x0c, 0x00, 0x00, 0x00, // gzip footer (chksum, len) |
| | 892 | }, "Hello world\n"); |
| | 893 | } |
| | 894 | |
| | 895 | test "gzip dynamic block (type 2)" { |
| | 896 | try testGzipDecompress(&[_]u8{ |
| | 897 | 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, // gzip header (10 bytes) |
| | 898 | 0x3d, 0xc6, 0x39, 0x11, 0x00, 0x00, 0x0c, 0x02, // deflate data block type 2 |
| | 899 | 0x30, 0x2b, 0xb5, 0x52, 0x1e, 0xff, 0x96, 0x38, |
| | 900 | 0x16, 0x96, 0x5c, 0x1e, 0x94, 0xcb, 0x6d, 0x01, |
| | 901 | 0x17, 0x1c, 0x39, 0xb4, 0x13, 0x00, 0x00, 0x00, // gzip footer (chksum, len) |
| | 902 | }, "ABCDEABCD ABCDEABCD"); |
| | 903 | } |
| | 904 | |
| | 905 | test "gzip header with name" { |
| | 906 | try testGzipDecompress(&[_]u8{ |
| | 907 | 0x1f, 0x8b, 0x08, 0x08, 0xe5, 0x70, 0xb1, 0x65, 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e, |
| | 908 | 0x74, 0x78, 0x74, 0x00, 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1, |
| | 909 | 0x02, 0x00, 0xd5, 0xe0, 0x39, 0xb7, 0x0c, 0x00, 0x00, 0x00, |
| | 910 | }, "Hello world\n"); |
| | 911 | } |
| | 912 | |
| | 913 | fn testGzipDecompress(in: []const u8, out: []const u8) !void { |
| | 914 | var reader: Reader = .fixed(in); |
| | 915 | var aw: Writer.Allocating = .init(testing.allocator); |
| | 916 | try aw.ensureUnusedCapacity(flate.history_len); |
| | 917 | defer aw.deinit(); |
| | 918 | |
| | 919 | var decompress: Decompress = .init(&reader, .gzip, &.{}); |
| | 920 | const r = &decompress.reader; |
| | 921 | _ = try r.streamRemaining(&aw.writer); |
| | 922 | try testing.expectEqualStrings(out, aw.getWritten()); |
| 933 | } | 923 | } |
| 934 | | 924 | |
| 935 | test "zlib decompress" { | 925 | test "zlib decompress" { |