2013-04-23 08:28:39 +00:00
|
|
|
//===--- Compression.cpp - Compression implementation ---------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements compression functions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Support/Compression.h"
|
2015-03-23 18:07:13 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2013-04-23 08:28:39 +00:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/Config/config.h"
|
2013-04-23 12:17:46 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2017-01-17 15:45:07 +00:00
|
|
|
#include "llvm/Support/Error.h"
|
2013-04-23 08:28:39 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#if LLVM_ENABLE_ZLIB == 1 && HAVE_ZLIB_H
|
|
|
|
#include <zlib.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2013-04-23 08:57:30 +00:00
|
|
|
#if LLVM_ENABLE_ZLIB == 1 && HAVE_LIBZ
|
2017-01-17 15:45:07 +00:00
|
|
|
static Error createError(StringRef Err) {
|
|
|
|
return make_error<StringError>(Err, inconvertibleErrorCode());
|
|
|
|
}
|
|
|
|
|
2013-04-23 08:28:39 +00:00
|
|
|
static int encodeZlibCompressionLevel(zlib::CompressionLevel Level) {
|
|
|
|
switch (Level) {
|
|
|
|
case zlib::NoCompression: return 0;
|
|
|
|
case zlib::BestSpeedCompression: return 1;
|
|
|
|
case zlib::DefaultCompression: return Z_DEFAULT_COMPRESSION;
|
|
|
|
case zlib::BestSizeCompression: return 9;
|
|
|
|
}
|
2013-04-23 10:12:16 +00:00
|
|
|
llvm_unreachable("Invalid zlib::CompressionLevel!");
|
2013-04-23 08:28:39 +00:00
|
|
|
}
|
|
|
|
|
2017-01-17 15:45:07 +00:00
|
|
|
static StringRef convertZlibCodeToString(int Code) {
|
|
|
|
switch (Code) {
|
|
|
|
case Z_MEM_ERROR:
|
|
|
|
return "zlib error: Z_MEM_ERROR";
|
|
|
|
case Z_BUF_ERROR:
|
|
|
|
return "zlib error: Z_BUF_ERROR";
|
|
|
|
case Z_STREAM_ERROR:
|
|
|
|
return "zlib error: Z_STREAM_ERROR";
|
|
|
|
case Z_DATA_ERROR:
|
|
|
|
return "zlib error: Z_DATA_ERROR";
|
|
|
|
case Z_OK:
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unknown or unexpected zlib status code");
|
2013-04-23 08:28:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool zlib::isAvailable() { return true; }
|
2017-01-17 15:45:07 +00:00
|
|
|
|
|
|
|
Error zlib::compress(StringRef InputBuffer,
|
|
|
|
SmallVectorImpl<char> &CompressedBuffer,
|
|
|
|
CompressionLevel Level) {
|
2013-04-23 08:28:39 +00:00
|
|
|
unsigned long CompressedSize = ::compressBound(InputBuffer.size());
|
2014-04-05 21:53:04 +00:00
|
|
|
CompressedBuffer.resize(CompressedSize);
|
2013-04-23 08:28:39 +00:00
|
|
|
int CLevel = encodeZlibCompressionLevel(Level);
|
2017-01-17 15:45:07 +00:00
|
|
|
int Res = ::compress2((Bytef *)CompressedBuffer.data(), &CompressedSize,
|
|
|
|
(const Bytef *)InputBuffer.data(), InputBuffer.size(),
|
|
|
|
CLevel);
|
2014-11-25 15:24:07 +00:00
|
|
|
// Tell MemorySanitizer that zlib output buffer is fully initialized.
|
|
|
|
// This avoids a false report when running LLVM with uninstrumented ZLib.
|
|
|
|
__msan_unpoison(CompressedBuffer.data(), CompressedSize);
|
2014-04-05 21:53:04 +00:00
|
|
|
CompressedBuffer.resize(CompressedSize);
|
2017-01-17 15:45:07 +00:00
|
|
|
return Res ? createError(convertZlibCodeToString(Res)) : Error::success();
|
2013-04-23 08:28:39 +00:00
|
|
|
}
|
|
|
|
|
2017-01-17 15:45:07 +00:00
|
|
|
Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer,
|
|
|
|
size_t &UncompressedSize) {
|
|
|
|
int Res =
|
2016-09-09 19:32:36 +00:00
|
|
|
::uncompress((Bytef *)UncompressedBuffer, (uLongf *)&UncompressedSize,
|
2017-01-17 15:45:07 +00:00
|
|
|
(const Bytef *)InputBuffer.data(), InputBuffer.size());
|
2016-09-09 19:32:36 +00:00
|
|
|
// Tell MemorySanitizer that zlib output buffer is fully initialized.
|
|
|
|
// This avoids a false report when running LLVM with uninstrumented ZLib.
|
|
|
|
__msan_unpoison(UncompressedBuffer, UncompressedSize);
|
2017-01-17 15:45:07 +00:00
|
|
|
return Res ? createError(convertZlibCodeToString(Res)) : Error::success();
|
2016-09-09 19:32:36 +00:00
|
|
|
}
|
|
|
|
|
2017-01-17 15:45:07 +00:00
|
|
|
Error zlib::uncompress(StringRef InputBuffer,
|
|
|
|
SmallVectorImpl<char> &UncompressedBuffer,
|
|
|
|
size_t UncompressedSize) {
|
2014-04-05 21:26:44 +00:00
|
|
|
UncompressedBuffer.resize(UncompressedSize);
|
2017-01-17 15:45:07 +00:00
|
|
|
Error E =
|
2016-09-09 19:32:36 +00:00
|
|
|
uncompress(InputBuffer, UncompressedBuffer.data(), UncompressedSize);
|
2014-04-05 21:26:44 +00:00
|
|
|
UncompressedBuffer.resize(UncompressedSize);
|
2017-01-17 15:45:07 +00:00
|
|
|
return E;
|
2013-04-23 08:28:39 +00:00
|
|
|
}
|
|
|
|
|
2013-08-14 16:03:29 +00:00
|
|
|
uint32_t zlib::crc32(StringRef Buffer) {
|
|
|
|
return ::crc32(0, (const Bytef *)Buffer.data(), Buffer.size());
|
|
|
|
}
|
|
|
|
|
2013-04-23 08:28:39 +00:00
|
|
|
#else
|
|
|
|
bool zlib::isAvailable() { return false; }
|
2017-01-17 15:45:07 +00:00
|
|
|
Error zlib::compress(StringRef InputBuffer,
|
|
|
|
SmallVectorImpl<char> &CompressedBuffer,
|
|
|
|
CompressionLevel Level) {
|
|
|
|
llvm_unreachable("zlib::compress is unavailable");
|
2013-04-23 08:28:39 +00:00
|
|
|
}
|
2017-01-17 15:45:07 +00:00
|
|
|
Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer,
|
|
|
|
size_t &UncompressedSize) {
|
|
|
|
llvm_unreachable("zlib::uncompress is unavailable");
|
2016-09-12 13:00:51 +00:00
|
|
|
}
|
2017-01-17 15:45:07 +00:00
|
|
|
Error zlib::uncompress(StringRef InputBuffer,
|
|
|
|
SmallVectorImpl<char> &UncompressedBuffer,
|
|
|
|
size_t UncompressedSize) {
|
|
|
|
llvm_unreachable("zlib::uncompress is unavailable");
|
2013-04-23 08:28:39 +00:00
|
|
|
}
|
2013-08-14 16:03:29 +00:00
|
|
|
uint32_t zlib::crc32(StringRef Buffer) {
|
|
|
|
llvm_unreachable("zlib::crc32 is unavailable");
|
|
|
|
}
|
2013-04-23 08:28:39 +00:00
|
|
|
#endif
|
|
|
|
|