1 //===--- Compression.cpp - Compression implementation ---------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements compression functions. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Support/Compression.h" 15 #include "llvm/ADT/OwningPtr.h" 16 #include "llvm/ADT/StringRef.h" 17 #include "llvm/Config/config.h" 18 #include "llvm/Support/Compiler.h" 19 #include "llvm/Support/ErrorHandling.h" 20 #include "llvm/Support/MemoryBuffer.h" 21 #if LLVM_ENABLE_ZLIB == 1 && HAVE_ZLIB_H 22 #include <zlib.h> 23 #endif 24 25 using namespace llvm; 26 27 #if LLVM_ENABLE_ZLIB == 1 && HAVE_LIBZ 28 static int encodeZlibCompressionLevel(zlib::CompressionLevel Level) { 29 switch (Level) { 30 case zlib::NoCompression: return 0; 31 case zlib::BestSpeedCompression: return 1; 32 case zlib::DefaultCompression: return Z_DEFAULT_COMPRESSION; 33 case zlib::BestSizeCompression: return 9; 34 } 35 llvm_unreachable("Invalid zlib::CompressionLevel!"); 36 } 37 38 static zlib::Status encodeZlibReturnValue(int ReturnValue) { 39 switch (ReturnValue) { 40 case Z_OK: return zlib::StatusOK; 41 case Z_MEM_ERROR: return zlib::StatusOutOfMemory; 42 case Z_BUF_ERROR: return zlib::StatusBufferTooShort; 43 case Z_STREAM_ERROR: return zlib::StatusInvalidArg; 44 case Z_DATA_ERROR: return zlib::StatusInvalidData; 45 default: llvm_unreachable("unknown zlib return status!"); 46 } 47 } 48 49 bool zlib::isAvailable() { return true; } 50 zlib::Status zlib::compress(StringRef InputBuffer, 51 OwningPtr<MemoryBuffer> &CompressedBuffer, 52 CompressionLevel Level) { 53 unsigned long CompressedSize = ::compressBound(InputBuffer.size()); 54 OwningArrayPtr<char> TmpBuffer(new char[CompressedSize]); 55 int CLevel = encodeZlibCompressionLevel(Level); 56 Status Res = encodeZlibReturnValue(::compress2( 57 (Bytef *)TmpBuffer.get(), &CompressedSize, 58 (const Bytef *)InputBuffer.data(), InputBuffer.size(), CLevel)); 59 if (Res == StatusOK) { 60 CompressedBuffer.reset(MemoryBuffer::getMemBufferCopy( 61 StringRef(TmpBuffer.get(), CompressedSize))); 62 // Tell MSan that memory initialized by zlib is valid. 63 __msan_unpoison(CompressedBuffer->getBufferStart(), CompressedSize); 64 } 65 return Res; 66 } 67 68 zlib::Status zlib::uncompress(StringRef InputBuffer, 69 OwningPtr<MemoryBuffer> &UncompressedBuffer, 70 size_t UncompressedSize) { 71 OwningArrayPtr<char> TmpBuffer(new char[UncompressedSize]); 72 Status Res = encodeZlibReturnValue( 73 ::uncompress((Bytef *)TmpBuffer.get(), (uLongf *)&UncompressedSize, 74 (const Bytef *)InputBuffer.data(), InputBuffer.size())); 75 if (Res == StatusOK) { 76 UncompressedBuffer.reset(MemoryBuffer::getMemBufferCopy( 77 StringRef(TmpBuffer.get(), UncompressedSize))); 78 // Tell MSan that memory initialized by zlib is valid. 79 __msan_unpoison(UncompressedBuffer->getBufferStart(), UncompressedSize); 80 } 81 return Res; 82 } 83 84 uint32_t zlib::crc32(StringRef Buffer) { 85 return ::crc32(0, (const Bytef *)Buffer.data(), Buffer.size()); 86 } 87 88 #else 89 bool zlib::isAvailable() { return false; } 90 zlib::Status zlib::compress(StringRef InputBuffer, 91 OwningPtr<MemoryBuffer> &CompressedBuffer, 92 CompressionLevel Level) { 93 return zlib::StatusUnsupported; 94 } 95 zlib::Status zlib::uncompress(StringRef InputBuffer, 96 OwningPtr<MemoryBuffer> &UncompressedBuffer, 97 size_t UncompressedSize) { 98 return zlib::StatusUnsupported; 99 } 100 uint32_t zlib::crc32(StringRef Buffer) { 101 llvm_unreachable("zlib::crc32 is unavailable"); 102 } 103 #endif 104 105