From 69b15d96cebdbb7201b1849b4f62786633d70b8d Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Wed, 1 Nov 2017 10:56:09 -0700 Subject: Introduce WiX Intermediate Representation --- src/WixToolset.Data/FileStructure.cs | 65 ++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 22 deletions(-) (limited to 'src/WixToolset.Data/FileStructure.cs') diff --git a/src/WixToolset.Data/FileStructure.cs b/src/WixToolset.Data/FileStructure.cs index 7265a51d..9ad9405a 100644 --- a/src/WixToolset.Data/FileStructure.cs +++ b/src/WixToolset.Data/FileStructure.cs @@ -7,6 +7,7 @@ namespace WixToolset.Data using System.Diagnostics; using System.IO; using System.Linq; + using System.Text; /// /// Class that understands the standard file structures in the WiX toolset. @@ -20,6 +21,8 @@ namespace WixToolset.Data private static readonly Dictionary SupportedFileFormats = new Dictionary() { + { "wir", FileFormat.WixIR }, + { "wixirf", FileFormat.WixIR }, { "wixobj", FileFormat.Wixobj }, { "wixlib", FileFormat.Wixlib }, { "wixout", FileFormat.Wixout }, @@ -36,7 +39,7 @@ namespace WixToolset.Data /// /// Count of embedded files in the file structure. /// - public int EmbeddedFileCount { get { return this.embeddedFileSizes.Length; } } + public int EmbeddedFileCount => this.embeddedFileSizes.Length; /// /// File format of the file structure. @@ -50,15 +53,15 @@ namespace WixToolset.Data /// File format for the file structure. /// Paths to files to embedd in the file structure. /// Newly created file structure. - public static FileStructure Create(Stream stream, FileFormat fileFormat, List embedFilePaths) + public static FileStructure Create(Stream stream, FileFormat fileFormat, IEnumerable embedFilePaths) { - FileStructure fs = new FileStructure(); - using (NonClosingStreamWrapper wrapper = new NonClosingStreamWrapper(stream)) - using (BinaryWriter writer = new BinaryWriter(wrapper)) + var fs = new FileStructure(); + + using (var writer = new BinaryWriter(stream, Encoding.UTF8, true)) { fs.WriteType(writer, fileFormat); - fs.WriteEmbeddedFiles(writer, embedFilePaths ?? new List()); + fs.WriteEmbeddedFiles(writer, embedFilePaths.ToArray()); // Remember the data stream offset, which is right after the embedded files have been written. fs.dataStreamOffset = stream.Position; @@ -76,13 +79,13 @@ namespace WixToolset.Data /// File structure populated from the stream. public static FileStructure Read(Stream stream) { - FileStructure fs = new FileStructure(); - using (NonClosingStreamWrapper wrapper = new NonClosingStreamWrapper(stream)) - using (BinaryReader reader = new BinaryReader(wrapper)) + var fs = new FileStructure(); + + using (var reader = new BinaryReader(stream, Encoding.UTF8, true)) { fs.FileFormat = FileStructure.ReadFileFormat(reader); - if (FileFormat.Unknown != fs.FileFormat) + if (fs.FileFormat != FileFormat.Unknown) { fs.embeddedFileSizes = FileStructure.ReadEmbeddedFileSizes(reader); @@ -107,8 +110,7 @@ namespace WixToolset.Data /// Best guess at file format. public static FileFormat GuessFileFormatFromExtension(string extension) { - FileFormat format; - return FileStructure.SupportedFileFormats.TryGetValue(extension.TrimStart('.').ToLowerInvariant(), out format) ? format : FileFormat.Unknown; + return FileStructure.SupportedFileFormats.TryGetValue(extension.TrimStart('.').ToLowerInvariant(), out var format) ? format : FileFormat.Unknown; } /// @@ -124,8 +126,7 @@ namespace WixToolset.Data try { - using (NonClosingStreamWrapper wrapper = new NonClosingStreamWrapper(stream)) - using (BinaryReader reader = new BinaryReader(wrapper)) + using (var reader = new BinaryReader(stream, Encoding.UTF8, true)) { format = FileStructure.ReadFileFormat(reader); } @@ -182,7 +183,21 @@ namespace WixToolset.Data } /// - /// Disposes of the internsl state of the file structure. + /// Gets the data of the file as a string. + /// + /// String contents data of the file. + public string GetData() + { + var bytes = new byte[this.stream.Length - this.dataStreamOffset]; + + this.stream.Seek(this.dataStreamOffset, SeekOrigin.Begin); + this.stream.Read(bytes, 0, bytes.Length); + + return Encoding.UTF8.GetString(bytes); + } + + /// + /// Disposes of the internal state of the file structure. /// public void Dispose() { @@ -217,7 +232,13 @@ namespace WixToolset.Data { FileFormat format = FileFormat.Unknown; - string type = new string(reader.ReadChars(6)); + string type = new string(reader.ReadChars(3)); + if (FileStructure.SupportedFileFormats.TryGetValue(type, out format)) + { + return format; + } + + type += new string(reader.ReadChars(3)); FileStructure.SupportedFileFormats.TryGetValue(type, out format); return format; @@ -256,21 +277,21 @@ namespace WixToolset.Data this.FileFormat = fileFormat; - Debug.Assert(6 == type.ToCharArray().Length); + Debug.Assert(3 == type.ToCharArray().Length || 6 == type.ToCharArray().Length); writer.Write(type.ToCharArray()); return writer; } - private BinaryWriter WriteEmbeddedFiles(BinaryWriter writer, List embedFilePaths) + private BinaryWriter WriteEmbeddedFiles(BinaryWriter writer, string[] embedFilePaths) { // First write the count of embedded files as a Uint32; - writer.Write((uint)embedFilePaths.Count); + writer.Write((uint)embedFilePaths.Length); - this.embeddedFileSizes = new long[embedFilePaths.Count]; + this.embeddedFileSizes = new long[embedFilePaths.Length]; // Next write out the size of each file as a Uint64 in order. - FileInfo[] files = new FileInfo[embedFilePaths.Count]; - for (int i = 0; i < embedFilePaths.Count; ++i) + FileInfo[] files = new FileInfo[embedFilePaths.Length]; + for (int i = 0; i < embedFilePaths.Length; ++i) { files[i] = new FileInfo(embedFilePaths[i]); -- cgit v1.2.3-55-g6feb