-
Notifications
You must be signed in to change notification settings - Fork 26
Add support for the ChunkCodecCore interface #251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nhz2
wants to merge
5
commits into
master
Choose a base branch
from
nz/chunkcodecs
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,9 @@ license = "MIT" | |
authors = ["Kenta Sato <[email protected]>"] | ||
version = "0.11.3" | ||
|
||
[deps] | ||
ChunkCodecCore = "0b6fb165-00bc-4d37-ab8b-79f91016dbe1" | ||
|
||
[compat] | ||
ChunkCodecCore = "1" | ||
julia = "1.6" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
# Allow a `TranscodingStreams.Codec` to be used as a decoder. | ||
|
||
using ChunkCodecCore: | ||
check_in_range, | ||
check_contiguous, | ||
grow_dst!, | ||
MaybeSize, | ||
NOT_SIZE | ||
import ChunkCodecCore: | ||
try_decode!, | ||
try_resize_decode!, | ||
try_find_decoded_size | ||
|
||
# `Codec` subtypes may want to specialize `try_find_decoded_size` | ||
function try_find_decoded_size(::Codec, src::AbstractVector{UInt8})::Nothing | ||
nothing | ||
end | ||
|
||
function try_decode!(codec::Codec, dst::AbstractVector{UInt8}, src::AbstractVector{UInt8}; kwargs...)::MaybeSize | ||
try_resize_decode!(codec, dst, src, Int64(length(dst))) | ||
end | ||
|
||
function try_resize_decode!(codec::Codec, dst::AbstractVector{UInt8}, src::AbstractVector{UInt8}, max_size::Int64; kwargs...)::MaybeSize | ||
dst_size::Int64 = length(dst) | ||
src_size::Int64 = length(src) | ||
check_contiguous(dst) | ||
check_contiguous(src) | ||
cconv_src = Base.cconvert(Ptr{UInt8}, src) | ||
if dst_size < max_size | ||
# Do the equivalent of `_default_output_buffer(codec, input)` in `transcode` | ||
# by resizing `dst` | ||
expected_dst_size::Int64 = GC.@preserve(cconv_src, min(initial_output_size( | ||
codec, | ||
Memory(Base.unsafe_convert(Ptr{UInt8}, cconv_src), src_size), | ||
), max_size)) | ||
if expected_dst_size > dst_size | ||
resize!(dst, expected_dst_size) | ||
dst_size = expected_dst_size | ||
end | ||
end | ||
src_left::Int64 = src_size | ||
dst_left::Int64 = dst_size | ||
err = Error() | ||
# Outer loop to decode a concatenation of multiple compressed streams. | ||
while true | ||
if startproc(codec, :write, err) === :error | ||
@goto handle_error | ||
end | ||
if pledgeinsize(codec, src_left, err) === :error | ||
@goto handle_error | ||
end | ||
# The process loop | ||
while true | ||
GC.@preserve cconv_src begin | ||
local src_p = Base.unsafe_convert(Ptr{UInt8}, cconv_src) + (src_size - src_left) | ||
local input = Memory(src_p, src_left) | ||
# ensure minoutsize is provided | ||
local dst_space_needed = minoutsize(codec, input) | ||
while dst_space_needed > dst_left | ||
local next_size = grow_dst!(dst, max_size) | ||
if isnothing(next_size) | ||
break # reached max_size limit | ||
end | ||
dst_left += next_size - dst_size | ||
dst_size = next_size | ||
@assert dst_left > 0 | ||
end | ||
cconv_dst = Base.cconvert(Ptr{UInt8}, dst) | ||
GC.@preserve cconv_dst begin | ||
local dst_p = Base.unsafe_convert(Ptr{UInt8}, cconv_dst) + (dst_size - dst_left) | ||
if dst_space_needed > dst_left | ||
# Try to do the decoding into a scratch buffer | ||
# The scratch buffer should typically be just a few bytes. | ||
# This enables handling the `return NOT_SIZE` case | ||
# while respecting the `minoutsize` restrictions. | ||
local scratch_dst = zeros(UInt8, dst_space_needed) | ||
local cconv_scratch_dst = Base.cconvert(Ptr{UInt8}, scratch_dst) | ||
GC.@preserve cconv_scratch_dst let | ||
local scratch_p = Base.unsafe_convert(Ptr{UInt8}, cconv_scratch_dst) | ||
local output = Memory(scratch_p, dst_space_needed) | ||
local Δin, Δout, code = process(codec, input, output, err) | ||
if code === :error | ||
@goto handle_error | ||
end | ||
local valid_Δout = min(Δout, dst_left) | ||
src_left -= Δin | ||
unsafe_copyto!(dst_p, scratch_p, valid_Δout) | ||
if Δout > dst_left | ||
# Ran out of dst space | ||
return NOT_SIZE | ||
end | ||
dst_left -= Δout | ||
if code === :end | ||
if src_left > 0 | ||
break # out of the process loop to decode next stream | ||
else | ||
# Done | ||
return dst_size - dst_left | ||
end | ||
end | ||
end | ||
else | ||
local output = Memory(dst_p, dst_left) | ||
local Δin, Δout, code = process(codec, input, output, err) | ||
if code === :error | ||
@goto handle_error | ||
end | ||
src_left -= Δin | ||
dst_left -= Δout | ||
if code === :end | ||
if src_left > 0 | ||
break # out of the process loop to decode next stream | ||
else | ||
# Done | ||
return dst_size - dst_left | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
@label handle_error | ||
if !haserror(err) | ||
set_default_error!(err) | ||
end | ||
throw(err[]) | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.