/// <summary> /// Decode the raw on-disk representation of a DragonDos file to a file object. /// </summary> /// <param name="fileinfo">File information.</param> /// <param name="raw">Raw on-disk file data.</param> /// <returns>File object.</returns> /// <exception cref="InvalidFileException">Thrown if the file is invalid and cannot be properly decoded.</exception> internal static DragonDosFile DecodeFile(DragonDosFileInfo fileinfo, byte[] raw) { /* If the file does not contain a valid file header it must be a data file. */ if (raw.Length < FileHeaderSize || raw[0] != FileHeaderFirstByte || raw[8] != FileHeaderLastByte) { return(new DragonDosFile(fileinfo, DragonDosFileType.Data, raw)); } /* Extract the file payload data. */ var data = new byte[raw.Length - FileHeaderSize]; Array.Copy(raw, FileHeaderSize, data, 0, data.Length); /* Create the correct file object. */ switch ((DragonDosFileType)raw[1]) { case DragonDosFileType.Basic: return(new DragonDosFile(fileinfo, DragonDosFileType.Basic, data)); case DragonDosFileType.MachineCode: int loadAddress = (raw[2] << 8) | raw[3]; int startAddress = (raw[6] << 8) | raw[7]; return(new DragonDosFile(fileinfo, DragonDosFileType.MachineCode, data, loadAddress, startAddress)); default: throw new InvalidFileException(fileinfo.Name, String.Format("Unknown file type identifier {0}", raw[1])); } }
/// <summary> /// Decode the raw on-disk representation of a DragonDos file to a file object. /// </summary> /// <param name="fileinfo">File information.</param> /// <param name="raw">Raw on-disk file data.</param> /// <returns>File object.</returns> /// <exception cref="InvalidFileException">Thrown if the file is invalid and cannot be properly decoded.</exception> internal static DragonDosFile DecodeFile(DragonDosFileInfo fileinfo, byte[] raw) { /* If the file does not contain a valid file header it must be a data file. */ if (raw.Length < FileHeaderSize || raw[0] != FileHeaderFirstByte || raw[8] != FileHeaderLastByte) return new DragonDosFile(fileinfo, DragonDosFileType.Data, raw); /* Extract the file payload data. */ var data = new byte[raw.Length - FileHeaderSize]; Array.Copy(raw, FileHeaderSize, data, 0, data.Length); /* Create the correct file object. */ switch ((DragonDosFileType) raw[1]) { case DragonDosFileType.Basic: return new DragonDosFile(fileinfo, DragonDosFileType.Basic, data); case DragonDosFileType.MachineCode: int loadAddress = (raw[2] << 8) | raw[3]; int startAddress = (raw[6] << 8) | raw[7]; return new DragonDosFile(fileinfo, DragonDosFileType.MachineCode, data, loadAddress, startAddress); default: throw new InvalidFileException(fileinfo.Name, String.Format("Unknown file type identifier {0}", raw[1])); } }
/// <summary> /// Create a DragonDos file object that is associated with a filesystem directory entry. /// </summary> /// <param name="info">File directory information.</param> /// <param name="type">File type.</param> /// <param name="data">File payload data.</param> /// <param name="loadaddress">Load address for machine code programs.</param> /// <param name="startaddress">Execution start address for machine code programs.</param> /// <remarks> /// This class assumes ownership of the data parameter and assumes that the caller will not make changes to the contents of this /// array. /// </remarks> private DragonDosFile(DragonDosFileInfo info, DragonDosFileType type, byte[] data, int loadaddress = 0, int startaddress = 0) : this(type, data, loadaddress, startaddress) { fileinfo = info; }