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 int encodeZlibCompressionLevel(zlib::CompressionLevel 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;
39 llvm_unreachable("Invalid zlib::CompressionLevel!");
42 static StringRef convertZlibCodeToString(int Code) {
45 return "zlib error: Z_MEM_ERROR";
47 return "zlib error: Z_BUF_ERROR";
49 return "zlib error: Z_STREAM_ERROR";
51 return "zlib error: Z_DATA_ERROR";
54 llvm_unreachable("unknown or unexpected zlib status code");
58 bool zlib::isAvailable() { return true; }
60 Error zlib::compress(StringRef InputBuffer,
61 SmallVectorImpl<char> &CompressedBuffer,
62 CompressionLevel Level) {
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(),
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();
76 Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer,
77 size_t &UncompressedSize) {
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();
87 Error zlib::uncompress(StringRef InputBuffer,
88 SmallVectorImpl<char> &UncompressedBuffer,
89 size_t UncompressedSize) {
90 UncompressedBuffer.resize(UncompressedSize);
92 uncompress(InputBuffer, UncompressedBuffer.data(), UncompressedSize);
93 UncompressedBuffer.resize(UncompressedSize);
97 uint32_t zlib::crc32(StringRef Buffer) {
98 return ::crc32(0, (const Bytef *)Buffer.data(), Buffer.size());
102 bool zlib::isAvailable() { return false; }
103 Error zlib::compress(StringRef InputBuffer,
104 SmallVectorImpl<char> &CompressedBuffer,
105 CompressionLevel Level) {
106 llvm_unreachable("zlib::compress is unavailable");
108 Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer,
109 size_t &UncompressedSize) {
110 llvm_unreachable("zlib::uncompress is unavailable");
112 Error zlib::uncompress(StringRef InputBuffer,
113 SmallVectorImpl<char> &UncompressedBuffer,
114 size_t UncompressedSize) {
115 llvm_unreachable("zlib::uncompress is unavailable");
117 uint32_t zlib::crc32(StringRef Buffer) {
118 llvm_unreachable("zlib::crc32 is unavailable");