Index: include/llvm/Support/Compression.h =================================================================== --- include/llvm/Support/Compression.h +++ include/llvm/Support/Compression.h @@ -15,6 +15,7 @@ #define LLVM_SUPPORT_COMPRESSION_H #include "llvm/Support/DataTypes.h" +#include namespace llvm { template class SmallVectorImpl; @@ -35,6 +36,10 @@ Error compress(StringRef InputBuffer, SmallVectorImpl &CompressedBuffer, CompressionLevel Level = DefaultCompression); +Error compress(StringRef Input, std::unique_ptr &CompressedData, + size_t &CompressedSize, + CompressionLevel Level = DefaultCompression); + Error uncompress(StringRef InputBuffer, char *UncompressedBuffer, size_t &UncompressedSize); Index: lib/Support/Compression.cpp =================================================================== --- lib/Support/Compression.cpp +++ lib/Support/Compression.cpp @@ -73,6 +73,20 @@ return Res ? createError(convertZlibCodeToString(Res)) : Error::success(); } +Error zlib::compress(StringRef Input, + std::unique_ptr &CompressedData, + size_t &CompressedSize, CompressionLevel Level) { + CompressedSize = (size_t)::compressBound(Input.size()); + CompressedData.reset(new uint8_t[CompressedSize]); + int CLevel = encodeZlibCompressionLevel(Level); + int Res = ::compress2(CompressedData.get(), &CompressedSize, + (const Bytef *)Input.data(), Input.size(), CLevel); + // Tell MemorySanitizer that zlib output buffer is fully initialized. + // This avoids a false report when running LLVM with uninstrumented ZLib. + __msan_unpoison(CompressedData.get(), CompressedSize); + return Res ? createError(convertZlibCodeToString(Res)) : Error::success(); +} + Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer, size_t &UncompressedSize) { int Res =