1//===-- WindowsResourceDumper.cpp - Windows Resource printer --------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the Windows resource (.res) dumper for llvm-readobj.
10//
11//===----------------------------------------------------------------------===//
12
13#include "WindowsResourceDumper.h"
14#include "llvm/Object/WindowsResource.h"
15#include "llvm/Support/ConvertUTF.h"
16#include "llvm/Support/ScopedPrinter.h"
17
18namespace llvm {
19namespace object {
20namespace WindowsRes {
21
22std::string stripUTF16(const ArrayRef<UTF16> &UTF16Str) {
23 std::string Result;
24 Result.reserve(res: UTF16Str.size());
25
26 for (UTF16 Ch : UTF16Str) {
27 // UTF16Str will have swapped byte order in case of big-endian machines.
28 // Swap it back in such a case.
29 uint16_t ChValue = support::endian::byte_swap(value: Ch, endian: llvm::endianness::little);
30 if (ChValue <= 0xFF)
31 Result += ChValue;
32 else
33 Result += '?';
34 }
35 return Result;
36}
37
38Error Dumper::printData() {
39 auto EntryPtrOrErr = WinRes->getHeadEntry();
40 if (!EntryPtrOrErr)
41 return EntryPtrOrErr.takeError();
42 auto EntryPtr = *EntryPtrOrErr;
43
44 bool IsEnd = false;
45 while (!IsEnd) {
46 printEntry(Ref: EntryPtr);
47
48 if (auto Err = EntryPtr.moveNext(End&: IsEnd))
49 return Err;
50 }
51 return Error::success();
52}
53
54void Dumper::printEntry(const ResourceEntryRef &Ref) {
55 if (Ref.checkTypeString()) {
56 auto NarrowStr = stripUTF16(UTF16Str: Ref.getTypeString());
57 SW.printString(Label: "Resource type (string)", Value: NarrowStr);
58 } else {
59 SmallString<20> IDStr;
60 raw_svector_ostream OS(IDStr);
61 printResourceTypeName(TypeID: Ref.getTypeID(), OS);
62 SW.printString(Label: "Resource type (int)", Value: IDStr);
63 }
64
65 if (Ref.checkNameString()) {
66 auto NarrowStr = stripUTF16(UTF16Str: Ref.getNameString());
67 SW.printString(Label: "Resource name (string)", Value: NarrowStr);
68 } else
69 SW.printNumber(Label: "Resource name (int)", Value: Ref.getNameID());
70
71 SW.printNumber(Label: "Data version", Value: Ref.getDataVersion());
72 SW.printHex(Label: "Memory flags", Value: Ref.getMemoryFlags());
73 SW.printNumber(Label: "Language ID", Value: Ref.getLanguage());
74 SW.printNumber(Label: "Version (major)", Value: Ref.getMajorVersion());
75 SW.printNumber(Label: "Version (minor)", Value: Ref.getMinorVersion());
76 SW.printNumber(Label: "Characteristics", Value: Ref.getCharacteristics());
77 SW.printNumber(Label: "Data size", Value: (uint64_t)Ref.getData().size());
78 SW.printBinary(Label: "Data:", Value: Ref.getData());
79 SW.startLine() << "\n";
80}
81
82} // namespace WindowsRes
83} // namespace object
84} // namespace llvm
85

source code of llvm/tools/llvm-readobj/WindowsResourceDumper.cpp