blob: 1f568a9636712efcfb053c278312b91033b4ab68 [file] [log] [blame]
Marek Sokolowski96bd9232017-09-20 18:33:35 +00001//===-- WindowsResourceDumper.cpp - Windows Resource printer --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Windows resource (.res) dumper for llvm-readobj.
11//
12//===----------------------------------------------------------------------===//
13
14#include "WindowsResourceDumper.h"
15#include "Error.h"
Marek Sokolowski96bd9232017-09-20 18:33:35 +000016#include "llvm/Object/WindowsResource.h"
Marek Sokolowski16676052017-09-20 23:26:05 +000017#include "llvm/Support/ConvertUTF.h"
Marek Sokolowski96bd9232017-09-20 18:33:35 +000018#include "llvm/Support/ScopedPrinter.h"
19
20namespace llvm {
21namespace object {
22namespace WindowsRes {
23
24std::string stripUTF16(const ArrayRef<UTF16> &UTF16Str) {
25 std::string Result;
26 Result.reserve(UTF16Str.size());
27
28 for (UTF16 Ch : UTF16Str) {
Marek Sokolowskibf41ef72017-09-20 23:07:39 +000029 // UTF16Str will have swapped byte order in case of big-endian machines.
30 // Swap it back in such a case.
Marek Sokolowski1481a4e2017-09-21 20:36:38 +000031 uint16_t ChValue = support::endian::byte_swap(Ch, support::little);
Marek Sokolowskibf41ef72017-09-20 23:07:39 +000032 if (ChValue <= 0xFF)
33 Result += ChValue;
Marek Sokolowski96bd9232017-09-20 18:33:35 +000034 else
35 Result += '?';
36 }
37 return Result;
38}
39
40Error Dumper::printData() {
41 auto EntryPtrOrErr = WinRes->getHeadEntry();
42 if (!EntryPtrOrErr)
43 return EntryPtrOrErr.takeError();
44 auto EntryPtr = *EntryPtrOrErr;
45
46 bool IsEnd = false;
47 while (!IsEnd) {
48 printEntry(EntryPtr);
49
50 if (auto Err = EntryPtr.moveNext(IsEnd))
51 return Err;
52 }
53 return Error::success();
54}
55
56void Dumper::printEntry(const ResourceEntryRef &Ref) {
57 if (Ref.checkTypeString()) {
58 auto NarrowStr = stripUTF16(Ref.getTypeString());
59 SW.printString("Resource type (string)", NarrowStr);
60 } else
61 SW.printNumber("Resource type (int)", Ref.getTypeID());
62
63 if (Ref.checkNameString()) {
64 auto NarrowStr = stripUTF16(Ref.getNameString());
65 SW.printString("Resource name (string)", NarrowStr);
66 } else
67 SW.printNumber("Resource name (int)", Ref.getNameID());
68
69 SW.printNumber("Data version", Ref.getDataVersion());
70 SW.printHex("Memory flags", Ref.getMemoryFlags());
71 SW.printNumber("Language ID", Ref.getLanguage());
72 SW.printNumber("Version (major)", Ref.getMajorVersion());
73 SW.printNumber("Version (minor)", Ref.getMinorVersion());
74 SW.printNumber("Characteristics", Ref.getCharacteristics());
Marek Sokolowskia656eb82017-09-20 21:03:37 +000075 SW.printNumber("Data size", (uint64_t)Ref.getData().size());
Marek Sokolowski96bd9232017-09-20 18:33:35 +000076 SW.printBinary("Data:", Ref.getData());
77 SW.startLine() << "\n";
78}
79
80} // namespace WindowsRes
81} // namespace object
82} // namespace llvm