1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
//===--- Compression.cpp - Compression implementation ---------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file implements compression functions.
//
//===----------------------------------------------------------------------===//
#include "llvm/Support/Compression.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Config/config.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
#if LLVM_ENABLE_ZLIB
#include <zlib.h>
#endif
#if LLVM_ENABLE_ZSTD
#include <zstd.h>
#endif
using namespace llvm;
using namespace llvm::compression;
#if LLVM_ENABLE_ZLIB
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");
}
}
bool zlib::isAvailable() { return true; }
void zlib::compress(ArrayRef<uint8_t> Input,
SmallVectorImpl<uint8_t> &CompressedBuffer, int Level) {
unsigned long CompressedSize = ::compressBound(Input.size());
CompressedBuffer.resize_for_overwrite(CompressedSize);
int Res = ::compress2((Bytef *)CompressedBuffer.data(), &CompressedSize,
(const Bytef *)Input.data(), Input.size(), Level);
if (Res == Z_MEM_ERROR)
report_bad_alloc_error("Allocation failed");
assert(Res == Z_OK);
// 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);
if (CompressedSize < CompressedBuffer.size())
CompressedBuffer.truncate(CompressedSize);
}
Error zlib::uncompress(ArrayRef<uint8_t> Input, uint8_t *UncompressedBuffer,
size_t &UncompressedSize) {
int Res =
::uncompress((Bytef *)UncompressedBuffer, (uLongf *)&UncompressedSize,
(const Bytef *)Input.data(), Input.size());
// Tell MemorySanitizer that zlib output buffer is fully initialized.
// This avoids a false report when running LLVM with uninstrumented ZLib.
__msan_unpoison(UncompressedBuffer, UncompressedSize);
return Res ? make_error<StringError>(convertZlibCodeToString(Res),
inconvertibleErrorCode())
: Error::success();
}
Error zlib::uncompress(ArrayRef<uint8_t> Input,
SmallVectorImpl<uint8_t> &UncompressedBuffer,
size_t UncompressedSize) {
UncompressedBuffer.resize_for_overwrite(UncompressedSize);
Error E =
zlib::uncompress(Input, UncompressedBuffer.data(), UncompressedSize);
if (UncompressedSize < UncompressedBuffer.size())
UncompressedBuffer.truncate(UncompressedSize);
return E;
}
#else
bool zlib::isAvailable() { return false; }
void zlib::compress(ArrayRef<uint8_t> Input,
SmallVectorImpl<uint8_t> &CompressedBuffer, int Level) {
llvm_unreachable("zlib::compress is unavailable");
}
Error zlib::uncompress(ArrayRef<uint8_t> Input, uint8_t *UncompressedBuffer,
size_t &UncompressedSize) {
llvm_unreachable("zlib::uncompress is unavailable");
}
Error zlib::uncompress(ArrayRef<uint8_t> Input,
SmallVectorImpl<uint8_t> &UncompressedBuffer,
size_t UncompressedSize) {
llvm_unreachable("zlib::uncompress is unavailable");
}
#endif
#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
|