This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathdeploy_windows.cpp
More file actions
2181 lines (1832 loc) · 73.7 KB
/
deploy_windows.cpp
File metadata and controls
2181 lines (1832 loc) · 73.7 KB
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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) 2003-2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include "prefix.h"
#include "globdefs.h"
#include "objdefs.h"
#include "parsedef.h"
#include "filedefs.h"
#include "exec.h"
#include "handler.h"
#include "scriptpt.h"
#include "variable.h"
#include "statemnt.h"
#include "osspec.h"
#include "deploy.h"
constexpr uint32_t kAddressToPEAddress = 60;
constexpr uint32_t kPEAddressSize = 4;
constexpr uint32_t kMagicOffset = 0x18;
constexpr uint16_t kHeaderMagic32 = 0x10b;
constexpr uint16_t kHeaderMagic64 = 0x20b;
////////////////////////////////////////////////////////////////////////////////
//
// This section contains definitions for the various structures needed to
// process the PE format. On Windows many of these are defined in winnt.h which
// is always included. Obviously, on other platforms this is not present so we
// replicate them here.
//
#ifndef FIELD_OFFSET
#define FIELD_OFFSET(type, field) ((LONG)(intptr_t)&(((type *)0)->field))
#endif
// Defining common types for 32 and 64 bit
#if !defined(_WIN32) && !defined(_WIN64)
typedef char CHAR;
typedef unsigned short WCHAR;
typedef unsigned char BYTE;
typedef unsigned short WORD;
// FG-2014-09-17: [[ Bugfix 13463 ]] "long" is 64 bits on Linux x86_64
typedef uint32_t DWORD;
typedef int32_t LONG;
typedef uintptr_t LONG_PTR;
#define IMAGE_DOS_SIGNATURE 0x5A4D // MZ
#define IMAGE_OS2_SIGNATURE 0x454E // NE
#define IMAGE_OS2_SIGNATURE_LE 0x454C // LE
#define IMAGE_VXD_SIGNATURE 0x454C // LE
#define IMAGE_NT_SIGNATURE 0x00004550 // PE00
typedef struct _IMAGE_DOS_HEADER { // DOS .EXE header
WORD e_magic; // Magic number
WORD e_cblp; // Bytes on last page of file
WORD e_cp; // Pages in file
WORD e_crlc; // Relocations
WORD e_cparhdr; // Size of header in paragraphs
WORD e_minalloc; // Minimum extra paragraphs needed
WORD e_maxalloc; // Maximum extra paragraphs needed
WORD e_ss; // Initial (relative) SS value
WORD e_sp; // Initial SP value
WORD e_csum; // Checksum
WORD e_ip; // Initial IP value
WORD e_cs; // Initial (relative) CS value
WORD e_lfarlc; // File address of relocation table
WORD e_ovno; // Overlay number
WORD e_res[4]; // Reserved words
WORD e_oemid; // OEM identifier (for e_oeminfo)
WORD e_oeminfo; // OEM information; e_oemid specific
WORD e_res2[10]; // Reserved words
LONG e_lfanew; // File address of new exe header
} IMAGE_DOS_HEADER, *PIMAGE_DOS_HEADER;
typedef struct _IMAGE_FILE_HEADER {
WORD Machine;
WORD NumberOfSections;
DWORD TimeDateStamp;
DWORD PointerToSymbolTable;
DWORD NumberOfSymbols;
WORD SizeOfOptionalHeader;
WORD Characteristics;
} IMAGE_FILE_HEADER, *PIMAGE_FILE_HEADER;
//
// File header format.
//
#define IMAGE_SIZEOF_FILE_HEADER 20
#define IMAGE_FILE_RELOCS_STRIPPED 0x0001 // Relocation info stripped from file.
#define IMAGE_FILE_EXECUTABLE_IMAGE 0x0002 // File is executable (i.e. no unresolved externel references).
#define IMAGE_FILE_LINE_NUMS_STRIPPED 0x0004 // Line nunbers stripped from file.
#define IMAGE_FILE_LOCAL_SYMS_STRIPPED 0x0008 // Local symbols stripped from file.
#define IMAGE_FILE_AGGRESIVE_WS_TRIM 0x0010 // Agressively trim working set
#define IMAGE_FILE_LARGE_ADDRESS_AWARE 0x0020 // App can handle >2gb addresses
#define IMAGE_FILE_BYTES_REVERSED_LO 0x0080 // Bytes of machine word are reversed.
#define IMAGE_FILE_32BIT_MACHINE 0x0100 // 32 bit word machine.
#define IMAGE_FILE_DEBUG_STRIPPED 0x0200 // Debugging info stripped from file in .DBG file
#define IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP 0x0400 // If Image is on removable media, copy and run from the swap file.
#define IMAGE_FILE_NET_RUN_FROM_SWAP 0x0800 // If Image is on Net, copy and run from the swap file.
#define IMAGE_FILE_SYSTEM 0x1000 // System File.
#define IMAGE_FILE_DLL 0x2000 // File is a DLL.
#define IMAGE_FILE_UP_SYSTEM_ONLY 0x4000 // File should only be run on a UP machine
#define IMAGE_FILE_BYTES_REVERSED_HI 0x8000 // Bytes of machine word are reversed.
#define IMAGE_FILE_MACHINE_UNKNOWN 0
#define IMAGE_FILE_MACHINE_I386 0x014c // Intel 386.
#define IMAGE_FILE_MACHINE_R3000 0x0162 // MIPS little-endian, 0x160 big-endian
#define IMAGE_FILE_MACHINE_R4000 0x0166 // MIPS little-endian
#define IMAGE_FILE_MACHINE_R10000 0x0168 // MIPS little-endian
#define IMAGE_FILE_MACHINE_WCEMIPSV2 0x0169 // MIPS little-endian WCE v2
#define IMAGE_FILE_MACHINE_ALPHA 0x0184 // Alpha_AXP
#define IMAGE_FILE_MACHINE_SH3 0x01a2 // SH3 little-endian
#define IMAGE_FILE_MACHINE_SH3DSP 0x01a3
#define IMAGE_FILE_MACHINE_SH3E 0x01a4 // SH3E little-endian
#define IMAGE_FILE_MACHINE_SH4 0x01a6 // SH4 little-endian
#define IMAGE_FILE_MACHINE_SH5 0x01a8 // SH5
#define IMAGE_FILE_MACHINE_ARM 0x01c0 // ARM Little-Endian
#define IMAGE_FILE_MACHINE_THUMB 0x01c2
#define IMAGE_FILE_MACHINE_AM33 0x01d3
#define IMAGE_FILE_MACHINE_POWERPC 0x01F0 // IBM PowerPC Little-Endian
#define IMAGE_FILE_MACHINE_POWERPCFP 0x01f1
#define IMAGE_FILE_MACHINE_IA64 0x0200 // Intel 64
#define IMAGE_FILE_MACHINE_MIPS16 0x0266 // MIPS
#define IMAGE_FILE_MACHINE_ALPHA64 0x0284 // ALPHA64
#define IMAGE_FILE_MACHINE_MIPSFPU 0x0366 // MIPS
#define IMAGE_FILE_MACHINE_MIPSFPU16 0x0466 // MIPS
#define IMAGE_FILE_MACHINE_AXP64 IMAGE_FILE_MACHINE_ALPHA64
#define IMAGE_FILE_MACHINE_TRICORE 0x0520 // Infineon
#define IMAGE_FILE_MACHINE_CEF 0x0CEF
#define IMAGE_FILE_MACHINE_EBC 0x0EBC // EFI Byte Code
#define IMAGE_FILE_MACHINE_AMD64 0x8664 // AMD64 (K8)
#define IMAGE_FILE_MACHINE_M32R 0x9041 // M32R little-endian
#define IMAGE_FILE_MACHINE_CEE 0xC0EE
//
// Directory format.
//
typedef struct _IMAGE_DATA_DIRECTORY {
DWORD VirtualAddress;
DWORD Size;
} IMAGE_DATA_DIRECTORY, *PIMAGE_DATA_DIRECTORY;
#define IMAGE_NUMBEROF_DIRECTORY_ENTRIES 16
// Directory Entries
#define IMAGE_DIRECTORY_ENTRY_EXPORT 0 // Export Directory
#define IMAGE_DIRECTORY_ENTRY_IMPORT 1 // Import Directory
#define IMAGE_DIRECTORY_ENTRY_RESOURCE 2 // Resource Directory
#define IMAGE_DIRECTORY_ENTRY_EXCEPTION 3 // Exception Directory
#define IMAGE_DIRECTORY_ENTRY_SECURITY 4 // Security Directory
#define IMAGE_DIRECTORY_ENTRY_BASERELOC 5 // Base Relocation Table
#define IMAGE_DIRECTORY_ENTRY_DEBUG 6 // Debug Directory
// IMAGE_DIRECTORY_ENTRY_COPYRIGHT 7 // (X86 usage)
#define IMAGE_DIRECTORY_ENTRY_ARCHITECTURE 7 // Architecture Specific Data
#define IMAGE_DIRECTORY_ENTRY_GLOBALPTR 8 // RVA of GP
#define IMAGE_DIRECTORY_ENTRY_TLS 9 // TLS Directory
#define IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG 10 // Load Configuration Directory
#define IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT 11 // Bound Import Directory in headers
#define IMAGE_DIRECTORY_ENTRY_IAT 12 // Import Address Table
#define IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT 13 // Delay Load Import Descriptors
#define IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR 14 // COM Runtime descriptor
//
// Section header format.
//
#define IMAGE_SIZEOF_SHORT_NAME 8
typedef struct _IMAGE_SECTION_HEADER {
BYTE Name[IMAGE_SIZEOF_SHORT_NAME];
union {
DWORD PhysicalAddress;
DWORD VirtualSize;
} Misc;
DWORD VirtualAddress;
DWORD SizeOfRawData;
DWORD PointerToRawData;
DWORD PointerToRelocations;
DWORD PointerToLinenumbers;
WORD NumberOfRelocations;
WORD NumberOfLinenumbers;
DWORD Characteristics;
} IMAGE_SECTION_HEADER, *PIMAGE_SECTION_HEADER;
#define IMAGE_SIZEOF_SECTION_HEADER 40
//
// Section characteristics.
//
// IMAGE_SCN_TYPE_REG 0x00000000 // Reserved.
// IMAGE_SCN_TYPE_DSECT 0x00000001 // Reserved.
// IMAGE_SCN_TYPE_NOLOAD 0x00000002 // Reserved.
// IMAGE_SCN_TYPE_GROUP 0x00000004 // Reserved.
#define IMAGE_SCN_TYPE_NO_PAD 0x00000008 // Reserved.
// IMAGE_SCN_TYPE_COPY 0x00000010 // Reserved.
#define IMAGE_SCN_CNT_CODE 0x00000020 // Section contains code.
#define IMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040 // Section contains initialized data.
#define IMAGE_SCN_CNT_UNINITIALIZED_DATA 0x00000080 // Section contains uninitialized data.
#define IMAGE_SCN_LNK_OTHER 0x00000100 // Reserved.
#define IMAGE_SCN_LNK_INFO 0x00000200 // Section contains comments or some other type of information.
// IMAGE_SCN_TYPE_OVER 0x00000400 // Reserved.
#define IMAGE_SCN_LNK_REMOVE 0x00000800 // Section contents will not become part of image.
#define IMAGE_SCN_LNK_COMDAT 0x00001000 // Section contents comdat.
// 0x00002000 // Reserved.
// IMAGE_SCN_MEM_PROTECTED - Obsolete 0x00004000
#define IMAGE_SCN_NO_DEFER_SPEC_EXC 0x00004000 // Reset speculative exceptions handling bits in the TLB entries for this section.
#define IMAGE_SCN_GPREL 0x00008000 // Section content can be accessed relative to GP
#define IMAGE_SCN_MEM_FARDATA 0x00008000
// IMAGE_SCN_MEM_SYSHEAP - Obsolete 0x00010000
#define IMAGE_SCN_MEM_PURGEABLE 0x00020000
#define IMAGE_SCN_MEM_16BIT 0x00020000
#define IMAGE_SCN_MEM_LOCKED 0x00040000
#define IMAGE_SCN_MEM_PRELOAD 0x00080000
#define IMAGE_SCN_ALIGN_1BYTES 0x00100000 //
#define IMAGE_SCN_ALIGN_2BYTES 0x00200000 //
#define IMAGE_SCN_ALIGN_4BYTES 0x00300000 //
#define IMAGE_SCN_ALIGN_8BYTES 0x00400000 //
#define IMAGE_SCN_ALIGN_16BYTES 0x00500000 // Default alignment if no others are specified.
#define IMAGE_SCN_ALIGN_32BYTES 0x00600000 //
#define IMAGE_SCN_ALIGN_64BYTES 0x00700000 //
#define IMAGE_SCN_ALIGN_128BYTES 0x00800000 //
#define IMAGE_SCN_ALIGN_256BYTES 0x00900000 //
#define IMAGE_SCN_ALIGN_512BYTES 0x00A00000 //
#define IMAGE_SCN_ALIGN_1024BYTES 0x00B00000 //
#define IMAGE_SCN_ALIGN_2048BYTES 0x00C00000 //
#define IMAGE_SCN_ALIGN_4096BYTES 0x00D00000 //
#define IMAGE_SCN_ALIGN_8192BYTES 0x00E00000 //
// Unused 0x00F00000
#define IMAGE_SCN_ALIGN_MASK 0x00F00000
#define IMAGE_SCN_LNK_NRELOC_OVFL 0x01000000 // Section contains extended relocations.
#define IMAGE_SCN_MEM_DISCARDABLE 0x02000000 // Section can be discarded.
#define IMAGE_SCN_MEM_NOT_CACHED 0x04000000 // Section is not cachable.
#define IMAGE_SCN_MEM_NOT_PAGED 0x08000000 // Section is not pageable.
#define IMAGE_SCN_MEM_SHARED 0x10000000 // Section is shareable.
#define IMAGE_SCN_MEM_EXECUTE 0x20000000 // Section is executable.
#define IMAGE_SCN_MEM_READ 0x40000000 // Section is readable.
#define IMAGE_SCN_MEM_WRITE 0x80000000 // Section is writeable.
//
// Resource Format.
//
//
// Resource directory consists of two counts, following by a variable length
// array of directory entries. The first count is the number of entries at
// beginning of the array that have actual names associated with each entry.
// The entries are in ascending order, case insensitive strings. The second
// count is the number of entries that immediately follow the named entries.
// This second count identifies the number of entries that have 16-bit integer
// Ids as their name. These entries are also sorted in ascending order.
//
// This structure allows fast lookup by either name or number, but for any
// given resource entry only one form of lookup is supported, not both.
// This is consistant with the syntax of the .RC file and the .RES file.
//
typedef struct _IMAGE_RESOURCE_DIRECTORY {
DWORD Characteristics;
DWORD TimeDateStamp;
WORD MajorVersion;
WORD MinorVersion;
WORD NumberOfNamedEntries;
WORD NumberOfIdEntries;
// IMAGE_RESOURCE_DIRECTORY_ENTRY DirectoryEntries[];
} IMAGE_RESOURCE_DIRECTORY, *PIMAGE_RESOURCE_DIRECTORY;
#define IMAGE_RESOURCE_NAME_IS_STRING 0x80000000
#define IMAGE_RESOURCE_DATA_IS_DIRECTORY 0x80000000
//
// Each directory contains the 32-bit Name of the entry and an offset,
// relative to the beginning of the resource directory of the data associated
// with this directory entry. If the name of the entry is an actual text
// string instead of an integer Id, then the high order bit of the name field
// is set to one and the low order 31-bits are an offset, relative to the
// beginning of the resource directory of the string, which is of type
// IMAGE_RESOURCE_DIRECTORY_STRING. Otherwise the high bit is clear and the
// low-order 16-bits are the integer Id that identify this resource directory
// entry. If the directory entry is yet another resource directory (i.e. a
// subdirectory), then the high order bit of the offset field will be
// set to indicate this. Otherwise the high bit is clear and the offset
// field points to a resource data entry.
//
typedef struct _IMAGE_RESOURCE_DIRECTORY_ENTRY {
union {
struct {
DWORD NameOffset:31;
DWORD NameIsString:1;
};
DWORD Name;
#ifdef __BIG_ENDIAN__
WORD __pad;
WORD Id;
#else
WORD Id;
#endif
};
union {
DWORD OffsetToData;
struct {
#ifdef __BIG_ENDIAN__
DWORD DataIsDirectory:1;
DWORD OffsetToDirectory:31;
#else
DWORD OffsetToDirectory:31;
DWORD DataIsDirectory:1;
#endif
};
};
} IMAGE_RESOURCE_DIRECTORY_ENTRY, *PIMAGE_RESOURCE_DIRECTORY_ENTRY;
//
// For resource directory entries that have actual string names, the Name
// field of the directory entry points to an object of the following type.
// All of these string objects are stored together after the last resource
// directory entry and before the first resource data object. This minimizes
// the impact of these variable length objects on the alignment of the fixed
// size directory entry objects.
//
typedef struct _IMAGE_RESOURCE_DIRECTORY_STRING {
WORD Length;
CHAR NameString[ 1 ];
} IMAGE_RESOURCE_DIRECTORY_STRING, *PIMAGE_RESOURCE_DIRECTORY_STRING;
typedef struct _IMAGE_RESOURCE_DIR_STRING_U {
WORD Length;
WCHAR NameString[ 1 ];
} IMAGE_RESOURCE_DIR_STRING_U, *PIMAGE_RESOURCE_DIR_STRING_U;
//
// Each resource data entry describes a leaf node in the resource directory
// tree. It contains an offset, relative to the beginning of the resource
// directory of the data for the resource, a size field that gives the number
// of bytes of data at that offset, a CodePage that should be used when
// decoding code point values within the resource data. Typically for new
// applications the code page would be the unicode code page.
//
typedef struct _IMAGE_RESOURCE_DATA_ENTRY {
DWORD OffsetToData;
DWORD Size;
DWORD CodePage;
DWORD Reserved;
} IMAGE_RESOURCE_DATA_ENTRY, *PIMAGE_RESOURCE_DATA_ENTRY;
typedef struct tagVS_FIXEDFILEINFO
{
DWORD dwSignature; /* e.g. 0xfeef04bd */
DWORD dwStrucVersion; /* e.g. 0x00000042 = "0.42" */
DWORD dwFileVersionMS; /* e.g. 0x00030075 = "3.75" */
DWORD dwFileVersionLS; /* e.g. 0x00000031 = "0.31" */
DWORD dwProductVersionMS; /* e.g. 0x00030010 = "3.10" */
DWORD dwProductVersionLS; /* e.g. 0x00000031 = "0.31" */
DWORD dwFileFlagsMask; /* = 0x3F for version "0.42" */
DWORD dwFileFlags; /* e.g. VFF_DEBUG | VFF_PRERELEASE */
DWORD dwFileOS; /* e.g. VOS_DOS_WINDOWS16 */
DWORD dwFileType; /* e.g. VFT_DRIVER */
DWORD dwFileSubtype; /* e.g. VFT2_DRV_KEYBOARD */
DWORD dwFileDateMS; /* e.g. 0 */
DWORD dwFileDateLS; /* e.g. 0 */
} VS_FIXEDFILEINFO;
#endif
#if !defined(_WIN32)
//
// Optional header format.
//
typedef struct _IMAGE_OPTIONAL_HEADER_32 {
//
// Standard fields.
//
WORD Magic;
BYTE MajorLinkerVersion;
BYTE MinorLinkerVersion;
DWORD SizeOfCode;
DWORD SizeOfInitializedData;
DWORD SizeOfUninitializedData;
DWORD AddressOfEntryPoint;
DWORD BaseOfCode;
DWORD BaseOfData;
//
// NT additional fields.
//
DWORD ImageBase; // l
DWORD SectionAlignment; // l
DWORD FileAlignment; // l
WORD MajorOperatingSystemVersion; // s
WORD MinorOperatingSystemVersion; // s
WORD MajorImageVersion; // s
WORD MinorImageVersion; // s
WORD MajorSubsystemVersion; // s
WORD MinorSubsystemVersion; // s
DWORD Win32VersionValue; // l
DWORD SizeOfImage; // l
DWORD SizeOfHeaders; // l
DWORD CheckSum; // l
WORD Subsystem; // s
WORD DllCharacteristics; // s
DWORD SizeOfStackReserve; // l
DWORD SizeOfStackCommit; // l
DWORD SizeOfHeapReserve; // l
DWORD SizeOfHeapCommit; // l
DWORD LoaderFlags; // l
DWORD NumberOfRvaAndSizes; // l
IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES];
} IMAGE_OPTIONAL_HEADER32, *PIMAGE_OPTIONAL_HEADER32;
// The following section should move to the template args
// in the templated version of MCDeployWindows
/*
typedef IMAGE_OPTIONAL_HEADER32 IMAGE_OPTIONAL_HEADER;
typedef PIMAGE_OPTIONAL_HEADER32 PIMAGE_OPTIONAL_HEADER;
*/
#define IMAGE_NT_OPTIONAL_HDR_MAGIC IMAGE_NT_OPTIONAL_HDR32_MAGIC
typedef struct _IMAGE_NT_HEADERS {
DWORD Signature;
IMAGE_FILE_HEADER FileHeader;
IMAGE_OPTIONAL_HEADER32 OptionalHeader;
} IMAGE_NT_HEADERS32, *PIMAGE_NT_HEADERS32;
// The following section should move to the template args
// in the templated version of MCDeployWindows
/*
typedef IMAGE_NT_HEADERS32 IMAGE_NT_HEADERS;
typedef PIMAGE_NT_HEADERS32 PIMAGE_NT_HEADERS;
*/
#endif // if !defined(_WIN32)
#if !defined(_WIN64) && !defined(_WINNT_)
typedef uint64_t ULONGLONG;
//
// Optional header format.
//
typedef struct _IMAGE_OPTIONAL_HEADER_64 {
// Standard fields
WORD Magic; // s
BYTE MajorLinkerVersion; // b
BYTE MinorLinkerVersion; // b
DWORD SizeOfCode; // l
DWORD SizeOfInitializedData; // l
DWORD SizeOfUninitializedData; // l
DWORD AddressOfEntryPoint; // l
DWORD BaseOfCode; // l
// NT Fields
ULONGLONG ImageBase; // q
DWORD SectionAlignment; // l
DWORD FileAlignment; // l
WORD MajorOperatingSystemVersion; // s
WORD MinorOperatingSystemVersion; // s
WORD MajorImageVersion; // s
WORD MinorImageVersion; // s
WORD MajorSubsystemVersion; // s
WORD MinorSubsystemVersion; // s
DWORD Win32VersionValue; // l
DWORD SizeOfImage; // l
DWORD SizeOfHeaders; // l
DWORD CheckSum; // l
WORD Subsystem; // s
WORD DllCharacteristics; // s
ULONGLONG SizeOfStackReserve; // q
ULONGLONG SizeOfStackCommit; // q
ULONGLONG SizeOfHeapReserve; // q
ULONGLONG SizeOfHeapCommit; // q
DWORD LoaderFlags; // l
DWORD NumberOfRvaAndSizes; // l
IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES];
// sbbl lll l q ll ss ss ss l l l l s s q q q q l l
} IMAGE_OPTIONAL_HEADER64, *PIMAGE_OPTIONAL_HEADER64;
// The following section should move to the template args
// in the templated version of MCDeployWindows
/*
typedef IMAGE_OPTIONAL_HEADER64 IMAGE_OPTIONAL_HEADER;
typedef PIMAGE_OPTIONAL_HEADER64 PIMAGE_OPTIONAL_HEADER;
*/
#define IMAGE_NT_OPTIONAL_HDR_MAGIC IMAGE_NT_OPTIONAL_HDR64_MAGIC
typedef struct _IMAGE_NT_HEADERS_64 {
DWORD Signature;
IMAGE_FILE_HEADER FileHeader;
IMAGE_OPTIONAL_HEADER64 OptionalHeader;
} IMAGE_NT_HEADERS64, *PIMAGE_NT_HEADERS64;
// The following section should move to the template args
// in the templated version of MCDeployWindows
/*
typedef IMAGE_NT_HEADERS64 IMAGE_NT_HEADERS;
typedef PIMAGE_NT_HEADERS64 PIMAGE_NT_HEADERS;
*/
#endif // if !defined(_WIN64)
// The following structures are for those used in ICO files and in ICON and
// GROUP_ICON resources. These (for some reason) do not appear in any of the
// windows headers, so we replicate them for all platforms here.
typedef struct
{
BYTE bWidth; // Width, in pixels, of the image
BYTE bHeight; // Height, in pixels, of the image
BYTE bColorCount; // Number of colors in image (0 if >=8bpp)
BYTE bReserved; // Reserved ( must be 0)
WORD wPlanes; // Color Planes
WORD wBitCount; // Bits per pixel
DWORD dwBytesInRes; // How many bytes in this resource?
DWORD dwImageOffset; // Where in the file is this image?
} ICONDIRENTRY, *LPICONDIRENTRY;
#define sizeof_ICONDIRENTRY 16
typedef struct
{
WORD idReserved; // Reserved (must be 0)
WORD idType; // Resource Type (1 for icons)
WORD idCount; // How many images?
// ICONDIRENTRY idEntries[]; // An entry for each image (idCount of 'em)
} ICONDIR, *LPICONDIR;
#define sizeof_ICONDIR 6
typedef struct
{
BYTE bWidth; // Width, in pixels, of the image
BYTE bHeight; // Height, in pixels, of the image
BYTE bColorCount; // Number of colors in image (0 if >=8bpp)
BYTE bReserved; // Reserved
WORD wPlanes; // Color Planes
WORD wBitCount; // Bits per pixel
DWORD dwBytesInRes; // how many bytes in this resource?
WORD nID; // the ID
} GRPICONDIRENTRY, *LPGRPICONDIRENTRY;
#define sizeof_GRPICONDIRENTRY 14
typedef struct
{
WORD idReserved; // Reserved (must be 0)
WORD idType; // Resource type (1 for icons)
WORD idCount; // How many images?
GRPICONDIRENTRY idEntries[1]; // The entries for each image
} GRPICONDIR, *LPGRPICONDIR;
#define sizeof_GRPICONDIR 6
////////////////////////////////////////////////////////////////////////////////
// These methods swap the little-endian values we need/find in a windows exe
// to host byte-order. Obviously they will be trivial on little-endian
// architectures.
static inline void swap_uint16(uint16_t& x)
{
#ifdef __BIG_ENDIAN__
x = ((x & 0xff) << 8) | ((x >> 8) & 0xff);
#endif
}
static inline uint16_t swapped_uint16(uint16_t x)
{
#ifdef __BIG_ENDIAN__
return((x & 0xff) << 8) | ((x >> 8) & 0xff);
#else
return x;
#endif
}
static inline void swap_uint16s(uint16_t *x, uint32_t n)
{
#ifdef __BIG_ENDIAN__
for(uint32_t i = 0; i < n; i++)
swap_uint16(x[i]);
#endif
}
static inline void swap_uint32(uint32_t& x)
{
#ifdef __BIG_ENDIAN__
x = ((x >> 24) & 0xff) | ((x >> 8) & 0xff00) | ((x & 0xff00) << 8) | ((x & 0xff) << 24);
#endif
}
// Now, in theory (for byte-swapping at least) DWORD == long == uint32. However
// reference parameters need to be of the exact same base type (even if there
// are trivial conversions) so we make separate routines. Note that rather
// than replicate the swap, we convert swap and convert back - this stops
// signedness issues and also will stop 'type-punned' pointer issues if we
// were to try and do it by pulling the values through *(<type> *)&v type
// patterns.
static inline void swap_dword(DWORD& x)
{
#ifdef __BIG_ENDIAN__
uint32_t y;
y = x;
swap_uint32(y);
x = y;
#endif
}
static inline void swap_long(LONG& x)
{
#ifdef __BIG_ENDIAN__
uint32_t y;
y = x;
swap_uint32(y);
x = y;
#endif
}
// This is a simple pattern based endian swap routine. The format string f consists of
// a string of 'l', 's', 'b', ' ' or any combination. Spaces are ignored, 'b' causes the
// next byte of data to be skipped, 's' causes the next two bytes to be swapped, and 'l'
// the next four.
static inline void swap_format(const char *f, void *p, uint32_t s)
{
MCDeployByteSwapRecord(false, f, p, s);
}
// These routines swap the PE structures we need. Note that we don't bother
// wrapping these in __BIG_ENDIAN__ switches, since (hopefully) even the poorest
// of optimizers will notice that the functions they are calling are no-ops :o)
template<typename DeployPlatformTrait>
static inline void swap_IMAGE_NT_HEADERS(typename DeployPlatformTrait::IMAGE_NT_HEADERS& x)
{
DeployPlatformTrait::swap_IMAGE_NT_HEADERS(x);
}
static inline void swap_IMAGE_DOS_HEADER(IMAGE_DOS_HEADER& x)
{
swap_uint16s((uint16_t *)&x, 30);
swap_long(x . e_lfanew);
}
static inline void swap_IMAGE_SECTION_HEADER(IMAGE_SECTION_HEADER& x)
{
// MW-2009-07-14: Incorrect format for record - extra 'l' before pair of 's'
swap_format("bbbbbbbbllllllssl", &x, sizeof(IMAGE_SECTION_HEADER));
}
static inline void swap_IMAGE_RESOURCE_DATA_ENTRY(IMAGE_RESOURCE_DATA_ENTRY& x)
{
swap_format("llll", &x, sizeof(IMAGE_RESOURCE_DATA_ENTRY));
}
static inline void swap_IMAGE_RESOURCE_DIRECTORY(IMAGE_RESOURCE_DIRECTORY& x)
{
swap_format("llssss", &x, sizeof(IMAGE_RESOURCE_DIRECTORY));
}
static inline void swap_IMAGE_RESOURCE_DIRECTORY_ENTRY(IMAGE_RESOURCE_DIRECTORY_ENTRY& x)
{
swap_dword(x . Name);
swap_dword(x . OffsetToData);
}
static inline void swap_ICONDIR(ICONDIR& x)
{
swap_format("sss", &x, sizeof_ICONDIR);
}
static inline void swap_ICONDIRENTRY(ICONDIRENTRY& x)
{
swap_format("bbbbssll", &x, sizeof_ICONDIRENTRY);
}
static inline void swap_GRPICONDIR(GRPICONDIR& x)
{
swap_format("sss", &x, sizeof_GRPICONDIR);
}
static inline void swap_GRPICONDIRENTRY(GRPICONDIRENTRY& x)
{
swap_format("bbbbssls", &x, sizeof_GRPICONDIRENTRY);
}
////////////////////////////////////////////////////////////////////////////////
//
// This section contains methods for operating on a Windows PE resource tree.
//
struct MCWindowsPE32Traits
{
typedef IMAGE_OPTIONAL_HEADER32 IMAGE_OPTIONAL_HEADER;
typedef PIMAGE_OPTIONAL_HEADER32 PIMAGE_OPTIONAL_HEADER;
typedef IMAGE_NT_HEADERS32 IMAGE_NT_HEADERS;
typedef PIMAGE_NT_HEADERS32 PIMAGE_NT_HEADERS;
static inline void swap_IMAGE_NT_HEADERS(IMAGE_NT_HEADERS& x)
{
swap_format("l sslllss", &x, FIELD_OFFSET(IMAGE_NT_HEADERS, OptionalHeader));
swap_format("sbbllllll lllssssssllllssllllll ll ll ll ll ll ll ll ll ll ll ll ll ll ll ll ll", &x.OptionalHeader, x.FileHeader.SizeOfOptionalHeader);
}
};
struct MCWindowsPE64Traits
{
typedef IMAGE_OPTIONAL_HEADER64 IMAGE_OPTIONAL_HEADER;
typedef PIMAGE_OPTIONAL_HEADER64 PIMAGE_OPTIONAL_HEADER;
typedef IMAGE_NT_HEADERS64 IMAGE_NT_HEADERS;
typedef PIMAGE_NT_HEADERS64 PIMAGE_NT_HEADERS;
static inline void swap_IMAGE_NT_HEADERS(IMAGE_NT_HEADERS& x)
{
swap_format("l sslllss", &x, FIELD_OFFSET(IMAGE_NT_HEADERS, OptionalHeader));
swap_format("sbblllll qllssssssllllssqqqqll ll ll ll ll ll ll ll ll ll ll ll ll ll ll ll ll", &x.OptionalHeader, x.FileHeader.SizeOfOptionalHeader);
}
};
struct MCWindowsResources
{
// The id of the resource
uint32_t id;
// The name of the resource - note either an id or a name is valid, but
// not both. An unnamd entry wil have a NULL name pointer.
uint32_t name_length;
uint16_t *name;
// Whether this is a directory or entry
bool is_table;
union
{
// FG-2014-09-17: [[ Bugfix 13463 ]]
// The members of this union should be aligned with similarly-sized
// fields in order to prevent issues on 64-bit systems (in particular,
// a bool should not be lined up with a pointer as compilers are allowed
// to write anything they like into the high-order bytes).
struct
{
uint32_t entry_count;
uint32_t _pad_codepage; // PADDING
bool _pad_in_file; // PADDING
MCWindowsResources *entries;
} table;
struct
{
// The size of the data attached to the resource.
uint32_t size;
// The codepage of the data
uint32_t codepage;
// Whether the data resides in the source file.
bool in_file;
// Either the offset into the source file of the data, or the pointer
// to the data if new. This depends on data_in_file.
union
{
uint32_t offset;
void *buffer;
};
} data;
};
};
static void MCWindowsResourcesInitialize(MCWindowsResources& self)
{
memset(&self, 0, sizeof(MCWindowsResources));
}
static void MCWindowsResourcesFinalize(MCWindowsResources& self);
static void MCWindowsResourcesFinalizeData(MCWindowsResources& self)
{
if (self . is_table)
{
for(uint32_t i = 0; i < self . table . entry_count; i++)
MCWindowsResourcesFinalize(self . table . entries[i]);
delete[] self . table . entries;
self . is_table = false;
}
else if (!self . data . in_file)
free(self . data . buffer);
self . data . size = 0;
self . data . codepage = 0;
self . data . in_file = false;
self . data . buffer = NULL;
}
static void MCWindowsResourcesFinalize(MCWindowsResources& self)
{
if (self . name != NULL)
delete[] self . name;
MCWindowsResourcesFinalizeData(self);
}
static bool MCWindowsResourcesFind(MCWindowsResources& self, uint32_t p_id, MCWindowsResources*& r_res)
{
if (!self . is_table)
{
MCWindowsResourcesFinalizeData(self);
self . is_table = true;
self . table . entry_count = 0;
}
for(uint32_t i = 0; i < self . table . entry_count; i++)
if (self . table . entries[i] . id == p_id)
{
r_res = &self . table . entries[i];
return true;
}
MCWindowsResources *t_new_entries;
t_new_entries = (MCWindowsResources *)realloc(self . table . entries, sizeof(MCWindowsResources) * (self . table . entry_count + 1));
if (t_new_entries == NULL)
return MCDeployThrow(kMCDeployErrorNoMemory);
self . table . entries = t_new_entries;
self . table . entry_count += 1;
MCWindowsResourcesInitialize(self . table . entries[self . table . entry_count - 1]);
self . table . entries[self . table . entry_count - 1] . id = p_id;
r_res = &self . table . entries[self . table . entry_count - 1];
return true;
}
static void MCWindowsResourcesSet(MCWindowsResources& self, void *p_data, uint32_t p_size)
{
MCWindowsResourcesFinalizeData(self);
self . is_table = false;
self . data . size = p_size;
self . data . in_file = false;
self . data . codepage = 0;
self . data . buffer = p_data;
}
////////////////////////////////////////////////////////////////////////////////
// This method cleans out any existing icon resources in the exe
static void MCWindowsResourcesClearIcons(MCWindowsResources& self)
{
MCWindowsResources *t_branch;
if (MCWindowsResourcesFind(self, 3, t_branch))
{
MCWindowsResourcesFinalizeData(*t_branch);
t_branch -> is_table = true;
t_branch -> table . entry_count = 0;
}
if (MCWindowsResourcesFind(self, 14, t_branch))
{
MCWindowsResourcesFinalizeData(*t_branch);
t_branch -> is_table = true;
t_branch -> table . entry_count = 0;
}
}
// This method inserts the given icon file into a set of resources using the given id.
static bool MCWindowsResourcesAddIcon(MCWindowsResources& self, MCStringRef p_icon_file, uint32_t p_id, uint32_t p_culture_id)
{
bool t_success;
t_success = true;
// First thing to do is try to and open the icon file
MCDeployFileRef t_icon;
t_icon = NULL;
if (t_success)
t_success = MCDeployFileOpen(p_icon_file, kMCOpenFileModeRead, t_icon);
// Next read the header - care here to ensure correct structure size
ICONDIR t_dir;
if (t_success)
t_success = MCDeployFileRead(t_icon, &t_dir, sizeof_ICONDIR);
if (t_success)
swap_ICONDIR(t_dir);
// Now read in the entries - care here to ensure correct structure size
ICONDIRENTRY *t_entries;
t_entries = NULL;
if (t_success)
{
t_entries = new (nothrow) ICONDIRENTRY[t_dir . idCount];
if (t_entries == NULL)
t_success = MCDeployThrow(kMCDeployErrorNoMemory);
}
if (t_success)
for(uint32_t i = 0; i < t_dir . idCount && t_success; i++)
{
t_success = MCDeployFileRead(t_icon, &t_entries[i], sizeof_ICONDIRENTRY);
if (t_success)
swap_ICONDIRENTRY(t_entries[i]);
}
// Now we construct the resource hierarchy - first get the icon dir so
// we know what ids to use.
MCWindowsResources *t_icon_dir;
if (t_success)
t_success = MCWindowsResourcesFind(self, 3, t_icon_dir);
uint32_t t_last_icon_id;
t_last_icon_id = 0;
if (t_success && t_icon_dir -> is_table)
for(uint32_t i = 0; i < t_icon_dir -> table . entry_count; i++)
t_last_icon_id = MCU_max(t_icon_dir -> table . entries[i] . id, t_last_icon_id);
// And now get the grpicon dir
MCWindowsResources *t_grpicon_dir;
if (t_success)
t_success = MCWindowsResourcesFind(self, 14, t_grpicon_dir);
// First lets add in the group icon resource - we will overwrite one if already
// present with the same id.
MCWindowsResources *t_grpicon;
if (t_success)
t_success =
MCWindowsResourcesFind(*t_grpicon_dir, p_id, t_grpicon) &&
MCWindowsResourcesFind(*t_grpicon, p_culture_id, t_grpicon);
// We have our grpicon resource leaf, so now we construct the grpicon data
// and set.
uint8_t *t_grpicon_data;
t_grpicon_data = NULL;
if (t_success)
{
t_grpicon_data = new (nothrow) uint8_t[sizeof_GRPICONDIR + sizeof_GRPICONDIRENTRY * t_dir . idCount];
if (t_grpicon_data == NULL)
t_success = MCDeployThrow(kMCDeployErrorNoMemory);
}
if (t_success)
{
uint8_t *t_ptr;
t_ptr = t_grpicon_data;
GRPICONDIR t_grp_dir;
t_grp_dir . idReserved = 0;
t_grp_dir . idType = 1;
t_grp_dir . idCount = t_dir . idCount;
swap_GRPICONDIR(t_grp_dir);
memcpy(t_ptr, &t_grp_dir, sizeof_GRPICONDIR);
t_ptr += sizeof_GRPICONDIR;
for(uint32_t i = 0; i < t_dir . idCount; i++)
{
GRPICONDIRENTRY t_grp_entry;
t_grp_entry . bWidth = t_entries[i] . bWidth;
t_grp_entry . bHeight = t_entries[i] . bHeight;
t_grp_entry . bColorCount = t_entries[i] . bColorCount;
t_grp_entry . bReserved = 0;
t_grp_entry . wPlanes = t_entries[i] . wPlanes;
t_grp_entry . wBitCount = t_entries[i] . wBitCount;
t_grp_entry . dwBytesInRes = t_entries[i] . dwBytesInRes;
t_grp_entry . nID = t_last_icon_id + i + 1;