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