Marek Sokolowski | 96bd923 | 2017-09-20 18:33:35 +0000 | [diff] [blame] | 1 | //===-- 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 Sokolowski | 96bd923 | 2017-09-20 18:33:35 +0000 | [diff] [blame] | 16 | #include "llvm/Object/WindowsResource.h" |
Marek Sokolowski | 1667605 | 2017-09-20 23:26:05 +0000 | [diff] [blame] | 17 | #include "llvm/Support/ConvertUTF.h" |
Marek Sokolowski | 96bd923 | 2017-09-20 18:33:35 +0000 | [diff] [blame] | 18 | #include "llvm/Support/ScopedPrinter.h" |
| 19 | |
| 20 | namespace llvm { |
| 21 | namespace object { |
| 22 | namespace WindowsRes { |
| 23 | |
| 24 | std::string stripUTF16(const ArrayRef<UTF16> &UTF16Str) { |
| 25 | std::string Result; |
| 26 | Result.reserve(UTF16Str.size()); |
| 27 | |
| 28 | for (UTF16 Ch : UTF16Str) { |
Marek Sokolowski | bf41ef7 | 2017-09-20 23:07:39 +0000 | [diff] [blame] | 29 | // UTF16Str will have swapped byte order in case of big-endian machines. |
| 30 | // Swap it back in such a case. |
Marek Sokolowski | 1481a4e | 2017-09-21 20:36:38 +0000 | [diff] [blame] | 31 | uint16_t ChValue = support::endian::byte_swap(Ch, support::little); |
Marek Sokolowski | bf41ef7 | 2017-09-20 23:07:39 +0000 | [diff] [blame] | 32 | if (ChValue <= 0xFF) |
| 33 | Result += ChValue; |
Marek Sokolowski | 96bd923 | 2017-09-20 18:33:35 +0000 | [diff] [blame] | 34 | else |
| 35 | Result += '?'; |
| 36 | } |
| 37 | return Result; |
| 38 | } |
| 39 | |
| 40 | Error 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 | |
| 56 | void 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 Sokolowski | a656eb8 | 2017-09-20 21:03:37 +0000 | [diff] [blame] | 75 | SW.printNumber("Data size", (uint64_t)Ref.getData().size()); |
Marek Sokolowski | 96bd923 | 2017-09-20 18:33:35 +0000 | [diff] [blame] | 76 | SW.printBinary("Data:", Ref.getData()); |
| 77 | SW.startLine() << "\n"; |
| 78 | } |
| 79 | |
| 80 | } // namespace WindowsRes |
| 81 | } // namespace object |
| 82 | } // namespace llvm |