aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Core.Burn/Bundles/BurnWriter.cs
blob: 08eeaa15accc3d74ac7c51efd7e105dd15a32052 (plain)
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
// 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.

namespace WixToolset.Core.Burn.Bundles
{
    using System;
    using System.Diagnostics;
    using System.IO;
    using WixToolset.Data;
    using WixToolset.Extensibility.Services;

    /// <summary>
    /// Burn PE writer for the WiX toolset.
    /// </summary>
    /// <remarks>This class encapsulates reading/writing to a stub EXE for
    /// creating bundled/chained setup packages.</remarks>
    /// <example>
    /// using (BurnWriter writer = new BurnWriter(fileExe, this.core, guid))
    /// {
    ///     writer.AppendContainer(file1, BurnWriter.Container.UX);
    ///     writer.AppendContainer(file2, BurnWriter.Container.Attached);
    /// }
    /// </example>
    internal class BurnWriter : BurnCommon
    {
        private bool disposed;
        private bool invalidBundle;
        private BinaryWriter binaryWriter;

        /// <summary>
        /// Creates a BurnWriter for re-writing a PE file.
        /// </summary>
        /// <param name="fileExe">File to modify in-place.</param>
        /// <param name="bundleGuid">GUID for the bundle.</param>
        private BurnWriter(IMessaging messaging, string fileExe)
            : base(messaging, fileExe)
        {
        }

        /// <summary>
        /// Opens a Burn writer.
        /// </summary>
        /// <param name="fileExe">Path to file.</param>
        /// <returns>Burn writer.</returns>
        public static BurnWriter Open(IMessaging messaging, string fileExe)
        {
            BurnWriter writer = new BurnWriter(messaging, fileExe);

            using (BinaryReader binaryReader = new BinaryReader(File.Open(fileExe, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete)))
            {
                if (!writer.Initialize(binaryReader))
                {
                    writer.invalidBundle = true;
                }
            }

            if (!writer.invalidBundle)
            {
                writer.binaryWriter = new BinaryWriter(File.Open(fileExe, FileMode.Open, FileAccess.ReadWrite, FileShare.Read | FileShare.Delete));
            }

            return writer;
        }

        /// <summary>
        /// Update the ".wixburn" section data.
        /// </summary>
        /// <param name="stubSize">Size of the stub engine "burn.exe".</param>
        /// <param name="bundleId">Unique identifier for this bundle.</param>
        /// <returns></returns>
        public bool InitializeBundleSectionData(long stubSize, Guid bundleId)
        {
            if (this.invalidBundle)
            {
                return false;
            }

            this.WriteToBurnSectionOffset(BURN_SECTION_OFFSET_MAGIC, BURN_SECTION_MAGIC);
            this.WriteToBurnSectionOffset(BURN_SECTION_OFFSET_VERSION, BURN_SECTION_VERSION);

            this.messaging.Write(VerboseMessages.BundleGuid(bundleId.ToString("B")));
            this.binaryWriter.BaseStream.Seek(this.wixburnDataOffset + BURN_SECTION_OFFSET_BUNDLEGUID, SeekOrigin.Begin);
            this.binaryWriter.Write(bundleId.ToByteArray());

            this.StubSize = (uint)stubSize;

            this.WriteToBurnSectionOffset(BURN_SECTION_OFFSET_STUBSIZE, this.StubSize);
            this.WriteToBurnSectionOffset(BURN_SECTION_OFFSET_ORIGINALCHECKSUM, 0);
            this.WriteToBurnSectionOffset(BURN_SECTION_OFFSET_ORIGINALSIGNATUREOFFSET, 0);
            this.WriteToBurnSectionOffset(BURN_SECTION_OFFSET_ORIGINALSIGNATURESIZE, 0);
            this.WriteToBurnSectionOffset(BURN_SECTION_OFFSET_FORMAT, 1); // Hard-coded to CAB for now.
            this.WriteToBurnSectionOffset(BURN_SECTION_OFFSET_COUNT, 0);
            this.WriteToBurnSectionOffset(BURN_SECTION_OFFSET_UXSIZE, 0);
            this.WriteToBurnSectionOffset(BURN_SECTION_OFFSET_ATTACHEDCONTAINERSIZE, 0);
            this.binaryWriter.BaseStream.Flush();

            this.EngineSize = this.StubSize;

            return true;
        }

        /// <summary>
        /// Appends a UX or Attached container to the exe and updates the ".wixburn" section data to point to it.
        /// </summary>
        /// <param name="fileContainer">File path to append to the current exe.</param>
        /// <param name="container">Container section represented by the fileContainer.</param>
        /// <returns>true if the container data is successfully appended; false otherwise</returns>
        public bool AppendContainer(string fileContainer, BurnCommon.Container container)
        {
            using (FileStream reader = File.OpenRead(fileContainer))
            {
                return this.AppendContainer(reader, reader.Length, container);
            }
        }

        /// <summary>
        /// Appends a UX or Attached container to the exe and updates the ".wixburn" section data to point to it.
        /// </summary>
        /// <param name="containerStream">File stream to append to the current exe.</param>
        /// <param name="containerSize">Size of container to append.</param>
        /// <param name="container">Container section represented by the fileContainer.</param>
        /// <returns>true if the container data is successfully appended; false otherwise</returns>
        public bool AppendContainer(Stream containerStream, long containerSize, BurnCommon.Container container)
        {
            UInt32 burnSectionCount = 0;
            UInt32 burnSectionOffsetSize = 0;

            switch (container)
            {
                case Container.UX:
                    burnSectionCount = 1;
                    burnSectionOffsetSize = BURN_SECTION_OFFSET_UXSIZE;
                    // TODO: verify that the size in the section data is 0 or the same size.
                    this.EngineSize += (uint)containerSize;
                    this.UXSize = (uint)containerSize;
                    break;

                case Container.Attached:
                    burnSectionCount = 2;
                    burnSectionOffsetSize = BURN_SECTION_OFFSET_ATTACHEDCONTAINERSIZE;
                    // TODO: verify that the size in the section data is 0 or the same size.
                    this.AttachedContainerSize = (uint)containerSize;
                    break;

                default:
                    Debug.Assert(false);
                    return false;
            }

            return AppendContainer(containerStream, (UInt32)containerSize, burnSectionOffsetSize, burnSectionCount);
        }

        public void RememberThenResetSignature()
        {
            if (this.invalidBundle)
            {
                return;
            }

            this.OriginalChecksum = this.Checksum;
            this.OriginalSignatureOffset = this.SignatureOffset;
            this.OriginalSignatureSize = this.SignatureSize;

            this.WriteToBurnSectionOffset(BURN_SECTION_OFFSET_ORIGINALCHECKSUM, this.OriginalChecksum);
            this.WriteToBurnSectionOffset(BURN_SECTION_OFFSET_ORIGINALSIGNATUREOFFSET, this.OriginalSignatureOffset);
            this.WriteToBurnSectionOffset(BURN_SECTION_OFFSET_ORIGINALSIGNATURESIZE, this.OriginalSignatureSize);

            this.Checksum = 0;
            this.SignatureOffset = 0;
            this.SignatureSize = 0;

            this.WriteToOffset(this.checksumOffset, this.Checksum);
            this.WriteToOffset(this.certificateTableSignatureOffset, this.SignatureOffset);
            this.WriteToOffset(this.certificateTableSignatureSize, this.SignatureSize);
        }

        /// <summary>
        /// Dispose object.
        /// </summary>
        /// <param name="disposing">True when releasing managed objects.</param>
        protected override void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing && this.binaryWriter != null)
                {
                    this.binaryWriter.Close();
                    this.binaryWriter = null;
                }

                this.disposed = true;
            }
        }

        /// <summary>
        /// Appends a container to the exe and updates the ".wixburn" section data to point to it.
        /// </summary>
        /// <param name="containerStream">File stream to append to the current exe.</param>
        /// <param name="burnSectionOffsetSize">Offset of size field for this container in ".wixburn" section data.</param>
        /// <returns>true if the container data is successfully appended; false otherwise</returns>
        private bool AppendContainer(Stream containerStream, UInt32 containerSize, UInt32 burnSectionOffsetSize, UInt32 burnSectionCount)
        {
            if (this.invalidBundle)
            {
                return false;
            }

            // Update the ".wixburn" section data
            this.WriteToBurnSectionOffset(BURN_SECTION_OFFSET_COUNT, burnSectionCount);
            this.WriteToBurnSectionOffset(burnSectionOffsetSize, containerSize);

            // Append the container to the end of the existing bits.
            this.binaryWriter.BaseStream.Seek(0, SeekOrigin.End);
            BurnCommon.CopyStream(containerStream, this.binaryWriter.BaseStream, (int)containerSize);
            this.binaryWriter.BaseStream.Flush();

            return true;
        }

        /// <summary>
        /// Writes the value to an offset in the Burn section data.
        /// </summary>
        /// <param name="offset">Offset in to the Burn section data.</param>
        /// <param name="value">Value to write.</param>
        private void WriteToBurnSectionOffset(uint offset, uint value)
        {
            this.WriteToOffset(this.wixburnDataOffset + offset, value);
        }

        /// <summary>
        /// Writes the value to an offset in the Burn stub.
        /// </summary>
        /// <param name="offset">Offset in to the Burn stub.</param>
        /// <param name="value">Value to write.</param>
        private void WriteToOffset(uint offset, uint value)
        {
            this.binaryWriter.BaseStream.Seek((int)offset, SeekOrigin.Begin);
            this.binaryWriter.Write(value);
        }
    }
}