|
| 1 | +""" |
| 2 | + Blosc2DecodingError() |
| 3 | +
|
| 4 | +Error for data that cannot be decoded. |
| 5 | +""" |
| 6 | +struct Blosc2DecodingError <: DecodingError |
| 7 | +end |
| 8 | + |
| 9 | +function Base.showerror(io::IO, err::Blosc2DecodingError) |
| 10 | + print(io, "Blosc2DecodingError: blosc2 compressed buffer cannot be decoded") |
| 11 | + return nothing |
| 12 | +end |
| 13 | + |
| 14 | +""" |
| 15 | + struct Blosc2DecodeOptions <: DecodeOptions |
| 16 | + Blosc2DecodeOptions(; kwargs...) |
| 17 | +
|
| 18 | +Blosc2 decompression using c-blosc2 library: https://github.com/Blosc/c-blosc2 |
| 19 | +
|
| 20 | +# Keyword Arguments |
| 21 | +
|
| 22 | +- `codec::Blosc2Codec=Blosc2Codec()` |
| 23 | +""" |
| 24 | +struct Blosc2DecodeOptions <: DecodeOptions |
| 25 | + codec::Blosc2Codec |
| 26 | +end |
| 27 | +Blosc2DecodeOptions(; codec::Blosc2Codec=Blosc2Codec(), kwargs...) = Blosc2DecodeOptions(codec) |
| 28 | + |
| 29 | +function try_find_decoded_size(::Blosc2DecodeOptions, src::AbstractVector{UInt8})::Int64 |
| 30 | + check_contiguous(src) |
| 31 | + |
| 32 | + copy_cframe = false |
| 33 | + schunk = @ccall libblosc2.blosc2_schunk_from_buffer(src::Ptr{UInt8}, length(src)::Int64, copy_cframe::UInt8)::Ptr{Blosc2SChunk} |
| 34 | + if schunk == Ptr{Blosc2Storage}() |
| 35 | + # These are not a valid blosc2-encoded data |
| 36 | + throw(Blosc2DecodingError()) |
| 37 | + end |
| 38 | + @ccall libblosc2.blosc2_schunk_avoid_cframe_free(schunk::Ptr{Blosc2SChunk}, true::UInt8)::Cvoid |
| 39 | + |
| 40 | + total_nbytes = Int64(0) |
| 41 | + |
| 42 | + nchunks = unsafe_load(schunk).nchunks |
| 43 | + for nchunk in 0:(nchunks - 1) |
| 44 | + cbuffer = Ref{Ptr{UInt8}}() |
| 45 | + needs_free = Ref{UInt8}() |
| 46 | + chunksize = @ccall libblosc2.blosc2_schunk_get_chunk(schunk::Ptr{Blosc2SChunk}, nchunk::Int64, cbuffer::Ref{Ptr{UInt8}}, |
| 47 | + needs_free::Ref{UInt8})::Cint |
| 48 | + @assert chunksize > 0 |
| 49 | + cbuffer = cbuffer[] |
| 50 | + needs_free = Bool(needs_free[]) |
| 51 | + |
| 52 | + nbytes = Ref{Int32}() |
| 53 | + success = @ccall libblosc2.blosc1_cbuffer_validate(cbuffer::Ptr{Cvoid}, chunksize::Cint, nbytes::Ref{Cint})::Cint |
| 54 | + @assert success == 0 |
| 55 | + nbytes = nbytes[] |
| 56 | + |
| 57 | + total_nbytes += nbytes |
| 58 | + |
| 59 | + if needs_free |
| 60 | + # We could provide buffer into which to decode instead, reusing that buffer |
| 61 | + Libc.free(cbuffer) |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + # TODO: Use this instead of the loop above |
| 66 | + @assert unsafe_load(schunk).nbytes == total_nbytes |
| 67 | + |
| 68 | + success = @ccall libblosc2.blosc2_schunk_free(schunk::Ptr{Cvoid})::Cint |
| 69 | + @assert success == 0 |
| 70 | + |
| 71 | + return total_nbytes::Int64 |
| 72 | +end |
| 73 | + |
| 74 | +#TODO: implement `try_resize_decode!` |
| 75 | + |
| 76 | +function try_decode!(d::Blosc2DecodeOptions, dst::AbstractVector{UInt8}, src::AbstractVector{UInt8}; |
| 77 | + kwargs...)::Union{Nothing,Int64} |
| 78 | + check_contiguous(dst) |
| 79 | + check_contiguous(src) |
| 80 | + |
| 81 | + schunk = @ccall libblosc2.blosc2_schunk_from_buffer(src::Ptr{UInt8}, length(src)::Int64, false::UInt8)::Ptr{Blosc2SChunk} |
| 82 | + @assert schunk != Ptr{Blosc2Storage}() |
| 83 | + @ccall libblosc2.blosc2_schunk_avoid_cframe_free(schunk::Ptr{Blosc2SChunk}, true::UInt8)::Cvoid |
| 84 | + |
| 85 | + there_was_an_error = false |
| 86 | + total_nbytes = Int64(0) |
| 87 | + |
| 88 | + nchunks = unsafe_load(schunk).nchunks |
| 89 | + for nchunk in 0:(nchunks - 1) |
| 90 | + cbuffer = Ref{Ptr{UInt8}}() |
| 91 | + needs_free = Ref{UInt8}() |
| 92 | + chunksize = @ccall libblosc2.blosc2_schunk_get_chunk(schunk::Ptr{Blosc2SChunk}, nchunk::Int64, cbuffer::Ref{Ptr{UInt8}}, |
| 93 | + needs_free::Ref{UInt8})::Cint |
| 94 | + @assert chunksize > 0 |
| 95 | + cbuffer = cbuffer[] |
| 96 | + needs_free = Bool(needs_free[]) |
| 97 | + |
| 98 | + nbytes = Ref{Int32}() |
| 99 | + success = @ccall libblosc2.blosc1_cbuffer_validate(cbuffer::Ptr{Cvoid}, chunksize::Cint, nbytes::Ref{Cint})::Cint |
| 100 | + @assert success == 0 |
| 101 | + nbytes = nbytes[] |
| 102 | + |
| 103 | + if needs_free |
| 104 | + Libc.free(cbuffer) |
| 105 | + end |
| 106 | + |
| 107 | + # TODO: Use this instead of checking each chunk |
| 108 | + @assert unsafe_load(schunk).nbytes == nbytes |
| 109 | + |
| 110 | + if total_nbytes + nbytes > length(dst) |
| 111 | + there_was_an_error = true |
| 112 | + break |
| 113 | + end |
| 114 | + |
| 115 | + @assert total_nbytes + nbytes <= length(dst) |
| 116 | + nbytes′ = @ccall libblosc2.blosc2_schunk_decompress_chunk(schunk::Ptr{Blosc2SChunk}, nchunk::Int64, |
| 117 | + pointer(dst, total_nbytes+1)::Ptr{Cvoid}, nbytes::Int32)::Cint |
| 118 | + @assert nbytes′ >= 0 |
| 119 | + @assert nbytes′ == nbytes |
| 120 | + |
| 121 | + total_nbytes += nbytes |
| 122 | + end |
| 123 | + |
| 124 | + success = @ccall libblosc2.blosc2_schunk_free(schunk::Ptr{Cvoid})::Cint |
| 125 | + @assert success == 0 |
| 126 | + |
| 127 | + if there_was_an_error |
| 128 | + return nothing |
| 129 | + end |
| 130 | + |
| 131 | + return total_nbytes::Int64 |
| 132 | +end |
0 commit comments