1 //===-- ResourceFileWriter.cpp --------------------------------*- 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 implements the visitor serializing resources to a .res stream.
11 //
12 //===---------------------------------------------------------------------===//
13 
14 #include "ResourceFileWriter.h"
15 
16 #include "llvm/Object/WindowsResource.h"
17 #include "llvm/Support/ConvertUTF.h"
18 #include "llvm/Support/Endian.h"
19 #include "llvm/Support/EndianStream.h"
20 #include "llvm/Support/Path.h"
21 #include "llvm/Support/Process.h"
22 
23 using namespace llvm::support;
24 
25 // Take an expression returning llvm::Error and forward the error if it exists.
26 #define RETURN_IF_ERROR(Expr)                                                  \
27   if (auto Err = (Expr))                                                       \
28     return Err;
29 
30 namespace llvm {
31 namespace rc {
32 
33 // Class that employs RAII to save the current FileWriter object state
34 // and revert to it as soon as we leave the scope. This is useful if resources
35 // declare their own resource-local statements.
36 class ContextKeeper {
37   ResourceFileWriter *FileWriter;
38   ResourceFileWriter::ObjectInfo SavedInfo;
39 
40 public:
41   ContextKeeper(ResourceFileWriter *V)
42       : FileWriter(V), SavedInfo(V->ObjectData) {}
43   ~ContextKeeper() { FileWriter->ObjectData = SavedInfo; }
44 };
45 
46 static Error createError(const Twine &Message,
47                          std::errc Type = std::errc::invalid_argument) {
48   return make_error<StringError>(Message, std::make_error_code(Type));
49 }
50 
51 static Error checkNumberFits(uint32_t Number, size_t MaxBits,
52                              const Twine &FieldName) {
53   assert(1 <= MaxBits && MaxBits <= 32);
54   if (!(Number >> MaxBits))
55     return Error::success();
56   return createError(FieldName + " (" + Twine(Number) + ") does not fit in " +
57                          Twine(MaxBits) + " bits.",
58                      std::errc::value_too_large);
59 }
60 
61 template <typename FitType>
62 static Error checkNumberFits(uint32_t Number, const Twine &FieldName) {
63   return checkNumberFits(Number, sizeof(FitType) * 8, FieldName);
64 }
65 
66 // A similar function for signed integers.
67 template <typename FitType>
68 static Error checkSignedNumberFits(uint32_t Number, const Twine &FieldName,
69                                    bool CanBeNegative) {
70   int32_t SignedNum = Number;
71   if (SignedNum < std::numeric_limits<FitType>::min() ||
72       SignedNum > std::numeric_limits<FitType>::max())
73     return createError(FieldName + " (" + Twine(SignedNum) +
74                            ") does not fit in " + Twine(sizeof(FitType) * 8) +
75                            "-bit signed integer type.",
76                        std::errc::value_too_large);
77 
78   if (!CanBeNegative && SignedNum < 0)
79     return createError(FieldName + " (" + Twine(SignedNum) +
80                        ") cannot be negative.");
81 
82   return Error::success();
83 }
84 
85 static Error checkRCInt(RCInt Number, const Twine &FieldName) {
86   if (Number.isLong())
87     return Error::success();
88   return checkNumberFits<uint16_t>(Number, FieldName);
89 }
90 
91 static Error checkIntOrString(IntOrString Value, const Twine &FieldName) {
92   if (!Value.isInt())
93     return Error::success();
94   return checkNumberFits<uint16_t>(Value.getInt(), FieldName);
95 }
96 
97 static bool stripQuotes(StringRef &Str, bool &IsLongString) {
98   if (!Str.contains('"'))
99     return false;
100 
101   // Just take the contents of the string, checking if it's been marked long.
102   IsLongString = Str.startswith_lower("L");
103   if (IsLongString)
104     Str = Str.drop_front();
105 
106   bool StripSuccess = Str.consume_front("\"") && Str.consume_back("\"");
107   (void)StripSuccess;
108   assert(StripSuccess && "Strings should be enclosed in quotes.");
109   return true;
110 }
111 
112 // Describes a way to handle '\0' characters when processing the string.
113 // rc.exe tool sometimes behaves in a weird way in postprocessing.
114 // If the string to be output is equivalent to a C-string (e.g. in MENU
115 // titles), string is (predictably) truncated after first 0-byte.
116 // When outputting a string table, the behavior is equivalent to appending
117 // '\0\0' at the end of the string, and then stripping the string
118 // before the first '\0\0' occurrence.
119 // Finally, when handling strings in user-defined resources, 0-bytes
120 // aren't stripped, nor do they terminate the string.
121 
122 enum class NullHandlingMethod {
123   UserResource,   // Don't terminate string on '\0'.
124   CutAtNull,      // Terminate string on '\0'.
125   CutAtDoubleNull // Terminate string on '\0\0'; strip final '\0'.
126 };
127 
128 // Parses an identifier or string and returns a processed version of it:
129 //   * String the string boundary quotes.
130 //   * Squash "" to a single ".
131 //   * Replace the escape sequences with their processed version.
132 // For identifiers, this is no-op.
133 static Error processString(StringRef Str, NullHandlingMethod NullHandler,
134                            bool &IsLongString, SmallVectorImpl<UTF16> &Result) {
135   bool IsString = stripQuotes(Str, IsLongString);
136   SmallVector<UTF16, 128> Chars;
137   convertUTF8ToUTF16String(Str, Chars);
138 
139   if (!IsString) {
140     // It's an identifier if it's not a string. Make all characters uppercase.
141     for (UTF16 &Ch : Chars) {
142       assert(Ch <= 0x7F && "We didn't allow identifiers to be non-ASCII");
143       Ch = toupper(Ch);
144     }
145     Result.swap(Chars);
146     return Error::success();
147   }
148   Result.reserve(Chars.size());
149   size_t Pos = 0;
150 
151   auto AddRes = [&Result, NullHandler, IsLongString](UTF16 Char) -> Error {
152     if (!IsLongString) {
153       if (NullHandler == NullHandlingMethod::UserResource) {
154         // Narrow strings in user-defined resources are *not* output in
155         // UTF-16 format.
156         if (Char > 0xFF)
157           return createError("Non-8-bit codepoint (" + Twine(Char) +
158                              ") can't occur in a user-defined narrow string");
159 
160       } else {
161         // In case of narrow non-user strings, Windows RC converts
162         // [0x80, 0xFF] chars according to the current codepage.
163         // There is no 'codepage' concept settled in every supported platform,
164         // so we should reject such inputs.
165         if (Char > 0x7F && Char <= 0xFF)
166           return createError("Non-ASCII 8-bit codepoint (" + Twine(Char) +
167                              ") can't "
168                              "occur in a non-Unicode string");
169       }
170     }
171 
172     Result.push_back(Char);
173     return Error::success();
174   };
175 
176   while (Pos < Chars.size()) {
177     UTF16 CurChar = Chars[Pos];
178     ++Pos;
179 
180     // Strip double "".
181     if (CurChar == '"') {
182       if (Pos == Chars.size() || Chars[Pos] != '"')
183         return createError("Expected \"\"");
184       ++Pos;
185       RETURN_IF_ERROR(AddRes('"'));
186       continue;
187     }
188 
189     if (CurChar == '\\') {
190       UTF16 TypeChar = Chars[Pos];
191       ++Pos;
192 
193       if (TypeChar == 'x' || TypeChar == 'X') {
194         // Read a hex number. Max number of characters to read differs between
195         // narrow and wide strings.
196         UTF16 ReadInt = 0;
197         size_t RemainingChars = IsLongString ? 4 : 2;
198         // We don't want to read non-ASCII hex digits. std:: functions past
199         // 0xFF invoke UB.
200         //
201         // FIXME: actually, Microsoft version probably doesn't check this
202         // condition and uses their Unicode version of 'isxdigit'. However,
203         // there are some hex-digit Unicode character outside of ASCII, and
204         // some of these are actually accepted by rc.exe, the notable example
205         // being fullwidth forms (U+FF10..U+FF19 etc.) These can be written
206         // instead of ASCII digits in \x... escape sequence and get accepted.
207         // However, the resulting hexcodes seem totally unpredictable.
208         // We think it's infeasible to try to reproduce this behavior, nor to
209         // put effort in order to detect it.
210         while (RemainingChars && Pos < Chars.size() && Chars[Pos] < 0x80) {
211           if (!isxdigit(Chars[Pos]))
212             break;
213           char Digit = tolower(Chars[Pos]);
214           ++Pos;
215 
216           ReadInt <<= 4;
217           if (isdigit(Digit))
218             ReadInt |= Digit - '0';
219           else
220             ReadInt |= Digit - 'a' + 10;
221 
222           --RemainingChars;
223         }
224 
225         RETURN_IF_ERROR(AddRes(ReadInt));
226         continue;
227       }
228 
229       if (TypeChar >= '0' && TypeChar < '8') {
230         // Read an octal number. Note that we've already read the first digit.
231         UTF16 ReadInt = TypeChar - '0';
232         size_t RemainingChars = IsLongString ? 6 : 2;
233 
234         while (RemainingChars && Pos < Chars.size() && Chars[Pos] >= '0' &&
235                Chars[Pos] < '8') {
236           ReadInt <<= 3;
237           ReadInt |= Chars[Pos] - '0';
238           --RemainingChars;
239           ++Pos;
240         }
241 
242         RETURN_IF_ERROR(AddRes(ReadInt));
243 
244         continue;
245       }
246 
247       switch (TypeChar) {
248       case 'A':
249       case 'a':
250         // Windows '\a' translates into '\b' (Backspace).
251         RETURN_IF_ERROR(AddRes('\b'));
252         break;
253 
254       case 'n': // Somehow, RC doesn't recognize '\N' and '\R'.
255         RETURN_IF_ERROR(AddRes('\n'));
256         break;
257 
258       case 'r':
259         RETURN_IF_ERROR(AddRes('\r'));
260         break;
261 
262       case 'T':
263       case 't':
264         RETURN_IF_ERROR(AddRes('\t'));
265         break;
266 
267       case '\\':
268         RETURN_IF_ERROR(AddRes('\\'));
269         break;
270 
271       case '"':
272         // RC accepts \" only if another " comes afterwards; then, \"" means
273         // a single ".
274         if (Pos == Chars.size() || Chars[Pos] != '"')
275           return createError("Expected \\\"\"");
276         ++Pos;
277         RETURN_IF_ERROR(AddRes('"'));
278         break;
279 
280       default:
281         // If TypeChar means nothing, \ is should be output to stdout with
282         // following char. However, rc.exe consumes these characters when
283         // dealing with wide strings.
284         if (!IsLongString) {
285           RETURN_IF_ERROR(AddRes('\\'));
286           RETURN_IF_ERROR(AddRes(TypeChar));
287         }
288         break;
289       }
290 
291       continue;
292     }
293 
294     // If nothing interesting happens, just output the character.
295     RETURN_IF_ERROR(AddRes(CurChar));
296   }
297 
298   switch (NullHandler) {
299   case NullHandlingMethod::CutAtNull:
300     for (size_t Pos = 0; Pos < Result.size(); ++Pos)
301       if (Result[Pos] == '\0')
302         Result.resize(Pos);
303     break;
304 
305   case NullHandlingMethod::CutAtDoubleNull:
306     for (size_t Pos = 0; Pos + 1 < Result.size(); ++Pos)
307       if (Result[Pos] == '\0' && Result[Pos + 1] == '\0')
308         Result.resize(Pos);
309     if (Result.size() > 0 && Result.back() == '\0')
310       Result.pop_back();
311     break;
312 
313   case NullHandlingMethod::UserResource:
314     break;
315   }
316 
317   return Error::success();
318 }
319 
320 uint64_t ResourceFileWriter::writeObject(const ArrayRef<uint8_t> Data) {
321   uint64_t Result = tell();
322   FS->write((const char *)Data.begin(), Data.size());
323   return Result;
324 }
325 
326 Error ResourceFileWriter::writeCString(StringRef Str, bool WriteTerminator) {
327   SmallVector<UTF16, 128> ProcessedString;
328   bool IsLongString;
329   RETURN_IF_ERROR(processString(Str, NullHandlingMethod::CutAtNull,
330                                 IsLongString, ProcessedString));
331   for (auto Ch : ProcessedString)
332     writeInt<uint16_t>(Ch);
333   if (WriteTerminator)
334     writeInt<uint16_t>(0);
335   return Error::success();
336 }
337 
338 Error ResourceFileWriter::writeIdentifier(const IntOrString &Ident) {
339   return writeIntOrString(Ident);
340 }
341 
342 Error ResourceFileWriter::writeIntOrString(const IntOrString &Value) {
343   if (!Value.isInt())
344     return writeCString(Value.getString());
345 
346   writeInt<uint16_t>(0xFFFF);
347   writeInt<uint16_t>(Value.getInt());
348   return Error::success();
349 }
350 
351 void ResourceFileWriter::writeRCInt(RCInt Value) {
352   if (Value.isLong())
353     writeInt<uint32_t>(Value);
354   else
355     writeInt<uint16_t>(Value);
356 }
357 
358 Error ResourceFileWriter::appendFile(StringRef Filename) {
359   bool IsLong;
360   stripQuotes(Filename, IsLong);
361 
362   auto File = loadFile(Filename);
363   if (!File)
364     return File.takeError();
365 
366   *FS << (*File)->getBuffer();
367   return Error::success();
368 }
369 
370 void ResourceFileWriter::padStream(uint64_t Length) {
371   assert(Length > 0);
372   uint64_t Location = tell();
373   Location %= Length;
374   uint64_t Pad = (Length - Location) % Length;
375   for (uint64_t i = 0; i < Pad; ++i)
376     writeInt<uint8_t>(0);
377 }
378 
379 Error ResourceFileWriter::handleError(Error Err, const RCResource *Res) {
380   if (Err)
381     return joinErrors(createError("Error in " + Res->getResourceTypeName() +
382                                   " statement (ID " + Twine(Res->ResName) +
383                                   "): "),
384                       std::move(Err));
385   return Error::success();
386 }
387 
388 Error ResourceFileWriter::visitNullResource(const RCResource *Res) {
389   return writeResource(Res, &ResourceFileWriter::writeNullBody);
390 }
391 
392 Error ResourceFileWriter::visitAcceleratorsResource(const RCResource *Res) {
393   return writeResource(Res, &ResourceFileWriter::writeAcceleratorsBody);
394 }
395 
396 Error ResourceFileWriter::visitCursorResource(const RCResource *Res) {
397   return handleError(visitIconOrCursorResource(Res), Res);
398 }
399 
400 Error ResourceFileWriter::visitDialogResource(const RCResource *Res) {
401   return writeResource(Res, &ResourceFileWriter::writeDialogBody);
402 }
403 
404 Error ResourceFileWriter::visitIconResource(const RCResource *Res) {
405   return handleError(visitIconOrCursorResource(Res), Res);
406 }
407 
408 Error ResourceFileWriter::visitCaptionStmt(const CaptionStmt *Stmt) {
409   ObjectData.Caption = Stmt->Value;
410   return Error::success();
411 }
412 
413 Error ResourceFileWriter::visitHTMLResource(const RCResource *Res) {
414   return writeResource(Res, &ResourceFileWriter::writeHTMLBody);
415 }
416 
417 Error ResourceFileWriter::visitMenuResource(const RCResource *Res) {
418   return writeResource(Res, &ResourceFileWriter::writeMenuBody);
419 }
420 
421 Error ResourceFileWriter::visitStringTableResource(const RCResource *Base) {
422   const auto *Res = cast<StringTableResource>(Base);
423 
424   ContextKeeper RAII(this);
425   RETURN_IF_ERROR(Res->applyStmts(this));
426 
427   for (auto &String : Res->Table) {
428     RETURN_IF_ERROR(checkNumberFits<uint16_t>(String.first, "String ID"));
429     uint16_t BundleID = String.first >> 4;
430     StringTableInfo::BundleKey Key(BundleID, ObjectData.LanguageInfo);
431     auto &BundleData = StringTableData.BundleData;
432     auto Iter = BundleData.find(Key);
433 
434     if (Iter == BundleData.end()) {
435       // Need to create a bundle.
436       StringTableData.BundleList.push_back(Key);
437       auto EmplaceResult =
438           BundleData.emplace(Key, StringTableInfo::Bundle(ObjectData));
439       assert(EmplaceResult.second && "Could not create a bundle");
440       Iter = EmplaceResult.first;
441     }
442 
443     RETURN_IF_ERROR(
444         insertStringIntoBundle(Iter->second, String.first, String.second));
445   }
446 
447   return Error::success();
448 }
449 
450 Error ResourceFileWriter::visitUserDefinedResource(const RCResource *Res) {
451   return writeResource(Res, &ResourceFileWriter::writeUserDefinedBody);
452 }
453 
454 Error ResourceFileWriter::visitVersionInfoResource(const RCResource *Res) {
455   return writeResource(Res, &ResourceFileWriter::writeVersionInfoBody);
456 }
457 
458 Error ResourceFileWriter::visitCharacteristicsStmt(
459     const CharacteristicsStmt *Stmt) {
460   ObjectData.Characteristics = Stmt->Value;
461   return Error::success();
462 }
463 
464 Error ResourceFileWriter::visitFontStmt(const FontStmt *Stmt) {
465   RETURN_IF_ERROR(checkNumberFits<uint16_t>(Stmt->Size, "Font size"));
466   RETURN_IF_ERROR(checkNumberFits<uint16_t>(Stmt->Weight, "Font weight"));
467   RETURN_IF_ERROR(checkNumberFits<uint8_t>(Stmt->Charset, "Font charset"));
468   ObjectInfo::FontInfo Font{Stmt->Size, Stmt->Name, Stmt->Weight, Stmt->Italic,
469                             Stmt->Charset};
470   ObjectData.Font.emplace(Font);
471   return Error::success();
472 }
473 
474 Error ResourceFileWriter::visitLanguageStmt(const LanguageResource *Stmt) {
475   RETURN_IF_ERROR(checkNumberFits(Stmt->Lang, 10, "Primary language ID"));
476   RETURN_IF_ERROR(checkNumberFits(Stmt->SubLang, 6, "Sublanguage ID"));
477   ObjectData.LanguageInfo = Stmt->Lang | (Stmt->SubLang << 10);
478   return Error::success();
479 }
480 
481 Error ResourceFileWriter::visitStyleStmt(const StyleStmt *Stmt) {
482   ObjectData.Style = Stmt->Value;
483   return Error::success();
484 }
485 
486 Error ResourceFileWriter::visitVersionStmt(const VersionStmt *Stmt) {
487   ObjectData.VersionInfo = Stmt->Value;
488   return Error::success();
489 }
490 
491 Error ResourceFileWriter::writeResource(
492     const RCResource *Res,
493     Error (ResourceFileWriter::*BodyWriter)(const RCResource *)) {
494   // We don't know the sizes yet.
495   object::WinResHeaderPrefix HeaderPrefix{ulittle32_t(0U), ulittle32_t(0U)};
496   uint64_t HeaderLoc = writeObject(HeaderPrefix);
497 
498   auto ResType = Res->getResourceType();
499   RETURN_IF_ERROR(checkIntOrString(ResType, "Resource type"));
500   RETURN_IF_ERROR(checkIntOrString(Res->ResName, "Resource ID"));
501   RETURN_IF_ERROR(handleError(writeIdentifier(ResType), Res));
502   RETURN_IF_ERROR(handleError(writeIdentifier(Res->ResName), Res));
503 
504   // Apply the resource-local optional statements.
505   ContextKeeper RAII(this);
506   RETURN_IF_ERROR(handleError(Res->applyStmts(this), Res));
507 
508   padStream(sizeof(uint32_t));
509   object::WinResHeaderSuffix HeaderSuffix{
510       ulittle32_t(0), // DataVersion; seems to always be 0
511       ulittle16_t(Res->getMemoryFlags()), ulittle16_t(ObjectData.LanguageInfo),
512       ulittle32_t(ObjectData.VersionInfo),
513       ulittle32_t(ObjectData.Characteristics)};
514   writeObject(HeaderSuffix);
515 
516   uint64_t DataLoc = tell();
517   RETURN_IF_ERROR(handleError((this->*BodyWriter)(Res), Res));
518   // RETURN_IF_ERROR(handleError(dumpResource(Ctx)));
519 
520   // Update the sizes.
521   HeaderPrefix.DataSize = tell() - DataLoc;
522   HeaderPrefix.HeaderSize = DataLoc - HeaderLoc;
523   writeObjectAt(HeaderPrefix, HeaderLoc);
524   padStream(sizeof(uint32_t));
525 
526   return Error::success();
527 }
528 
529 // --- NullResource helpers. --- //
530 
531 Error ResourceFileWriter::writeNullBody(const RCResource *) {
532   return Error::success();
533 }
534 
535 // --- AcceleratorsResource helpers. --- //
536 
537 Error ResourceFileWriter::writeSingleAccelerator(
538     const AcceleratorsResource::Accelerator &Obj, bool IsLastItem) {
539   using Accelerator = AcceleratorsResource::Accelerator;
540   using Opt = Accelerator::Options;
541 
542   struct AccelTableEntry {
543     ulittle16_t Flags;
544     ulittle16_t ANSICode;
545     ulittle16_t Id;
546     uint16_t Padding;
547   } Entry{ulittle16_t(0), ulittle16_t(0), ulittle16_t(0), 0};
548 
549   bool IsASCII = Obj.Flags & Opt::ASCII, IsVirtKey = Obj.Flags & Opt::VIRTKEY;
550 
551   // Remove ASCII flags (which doesn't occur in .res files).
552   Entry.Flags = Obj.Flags & ~Opt::ASCII;
553 
554   if (IsLastItem)
555     Entry.Flags |= 0x80;
556 
557   RETURN_IF_ERROR(checkNumberFits<uint16_t>(Obj.Id, "ACCELERATORS entry ID"));
558   Entry.Id = ulittle16_t(Obj.Id);
559 
560   auto createAccError = [&Obj](const char *Msg) {
561     return createError("Accelerator ID " + Twine(Obj.Id) + ": " + Msg);
562   };
563 
564   if (IsASCII && IsVirtKey)
565     return createAccError("Accelerator can't be both ASCII and VIRTKEY");
566 
567   if (!IsVirtKey && (Obj.Flags & (Opt::ALT | Opt::SHIFT | Opt::CONTROL)))
568     return createAccError("Can only apply ALT, SHIFT or CONTROL to VIRTKEY"
569                           " accelerators");
570 
571   if (Obj.Event.isInt()) {
572     if (!IsASCII && !IsVirtKey)
573       return createAccError(
574           "Accelerator with a numeric event must be either ASCII"
575           " or VIRTKEY");
576 
577     uint32_t EventVal = Obj.Event.getInt();
578     RETURN_IF_ERROR(
579         checkNumberFits<uint16_t>(EventVal, "Numeric event key ID"));
580     Entry.ANSICode = ulittle16_t(EventVal);
581     writeObject(Entry);
582     return Error::success();
583   }
584 
585   StringRef Str = Obj.Event.getString();
586   bool IsWide;
587   stripQuotes(Str, IsWide);
588 
589   if (Str.size() == 0 || Str.size() > 2)
590     return createAccError(
591         "Accelerator string events should have length 1 or 2");
592 
593   if (Str[0] == '^') {
594     if (Str.size() == 1)
595       return createAccError("No character following '^' in accelerator event");
596     if (IsVirtKey)
597       return createAccError(
598           "VIRTKEY accelerator events can't be preceded by '^'");
599 
600     char Ch = Str[1];
601     if (Ch >= 'a' && Ch <= 'z')
602       Entry.ANSICode = ulittle16_t(Ch - 'a' + 1);
603     else if (Ch >= 'A' && Ch <= 'Z')
604       Entry.ANSICode = ulittle16_t(Ch - 'A' + 1);
605     else
606       return createAccError("Control character accelerator event should be"
607                             " alphabetic");
608 
609     writeObject(Entry);
610     return Error::success();
611   }
612 
613   if (Str.size() == 2)
614     return createAccError("Event string should be one-character, possibly"
615                           " preceded by '^'");
616 
617   uint8_t EventCh = Str[0];
618   // The original tool just warns in this situation. We chose to fail.
619   if (IsVirtKey && !isalnum(EventCh))
620     return createAccError("Non-alphanumeric characters cannot describe virtual"
621                           " keys");
622   if (EventCh > 0x7F)
623     return createAccError("Non-ASCII description of accelerator");
624 
625   if (IsVirtKey)
626     EventCh = toupper(EventCh);
627   Entry.ANSICode = ulittle16_t(EventCh);
628   writeObject(Entry);
629   return Error::success();
630 }
631 
632 Error ResourceFileWriter::writeAcceleratorsBody(const RCResource *Base) {
633   auto *Res = cast<AcceleratorsResource>(Base);
634   size_t AcceleratorId = 0;
635   for (auto &Acc : Res->Accelerators) {
636     ++AcceleratorId;
637     RETURN_IF_ERROR(
638         writeSingleAccelerator(Acc, AcceleratorId == Res->Accelerators.size()));
639   }
640   return Error::success();
641 }
642 
643 // --- CursorResource and IconResource helpers. --- //
644 
645 // ICONRESDIR structure. Describes a single icon in resouce group.
646 //
647 // Ref: msdn.microsoft.com/en-us/library/windows/desktop/ms648016.aspx
648 struct IconResDir {
649   uint8_t Width;
650   uint8_t Height;
651   uint8_t ColorCount;
652   uint8_t Reserved;
653 };
654 
655 // CURSORDIR structure. Describes a single cursor in resource group.
656 //
657 // Ref: msdn.microsoft.com/en-us/library/windows/desktop/ms648011(v=vs.85).aspx
658 struct CursorDir {
659   ulittle16_t Width;
660   ulittle16_t Height;
661 };
662 
663 // RESDIRENTRY structure, stripped from the last item. Stripping made
664 // for compatibility with RESDIR.
665 //
666 // Ref: msdn.microsoft.com/en-us/library/windows/desktop/ms648026(v=vs.85).aspx
667 struct ResourceDirEntryStart {
668   union {
669     CursorDir Cursor; // Used in CURSOR resources.
670     IconResDir Icon;  // Used in .ico and .cur files, and ICON resources.
671   };
672   ulittle16_t Planes;   // HotspotX (.cur files but not CURSOR resource).
673   ulittle16_t BitCount; // HotspotY (.cur files but not CURSOR resource).
674   ulittle32_t Size;
675   // ulittle32_t ImageOffset;  // Offset to image data (ICONDIRENTRY only).
676   // ulittle16_t IconID;       // Resource icon ID (RESDIR only).
677 };
678 
679 // BITMAPINFOHEADER structure. Describes basic information about the bitmap
680 // being read.
681 //
682 // Ref: msdn.microsoft.com/en-us/library/windows/desktop/dd183376(v=vs.85).aspx
683 struct BitmapInfoHeader {
684   ulittle32_t Size;
685   ulittle32_t Width;
686   ulittle32_t Height;
687   ulittle16_t Planes;
688   ulittle16_t BitCount;
689   ulittle32_t Compression;
690   ulittle32_t SizeImage;
691   ulittle32_t XPelsPerMeter;
692   ulittle32_t YPelsPerMeter;
693   ulittle32_t ClrUsed;
694   ulittle32_t ClrImportant;
695 };
696 
697 // Group icon directory header. Called ICONDIR in .ico/.cur files and
698 // NEWHEADER in .res files.
699 //
700 // Ref: msdn.microsoft.com/en-us/library/windows/desktop/ms648023(v=vs.85).aspx
701 struct GroupIconDir {
702   ulittle16_t Reserved; // Always 0.
703   ulittle16_t ResType;  // 1 for icons, 2 for cursors.
704   ulittle16_t ResCount; // Number of items.
705 };
706 
707 enum class IconCursorGroupType { Icon, Cursor };
708 
709 class SingleIconCursorResource : public RCResource {
710 public:
711   IconCursorGroupType Type;
712   const ResourceDirEntryStart &Header;
713   ArrayRef<uint8_t> Image;
714 
715   SingleIconCursorResource(IconCursorGroupType ResourceType,
716                            const ResourceDirEntryStart &HeaderEntry,
717                            ArrayRef<uint8_t> ImageData)
718       : Type(ResourceType), Header(HeaderEntry), Image(ImageData) {}
719 
720   Twine getResourceTypeName() const override { return "Icon/cursor image"; }
721   IntOrString getResourceType() const override {
722     return Type == IconCursorGroupType::Icon ? RkSingleIcon : RkSingleCursor;
723   }
724   uint16_t getMemoryFlags() const override {
725     return MfDiscardable | MfMoveable;
726   }
727   ResourceKind getKind() const override { return RkSingleCursorOrIconRes; }
728   static bool classof(const RCResource *Res) {
729     return Res->getKind() == RkSingleCursorOrIconRes;
730   }
731 };
732 
733 class IconCursorGroupResource : public RCResource {
734 public:
735   IconCursorGroupType Type;
736   GroupIconDir Header;
737   std::vector<ResourceDirEntryStart> ItemEntries;
738 
739   IconCursorGroupResource(IconCursorGroupType ResourceType,
740                           const GroupIconDir &HeaderData,
741                           std::vector<ResourceDirEntryStart> &&Entries)
742       : Type(ResourceType), Header(HeaderData),
743         ItemEntries(std::move(Entries)) {}
744 
745   Twine getResourceTypeName() const override { return "Icon/cursor group"; }
746   IntOrString getResourceType() const override {
747     return Type == IconCursorGroupType::Icon ? RkIconGroup : RkCursorGroup;
748   }
749   ResourceKind getKind() const override { return RkCursorOrIconGroupRes; }
750   static bool classof(const RCResource *Res) {
751     return Res->getKind() == RkCursorOrIconGroupRes;
752   }
753 };
754 
755 Error ResourceFileWriter::writeSingleIconOrCursorBody(const RCResource *Base) {
756   auto *Res = cast<SingleIconCursorResource>(Base);
757   if (Res->Type == IconCursorGroupType::Cursor) {
758     // In case of cursors, two WORDS are appended to the beginning
759     // of the resource: HotspotX (Planes in RESDIRENTRY),
760     // and HotspotY (BitCount).
761     //
762     // Ref: msdn.microsoft.com/en-us/library/windows/desktop/ms648026.aspx
763     //  (Remarks section).
764     writeObject(Res->Header.Planes);
765     writeObject(Res->Header.BitCount);
766   }
767 
768   writeObject(Res->Image);
769   return Error::success();
770 }
771 
772 Error ResourceFileWriter::writeIconOrCursorGroupBody(const RCResource *Base) {
773   auto *Res = cast<IconCursorGroupResource>(Base);
774   writeObject(Res->Header);
775   for (auto Item : Res->ItemEntries) {
776     writeObject(Item);
777     writeInt(IconCursorID++);
778   }
779   return Error::success();
780 }
781 
782 Error ResourceFileWriter::visitSingleIconOrCursor(const RCResource *Res) {
783   return writeResource(Res, &ResourceFileWriter::writeSingleIconOrCursorBody);
784 }
785 
786 Error ResourceFileWriter::visitIconOrCursorGroup(const RCResource *Res) {
787   return writeResource(Res, &ResourceFileWriter::writeIconOrCursorGroupBody);
788 }
789 
790 Error ResourceFileWriter::visitIconOrCursorResource(const RCResource *Base) {
791   IconCursorGroupType Type;
792   StringRef FileStr;
793   IntOrString ResName = Base->ResName;
794 
795   if (auto *IconRes = dyn_cast<IconResource>(Base)) {
796     FileStr = IconRes->IconLoc;
797     Type = IconCursorGroupType::Icon;
798   } else {
799     auto *CursorRes = dyn_cast<CursorResource>(Base);
800     FileStr = CursorRes->CursorLoc;
801     Type = IconCursorGroupType::Cursor;
802   }
803 
804   bool IsLong;
805   stripQuotes(FileStr, IsLong);
806   auto File = loadFile(FileStr);
807 
808   if (!File)
809     return File.takeError();
810 
811   BinaryStreamReader Reader((*File)->getBuffer(), support::little);
812 
813   // Read the file headers.
814   //   - At the beginning, ICONDIR/NEWHEADER header.
815   //   - Then, a number of RESDIR headers follow. These contain offsets
816   //       to data.
817   const GroupIconDir *Header;
818 
819   RETURN_IF_ERROR(Reader.readObject(Header));
820   if (Header->Reserved != 0)
821     return createError("Incorrect icon/cursor Reserved field; should be 0.");
822   uint16_t NeededType = Type == IconCursorGroupType::Icon ? 1 : 2;
823   if (Header->ResType != NeededType)
824     return createError("Incorrect icon/cursor ResType field; should be " +
825                        Twine(NeededType) + ".");
826 
827   uint16_t NumItems = Header->ResCount;
828 
829   // Read single ico/cur headers.
830   std::vector<ResourceDirEntryStart> ItemEntries;
831   ItemEntries.reserve(NumItems);
832   std::vector<uint32_t> ItemOffsets(NumItems);
833   for (size_t ID = 0; ID < NumItems; ++ID) {
834     const ResourceDirEntryStart *Object;
835     RETURN_IF_ERROR(Reader.readObject(Object));
836     ItemEntries.push_back(*Object);
837     RETURN_IF_ERROR(Reader.readInteger(ItemOffsets[ID]));
838   }
839 
840   // Now write each icon/cursors one by one. At first, all the contents
841   // without ICO/CUR header. This is described by SingleIconCursorResource.
842   for (size_t ID = 0; ID < NumItems; ++ID) {
843     // Load the fragment of file.
844     Reader.setOffset(ItemOffsets[ID]);
845     ArrayRef<uint8_t> Image;
846     RETURN_IF_ERROR(Reader.readArray(Image, ItemEntries[ID].Size));
847     SingleIconCursorResource SingleRes(Type, ItemEntries[ID], Image);
848     SingleRes.setName(IconCursorID + ID);
849     RETURN_IF_ERROR(visitSingleIconOrCursor(&SingleRes));
850   }
851 
852   // Now, write all the headers concatenated into a separate resource.
853   for (size_t ID = 0; ID < NumItems; ++ID) {
854     if (Type == IconCursorGroupType::Icon) {
855       // rc.exe seems to always set NumPlanes to 1. No idea why it happens.
856       ItemEntries[ID].Planes = 1;
857       continue;
858     }
859 
860     // We need to rewrite the cursor headers.
861     const auto &OldHeader = ItemEntries[ID];
862     ResourceDirEntryStart NewHeader;
863     NewHeader.Cursor.Width = OldHeader.Icon.Width;
864     // Each cursor in fact stores two bitmaps, one under another.
865     // Height provided in cursor definition describes the height of the
866     // cursor, whereas the value existing in resource definition describes
867     // the height of the bitmap. Therefore, we need to double this height.
868     NewHeader.Cursor.Height = OldHeader.Icon.Height * 2;
869 
870     // Now, we actually need to read the bitmap header to find
871     // the number of planes and the number of bits per pixel.
872     Reader.setOffset(ItemOffsets[ID]);
873     const BitmapInfoHeader *BMPHeader;
874     RETURN_IF_ERROR(Reader.readObject(BMPHeader));
875     NewHeader.Planes = BMPHeader->Planes;
876     NewHeader.BitCount = BMPHeader->BitCount;
877 
878     // Two WORDs were written at the beginning of the resource (hotspot
879     // location). This is reflected in Size field.
880     NewHeader.Size = OldHeader.Size + 2 * sizeof(uint16_t);
881 
882     ItemEntries[ID] = NewHeader;
883   }
884 
885   IconCursorGroupResource HeaderRes(Type, *Header, std::move(ItemEntries));
886   HeaderRes.setName(ResName);
887   RETURN_IF_ERROR(visitIconOrCursorGroup(&HeaderRes));
888 
889   return Error::success();
890 }
891 
892 // --- DialogResource helpers. --- //
893 
894 Error ResourceFileWriter::writeSingleDialogControl(const Control &Ctl,
895                                                    bool IsExtended) {
896   // Each control should be aligned to DWORD.
897   padStream(sizeof(uint32_t));
898 
899   auto TypeInfo = Control::SupportedCtls.lookup(Ctl.Type);
900   uint32_t CtlStyle = TypeInfo.Style | Ctl.Style.getValueOr(0);
901   uint32_t CtlExtStyle = Ctl.ExtStyle.getValueOr(0);
902 
903   // DIALOG(EX) item header prefix.
904   if (!IsExtended) {
905     struct {
906       ulittle32_t Style;
907       ulittle32_t ExtStyle;
908     } Prefix{ulittle32_t(CtlStyle), ulittle32_t(CtlExtStyle)};
909     writeObject(Prefix);
910   } else {
911     struct {
912       ulittle32_t HelpID;
913       ulittle32_t ExtStyle;
914       ulittle32_t Style;
915     } Prefix{ulittle32_t(Ctl.HelpID.getValueOr(0)), ulittle32_t(CtlExtStyle),
916              ulittle32_t(CtlStyle)};
917     writeObject(Prefix);
918   }
919 
920   // Common fixed-length part.
921   RETURN_IF_ERROR(checkSignedNumberFits<int16_t>(
922       Ctl.X, "Dialog control x-coordinate", true));
923   RETURN_IF_ERROR(checkSignedNumberFits<int16_t>(
924       Ctl.Y, "Dialog control y-coordinate", true));
925   RETURN_IF_ERROR(
926       checkSignedNumberFits<int16_t>(Ctl.Width, "Dialog control width", false));
927   RETURN_IF_ERROR(checkSignedNumberFits<int16_t>(
928       Ctl.Height, "Dialog control height", false));
929   struct {
930     ulittle16_t X;
931     ulittle16_t Y;
932     ulittle16_t Width;
933     ulittle16_t Height;
934   } Middle{ulittle16_t(Ctl.X), ulittle16_t(Ctl.Y), ulittle16_t(Ctl.Width),
935            ulittle16_t(Ctl.Height)};
936   writeObject(Middle);
937 
938   // ID; it's 16-bit in DIALOG and 32-bit in DIALOGEX.
939   if (!IsExtended) {
940     RETURN_IF_ERROR(checkNumberFits<uint16_t>(
941         Ctl.ID, "Control ID in simple DIALOG resource"));
942     writeInt<uint16_t>(Ctl.ID);
943   } else {
944     writeInt<uint32_t>(Ctl.ID);
945   }
946 
947   // Window class - either 0xFFFF + 16-bit integer or a string.
948   RETURN_IF_ERROR(writeIntOrString(IntOrString(TypeInfo.CtlClass)));
949 
950   // Element caption/reference ID. ID is preceded by 0xFFFF.
951   RETURN_IF_ERROR(checkIntOrString(Ctl.Title, "Control reference ID"));
952   RETURN_IF_ERROR(writeIntOrString(Ctl.Title));
953 
954   // # bytes of extra creation data count. Don't pass any.
955   writeInt<uint16_t>(0);
956 
957   return Error::success();
958 }
959 
960 Error ResourceFileWriter::writeDialogBody(const RCResource *Base) {
961   auto *Res = cast<DialogResource>(Base);
962 
963   // Default style: WS_POPUP | WS_BORDER | WS_SYSMENU.
964   const uint32_t DefaultStyle = 0x80880000;
965   const uint32_t StyleFontFlag = 0x40;
966   const uint32_t StyleCaptionFlag = 0x00C00000;
967 
968   uint32_t UsedStyle = ObjectData.Style.getValueOr(DefaultStyle);
969   if (ObjectData.Font)
970     UsedStyle |= StyleFontFlag;
971   else
972     UsedStyle &= ~StyleFontFlag;
973 
974   // Actually, in case of empty (but existent) caption, the examined field
975   // is equal to "\"\"". That's why empty captions are still noticed.
976   if (ObjectData.Caption != "")
977     UsedStyle |= StyleCaptionFlag;
978 
979   const uint16_t DialogExMagic = 0xFFFF;
980 
981   // Write DIALOG(EX) header prefix. These are pretty different.
982   if (!Res->IsExtended) {
983     // We cannot let the higher word of DefaultStyle be equal to 0xFFFF.
984     // In such a case, whole object (in .res file) is equivalent to a
985     // DIALOGEX. It might lead to access violation/segmentation fault in
986     // resource readers. For example,
987     //   1 DIALOG 0, 0, 0, 65432
988     //   STYLE 0xFFFF0001 {}
989     // would be compiled to a DIALOGEX with 65432 controls.
990     if ((UsedStyle >> 16) == DialogExMagic)
991       return createError("16 higher bits of DIALOG resource style cannot be"
992                          " equal to 0xFFFF");
993 
994     struct {
995       ulittle32_t Style;
996       ulittle32_t ExtStyle;
997     } Prefix{ulittle32_t(UsedStyle),
998              ulittle32_t(0)}; // As of now, we don't keep EXSTYLE.
999 
1000     writeObject(Prefix);
1001   } else {
1002     struct {
1003       ulittle16_t Version;
1004       ulittle16_t Magic;
1005       ulittle32_t HelpID;
1006       ulittle32_t ExtStyle;
1007       ulittle32_t Style;
1008     } Prefix{ulittle16_t(1), ulittle16_t(DialogExMagic),
1009              ulittle32_t(Res->HelpID), ulittle32_t(0), ulittle32_t(UsedStyle)};
1010 
1011     writeObject(Prefix);
1012   }
1013 
1014   // Now, a common part. First, fixed-length fields.
1015   RETURN_IF_ERROR(checkNumberFits<uint16_t>(Res->Controls.size(),
1016                                             "Number of dialog controls"));
1017   RETURN_IF_ERROR(
1018       checkSignedNumberFits<int16_t>(Res->X, "Dialog x-coordinate", true));
1019   RETURN_IF_ERROR(
1020       checkSignedNumberFits<int16_t>(Res->Y, "Dialog y-coordinate", true));
1021   RETURN_IF_ERROR(
1022       checkSignedNumberFits<int16_t>(Res->Width, "Dialog width", false));
1023   RETURN_IF_ERROR(
1024       checkSignedNumberFits<int16_t>(Res->Height, "Dialog height", false));
1025   struct {
1026     ulittle16_t Count;
1027     ulittle16_t PosX;
1028     ulittle16_t PosY;
1029     ulittle16_t DialogWidth;
1030     ulittle16_t DialogHeight;
1031   } Middle{ulittle16_t(Res->Controls.size()), ulittle16_t(Res->X),
1032            ulittle16_t(Res->Y), ulittle16_t(Res->Width),
1033            ulittle16_t(Res->Height)};
1034   writeObject(Middle);
1035 
1036   // MENU field. As of now, we don't keep them in the state and can peacefully
1037   // think there is no menu attached to the dialog.
1038   writeInt<uint16_t>(0);
1039 
1040   // Window CLASS field. Not kept here.
1041   writeInt<uint16_t>(0);
1042 
1043   // Window title or a single word equal to 0.
1044   RETURN_IF_ERROR(writeCString(ObjectData.Caption));
1045 
1046   // If there *is* a window font declared, output its data.
1047   auto &Font = ObjectData.Font;
1048   if (Font) {
1049     writeInt<uint16_t>(Font->Size);
1050     // Additional description occurs only in DIALOGEX.
1051     if (Res->IsExtended) {
1052       writeInt<uint16_t>(Font->Weight);
1053       writeInt<uint8_t>(Font->IsItalic);
1054       writeInt<uint8_t>(Font->Charset);
1055     }
1056     RETURN_IF_ERROR(writeCString(Font->Typeface));
1057   }
1058 
1059   auto handleCtlError = [&](Error &&Err, const Control &Ctl) -> Error {
1060     if (!Err)
1061       return Error::success();
1062     return joinErrors(createError("Error in " + Twine(Ctl.Type) +
1063                                   " control  (ID " + Twine(Ctl.ID) + "):"),
1064                       std::move(Err));
1065   };
1066 
1067   for (auto &Ctl : Res->Controls)
1068     RETURN_IF_ERROR(
1069         handleCtlError(writeSingleDialogControl(Ctl, Res->IsExtended), Ctl));
1070 
1071   return Error::success();
1072 }
1073 
1074 // --- HTMLResource helpers. --- //
1075 
1076 Error ResourceFileWriter::writeHTMLBody(const RCResource *Base) {
1077   return appendFile(cast<HTMLResource>(Base)->HTMLLoc);
1078 }
1079 
1080 // --- MenuResource helpers. --- //
1081 
1082 Error ResourceFileWriter::writeMenuDefinition(
1083     const std::unique_ptr<MenuDefinition> &Def, uint16_t Flags) {
1084   assert(Def);
1085   const MenuDefinition *DefPtr = Def.get();
1086 
1087   if (auto *MenuItemPtr = dyn_cast<MenuItem>(DefPtr)) {
1088     writeInt<uint16_t>(Flags);
1089     RETURN_IF_ERROR(
1090         checkNumberFits<uint16_t>(MenuItemPtr->Id, "MENUITEM action ID"));
1091     writeInt<uint16_t>(MenuItemPtr->Id);
1092     RETURN_IF_ERROR(writeCString(MenuItemPtr->Name));
1093     return Error::success();
1094   }
1095 
1096   if (isa<MenuSeparator>(DefPtr)) {
1097     writeInt<uint16_t>(Flags);
1098     writeInt<uint32_t>(0);
1099     return Error::success();
1100   }
1101 
1102   auto *PopupPtr = cast<PopupItem>(DefPtr);
1103   writeInt<uint16_t>(Flags);
1104   RETURN_IF_ERROR(writeCString(PopupPtr->Name));
1105   return writeMenuDefinitionList(PopupPtr->SubItems);
1106 }
1107 
1108 Error ResourceFileWriter::writeMenuDefinitionList(
1109     const MenuDefinitionList &List) {
1110   for (auto &Def : List.Definitions) {
1111     uint16_t Flags = Def->getResFlags();
1112     // Last element receives an additional 0x80 flag.
1113     const uint16_t LastElementFlag = 0x0080;
1114     if (&Def == &List.Definitions.back())
1115       Flags |= LastElementFlag;
1116 
1117     RETURN_IF_ERROR(writeMenuDefinition(Def, Flags));
1118   }
1119   return Error::success();
1120 }
1121 
1122 Error ResourceFileWriter::writeMenuBody(const RCResource *Base) {
1123   // At first, MENUHEADER structure. In fact, these are two WORDs equal to 0.
1124   // Ref: msdn.microsoft.com/en-us/library/windows/desktop/ms648018.aspx
1125   writeInt<uint32_t>(0);
1126 
1127   return writeMenuDefinitionList(cast<MenuResource>(Base)->Elements);
1128 }
1129 
1130 // --- StringTableResource helpers. --- //
1131 
1132 class BundleResource : public RCResource {
1133 public:
1134   using BundleType = ResourceFileWriter::StringTableInfo::Bundle;
1135   BundleType Bundle;
1136 
1137   BundleResource(const BundleType &StrBundle) : Bundle(StrBundle) {}
1138   IntOrString getResourceType() const override { return 6; }
1139 
1140   ResourceKind getKind() const override { return RkStringTableBundle; }
1141   static bool classof(const RCResource *Res) {
1142     return Res->getKind() == RkStringTableBundle;
1143   }
1144 };
1145 
1146 Error ResourceFileWriter::visitStringTableBundle(const RCResource *Res) {
1147   return writeResource(Res, &ResourceFileWriter::writeStringTableBundleBody);
1148 }
1149 
1150 Error ResourceFileWriter::insertStringIntoBundle(
1151     StringTableInfo::Bundle &Bundle, uint16_t StringID, StringRef String) {
1152   uint16_t StringLoc = StringID & 15;
1153   if (Bundle.Data[StringLoc])
1154     return createError("Multiple STRINGTABLE strings located under ID " +
1155                        Twine(StringID));
1156   Bundle.Data[StringLoc] = String;
1157   return Error::success();
1158 }
1159 
1160 Error ResourceFileWriter::writeStringTableBundleBody(const RCResource *Base) {
1161   auto *Res = cast<BundleResource>(Base);
1162   for (size_t ID = 0; ID < Res->Bundle.Data.size(); ++ID) {
1163     // The string format is a tiny bit different here. We
1164     // first output the size of the string, and then the string itself
1165     // (which is not null-terminated).
1166     bool IsLongString;
1167     SmallVector<UTF16, 128> Data;
1168     RETURN_IF_ERROR(processString(Res->Bundle.Data[ID].getValueOr(StringRef()),
1169                                   NullHandlingMethod::CutAtDoubleNull,
1170                                   IsLongString, Data));
1171     if (AppendNull && Res->Bundle.Data[ID])
1172       Data.push_back('\0');
1173     RETURN_IF_ERROR(
1174         checkNumberFits<uint16_t>(Data.size(), "STRINGTABLE string size"));
1175     writeInt<uint16_t>(Data.size());
1176     for (auto Char : Data)
1177       writeInt(Char);
1178   }
1179   return Error::success();
1180 }
1181 
1182 Error ResourceFileWriter::dumpAllStringTables() {
1183   for (auto Key : StringTableData.BundleList) {
1184     auto Iter = StringTableData.BundleData.find(Key);
1185     assert(Iter != StringTableData.BundleData.end());
1186 
1187     // For a moment, revert the context info to moment of bundle declaration.
1188     ContextKeeper RAII(this);
1189     ObjectData = Iter->second.DeclTimeInfo;
1190 
1191     BundleResource Res(Iter->second);
1192     // Bundle #(k+1) contains keys [16k, 16k + 15].
1193     Res.setName(Key.first + 1);
1194     RETURN_IF_ERROR(visitStringTableBundle(&Res));
1195   }
1196   return Error::success();
1197 }
1198 
1199 // --- UserDefinedResource helpers. --- //
1200 
1201 Error ResourceFileWriter::writeUserDefinedBody(const RCResource *Base) {
1202   auto *Res = cast<UserDefinedResource>(Base);
1203 
1204   if (Res->IsFileResource)
1205     return appendFile(Res->FileLoc);
1206 
1207   for (auto &Elem : Res->Contents) {
1208     if (Elem.isInt()) {
1209       RETURN_IF_ERROR(
1210           checkRCInt(Elem.getInt(), "Number in user-defined resource"));
1211       writeRCInt(Elem.getInt());
1212       continue;
1213     }
1214 
1215     SmallVector<UTF16, 128> ProcessedString;
1216     bool IsLongString;
1217     RETURN_IF_ERROR(processString(Elem.getString(),
1218                                   NullHandlingMethod::UserResource,
1219                                   IsLongString, ProcessedString));
1220 
1221     for (auto Ch : ProcessedString) {
1222       if (IsLongString) {
1223         writeInt(Ch);
1224         continue;
1225       }
1226 
1227       RETURN_IF_ERROR(checkNumberFits<uint8_t>(
1228           Ch, "Character in narrow string in user-defined resource"));
1229       writeInt<uint8_t>(Ch);
1230     }
1231   }
1232 
1233   return Error::success();
1234 }
1235 
1236 // --- VersionInfoResourceResource helpers. --- //
1237 
1238 Error ResourceFileWriter::writeVersionInfoBlock(const VersionInfoBlock &Blk) {
1239   // Output the header if the block has name.
1240   bool OutputHeader = Blk.Name != "";
1241   uint64_t LengthLoc;
1242 
1243   if (OutputHeader) {
1244     LengthLoc = writeInt<uint16_t>(0);
1245     writeInt<uint16_t>(0);
1246     writeInt<uint16_t>(1); // true
1247     RETURN_IF_ERROR(writeCString(Blk.Name));
1248     padStream(sizeof(uint32_t));
1249   }
1250 
1251   for (const std::unique_ptr<VersionInfoStmt> &Item : Blk.Stmts) {
1252     VersionInfoStmt *ItemPtr = Item.get();
1253 
1254     if (auto *BlockPtr = dyn_cast<VersionInfoBlock>(ItemPtr)) {
1255       RETURN_IF_ERROR(writeVersionInfoBlock(*BlockPtr));
1256       continue;
1257     }
1258 
1259     auto *ValuePtr = cast<VersionInfoValue>(ItemPtr);
1260     RETURN_IF_ERROR(writeVersionInfoValue(*ValuePtr));
1261   }
1262 
1263   if (OutputHeader) {
1264     uint64_t CurLoc = tell();
1265     writeObjectAt(ulittle16_t(CurLoc - LengthLoc), LengthLoc);
1266   }
1267 
1268   padStream(sizeof(uint32_t));
1269   return Error::success();
1270 }
1271 
1272 Error ResourceFileWriter::writeVersionInfoValue(const VersionInfoValue &Val) {
1273   // rc has a peculiar algorithm to output VERSIONINFO VALUEs. Each VALUE
1274   // is a mapping from the key (string) to the value (a sequence of ints or
1275   // a sequence of strings).
1276   //
1277   // If integers are to be written: width of each integer written depends on
1278   // whether it's been declared 'long' (it's DWORD then) or not (it's WORD).
1279   // ValueLength defined in structure referenced below is then the total
1280   // number of bytes taken by these integers.
1281   //
1282   // If strings are to be written: characters are always WORDs.
1283   // Moreover, '\0' character is written after the last string, and between
1284   // every two strings separated by comma (if strings are not comma-separated,
1285   // they're simply concatenated). ValueLength is equal to the number of WORDs
1286   // written (that is, half of the bytes written).
1287   //
1288   // Ref: msdn.microsoft.com/en-us/library/windows/desktop/ms646994.aspx
1289   bool HasStrings = false, HasInts = false;
1290   for (auto &Item : Val.Values)
1291     (Item.isInt() ? HasInts : HasStrings) = true;
1292 
1293   assert((HasStrings || HasInts) && "VALUE must have at least one argument");
1294   if (HasStrings && HasInts)
1295     return createError(Twine("VALUE ") + Val.Key +
1296                        " cannot contain both strings and integers");
1297 
1298   auto LengthLoc = writeInt<uint16_t>(0);
1299   auto ValLengthLoc = writeInt<uint16_t>(0);
1300   writeInt<uint16_t>(HasStrings);
1301   RETURN_IF_ERROR(writeCString(Val.Key));
1302   padStream(sizeof(uint32_t));
1303 
1304   auto DataLoc = tell();
1305   for (size_t Id = 0; Id < Val.Values.size(); ++Id) {
1306     auto &Item = Val.Values[Id];
1307     if (Item.isInt()) {
1308       auto Value = Item.getInt();
1309       RETURN_IF_ERROR(checkRCInt(Value, "VERSIONINFO integer value"));
1310       writeRCInt(Value);
1311       continue;
1312     }
1313 
1314     bool WriteTerminator =
1315         Id == Val.Values.size() - 1 || Val.HasPrecedingComma[Id + 1];
1316     RETURN_IF_ERROR(writeCString(Item.getString(), WriteTerminator));
1317   }
1318 
1319   auto CurLoc = tell();
1320   auto ValueLength = CurLoc - DataLoc;
1321   if (HasStrings) {
1322     assert(ValueLength % 2 == 0);
1323     ValueLength /= 2;
1324   }
1325   writeObjectAt(ulittle16_t(CurLoc - LengthLoc), LengthLoc);
1326   writeObjectAt(ulittle16_t(ValueLength), ValLengthLoc);
1327   padStream(sizeof(uint32_t));
1328   return Error::success();
1329 }
1330 
1331 template <typename Ty>
1332 static Ty getWithDefault(const StringMap<Ty> &Map, StringRef Key,
1333                          const Ty &Default) {
1334   auto Iter = Map.find(Key);
1335   if (Iter != Map.end())
1336     return Iter->getValue();
1337   return Default;
1338 }
1339 
1340 Error ResourceFileWriter::writeVersionInfoBody(const RCResource *Base) {
1341   auto *Res = cast<VersionInfoResource>(Base);
1342 
1343   const auto &FixedData = Res->FixedData;
1344 
1345   struct /* VS_FIXEDFILEINFO */ {
1346     ulittle32_t Signature = ulittle32_t(0xFEEF04BD);
1347     ulittle32_t StructVersion = ulittle32_t(0x10000);
1348     // It's weird to have most-significant DWORD first on the little-endian
1349     // machines, but let it be this way.
1350     ulittle32_t FileVersionMS;
1351     ulittle32_t FileVersionLS;
1352     ulittle32_t ProductVersionMS;
1353     ulittle32_t ProductVersionLS;
1354     ulittle32_t FileFlagsMask;
1355     ulittle32_t FileFlags;
1356     ulittle32_t FileOS;
1357     ulittle32_t FileType;
1358     ulittle32_t FileSubtype;
1359     // MS implementation seems to always set these fields to 0.
1360     ulittle32_t FileDateMS = ulittle32_t(0);
1361     ulittle32_t FileDateLS = ulittle32_t(0);
1362   } FixedInfo;
1363 
1364   // First, VS_VERSIONINFO.
1365   auto LengthLoc = writeInt<uint16_t>(0);
1366   writeInt<uint16_t>(sizeof(FixedInfo));
1367   writeInt<uint16_t>(0);
1368   cantFail(writeCString("VS_VERSION_INFO"));
1369   padStream(sizeof(uint32_t));
1370 
1371   using VersionInfoFixed = VersionInfoResource::VersionInfoFixed;
1372   auto GetField = [&](VersionInfoFixed::VersionInfoFixedType Type) {
1373     static const SmallVector<uint32_t, 4> DefaultOut{0, 0, 0, 0};
1374     if (!FixedData.IsTypePresent[(int)Type])
1375       return DefaultOut;
1376     return FixedData.FixedInfo[(int)Type];
1377   };
1378 
1379   auto FileVer = GetField(VersionInfoFixed::FtFileVersion);
1380   RETURN_IF_ERROR(checkNumberFits<uint16_t>(
1381       *std::max_element(FileVer.begin(), FileVer.end()), "FILEVERSION fields"));
1382   FixedInfo.FileVersionMS = (FileVer[0] << 16) | FileVer[1];
1383   FixedInfo.FileVersionLS = (FileVer[2] << 16) | FileVer[3];
1384 
1385   auto ProdVer = GetField(VersionInfoFixed::FtProductVersion);
1386   RETURN_IF_ERROR(checkNumberFits<uint16_t>(
1387       *std::max_element(ProdVer.begin(), ProdVer.end()),
1388       "PRODUCTVERSION fields"));
1389   FixedInfo.ProductVersionMS = (ProdVer[0] << 16) | ProdVer[1];
1390   FixedInfo.ProductVersionLS = (ProdVer[2] << 16) | ProdVer[3];
1391 
1392   FixedInfo.FileFlagsMask = GetField(VersionInfoFixed::FtFileFlagsMask)[0];
1393   FixedInfo.FileFlags = GetField(VersionInfoFixed::FtFileFlags)[0];
1394   FixedInfo.FileOS = GetField(VersionInfoFixed::FtFileOS)[0];
1395   FixedInfo.FileType = GetField(VersionInfoFixed::FtFileType)[0];
1396   FixedInfo.FileSubtype = GetField(VersionInfoFixed::FtFileSubtype)[0];
1397 
1398   writeObject(FixedInfo);
1399   padStream(sizeof(uint32_t));
1400 
1401   RETURN_IF_ERROR(writeVersionInfoBlock(Res->MainBlock));
1402 
1403   // FIXME: check overflow?
1404   writeObjectAt(ulittle16_t(tell() - LengthLoc), LengthLoc);
1405 
1406   return Error::success();
1407 }
1408 
1409 Expected<std::unique_ptr<MemoryBuffer>>
1410 ResourceFileWriter::loadFile(StringRef File) const {
1411   SmallString<128> Path;
1412   SmallString<128> Cwd;
1413   std::unique_ptr<MemoryBuffer> Result;
1414 
1415   // 1. The current working directory.
1416   sys::fs::current_path(Cwd);
1417   Path.assign(Cwd.begin(), Cwd.end());
1418   sys::path::append(Path, File);
1419   if (sys::fs::exists(Path))
1420     return errorOrToExpected(MemoryBuffer::getFile(Path, -1, false));
1421 
1422   // 2. The directory of the input resource file, if it is different from the
1423   // current
1424   //    working directory.
1425   StringRef InputFileDir = sys::path::parent_path(Params.InputFilePath);
1426   Path.assign(InputFileDir.begin(), InputFileDir.end());
1427   sys::path::append(Path, File);
1428   if (sys::fs::exists(Path))
1429     return errorOrToExpected(MemoryBuffer::getFile(Path, -1, false));
1430 
1431   // 3. All of the include directories specified on the command line.
1432   for (StringRef ForceInclude : Params.Include) {
1433     Path.assign(ForceInclude.begin(), ForceInclude.end());
1434     sys::path::append(Path, File);
1435     if (sys::fs::exists(Path))
1436       return errorOrToExpected(MemoryBuffer::getFile(Path, -1, false));
1437   }
1438 
1439   if (auto Result =
1440           llvm::sys::Process::FindInEnvPath("INCLUDE", File, Params.NoInclude))
1441     return errorOrToExpected(MemoryBuffer::getFile(*Result, -1, false));
1442 
1443   return make_error<StringError>("error : file not found : " + Twine(File),
1444                                  inconvertibleErrorCode());
1445 }
1446 
1447 } // namespace rc
1448 } // namespace llvm
1449