1 //
2 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3 // See https://llvm.org/LICENSE.txt for license information.
4 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5 //
6 //===---------------------------------------------------------------------===//
7 //
8 // This implements methods defined in ResourceScriptStmt.h.
9 //
10 // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa380599(v=vs.85).aspx
11 //
12 //===---------------------------------------------------------------------===//
13 
14 #include "ResourceScriptStmt.h"
15 
16 namespace llvm {
17 namespace rc {
18 
operator <<(raw_ostream & OS,const IntOrString & Item)19 raw_ostream &operator<<(raw_ostream &OS, const IntOrString &Item) {
20   if (Item.IsInt)
21     return OS << Item.Data.Int;
22   else
23     return OS << Item.Data.String;
24 }
25 
log(raw_ostream & OS) const26 raw_ostream &OptionalStmtList::log(raw_ostream &OS) const {
27   for (const auto &Stmt : Statements) {
28     OS << "  Option: ";
29     Stmt->log(OS);
30   }
31   return OS;
32 }
33 
log(raw_ostream & OS) const34 raw_ostream &LanguageResource::log(raw_ostream &OS) const {
35   return OS << "Language: " << Lang << ", Sublanguage: " << SubLang << "\n";
36 }
37 
38 StringRef AcceleratorsResource::Accelerator::OptionsStr
39     [AcceleratorsResource::Accelerator::NumFlags] = {
40         "ASCII", "VIRTKEY", "NOINVERT", "ALT", "SHIFT", "CONTROL"};
41 
42 uint32_t AcceleratorsResource::Accelerator::OptionsFlags
43     [AcceleratorsResource::Accelerator::NumFlags] = {ASCII, VIRTKEY, NOINVERT,
44                                                      ALT,   SHIFT,   CONTROL};
45 
log(raw_ostream & OS) const46 raw_ostream &AcceleratorsResource::log(raw_ostream &OS) const {
47   OS << "Accelerators (" << ResName << "): \n";
48   OptStatements->log(OS);
49   for (const auto &Acc : Accelerators) {
50     OS << "  Accelerator: " << Acc.Event << " " << Acc.Id;
51     for (size_t i = 0; i < Accelerator::NumFlags; ++i)
52       if (Acc.Flags & Accelerator::OptionsFlags[i])
53         OS << " " << Accelerator::OptionsStr[i];
54     OS << "\n";
55   }
56   return OS;
57 }
58 
log(raw_ostream & OS) const59 raw_ostream &BitmapResource::log(raw_ostream &OS) const {
60   return OS << "Bitmap (" << ResName << "): " << BitmapLoc << "\n";
61 }
62 
log(raw_ostream & OS) const63 raw_ostream &CursorResource::log(raw_ostream &OS) const {
64   return OS << "Cursor (" << ResName << "): " << CursorLoc << "\n";
65 }
66 
log(raw_ostream & OS) const67 raw_ostream &IconResource::log(raw_ostream &OS) const {
68   return OS << "Icon (" << ResName << "): " << IconLoc << "\n";
69 }
70 
log(raw_ostream & OS) const71 raw_ostream &HTMLResource::log(raw_ostream &OS) const {
72   return OS << "HTML (" << ResName << "): " << HTMLLoc << "\n";
73 }
74 
75 StringRef MenuDefinition::OptionsStr[MenuDefinition::NumFlags] = {
76     "CHECKED", "GRAYED", "HELP", "INACTIVE", "MENUBARBREAK", "MENUBREAK"};
77 
78 uint32_t MenuDefinition::OptionsFlags[MenuDefinition::NumFlags] = {
79     CHECKED, GRAYED, HELP, INACTIVE, MENUBARBREAK, MENUBREAK};
80 
logFlags(raw_ostream & OS,uint16_t Flags)81 raw_ostream &MenuDefinition::logFlags(raw_ostream &OS, uint16_t Flags) {
82   for (size_t i = 0; i < NumFlags; ++i)
83     if (Flags & OptionsFlags[i])
84       OS << " " << OptionsStr[i];
85   return OS;
86 }
87 
log(raw_ostream & OS) const88 raw_ostream &MenuDefinitionList::log(raw_ostream &OS) const {
89   OS << "  Menu list starts\n";
90   for (auto &Item : Definitions)
91     Item->log(OS);
92   return OS << "  Menu list ends\n";
93 }
94 
log(raw_ostream & OS) const95 raw_ostream &MenuItem::log(raw_ostream &OS) const {
96   OS << "  MenuItem (" << Name << "), ID = " << Id;
97   logFlags(OS, Flags);
98   return OS << "\n";
99 }
100 
log(raw_ostream & OS) const101 raw_ostream &MenuSeparator::log(raw_ostream &OS) const {
102   return OS << "  Menu separator\n";
103 }
104 
log(raw_ostream & OS) const105 raw_ostream &PopupItem::log(raw_ostream &OS) const {
106   OS << "  Popup (" << Name << ")";
107   logFlags(OS, Flags);
108   OS << ":\n";
109   return SubItems.log(OS);
110 }
111 
log(raw_ostream & OS) const112 raw_ostream &MenuResource::log(raw_ostream &OS) const {
113   OS << "Menu (" << ResName << "):\n";
114   OptStatements->log(OS);
115   return Elements.log(OS);
116 }
117 
log(raw_ostream & OS) const118 raw_ostream &StringTableResource::log(raw_ostream &OS) const {
119   OS << "StringTable:\n";
120   OptStatements->log(OS);
121   for (const auto &String : Table) {
122     OS << "  " << String.first << " =>";
123     for (const auto &S : String.second)
124       OS << " " << S;
125     OS << "\n";
126   }
127   return OS;
128 }
129 
130 const StringMap<Control::CtlInfo> Control::SupportedCtls = {
131     {"LTEXT", CtlInfo{0x50020000, ClsStatic, true}},
132     {"CTEXT", CtlInfo{0x50020001, ClsStatic, true}},
133     {"RTEXT", CtlInfo{0x50020002, ClsStatic, true}},
134     {"ICON", CtlInfo{0x50000003, ClsStatic, true}},
135     {"PUSHBUTTON", CtlInfo{0x50010000, ClsButton, true}},
136     {"DEFPUSHBUTTON", CtlInfo{0x50010001, ClsButton, true}},
137     {"AUTO3STATE", CtlInfo{0x50010006, ClsButton, true}},
138     {"AUTOCHECKBOX", CtlInfo{0x50010003, ClsButton, true}},
139     {"AUTORADIOBUTTON", CtlInfo{0x50000009, ClsButton, true}},
140     {"CHECKBOX", CtlInfo{0x50010002, ClsButton, true}},
141     {"GROUPBOX", CtlInfo{0x50000007, ClsButton, true}},
142     {"RADIOBUTTON", CtlInfo{0x50000004, ClsButton, true}},
143     {"STATE3", CtlInfo{0x50010005, ClsButton, true}},
144     {"PUSHBOX", CtlInfo{0x5001000A, ClsButton, true}},
145     {"EDITTEXT", CtlInfo{0x50810000, ClsEdit, false}},
146     {"COMBOBOX", CtlInfo{0x50000000, ClsComboBox, false}},
147     {"LISTBOX", CtlInfo{0x50800001, ClsListBox, false}},
148     {"SCROLLBAR", CtlInfo{0x50000000, ClsScrollBar, false}},
149     {"CONTROL", CtlInfo{0x50000000, 0, true}},
150 };
151 
log(raw_ostream & OS) const152 raw_ostream &Control::log(raw_ostream &OS) const {
153   OS << "  Control (" << ID << "): " << Type << ", title: " << Title
154      << ", loc: (" << X << ", " << Y << "), size: [" << Width << ", " << Height
155      << "]";
156   if (Style)
157     OS << ", style: " << (*Style).getValue();
158   if (ExtStyle)
159     OS << ", ext. style: " << *ExtStyle;
160   if (HelpID)
161     OS << ", help ID: " << *HelpID;
162   return OS << "\n";
163 }
164 
log(raw_ostream & OS) const165 raw_ostream &DialogResource::log(raw_ostream &OS) const {
166   OS << "Dialog" << (IsExtended ? "Ex" : "") << " (" << ResName << "): loc: ("
167      << X << ", " << Y << "), size: [" << Width << ", " << Height
168      << "], help ID: " << HelpID << "\n";
169   OptStatements->log(OS);
170   for (auto &Ctl : Controls)
171     Ctl.log(OS);
172   return OS;
173 }
174 
log(raw_ostream & OS) const175 raw_ostream &VersionInfoBlock::log(raw_ostream &OS) const {
176   OS << "  Start of block (name: " << Name << ")\n";
177   for (auto &Stmt : Stmts)
178     Stmt->log(OS);
179   return OS << "  End of block\n";
180 }
181 
log(raw_ostream & OS) const182 raw_ostream &VersionInfoValue::log(raw_ostream &OS) const {
183   OS << "  " << Key << " =>";
184   size_t NumValues = Values.size();
185   for (size_t Id = 0; Id < NumValues; ++Id) {
186     if (Id > 0 && HasPrecedingComma[Id])
187       OS << ",";
188     OS << " " << Values[Id];
189   }
190   return OS << "\n";
191 }
192 
193 using VersionInfoFixed = VersionInfoResource::VersionInfoFixed;
194 using VersionInfoFixedType = VersionInfoFixed::VersionInfoFixedType;
195 
196 const StringRef
197     VersionInfoFixed::FixedFieldsNames[VersionInfoFixed::FtNumTypes] = {
198         "",          "FILEVERSION", "PRODUCTVERSION", "FILEFLAGSMASK",
199         "FILEFLAGS", "FILEOS",      "FILETYPE",       "FILESUBTYPE"};
200 
201 const StringMap<VersionInfoFixedType> VersionInfoFixed::FixedFieldsInfoMap = {
202     {FixedFieldsNames[FtFileVersion], FtFileVersion},
203     {FixedFieldsNames[FtProductVersion], FtProductVersion},
204     {FixedFieldsNames[FtFileFlagsMask], FtFileFlagsMask},
205     {FixedFieldsNames[FtFileFlags], FtFileFlags},
206     {FixedFieldsNames[FtFileOS], FtFileOS},
207     {FixedFieldsNames[FtFileType], FtFileType},
208     {FixedFieldsNames[FtFileSubtype], FtFileSubtype}};
209 
getFixedType(StringRef Type)210 VersionInfoFixedType VersionInfoFixed::getFixedType(StringRef Type) {
211   auto UpperType = Type.upper();
212   auto Iter = FixedFieldsInfoMap.find(UpperType);
213   if (Iter != FixedFieldsInfoMap.end())
214     return Iter->getValue();
215   return FtUnknown;
216 }
217 
isTypeSupported(VersionInfoFixedType Type)218 bool VersionInfoFixed::isTypeSupported(VersionInfoFixedType Type) {
219   return FtUnknown < Type && Type < FtNumTypes;
220 }
221 
isVersionType(VersionInfoFixedType Type)222 bool VersionInfoFixed::isVersionType(VersionInfoFixedType Type) {
223   switch (Type) {
224   case FtFileVersion:
225   case FtProductVersion:
226     return true;
227 
228   default:
229     return false;
230   }
231 }
232 
log(raw_ostream & OS) const233 raw_ostream &VersionInfoFixed::log(raw_ostream &OS) const {
234   for (int Type = FtUnknown; Type < FtNumTypes; ++Type) {
235     if (!isTypeSupported((VersionInfoFixedType)Type))
236       continue;
237     OS << "  Fixed: " << FixedFieldsNames[Type] << ":";
238     for (uint32_t Val : FixedInfo[Type])
239       OS << " " << Val;
240     OS << "\n";
241   }
242   return OS;
243 }
244 
log(raw_ostream & OS) const245 raw_ostream &VersionInfoResource::log(raw_ostream &OS) const {
246   OS << "VersionInfo (" << ResName << "):\n";
247   FixedData.log(OS);
248   return MainBlock.log(OS);
249 }
250 
log(raw_ostream & OS) const251 raw_ostream &UserDefinedResource::log(raw_ostream &OS) const {
252   OS << "User-defined (type: " << Type << ", name: " << ResName << "): ";
253   if (IsFileResource)
254     return OS << FileLoc << "\n";
255   OS << "data = ";
256   for (auto &Item : Contents)
257     OS << Item << " ";
258   return OS << "\n";
259 }
260 
log(raw_ostream & OS) const261 raw_ostream &CharacteristicsStmt::log(raw_ostream &OS) const {
262   return OS << "Characteristics: " << Value << "\n";
263 }
264 
log(raw_ostream & OS) const265 raw_ostream &VersionStmt::log(raw_ostream &OS) const {
266   return OS << "Version: " << Value << "\n";
267 }
268 
log(raw_ostream & OS) const269 raw_ostream &CaptionStmt::log(raw_ostream &OS) const {
270   return OS << "Caption: " << Value << "\n";
271 }
272 
log(raw_ostream & OS) const273 raw_ostream &ClassStmt::log(raw_ostream &OS) const {
274   return OS << "Class: " << Value << "\n";
275 }
276 
log(raw_ostream & OS) const277 raw_ostream &FontStmt::log(raw_ostream &OS) const {
278   OS << "Font: size = " << Size << ", face = " << Name
279      << ", weight = " << Weight;
280   if (Italic)
281     OS << ", italic";
282   return OS << ", charset = " << Charset << "\n";
283 }
284 
log(raw_ostream & OS) const285 raw_ostream &StyleStmt::log(raw_ostream &OS) const {
286   return OS << "Style: " << Value << "\n";
287 }
288 
log(raw_ostream & OS) const289 raw_ostream &ExStyleStmt::log(raw_ostream &OS) const {
290   return OS << "ExStyle: " << Value << "\n";
291 }
292 
293 } // namespace rc
294 } // namespace llvm
295