2013-04-23 16:28:39 +08: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"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/Config/config.h"
|
2013-04-23 20:17:46 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2013-04-23 16:28:39 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#if LLVM_ENABLE_ZLIB == 1 && HAVE_ZLIB_H
|
|
|
|
#include <zlib.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2013-04-23 16:57:30 +08:00
|
|
|
#if LLVM_ENABLE_ZLIB == 1 && HAVE_LIBZ
|
2013-04-23 16:28:39 +08: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 18:12:16 +08:00
|
|
|
llvm_unreachable("Invalid zlib::CompressionLevel!");
|
2013-04-23 16:28:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static zlib::Status encodeZlibReturnValue(int ReturnValue) {
|
|
|
|
switch (ReturnValue) {
|
|
|
|
case Z_OK: return zlib::StatusOK;
|
|
|
|
case Z_MEM_ERROR: return zlib::StatusOutOfMemory;
|
|
|
|
case Z_BUF_ERROR: return zlib::StatusBufferTooShort;
|
|
|
|
case Z_STREAM_ERROR: return zlib::StatusInvalidArg;
|
|
|
|
case Z_DATA_ERROR: return zlib::StatusInvalidData;
|
|
|
|
default: llvm_unreachable("unknown zlib return status!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool zlib::isAvailable() { return true; }
|
|
|
|
zlib::Status zlib::compress(StringRef InputBuffer,
|
2014-03-06 13:51:42 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> &CompressedBuffer,
|
2013-04-23 16:28:39 +08:00
|
|
|
CompressionLevel Level) {
|
|
|
|
unsigned long CompressedSize = ::compressBound(InputBuffer.size());
|
2014-03-06 13:51:42 +08:00
|
|
|
std::unique_ptr<char[]> TmpBuffer(new char[CompressedSize]);
|
2013-04-23 16:28:39 +08:00
|
|
|
int CLevel = encodeZlibCompressionLevel(Level);
|
|
|
|
Status Res = encodeZlibReturnValue(::compress2(
|
|
|
|
(Bytef *)TmpBuffer.get(), &CompressedSize,
|
|
|
|
(const Bytef *)InputBuffer.data(), InputBuffer.size(), CLevel));
|
2013-04-23 20:17:46 +08:00
|
|
|
if (Res == StatusOK) {
|
2013-04-23 16:28:39 +08:00
|
|
|
CompressedBuffer.reset(MemoryBuffer::getMemBufferCopy(
|
|
|
|
StringRef(TmpBuffer.get(), CompressedSize)));
|
2013-04-23 20:17:46 +08:00
|
|
|
// Tell MSan that memory initialized by zlib is valid.
|
2013-04-23 21:35:32 +08:00
|
|
|
__msan_unpoison(CompressedBuffer->getBufferStart(), CompressedSize);
|
2013-04-23 20:17:46 +08:00
|
|
|
}
|
2013-04-23 16:28:39 +08:00
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
|
|
|
zlib::Status zlib::uncompress(StringRef InputBuffer,
|
2014-03-06 13:51:42 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> &UncompressedBuffer,
|
2013-04-23 16:28:39 +08:00
|
|
|
size_t UncompressedSize) {
|
2014-03-06 13:51:42 +08:00
|
|
|
std::unique_ptr<char[]> TmpBuffer(new char[UncompressedSize]);
|
2013-04-23 16:28:39 +08:00
|
|
|
Status Res = encodeZlibReturnValue(
|
|
|
|
::uncompress((Bytef *)TmpBuffer.get(), (uLongf *)&UncompressedSize,
|
|
|
|
(const Bytef *)InputBuffer.data(), InputBuffer.size()));
|
2013-04-23 20:17:46 +08:00
|
|
|
if (Res == StatusOK) {
|
2013-04-23 16:28:39 +08:00
|
|
|
UncompressedBuffer.reset(MemoryBuffer::getMemBufferCopy(
|
|
|
|
StringRef(TmpBuffer.get(), UncompressedSize)));
|
2013-04-23 20:17:46 +08:00
|
|
|
// Tell MSan that memory initialized by zlib is valid.
|
2013-04-23 21:35:32 +08:00
|
|
|
__msan_unpoison(UncompressedBuffer->getBufferStart(), UncompressedSize);
|
2013-04-23 20:17:46 +08:00
|
|
|
}
|
2013-04-23 16:28:39 +08:00
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
2013-08-15 00:03:29 +08:00
|
|
|
uint32_t zlib::crc32(StringRef Buffer) {
|
|
|
|
return ::crc32(0, (const Bytef *)Buffer.data(), Buffer.size());
|
|
|
|
}
|
|
|
|
|
2013-04-23 16:28:39 +08:00
|
|
|
#else
|
|
|
|
bool zlib::isAvailable() { return false; }
|
|
|
|
zlib::Status zlib::compress(StringRef InputBuffer,
|
2014-03-06 13:51:42 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> &CompressedBuffer,
|
2013-04-23 16:28:39 +08:00
|
|
|
CompressionLevel Level) {
|
|
|
|
return zlib::StatusUnsupported;
|
|
|
|
}
|
|
|
|
zlib::Status zlib::uncompress(StringRef InputBuffer,
|
2014-03-06 13:51:42 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> &UncompressedBuffer,
|
2013-04-23 16:28:39 +08:00
|
|
|
size_t UncompressedSize) {
|
|
|
|
return zlib::StatusUnsupported;
|
|
|
|
}
|
2013-08-15 00:03:29 +08:00
|
|
|
uint32_t zlib::crc32(StringRef Buffer) {
|
|
|
|
llvm_unreachable("zlib::crc32 is unavailable");
|
|
|
|
}
|
2013-04-23 16:28:39 +08:00
|
|
|
#endif
|
|
|
|
|