Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 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" |
Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/StringRef.h" |
| 16 | #include "llvm/Config/config.h" |
Alexey Samsonov | 0c9f1bf | 2013-04-23 12:17:46 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Compiler.h" |
Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 18 | #include "llvm/Support/ErrorHandling.h" |
Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 19 | #if LLVM_ENABLE_ZLIB == 1 && HAVE_ZLIB_H |
| 20 | #include <zlib.h> |
| 21 | #endif |
| 22 | |
| 23 | using namespace llvm; |
| 24 | |
Alexey Samsonov | 28acf05 | 2013-04-23 08:57:30 +0000 | [diff] [blame] | 25 | #if LLVM_ENABLE_ZLIB == 1 && HAVE_LIBZ |
Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 26 | static int encodeZlibCompressionLevel(zlib::CompressionLevel Level) { |
| 27 | switch (Level) { |
| 28 | case zlib::NoCompression: return 0; |
| 29 | case zlib::BestSpeedCompression: return 1; |
| 30 | case zlib::DefaultCompression: return Z_DEFAULT_COMPRESSION; |
| 31 | case zlib::BestSizeCompression: return 9; |
| 32 | } |
Hans Wennborg | 63761d4b | 2013-04-23 10:12:16 +0000 | [diff] [blame] | 33 | llvm_unreachable("Invalid zlib::CompressionLevel!"); |
Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | static zlib::Status encodeZlibReturnValue(int ReturnValue) { |
| 37 | switch (ReturnValue) { |
| 38 | case Z_OK: return zlib::StatusOK; |
| 39 | case Z_MEM_ERROR: return zlib::StatusOutOfMemory; |
| 40 | case Z_BUF_ERROR: return zlib::StatusBufferTooShort; |
| 41 | case Z_STREAM_ERROR: return zlib::StatusInvalidArg; |
| 42 | case Z_DATA_ERROR: return zlib::StatusInvalidData; |
| 43 | default: llvm_unreachable("unknown zlib return status!"); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | bool zlib::isAvailable() { return true; } |
| 48 | zlib::Status zlib::compress(StringRef InputBuffer, |
David Blaikie | 857497b | 2014-04-05 21:53:04 +0000 | [diff] [blame] | 49 | SmallVectorImpl<char> &CompressedBuffer, |
Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 50 | CompressionLevel Level) { |
| 51 | unsigned long CompressedSize = ::compressBound(InputBuffer.size()); |
David Blaikie | 857497b | 2014-04-05 21:53:04 +0000 | [diff] [blame] | 52 | CompressedBuffer.resize(CompressedSize); |
Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 53 | int CLevel = encodeZlibCompressionLevel(Level); |
| 54 | Status Res = encodeZlibReturnValue(::compress2( |
David Blaikie | 857497b | 2014-04-05 21:53:04 +0000 | [diff] [blame] | 55 | (Bytef *)CompressedBuffer.data(), &CompressedSize, |
Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 56 | (const Bytef *)InputBuffer.data(), InputBuffer.size(), CLevel)); |
Evgeniy Stepanov | 28cacae | 2014-11-25 15:24:07 +0000 | [diff] [blame] | 57 | // Tell MemorySanitizer that zlib output buffer is fully initialized. |
| 58 | // This avoids a false report when running LLVM with uninstrumented ZLib. |
| 59 | __msan_unpoison(CompressedBuffer.data(), CompressedSize); |
David Blaikie | 857497b | 2014-04-05 21:53:04 +0000 | [diff] [blame] | 60 | CompressedBuffer.resize(CompressedSize); |
Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 61 | return Res; |
| 62 | } |
| 63 | |
| 64 | zlib::Status zlib::uncompress(StringRef InputBuffer, |
David Blaikie | a505f24 | 2014-04-05 21:26:44 +0000 | [diff] [blame] | 65 | SmallVectorImpl<char> &UncompressedBuffer, |
Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 66 | size_t UncompressedSize) { |
David Blaikie | a505f24 | 2014-04-05 21:26:44 +0000 | [diff] [blame] | 67 | UncompressedBuffer.resize(UncompressedSize); |
| 68 | Status Res = encodeZlibReturnValue(::uncompress( |
| 69 | (Bytef *)UncompressedBuffer.data(), (uLongf *)&UncompressedSize, |
| 70 | (const Bytef *)InputBuffer.data(), InputBuffer.size())); |
Evgeniy Stepanov | 28cacae | 2014-11-25 15:24:07 +0000 | [diff] [blame] | 71 | // Tell MemorySanitizer that zlib output buffer is fully initialized. |
| 72 | // This avoids a false report when running LLVM with uninstrumented ZLib. |
| 73 | __msan_unpoison(UncompressedBuffer.data(), UncompressedSize); |
David Blaikie | a505f24 | 2014-04-05 21:26:44 +0000 | [diff] [blame] | 74 | UncompressedBuffer.resize(UncompressedSize); |
Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 75 | return Res; |
| 76 | } |
| 77 | |
Alexey Samsonov | 6ede706 | 2013-08-14 16:03:29 +0000 | [diff] [blame] | 78 | uint32_t zlib::crc32(StringRef Buffer) { |
| 79 | return ::crc32(0, (const Bytef *)Buffer.data(), Buffer.size()); |
| 80 | } |
| 81 | |
Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 82 | #else |
| 83 | bool zlib::isAvailable() { return false; } |
| 84 | zlib::Status zlib::compress(StringRef InputBuffer, |
David Blaikie | 857497b | 2014-04-05 21:53:04 +0000 | [diff] [blame] | 85 | SmallVectorImpl<char> &CompressedBuffer, |
Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 86 | CompressionLevel Level) { |
| 87 | return zlib::StatusUnsupported; |
| 88 | } |
| 89 | zlib::Status zlib::uncompress(StringRef InputBuffer, |
David Blaikie | 857497b | 2014-04-05 21:53:04 +0000 | [diff] [blame] | 90 | SmallVectorImpl<char> &UncompressedBuffer, |
Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 91 | size_t UncompressedSize) { |
| 92 | return zlib::StatusUnsupported; |
| 93 | } |
Alexey Samsonov | 6ede706 | 2013-08-14 16:03:29 +0000 | [diff] [blame] | 94 | uint32_t zlib::crc32(StringRef Buffer) { |
| 95 | llvm_unreachable("zlib::crc32 is unavailable"); |
| 96 | } |
Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 97 | #endif |
| 98 | |