X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;ds=sidebyside;f=folly%2Fio%2FCompression.cpp;h=8c8012dab9a9d84952eaaa3bd125e43ab3a36fd2;hb=b74275bb6e90717575e138155c004b607f7b3364;hp=d7a0544dc3f094d371774f249607e04a9adc2717;hpb=eb9d7cd7754c3f21ec767e261dff0906c35d0cf6;p=folly.git diff --git a/folly/io/Compression.cpp b/folly/io/Compression.cpp index d7a0544d..8c8012da 100644 --- a/folly/io/Compression.cpp +++ b/folly/io/Compression.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2014 Facebook, Inc. + * Copyright 2015 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -148,6 +148,8 @@ std::unique_ptr NoCompressionCodec::doUncompress( return data->clone(); } +#if (FOLLY_HAVE_LIBLZ4 || FOLLY_HAVE_LIBLZMA) + namespace { void encodeVarintToIOBuf(uint64_t val, folly::IOBuf* out) { @@ -155,17 +157,26 @@ void encodeVarintToIOBuf(uint64_t val, folly::IOBuf* out) { out->append(encodeVarint(val, out->writableTail())); } -uint64_t decodeVarintFromCursor(folly::io::Cursor& cursor) { - // Must have enough room in *this* buffer. - auto p = cursor.peek(); - folly::ByteRange range(p.first, p.second); - uint64_t val = decodeVarint(range); - cursor.skip(range.data() - p.first); +inline uint64_t decodeVarintFromCursor(folly::io::Cursor& cursor) { + uint64_t val = 0; + int8_t b = 0; + for (int shift = 0; shift <= 63; shift += 7) { + b = cursor.read(); + val |= static_cast(b & 0x7f) << shift; + if (b >= 0) { + break; + } + } + if (b < 0) { + throw std::invalid_argument("Invalid varint value. Too big."); + } return val; } } // namespace +#endif // FOLLY_HAVE_LIBLZ4 || FOLLY_HAVE_LIBLZMA + #if FOLLY_HAVE_LIBLZ4 /** @@ -908,55 +919,55 @@ std::unique_ptr LZMA2Codec::doUncompress(const IOBuf* data, #endif // FOLLY_HAVE_LIBLZMA -typedef std::unique_ptr (*CodecFactory)(int, CodecType); +} // namespace + +std::unique_ptr getCodec(CodecType type, int level) { + typedef std::unique_ptr (*CodecFactory)(int, CodecType); -CodecFactory gCodecFactories[ + static CodecFactory codecFactories[ static_cast(CodecType::NUM_CODEC_TYPES)] = { - nullptr, // USER_DEFINED - NoCompressionCodec::create, + nullptr, // USER_DEFINED + NoCompressionCodec::create, #if FOLLY_HAVE_LIBLZ4 - LZ4Codec::create, + LZ4Codec::create, #else - nullptr, + nullptr, #endif #if FOLLY_HAVE_LIBSNAPPY - SnappyCodec::create, + SnappyCodec::create, #else - nullptr, + nullptr, #endif #if FOLLY_HAVE_LIBZ - ZlibCodec::create, + ZlibCodec::create, #else - nullptr, + nullptr, #endif #if FOLLY_HAVE_LIBLZ4 - LZ4Codec::create, + LZ4Codec::create, #else - nullptr, + nullptr, #endif #if FOLLY_HAVE_LIBLZMA - LZMA2Codec::create, - LZMA2Codec::create, + LZMA2Codec::create, + LZMA2Codec::create, #else - nullptr, - nullptr, + nullptr, + nullptr, #endif -}; - -} // namespace + }; -std::unique_ptr getCodec(CodecType type, int level) { size_t idx = static_cast(type); if (idx >= static_cast(CodecType::NUM_CODEC_TYPES)) { throw std::invalid_argument(to( "Compression type ", idx, " not supported")); } - auto factory = gCodecFactories[idx]; + auto factory = codecFactories[idx]; if (!factory) { throw std::invalid_argument(to( "Compression type ", idx, " not supported"));