2017-01-11 23:26:41 +08:00
|
|
|
//===-- Decompressor.cpp --------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08: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
|
2017-01-11 23:26:41 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Object/Decompressor.h"
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/ELF.h"
|
2022-02-11 01:39:06 +08:00
|
|
|
#include "llvm/Object/ObjectFile.h"
|
2017-01-11 23:26:41 +08:00
|
|
|
#include "llvm/Support/Compression.h"
|
|
|
|
#include "llvm/Support/DataExtractor.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/Support/Endian.h"
|
2017-01-11 23:26:41 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::support::endian;
|
|
|
|
using namespace object;
|
|
|
|
|
|
|
|
Expected<Decompressor> Decompressor::create(StringRef Name, StringRef Data,
|
|
|
|
bool IsLE, bool Is64Bit) {
|
2022-07-09 02:19:05 +08:00
|
|
|
if (!compression::zlib::isAvailable())
|
2017-01-11 23:26:41 +08:00
|
|
|
return createError("zlib is not available");
|
|
|
|
|
2017-09-06 06:04:00 +08:00
|
|
|
Decompressor D(Data);
|
2022-07-15 07:19:32 +08:00
|
|
|
if (Error Err = D.consumeCompressedZLibHeader(Is64Bit, IsLE))
|
2020-02-10 23:06:45 +08:00
|
|
|
return std::move(Err);
|
2017-01-11 23:26:41 +08:00
|
|
|
return D;
|
|
|
|
}
|
|
|
|
|
2017-09-06 06:04:00 +08:00
|
|
|
Decompressor::Decompressor(StringRef Data)
|
|
|
|
: SectionData(Data), DecompressedSize(0) {}
|
2017-01-11 23:26:41 +08:00
|
|
|
|
|
|
|
Error Decompressor::consumeCompressedZLibHeader(bool Is64Bit,
|
|
|
|
bool IsLittleEndian) {
|
|
|
|
using namespace ELF;
|
|
|
|
uint64_t HdrSize = Is64Bit ? sizeof(Elf64_Chdr) : sizeof(Elf32_Chdr);
|
|
|
|
if (SectionData.size() < HdrSize)
|
|
|
|
return createError("corrupted compressed section header");
|
|
|
|
|
|
|
|
DataExtractor Extractor(SectionData, IsLittleEndian, 0);
|
2019-08-06 18:49:40 +08:00
|
|
|
uint64_t Offset = 0;
|
2017-01-11 23:26:41 +08:00
|
|
|
if (Extractor.getUnsigned(&Offset, Is64Bit ? sizeof(Elf64_Word)
|
|
|
|
: sizeof(Elf32_Word)) !=
|
|
|
|
ELFCOMPRESS_ZLIB)
|
|
|
|
return createError("unsupported compression type");
|
|
|
|
|
|
|
|
// Skip Elf64_Chdr::ch_reserved field.
|
|
|
|
if (Is64Bit)
|
|
|
|
Offset += sizeof(Elf64_Word);
|
|
|
|
|
|
|
|
DecompressedSize = Extractor.getUnsigned(
|
|
|
|
&Offset, Is64Bit ? sizeof(Elf64_Xword) : sizeof(Elf32_Word));
|
|
|
|
SectionData = SectionData.substr(HdrSize);
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2022-07-14 07:26:54 +08:00
|
|
|
Error Decompressor::decompress(MutableArrayRef<uint8_t> Buffer) {
|
2017-01-11 23:26:41 +08:00
|
|
|
size_t Size = Buffer.size();
|
2022-07-14 07:26:54 +08:00
|
|
|
return compression::zlib::uncompress(arrayRefFromStringRef(SectionData),
|
|
|
|
Buffer.data(), Size);
|
2017-01-11 23:26:41 +08:00
|
|
|
}
|