| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
| 2 | |
| 3 | namespace WixToolset.Dtf.Resources |
| 4 | { |
| 5 | using System; |
| 6 | using System.IO; |
| 7 | using System.Text; |
| 8 | using System.Reflection; |
| 9 | using System.Collections; |
| 10 | using System.Globalization; |
| 11 | using System.Diagnostics.CodeAnalysis; |
| 12 | |
| 13 | internal class FixedFileVersionInfo |
| 14 | { |
| 15 | public FixedFileVersionInfo() |
| 16 | { |
| 17 | // Set reasonable defaults |
| 18 | this.signature = 0xFEEF04BD; |
| 19 | this.structVersion = 0x00010000; // v1.0 |
| 20 | this.FileVersion = new Version(0, 0, 0, 0); |
| 21 | this.ProductVersion = new Version(0, 0, 0, 0); |
| 22 | this.FileFlagsMask = VersionBuildTypes.Debug | VersionBuildTypes.Prerelease; |
| 23 | this.FileFlags = VersionBuildTypes.None; |
| 24 | this.FileOS = VersionFileOS.NT_WINDOWS32; |
| 25 | this.FileType = VersionFileType.Application; |
| 26 | this.FileSubtype = VersionFileSubtype.Unknown; |
| 27 | this.Timestamp = DateTime.MinValue; |
| 28 | } |
| 29 | |
| 30 | private uint signature; |
| 31 | private uint structVersion; |
| 32 | |
| 33 | public Version FileVersion |
| 34 | { |
| 35 | get |
| 36 | { |
| 37 | return this.fileVersion; |
| 38 | } |
| 39 | |
| 40 | set |
| 41 | { |
| 42 | if (value == null) |
| 43 | { |
| 44 | throw new InvalidOperationException(); |
| 45 | } |
| 46 | |
| 47 | this.fileVersion = value; |
| 48 | } |
| 49 | } |
| 50 | private Version fileVersion; |
| 51 | |
| 52 | public Version ProductVersion |
| 53 | { |
| 54 | get |
| 55 | { |
| 56 | return this.productVersion; |
| 57 | } |
| 58 | |
| 59 | set |
| 60 | { |
| 61 | if (value == null) |
| 62 | { |
| 63 | throw new InvalidOperationException(); |
| 64 | } |
| 65 | |
| 66 | this.productVersion = value; |
| 67 | } |
| 68 | } |
| 69 | private Version productVersion; |
| 70 | |
| 71 | public VersionBuildTypes FileFlagsMask |
| 72 | { |
| 73 | get { return this.fileFlagsMask; } |
| 74 | set { this.fileFlagsMask = value; } |
| 75 | } |
| 76 | private VersionBuildTypes fileFlagsMask; |
| 77 | |
| 78 | public VersionBuildTypes FileFlags |
| 79 | { |
| 80 | get { return this.fileFlags; } |
| 81 | set { this.fileFlags = value; } |
| 82 | } |
| 83 | private VersionBuildTypes fileFlags; |
| 84 | |
| 85 | [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] |
| 86 | public VersionFileOS FileOS |
| 87 | { |
| 88 | get { return this.fileOS; } |
| 89 | set { this.fileOS = value; } |
| 90 | } |
| 91 | private VersionFileOS fileOS; |
| 92 | |
| 93 | public VersionFileType FileType |
| 94 | { |
| 95 | get { return this.fileType; } |
| 96 | set { this.fileType = value; } |
| 97 | } |
| 98 | private VersionFileType fileType; |
| 99 | |
| 100 | public VersionFileSubtype FileSubtype |
| 101 | { |
| 102 | get { return this.fileSubtype; } |
| 103 | set { this.fileSubtype = value; } |
| 104 | } |
| 105 | private VersionFileSubtype fileSubtype; |
| 106 | |
| 107 | public DateTime Timestamp |
| 108 | { |
| 109 | get { return this.timestamp; } |
| 110 | set { this.timestamp = value; } |
| 111 | } |
| 112 | private DateTime timestamp; |
| 113 | |
| 114 | public void Read(BinaryReader reader) |
| 115 | { |
| 116 | this.signature = reader.ReadUInt32(); |
| 117 | this.structVersion = reader.ReadUInt32(); |
| 118 | this.fileVersion = UInt64ToVersion(reader.ReadUInt64()); |
| 119 | this.productVersion = UInt64ToVersion(reader.ReadUInt64()); |
| 120 | this.fileFlagsMask = (VersionBuildTypes) reader.ReadInt32(); |
| 121 | this.fileFlags = (VersionBuildTypes) reader.ReadInt32(); |
| 122 | this.fileOS = (VersionFileOS) reader.ReadInt32(); |
| 123 | this.fileType = (VersionFileType) reader.ReadInt32(); |
| 124 | this.fileSubtype = (VersionFileSubtype) reader.ReadInt32(); |
| 125 | this.timestamp = UInt64ToDateTime(reader.ReadUInt64()); |
| 126 | } |
| 127 | |
| 128 | public void Write(BinaryWriter writer) |
| 129 | { |
| 130 | writer.Write(this.signature); |
| 131 | writer.Write(this.structVersion); |
| 132 | writer.Write(VersionToUInt64(this.fileVersion)); |
| 133 | writer.Write(VersionToUInt64(this.productVersion)); |
| 134 | writer.Write((int) this.fileFlagsMask); |
| 135 | writer.Write((int) this.fileFlags); |
| 136 | writer.Write((int) this.fileOS); |
| 137 | writer.Write((int) this.fileType); |
| 138 | writer.Write((int) this.fileSubtype); |
| 139 | writer.Write(DateTimeToUInt64(this.timestamp)); |
| 140 | } |
| 141 | |
| 142 | public static explicit operator FixedFileVersionInfo(byte[] bytesValue) |
| 143 | { |
| 144 | FixedFileVersionInfo ffviValue = new FixedFileVersionInfo(); |
| 145 | using (BinaryReader reader = new BinaryReader(new MemoryStream(bytesValue, false))) |
| 146 | { |
| 147 | ffviValue.Read(reader); |
| 148 | } |
| 149 | return ffviValue; |
| 150 | } |
| 151 | |
| 152 | public static explicit operator byte[](FixedFileVersionInfo ffviValue) |
| 153 | { |
| 154 | const int FFVI_LENGTH = 52; |
| 155 | |
| 156 | byte[] bytesValue = new byte[FFVI_LENGTH]; |
| 157 | using (BinaryWriter writer = new BinaryWriter(new MemoryStream(bytesValue, true))) |
| 158 | { |
| 159 | ffviValue.Write(writer); |
| 160 | } |
| 161 | return bytesValue; |
| 162 | } |
| 163 | |
| 164 | private static Version UInt64ToVersion(ulong version) |
| 165 | { |
| 166 | return new Version((int) ((version >> 16) & 0xFFFF), (int) (version & 0xFFFF), (int) (version >> 48), (int) ((version >> 32) & 0xFFFF)); |
| 167 | } |
| 168 | private static ulong VersionToUInt64(Version version) |
| 169 | { |
| 170 | return (((ulong) (ushort) version.Major) << 16) | ((ulong) (ushort) version.Minor) |
| 171 | | (((ulong) (ushort) version.Build) << 48) | (((ulong) (ushort) version.Revision) << 32); |
| 172 | } |
| 173 | |
| 174 | private static DateTime UInt64ToDateTime(ulong dateTime) |
| 175 | { |
| 176 | return (dateTime == 0 ? DateTime.MinValue : DateTime.FromFileTime((long) dateTime)); |
| 177 | } |
| 178 | private static ulong DateTimeToUInt64(DateTime dateTime) |
| 179 | { |
| 180 | return (dateTime == DateTime.MinValue ? 0 : (ulong) dateTime.ToFileTime()); |
| 181 | } |
| 182 | } |
| 183 | } |