LLVM  6.0.0svn
Compression.cpp
Go to the documentation of this file.
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 
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/Config/config.h"
18 #include "llvm/Support/Compiler.h"
19 #include "llvm/Support/Error.h"
21 #if LLVM_ENABLE_ZLIB == 1 && HAVE_ZLIB_H
22 #include <zlib.h>
23 #endif
24 
25 using namespace llvm;
26 
27 #if LLVM_ENABLE_ZLIB == 1 && HAVE_LIBZ
29  return make_error<StringError>(Err, inconvertibleErrorCode());
30 }
31 
33  switch (Level) {
34  case zlib::NoCompression: return 0;
35  case zlib::BestSpeedCompression: return 1;
36  case zlib::DefaultCompression: return Z_DEFAULT_COMPRESSION;
37  case zlib::BestSizeCompression: return 9;
38  }
39  llvm_unreachable("Invalid zlib::CompressionLevel!");
40 }
41 
43  switch (Code) {
44  case Z_MEM_ERROR:
45  return "zlib error: Z_MEM_ERROR";
46  case Z_BUF_ERROR:
47  return "zlib error: Z_BUF_ERROR";
48  case Z_STREAM_ERROR:
49  return "zlib error: Z_STREAM_ERROR";
50  case Z_DATA_ERROR:
51  return "zlib error: Z_DATA_ERROR";
52  case Z_OK:
53  default:
54  llvm_unreachable("unknown or unexpected zlib status code");
55  }
56 }
57 
58 bool zlib::isAvailable() { return true; }
59 
61  SmallVectorImpl<char> &CompressedBuffer,
63  unsigned long CompressedSize = ::compressBound(InputBuffer.size());
64  CompressedBuffer.resize(CompressedSize);
65  int CLevel = encodeZlibCompressionLevel(Level);
66  int Res = ::compress2((Bytef *)CompressedBuffer.data(), &CompressedSize,
67  (const Bytef *)InputBuffer.data(), InputBuffer.size(),
68  CLevel);
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(CompressedBuffer.data(), CompressedSize);
72  CompressedBuffer.resize(CompressedSize);
73  return Res ? createError(convertZlibCodeToString(Res)) : Error::success();
74 }
75 
76 Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer,
77  size_t &UncompressedSize) {
78  int Res =
79  ::uncompress((Bytef *)UncompressedBuffer, (uLongf *)&UncompressedSize,
80  (const Bytef *)InputBuffer.data(), InputBuffer.size());
81  // Tell MemorySanitizer that zlib output buffer is fully initialized.
82  // This avoids a false report when running LLVM with uninstrumented ZLib.
83  __msan_unpoison(UncompressedBuffer, UncompressedSize);
84  return Res ? createError(convertZlibCodeToString(Res)) : Error::success();
85 }
86 
88  SmallVectorImpl<char> &UncompressedBuffer,
89  size_t UncompressedSize) {
90  UncompressedBuffer.resize(UncompressedSize);
91  Error E =
92  uncompress(InputBuffer, UncompressedBuffer.data(), UncompressedSize);
93  UncompressedBuffer.resize(UncompressedSize);
94  return E;
95 }
96 
98  return ::crc32(0, (const Bytef *)Buffer.data(), Buffer.size());
99 }
100 
101 #else
102 bool zlib::isAvailable() { return false; }
103 Error zlib::compress(StringRef InputBuffer,
104  SmallVectorImpl<char> &CompressedBuffer,
106  llvm_unreachable("zlib::compress is unavailable");
107 }
108 Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer,
109  size_t &UncompressedSize) {
110  llvm_unreachable("zlib::uncompress is unavailable");
111 }
112 Error zlib::uncompress(StringRef InputBuffer,
113  SmallVectorImpl<char> &UncompressedBuffer,
114  size_t UncompressedSize) {
115  llvm_unreachable("zlib::uncompress is unavailable");
116 }
118  llvm_unreachable("zlib::crc32 is unavailable");
119 }
120 #endif
121 
static Error createError(StringRef Err)
Definition: Compression.cpp:28
static int encodeZlibCompressionLevel(zlib::CompressionLevel Level)
Definition: Compression.cpp:32
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:128
bool isAvailable()
Definition: Compression.cpp:58
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static StringRef convertZlibCodeToString(int Code)
Definition: Compression.cpp:42
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static ErrorSuccess success()
Create a success value.
Definition: Error.h:313
Error compress(StringRef InputBuffer, SmallVectorImpl< char > &CompressedBuffer, CompressionLevel Level=DefaultCompression)
Definition: Compression.cpp:60
#define __msan_unpoison(p, size)
Definition: Compiler.h:377
uint32_t crc32(StringRef Buffer)
Definition: Compression.cpp:97
pointer data()
Return a pointer to the vector&#39;s buffer, even if empty().
Definition: SmallVector.h:143
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
Error uncompress(StringRef InputBuffer, char *UncompressedBuffer, size_t &UncompressedSize)
Definition: Compression.cpp:76
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:73
void resize(size_type N)
Definition: SmallVector.h:355