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