Path: blob/main/contrib/llvm-project/llvm/lib/Object/Decompressor.cpp
35232 views
//===-- Decompressor.cpp --------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "llvm/Object/Decompressor.h"9#include "llvm/ADT/StringExtras.h"10#include "llvm/BinaryFormat/ELF.h"11#include "llvm/Object/ObjectFile.h"12#include "llvm/Support/Compression.h"13#include "llvm/Support/DataExtractor.h"14#include "llvm/Support/Endian.h"1516using namespace llvm;17using namespace llvm::support::endian;18using namespace object;1920Expected<Decompressor> Decompressor::create(StringRef Name, StringRef Data,21bool IsLE, bool Is64Bit) {22Decompressor D(Data);23if (Error Err = D.consumeCompressedHeader(Is64Bit, IsLE))24return std::move(Err);25return D;26}2728Decompressor::Decompressor(StringRef Data)29: SectionData(Data), DecompressedSize(0) {}3031Error Decompressor::consumeCompressedHeader(bool Is64Bit, bool IsLittleEndian) {32using namespace ELF;33uint64_t HdrSize = Is64Bit ? sizeof(Elf64_Chdr) : sizeof(Elf32_Chdr);34if (SectionData.size() < HdrSize)35return createError("corrupted compressed section header");3637DataExtractor Extractor(SectionData, IsLittleEndian, 0);38uint64_t Offset = 0;39auto ChType = Extractor.getUnsigned(&Offset, Is64Bit ? sizeof(Elf64_Word)40: sizeof(Elf32_Word));41switch (ChType) {42case ELFCOMPRESS_ZLIB:43CompressionType = DebugCompressionType::Zlib;44break;45case ELFCOMPRESS_ZSTD:46CompressionType = DebugCompressionType::Zstd;47break;48default:49return createError("unsupported compression type (" + Twine(ChType) + ")");50}51if (const char *Reason = llvm::compression::getReasonIfUnsupported(52compression::formatFor(CompressionType)))53return createError(Reason);5455// Skip Elf64_Chdr::ch_reserved field.56if (Is64Bit)57Offset += sizeof(Elf64_Word);5859DecompressedSize = Extractor.getUnsigned(60&Offset, Is64Bit ? sizeof(Elf64_Xword) : sizeof(Elf32_Word));61SectionData = SectionData.substr(HdrSize);62return Error::success();63}6465Error Decompressor::decompress(MutableArrayRef<uint8_t> Output) {66return compression::decompress(CompressionType,67arrayRefFromStringRef(SectionData),68Output.data(), Output.size());69}707172