LLVM  5.0.0
WindowsResource.h
Go to the documentation of this file.
1 //===-- WindowsResource.h ---------------------------------------*- C++-*-===//
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 declares the .res file class. .res files are intermediate
11 // products of the typical resource-compilation process on Windows. This
12 // process is as follows:
13 //
14 // .rc file(s) ---(rc.exe)---> .res file(s) ---(cvtres.exe)---> COFF file
15 //
16 // .rc files are human-readable scripts that list all resources a program uses.
17 //
18 // They are compiled into .res files, which are a list of the resources in
19 // binary form.
20 //
21 // Finally the data stored in the .res is compiled into a COFF file, where it
22 // is organized in a directory tree structure for optimized access by the
23 // program during runtime.
24 //
25 // Ref: msdn.microsoft.com/en-us/library/windows/desktop/ms648007(v=vs.85).aspx
26 //
27 //===---------------------------------------------------------------------===//
28 
29 #ifndef LLVM_INCLUDE_LLVM_OBJECT_RESFILE_H
30 #define LLVM_INCLUDE_LLVM_OBJECT_RESFILE_H
31 
32 #include "llvm/ADT/ArrayRef.h"
33 #include "llvm/BinaryFormat/COFF.h"
34 #include "llvm/Object/Binary.h"
35 #include "llvm/Object/Error.h"
39 #include "llvm/Support/Endian.h"
40 #include "llvm/Support/Error.h"
42 
43 #include <map>
44 
45 namespace llvm {
46 namespace object {
47 
48 class WindowsResource;
49 
50 const size_t WIN_RES_MAGIC_SIZE = 16;
51 const size_t WIN_RES_NULL_ENTRY_SIZE = 16;
54 const uint16_t WIN_RES_PURE_MOVEABLE = 0x0030;
55 
59 };
60 
61 // Type and Name may each either be an integer ID or a string. This struct is
62 // only used in the case where they are both IDs.
63 struct WinResIDs {
64  uint16_t TypeFlag;
66  uint16_t NameFlag;
68 
69  void setType(uint16_t ID) {
70  TypeFlag = 0xffff;
71  TypeID = ID;
72  }
73 
74  void setName(uint16_t ID) {
75  NameFlag = 0xffff;
76  NameID = ID;
77  }
78 };
79 
86 };
87 
89 public:
90  Error moveNext(bool &End);
91  bool checkTypeString() const { return IsStringType; }
92  ArrayRef<UTF16> getTypeString() const { return Type; }
93  uint16_t getTypeID() const { return TypeID; }
94  bool checkNameString() const { return IsStringName; }
95  ArrayRef<UTF16> getNameString() const { return Name; }
96  uint16_t getNameID() const { return NameID; }
97  uint16_t getLanguage() const { return Suffix->Language; }
98  uint16_t getMajorVersion() const { return Suffix->Version >> 16; }
99  uint16_t getMinorVersion() const { return Suffix->Version; }
100  uint32_t getCharacteristics() const { return Suffix->Characteristics; }
101  ArrayRef<uint8_t> getData() const { return Data; }
102 
103 private:
104  friend class WindowsResource;
105 
107  Error &Err);
108 
109  Error loadNext();
110 
111  BinaryStreamReader Reader;
112  bool IsStringType;
114  uint16_t TypeID;
115  bool IsStringName;
117  uint16_t NameID;
118  const WinResHeaderSuffix *Suffix = nullptr;
120  const WindowsResource *OwningRes = nullptr;
121 };
122 
123 class WindowsResource : public Binary {
124 public:
126 
127  static bool classof(const Binary *V) { return V->isWinRes(); }
128 
131 
132 private:
133  friend class ResourceEntryRef;
134 
136 
137  BinaryByteStream BBS;
138 };
139 
141 public:
142  class TreeNode;
145  void printTree(raw_ostream &OS) const;
146  const TreeNode &getTree() const { return Root; }
147  const ArrayRef<std::vector<uint8_t>> getData() const { return Data; }
149  return StringTable;
150  }
151 
152  class TreeNode {
153  public:
154  template <typename T>
155  using Children = std::map<T, std::unique_ptr<TreeNode>>;
156 
157  void print(ScopedPrinter &Writer, StringRef Name) const;
158  uint32_t getTreeSize() const;
159  uint32_t getStringIndex() const { return StringIndex; }
160  uint32_t getDataIndex() const { return DataIndex; }
161  uint16_t getMajorVersion() const { return MajorVersion; }
162  uint16_t getMinorVersion() const { return MinorVersion; }
163  uint32_t getCharacteristics() const { return Characteristics; }
164  bool checkIsDataNode() const { return IsDataNode; }
165  const Children<uint32_t> &getIDChildren() const { return IDChildren; }
167  return StringChildren;
168  }
169 
170  private:
171  friend class WindowsResourceParser;
172 
173  static uint32_t StringCount;
174  static uint32_t DataCount;
175 
176  static std::unique_ptr<TreeNode> createStringNode();
177  static std::unique_ptr<TreeNode> createIDNode();
178  static std::unique_ptr<TreeNode> createDataNode(uint16_t MajorVersion,
179  uint16_t MinorVersion,
181 
182  explicit TreeNode(bool IsStringNode);
183  TreeNode(uint16_t MajorVersion, uint16_t MinorVersion,
185 
186  void addEntry(const ResourceEntryRef &Entry, bool &IsNewTypeString,
187  bool &IsNewNameString);
188  TreeNode &addTypeNode(const ResourceEntryRef &Entry, bool &IsNewTypeString);
189  TreeNode &addNameNode(const ResourceEntryRef &Entry, bool &IsNewNameString);
190  TreeNode &addLanguageNode(const ResourceEntryRef &Entry);
191  TreeNode &addChild(uint32_t ID, bool IsDataNode = false,
192  uint16_t MajorVersion = 0, uint16_t MinorVersion = 0,
194  TreeNode &addChild(ArrayRef<UTF16> NameRef, bool &IsNewString);
195 
196  bool IsDataNode = false;
197  uint32_t StringIndex;
198  uint32_t DataIndex;
199  Children<uint32_t> IDChildren;
200  Children<std::string> StringChildren;
201  uint16_t MajorVersion = 0;
202  uint16_t MinorVersion = 0;
204  };
205 
206 private:
207  TreeNode Root;
208  std::vector<std::vector<uint8_t>> Data;
209  std::vector<std::vector<UTF16>> StringTable;
210 };
211 
215 
216 } // namespace object
217 } // namespace llvm
218 
219 #endif
const TreeNode & getTree() const
const size_t WIN_RES_NULL_ENTRY_SIZE
An implementation of BinaryStream which holds its entire data set in a single contiguous buffer...
const Children< uint32_t > & getIDChildren() const
Error parse(WindowsResource *WR)
Type::TypeID TypeID
ArrayRef< uint8_t > getData() const
const ArrayRef< std::vector< uint8_t > > getData() const
support::ulittle32_t HeaderSize
support::ulittle16_t NameID
void setName(uint16_t ID)
support::ulittle16_t TypeID
uint16_t getMajorVersion() const
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
uint16_t getMinorVersion() const
static bool classof(const Binary *V)
bool isWinRes() const
Definition: Binary.h:135
ArrayRef< UTF16 > getTypeString() const
static Expected< std::unique_ptr< WindowsResource > > createWindowsResource(MemoryBufferRef Source)
Expected< ResourceEntryRef > getHeadEntry()
const char * Name
void print(ScopedPrinter &Writer, StringRef Name) const
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
uint32_t getCharacteristics() const
static const unsigned End
const uint16_t WIN_RES_PURE_MOVEABLE
const uint32_t WIN_RES_HEADER_ALIGNMENT
void setType(uint16_t ID)
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
support::ulittle32_t DataVersion
const Children< std::string > & getStringChildren() const
support::ulittle16_t MemoryFlags
const size_t WIN_RES_MAGIC_SIZE
Expected< std::unique_ptr< MemoryBuffer > > writeWindowsResourceCOFF(llvm::COFF::MachineTypes MachineType, const WindowsResourceParser &Parser)
COFFYAML::WeakExternalCharacteristics Characteristics
Definition: COFFYAML.cpp:283
std::map< T, std::unique_ptr< TreeNode >> Children
MachineTypes
Definition: COFF.h:94
const uint32_t WIN_RES_DATA_ALIGNMENT
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
void printTree(raw_ostream &OS) const
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
Provides read only access to a subclass of BinaryStream.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
ArrayRef< UTF16 > getNameString() const
support::ulittle32_t Characteristics
const ArrayRef< std::vector< UTF16 > > getStringTable() const