/// <summary> /// Initializes a new instance of the <see cref="DiskImageFile"/> class. /// </summary> /// <param name="stream">The stream to read.</param> /// <param name="ownsStream">Indicates if the new instance should control the lifetime of the stream.</param> public DiskImageFile(Stream stream, Ownership ownsStream) { this.udifHeader = new UdifResourceFile(); this.stream = stream ?? throw new ArgumentNullException(nameof(stream)); this.ownsStream = ownsStream; if (stream.Length < this.udifHeader.Size) { throw new InvalidDataException("The file is not a valid DMG file: could not read the UDIF header."); } stream.Position = stream.Length - this.udifHeader.Size; byte[] data = StreamUtilities.ReadExact(stream, this.udifHeader.Size); this.udifHeader.ReadFrom(data, 0); if (!this.udifHeader.SignatureValid) { throw new InvalidDataException("The file is not a valid DMG file: could not read the UDIF header."); } stream.Position = (long)this.udifHeader.XmlOffset; byte[] xmlData = StreamUtilities.ReadExact(stream, (int)this.udifHeader.XmlLength); Dictionary <string, object> plist = (Dictionary <string, object>)XmlPropertyListParser.Parse(xmlData).ToObject(); this.resources = ResourceFork.FromPlist(plist); this.Buffer = new UdifBuffer(stream, this.resources, this.udifHeader.SectorCount); }
/// <summary> /// Initializes a new instance of the DiskImageFile class. /// </summary> /// <param name="stream">The stream to read</param> /// <param name="ownsStream">Indicates if the new instance should control the lifetime of the stream.</param> public DiskImageFile(Stream stream, Ownership ownsStream) { _udifHeader = new UdifResourceFile(); _stream = stream; _ownsStream = ownsStream; stream.Position = stream.Length - _udifHeader.Size; byte[] data = Utilities.ReadFully(stream, _udifHeader.Size); _udifHeader.ReadFrom(data, 0); if (_udifHeader.SignatureValid) { stream.Position = (long)_udifHeader.XmlOffset; byte[] xmlData = Utilities.ReadFully(stream, (int)_udifHeader.XmlLength); var plist = Plist.Parse(new MemoryStream(xmlData)); _resources = ResourceFork.FromPlist(plist); _buffer = new UdifBuffer(stream, _resources, _udifHeader.SectorCount); } }
/// <summary> /// Initializes a new instance of the DiskImageFile class. /// </summary> /// <param name="stream">The stream to read.</param> /// <param name="ownsStream">Indicates if the new instance should control the lifetime of the stream.</param> public DiskImageFile(Stream stream, Ownership ownsStream) { _udifHeader = new UdifResourceFile(); _stream = stream; _ownsStream = ownsStream; stream.Position = stream.Length - _udifHeader.Size; byte[] data = StreamUtilities.ReadExact(stream, _udifHeader.Size); _udifHeader.ReadFrom(data, 0); if (_udifHeader.SignatureValid) { stream.Position = (long)_udifHeader.XmlOffset; byte[] xmlData = StreamUtilities.ReadExact(stream, (int)_udifHeader.XmlLength); Dictionary <string, object> plist = Plist.Parse(new MemoryStream(xmlData)); _resources = ResourceFork.FromPlist(plist); Buffer = new UdifBuffer(stream, _resources, _udifHeader.SectorCount); } }