diff --git a/Project.toml b/Project.toml index fa3a41a..15efc4f 100644 --- a/Project.toml +++ b/Project.toml @@ -10,6 +10,6 @@ TranscodingStreams = "3bb67fe8-82b1-5028-8e26-92a6c54297fa" Zstd_jll = "3161d3a3-bdf6-5164-811a-617609db77b4" [compat] -TranscodingStreams = "0.9, 0.10, 0.11" +TranscodingStreams = "0.11.3" Zstd_jll = "1.5.5" -julia = "1.3" +julia = "1.6" diff --git a/src/compression.jl b/src/compression.jl index 8ce79dd..7476650 100644 --- a/src/compression.jl +++ b/src/compression.jl @@ -105,23 +105,21 @@ function TranscodingStreams.startproc(codec::ZstdCompressor, mode::Symbol, error return :ok end -if isdefined(TranscodingStreams, :pledgeinsize) - function TranscodingStreams.pledgeinsize(codec::ZstdCompressor, insize::Int64, error::Error)::Symbol - if codec.cstream.ptr == C_NULL - Base.error("`startproc` must be called before `pledgeinsize`") - end - srcsize = if signbit(insize) - ZSTD_CONTENTSIZE_UNKNOWN - else - Culonglong(insize) - end - code = LibZstd.ZSTD_CCtx_setPledgedSrcSize(codec.cstream, srcsize) - if iserror(code) - error[] = ErrorException("zstd error setting pledged source size") - :error - else - :ok - end +function TranscodingStreams.pledgeinsize(codec::ZstdCompressor, insize::Int64, error::Error)::Symbol + if codec.cstream.ptr == C_NULL + Base.error("`startproc` must be called before `pledgeinsize`") + end + srcsize = if signbit(insize) + ZSTD_CONTENTSIZE_UNKNOWN + else + Culonglong(insize) + end + code = LibZstd.ZSTD_CCtx_setPledgedSrcSize(codec.cstream, srcsize) + if iserror(code) + error[] = ErrorException("zstd error setting pledged source size") + :error + else + :ok end end diff --git a/test/runtests.jl b/test/runtests.jl index 78402cc..56b8d5a 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -165,63 +165,61 @@ include("utils.jl") end @testset "pledgeinsize" begin - if isdefined(TranscodingStreams, :pledgeinsize) - # when pledgeinsize is available transcode should save the - # decompressed size in a header - for n in [0:30; 1000; 1000000;] - v = transcode(ZstdCompressor, rand(UInt8, n)) - @test CodecZstd.find_decompressed_size(v) == n - end + # when pledgeinsize is available transcode should save the + # decompressed size in a header + for n in [0:30; 1000; 1000000;] + v = transcode(ZstdCompressor, rand(UInt8, n)) + @test CodecZstd.find_decompressed_size(v) == n + end - # Test what happens if pledgeinsize promise is broken - d1 = zeros(UInt8, 10000) - d2 = zeros(UInt8, 10000) - GC.@preserve d1 d2 begin - @testset "too many bytes" begin - m1 = TranscodingStreams.Memory(pointer(d1), 1000) - m2 = TranscodingStreams.Memory(pointer(d2), 1000) - codec = ZstdCompressor() - e = TranscodingStreams.Error() - @test TranscodingStreams.startproc(codec, :read, e) === :ok - @test TranscodingStreams.pledgeinsize(codec, Int64(10), e) === :ok - @test TranscodingStreams.process(codec, m1, m2, e) === (0, 0, :error) - @test e[] == ErrorException("zstd error: Src size is incorrect") - TranscodingStreams.finalize(codec) - end - @testset "too few bytes" begin - m1 = TranscodingStreams.Memory(pointer(d1), 10) - m2 = TranscodingStreams.Memory(pointer(d2), 1000) - codec = ZstdCompressor() - e = TranscodingStreams.Error() - @test TranscodingStreams.startproc(codec, :read, e) === :ok - @test TranscodingStreams.pledgeinsize(codec, Int64(10000), e) === :ok - @test TranscodingStreams.process(codec, m1, m2, e)[3] === :ok - m1 = TranscodingStreams.Memory(pointer(d1), 0) - @test TranscodingStreams.process(codec, m1, m2, e)[3] === :error - @test e[] == ErrorException("zstd error: Src size is incorrect") - TranscodingStreams.finalize(codec) - end - @testset "set pledgeinsize after process" begin - m1 = TranscodingStreams.Memory(pointer(d1), 1000) - m2 = TranscodingStreams.Memory(pointer(d2), 1000) - codec = ZstdCompressor() - e = TranscodingStreams.Error() - @test TranscodingStreams.startproc(codec, :read, e) === :ok - @test TranscodingStreams.process(codec, m1, m2, e)[3] === :ok - @test TranscodingStreams.pledgeinsize(codec, Int64(10000), e) === :error - @test e[] == ErrorException("zstd error setting pledged source size") - TranscodingStreams.finalize(codec) - end - @testset "set unknown pledgeinsize" begin - m1 = TranscodingStreams.Memory(pointer(d1), 1000) - m2 = TranscodingStreams.Memory(pointer(d2), 1000) - codec = ZstdCompressor() - e = TranscodingStreams.Error() - @test TranscodingStreams.startproc(codec, :read, e) === :ok - @test TranscodingStreams.pledgeinsize(codec, Int64(-1), e) === :ok - @test TranscodingStreams.process(codec, m1, m2, e)[3] === :ok - TranscodingStreams.finalize(codec) - end + # Test what happens if pledgeinsize promise is broken + d1 = zeros(UInt8, 10000) + d2 = zeros(UInt8, 10000) + GC.@preserve d1 d2 begin + @testset "too many bytes" begin + m1 = TranscodingStreams.Memory(pointer(d1), 1000) + m2 = TranscodingStreams.Memory(pointer(d2), 1000) + codec = ZstdCompressor() + e = TranscodingStreams.Error() + @test TranscodingStreams.startproc(codec, :read, e) === :ok + @test TranscodingStreams.pledgeinsize(codec, Int64(10), e) === :ok + @test TranscodingStreams.process(codec, m1, m2, e) === (0, 0, :error) + @test e[] == ErrorException("zstd error: Src size is incorrect") + TranscodingStreams.finalize(codec) + end + @testset "too few bytes" begin + m1 = TranscodingStreams.Memory(pointer(d1), 10) + m2 = TranscodingStreams.Memory(pointer(d2), 1000) + codec = ZstdCompressor() + e = TranscodingStreams.Error() + @test TranscodingStreams.startproc(codec, :read, e) === :ok + @test TranscodingStreams.pledgeinsize(codec, Int64(10000), e) === :ok + @test TranscodingStreams.process(codec, m1, m2, e)[3] === :ok + m1 = TranscodingStreams.Memory(pointer(d1), 0) + @test TranscodingStreams.process(codec, m1, m2, e)[3] === :error + @test e[] == ErrorException("zstd error: Src size is incorrect") + TranscodingStreams.finalize(codec) + end + @testset "set pledgeinsize after process" begin + m1 = TranscodingStreams.Memory(pointer(d1), 1000) + m2 = TranscodingStreams.Memory(pointer(d2), 1000) + codec = ZstdCompressor() + e = TranscodingStreams.Error() + @test TranscodingStreams.startproc(codec, :read, e) === :ok + @test TranscodingStreams.process(codec, m1, m2, e)[3] === :ok + @test TranscodingStreams.pledgeinsize(codec, Int64(10000), e) === :error + @test e[] == ErrorException("zstd error setting pledged source size") + TranscodingStreams.finalize(codec) + end + @testset "set unknown pledgeinsize" begin + m1 = TranscodingStreams.Memory(pointer(d1), 1000) + m2 = TranscodingStreams.Memory(pointer(d2), 1000) + codec = ZstdCompressor() + e = TranscodingStreams.Error() + @test TranscodingStreams.startproc(codec, :read, e) === :ok + @test TranscodingStreams.pledgeinsize(codec, Int64(-1), e) === :ok + @test TranscodingStreams.process(codec, m1, m2, e)[3] === :ok + TranscodingStreams.finalize(codec) end end end @@ -265,11 +263,9 @@ include("utils.jl") TranscodingStreams.finalize(codec) data = [0x00,0x01] GC.@preserve data let m = TranscodingStreams.Memory(pointer(data), length(data)) - if isdefined(TranscodingStreams, :pledgeinsize) - try - TranscodingStreams.pledgeinsize(codec, Int64(10), TranscodingStreams.Error()) - catch - end + try + TranscodingStreams.pledgeinsize(codec, Int64(10), TranscodingStreams.Error()) + catch end try TranscodingStreams.expectedsize(codec, m)