LLVM  8.0.1
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 (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");
45  }
46 }
47 
48 bool zlib::isAvailable() { return true; }
49 
51  SmallVectorImpl<char> &CompressedBuffer, int Level) {
52  unsigned long CompressedSize = ::compressBound(InputBuffer.size());
53  CompressedBuffer.reserve(CompressedSize);
54  int Res =
55  ::compress2((Bytef *)CompressedBuffer.data(), &CompressedSize,
56  (const Bytef *)InputBuffer.data(), InputBuffer.size(), Level);
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);
60  CompressedBuffer.set_size(CompressedSize);
61  return Res ? createError(convertZlibCodeToString(Res)) : Error::success();
62 }
63 
64 Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer,
65  size_t &UncompressedSize) {
66  int Res =
67  ::uncompress((Bytef *)UncompressedBuffer, (uLongf *)&UncompressedSize,
68  (const Bytef *)InputBuffer.data(), InputBuffer.size());
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);
72  return Res ? createError(convertZlibCodeToString(Res)) : Error::success();
73 }
74 
76  SmallVectorImpl<char> &UncompressedBuffer,
77  size_t UncompressedSize) {
78  UncompressedBuffer.resize(UncompressedSize);
79  Error E =
80  uncompress(InputBuffer, UncompressedBuffer.data(), UncompressedSize);
81  UncompressedBuffer.resize(UncompressedSize);
82  return E;
83 }
84 
86  return ::crc32(0, (const Bytef *)Buffer.data(), Buffer.size());
87 }
88 
89 #else
90 bool zlib::isAvailable() { return false; }
91 Error zlib::compress(StringRef InputBuffer,
92  SmallVectorImpl<char> &CompressedBuffer, int Level) {
93  llvm_unreachable("zlib::compress is unavailable");
94 }
95 Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer,
96  size_t &UncompressedSize) {
97  llvm_unreachable("zlib::uncompress is unavailable");
98 }
99 Error zlib::uncompress(StringRef InputBuffer,
100  SmallVectorImpl<char> &UncompressedBuffer,
101  size_t UncompressedSize) {
102  llvm_unreachable("zlib::uncompress is unavailable");
103 }
105  llvm_unreachable("zlib::crc32 is unavailable");
106 }
107 #endif
static Error createError(StringRef Err)
Definition: Compression.cpp:28
This class represents lattice values for constants.
Definition: AllocatorList.h:24
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
void reserve(size_type N)
Definition: SmallVector.h:376
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
Error compress(StringRef InputBuffer, SmallVectorImpl< char > &CompressedBuffer, int Level=DefaultCompression)
Definition: Compression.cpp:50
bool isAvailable()
Definition: Compression.cpp:48
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define __msan_unpoison(p, size)
Definition: Compiler.h:393
static StringRef convertZlibCodeToString(int Code)
Definition: Compression.cpp:32
#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:327
uint32_t crc32(StringRef Buffer)
Definition: Compression.cpp:85
pointer data()
Return a pointer to the vector&#39;s buffer, even if empty().
Definition: SmallVector.h:149
void set_size(size_t Size)
Set the array size to N, which the current array must have enough capacity for.
Definition: SmallVector.h:67
Lightweight error class with error context and mandatory checking.
Definition: Error.h:158
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:64
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:78
void resize(size_type N)
Definition: SmallVector.h:351