Initial commit - WPinternals 2.6

This commit is contained in:
Rene Lergner
2018-10-25 22:35:49 +02:00
commit 396ae57f05
483 changed files with 159677 additions and 0 deletions
+223
View File
@@ -0,0 +1,223 @@
//
// Copyright (c) 2008-2011, Kenneth Bell
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
namespace DiscUtils.Raw
{
using System;
using System.Collections.Generic;
using System.IO;
/// <summary>
/// Represents a raw disk image.
/// </summary>
/// <remarks>This disk format is simply an uncompressed capture of all blocks on a disk.</remarks>
public sealed class Disk : VirtualDisk
{
private DiskImageFile _file;
/// <summary>
/// Initializes a new instance of the Disk 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 Disk(Stream stream, Ownership ownsStream)
: this(stream, ownsStream, null)
{
}
/// <summary>
/// Initializes a new instance of the Disk 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>
/// <param name="geometry">The emulated geometry of the disk.</param>
public Disk(Stream stream, Ownership ownsStream, Geometry geometry)
{
_file = new DiskImageFile(stream, ownsStream, geometry);
}
/// <summary>
/// Initializes a new instance of the Disk class.
/// </summary>
/// <param name="path">The path to the disk image.</param>
public Disk(string path)
{
_file = new DiskImageFile(new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.None), Ownership.Dispose, null);
}
/// <summary>
/// Initializes a new instance of the Disk class.
/// </summary>
/// <param name="path">The path to the disk image.</param>
/// <param name="access">The access requested to the disk.</param>
public Disk(string path, FileAccess access)
{
FileShare share = (access == FileAccess.Read) ? FileShare.Read : FileShare.None;
_file = new DiskImageFile(new FileStream(path, FileMode.Open, access, share), Ownership.Dispose, null);
}
/// <summary>
/// Initializes a new instance of the Disk class.
/// </summary>
/// <param name="file">The contents of the disk.</param>
private Disk(DiskImageFile file)
{
_file = file;
}
/// <summary>
/// Gets the geometry of the disk.
/// </summary>
public override Geometry Geometry
{
get { return _file.Geometry; }
}
/// <summary>
/// Gets the type of disk represented by this object.
/// </summary>
public override VirtualDiskClass DiskClass
{
get { return _file.DiskType; }
}
/// <summary>
/// Gets the capacity of the disk (in bytes).
/// </summary>
public override long Capacity
{
get { return _file.Capacity; }
}
/// <summary>
/// Gets the content of the disk as a stream.
/// </summary>
/// <remarks>Note the returned stream is not guaranteed to be at any particular position. The actual position
/// will depend on the last partition table/file system activity, since all access to the disk contents pass
/// through a single stream instance. Set the stream position before accessing the stream.</remarks>
public override SparseStream Content
{
get { return _file.Content; }
}
/// <summary>
/// Gets the layers that make up the disk.
/// </summary>
public override IEnumerable<VirtualDiskLayer> Layers
{
get { yield return _file; }
}
/// <summary>
/// Gets information about the type of disk.
/// </summary>
/// <remarks>This property provides access to meta-data about the disk format, for example whether the
/// BIOS geometry is preserved in the disk file.</remarks>
public override VirtualDiskTypeInfo DiskTypeInfo
{
get { return DiskFactory.MakeDiskTypeInfo(); }
}
/// <summary>
/// Initializes a stream as an unformatted disk.
/// </summary>
/// <param name="stream">The stream to initialize.</param>
/// <param name="ownsStream">Indicates if the new instance controls the lifetime of the stream.</param>
/// <param name="capacity">The desired capacity of the new disk.</param>
/// <returns>An object that accesses the stream as a disk.</returns>
public static Disk Initialize(Stream stream, Ownership ownsStream, long capacity)
{
return Initialize(stream, ownsStream, capacity, null);
}
/// <summary>
/// Initializes a stream as an unformatted disk.
/// </summary>
/// <param name="stream">The stream to initialize.</param>
/// <param name="ownsStream">Indicates if the new instance controls the lifetime of the stream.</param>
/// <param name="capacity">The desired capacity of the new disk.</param>
/// <param name="geometry">The desired geometry of the new disk, or <c>null</c> for default.</param>
/// <returns>An object that accesses the stream as a disk.</returns>
public static Disk Initialize(Stream stream, Ownership ownsStream, long capacity, Geometry geometry)
{
return new Disk(DiskImageFile.Initialize(stream, ownsStream, capacity, geometry));
}
/// <summary>
/// Initializes a stream as an unformatted floppy disk.
/// </summary>
/// <param name="stream">The stream to initialize.</param>
/// <param name="ownsStream">Indicates if the new instance controls the lifetime of the stream.</param>
/// <param name="type">The type of floppy disk image to create.</param>
/// <returns>An object that accesses the stream as a disk.</returns>
public static Disk Initialize(Stream stream, Ownership ownsStream, FloppyDiskType type)
{
return new Disk(DiskImageFile.Initialize(stream, ownsStream, type));
}
/// <summary>
/// Create a new differencing disk, possibly within an existing disk.
/// </summary>
/// <param name="fileSystem">The file system to create the disk on.</param>
/// <param name="path">The path (or URI) for the disk to create.</param>
/// <returns>The newly created disk.</returns>
public override VirtualDisk CreateDifferencingDisk(DiscFileSystem fileSystem, string path)
{
throw new NotSupportedException("Differencing disks not supported for raw disks");
}
/// <summary>
/// Create a new differencing disk.
/// </summary>
/// <param name="path">The path (or URI) for the disk to create.</param>
/// <returns>The newly created disk.</returns>
public override VirtualDisk CreateDifferencingDisk(string path)
{
throw new NotSupportedException("Differencing disks not supported for raw disks");
}
/// <summary>
/// Disposes of underlying resources.
/// </summary>
/// <param name="disposing">Set to <c>true</c> if called within Dispose(),
/// else <c>false</c>.</param>
protected override void Dispose(bool disposing)
{
try
{
if (disposing)
{
if (_file != null)
{
_file.Dispose();
}
_file = null;
}
}
finally
{
base.Dispose(disposing);
}
}
}
}
+81
View File
@@ -0,0 +1,81 @@
//
// Copyright (c) 2008-2011, Kenneth Bell
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
namespace DiscUtils.Raw
{
using System;
using System.Collections.Generic;
using System.IO;
[VirtualDiskFactory("RAW", ".img,.ima,.vfd,.flp,.bif")]
internal sealed class DiskFactory : VirtualDiskFactory
{
public override string[] Variants
{
get { return new string[] { }; }
}
public override VirtualDiskTypeInfo GetDiskTypeInformation(string variant)
{
return MakeDiskTypeInfo();
}
public override DiskImageBuilder GetImageBuilder(string variant)
{
throw new NotSupportedException();
}
public override VirtualDisk CreateDisk(FileLocator locator, string variant, string path, VirtualDiskParameters diskParameters)
{
return Disk.Initialize(locator.Open(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None), Ownership.Dispose, diskParameters.Capacity, diskParameters.Geometry);
}
public override VirtualDisk OpenDisk(string path, FileAccess access)
{
return new Disk(path, access);
}
public override VirtualDisk OpenDisk(FileLocator locator, string path, FileAccess access)
{
FileShare share = access == FileAccess.Read ? FileShare.Read : FileShare.None;
return new Disk(locator.Open(path, FileMode.Open, access, share), Ownership.Dispose);
}
public override VirtualDiskLayer OpenDiskLayer(FileLocator locator, string path, FileAccess access)
{
return null;
}
internal static VirtualDiskTypeInfo MakeDiskTypeInfo()
{
return new VirtualDiskTypeInfo()
{
Name = "RAW",
Variant = string.Empty,
CanBeHardDisk = true,
DeterministicGeometry = true,
PreservesBiosGeometry = false,
CalcGeometry = c => Geometry.FromCapacity(c),
};
}
}
}
+258
View File
@@ -0,0 +1,258 @@
//
// Copyright (c) 2008-2011, Kenneth Bell
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
namespace DiscUtils.Raw
{
using System;
using System.IO;
using DiscUtils.Partitions;
/// <summary>
/// Represents a single raw disk image file.
/// </summary>
public sealed class DiskImageFile : VirtualDiskLayer
{
private SparseStream _content;
private Ownership _ownsContent;
private Geometry _geometry;
/// <summary>
/// Initializes a new instance of the DiskImageFile class.
/// </summary>
/// <param name="stream">The stream to interpret.</param>
public DiskImageFile(Stream stream)
: this(stream, Ownership.None, null)
{
}
/// <summary>
/// Initializes a new instance of the DiskImageFile class.
/// </summary>
/// <param name="stream">The stream to interpret.</param>
/// <param name="ownsStream">Indicates if the new instance should control the lifetime of the stream.</param>
/// <param name="geometry">The emulated geometry of the disk.</param>
public DiskImageFile(Stream stream, Ownership ownsStream, Geometry geometry)
{
_content = stream as SparseStream;
_ownsContent = ownsStream;
if (_content == null)
{
_content = SparseStream.FromStream(stream, ownsStream);
_ownsContent = Ownership.Dispose;
}
_geometry = geometry ?? DetectGeometry(_content);
}
/// <summary>
/// Gets the geometry of the file.
/// </summary>
public override Geometry Geometry
{
get { return _geometry; }
}
/// <summary>
/// Gets a value indicating if the layer only stores meaningful sectors.
/// </summary>
public override bool IsSparse
{
get { return false; }
}
/// <summary>
/// Gets a value indicating whether the file is a differencing disk.
/// </summary>
public override bool NeedsParent
{
get { return false; }
}
/// <summary>
/// Gets the type of disk represented by this object.
/// </summary>
public VirtualDiskClass DiskType
{
get { return DetectDiskType(Capacity); }
}
internal override long Capacity
{
get { return _content.Length; }
}
internal override FileLocator RelativeFileLocator
{
get { return null; }
}
internal SparseStream Content
{
get { return _content; }
}
/// <summary>
/// Initializes a stream as a raw disk image.
/// </summary>
/// <param name="stream">The stream to initialize.</param>
/// <param name="ownsStream">Indicates if the new instance controls the lifetime of the stream.</param>
/// <param name="capacity">The desired capacity of the new disk.</param>
/// <param name="geometry">The geometry of the new disk.</param>
/// <returns>An object that accesses the stream as a raw disk image.</returns>
public static DiskImageFile Initialize(Stream stream, Ownership ownsStream, long capacity, Geometry geometry)
{
stream.SetLength(Utilities.RoundUp(capacity, Sizes.Sector));
// Wipe any pre-existing master boot record / BPB
stream.Position = 0;
stream.Write(new byte[Sizes.Sector], 0, Sizes.Sector);
stream.Position = 0;
return new DiskImageFile(stream, ownsStream, geometry);
}
/// <summary>
/// Initializes a stream as an unformatted floppy disk.
/// </summary>
/// <param name="stream">The stream to initialize.</param>
/// <param name="ownsStream">Indicates if the new instance controls the lifetime of the stream.</param>
/// <param name="type">The type of floppy disk image to create.</param>
/// <returns>An object that accesses the stream as a disk.</returns>
public static DiskImageFile Initialize(Stream stream, Ownership ownsStream, FloppyDiskType type)
{
return Initialize(stream, ownsStream, FloppyCapacity(type), null);
}
/// <summary>
/// Gets the content of this layer.
/// </summary>
/// <param name="parent">The parent stream (if any).</param>
/// <param name="ownsParent">Controls ownership of the parent stream.</param>
/// <returns>The content as a stream.</returns>
public override SparseStream OpenContent(SparseStream parent, Ownership ownsParent)
{
if (ownsParent == Ownership.Dispose && parent != null)
{
parent.Dispose();
}
return SparseStream.FromStream(Content, Ownership.None);
}
/// <summary>
/// Gets the possible locations of the parent file (if any).
/// </summary>
/// <returns>Array of strings, empty if no parent.</returns>
public override string[] GetParentLocations()
{
return new string[0];
}
/// <summary>
/// Disposes of underlying resources.
/// </summary>
/// <param name="disposing">Set to <c>true</c> if called within Dispose(),
/// else <c>false</c>.</param>
protected override void Dispose(bool disposing)
{
try
{
if (disposing)
{
if (_ownsContent == Ownership.Dispose && _content != null)
{
_content.Dispose();
}
_content = null;
}
}
finally
{
base.Dispose(disposing);
}
}
/// <summary>
/// Calculates the best guess geometry of a disk.
/// </summary>
/// <param name="disk">The disk to detect the geometry of.</param>
/// <returns>The geometry of the disk.</returns>
private static Geometry DetectGeometry(Stream disk)
{
long capacity = disk.Length;
// First, check for floppy disk capacities - these have well-defined geometries
if (capacity == Sizes.Sector * 1440)
{
return new Geometry(80, 2, 9);
}
else if (capacity == Sizes.Sector * 2880)
{
return new Geometry(80, 2, 18);
}
else if (capacity == Sizes.Sector * 5760)
{
return new Geometry(80, 2, 36);
}
// Failing that, try to detect the geometry from any partition table.
// Note: this call falls back to guessing the geometry from the capacity
return BiosPartitionTable.DetectGeometry(disk);
}
/// <summary>
/// Calculates the best guess disk type (i.e. floppy or hard disk).
/// </summary>
/// <param name="capacity">The capacity of the disk.</param>
/// <returns>The disk type.</returns>
private static VirtualDiskClass DetectDiskType(long capacity)
{
if (capacity == Sizes.Sector * 1440
|| capacity == Sizes.Sector * 2880
|| capacity == Sizes.Sector * 5760)
{
return VirtualDiskClass.FloppyDisk;
}
else
{
return VirtualDiskClass.HardDisk;
}
}
private static long FloppyCapacity(FloppyDiskType type)
{
switch (type)
{
case FloppyDiskType.DoubleDensity:
return Sizes.Sector * 1440;
case FloppyDiskType.HighDensity:
return Sizes.Sector * 2880;
case FloppyDiskType.Extended:
return Sizes.Sector * 5760;
default:
throw new ArgumentException("Invalid floppy disk type", "type");
}
}
}
}