1 //===--- Compression.cpp - Compression implementation ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements compression functions.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/Compression.h"
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"
20 #include "llvm/Support/ErrorHandling.h"
21 #if LLVM_ENABLE_ZLIB == 1 && HAVE_ZLIB_H
27 #if LLVM_ENABLE_ZLIB == 1 && HAVE_LIBZ
28 static Error
createError(StringRef Err
) {
29 return make_error
<StringError
>(Err
, inconvertibleErrorCode());
32 static StringRef
convertZlibCodeToString(int Code
) {
35 return "zlib error: Z_MEM_ERROR";
37 return "zlib error: Z_BUF_ERROR";
39 return "zlib error: Z_STREAM_ERROR";
41 return "zlib error: Z_DATA_ERROR";
44 llvm_unreachable("unknown or unexpected zlib status code");
48 bool zlib::isAvailable() { return true; }
50 Error
zlib::compress(StringRef InputBuffer
,
51 SmallVectorImpl
<char> &CompressedBuffer
, int Level
) {
52 unsigned long CompressedSize
= ::compressBound(InputBuffer
.size());
53 CompressedBuffer
.reserve(CompressedSize
);
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();
64 Error
zlib::uncompress(StringRef InputBuffer
, char *UncompressedBuffer
,
65 size_t &UncompressedSize
) {
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();
75 Error
zlib::uncompress(StringRef InputBuffer
,
76 SmallVectorImpl
<char> &UncompressedBuffer
,
77 size_t UncompressedSize
) {
78 UncompressedBuffer
.resize(UncompressedSize
);
80 uncompress(InputBuffer
, UncompressedBuffer
.data(), UncompressedSize
);
81 UncompressedBuffer
.resize(UncompressedSize
);
85 uint32_t zlib::crc32(StringRef Buffer
) {
86 return ::crc32(0, (const Bytef
*)Buffer
.data(), Buffer
.size());
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");
95 Error
zlib::uncompress(StringRef InputBuffer
, char *UncompressedBuffer
,
96 size_t &UncompressedSize
) {
97 llvm_unreachable("zlib::uncompress is unavailable");
99 Error
zlib::uncompress(StringRef InputBuffer
,
100 SmallVectorImpl
<char> &UncompressedBuffer
,
101 size_t UncompressedSize
) {
102 llvm_unreachable("zlib::uncompress is unavailable");
104 uint32_t zlib::crc32(StringRef Buffer
) {
105 llvm_unreachable("zlib::crc32 is unavailable");