| ... | ... | @@ -842,40 +842,29 @@ test "encode/decode literals" { |
| 842 | 842 | } |
| 843 | 843 | |
| 844 | 844 | test "non compressed block (type 0)" { |
| 845 | | try testBasicCase(&[_]u8{ |
| 845 | try testDecompress(.raw, &[_]u8{ |
| 846 | 846 | 0b0000_0001, 0b0000_1100, 0x00, 0b1111_0011, 0xff, // deflate fixed buffer header len, nlen |
| 847 | 847 | 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 0x0a, // non compressed data |
| 848 | 848 | }, "Hello world\n"); |
| 849 | 849 | } |
| 850 | 850 | |
| 851 | 851 | test "fixed code block (type 1)" { |
| 852 | | try testBasicCase(&[_]u8{ |
| 852 | try testDecompress(.raw, &[_]u8{ |
| 853 | 853 | 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0x57, 0x28, 0xcf, // deflate data block type 1 |
| 854 | 854 | 0x2f, 0xca, 0x49, 0xe1, 0x02, 0x00, |
| 855 | 855 | }, "Hello world\n"); |
| 856 | 856 | } |
| 857 | 857 | |
| 858 | 858 | test "dynamic block (type 2)" { |
| 859 | | try testBasicCase(&[_]u8{ |
| 859 | try testDecompress(.raw, &[_]u8{ |
| 860 | 860 | 0x3d, 0xc6, 0x39, 0x11, 0x00, 0x00, 0x0c, 0x02, // deflate data block type 2 |
| 861 | 861 | 0x30, 0x2b, 0xb5, 0x52, 0x1e, 0xff, 0x96, 0x38, |
| 862 | 862 | 0x16, 0x96, 0x5c, 0x1e, 0x94, 0xcb, 0x6d, 0x01, |
| 863 | 863 | }, "ABCDEABCD ABCDEABCD"); |
| 864 | 864 | } |
| 865 | 865 | |
| 866 | | fn testBasicCase(in: []const u8, out: []const u8) !void { |
| 867 | | var reader: Reader = .fixed(in); |
| 868 | | var aw: Writer.Allocating = .init(testing.allocator); |
| 869 | | try aw.ensureUnusedCapacity(flate.history_len); |
| 870 | | defer aw.deinit(); |
| 871 | | |
| 872 | | var decompress: Decompress = .init(&reader, .raw, &.{}); |
| 873 | | _ = try decompress.reader.streamRemaining(&aw.writer); |
| 874 | | try testing.expectEqualStrings(out, aw.getWritten()); |
| 875 | | } |
| 876 | | |
| 877 | 866 | test "gzip non compressed block (type 0)" { |
| 878 | | try testGzipDecompress(&[_]u8{ |
| 867 | try testDecompress(.gzip, &[_]u8{ |
| 879 | 868 | 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, // gzip header (10 bytes) |
| 880 | 869 | 0b0000_0001, 0b0000_1100, 0x00, 0b1111_0011, 0xff, // deflate fixed buffer header len, nlen |
| 881 | 870 | 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 0x0a, // non compressed data |
| ... | ... | @@ -885,7 +874,7 @@ test "gzip non compressed block (type 0)" { |
| 885 | 874 | } |
| 886 | 875 | |
| 887 | 876 | test "gzip fixed code block (type 1)" { |
| 888 | | try testGzipDecompress(&[_]u8{ |
| 877 | try testDecompress(.gzip, &[_]u8{ |
| 889 | 878 | 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x03, // gzip header (10 bytes) |
| 890 | 879 | 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0x57, 0x28, 0xcf, // deflate data block type 1 |
| 891 | 880 | 0x2f, 0xca, 0x49, 0xe1, 0x02, 0x00, |
| ... | ... | @@ -894,7 +883,7 @@ test "gzip fixed code block (type 1)" { |
| 894 | 883 | } |
| 895 | 884 | |
| 896 | 885 | test "gzip dynamic block (type 2)" { |
| 897 | | try testGzipDecompress(&[_]u8{ |
| 886 | try testDecompress(.gzip, &[_]u8{ |
| 898 | 887 | 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, // gzip header (10 bytes) |
| 899 | 888 | 0x3d, 0xc6, 0x39, 0x11, 0x00, 0x00, 0x0c, 0x02, // deflate data block type 2 |
| 900 | 889 | 0x30, 0x2b, 0xb5, 0x52, 0x1e, 0xff, 0x96, 0x38, |
| ... | ... | @@ -904,50 +893,20 @@ test "gzip dynamic block (type 2)" { |
| 904 | 893 | } |
| 905 | 894 | |
| 906 | 895 | test "gzip header with name" { |
| 907 | | try testGzipDecompress(&[_]u8{ |
| 896 | try testDecompress(.gzip, &[_]u8{ |
| 908 | 897 | 0x1f, 0x8b, 0x08, 0x08, 0xe5, 0x70, 0xb1, 0x65, 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e, |
| 909 | 898 | 0x74, 0x78, 0x74, 0x00, 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1, |
| 910 | 899 | 0x02, 0x00, 0xd5, 0xe0, 0x39, 0xb7, 0x0c, 0x00, 0x00, 0x00, |
| 911 | 900 | }, "Hello world\n"); |
| 912 | 901 | } |
| 913 | 902 | |
| 914 | | fn testGzipDecompress(in: []const u8, out: []const u8) !void { |
| 915 | | var reader: Reader = .fixed(in); |
| 916 | | var aw: Writer.Allocating = .init(testing.allocator); |
| 917 | | try aw.ensureUnusedCapacity(flate.history_len); |
| 918 | | defer aw.deinit(); |
| 919 | | |
| 920 | | var decompress: Decompress = .init(&reader, .gzip, &.{}); |
| 921 | | _ = try decompress.reader.streamRemaining(&aw.writer); |
| 922 | | try testing.expectEqualStrings(out, aw.getWritten()); |
| 923 | | } |
| 924 | | |
| 925 | | test "zlib decompress" { |
| 926 | | const cases = [_]struct { |
| 927 | | in: []const u8, |
| 928 | | out: []const u8, |
| 929 | | }{ |
| 930 | | // non compressed block (type 0) |
| 931 | | .{ |
| 932 | | .in = &[_]u8{ |
| 933 | | 0x78, 0b10_0_11100, // zlib header (2 bytes) |
| 934 | | 0b0000_0001, 0b0000_1100, 0x00, 0b1111_0011, 0xff, // deflate fixed buffer header len, nlen |
| 935 | | 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 0x0a, // non compressed data |
| 936 | | 0x1c, 0xf2, 0x04, 0x47, // zlib footer: checksum |
| 937 | | }, |
| 938 | | .out = "Hello world\n", |
| 939 | | }, |
| 940 | | }; |
| 941 | | for (cases) |c| { |
| 942 | | var fb: Reader = .fixed(c.in); |
| 943 | | var aw: Writer.Allocating = .init(testing.allocator); |
| 944 | | defer aw.deinit(); |
| 945 | | |
| 946 | | var decompress: Decompress = .init(&fb, .zlib, &.{}); |
| 947 | | const r = &decompress.reader; |
| 948 | | _ = try r.streamRemaining(&aw.writer); |
| 949 | | try testing.expectEqualStrings(c.out, aw.getWritten()); |
| 950 | | } |
| 903 | test "zlib decompress non compressed block (type 0)" { |
| 904 | try testDecompress(.zlib, &[_]u8{ |
| 905 | 0x78, 0b10_0_11100, // zlib header (2 bytes) |
| 906 | 0b0000_0001, 0b0000_1100, 0x00, 0b1111_0011, 0xff, // deflate fixed buffer header len, nlen |
| 907 | 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 0x0a, // non compressed data |
| 908 | 0x1c, 0xf2, 0x04, 0x47, // zlib footer: checksum |
| 909 | }, "Hello world\n"); |
| 951 | 910 | } |
| 952 | 911 | |
| 953 | 912 | test "fuzzing tests" { |
| ... | ... | @@ -1001,6 +960,7 @@ test "fuzzing tests" { |
| 1001 | 960 | inline for (cases) |c| { |
| 1002 | 961 | var in: Reader = .fixed(@embedFile("testdata/fuzz/" ++ c.input ++ ".input")); |
| 1003 | 962 | var aw: Writer.Allocating = .init(testing.allocator); |
| 963 | try aw.ensureUnusedCapacity(flate.history_len); |
| 1004 | 964 | defer aw.deinit(); |
| 1005 | 965 | |
| 1006 | 966 | var decompress: Decompress = .init(&in, .raw, &.{}); |
| ... | ... | @@ -1021,6 +981,7 @@ test "bug 18966" { |
| 1021 | 981 | |
| 1022 | 982 | var in: Reader = .fixed(input); |
| 1023 | 983 | var aw: Writer.Allocating = .init(testing.allocator); |
| 984 | try aw.ensureUnusedCapacity(flate.history_len); |
| 1024 | 985 | defer aw.deinit(); |
| 1025 | 986 | |
| 1026 | 987 | var decompress: Decompress = .init(&in, .gzip, &.{}); |
| ... | ... | @@ -1146,6 +1107,7 @@ test "gzip header" { |
| 1146 | 1107 | fn testDecompress(container: Container, compressed: []const u8, expected_plain: []const u8) !void { |
| 1147 | 1108 | var in: std.Io.Reader = .fixed(compressed); |
| 1148 | 1109 | var aw: std.Io.Writer.Allocating = .init(testing.allocator); |
| 1110 | try aw.ensureUnusedCapacity(flate.history_len); |
| 1149 | 1111 | defer aw.deinit(); |
| 1150 | 1112 | |
| 1151 | 1113 | var decompress: Decompress = .init(&in, container, &.{}); |