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" |
| 18 | #include "llvm/Support/ErrorHandling.h" |
| 19 | #include "llvm/Support/MemoryBuffer.h" |
| 20 | #if LLVM_ENABLE_ZLIB == 1 && HAVE_ZLIB_H |
| 21 | #include <zlib.h> |
| 22 | #endif |
| 23 | |
| 24 | using namespace llvm; |
| 25 | |
Alexey Samsonov | a0bd5df | 2013-04-23 08:57:30 +0000 | [diff] [blame^] | 26 | #if LLVM_ENABLE_ZLIB == 1 && HAVE_LIBZ |
Alexey Samsonov | ee03c94 | 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 | } |
| 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, |
| 49 | OwningPtr<MemoryBuffer> &CompressedBuffer, |
| 50 | CompressionLevel Level) { |
| 51 | unsigned long CompressedSize = ::compressBound(InputBuffer.size()); |
| 52 | OwningArrayPtr<char> TmpBuffer(new char[CompressedSize]); |
| 53 | int CLevel = encodeZlibCompressionLevel(Level); |
| 54 | Status Res = encodeZlibReturnValue(::compress2( |
| 55 | (Bytef *)TmpBuffer.get(), &CompressedSize, |
| 56 | (const Bytef *)InputBuffer.data(), InputBuffer.size(), CLevel)); |
| 57 | if (Res == StatusOK) |
| 58 | CompressedBuffer.reset(MemoryBuffer::getMemBufferCopy( |
| 59 | StringRef(TmpBuffer.get(), CompressedSize))); |
| 60 | return Res; |
| 61 | } |
| 62 | |
| 63 | zlib::Status zlib::uncompress(StringRef InputBuffer, |
| 64 | OwningPtr<MemoryBuffer> &UncompressedBuffer, |
| 65 | size_t UncompressedSize) { |
| 66 | OwningArrayPtr<char> TmpBuffer(new char[UncompressedSize]); |
| 67 | Status Res = encodeZlibReturnValue( |
| 68 | ::uncompress((Bytef *)TmpBuffer.get(), (uLongf *)&UncompressedSize, |
| 69 | (const Bytef *)InputBuffer.data(), InputBuffer.size())); |
| 70 | if (Res == StatusOK) |
| 71 | UncompressedBuffer.reset(MemoryBuffer::getMemBufferCopy( |
| 72 | StringRef(TmpBuffer.get(), UncompressedSize))); |
| 73 | return Res; |
| 74 | } |
| 75 | |
| 76 | #else |
| 77 | bool zlib::isAvailable() { return false; } |
| 78 | zlib::Status zlib::compress(StringRef InputBuffer, |
| 79 | OwningPtr<MemoryBuffer> &CompressedBuffer, |
| 80 | CompressionLevel Level) { |
| 81 | return zlib::StatusUnsupported; |
| 82 | } |
| 83 | zlib::Status zlib::uncompress(StringRef InputBuffer, |
| 84 | OwningPtr<MemoryBuffer> &UncompressedBuffer, |
| 85 | size_t UncompressedSize) { |
| 86 | return zlib::StatusUnsupported; |
| 87 | } |
| 88 | #endif |
| 89 | |