Path: blob/main/contrib/llvm-project/llvm/tools/llvm-readobj/WindowsResourceDumper.cpp
35231 views
//===-- WindowsResourceDumper.cpp - Windows Resource printer --------------===//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//===----------------------------------------------------------------------===//7//8// This file implements the Windows resource (.res) dumper for llvm-readobj.9//10//===----------------------------------------------------------------------===//1112#include "WindowsResourceDumper.h"13#include "llvm/Object/WindowsResource.h"14#include "llvm/Support/ConvertUTF.h"15#include "llvm/Support/ScopedPrinter.h"1617namespace llvm {18namespace object {19namespace WindowsRes {2021std::string stripUTF16(const ArrayRef<UTF16> &UTF16Str) {22std::string Result;23Result.reserve(UTF16Str.size());2425for (UTF16 Ch : UTF16Str) {26// UTF16Str will have swapped byte order in case of big-endian machines.27// Swap it back in such a case.28uint16_t ChValue = support::endian::byte_swap(Ch, llvm::endianness::little);29if (ChValue <= 0xFF)30Result += ChValue;31else32Result += '?';33}34return Result;35}3637Error Dumper::printData() {38auto EntryPtrOrErr = WinRes->getHeadEntry();39if (!EntryPtrOrErr)40return EntryPtrOrErr.takeError();41auto EntryPtr = *EntryPtrOrErr;4243bool IsEnd = false;44while (!IsEnd) {45printEntry(EntryPtr);4647if (auto Err = EntryPtr.moveNext(IsEnd))48return Err;49}50return Error::success();51}5253void Dumper::printEntry(const ResourceEntryRef &Ref) {54if (Ref.checkTypeString()) {55auto NarrowStr = stripUTF16(Ref.getTypeString());56SW.printString("Resource type (string)", NarrowStr);57} else {58SmallString<20> IDStr;59raw_svector_ostream OS(IDStr);60printResourceTypeName(Ref.getTypeID(), OS);61SW.printString("Resource type (int)", IDStr);62}6364if (Ref.checkNameString()) {65auto NarrowStr = stripUTF16(Ref.getNameString());66SW.printString("Resource name (string)", NarrowStr);67} else68SW.printNumber("Resource name (int)", Ref.getNameID());6970SW.printNumber("Data version", Ref.getDataVersion());71SW.printHex("Memory flags", Ref.getMemoryFlags());72SW.printNumber("Language ID", Ref.getLanguage());73SW.printNumber("Version (major)", Ref.getMajorVersion());74SW.printNumber("Version (minor)", Ref.getMinorVersion());75SW.printNumber("Characteristics", Ref.getCharacteristics());76SW.printNumber("Data size", (uint64_t)Ref.getData().size());77SW.printBinary("Data:", Ref.getData());78SW.startLine() << "\n";79}8081} // namespace WindowsRes82} // namespace object83} // namespace llvm848586