1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
use ;
use ;
use PascalString;
use Resource;
use TemplateSerializer;
pub type TemplateItem = ;
;
//decode_TMPL(const void* data, size_t size) {
//StringReader r(data, size);
//using Entry = TemplateEntry;
//using Type = Entry::Type;
//using Format = Entry::Format;
//vector<unique_ptr<Entry>> ret;
//vector<vector<unique_ptr<Entry>>*> write_stack;
//write_stack.emplace_back(&ret);
//bool in_bbit_array = false;
//while (!r.eof()) {
//string name = decode_mac_roman(r.readx(r.get_u8()));
//uint32_t type = r.get_u32b();
//if (write_stack.empty()) {
//throw runtime_error("TMPL ended list with no list open");
//}
//auto* entries = write_stack.back();
//if (in_bbit_array && type != 0x42424954) {
//throw runtime_error("BBIT array length is not a multiple of 8");
//}
// //For templates supported by Resorcerer, see http://www.mathemaesthetics.com/ResTemplates.html
//switch (type) {
//case 0x44564452: // DVDR; Resorcerer-only. Divider line with comment (0 bytes)
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::VOID, Format::DECIMAL, 0, 0, 0));
//break;
//case 0x43415345: { // CASE; Resorcerer-only. Symbolic and/or default value (0 bytes)
// //These appear to be of the format <name>=<value>. <value> is an
// //integer in decimal format or hex (preceded by a $).
//auto tokens = split(name, '=');
//if (tokens.size() != 2) {
//throw runtime_error("CASE entry does not contain exactly one '=' character");
//}
//if (tokens[1].empty()) {
//throw runtime_error("CASE value token is empty");
//}
//name = tokens[0];
//int32_t value = (tokens[1][0] == '$')
//? strtol(&tokens[1][1], nullptr, 16)
//: stol(tokens[1], nullptr, 10);
//entries->back()->case_names.emplace(value, std::move(tokens[0]));
//break;
//}
//case 0x55425954: // UBYT; Resorcerer-only. "unsigned decimal byte"
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::INTEGER, Format::DECIMAL, 1, 0, 0, false));
//break;
//case 0x55575244: // UWRD; Resorcerer-only. "unsigned decimal word"
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::INTEGER, Format::DECIMAL, 2, 0, 0, false));
//break;
//case 0x554C4E47: // ULNG; Resorcerer-only. "unsigned decimal long"
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::INTEGER, Format::DECIMAL, 4, 0, 0, false));
//break;
//case 0x44415445: // DATE; Resorcerer-only. Macintosh System Date/Time (seconds)
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::INTEGER, Format::DATE, 4, 0, 0, false));
//break;
//case 0x44425954: // DBYT; Resorcerer-only. "signed decimal byte"
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::INTEGER, Format::DECIMAL, 1, 0, 0));
//break;
//case 0x44575244: // DWRD; Resorcerer-only. "signed decimal word"
//case 0x52534944: // RSID; Resorcerer-only. "resource ID"
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::INTEGER, Format::DECIMAL, 2, 0, 0));
//break;
//case 0x444C4E47: // DLNG; Resorcerer-only. "signed decimal long"
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::INTEGER, Format::DECIMAL, 4, 0, 0));
//break;
//case 0x48425954: // HBYT; Resorcerer-only. "hex byte"
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::INTEGER, Format::HEX, 1, 0, 0));
//break;
//case 0x48575244: // HWRD; Resorcerer-only. "hex word"
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::INTEGER, Format::HEX, 2, 0, 0));
//break;
//case 0x484C4E47: // HLNG; Resorcerer-only. "hex long"
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::INTEGER, Format::HEX, 4, 0, 0));
//break;
//case 0x46495844: // FIXD; Resorcerer-only. 16:16 Fixed Point Number.
// // .width specifies the width per component (16+16=32)
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::FIXED_POINT, Format::DECIMAL, 2, 0, 0));
//break;
//case 0x504E5420: // PNT ; Resorcerer-only. QuickDraw Point.
// // .width specifies the width per component (16+16=32)
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::POINT_2D, Format::DECIMAL, 2, 0, 0));
//break;
//case 0x41575244: // AWRD; align to 2-byte boundary
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::ALIGNMENT, Format::HEX, 0, 2, 0));
//break;
//case 0x414C4E47: // ALNG; align to 2-byte boundary
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::ALIGNMENT, Format::HEX, 0, 4, 0));
//break;
//case 0x46425954: // FBYT; fill byte
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::ZERO_FILL, Format::HEX, 1, 0, 0));
//break;
//case 0x46575244: // FWRD; fill word
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::ZERO_FILL, Format::HEX, 2, 0, 0));
//break;
//case 0x464C4E47: // FLNG; fill long
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::ZERO_FILL, Format::HEX, 4, 0, 0));
//break;
//case 0x48455844: // HEXD; hex dump
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::EOF_STRING, Format::HEX, 0, 0, 0));
//break;
//case 0x50535452: // PSTR; Pascal string
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::PSTRING, Format::TEXT, 1, 0, 0));
//break;
//case 0x57535452: // WSTR; Pascal string with word-sized length
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::PSTRING, Format::TEXT, 2, 0, 0));
//break;
//case 0x4C535452: // LSTR; Pascal string with long-sized length
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::PSTRING, Format::TEXT, 4, 0, 0));
//break;
//case 0x45535452: // ESTR; even-padded Pascal string
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::PSTRING, Format::TEXT, 1, 2, 0));
//break;
//case 0x4F535452: // OSTR; odd-padded Pascal string
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::PSTRING, Format::TEXT, 1, 2, 1));
//break;
//case 0x43535452: // CSTR; C string (null-terminated)
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::CSTRING, Format::TEXT, 1, 0, 0));
//break;
//case 0x45435354: // ECST; even-padded C string
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::CSTRING, Format::TEXT, 1, 2, 0));
//break;
//case 0x4F435354: // OCST; odd-padded C string
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::CSTRING, Format::TEXT, 1, 2, 1));
//break;
//case 0x424F4F4C: // BOOL; boolean word
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::BOOL, Format::FLAG, 2, 0, 0));
//break;
//case 0x42424954: // BBIT; bit within a word
//if (in_bbit_array) {
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::BOOL, Format::FLAG, 2, 0, 0));
//if (entries->size() == 8) {
//write_stack.pop_back();
//in_bbit_array = false;
//}
//} else {
//entries->emplace_back(make_unique<Entry>("", Type::BITFIELD, Format::FLAG, 1, 0, 0));
//entries = write_stack.emplace_back(&entries->back()->list_entries);
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::BOOL, Format::FLAG, 2, 0, 0));
//in_bbit_array = true;
//}
//break;
//case 0x43484152: // CHAR; ASCII character
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::INTEGER, Format::TEXT, 1, 0, 0));
//break;
//case 0x544E414D: // TNAM; type name
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::INTEGER, Format::TEXT, 4, 0, 0));
//break;
//case 0x52454354: // RECT; QuickDraw Rectangle
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::RECT, Format::DECIMAL, 2, 0, 0));
//break;
//case 0x434F4C52: // COLR; QuickDraw Color RGB Triplet
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::COLOR, Format::HEX, 2, 0, 0, false));
//break;
//case 0x4C53545A: // LSTZ
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::LIST_ZERO_BYTE, Format::FLAG, 0, 0, 0));
//write_stack.emplace_back(&entries->back()->list_entries);
//break;
//case 0x4C535442: // LSTB
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::LIST_EOF, Format::FLAG, 0, 0, 0));
//write_stack.emplace_back(&entries->back()->list_entries);
//break;
//case 0x5A434E54: // ZCNT
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::LIST_ZERO_COUNT, Format::HEX, 2, 0, 0));
//write_stack.emplace_back(&entries->back()->list_entries);
//break;
//case 0x4F434E54: // OCNT
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::LIST_ONE_COUNT, Format::HEX, 2, 0, 0));
//write_stack.emplace_back(&entries->back()->list_entries);
//break;
//case 0x4C434E54: // LCNT; Resorcerer-only. One-based long count of list items
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::LIST_ONE_COUNT, Format::HEX, 4, 0, 0));
//write_stack.emplace_back(&entries->back()->list_entries);
//break;
//case 0x4C535443: { // LSTC
// // ZCNT or OCNT should have already opened the list; make sure that it
// // was the previous command.
//if (write_stack.size() < 2) {
//throw runtime_error("LSTC without preceding ZCNT/OCNT/LCNT in TMPL");
//}
//if (!write_stack.back()->empty()) {
//throw runtime_error("commands between ZCNT/OCNT/LCNT and LSTC");
//}
//auto list_type = write_stack[write_stack.size() - 2]->back()->type;
//if ((list_type != Type::LIST_ZERO_COUNT) && (list_type != Type::LIST_ONE_COUNT)) {
//throw runtime_error("LSTC used for non-ZCNT/OCNT list");
//}
//break;
//}
//case 0x4C535445: // LSTE
//write_stack.pop_back();
//break;
//case 0x50313030: // P100; Pnnn = Pascal string with size 0xnnn / fixed-size Pascal string with NUL-byte padding
// //According to an alert in Resorcerer when editing a resource that uses a template with an odd Pnnn in it,
// //this can either be:
// //- a Pascal string of "0xnnn" characters, prefixed with the length byte (ResEdit)
// //- a Pascal string of "0xnnn - 1" characters, prefixed with the length byte (Resorcerer)
// //As a Pascal string can be at most 255 (0xFF) characters long, if "nnn" is > 0xFF, the remaining bytes are
// //filled with NUL-bytes.
// //This appears to be a bug. Stuffit Expander has a TMPL with a P100
// //field in it, but P100 isn't valid - a 1-byte pstring can only be 0xFF
// //bytes long. It looks like whoever wrote the TMPL mistakely included
// //the length byte here... reading some of the resources that would
// //match the relevant TMPL makes it look like we should treat P100 as if
// //it were P0FF, so that's what we'll do.
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::FIXED_PSTRING, Format::TEXT, 0xFF, 0, 0));
//break;
//default:
//try {
//if ((type & 0xFF000000) == 0x48000000) {
// // Hnnn (fixed-length hex dump)
//uint16_t width =
//value_for_hex_char(type & 0xFF) |
//(value_for_hex_char((type >> 8) & 0xFF) << 4) |
//(value_for_hex_char((type >> 16) & 0xFF) << 8);
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::STRING, Format::HEX, width, 0, 0));
//} else if ((type & 0xFF000000) == 0x43000000) {
// // Cnnn (C string with fixed NUL-byte padding)
//uint16_t width =
//value_for_hex_char(type & 0xFF) |
//(value_for_hex_char((type >> 8) & 0xFF) << 4) |
//(value_for_hex_char((type >> 16) & 0xFF) << 8);
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::FIXED_CSTRING, Format::TEXT, width, 0, 0));
//} else if ((type & 0xFFFF0000) == 0x50300000) {
// // P0nn (Pascal string with fixed NUL-byte padding)
//uint16_t width =
//value_for_hex_char(type & 0xFF) |
//(value_for_hex_char((type >> 8) & 0xFF) << 4);
//entries->emplace_back(make_unique<Entry>(std::move(name), Type::FIXED_PSTRING, Format::TEXT, width, 0, 0));
//} else {
//throw runtime_error("unknown field type: " + string_for_resource_type(type));
//}
//} catch (const out_of_range&) {
//throw runtime_error("unknown field type: " + string_for_resource_type(type));
//}
//}
//}
//if (write_stack.size() != 1) {
//throw runtime_error("unterminated list in TMPL");
//}
//return ret;
//}