LLVM  9.0.0svn
Compression.cpp
Go to the documentation of this file.
1 //===--- Compression.cpp - Compression implementation ---------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements compression functions.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Config/config.h"
17 #include "llvm/Support/Compiler.h"
18 #include "llvm/Support/Error.h"
20 #if LLVM_ENABLE_ZLIB == 1 && HAVE_ZLIB_H
21 #include <zlib.h>
22 #endif
23 
24 using namespace llvm;
25 
26 #if LLVM_ENABLE_ZLIB == 1 && HAVE_LIBZ
28  return make_error<StringError>(Err, inconvertibleErrorCode());
29 }
30 
32  switch (Code) {
33  case Z_MEM_ERROR:
34  return "zlib error: Z_MEM_ERROR";
35  case Z_BUF_ERROR:
36  return "zlib error: Z_BUF_ERROR";
37  case Z_STREAM_ERROR:
38  return "zlib error: Z_STREAM_ERROR";
39  case Z_DATA_ERROR:
40  return "zlib error: Z_DATA_ERROR";
41  case Z_OK:
42  default:
43  llvm_unreachable("unknown or unexpected zlib status code");
44  }
45 }
46 
47 bool zlib::isAvailable() { return true; }
48 
50  SmallVectorImpl<char> &CompressedBuffer, int Level) {
51  unsigned long CompressedSize = ::compressBound(InputBuffer.size());
52  CompressedBuffer.reserve(CompressedSize);
53  int Res =
54  ::compress2((Bytef *)CompressedBuffer.data(), &CompressedSize,
55  (const Bytef *)InputBuffer.data(), InputBuffer.size(), Level);
56  // Tell MemorySanitizer that zlib output buffer is fully initialized.
57  // This avoids a false report when running LLVM with uninstrumented ZLib.
58  __msan_unpoison(CompressedBuffer.data(), CompressedSize);
59  CompressedBuffer.set_size(CompressedSize);
60  return Res ? createError(convertZlibCodeToString(Res)) : Error::success();
61 }
62 
63 Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer,
64  size_t &UncompressedSize) {
65  int Res =
66  ::uncompress((Bytef *)UncompressedBuffer, (uLongf *)&UncompressedSize,
67  (const Bytef *)InputBuffer.data(), InputBuffer.size());
68  // Tell MemorySanitizer that zlib output buffer is fully initialized.
69  // This avoids a false report when running LLVM with uninstrumented ZLib.
70  __msan_unpoison(UncompressedBuffer, UncompressedSize);
71  return Res ? createError(convertZlibCodeToString(Res)) : Error::success();
72 }
73 
75  SmallVectorImpl<char> &UncompressedBuffer,
76  size_t UncompressedSize) {
77  UncompressedBuffer.resize(UncompressedSize);
78  Error E =
79  uncompress(InputBuffer, UncompressedBuffer.data(), UncompressedSize);
80  UncompressedBuffer.resize(UncompressedSize);
81  return E;
82 }
83 
85  return ::crc32(0, (const Bytef *)Buffer.data(), Buffer.size());
86 }
87 
88 #else
89 bool zlib::isAvailable() { return false; }
90 Error zlib::compress(StringRef InputBuffer,
91  SmallVectorImpl<char> &CompressedBuffer, int Level) {
92  llvm_unreachable("zlib::compress is unavailable");
93 }
94 Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer,
95  size_t &UncompressedSize) {
96  llvm_unreachable("zlib::uncompress is unavailable");
97 }
98 Error zlib::uncompress(StringRef InputBuffer,
99  SmallVectorImpl<char> &UncompressedBuffer,
100  size_t UncompressedSize) {
101  llvm_unreachable("zlib::uncompress is unavailable");
102 }
104  llvm_unreachable("zlib::crc32 is unavailable");
105 }
106 #endif
static Error createError(StringRef Err)
Definition: Compression.cpp:27
This class represents lattice values for constants.
Definition: AllocatorList.h:23
void reserve(size_type N)
Definition: SmallVector.h:368
Error compress(StringRef InputBuffer, SmallVectorImpl< char > &CompressedBuffer, int Level=DefaultCompression)
Definition: Compression.cpp:49
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:130
bool isAvailable()
Definition: Compression.cpp:47
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static StringRef convertZlibCodeToString(int Code)
Definition: Compression.cpp:31
#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:326
#define __msan_unpoison(p, size)
Definition: Compiler.h:392
uint32_t crc32(StringRef Buffer)
Definition: Compression.cpp:84
pointer data()
Return a pointer to the vector&#39;s buffer, even if empty().
Definition: SmallVector.h:144
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:122
void set_size(size_t Size)
Set the array size to N, which the current array must have enough capacity for.
Definition: SmallVector.h:66
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
Error uncompress(StringRef InputBuffer, char *UncompressedBuffer, size_t &UncompressedSize)
Definition: Compression.cpp:63
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:77
void resize(size_type N)
Definition: SmallVector.h:343