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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
|
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/util/compression_internal.h"
#include <algorithm>
#include <cstdint>
#include <cstring>
#include <limits>
#include <memory>
#include <zconf.h>
#include <zlib.h>
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/result.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/status.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/util/logging.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/util/macros.h"
namespace arrow20 {
namespace util {
namespace internal {
namespace {
// ----------------------------------------------------------------------
// gzip implementation
// These are magic numbers from zlib.h. Not clear why they are not defined
// there.
// Maximum window size
constexpr int kGZipMaxWindowBits = 15;
// Minimum window size
constexpr int kGZipMinWindowBits = 9;
// Default window size
constexpr int kGZipDefaultWindowBits = 15;
// Output Gzip.
constexpr int GZIP_CODEC = 16;
// Determine if this is libz or gzip from header.
constexpr int DETECT_CODEC = 32;
constexpr int kGZipMinCompressionLevel = 1;
constexpr int kGZipMaxCompressionLevel = 9;
int CompressionWindowBitsForFormat(GZipFormat format, int window_bits) {
switch (format) {
case GZipFormat::DEFLATE:
window_bits = -window_bits;
break;
case GZipFormat::GZIP:
window_bits += GZIP_CODEC;
break;
case GZipFormat::ZLIB:
break;
}
return window_bits;
}
int DecompressionWindowBitsForFormat(GZipFormat format, int window_bits) {
if (format == GZipFormat::DEFLATE) {
return -window_bits;
} else {
/* If not deflate, autodetect format from header */
return window_bits | DETECT_CODEC;
}
}
Status ZlibErrorPrefix(const char* prefix_msg, const char* msg) {
return Status::IOError(prefix_msg, (msg) ? msg : "(unknown error)");
}
// ----------------------------------------------------------------------
// gzip decompressor implementation
class GZipDecompressor : public Decompressor {
public:
explicit GZipDecompressor(GZipFormat format, int window_bits)
: format_(format),
window_bits_(window_bits),
initialized_(false),
finished_(false) {}
~GZipDecompressor() override {
if (initialized_) {
inflateEnd(&stream_);
}
}
Status Init() {
DCHECK(!initialized_);
memset(&stream_, 0, sizeof(stream_));
finished_ = false;
int ret;
int window_bits = DecompressionWindowBitsForFormat(format_, window_bits_);
if ((ret = inflateInit2(&stream_, window_bits)) != Z_OK) {
return ZlibError("zlib inflateInit failed: ");
} else {
initialized_ = true;
return Status::OK();
}
}
Status Reset() override {
DCHECK(initialized_);
finished_ = false;
int ret;
if ((ret = inflateReset(&stream_)) != Z_OK) {
return ZlibError("zlib inflateReset failed: ");
} else {
return Status::OK();
}
}
Result<DecompressResult> Decompress(int64_t input_len, const uint8_t* input,
int64_t output_len, uint8_t* output) override {
static constexpr auto input_limit =
static_cast<int64_t>(std::numeric_limits<uInt>::max());
stream_.next_in = const_cast<Bytef*>(reinterpret_cast<const Bytef*>(input));
stream_.avail_in = static_cast<uInt>(std::min(input_len, input_limit));
stream_.next_out = reinterpret_cast<Bytef*>(output);
stream_.avail_out = static_cast<uInt>(std::min(output_len, input_limit));
int ret;
ret = inflate(&stream_, Z_SYNC_FLUSH);
if (ret == Z_DATA_ERROR || ret == Z_STREAM_ERROR || ret == Z_MEM_ERROR) {
return ZlibError("zlib inflate failed: ");
}
if (ret == Z_NEED_DICT) {
return ZlibError("zlib inflate failed (need preset dictionary): ");
}
finished_ = (ret == Z_STREAM_END);
if (ret == Z_BUF_ERROR) {
// No progress was possible
return DecompressResult{0, 0, true};
} else {
ARROW_CHECK(ret == Z_OK || ret == Z_STREAM_END);
// Some progress has been made
return DecompressResult{input_len - stream_.avail_in,
output_len - stream_.avail_out, false};
}
return Status::OK();
}
bool IsFinished() override { return finished_; }
protected:
Status ZlibError(const char* prefix_msg) {
return ZlibErrorPrefix(prefix_msg, stream_.msg);
}
z_stream stream_;
GZipFormat format_;
int window_bits_;
bool initialized_;
bool finished_;
};
// ----------------------------------------------------------------------
// gzip compressor implementation
class GZipCompressor : public Compressor {
public:
explicit GZipCompressor(int compression_level)
: initialized_(false), compression_level_(compression_level) {}
~GZipCompressor() override {
if (initialized_) {
deflateEnd(&stream_);
}
}
Status Init(GZipFormat format, int input_window_bits) {
DCHECK(!initialized_);
memset(&stream_, 0, sizeof(stream_));
int ret;
// Initialize to run specified format
int window_bits = CompressionWindowBitsForFormat(format, input_window_bits);
if ((ret = deflateInit2(&stream_, Z_DEFAULT_COMPRESSION, Z_DEFLATED, window_bits,
compression_level_, Z_DEFAULT_STRATEGY)) != Z_OK) {
return ZlibError("zlib deflateInit failed: ");
} else {
initialized_ = true;
return Status::OK();
}
}
Result<CompressResult> Compress(int64_t input_len, const uint8_t* input,
int64_t output_len, uint8_t* output) override {
DCHECK(initialized_) << "Called on non-initialized stream";
static constexpr auto input_limit =
static_cast<int64_t>(std::numeric_limits<uInt>::max());
stream_.next_in = const_cast<Bytef*>(reinterpret_cast<const Bytef*>(input));
stream_.avail_in = static_cast<uInt>(std::min(input_len, input_limit));
stream_.next_out = reinterpret_cast<Bytef*>(output);
stream_.avail_out = static_cast<uInt>(std::min(output_len, input_limit));
int64_t ret = 0;
ret = deflate(&stream_, Z_NO_FLUSH);
if (ret == Z_STREAM_ERROR) {
return ZlibError("zlib compress failed: ");
}
if (ret == Z_OK) {
// Some progress has been made
return CompressResult{input_len - stream_.avail_in, output_len - stream_.avail_out};
} else {
// No progress was possible
ARROW_CHECK_EQ(ret, Z_BUF_ERROR);
return CompressResult{0, 0};
}
}
Result<FlushResult> Flush(int64_t output_len, uint8_t* output) override {
DCHECK(initialized_) << "Called on non-initialized stream";
static constexpr auto input_limit =
static_cast<int64_t>(std::numeric_limits<uInt>::max());
stream_.avail_in = 0;
stream_.next_out = reinterpret_cast<Bytef*>(output);
stream_.avail_out = static_cast<uInt>(std::min(output_len, input_limit));
int64_t ret = 0;
ret = deflate(&stream_, Z_SYNC_FLUSH);
if (ret == Z_STREAM_ERROR) {
return ZlibError("zlib flush failed: ");
}
int64_t bytes_written;
if (ret == Z_OK) {
bytes_written = output_len - stream_.avail_out;
} else {
ARROW_CHECK_EQ(ret, Z_BUF_ERROR);
bytes_written = 0;
}
// "If deflate returns with avail_out == 0, this function must be called
// again with the same value of the flush parameter and more output space
// (updated avail_out), until the flush is complete (deflate returns
// with non-zero avail_out)."
// "Note that Z_BUF_ERROR is not fatal, and deflate() can be called again
// with more input and more output space to continue compressing."
return FlushResult{bytes_written, stream_.avail_out == 0};
}
Result<EndResult> End(int64_t output_len, uint8_t* output) override {
DCHECK(initialized_) << "Called on non-initialized stream";
static constexpr auto input_limit =
static_cast<int64_t>(std::numeric_limits<uInt>::max());
stream_.avail_in = 0;
stream_.next_out = reinterpret_cast<Bytef*>(output);
stream_.avail_out = static_cast<uInt>(std::min(output_len, input_limit));
int64_t ret = 0;
ret = deflate(&stream_, Z_FINISH);
if (ret == Z_STREAM_ERROR) {
return ZlibError("zlib flush failed: ");
}
int64_t bytes_written = output_len - stream_.avail_out;
if (ret == Z_STREAM_END) {
// Flush complete, we can now end the stream
initialized_ = false;
ret = deflateEnd(&stream_);
if (ret == Z_OK) {
return EndResult{bytes_written, false};
} else {
return ZlibError("zlib end failed: ");
}
} else {
// Not everything could be flushed,
return EndResult{bytes_written, true};
}
}
protected:
Status ZlibError(const char* prefix_msg) {
return ZlibErrorPrefix(prefix_msg, stream_.msg);
}
z_stream stream_;
bool initialized_;
int compression_level_;
};
// ----------------------------------------------------------------------
// gzip codec implementation
class GZipCodec : public Codec {
public:
explicit GZipCodec(int compression_level, GZipFormat format, int window_bits)
: format_(format),
window_bits_(window_bits),
compressor_initialized_(false),
decompressor_initialized_(false) {
compression_level_ = compression_level == kUseDefaultCompressionLevel
? kGZipDefaultCompressionLevel
: compression_level;
}
~GZipCodec() override {
EndCompressor();
EndDecompressor();
}
Result<std::shared_ptr<Compressor>> MakeCompressor() override {
auto ptr = std::make_shared<GZipCompressor>(compression_level_);
RETURN_NOT_OK(ptr->Init(format_, window_bits_));
return ptr;
}
Result<std::shared_ptr<Decompressor>> MakeDecompressor() override {
auto ptr = std::make_shared<GZipDecompressor>(format_, window_bits_);
RETURN_NOT_OK(ptr->Init());
return ptr;
}
Status InitCompressor() {
EndDecompressor();
memset(&stream_, 0, sizeof(stream_));
int ret;
// Initialize to run specified format
int window_bits = CompressionWindowBitsForFormat(format_, window_bits_);
if ((ret = deflateInit2(&stream_, Z_DEFAULT_COMPRESSION, Z_DEFLATED, window_bits,
compression_level_, Z_DEFAULT_STRATEGY)) != Z_OK) {
return ZlibErrorPrefix("zlib deflateInit failed: ", stream_.msg);
}
compressor_initialized_ = true;
return Status::OK();
}
void EndCompressor() {
if (compressor_initialized_) {
(void)deflateEnd(&stream_);
}
compressor_initialized_ = false;
}
Status InitDecompressor() {
EndCompressor();
memset(&stream_, 0, sizeof(stream_));
int ret;
// Initialize to run either deflate or zlib/gzip format
int window_bits = DecompressionWindowBitsForFormat(format_, window_bits_);
if ((ret = inflateInit2(&stream_, window_bits)) != Z_OK) {
return ZlibErrorPrefix("zlib inflateInit failed: ", stream_.msg);
}
decompressor_initialized_ = true;
return Status::OK();
}
void EndDecompressor() {
if (decompressor_initialized_) {
(void)inflateEnd(&stream_);
}
decompressor_initialized_ = false;
}
Result<int64_t> Decompress(int64_t input_length, const uint8_t* input,
int64_t output_buffer_length, uint8_t* output) override {
int64_t read_input_bytes = 0;
int64_t decompressed_bytes = 0;
if (!decompressor_initialized_) {
RETURN_NOT_OK(InitDecompressor());
}
if (output_buffer_length == 0) {
// The zlib library does not allow *output to be NULL, even when
// output_buffer_length is 0 (inflate() will return Z_STREAM_ERROR). We don't
// consider this an error, so bail early if no output is expected. Note that we
// don't signal an error if the input actually contains compressed data.
return 0;
}
// inflate() will not automatically decode concatenated gzip members, keep calling
// inflate until reading all input data (GH-38271).
while (read_input_bytes < input_length) {
// Reset the stream for this block
if (inflateReset(&stream_) != Z_OK) {
return ZlibErrorPrefix("zlib inflateReset failed: ", stream_.msg);
}
int ret = 0;
// gzip can run in streaming mode or non-streaming mode. We only
// support the non-streaming use case where we present it the entire
// compressed input and a buffer big enough to contain the entire
// compressed output. In the case where we don't know the output,
// we just make a bigger buffer and try the non-streaming mode
// from the beginning again.
stream_.next_in =
const_cast<Bytef*>(reinterpret_cast<const Bytef*>(input + read_input_bytes));
stream_.avail_in = static_cast<uInt>(input_length - read_input_bytes);
stream_.next_out = reinterpret_cast<Bytef*>(output + decompressed_bytes);
stream_.avail_out = static_cast<uInt>(output_buffer_length - decompressed_bytes);
// We know the output size. In this case, we can use Z_FINISH
// which is more efficient.
ret = inflate(&stream_, Z_FINISH);
if (ret == Z_OK) {
// Failure, buffer was too small
return Status::IOError("Too small a buffer passed to GZipCodec. InputLength=",
input_length, " OutputLength=", output_buffer_length);
}
// Failure for some other reason
if (ret != Z_STREAM_END) {
return ZlibErrorPrefix("GZipCodec failed: ", stream_.msg);
}
read_input_bytes += stream_.total_in;
decompressed_bytes += stream_.total_out;
}
return decompressed_bytes;
}
int64_t MaxCompressedLen(int64_t input_length,
const uint8_t* ARROW_ARG_UNUSED(input)) override {
// Must be in compression mode
if (!compressor_initialized_) {
Status s = InitCompressor();
ARROW_CHECK_OK(s);
}
int64_t max_len = deflateBound(&stream_, static_cast<uLong>(input_length));
// ARROW-3514: return a more pessimistic estimate to account for bugs
// in old zlib versions.
return max_len + 12;
}
Result<int64_t> Compress(int64_t input_length, const uint8_t* input,
int64_t output_buffer_len, uint8_t* output) override {
if (!compressor_initialized_) {
RETURN_NOT_OK(InitCompressor());
}
stream_.next_in = const_cast<Bytef*>(reinterpret_cast<const Bytef*>(input));
stream_.avail_in = static_cast<uInt>(input_length);
stream_.next_out = reinterpret_cast<Bytef*>(output);
stream_.avail_out = static_cast<uInt>(output_buffer_len);
int64_t ret = 0;
if ((ret = deflate(&stream_, Z_FINISH)) != Z_STREAM_END) {
if (ret == Z_OK) {
// Will return Z_OK (and stream.msg NOT set) if stream.avail_out is too
// small
return Status::IOError("zlib deflate failed, output buffer too small");
}
return ZlibErrorPrefix("zlib deflate failed: ", stream_.msg);
}
if (deflateReset(&stream_) != Z_OK) {
return ZlibErrorPrefix("zlib deflateReset failed: ", stream_.msg);
}
// Actual output length
return output_buffer_len - stream_.avail_out;
}
Status Init() override {
if (window_bits_ < kGZipMinWindowBits || window_bits_ > kGZipMaxWindowBits) {
return Status::Invalid("GZip window_bits should be between ", kGZipMinWindowBits,
" and ", kGZipMaxWindowBits);
}
const Status init_compressor_status = InitCompressor();
if (!init_compressor_status.ok()) {
return init_compressor_status;
}
return InitDecompressor();
}
Compression::type compression_type() const override { return Compression::GZIP; }
int compression_level() const override { return compression_level_; }
int minimum_compression_level() const override { return kGZipMinCompressionLevel; }
int maximum_compression_level() const override { return kGZipMaxCompressionLevel; }
int default_compression_level() const override { return kGZipDefaultCompressionLevel; }
private:
// zlib is stateful and the z_stream state variable must be initialized
// before
z_stream stream_;
// Realistically, this will always be GZIP, but we leave the option open to
// configure
GZipFormat format_;
// These variables are mutually exclusive. When the codec is in "compressor"
// state, compressor_initialized_ is true while decompressor_initialized_ is
// false. When it's decompressing, the opposite is true.
//
// Indeed, this is slightly hacky, but the alternative is having separate
// Compressor and Decompressor classes. If this ever becomes an issue, we can
// perform the refactoring then
int window_bits_;
bool compressor_initialized_;
bool decompressor_initialized_;
int compression_level_;
};
} // namespace
std::unique_ptr<Codec> MakeGZipCodec(int compression_level, GZipFormat format,
std::optional<int> window_bits) {
return std::make_unique<GZipCodec>(compression_level, format,
window_bits.value_or(kGZipDefaultWindowBits));
}
} // namespace internal
} // namespace util
} // namespace arrow20
|