17#include "llvm/Config/config.h"
45 unsigned CMF =
Input[0];
46 unsigned FLG =
Input[1];
47 if ((CMF & 0x0f) != 8 || (CMF >> 4) > 7)
49 return (CMF * 256 + FLG) % 31 == 0;
54 static constexpr uint8_t Magic[] = {0x28, 0xb5, 0x2f, 0xfd};
72 return "LLVM was not built with LLVM_ENABLE_ZLIB or did not find zlib at "
77 return "LLVM was not built with LLVM_ENABLE_ZSTD or did not find zstd at "
86 return "unknown compression format";
102 uint8_t *Output,
size_t UncompressedSize) {
114 size_t UncompressedSize) {
126 size_t UncompressedSize) {
132 size_t UncompressedSize) {
134 if (
const char *Reason =
142static StringRef convertZlibCodeToString(
int Code) {
145 return "zlib error: Z_MEM_ERROR";
147 return "zlib error: Z_BUF_ERROR";
149 return "zlib error: Z_STREAM_ERROR";
151 return "zlib error: Z_DATA_ERROR";
162 unsigned long CompressedSize = ::compressBound(
Input.size());
164 int Res = ::compress2((Bytef *)CompressedBuffer.
data(), &CompressedSize,
165 (
const Bytef *)
Input.data(),
Input.size(), Level);
166 if (Res == Z_MEM_ERROR)
172 if (CompressedSize < CompressedBuffer.
size())
173 CompressedBuffer.
truncate(CompressedSize);
177 size_t &UncompressedSize) {
178 int Res = ::uncompress((Bytef *)Output, (uLongf *)&UncompressedSize,
190 size_t UncompressedSize) {
193 if (UncompressedSize < Output.
size())
205 size_t &UncompressedSize) {
210 size_t UncompressedSize) {
224 ZSTD_CCtx *Cctx = ZSTD_createCCtx();
228 if (ZSTD_isError(ZSTD_CCtx_setParameter(
229 Cctx, ZSTD_c_enableLongDistanceMatching, EnableLdm ? 1 : 0))) {
235 ZSTD_CCtx_setParameter(Cctx, ZSTD_c_compressionLevel, Level))) {
240 unsigned long CompressedBufferSize = ZSTD_compressBound(
Input.size());
243 size_t const CompressedSize =
244 ZSTD_compress2(Cctx, CompressedBuffer.
data(), CompressedBufferSize,
249 if (ZSTD_isError(CompressedSize))
253 if (CompressedSize < CompressedBuffer.
size())
254 CompressedBuffer.
truncate(CompressedSize);
258 size_t &UncompressedSize) {
259 const size_t Res = ::ZSTD_decompress(
261 UncompressedSize = Res;
262 if (ZSTD_isError(Res))
273 size_t UncompressedSize) {
276 if (UncompressedSize < Output.
size())
289 size_t &UncompressedSize) {
294 size_t UncompressedSize) {
305static const char *convertLZMACodeToString(lzma_ret Code) {
307 case LZMA_STREAM_END:
308 return "lzma error: LZMA_STREAM_END";
310 return "lzma error: LZMA_NO_CHECK";
311 case LZMA_UNSUPPORTED_CHECK:
312 return "lzma error: LZMA_UNSUPPORTED_CHECK";
314 return "lzma error: LZMA_GET_CHECK";
316 return "lzma error: LZMA_MEM_ERROR";
317 case LZMA_MEMLIMIT_ERROR:
318 return "lzma error: LZMA_MEMLIMIT_ERROR";
319 case LZMA_FORMAT_ERROR:
320 return "lzma error: LZMA_FORMAT_ERROR";
321 case LZMA_OPTIONS_ERROR:
322 return "lzma error: LZMA_OPTIONS_ERROR";
323 case LZMA_DATA_ERROR:
324 return "lzma error: LZMA_DATA_ERROR";
326 return "lzma error: LZMA_BUF_ERROR";
327 case LZMA_PROG_ERROR:
328 return "lzma error: LZMA_PROG_ERROR";
336 if (
Input.size() < LZMA_STREAM_HEADER_SIZE)
338 "size of xz-compressed blob (%zu bytes) is smaller than the "
339 "LZMA_STREAM_HEADER_SIZE (%zu bytes)",
340 Input.size(),
size_t(LZMA_STREAM_HEADER_SIZE));
343 lzma_stream_flags FooterFlags{};
344 lzma_ret Ret = lzma_stream_footer_decode(
345 &FooterFlags,
Input.take_back(LZMA_STREAM_HEADER_SIZE).data());
348 convertLZMACodeToString(Ret));
351 uint64_t MinSize = FooterFlags.backward_size + 2 * LZMA_STREAM_HEADER_SIZE;
352 if (
Input.size() < MinSize)
354 "xz-compressed buffer size (%zu bytes) too small (required at "
355 "least %" PRIu64
" bytes)",
356 Input.size(), MinSize);
360 lzma_index *Index =
nullptr;
364 Ret = lzma_index_buffer_decode(
365 &Index, &MemLimit,
nullptr,
366 Input.take_back(LZMA_STREAM_HEADER_SIZE + FooterFlags.backward_size)
368 &InPos,
Input.size());
371 convertLZMACodeToString(Ret));
373 return lzma_index_uncompressed_size(Index);
382 if (!UncompressedSize)
385 if (*UncompressedSize > std::numeric_limits<size_t>::max())
387 " bytes) exceeds addressable memory",
392 Output.
resize(
static_cast<size_t>(*UncompressedSize));
396 lzma_ret Ret = lzma_stream_buffer_decode(
397 &MemLimit, 0,
nullptr,
Input.data(), &InPos,
Input.size(),
398 Output.
data(), &OutPos, Output.
size());
399 if (Ret != LZMA_OK) {
402 convertLZMACodeToString(Ret));
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define __msan_unpoison(p, size)
static bool isZstdMagic(ArrayRef< uint8_t > Input)
static bool isZlibHeader(ArrayRef< uint8_t > Input)
static std::optional< Format > identifyFormat(ArrayRef< uint8_t > Input)
This file defines the scope_exit class, which executes user-defined cleanup logic at scope exit.
This file defines the SmallVector class.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void resize_for_overwrite(size_type N)
Like resize, but T is POD, the new values won't be initialized.
void truncate(size_type N)
Like resize, but requires that N is less than size().
pointer data()
Return a pointer to the vector's buffer, even if empty().
Represent a constant reference to a string, i.e.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI bool isAvailable()
Return true if LLVM was built with LZMA support (LLVM_ENABLE_LZMA).
LLVM_ABI Error decompress(ArrayRef< uint8_t > Input, SmallVectorImpl< uint8_t > &Output)
Decompress an xz stream.
LLVM_ABI void compress(ArrayRef< uint8_t > Input, SmallVectorImpl< uint8_t > &CompressedBuffer, int Level=DefaultCompression)
LLVM_ABI Error decompress(ArrayRef< uint8_t > Input, uint8_t *Output, size_t &UncompressedSize)
LLVM_ABI bool isAvailable()
LLVM_ABI Error decompress(ArrayRef< uint8_t > Input, uint8_t *Output, size_t &UncompressedSize)
LLVM_ABI bool isAvailable()
LLVM_ABI void compress(ArrayRef< uint8_t > Input, SmallVectorImpl< uint8_t > &CompressedBuffer, int Level=DefaultCompression, bool EnableLdm=false)
LLVM_ABI const char * getReasonIfUnsupported(Format F)
LLVM_ABI Error decompress(DebugCompressionType T, ArrayRef< uint8_t > Input, uint8_t *Output, size_t UncompressedSize)
Format formatFor(DebugCompressionType Type)
LLVM_ABI void compress(Params P, ArrayRef< uint8_t > Input, SmallVectorImpl< uint8_t > &Output)
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
ArrayRef(const T &OneElt) -> ArrayRef< T >
LogicalResult success(bool IsSuccess=true)
Utility function to generate a LogicalResult.
LLVM_ABI void report_bad_alloc_error(const char *Reason, bool GenCrashDiag=true)
Reports a bad alloc error, calling any user defined bad alloc error handler.