/// <summary> /// Create a new VDK disk associated with the given stream. /// </summary> /// <param name="image">Stream for storing the disk image.</param> /// <param name="heads">Number of disk heads.</param> /// <param name="tracks">Number of tracks per head.</param> /// <param name="sectors">Number of sectors per track.</param> /// <returns>A disk object</returns> public static VdkDisk Create(Stream image, int heads, int tracks, int sectors) { if (image == null) { throw new ArgumentNullException("image"); } if (!image.CanRead) { throw new NotSupportedException("Disk image stream does not support reading"); } if (!image.CanSeek) { throw new NotSupportedException("Disk image stream does not support seeking"); } if (!image.CanWrite) { throw new NotSupportedException("Disk image stream does not support writing"); } var disk = new VdkDisk(image, heads, tracks, sectors); var header = new VdkHeader(heads, tracks); var headerRaw = header.Encode(); disk.vdkHeaderSize = headerRaw.Length; disk.DiskData = new byte[disk.vdkHeaderSize + heads * tracks * sectors * VdkSectorSize]; Array.Copy(headerRaw, disk.DiskData, disk.vdkHeaderSize); disk.IsWriteable = true; disk.IsModified = true; return(disk); }
/// <summary> /// Creates a VDK header object by parsing a byte array. /// </summary> /// <remarks> /// The parser ignores a number of VDK header fields. /// </remarks> /// <param name="raw">The buffer containing the encoded VDK header.</param> /// <returns>A VDK header object.</returns> public static VdkHeader Parse(byte[] raw) { if (raw == null) { throw new ArgumentNullException("raw"); } if (raw.Length < VdkDefaultHeaderSize) { throw new DiskImageFormatException("Disk image format too small to contain a VDK header"); } if (raw[0] != 'd' || raw[1] != 'k') { throw new DiskImageFormatException("VDK image header does not contain magic cookie"); } int headerSize = raw[2] + raw[3] * 256; if (headerSize < VdkDefaultHeaderSize) { throw new DiskImageFormatException("VDK header size is too small"); } int tracks = raw[8]; if (tracks < 1) { throw new DiskImageFormatException("VDK header specifies invalid number of disk tracks"); } int heads = raw[9]; if (heads < 1 || heads > 2) { throw new DiskImageFormatException("VDK header specifies invalid number of disk heads"); } var header = new VdkHeader(heads, tracks, headerSize); return(header); }
/// <summary> /// Reads a VDK disk image from a given stream. /// </summary> /// <param name="image">Stream containing the VDK disk image.</param> /// <param name="isWriteable">Allow write operations to this disk.</param> /// <returns>A disk object.</returns> public static VdkDisk Open(Stream image, bool isWriteable) { if (image == null) { throw new ArgumentNullException("image"); } if (!image.CanRead) { throw new NotSupportedException("Disk image stream does not support reading"); } var imageData = IOUtils.ReadStreamFully(image); var header = VdkHeader.Parse(imageData); int sectors = (imageData.Length - header.HeaderSize) / (header.Heads * header.Tracks * VdkSectorSize); var disk = new VdkDisk(image, header.Heads, header.Tracks, sectors); disk.DiskData = imageData; disk.vdkHeaderSize = header.HeaderSize; disk.IsWriteable = (isWriteable && image.CanSeek && image.CanWrite); return(disk); }
/// <summary> /// Create a new VDK disk associated with the given stream. /// </summary> /// <param name="image">Stream for storing the disk image.</param> /// <param name="heads">Number of disk heads.</param> /// <param name="tracks">Number of tracks per head.</param> /// <param name="sectors">Number of sectors per track.</param> /// <returns>A disk object</returns> public static VdkDisk Create(Stream image, int heads, int tracks, int sectors) { if (image == null) throw new ArgumentNullException("image"); if (!image.CanRead) throw new NotSupportedException("Disk image stream does not support reading"); if (!image.CanSeek) throw new NotSupportedException("Disk image stream does not support seeking"); if (!image.CanWrite) throw new NotSupportedException("Disk image stream does not support writing"); var disk = new VdkDisk(image, heads, tracks, sectors); var header = new VdkHeader(heads, tracks); var headerRaw = header.Encode(); disk.vdkHeaderSize = headerRaw.Length; disk.DiskData = new byte[disk.vdkHeaderSize + heads * tracks * sectors * VdkSectorSize]; Array.Copy(headerRaw, disk.DiskData, disk.vdkHeaderSize); disk.IsWriteable = true; disk.IsModified = true; return disk; }
/// <summary> /// Creates a VDK header object by parsing a byte array. /// </summary> /// <remarks> /// The parser ignores a number of VDK header fields. /// </remarks> /// <param name="raw">The buffer containing the encoded VDK header.</param> /// <returns>A VDK header object.</returns> public static VdkHeader Parse(byte[] raw) { if (raw == null) throw new ArgumentNullException("raw"); if (raw.Length < VdkDefaultHeaderSize) throw new DiskImageFormatException("Disk image format too small to contain a VDK header"); if (raw[0] != 'd' || raw[1] != 'k') throw new DiskImageFormatException("VDK image header does not contain magic cookie"); int headerSize = raw[2] + raw[3]*256; if (headerSize < VdkDefaultHeaderSize) throw new DiskImageFormatException("VDK header size is too small"); int tracks = raw[8]; if (tracks < 1) throw new DiskImageFormatException("VDK header specifies invalid number of disk tracks"); int heads = raw[9]; if (heads < 1 || heads > 2) throw new DiskImageFormatException("VDK header specifies invalid number of disk heads"); var header = new VdkHeader(heads, tracks, headerSize); return header; }