-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathencode.jl
More file actions
229 lines (204 loc) · 7.65 KB
/
encode.jl
File metadata and controls
229 lines (204 loc) · 7.65 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
const level_docs = """
- `level::Integer=-1`: The compression level must be -1, or between 0 and 9.
1 gives best speed, 9 gives best compression, 0 gives no compression at all
(the input data is simply copied a block at a time). -1
requests a default compromise between speed and compression (currently
equivalent to level 6).
"""
const strategy_docs = """
- `strategy::Integer=$(Z_DEFAULT_STRATEGY)`: The compression strategy must be between $(Z_DEFAULT_STRATEGY) and $(Z_FIXED).
The strategy parameter is used to tune the compression algorithm. It only
affects the compression ratio but not the correctness of the compressed
output even if it is not set appropriately.
- $(Z_DEFAULT_STRATEGY) (`Z_DEFAULT_STRATEGY`) is used for normal data.
- $(Z_FILTERED) (`Z_FILTERED`) is used for data produced by a filter (or predictor).
Filtered data consists mostly of small values with a somewhat random
distribution. In this case, the compression algorithm is tuned to compress
them better. The effect of `Z_FILTERED` is to force more Huffman coding
and less string matching; it is somewhat intermediate between
`Z_DEFAULT_STRATEGY` and `Z_HUFFMAN_ONLY`.
- $(Z_HUFFMAN_ONLY) (`Z_HUFFMAN_ONLY`) forces Huffman encoding only (no string match).
- $(Z_RLE) (`Z_RLE`) limits match distances to one (run-length encoding). `Z_RLE`
is designed to be almost as fast as `Z_HUFFMAN_ONLY`, but gives better
compression for PNG image data.
- $(Z_FIXED) (`Z_FIXED`) prevents the use of dynamic Huffman codes, allowing for a
simpler decoder for special applications.
"""
"""
struct ZlibEncodeOptions <: EncodeOptions
ZlibEncodeOptions(; kwargs...)
zlib compression using libzlib: https://www.zlib.net/
This is the zlib format described in RFC 1950
# Keyword Arguments
- `codec::ZlibCodec=ZlibCodec()`
$(level_docs)
$(strategy_docs)
"""
struct ZlibEncodeOptions <: EncodeOptions
codec::ZlibCodec
level::Int32
strategy::Int32
end
function ZlibEncodeOptions(;
codec::ZlibCodec=ZlibCodec(),
level::Integer=-1,
strategy::Integer=Z_DEFAULT_STRATEGY,
kwargs...
)
check_in_range(Z_DEFAULT_STRATEGY:Z_FIXED; strategy)
ZlibEncodeOptions(
codec,
Int32(clamp(level, -1, 9)),
Int32(strategy),
)
end
"""
struct DeflateEncodeOptions <: EncodeOptions
DeflateEncodeOptions(; kwargs...)
deflate compression using libzlib: https://www.zlib.net/
This is the deflate format described in RFC 1951
# Keyword Arguments
- `codec::DeflateCodec=DeflateCodec()`
$(level_docs)
$(strategy_docs)
"""
struct DeflateEncodeOptions <: EncodeOptions
codec::DeflateCodec
level::Int32
strategy::Int32
end
function DeflateEncodeOptions(;
codec::DeflateCodec=DeflateCodec(),
level::Integer=-1,
strategy::Integer=Z_DEFAULT_STRATEGY,
kwargs...
)
check_in_range(Z_DEFAULT_STRATEGY:Z_FIXED; strategy)
DeflateEncodeOptions(
codec,
Int32(clamp(level, -1, 9)),
Int32(strategy),
)
end
"""
struct GzipEncodeOptions <: EncodeOptions
GzipEncodeOptions(; kwargs...)
gzip compression using libzlib: https://www.zlib.net/
This is the gzip (.gz) format described in RFC 1952
# Keyword Arguments
- `codec::GzipCodec=GzipCodec()`
$(level_docs)
$(strategy_docs)
"""
struct GzipEncodeOptions <: EncodeOptions
codec::GzipCodec
level::Int32
strategy::Int32
end
function GzipEncodeOptions(;
codec::GzipCodec=GzipCodec(),
level::Integer=-1,
strategy::Integer=Z_DEFAULT_STRATEGY,
kwargs...
)
check_in_range(Z_DEFAULT_STRATEGY:Z_FIXED; strategy)
GzipEncodeOptions(
codec,
Int32(clamp(level, -1, 9)),
Int32(strategy),
)
end
const _AllEncodeOptions = Union{ZlibEncodeOptions, DeflateEncodeOptions, GzipEncodeOptions}
is_thread_safe(::_AllEncodeOptions) = true
# Modified from the deflateBound function in zlib/deflate.c
# Since the default gzip header, windowBits, and memLevel are always
# used in this package, the code can be simplified.
# If alternate settings are used this must be modified.
function encode_bound(e::_AllEncodeOptions, src_size::Int64)::Int64
wraplen = _wraplen(e)
clamp(
widen(src_size) +
widen(src_size>>12 + src_size>>14 + src_size>>25 + Int64(7) + wraplen),
Int64,
)
end
_wraplen(::ZlibEncodeOptions) = Int64(6)
_wraplen(::DeflateEncodeOptions) = Int64(0)
_wraplen(::GzipEncodeOptions) = Int64(18)
# max to prevent overflows in encode_bound
# From ChunkCodecTests.find_max_decoded_size(::EncodeOptions)
_max_decoded_size(::ZlibEncodeOptions)::Int64 = 0x7ff60087fa602c72
_max_decoded_size(::DeflateEncodeOptions)::Int64 = 0x7ff60087fa602c78
_max_decoded_size(::GzipEncodeOptions)::Int64 = 0x7ff60087fa602c66
decoded_size_range(e::_AllEncodeOptions) = Int64(0):Int64(1):_max_decoded_size(e)
function try_encode!(e::_AllEncodeOptions, dst::AbstractVector{UInt8}, src::AbstractVector{UInt8}; kwargs...)::MaybeSize
# -15: deflate, 15: zlib, 15+16: gzip
# smaller windowBits might break encode bound
windowBits = _windowBits(e.codec)
@assert windowBits ∈ (-15, 15, 15+16)
check_contiguous(dst)
check_contiguous(src)
src_size::Int64 = length(src)
dst_size::Int64 = length(dst)
check_in_range(decoded_size_range(e); src_size)
if iszero(dst_size)
return NOT_SIZE
end
stream = ZStream()
deflateInit2(stream, e.level, windowBits, e.strategy)
try
# deflate loop
cconv_src = Base.cconvert(Ptr{UInt8}, src)
cconv_dst = Base.cconvert(Ptr{UInt8}, dst)
GC.@preserve cconv_src cconv_dst begin
src_p = Base.unsafe_convert(Ptr{UInt8}, cconv_src)
dst_p = Base.unsafe_convert(Ptr{UInt8}, cconv_dst)
stream.next_in = src_p
stream.next_out = dst_p
src_left::Int64 = src_size
dst_left::Int64 = dst_size
while true
start_avail_in = clamp(src_left, Cuint)
start_avail_out = clamp(dst_left, Cuint)
stream.avail_in = start_avail_in
stream.avail_out = start_avail_out
@assert !iszero(stream.avail_out)
action = if stream.avail_in == src_left
Z_FINISH
else
Z_NO_FLUSH
end
ret = ccall(
(:deflate, libz),
Cint,
(Ref{ZStream}, Cint),
stream, action,
)
@assert ret != Z_STREAM_ERROR # state not clobbered
@assert stream.avail_in ≤ start_avail_in
@assert stream.avail_out ≤ start_avail_out
# there must be progress
@assert stream.avail_in < start_avail_in || stream.avail_out < start_avail_out
src_left -= start_avail_in - stream.avail_in
dst_left -= start_avail_out - stream.avail_out
@assert src_left ∈ 0:src_size
@assert dst_left ∈ 0:dst_size
@assert stream.next_in == src_p + (src_size - src_left)
@assert stream.next_out == dst_p + (dst_size - dst_left)
if ret == Z_STREAM_END
# yay done just return compressed size
@assert dst_left ∈ 0:dst_size
@assert iszero(src_left)
return dst_size - dst_left
end
if iszero(dst_left)
# no more space, but not Z_STREAM_END
return NOT_SIZE
end
@assert ret == Z_OK
end
end
finally
deflateEnd(stream)
end
end