2013-04-23 08:28:39 +00:00
|
|
|
//===--- Compression.cpp - Compression implementation ---------------------===//
|
|
|
|
//
|
2019-01-19 08:50:56 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-04-23 08:28:39 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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"
|
2020-04-30 13:07:13 -07:00
|
|
|
#if LLVM_ENABLE_ZLIB
|
2013-04-23 08:28:39 +00:00
|
|
|
#include <zlib.h>
|
|
|
|
#endif
|
2022-07-19 10:54:35 -07:00
|
|
|
#if LLVM_ENABLE_ZSTD
|
|
|
|
#include <zstd.h>
|
|
|
|
#endif
|
2013-04-23 08:28:39 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
2022-07-08 11:19:05 -07:00
|
|
|
using namespace llvm::compression;
|
2013-04-23 08:28:39 +00:00
|
|
|
|
2022-09-08 00:58:54 -07:00
|
|
|
const char *compression::getReasonIfUnsupported(compression::Format F) {
|
|
|
|
switch (F) {
|
|
|
|
case compression::Format::Zlib:
|
|
|
|
if (zlib::isAvailable())
|
|
|
|
return nullptr;
|
|
|
|
return "LLVM was not built with LLVM_ENABLE_ZLIB or did not find zlib at "
|
|
|
|
"build time";
|
|
|
|
case compression::Format::Zstd:
|
|
|
|
if (zstd::isAvailable())
|
|
|
|
return nullptr;
|
|
|
|
return "LLVM was not built with LLVM_ENABLE_ZSTD or did not find zstd at "
|
|
|
|
"build time";
|
|
|
|
}
|
|
|
|
llvm_unreachable("");
|
|
|
|
}
|
|
|
|
|
|
|
|
void compression::compress(Params P, ArrayRef<uint8_t> Input,
|
|
|
|
SmallVectorImpl<uint8_t> &Output) {
|
|
|
|
switch (P.format) {
|
|
|
|
case compression::Format::Zlib:
|
|
|
|
zlib::compress(Input, Output, P.level);
|
|
|
|
break;
|
|
|
|
case compression::Format::Zstd:
|
|
|
|
zstd::compress(Input, Output, P.level);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Error compression::decompress(compression::Format F, ArrayRef<uint8_t> Input,
|
|
|
|
SmallVectorImpl<uint8_t> &Output,
|
|
|
|
size_t UncompressedSize) {
|
|
|
|
switch (F) {
|
|
|
|
case compression::Format::Zlib:
|
|
|
|
return zlib::uncompress(Input, Output, UncompressedSize);
|
|
|
|
case compression::Format::Zstd:
|
|
|
|
return zstd::uncompress(Input, Output, UncompressedSize);
|
|
|
|
}
|
|
|
|
llvm_unreachable("");
|
|
|
|
}
|
|
|
|
|
|
|
|
Error compression::decompress(DebugCompressionType T, ArrayRef<uint8_t> Input,
|
|
|
|
SmallVectorImpl<uint8_t> &Output,
|
|
|
|
size_t UncompressedSize) {
|
|
|
|
return decompress(formatFor(T), Input, Output, UncompressedSize);
|
|
|
|
}
|
|
|
|
|
2020-04-30 13:07:13 -07:00
|
|
|
#if LLVM_ENABLE_ZLIB
|
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
|
|
|
|
2022-07-13 16:26:54 -07:00
|
|
|
void zlib::compress(ArrayRef<uint8_t> Input,
|
|
|
|
SmallVectorImpl<uint8_t> &CompressedBuffer, int Level) {
|
|
|
|
unsigned long CompressedSize = ::compressBound(Input.size());
|
2021-12-08 16:18:00 -08:00
|
|
|
CompressedBuffer.resize_for_overwrite(CompressedSize);
|
2022-07-13 16:26:54 -07:00
|
|
|
int Res = ::compress2((Bytef *)CompressedBuffer.data(), &CompressedSize,
|
|
|
|
(const Bytef *)Input.data(), Input.size(), Level);
|
2022-03-14 11:38:04 -07:00
|
|
|
if (Res == Z_MEM_ERROR)
|
|
|
|
report_bad_alloc_error("Allocation failed");
|
|
|
|
assert(Res == Z_OK);
|
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);
|
2022-07-13 15:08:40 -07:00
|
|
|
if (CompressedSize < CompressedBuffer.size())
|
|
|
|
CompressedBuffer.truncate(CompressedSize);
|
2013-04-23 08:28:39 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 16:26:54 -07:00
|
|
|
Error zlib::uncompress(ArrayRef<uint8_t> Input, uint8_t *UncompressedBuffer,
|
2017-01-17 15:45:07 +00:00
|
|
|
size_t &UncompressedSize) {
|
|
|
|
int Res =
|
2016-09-09 19:32:36 +00:00
|
|
|
::uncompress((Bytef *)UncompressedBuffer, (uLongf *)&UncompressedSize,
|
2022-07-13 16:26:54 -07:00
|
|
|
(const Bytef *)Input.data(), Input.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);
|
2022-07-08 11:19:05 -07:00
|
|
|
return Res ? make_error<StringError>(convertZlibCodeToString(Res),
|
|
|
|
inconvertibleErrorCode())
|
|
|
|
: Error::success();
|
2016-09-09 19:32:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 16:26:54 -07:00
|
|
|
Error zlib::uncompress(ArrayRef<uint8_t> Input,
|
|
|
|
SmallVectorImpl<uint8_t> &UncompressedBuffer,
|
2017-01-17 15:45:07 +00:00
|
|
|
size_t UncompressedSize) {
|
2021-12-08 16:18:00 -08:00
|
|
|
UncompressedBuffer.resize_for_overwrite(UncompressedSize);
|
2022-07-13 16:26:54 -07:00
|
|
|
Error E =
|
|
|
|
zlib::uncompress(Input, UncompressedBuffer.data(), UncompressedSize);
|
2022-07-13 15:08:40 -07:00
|
|
|
if (UncompressedSize < UncompressedBuffer.size())
|
|
|
|
UncompressedBuffer.truncate(UncompressedSize);
|
2017-01-17 15:45:07 +00:00
|
|
|
return E;
|
2013-04-23 08:28:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
bool zlib::isAvailable() { return false; }
|
2022-07-13 16:47:35 -07:00
|
|
|
void zlib::compress(ArrayRef<uint8_t> Input,
|
|
|
|
SmallVectorImpl<uint8_t> &CompressedBuffer, int Level) {
|
2017-01-17 15:45:07 +00:00
|
|
|
llvm_unreachable("zlib::compress is unavailable");
|
2013-04-23 08:28:39 +00:00
|
|
|
}
|
2022-07-13 16:47:35 -07:00
|
|
|
Error zlib::uncompress(ArrayRef<uint8_t> Input, uint8_t *UncompressedBuffer,
|
2017-01-17 15:45:07 +00:00
|
|
|
size_t &UncompressedSize) {
|
|
|
|
llvm_unreachable("zlib::uncompress is unavailable");
|
2016-09-12 13:00:51 +00:00
|
|
|
}
|
2022-07-13 16:47:35 -07:00
|
|
|
Error zlib::uncompress(ArrayRef<uint8_t> Input,
|
|
|
|
SmallVectorImpl<uint8_t> &UncompressedBuffer,
|
2017-01-17 15:45:07 +00:00
|
|
|
size_t UncompressedSize) {
|
|
|
|
llvm_unreachable("zlib::uncompress is unavailable");
|
2013-04-23 08:28:39 +00:00
|
|
|
}
|
|
|
|
#endif
|
2022-07-19 10:54:35 -07:00
|
|
|
|
|
|
|
#if LLVM_ENABLE_ZSTD
|
|
|
|
|
|
|
|
bool zstd::isAvailable() { return true; }
|
|
|
|
|
|
|
|
void zstd::compress(ArrayRef<uint8_t> Input,
|
|
|
|
SmallVectorImpl<uint8_t> &CompressedBuffer, int Level) {
|
|
|
|
unsigned long CompressedBufferSize = ::ZSTD_compressBound(Input.size());
|
|
|
|
CompressedBuffer.resize_for_overwrite(CompressedBufferSize);
|
|
|
|
unsigned long CompressedSize =
|
|
|
|
::ZSTD_compress((char *)CompressedBuffer.data(), CompressedBufferSize,
|
|
|
|
(const char *)Input.data(), Input.size(), Level);
|
|
|
|
if (ZSTD_isError(CompressedSize))
|
|
|
|
report_bad_alloc_error("Allocation failed");
|
|
|
|
// Tell MemorySanitizer that zstd output buffer is fully initialized.
|
|
|
|
// This avoids a false report when running LLVM with uninstrumented ZLib.
|
|
|
|
__msan_unpoison(CompressedBuffer.data(), CompressedSize);
|
|
|
|
if (CompressedSize < CompressedBuffer.size())
|
|
|
|
CompressedBuffer.truncate(CompressedSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
Error zstd::uncompress(ArrayRef<uint8_t> Input, uint8_t *UncompressedBuffer,
|
|
|
|
size_t &UncompressedSize) {
|
|
|
|
const size_t Res =
|
|
|
|
::ZSTD_decompress(UncompressedBuffer, UncompressedSize,
|
|
|
|
(const uint8_t *)Input.data(), Input.size());
|
|
|
|
UncompressedSize = Res;
|
|
|
|
// Tell MemorySanitizer that zstd output buffer is fully initialized.
|
|
|
|
// This avoids a false report when running LLVM with uninstrumented ZLib.
|
|
|
|
__msan_unpoison(UncompressedBuffer, UncompressedSize);
|
|
|
|
return ZSTD_isError(Res) ? make_error<StringError>(ZSTD_getErrorName(Res),
|
|
|
|
inconvertibleErrorCode())
|
|
|
|
: Error::success();
|
|
|
|
}
|
|
|
|
|
|
|
|
Error zstd::uncompress(ArrayRef<uint8_t> Input,
|
|
|
|
SmallVectorImpl<uint8_t> &UncompressedBuffer,
|
|
|
|
size_t UncompressedSize) {
|
|
|
|
UncompressedBuffer.resize_for_overwrite(UncompressedSize);
|
|
|
|
Error E =
|
|
|
|
zstd::uncompress(Input, UncompressedBuffer.data(), UncompressedSize);
|
|
|
|
if (UncompressedSize < UncompressedBuffer.size())
|
|
|
|
UncompressedBuffer.truncate(UncompressedSize);
|
|
|
|
return E;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
bool zstd::isAvailable() { return false; }
|
|
|
|
void zstd::compress(ArrayRef<uint8_t> Input,
|
|
|
|
SmallVectorImpl<uint8_t> &CompressedBuffer, int Level) {
|
|
|
|
llvm_unreachable("zstd::compress is unavailable");
|
|
|
|
}
|
|
|
|
Error zstd::uncompress(ArrayRef<uint8_t> Input, uint8_t *UncompressedBuffer,
|
|
|
|
size_t &UncompressedSize) {
|
|
|
|
llvm_unreachable("zstd::uncompress is unavailable");
|
|
|
|
}
|
|
|
|
Error zstd::uncompress(ArrayRef<uint8_t> Input,
|
|
|
|
SmallVectorImpl<uint8_t> &UncompressedBuffer,
|
|
|
|
size_t UncompressedSize) {
|
|
|
|
llvm_unreachable("zstd::uncompress is unavailable");
|
|
|
|
}
|
|
|
|
#endif
|