| 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" |
| George Rimar | 167ca4a | 2017-01-17 15:45:07 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Error.h" |
| Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 20 | #include "llvm/Support/ErrorHandling.h" |
| Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 21 | #if LLVM_ENABLE_ZLIB == 1 && HAVE_ZLIB_H |
| 22 | #include <zlib.h> |
| 23 | #endif |
| 24 | |
| 25 | using namespace llvm; |
| 26 | |
| Alexey Samsonov | 28acf05 | 2013-04-23 08:57:30 +0000 | [diff] [blame] | 27 | #if LLVM_ENABLE_ZLIB == 1 && HAVE_LIBZ |
| George Rimar | 167ca4a | 2017-01-17 15:45:07 +0000 | [diff] [blame] | 28 | static Error createError(StringRef Err) { |
| 29 | return make_error<StringError>(Err, inconvertibleErrorCode()); |
| 30 | } |
| 31 | |
| George Rimar | 167ca4a | 2017-01-17 15:45:07 +0000 | [diff] [blame] | 32 | static StringRef convertZlibCodeToString(int Code) { |
| 33 | switch (Code) { |
| 34 | case Z_MEM_ERROR: |
| 35 | return "zlib error: Z_MEM_ERROR"; |
| 36 | case Z_BUF_ERROR: |
| 37 | return "zlib error: Z_BUF_ERROR"; |
| 38 | case Z_STREAM_ERROR: |
| 39 | return "zlib error: Z_STREAM_ERROR"; |
| 40 | case Z_DATA_ERROR: |
| 41 | return "zlib error: Z_DATA_ERROR"; |
| 42 | case Z_OK: |
| 43 | default: |
| 44 | llvm_unreachable("unknown or unexpected zlib status code"); |
| Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
| 48 | bool zlib::isAvailable() { return true; } |
| George Rimar | 167ca4a | 2017-01-17 15:45:07 +0000 | [diff] [blame] | 49 | |
| 50 | Error zlib::compress(StringRef InputBuffer, |
| Rui Ueyama | 2c97adc | 2018-08-04 00:13:13 +0000 | [diff] [blame] | 51 | SmallVectorImpl<char> &CompressedBuffer, int Level) { |
| Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 52 | unsigned long CompressedSize = ::compressBound(InputBuffer.size()); |
| Fangrui Song | 23310a8 | 2018-08-03 19:37:49 +0000 | [diff] [blame] | 53 | CompressedBuffer.reserve(CompressedSize); |
| Rui Ueyama | 2c97adc | 2018-08-04 00:13:13 +0000 | [diff] [blame] | 54 | int Res = |
| 55 | ::compress2((Bytef *)CompressedBuffer.data(), &CompressedSize, |
| 56 | (const Bytef *)InputBuffer.data(), InputBuffer.size(), Level); |
| 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); |
| Fangrui Song | 23310a8 | 2018-08-03 19:37:49 +0000 | [diff] [blame] | 60 | CompressedBuffer.set_size(CompressedSize); |
| George Rimar | 167ca4a | 2017-01-17 15:45:07 +0000 | [diff] [blame] | 61 | return Res ? createError(convertZlibCodeToString(Res)) : Error::success(); |
| Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| George Rimar | 167ca4a | 2017-01-17 15:45:07 +0000 | [diff] [blame] | 64 | Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer, |
| 65 | size_t &UncompressedSize) { |
| 66 | int Res = |
| Rafael Espindola | 46cdcb0 | 2016-09-09 19:32:36 +0000 | [diff] [blame] | 67 | ::uncompress((Bytef *)UncompressedBuffer, (uLongf *)&UncompressedSize, |
| George Rimar | 167ca4a | 2017-01-17 15:45:07 +0000 | [diff] [blame] | 68 | (const Bytef *)InputBuffer.data(), InputBuffer.size()); |
| Rafael Espindola | 46cdcb0 | 2016-09-09 19:32:36 +0000 | [diff] [blame] | 69 | // Tell MemorySanitizer that zlib output buffer is fully initialized. |
| 70 | // This avoids a false report when running LLVM with uninstrumented ZLib. |
| 71 | __msan_unpoison(UncompressedBuffer, UncompressedSize); |
| George Rimar | 167ca4a | 2017-01-17 15:45:07 +0000 | [diff] [blame] | 72 | return Res ? createError(convertZlibCodeToString(Res)) : Error::success(); |
| Rafael Espindola | 46cdcb0 | 2016-09-09 19:32:36 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| George Rimar | 167ca4a | 2017-01-17 15:45:07 +0000 | [diff] [blame] | 75 | Error zlib::uncompress(StringRef InputBuffer, |
| 76 | SmallVectorImpl<char> &UncompressedBuffer, |
| 77 | size_t UncompressedSize) { |
| David Blaikie | a505f24 | 2014-04-05 21:26:44 +0000 | [diff] [blame] | 78 | UncompressedBuffer.resize(UncompressedSize); |
| George Rimar | 167ca4a | 2017-01-17 15:45:07 +0000 | [diff] [blame] | 79 | Error E = |
| Rafael Espindola | 46cdcb0 | 2016-09-09 19:32:36 +0000 | [diff] [blame] | 80 | uncompress(InputBuffer, UncompressedBuffer.data(), UncompressedSize); |
| David Blaikie | a505f24 | 2014-04-05 21:26:44 +0000 | [diff] [blame] | 81 | UncompressedBuffer.resize(UncompressedSize); |
| George Rimar | 167ca4a | 2017-01-17 15:45:07 +0000 | [diff] [blame] | 82 | return E; |
| Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| Alexey Samsonov | 6ede706 | 2013-08-14 16:03:29 +0000 | [diff] [blame] | 85 | uint32_t zlib::crc32(StringRef Buffer) { |
| 86 | return ::crc32(0, (const Bytef *)Buffer.data(), Buffer.size()); |
| 87 | } |
| 88 | |
| Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 89 | #else |
| 90 | bool zlib::isAvailable() { return false; } |
| George Rimar | 167ca4a | 2017-01-17 15:45:07 +0000 | [diff] [blame] | 91 | Error zlib::compress(StringRef InputBuffer, |
| Rui Ueyama | e9798f7 | 2018-08-04 00:23:37 +0000 | [diff] [blame] | 92 | SmallVectorImpl<char> &CompressedBuffer, int Level) { |
| George Rimar | 167ca4a | 2017-01-17 15:45:07 +0000 | [diff] [blame] | 93 | llvm_unreachable("zlib::compress is unavailable"); |
| Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 94 | } |
| George Rimar | 167ca4a | 2017-01-17 15:45:07 +0000 | [diff] [blame] | 95 | Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer, |
| 96 | size_t &UncompressedSize) { |
| 97 | llvm_unreachable("zlib::uncompress is unavailable"); |
| Rafael Espindola | 7494123 | 2016-09-12 13:00:51 +0000 | [diff] [blame] | 98 | } |
| George Rimar | 167ca4a | 2017-01-17 15:45:07 +0000 | [diff] [blame] | 99 | Error zlib::uncompress(StringRef InputBuffer, |
| 100 | SmallVectorImpl<char> &UncompressedBuffer, |
| 101 | size_t UncompressedSize) { |
| 102 | llvm_unreachable("zlib::uncompress is unavailable"); |
| Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 103 | } |
| Alexey Samsonov | 6ede706 | 2013-08-14 16:03:29 +0000 | [diff] [blame] | 104 | uint32_t zlib::crc32(StringRef Buffer) { |
| 105 | llvm_unreachable("zlib::crc32 is unavailable"); |
| 106 | } |
| Alexey Samsonov | 2fb337e | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 107 | #endif |