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)); |
David Blaikie | 857497b | 2014-04-05 21:53:04 +0000 | [diff] [blame] | 57 | CompressedBuffer.resize(CompressedSize); |
Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 58 | return Res; |
| 59 | } |
| 60 | |
| 61 | zlib::Status zlib::uncompress(StringRef InputBuffer, |
David Blaikie | a505f24 | 2014-04-05 21:26:44 +0000 | [diff] [blame] | 62 | SmallVectorImpl<char> &UncompressedBuffer, |
Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 63 | size_t UncompressedSize) { |
David Blaikie | a505f24 | 2014-04-05 21:26:44 +0000 | [diff] [blame] | 64 | UncompressedBuffer.resize(UncompressedSize); |
| 65 | Status Res = encodeZlibReturnValue(::uncompress( |
| 66 | (Bytef *)UncompressedBuffer.data(), (uLongf *)&UncompressedSize, |
| 67 | (const Bytef *)InputBuffer.data(), InputBuffer.size())); |
| 68 | UncompressedBuffer.resize(UncompressedSize); |
Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 69 | return Res; |
| 70 | } |
| 71 | |
Alexey Samsonov | 6ede706 | 2013-08-14 16:03:29 +0000 | [diff] [blame] | 72 | uint32_t zlib::crc32(StringRef Buffer) { |
| 73 | return ::crc32(0, (const Bytef *)Buffer.data(), Buffer.size()); |
| 74 | } |
| 75 | |
Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 76 | #else |
| 77 | bool zlib::isAvailable() { return false; } |
| 78 | zlib::Status zlib::compress(StringRef InputBuffer, |
David Blaikie | 857497b | 2014-04-05 21:53:04 +0000 | [diff] [blame] | 79 | SmallVectorImpl<char> &CompressedBuffer, |
Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 80 | CompressionLevel Level) { |
| 81 | return zlib::StatusUnsupported; |
| 82 | } |
| 83 | zlib::Status zlib::uncompress(StringRef InputBuffer, |
David Blaikie | 857497b | 2014-04-05 21:53:04 +0000 | [diff] [blame] | 84 | SmallVectorImpl<char> &UncompressedBuffer, |
Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 85 | size_t UncompressedSize) { |
| 86 | return zlib::StatusUnsupported; |
| 87 | } |
Alexey Samsonov | 6ede706 | 2013-08-14 16:03:29 +0000 | [diff] [blame] | 88 | uint32_t zlib::crc32(StringRef Buffer) { |
| 89 | llvm_unreachable("zlib::crc32 is unavailable"); |
| 90 | } |
Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 91 | #endif |
| 92 | |