Marek Sokolowski | e37621b | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 1 | //===-- ResourceSerializator.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 defines a visitor serializing resources to a .res stream. |
| 11 | // |
| 12 | //===---------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_TOOLS_LLVMRC_RESOURCESERIALIZATOR_H |
| 15 | #define LLVM_TOOLS_LLVMRC_RESOURCESERIALIZATOR_H |
| 16 | |
| 17 | #include "ResourceScriptStmt.h" |
| 18 | #include "ResourceVisitor.h" |
| 19 | |
| 20 | #include "llvm/Support/Endian.h" |
| 21 | |
| 22 | namespace llvm { |
Zachary Turner | e847ec4 | 2017-11-17 01:00:35 +0000 | [diff] [blame] | 23 | |
| 24 | class MemoryBuffer; |
| 25 | |
Marek Sokolowski | e37621b | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 26 | namespace rc { |
| 27 | |
Martin Storsjo | e99f5b4 | 2018-05-02 19:43:44 +0000 | [diff] [blame] | 28 | enum CodePage { |
| 29 | CpAcp = 0, // The current used codepage. Since there's no such |
| 30 | // notion in LLVM what codepage it actually means, |
| 31 | // this only allows ASCII. |
| 32 | CpWin1252 = 1252, // A codepage where most 8 bit values correspond to |
| 33 | // unicode code points with the same value. |
| 34 | CpUtf8 = 65001, // UTF-8. |
| 35 | }; |
| 36 | |
| 37 | struct WriterParams { |
Zachary Turner | e315d73 | 2017-10-11 20:12:09 +0000 | [diff] [blame] | 38 | std::vector<std::string> Include; // Additional folders to search for files. |
| 39 | std::vector<std::string> NoInclude; // Folders to exclude from file search. |
| 40 | StringRef InputFilePath; // The full path of the input file. |
Martin Storsjo | e99f5b4 | 2018-05-02 19:43:44 +0000 | [diff] [blame] | 41 | int CodePage = CpAcp; // The codepage for interpreting characters. |
Zachary Turner | e315d73 | 2017-10-11 20:12:09 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
Marek Sokolowski | e37621b | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 44 | class ResourceFileWriter : public Visitor { |
| 45 | public: |
Martin Storsjo | e99f5b4 | 2018-05-02 19:43:44 +0000 | [diff] [blame] | 46 | ResourceFileWriter(const WriterParams &Params, |
Zachary Turner | e315d73 | 2017-10-11 20:12:09 +0000 | [diff] [blame] | 47 | std::unique_ptr<raw_fd_ostream> Stream) |
| 48 | : Params(Params), FS(std::move(Stream)), IconCursorID(1) { |
Marek Sokolowski | e37621b | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 49 | assert(FS && "Output stream needs to be provided to the serializator"); |
| 50 | } |
| 51 | |
| 52 | Error visitNullResource(const RCResource *) override; |
Marek Sokolowski | b121e77 | 2017-09-29 19:07:44 +0000 | [diff] [blame] | 53 | Error visitAcceleratorsResource(const RCResource *) override; |
Zachary Turner | 4c72b95 | 2017-10-06 21:25:44 +0000 | [diff] [blame] | 54 | Error visitCursorResource(const RCResource *) override; |
Marek Sokolowski | bbf12f3 | 2017-09-30 00:38:52 +0000 | [diff] [blame] | 55 | Error visitDialogResource(const RCResource *) override; |
Marek Sokolowski | e37621b | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 56 | Error visitHTMLResource(const RCResource *) override; |
Zachary Turner | 4c72b95 | 2017-10-06 21:25:44 +0000 | [diff] [blame] | 57 | Error visitIconResource(const RCResource *) override; |
Marek Sokolowski | ea52993 | 2017-09-29 22:25:05 +0000 | [diff] [blame] | 58 | Error visitMenuResource(const RCResource *) override; |
Zachary Turner | 93bb30d | 2017-10-06 21:26:06 +0000 | [diff] [blame] | 59 | Error visitVersionInfoResource(const RCResource *) override; |
Zachary Turner | 84ad96b | 2017-10-06 21:30:55 +0000 | [diff] [blame] | 60 | Error visitStringTableResource(const RCResource *) override; |
Zachary Turner | 080f10e | 2017-10-06 21:52:15 +0000 | [diff] [blame] | 61 | Error visitUserDefinedResource(const RCResource *) override; |
Marek Sokolowski | e37621b | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 62 | |
Zachary Turner | 44bde8d | 2017-10-06 20:51:20 +0000 | [diff] [blame] | 63 | Error visitCaptionStmt(const CaptionStmt *) override; |
Marek Sokolowski | b121e77 | 2017-09-29 19:07:44 +0000 | [diff] [blame] | 64 | Error visitCharacteristicsStmt(const CharacteristicsStmt *) override; |
Martin Storsjo | 65de7bd | 2018-05-15 19:21:28 +0000 | [diff] [blame] | 65 | Error visitClassStmt(const ClassStmt *) override; |
Martin Storsjo | cec32ed | 2018-11-29 12:17:39 +0000 | [diff] [blame] | 66 | Error visitExStyleStmt(const ExStyleStmt *) override; |
Zachary Turner | 44bde8d | 2017-10-06 20:51:20 +0000 | [diff] [blame] | 67 | Error visitFontStmt(const FontStmt *) override; |
Marek Sokolowski | e37621b | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 68 | Error visitLanguageStmt(const LanguageResource *) override; |
Zachary Turner | 44bde8d | 2017-10-06 20:51:20 +0000 | [diff] [blame] | 69 | Error visitStyleStmt(const StyleStmt *) override; |
Marek Sokolowski | b121e77 | 2017-09-29 19:07:44 +0000 | [diff] [blame] | 70 | Error visitVersionStmt(const VersionStmt *) override; |
Marek Sokolowski | e37621b | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 71 | |
Zachary Turner | 84ad96b | 2017-10-06 21:30:55 +0000 | [diff] [blame] | 72 | // Stringtables are output at the end of .res file. We need a separate |
| 73 | // function to do it. |
| 74 | Error dumpAllStringTables(); |
| 75 | |
| 76 | bool AppendNull; // Append '\0' to each existing STRINGTABLE element? |
| 77 | |
Marek Sokolowski | e37621b | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 78 | struct ObjectInfo { |
| 79 | uint16_t LanguageInfo; |
Marek Sokolowski | b121e77 | 2017-09-29 19:07:44 +0000 | [diff] [blame] | 80 | uint32_t Characteristics; |
| 81 | uint32_t VersionInfo; |
Marek Sokolowski | e37621b | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 82 | |
Zachary Turner | 44bde8d | 2017-10-06 20:51:20 +0000 | [diff] [blame] | 83 | Optional<uint32_t> Style; |
Martin Storsjo | cec32ed | 2018-11-29 12:17:39 +0000 | [diff] [blame] | 84 | Optional<uint32_t> ExStyle; |
Zachary Turner | 44bde8d | 2017-10-06 20:51:20 +0000 | [diff] [blame] | 85 | StringRef Caption; |
| 86 | struct FontInfo { |
| 87 | uint32_t Size; |
| 88 | StringRef Typeface; |
| 89 | uint32_t Weight; |
| 90 | bool IsItalic; |
| 91 | uint32_t Charset; |
| 92 | }; |
| 93 | Optional<FontInfo> Font; |
Martin Storsjo | 65de7bd | 2018-05-15 19:21:28 +0000 | [diff] [blame] | 94 | IntOrString Class; |
Zachary Turner | 44bde8d | 2017-10-06 20:51:20 +0000 | [diff] [blame] | 95 | |
Martin Storsjo | 65de7bd | 2018-05-15 19:21:28 +0000 | [diff] [blame] | 96 | ObjectInfo() |
| 97 | : LanguageInfo(0), Characteristics(0), VersionInfo(0), |
| 98 | Class(StringRef()) {} |
Marek Sokolowski | e37621b | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 99 | } ObjectData; |
| 100 | |
Zachary Turner | 84ad96b | 2017-10-06 21:30:55 +0000 | [diff] [blame] | 101 | struct StringTableInfo { |
| 102 | // Each STRINGTABLE bundle depends on ID of the bundle and language |
| 103 | // description. |
| 104 | using BundleKey = std::pair<uint16_t, uint16_t>; |
| 105 | // Each bundle is in fact an array of 16 strings. |
| 106 | struct Bundle { |
| 107 | std::array<Optional<StringRef>, 16> Data; |
| 108 | ObjectInfo DeclTimeInfo; |
Martin Storsjo | 370633f | 2018-05-15 06:35:29 +0000 | [diff] [blame] | 109 | uint16_t MemoryFlags; |
| 110 | Bundle(const ObjectInfo &Info, uint16_t Flags) |
| 111 | : DeclTimeInfo(Info), MemoryFlags(Flags) {} |
Zachary Turner | 84ad96b | 2017-10-06 21:30:55 +0000 | [diff] [blame] | 112 | }; |
| 113 | std::map<BundleKey, Bundle> BundleData; |
Malcolm Parsons | 5fc9628 | 2018-01-24 10:33:39 +0000 | [diff] [blame] | 114 | // Bundles are listed in the order of their first occurrence. |
Zachary Turner | 84ad96b | 2017-10-06 21:30:55 +0000 | [diff] [blame] | 115 | std::vector<BundleKey> BundleList; |
| 116 | } StringTableData; |
| 117 | |
Marek Sokolowski | e37621b | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 118 | private: |
Zachary Turner | 5d1b2d3 | 2017-10-09 18:50:29 +0000 | [diff] [blame] | 119 | Error handleError(Error Err, const RCResource *Res); |
Marek Sokolowski | e37621b | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 120 | |
| 121 | Error |
| 122 | writeResource(const RCResource *Res, |
| 123 | Error (ResourceFileWriter::*BodyWriter)(const RCResource *)); |
| 124 | |
Marek Sokolowski | b121e77 | 2017-09-29 19:07:44 +0000 | [diff] [blame] | 125 | // NullResource |
Marek Sokolowski | e37621b | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 126 | Error writeNullBody(const RCResource *); |
Marek Sokolowski | b121e77 | 2017-09-29 19:07:44 +0000 | [diff] [blame] | 127 | |
| 128 | // AcceleratorsResource |
| 129 | Error writeSingleAccelerator(const AcceleratorsResource::Accelerator &, |
| 130 | bool IsLastItem); |
| 131 | Error writeAcceleratorsBody(const RCResource *); |
| 132 | |
Martin Storsjo | 31dc80f | 2018-05-07 20:27:37 +0000 | [diff] [blame] | 133 | // BitmapResource |
Roman Lebedev | 6441549 | 2018-05-07 21:06:53 +0000 | [diff] [blame] | 134 | Error visitBitmapResource(const RCResource *) override; |
Martin Storsjo | 31dc80f | 2018-05-07 20:27:37 +0000 | [diff] [blame] | 135 | Error writeBitmapBody(const RCResource *); |
| 136 | |
Zachary Turner | 4c72b95 | 2017-10-06 21:25:44 +0000 | [diff] [blame] | 137 | // CursorResource and IconResource |
| 138 | Error visitIconOrCursorResource(const RCResource *); |
| 139 | Error visitIconOrCursorGroup(const RCResource *); |
| 140 | Error visitSingleIconOrCursor(const RCResource *); |
| 141 | Error writeSingleIconOrCursorBody(const RCResource *); |
| 142 | Error writeIconOrCursorGroupBody(const RCResource *); |
| 143 | |
Marek Sokolowski | bbf12f3 | 2017-09-30 00:38:52 +0000 | [diff] [blame] | 144 | // DialogResource |
| 145 | Error writeSingleDialogControl(const Control &, bool IsExtended); |
| 146 | Error writeDialogBody(const RCResource *); |
| 147 | |
Marek Sokolowski | b121e77 | 2017-09-29 19:07:44 +0000 | [diff] [blame] | 148 | // HTMLResource |
Marek Sokolowski | e37621b | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 149 | Error writeHTMLBody(const RCResource *); |
| 150 | |
Marek Sokolowski | ea52993 | 2017-09-29 22:25:05 +0000 | [diff] [blame] | 151 | // MenuResource |
| 152 | Error writeMenuDefinition(const std::unique_ptr<MenuDefinition> &, |
| 153 | uint16_t Flags); |
| 154 | Error writeMenuDefinitionList(const MenuDefinitionList &List); |
| 155 | Error writeMenuBody(const RCResource *); |
| 156 | |
Zachary Turner | 84ad96b | 2017-10-06 21:30:55 +0000 | [diff] [blame] | 157 | // StringTableResource |
| 158 | Error visitStringTableBundle(const RCResource *); |
| 159 | Error writeStringTableBundleBody(const RCResource *); |
| 160 | Error insertStringIntoBundle(StringTableInfo::Bundle &Bundle, |
| 161 | uint16_t StringID, StringRef String); |
| 162 | |
Zachary Turner | 080f10e | 2017-10-06 21:52:15 +0000 | [diff] [blame] | 163 | // User defined resource |
| 164 | Error writeUserDefinedBody(const RCResource *); |
| 165 | |
Zachary Turner | 93bb30d | 2017-10-06 21:26:06 +0000 | [diff] [blame] | 166 | // VersionInfoResource |
| 167 | Error writeVersionInfoBody(const RCResource *); |
| 168 | Error writeVersionInfoBlock(const VersionInfoBlock &); |
| 169 | Error writeVersionInfoValue(const VersionInfoValue &); |
| 170 | |
Martin Storsjo | e99f5b4 | 2018-05-02 19:43:44 +0000 | [diff] [blame] | 171 | const WriterParams &Params; |
Zachary Turner | e315d73 | 2017-10-11 20:12:09 +0000 | [diff] [blame] | 172 | |
Marek Sokolowski | e37621b | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 173 | // Output stream handling. |
| 174 | std::unique_ptr<raw_fd_ostream> FS; |
| 175 | |
| 176 | uint64_t tell() const { return FS->tell(); } |
| 177 | |
| 178 | uint64_t writeObject(const ArrayRef<uint8_t> Data); |
| 179 | |
| 180 | template <typename T> uint64_t writeInt(const T &Value) { |
| 181 | support::detail::packed_endian_specific_integral<T, support::little, |
| 182 | support::unaligned> |
| 183 | Object(Value); |
| 184 | return writeObject(Object); |
| 185 | } |
| 186 | |
| 187 | template <typename T> uint64_t writeObject(const T &Value) { |
| 188 | return writeObject(ArrayRef<uint8_t>( |
| 189 | reinterpret_cast<const uint8_t *>(&Value), sizeof(T))); |
| 190 | } |
| 191 | |
| 192 | template <typename T> void writeObjectAt(const T &Value, uint64_t Position) { |
| 193 | FS->pwrite((const char *)&Value, sizeof(T), Position); |
| 194 | } |
| 195 | |
| 196 | Error writeCString(StringRef Str, bool WriteTerminator = true); |
| 197 | |
| 198 | Error writeIdentifier(const IntOrString &Ident); |
| 199 | Error writeIntOrString(const IntOrString &Data); |
| 200 | |
Zachary Turner | 93bb30d | 2017-10-06 21:26:06 +0000 | [diff] [blame] | 201 | void writeRCInt(RCInt); |
| 202 | |
Marek Sokolowski | e37621b | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 203 | Error appendFile(StringRef Filename); |
| 204 | |
| 205 | void padStream(uint64_t Length); |
Zachary Turner | 4c72b95 | 2017-10-06 21:25:44 +0000 | [diff] [blame] | 206 | |
Zachary Turner | e315d73 | 2017-10-11 20:12:09 +0000 | [diff] [blame] | 207 | Expected<std::unique_ptr<MemoryBuffer>> loadFile(StringRef File) const; |
| 208 | |
Zachary Turner | 4c72b95 | 2017-10-06 21:25:44 +0000 | [diff] [blame] | 209 | // Icon and cursor IDs are allocated starting from 1 and increasing for |
| 210 | // each icon/cursor dumped. This maintains the current ID to be allocated. |
| 211 | uint16_t IconCursorID; |
Marek Sokolowski | e37621b | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | } // namespace rc |
| 215 | } // namespace llvm |
| 216 | |
| 217 | #endif |