2 * Copyright 2014 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <folly/io/Compression.h>
24 #include <glog/logging.h>
26 #if FOLLY_HAVE_LIBSNAPPY
28 #include <snappy-sinksource.h>
35 #if FOLLY_HAVE_LIBLZMA
39 #include <folly/Conv.h>
40 #include <folly/Memory.h>
41 #include <folly/Portability.h>
42 #include <folly/ScopeGuard.h>
43 #include <folly/Varint.h>
44 #include <folly/io/Cursor.h>
46 namespace folly { namespace io {
48 Codec::Codec(CodecType type) : type_(type) { }
50 // Ensure consistent behavior in the nullptr case
51 std::unique_ptr<IOBuf> Codec::compress(const IOBuf* data) {
52 uint64_t len = data->computeChainDataLength();
54 return IOBuf::create(0);
55 } else if (len > maxUncompressedLength()) {
56 throw std::runtime_error("Codec: uncompressed length too large");
59 return doCompress(data);
62 std::unique_ptr<IOBuf> Codec::uncompress(const IOBuf* data,
63 uint64_t uncompressedLength) {
64 if (uncompressedLength == UNKNOWN_UNCOMPRESSED_LENGTH) {
65 if (needsUncompressedLength()) {
66 throw std::invalid_argument("Codec: uncompressed length required");
68 } else if (uncompressedLength > maxUncompressedLength()) {
69 throw std::runtime_error("Codec: uncompressed length too large");
73 if (uncompressedLength != UNKNOWN_UNCOMPRESSED_LENGTH &&
74 uncompressedLength != 0) {
75 throw std::runtime_error("Codec: invalid uncompressed length");
77 return IOBuf::create(0);
80 return doUncompress(data, uncompressedLength);
83 bool Codec::needsUncompressedLength() const {
84 return doNeedsUncompressedLength();
87 uint64_t Codec::maxUncompressedLength() const {
88 return doMaxUncompressedLength();
91 bool Codec::doNeedsUncompressedLength() const {
95 uint64_t Codec::doMaxUncompressedLength() const {
96 return UNLIMITED_UNCOMPRESSED_LENGTH;
104 class NoCompressionCodec FOLLY_FINAL : public Codec {
106 static std::unique_ptr<Codec> create(int level, CodecType type);
107 explicit NoCompressionCodec(int level, CodecType type);
110 std::unique_ptr<IOBuf> doCompress(const IOBuf* data) FOLLY_OVERRIDE;
111 std::unique_ptr<IOBuf> doUncompress(
113 uint64_t uncompressedLength) FOLLY_OVERRIDE;
116 std::unique_ptr<Codec> NoCompressionCodec::create(int level, CodecType type) {
117 return make_unique<NoCompressionCodec>(level, type);
120 NoCompressionCodec::NoCompressionCodec(int level, CodecType type)
122 DCHECK(type == CodecType::NO_COMPRESSION);
124 case COMPRESSION_LEVEL_DEFAULT:
125 case COMPRESSION_LEVEL_FASTEST:
126 case COMPRESSION_LEVEL_BEST:
130 throw std::invalid_argument(to<std::string>(
131 "NoCompressionCodec: invalid level ", level));
135 std::unique_ptr<IOBuf> NoCompressionCodec::doCompress(
137 return data->clone();
140 std::unique_ptr<IOBuf> NoCompressionCodec::doUncompress(
142 uint64_t uncompressedLength) {
143 if (uncompressedLength != UNKNOWN_UNCOMPRESSED_LENGTH &&
144 data->computeChainDataLength() != uncompressedLength) {
145 throw std::runtime_error(to<std::string>(
146 "NoCompressionCodec: invalid uncompressed length"));
148 return data->clone();
153 void encodeVarintToIOBuf(uint64_t val, folly::IOBuf* out) {
154 DCHECK_GE(out->tailroom(), kMaxVarintLength64);
155 out->append(encodeVarint(val, out->writableTail()));
158 uint64_t decodeVarintFromCursor(folly::io::Cursor& cursor) {
159 // Must have enough room in *this* buffer.
160 auto p = cursor.peek();
161 folly::ByteRange range(p.first, p.second);
162 uint64_t val = decodeVarint(range);
163 cursor.skip(range.data() - p.first);
169 #if FOLLY_HAVE_LIBLZ4
174 class LZ4Codec FOLLY_FINAL : public Codec {
176 static std::unique_ptr<Codec> create(int level, CodecType type);
177 explicit LZ4Codec(int level, CodecType type);
180 bool doNeedsUncompressedLength() const FOLLY_OVERRIDE;
181 uint64_t doMaxUncompressedLength() const FOLLY_OVERRIDE;
183 bool encodeSize() const { return type() == CodecType::LZ4_VARINT_SIZE; }
185 std::unique_ptr<IOBuf> doCompress(const IOBuf* data) FOLLY_OVERRIDE;
186 std::unique_ptr<IOBuf> doUncompress(
188 uint64_t uncompressedLength) FOLLY_OVERRIDE;
190 bool highCompression_;
193 std::unique_ptr<Codec> LZ4Codec::create(int level, CodecType type) {
194 return make_unique<LZ4Codec>(level, type);
197 LZ4Codec::LZ4Codec(int level, CodecType type) : Codec(type) {
198 DCHECK(type == CodecType::LZ4 || type == CodecType::LZ4_VARINT_SIZE);
201 case COMPRESSION_LEVEL_FASTEST:
202 case COMPRESSION_LEVEL_DEFAULT:
205 case COMPRESSION_LEVEL_BEST:
209 if (level < 1 || level > 2) {
210 throw std::invalid_argument(to<std::string>(
211 "LZ4Codec: invalid level: ", level));
213 highCompression_ = (level > 1);
216 bool LZ4Codec::doNeedsUncompressedLength() const {
217 return !encodeSize();
220 uint64_t LZ4Codec::doMaxUncompressedLength() const {
221 return LZ4_MAX_INPUT_SIZE;
224 std::unique_ptr<IOBuf> LZ4Codec::doCompress(const IOBuf* data) {
225 std::unique_ptr<IOBuf> clone;
226 if (data->isChained()) {
227 // LZ4 doesn't support streaming, so we have to coalesce
228 clone = data->clone();
233 uint32_t extraSize = encodeSize() ? kMaxVarintLength64 : 0;
234 auto out = IOBuf::create(extraSize + LZ4_compressBound(data->length()));
236 encodeVarintToIOBuf(data->length(), out.get());
240 if (highCompression_) {
241 n = LZ4_compressHC(reinterpret_cast<const char*>(data->data()),
242 reinterpret_cast<char*>(out->writableTail()),
245 n = LZ4_compress(reinterpret_cast<const char*>(data->data()),
246 reinterpret_cast<char*>(out->writableTail()),
251 CHECK_LE(n, out->capacity());
257 std::unique_ptr<IOBuf> LZ4Codec::doUncompress(
259 uint64_t uncompressedLength) {
260 std::unique_ptr<IOBuf> clone;
261 if (data->isChained()) {
262 // LZ4 doesn't support streaming, so we have to coalesce
263 clone = data->clone();
268 folly::io::Cursor cursor(data);
269 uint64_t actualUncompressedLength;
271 actualUncompressedLength = decodeVarintFromCursor(cursor);
272 if (uncompressedLength != UNKNOWN_UNCOMPRESSED_LENGTH &&
273 uncompressedLength != actualUncompressedLength) {
274 throw std::runtime_error("LZ4Codec: invalid uncompressed length");
277 actualUncompressedLength = uncompressedLength;
278 if (actualUncompressedLength == UNKNOWN_UNCOMPRESSED_LENGTH ||
279 actualUncompressedLength > maxUncompressedLength()) {
280 throw std::runtime_error("LZ4Codec: invalid uncompressed length");
284 auto p = cursor.peek();
285 auto out = IOBuf::create(actualUncompressedLength);
286 int n = LZ4_decompress_safe(reinterpret_cast<const char*>(p.first),
287 reinterpret_cast<char*>(out->writableTail()),
289 actualUncompressedLength);
291 if (n != actualUncompressedLength) {
292 throw std::runtime_error(to<std::string>(
293 "LZ4 decompression returned invalid value ", n));
295 out->append(actualUncompressedLength);
299 #endif // FOLLY_HAVE_LIBLZ4
301 #if FOLLY_HAVE_LIBSNAPPY
308 * Implementation of snappy::Source that reads from a IOBuf chain.
310 class IOBufSnappySource FOLLY_FINAL : public snappy::Source {
312 explicit IOBufSnappySource(const IOBuf* data);
313 size_t Available() const FOLLY_OVERRIDE;
314 const char* Peek(size_t* len) FOLLY_OVERRIDE;
315 void Skip(size_t n) FOLLY_OVERRIDE;
321 IOBufSnappySource::IOBufSnappySource(const IOBuf* data)
322 : available_(data->computeChainDataLength()),
326 size_t IOBufSnappySource::Available() const {
330 const char* IOBufSnappySource::Peek(size_t* len) {
331 auto p = cursor_.peek();
333 return reinterpret_cast<const char*>(p.first);
336 void IOBufSnappySource::Skip(size_t n) {
337 CHECK_LE(n, available_);
342 class SnappyCodec FOLLY_FINAL : public Codec {
344 static std::unique_ptr<Codec> create(int level, CodecType type);
345 explicit SnappyCodec(int level, CodecType type);
348 uint64_t doMaxUncompressedLength() const FOLLY_OVERRIDE;
349 std::unique_ptr<IOBuf> doCompress(const IOBuf* data) FOLLY_OVERRIDE;
350 std::unique_ptr<IOBuf> doUncompress(
352 uint64_t uncompressedLength) FOLLY_OVERRIDE;
355 std::unique_ptr<Codec> SnappyCodec::create(int level, CodecType type) {
356 return make_unique<SnappyCodec>(level, type);
359 SnappyCodec::SnappyCodec(int level, CodecType type) : Codec(type) {
360 DCHECK(type == CodecType::SNAPPY);
362 case COMPRESSION_LEVEL_FASTEST:
363 case COMPRESSION_LEVEL_DEFAULT:
364 case COMPRESSION_LEVEL_BEST:
368 throw std::invalid_argument(to<std::string>(
369 "SnappyCodec: invalid level: ", level));
373 uint64_t SnappyCodec::doMaxUncompressedLength() const {
374 // snappy.h uses uint32_t for lengths, so there's that.
375 return std::numeric_limits<uint32_t>::max();
378 std::unique_ptr<IOBuf> SnappyCodec::doCompress(const IOBuf* data) {
379 IOBufSnappySource source(data);
381 IOBuf::create(snappy::MaxCompressedLength(source.Available()));
383 snappy::UncheckedByteArraySink sink(reinterpret_cast<char*>(
384 out->writableTail()));
386 size_t n = snappy::Compress(&source, &sink);
388 CHECK_LE(n, out->capacity());
393 std::unique_ptr<IOBuf> SnappyCodec::doUncompress(const IOBuf* data,
394 uint64_t uncompressedLength) {
395 uint32_t actualUncompressedLength = 0;
398 IOBufSnappySource source(data);
399 if (!snappy::GetUncompressedLength(&source, &actualUncompressedLength)) {
400 throw std::runtime_error("snappy::GetUncompressedLength failed");
402 if (uncompressedLength != UNKNOWN_UNCOMPRESSED_LENGTH &&
403 uncompressedLength != actualUncompressedLength) {
404 throw std::runtime_error("snappy: invalid uncompressed length");
408 auto out = IOBuf::create(actualUncompressedLength);
411 IOBufSnappySource source(data);
412 if (!snappy::RawUncompress(&source,
413 reinterpret_cast<char*>(out->writableTail()))) {
414 throw std::runtime_error("snappy::RawUncompress failed");
418 out->append(actualUncompressedLength);
422 #endif // FOLLY_HAVE_LIBSNAPPY
428 class ZlibCodec FOLLY_FINAL : public Codec {
430 static std::unique_ptr<Codec> create(int level, CodecType type);
431 explicit ZlibCodec(int level, CodecType type);
434 std::unique_ptr<IOBuf> doCompress(const IOBuf* data) FOLLY_OVERRIDE;
435 std::unique_ptr<IOBuf> doUncompress(
437 uint64_t uncompressedLength) FOLLY_OVERRIDE;
439 std::unique_ptr<IOBuf> addOutputBuffer(z_stream* stream, uint32_t length);
440 bool doInflate(z_stream* stream, IOBuf* head, uint32_t bufferLength);
445 std::unique_ptr<Codec> ZlibCodec::create(int level, CodecType type) {
446 return make_unique<ZlibCodec>(level, type);
449 ZlibCodec::ZlibCodec(int level, CodecType type) : Codec(type) {
450 DCHECK(type == CodecType::ZLIB);
452 case COMPRESSION_LEVEL_FASTEST:
455 case COMPRESSION_LEVEL_DEFAULT:
456 level = Z_DEFAULT_COMPRESSION;
458 case COMPRESSION_LEVEL_BEST:
462 if (level != Z_DEFAULT_COMPRESSION && (level < 0 || level > 9)) {
463 throw std::invalid_argument(to<std::string>(
464 "ZlibCodec: invalid level: ", level));
469 std::unique_ptr<IOBuf> ZlibCodec::addOutputBuffer(z_stream* stream,
471 CHECK_EQ(stream->avail_out, 0);
473 auto buf = IOBuf::create(length);
476 stream->next_out = buf->writableData();
477 stream->avail_out = buf->length();
482 bool ZlibCodec::doInflate(z_stream* stream,
484 uint32_t bufferLength) {
485 if (stream->avail_out == 0) {
486 head->prependChain(addOutputBuffer(stream, bufferLength));
489 int rc = inflate(stream, Z_NO_FLUSH);
500 throw std::runtime_error(to<std::string>(
501 "ZlibCodec: inflate error: ", rc, ": ", stream->msg));
503 CHECK(false) << rc << ": " << stream->msg;
509 std::unique_ptr<IOBuf> ZlibCodec::doCompress(const IOBuf* data) {
511 stream.zalloc = nullptr;
512 stream.zfree = nullptr;
513 stream.opaque = nullptr;
515 int rc = deflateInit(&stream, level_);
517 throw std::runtime_error(to<std::string>(
518 "ZlibCodec: deflateInit error: ", rc, ": ", stream.msg));
521 stream.next_in = stream.next_out = nullptr;
522 stream.avail_in = stream.avail_out = 0;
523 stream.total_in = stream.total_out = 0;
525 bool success = false;
528 int rc = deflateEnd(&stream);
529 // If we're here because of an exception, it's okay if some data
531 CHECK(rc == Z_OK || (!success && rc == Z_DATA_ERROR))
532 << rc << ": " << stream.msg;
535 uint64_t uncompressedLength = data->computeChainDataLength();
536 uint64_t maxCompressedLength = deflateBound(&stream, uncompressedLength);
538 // Max 64MiB in one go
539 constexpr uint32_t maxSingleStepLength = uint32_t(64) << 20; // 64MiB
540 constexpr uint32_t defaultBufferLength = uint32_t(4) << 20; // 4MiB
542 auto out = addOutputBuffer(
544 (maxCompressedLength <= maxSingleStepLength ?
545 maxCompressedLength :
546 defaultBufferLength));
548 for (auto& range : *data) {
553 stream.next_in = const_cast<uint8_t*>(range.data());
554 stream.avail_in = range.size();
556 while (stream.avail_in != 0) {
557 if (stream.avail_out == 0) {
558 out->prependChain(addOutputBuffer(&stream, defaultBufferLength));
561 rc = deflate(&stream, Z_NO_FLUSH);
563 CHECK_EQ(rc, Z_OK) << stream.msg;
568 if (stream.avail_out == 0) {
569 out->prependChain(addOutputBuffer(&stream, defaultBufferLength));
572 rc = deflate(&stream, Z_FINISH);
573 } while (rc == Z_OK);
575 CHECK_EQ(rc, Z_STREAM_END) << stream.msg;
577 out->prev()->trimEnd(stream.avail_out);
579 success = true; // we survived
584 std::unique_ptr<IOBuf> ZlibCodec::doUncompress(const IOBuf* data,
585 uint64_t uncompressedLength) {
587 stream.zalloc = nullptr;
588 stream.zfree = nullptr;
589 stream.opaque = nullptr;
591 int rc = inflateInit(&stream);
593 throw std::runtime_error(to<std::string>(
594 "ZlibCodec: inflateInit error: ", rc, ": ", stream.msg));
597 stream.next_in = stream.next_out = nullptr;
598 stream.avail_in = stream.avail_out = 0;
599 stream.total_in = stream.total_out = 0;
601 bool success = false;
604 int rc = inflateEnd(&stream);
605 // If we're here because of an exception, it's okay if some data
607 CHECK(rc == Z_OK || (!success && rc == Z_DATA_ERROR))
608 << rc << ": " << stream.msg;
611 // Max 64MiB in one go
612 constexpr uint32_t maxSingleStepLength = uint32_t(64) << 20; // 64MiB
613 constexpr uint32_t defaultBufferLength = uint32_t(4) << 20; // 4MiB
615 auto out = addOutputBuffer(
617 ((uncompressedLength != UNKNOWN_UNCOMPRESSED_LENGTH &&
618 uncompressedLength <= maxSingleStepLength) ?
620 defaultBufferLength));
622 bool streamEnd = false;
623 for (auto& range : *data) {
628 stream.next_in = const_cast<uint8_t*>(range.data());
629 stream.avail_in = range.size();
631 while (stream.avail_in != 0) {
633 throw std::runtime_error(to<std::string>(
634 "ZlibCodec: junk after end of data"));
637 streamEnd = doInflate(&stream, out.get(), defaultBufferLength);
642 streamEnd = doInflate(&stream, out.get(), defaultBufferLength);
645 out->prev()->trimEnd(stream.avail_out);
647 if (uncompressedLength != UNKNOWN_UNCOMPRESSED_LENGTH &&
648 uncompressedLength != stream.total_out) {
649 throw std::runtime_error(to<std::string>(
650 "ZlibCodec: invalid uncompressed length"));
653 success = true; // we survived
658 #endif // FOLLY_HAVE_LIBZ
660 #if FOLLY_HAVE_LIBLZMA
665 class LZMA2Codec FOLLY_FINAL : public Codec {
667 static std::unique_ptr<Codec> create(int level, CodecType type);
668 explicit LZMA2Codec(int level, CodecType type);
671 bool doNeedsUncompressedLength() const FOLLY_OVERRIDE;
672 uint64_t doMaxUncompressedLength() const FOLLY_OVERRIDE;
674 bool encodeSize() const { return type() == CodecType::LZMA2_VARINT_SIZE; }
676 std::unique_ptr<IOBuf> doCompress(const IOBuf* data) FOLLY_OVERRIDE;
677 std::unique_ptr<IOBuf> doUncompress(
679 uint64_t uncompressedLength) FOLLY_OVERRIDE;
681 std::unique_ptr<IOBuf> addOutputBuffer(lzma_stream* stream, size_t length);
682 bool doInflate(lzma_stream* stream, IOBuf* head, size_t bufferLength);
687 std::unique_ptr<Codec> LZMA2Codec::create(int level, CodecType type) {
688 return make_unique<LZMA2Codec>(level, type);
691 LZMA2Codec::LZMA2Codec(int level, CodecType type) : Codec(type) {
692 DCHECK(type == CodecType::LZMA2 || type == CodecType::LZMA2_VARINT_SIZE);
694 case COMPRESSION_LEVEL_FASTEST:
697 case COMPRESSION_LEVEL_DEFAULT:
698 level = LZMA_PRESET_DEFAULT;
700 case COMPRESSION_LEVEL_BEST:
704 if (level < 0 || level > 9) {
705 throw std::invalid_argument(to<std::string>(
706 "LZMA2Codec: invalid level: ", level));
711 bool LZMA2Codec::doNeedsUncompressedLength() const {
712 return !encodeSize();
715 uint64_t LZMA2Codec::doMaxUncompressedLength() const {
716 // From lzma/base.h: "Stream is roughly 8 EiB (2^63 bytes)"
717 return uint64_t(1) << 63;
720 std::unique_ptr<IOBuf> LZMA2Codec::addOutputBuffer(
724 CHECK_EQ(stream->avail_out, 0);
726 auto buf = IOBuf::create(length);
729 stream->next_out = buf->writableData();
730 stream->avail_out = buf->length();
735 std::unique_ptr<IOBuf> LZMA2Codec::doCompress(const IOBuf* data) {
737 lzma_stream stream = LZMA_STREAM_INIT;
739 rc = lzma_easy_encoder(&stream, level_, LZMA_CHECK_NONE);
741 throw std::runtime_error(folly::to<std::string>(
742 "LZMA2Codec: lzma_easy_encoder error: ", rc));
745 SCOPE_EXIT { lzma_end(&stream); };
747 uint64_t uncompressedLength = data->computeChainDataLength();
748 uint64_t maxCompressedLength = lzma_stream_buffer_bound(uncompressedLength);
750 // Max 64MiB in one go
751 constexpr uint32_t maxSingleStepLength = uint32_t(64) << 20; // 64MiB
752 constexpr uint32_t defaultBufferLength = uint32_t(4) << 20; // 4MiB
754 auto out = addOutputBuffer(
756 (maxCompressedLength <= maxSingleStepLength ?
757 maxCompressedLength :
758 defaultBufferLength));
761 auto size = IOBuf::createCombined(kMaxVarintLength64);
762 encodeVarintToIOBuf(uncompressedLength, size.get());
763 size->appendChain(std::move(out));
764 out = std::move(size);
767 for (auto& range : *data) {
772 stream.next_in = const_cast<uint8_t*>(range.data());
773 stream.avail_in = range.size();
775 while (stream.avail_in != 0) {
776 if (stream.avail_out == 0) {
777 out->prependChain(addOutputBuffer(&stream, defaultBufferLength));
780 rc = lzma_code(&stream, LZMA_RUN);
783 throw std::runtime_error(folly::to<std::string>(
784 "LZMA2Codec: lzma_code error: ", rc));
790 if (stream.avail_out == 0) {
791 out->prependChain(addOutputBuffer(&stream, defaultBufferLength));
794 rc = lzma_code(&stream, LZMA_FINISH);
795 } while (rc == LZMA_OK);
797 if (rc != LZMA_STREAM_END) {
798 throw std::runtime_error(folly::to<std::string>(
799 "LZMA2Codec: lzma_code ended with error: ", rc));
802 out->prev()->trimEnd(stream.avail_out);
807 bool LZMA2Codec::doInflate(lzma_stream* stream,
809 size_t bufferLength) {
810 if (stream->avail_out == 0) {
811 head->prependChain(addOutputBuffer(stream, bufferLength));
814 lzma_ret rc = lzma_code(stream, LZMA_RUN);
819 case LZMA_STREAM_END:
822 throw std::runtime_error(to<std::string>(
823 "LZMA2Codec: lzma_code error: ", rc));
829 std::unique_ptr<IOBuf> LZMA2Codec::doUncompress(const IOBuf* data,
830 uint64_t uncompressedLength) {
832 lzma_stream stream = LZMA_STREAM_INIT;
834 rc = lzma_auto_decoder(&stream, std::numeric_limits<uint64_t>::max(), 0);
836 throw std::runtime_error(folly::to<std::string>(
837 "LZMA2Codec: lzma_auto_decoder error: ", rc));
840 SCOPE_EXIT { lzma_end(&stream); };
842 // Max 64MiB in one go
843 constexpr uint32_t maxSingleStepLength = uint32_t(64) << 20; // 64MiB
844 constexpr uint32_t defaultBufferLength = uint32_t(4) << 20; // 4MiB
846 folly::io::Cursor cursor(data);
847 uint64_t actualUncompressedLength;
849 actualUncompressedLength = decodeVarintFromCursor(cursor);
850 if (uncompressedLength != UNKNOWN_UNCOMPRESSED_LENGTH &&
851 uncompressedLength != actualUncompressedLength) {
852 throw std::runtime_error("LZMA2Codec: invalid uncompressed length");
855 actualUncompressedLength = uncompressedLength;
856 DCHECK_NE(actualUncompressedLength, UNKNOWN_UNCOMPRESSED_LENGTH);
859 auto out = addOutputBuffer(
861 (actualUncompressedLength <= maxSingleStepLength ?
862 actualUncompressedLength :
863 defaultBufferLength));
865 bool streamEnd = false;
866 auto buf = cursor.peek();
867 while (buf.second != 0) {
868 stream.next_in = const_cast<uint8_t*>(buf.first);
869 stream.avail_in = buf.second;
871 while (stream.avail_in != 0) {
873 throw std::runtime_error(to<std::string>(
874 "LZMA2Codec: junk after end of data"));
877 streamEnd = doInflate(&stream, out.get(), defaultBufferLength);
880 cursor.skip(buf.second);
885 streamEnd = doInflate(&stream, out.get(), defaultBufferLength);
888 out->prev()->trimEnd(stream.avail_out);
890 if (actualUncompressedLength != stream.total_out) {
891 throw std::runtime_error(to<std::string>(
892 "LZMA2Codec: invalid uncompressed length"));
898 #endif // FOLLY_HAVE_LIBLZMA
900 typedef std::unique_ptr<Codec> (*CodecFactory)(int, CodecType);
902 CodecFactory gCodecFactories[
903 static_cast<size_t>(CodecType::NUM_CODEC_TYPES)] = {
904 nullptr, // USER_DEFINED
905 NoCompressionCodec::create,
907 #if FOLLY_HAVE_LIBLZ4
913 #if FOLLY_HAVE_LIBSNAPPY
925 #if FOLLY_HAVE_LIBLZ4
931 #if FOLLY_HAVE_LIBLZMA
942 std::unique_ptr<Codec> getCodec(CodecType type, int level) {
943 size_t idx = static_cast<size_t>(type);
944 if (idx >= static_cast<size_t>(CodecType::NUM_CODEC_TYPES)) {
945 throw std::invalid_argument(to<std::string>(
946 "Compression type ", idx, " not supported"));
948 auto factory = gCodecFactories[idx];
950 throw std::invalid_argument(to<std::string>(
951 "Compression type ", idx, " not supported"));
953 auto codec = (*factory)(level, type);
954 DCHECK_EQ(static_cast<size_t>(codec->type()), idx);