Compression.cpp revision ab4d569f2841beaf6152179b1f4da6b7e0f3530e
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
24using namespace llvm;
25
26#if LLVM_ENABLE_ZLIB == 1 && HAVE_LIBZ
27static 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  llvm_unreachable("Invalid zlib::CompressionLevel!");
35}
36
37static 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
48bool zlib::isAvailable() { return true; }
49zlib::Status zlib::compress(StringRef InputBuffer,
50                            OwningPtr<MemoryBuffer> &CompressedBuffer,
51                            CompressionLevel Level) {
52  unsigned long CompressedSize = ::compressBound(InputBuffer.size());
53  OwningArrayPtr<char> TmpBuffer(new char[CompressedSize]);
54  int CLevel = encodeZlibCompressionLevel(Level);
55  Status Res = encodeZlibReturnValue(::compress2(
56      (Bytef *)TmpBuffer.get(), &CompressedSize,
57      (const Bytef *)InputBuffer.data(), InputBuffer.size(), CLevel));
58  if (Res == StatusOK)
59    CompressedBuffer.reset(MemoryBuffer::getMemBufferCopy(
60        StringRef(TmpBuffer.get(), CompressedSize)));
61  return Res;
62}
63
64zlib::Status zlib::uncompress(StringRef InputBuffer,
65                              OwningPtr<MemoryBuffer> &UncompressedBuffer,
66                              size_t UncompressedSize) {
67  OwningArrayPtr<char> TmpBuffer(new char[UncompressedSize]);
68  Status Res = encodeZlibReturnValue(
69      ::uncompress((Bytef *)TmpBuffer.get(), (uLongf *)&UncompressedSize,
70                   (const Bytef *)InputBuffer.data(), InputBuffer.size()));
71  if (Res == StatusOK)
72    UncompressedBuffer.reset(MemoryBuffer::getMemBufferCopy(
73        StringRef(TmpBuffer.get(), UncompressedSize)));
74  return Res;
75}
76
77#else
78bool zlib::isAvailable() { return false; }
79zlib::Status zlib::compress(StringRef InputBuffer,
80                            OwningPtr<MemoryBuffer> &CompressedBuffer,
81                            CompressionLevel Level) {
82  return zlib::StatusUnsupported;
83}
84zlib::Status zlib::uncompress(StringRef InputBuffer,
85                              OwningPtr<MemoryBuffer> &UncompressedBuffer,
86                              size_t UncompressedSize) {
87  return zlib::StatusUnsupported;
88}
89#endif
90
91