Skip to content

Commit a02f4c8

Browse files
authored
define expectedsize for decompressor (#14)
1 parent 269b08a commit a02f4c8

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/decompression.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,17 @@ function TranscodingStreams.process(codec::ZstdDecompressor, input::Memory, outp
7979
return Δin, Δout, code == 0 ? :end : :ok
8080
end
8181
end
82+
83+
function TranscodingStreams.expectedsize(codec::ZstdDecompressor, input::Memory)
84+
ret = find_decompressed_size(input.ptr, input.size)
85+
if ret == ZSTD_CONTENTSIZE_ERROR
86+
# something is bad, but we ignore it here
87+
return Int(input.size)
88+
elseif ret == ZSTD_CONTENTSIZE_UNKNOWN
89+
# random guess
90+
return Int(input.size * 2)
91+
else
92+
# exact size
93+
return Int(ret)
94+
end
95+
end

src/libzstd.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,14 @@ end
103103
function free!(dstream::DStream)
104104
return ccall((:ZSTD_freeDStream, libzstd), Csize_t, (Ptr{Cvoid},), dstream.ptr)
105105
end
106+
107+
108+
# Misc. functions
109+
# ---------------
110+
111+
const ZSTD_CONTENTSIZE_UNKNOWN = Culonglong(0) - 1
112+
const ZSTD_CONTENTSIZE_ERROR = Culonglong(0) - 2
113+
114+
function find_decompressed_size(src::Ptr, size::Integer)
115+
return ccall((:ZSTD_findDecompressedSize, libzstd), Culonglong, (Ptr{Cvoid}, Csize_t), src, size)
116+
end

0 commit comments

Comments
 (0)