WindowsResourceDumper.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #include "WindowsResourceDumper.h"
  13. #include "llvm/Object/WindowsResource.h"
  14. #include "llvm/Support/ConvertUTF.h"
  15. #include "llvm/Support/ScopedPrinter.h"
  16. namespace llvm {
  17. namespace object {
  18. namespace WindowsRes {
  19. std::string stripUTF16(const ArrayRef<UTF16> &UTF16Str) {
  20. std::string Result;
  21. Result.reserve(UTF16Str.size());
  22. for (UTF16 Ch : UTF16Str) {
  23. // UTF16Str will have swapped byte order in case of big-endian machines.
  24. // Swap it back in such a case.
  25. uint16_t ChValue = support::endian::byte_swap(Ch, support::little);
  26. if (ChValue <= 0xFF)
  27. Result += ChValue;
  28. else
  29. Result += '?';
  30. }
  31. return Result;
  32. }
  33. Error Dumper::printData() {
  34. auto EntryPtrOrErr = WinRes->getHeadEntry();
  35. if (!EntryPtrOrErr)
  36. return EntryPtrOrErr.takeError();
  37. auto EntryPtr = *EntryPtrOrErr;
  38. bool IsEnd = false;
  39. while (!IsEnd) {
  40. printEntry(EntryPtr);
  41. if (auto Err = EntryPtr.moveNext(IsEnd))
  42. return Err;
  43. }
  44. return Error::success();
  45. }
  46. void Dumper::printEntry(const ResourceEntryRef &Ref) {
  47. if (Ref.checkTypeString()) {
  48. auto NarrowStr = stripUTF16(Ref.getTypeString());
  49. SW.printString("Resource type (string)", NarrowStr);
  50. } else {
  51. SmallString<20> IDStr;
  52. raw_svector_ostream OS(IDStr);
  53. printResourceTypeName(Ref.getTypeID(), OS);
  54. SW.printString("Resource type (int)", IDStr);
  55. }
  56. if (Ref.checkNameString()) {
  57. auto NarrowStr = stripUTF16(Ref.getNameString());
  58. SW.printString("Resource name (string)", NarrowStr);
  59. } else
  60. SW.printNumber("Resource name (int)", Ref.getNameID());
  61. SW.printNumber("Data version", Ref.getDataVersion());
  62. SW.printHex("Memory flags", Ref.getMemoryFlags());
  63. SW.printNumber("Language ID", Ref.getLanguage());
  64. SW.printNumber("Version (major)", Ref.getMajorVersion());
  65. SW.printNumber("Version (minor)", Ref.getMinorVersion());
  66. SW.printNumber("Characteristics", Ref.getCharacteristics());
  67. SW.printNumber("Data size", (uint64_t)Ref.getData().size());
  68. SW.printBinary("Data:", Ref.getData());
  69. SW.startLine() << "\n";
  70. }
  71. } // namespace WindowsRes
  72. } // namespace object
  73. } // namespace llvm