Skip to content

Commit

Permalink
Add pledgeinsize
Browse files Browse the repository at this point in the history
  • Loading branch information
nhz2 committed Sep 7, 2024
1 parent 31cf742 commit 11cff42
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
28 changes: 23 additions & 5 deletions src/compression.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,31 @@ function TranscodingStreams.finalize(codec::ZstdCompressor)
return
end

function TranscodingStreams.startproc(codec::ZstdCompressor, mode::Symbol, error::Error)
code = reset!(codec.cstream, 0 #=unknown source size=#)
function TranscodingStreams.startproc(codec::ZstdCompressor, mode::Symbol, error::Error)::Symbol
code = reset!(codec.cstream)
if iserror(code)
error[] = ErrorException("zstd error")
return :error
error[] = ErrorException("zstd error resetting compression context")
:error

Check warning on line 108 in src/compression.jl

View check run for this annotation

Codecov / codecov/patch

src/compression.jl#L107-L108

Added lines #L107 - L108 were not covered by tests
else
:ok
end
end

if isdefined(TranscodingStreams, :pledgeinsize)
function TranscodingStreams.pledgeinsize(codec::ZstdCompressor, insize::Int64, error::Error)::Symbol
srcsize = if signbit(insize)
ZSTD_CONTENTSIZE_UNKNOWN

Check warning on line 117 in src/compression.jl

View check run for this annotation

Codecov / codecov/patch

src/compression.jl#L115-L117

Added lines #L115 - L117 were not covered by tests
else
Culonglong(insize)

Check warning on line 119 in src/compression.jl

View check run for this annotation

Codecov / codecov/patch

src/compression.jl#L119

Added line #L119 was not covered by tests
end
code = LibZstd.ZSTD_CCtx_setPledgedSrcSize(codec.cstream, srcsize)
if iserror(code)
error[] = ErrorException("zstd error setting pledged source size")
:error

Check warning on line 124 in src/compression.jl

View check run for this annotation

Codecov / codecov/patch

src/compression.jl#L121-L124

Added lines #L121 - L124 were not covered by tests
else
:ok

Check warning on line 126 in src/compression.jl

View check run for this annotation

Codecov / codecov/patch

src/compression.jl#L126

Added line #L126 was not covered by tests
end
end
return :ok
end

function TranscodingStreams.process(codec::ZstdCompressor, input::Memory, output::Memory, error::Error)
Expand Down
16 changes: 2 additions & 14 deletions src/libzstd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,11 @@ function initialize!(cstream::CStream, level::Integer)
return LibZstd.ZSTD_initCStream(cstream, level)
end

function reset!(cstream::CStream, srcsize::Integer)
# ZSTD_resetCStream is deprecated
# https://github.com/facebook/zstd/blob/9d2a45a705e22ad4817b41442949cd0f78597154/lib/zstd.h#L2253-L2272
function reset!(cstream::CStream)
res = LibZstd.ZSTD_CCtx_reset(cstream, LibZstd.ZSTD_reset_session_only)
if iserror(res)
return res
end
if srcsize == 0
# From zstd.h:
# Note: ZSTD_resetCStream() interprets pledgedSrcSize == 0 as ZSTD_CONTENTSIZE_UNKNOWN, but
# ZSTD_CCtx_setPledgedSrcSize() does not do the same, so ZSTD_CONTENTSIZE_UNKNOWN must be
# explicitly specified.
srcsize = ZSTD_CONTENTSIZE_UNKNOWN
end
reset!(cstream.ibuffer)
reset!(cstream.obuffer)
return LibZstd.ZSTD_CCtx_setPledgedSrcSize(cstream, srcsize)
return res
end

"""
Expand Down

0 comments on commit 11cff42

Please sign in to comment.