[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/livecodeali/livecode/develop/engine/src/deploy_windows.cpp [Back]  [Original]

/* 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 .  */

#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) & 0xff);
#endif
}

static inline uint16_t swapped_uint16(uint16_t x)
{
#ifdef __BIG_ENDIAN__
	return((x & 0xff) > 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)  is_text = p_is_text;
		t_child -> key = t_key;
		t_child -> value = t_value;
		t_child -> value_length = p_value_length;
		t_child -> next = NULL;
		t_child -> children = NULL;
		if (p_parent != NULL)
		{
			t_child -> next = p_parent -> children;
			p_parent -> children = t_child;
		}

		r_child = t_child;
	}
	else
	{
		free(t_key);
		free(t_value);
		delete t_child;
	}

	return t_success;
}

static void MCWindowsVersionInfoMeasure(MCWindowsVersionInfo *self, uint32_t& x_size)
{
	// General structure is:
	//   WORD length
	//   WORD valuelength
	//   WORD type
	//   WORD key[]
	//   if value then
	//     (pad to 32-bit)
	//     (value)
	//   if children then
	//     (pad to 32-bit)
	//     (children)

	x_size += sizeof(WORD) * 3;

	x_size += sizeof(WORD) * (strlen(self -> key) + 1);

	if (self -> value != NULL)
	{
		x_size = (x_size + 3) & ~3;
		x_size += self -> value_length;
	}

	if (self -> children != NULL)
	{
		for(MCWindowsVersionInfo *t_child = self -> children; t_child != NULL; t_child = t_child -> next)
		{
			x_size = (x_size + 3) & ~3;
			MCWindowsVersionInfoMeasure(t_child, x_size);
		}
	}
}

static void MCWindowsVersionInfoBuild(MCWindowsVersionInfo *self, void *p_buffer, uint32_t& x_offset)
{
	// Calculate the size of the record by measuring from the current offset.
	uint32_t t_offset_after;
	t_offset_after = x_offset;
	MCWindowsVersionInfoMeasure(self, t_offset_after);

	WORD t_length, t_value_length, t_type;
	t_length = swapped_uint16(t_offset_after - x_offset);
	t_value_length = swapped_uint16(self -> is_text ? self -> value_length / 2 : self -> value_length);
	t_type = swapped_uint16(self -> is_text ? 1 : 0);

	uint8_t *t_buffer;
	t_buffer = (uint8_t *)p_buffer;

	// Header
	memcpy(t_buffer + x_offset, &t_length, sizeof(WORD));
	x_offset += sizeof(WORD);
	memcpy(t_buffer + x_offset, &t_value_length, sizeof(WORD));
	x_offset += sizeof(WORD);
	memcpy(t_buffer + x_offset, &t_type, sizeof(WORD));
	x_offset += sizeof(WORD);
	for(uint32_t i = 0; i < strlen(self -> key) + 1; i++)
	{
		WCHAR t_char;
		t_char = swapped_uint16(self -> key[i]);
		memcpy(t_buffer + x_offset, &t_char, sizeof(WORD));
		x_offset += sizeof(WORD);
	}

	// Value
	if (self -> value != NULL)
	{
		x_offset = (x_offset + 3) & ~3;
		memcpy(t_buffer + x_offset, self -> value, self -> value_length);
		x_offset += self -> value_length;
	}

	// Children
	if (self -> children != NULL)
	{
		for(MCWindowsVersionInfo *t_child = self -> children; t_child != NULL; t_child = t_child -> next)
		{
			x_offset = (x_offset + 3) & ~3;
			MCWindowsVersionInfoBuild(t_child, t_buffer, x_offset);
		}
	}
}

static void MCWindowsVersionInfoDestroy(MCWindowsVersionInfo *self)
{
	free(self -> key);
	free(self -> value);
	while(self -> children != NULL)
	{
		MCWindowsVersionInfo *t_child;
		t_child = self -> children;
		self -> children = self -> children -> next;
		MCWindowsVersionInfoDestroy(t_child);
	}
	delete self;
}

static uint64_t MCWindowsVersionInfoParseVersion(MCStringRef p_string)
{
	uint32_t a, b, c, d;
    MCAutoStringRefAsUTF8String t_string_utf8;
    /* UNCHECKED */ t_string_utf8 . Lock(p_string);
	if (sscanf(*t_string_utf8, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
		return 0;
	return 0ULL | ((uint64_t)a > 32);
		t_fixed_info . dwProductVersionLS = (uint32_t)(t_product_version & 0xffffffff);
		MCDeployByteSwapRecord(false, "lllllllllllll", &t_fixed_info, sizeof(VS_FIXEDFILEINFO));
		t_success = MCWindowsVersionInfoAdd(NULL, "VS_VERSION_INFO", false, &t_fixed_info, sizeof(VS_FIXEDFILEINFO), t_version_info);
	}

	MCWindowsVersionInfo *t_var_file_info;
	if (t_success)
	{
		uint32_t t_var_info;
		t_var_info = (1200 

Web Proxy Viewer  |  New URL  |  Original Page