Alexey Samsonov | ee03c94 | 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" |
| 15 | #include "llvm/ADT/OwningPtr.h" |
| 16 | #include "llvm/ADT/StringRef.h" |
| 17 | #include "llvm/Config/config.h" |
Alexey Samsonov | 39dd5aa | 2013-04-23 12:17:46 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Compiler.h" |
Alexey Samsonov | ee03c94 | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 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 | |
Alexey Samsonov | a0bd5df | 2013-04-23 08:57:30 +0000 | [diff] [blame] | 27 | #if LLVM_ENABLE_ZLIB == 1 && HAVE_LIBZ |
Alexey Samsonov | ee03c94 | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 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 | } |
Hans Wennborg | ab4d569 | 2013-04-23 10:12:16 +0000 | [diff] [blame] | 35 | llvm_unreachable("Invalid zlib::CompressionLevel!"); |
Alexey Samsonov | ee03c94 | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 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)); |
Alexey Samsonov | 39dd5aa | 2013-04-23 12:17:46 +0000 | [diff] [blame] | 59 | if (Res == StatusOK) { |
Alexey Samsonov | ee03c94 | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 60 | CompressedBuffer.reset(MemoryBuffer::getMemBufferCopy( |
| 61 | StringRef(TmpBuffer.get(), CompressedSize))); |
Alexey Samsonov | 39dd5aa | 2013-04-23 12:17:46 +0000 | [diff] [blame] | 62 | // Tell MSan that memory initialized by zlib is valid. |
Alexey Samsonov | a1aabcd9 | 2013-04-23 13:35:32 +0000 | [diff] [blame] | 63 | __msan_unpoison(CompressedBuffer->getBufferStart(), CompressedSize); |
Alexey Samsonov | 39dd5aa | 2013-04-23 12:17:46 +0000 | [diff] [blame] | 64 | } |
Alexey Samsonov | ee03c94 | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 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())); |
Alexey Samsonov | 39dd5aa | 2013-04-23 12:17:46 +0000 | [diff] [blame] | 75 | if (Res == StatusOK) { |
Alexey Samsonov | ee03c94 | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 76 | UncompressedBuffer.reset(MemoryBuffer::getMemBufferCopy( |
| 77 | StringRef(TmpBuffer.get(), UncompressedSize))); |
Alexey Samsonov | 39dd5aa | 2013-04-23 12:17:46 +0000 | [diff] [blame] | 78 | // Tell MSan that memory initialized by zlib is valid. |
Alexey Samsonov | a1aabcd9 | 2013-04-23 13:35:32 +0000 | [diff] [blame] | 79 | __msan_unpoison(UncompressedBuffer->getBufferStart(), UncompressedSize); |
Alexey Samsonov | 39dd5aa | 2013-04-23 12:17:46 +0000 | [diff] [blame] | 80 | } |
Alexey Samsonov | ee03c94 | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 81 | return Res; |
| 82 | } |
| 83 | |
Alexey Samsonov | ef7aefc | 2013-08-14 16:03:29 +0000 | [diff] [blame] | 84 | uint32_t zlib::crc32(StringRef Buffer) { |
| 85 | return ::crc32(0, (const Bytef *)Buffer.data(), Buffer.size()); |
| 86 | } |
| 87 | |
Alexey Samsonov | ee03c94 | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 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 | } |
Alexey Samsonov | ef7aefc | 2013-08-14 16:03:29 +0000 | [diff] [blame] | 100 | uint32_t zlib::crc32(StringRef Buffer) { |
| 101 | llvm_unreachable("zlib::crc32 is unavailable"); |
| 102 | } |
Alexey Samsonov | ee03c94 | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 103 | #endif |
| 104 | |