-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathdecompression.jl
174 lines (138 loc) · 4.49 KB
/
decompression.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# Decompressor Codecs
# ====================
abstract type DecompressorCodec <: TranscodingStreams.Codec end
function Base.show(io::IO, codec::DecompressorCodec)
print(io, summary(codec), "(windowbits=$(codec.windowbits))")
end
# Gzip
# ----
struct GzipDecompressor <: DecompressorCodec
zstream::ZStream
windowbits::Int
end
"""
GzipDecompressor(;windowbits=$(Z_DEFAULT_WINDOWBITS), gziponly=false)
Create a gzip decompressor codec.
If `gziponly` is `false`, this codec can decompress the zlib format as well.
Arguments
---------
- `windowbits`: size of history buffer (8..15)
- `gziponly`: flag to inactivate data format detection
"""
function GzipDecompressor(;windowbits::Integer=Z_DEFAULT_WINDOWBITS, gziponly::Bool=false)
if !(8 ≤ windowbits ≤ 15)
throw(ArgumentError("windowbits must be within 8..15"))
end
return GzipDecompressor(ZStream(), windowbits+(gziponly ? 16 : 32))
end
const GzipDecompressorStream{S} = TranscodingStream{GzipDecompressor,S} where S<:IO
"""
GzipDecompressorStream(stream::IO; kwargs...)
Create a gzip decompression stream (see `GzipDecompressor` for `kwargs`).
"""
function GzipDecompressorStream(stream::IO; kwargs...)
x, y = splitkwargs(kwargs, (:windowbits, :gziponly))
return TranscodingStream(GzipDecompressor(;x...), stream; y...)
end
# Zlib
# ----
struct ZlibDecompressor <: DecompressorCodec
zstream::ZStream
windowbits::Int
end
"""
ZlibDecompressor(;windowbits=$(Z_DEFAULT_WINDOWBITS))
Create a zlib decompression codec.
Arguments
---------
- `windowbits`: size of history buffer (8..15)
"""
function ZlibDecompressor(;windowbits::Integer=Z_DEFAULT_WINDOWBITS)
if !(8 ≤ windowbits ≤ 15)
throw(ArgumentError("windowbits must be within 8..15"))
end
return ZlibDecompressor(ZStream(), windowbits)
end
const ZlibDecompressorStream{S} = TranscodingStream{ZlibDecompressor,S} where S<:IO
"""
ZlibDecompressorStream(stream::IO; kwargs...)
Create a deflate decompression stream (see `ZlibDecompressor` for `kwargs`).
"""
function ZlibDecompressorStream(stream::IO; kwargs...)
x, y = splitkwargs(kwargs, (:windowbits,))
return TranscodingStream(ZlibDecompressor(;x...), stream; y...)
end
# Deflate
# -------
struct DeflateDecompressor <: DecompressorCodec
zstream::ZStream
windowbits::Int
end
"""
DeflateDecompressor(;windowbits=$(Z_DEFAULT_WINDOWBITS))
Create a deflate decompression codec.
Arguments
---------
- `windowbits`: size of history buffer (8..15)
"""
function DeflateDecompressor(;windowbits::Integer=Z_DEFAULT_WINDOWBITS)
if !(8 ≤ windowbits ≤ 15)
throw(ArgumentError("windowbits must be within 8..15"))
end
return DeflateDecompressor(ZStream(), -Int(windowbits))
end
const DeflateDecompressorStream{S} = TranscodingStream{DeflateDecompressor,S} where S<:IO
"""
DeflateDecompressorStream(stream::IO; kwargs...)
Create a deflate decompression stream (see `DeflateDecompressor` for `kwargs`).
"""
function DeflateDecompressorStream(stream::IO; kwargs...)
x, y = splitkwargs(kwargs, (:windowbits,))
return TranscodingStream(DeflateDecompressor(;x...), stream; y...)
end
# Methods
# -------
function TranscodingStreams.initialize(codec::DecompressorCodec)
code = inflate_init!(codec.zstream, codec.windowbits)
if code != Z_OK
zerror(codec.zstream, code)
end
return
end
function TranscodingStreams.finalize(codec::DecompressorCodec)
zstream = codec.zstream
if zstream.state != C_NULL
code = inflate_end!(zstream)
if code != Z_OK
zerror(zstream, code)
end
end
return
end
function TranscodingStreams.startproc(codec::DecompressorCodec, ::Symbol, error::Error)
code = inflate_reset!(codec.zstream)
if code == Z_OK
return :ok
else
error[] = ErrorException(zlib_error_message(codec.zstream, code))
return :error
end
end
function TranscodingStreams.process(codec::DecompressorCodec, input::Memory, output::Memory, error::Error)
zstream = codec.zstream
zstream.next_in = input.ptr
zstream.avail_in = input.size
zstream.next_out = output.ptr
zstream.avail_out = output.size
code = inflate!(zstream, Z_NO_FLUSH)
Δin = Int(input.size - zstream.avail_in)
Δout = Int(output.size - zstream.avail_out)
if code == Z_OK
return Δin, Δout, :ok
elseif code == Z_STREAM_END
return Δin, Δout, :end
else
error[] = ErrorException(zlib_error_message(zstream, code))
return Δin, Δout, :error
end
end