/////////////////////////////////////////////////////////////////////////////// // Initialize CAS Headers public static TVCFileTypes.CASProgramFileHeaderType CreateProgramFileHeader(ProgramStorage in_storage) { TVCFileTypes.CASProgramFileHeaderType header = new TVCFileTypes.CASProgramFileHeaderType(); header.FileType = in_storage.GetFileTypeByte(); header.FileLength = in_storage.Length; header.Autorun = (byte)((in_storage.AutoStart) ? 0xff : 0x00); header.Version = 0; return(header); }
/////////////////////////////////////////////////////////////////////////////// // Saves CAS file public static void CASSave(Stream in_file, ProgramStorage in_storage) { using (BinaryWriter cas_file = new BinaryWriter(in_file)) { TVCFileTypes.CASUPMHeaderType upm_header = CreateUPMHeader(in_storage); TVCFileTypes.CASProgramFileHeaderType program_header = CreateProgramFileHeader(in_storage); cas_file.Write(upm_header); cas_file.Write(program_header); cas_file.Write(in_storage.Data, 0, in_storage.Length); } }
/// <summary> /// Closes opened file after write /// </summary> private void CasCloseWrite() { if (m_write_file != null) { try { if (m_write_mode == FileWriteMode.CASFile) { // read back cas header BinaryReader reader = new BinaryReader(m_write_stream); reader.BaseStream.Seek(Marshal.SizeOf(typeof(TVCFileTypes.CASUPMHeaderType)), SeekOrigin.Begin); TVCFileTypes.CASProgramFileHeaderType program_header = new TVCFileTypes.CASProgramFileHeaderType(); reader.Read(program_header); // in case of cas file update header TVCFileTypes.CASUPMHeaderType upm_header = new TVCFileTypes.CASUPMHeaderType(); ushort cas_length = (ushort)(program_header.FileLength + Marshal.SizeOf(typeof(TVCFileTypes.CASUPMHeaderType)) + Marshal.SizeOf(typeof(TVCFileTypes.CASProgramFileHeaderType))); upm_header.FileType = TVCFileTypes.CASBlockHeaderFileUnbuffered; upm_header.CopyProtect = 0; upm_header.BlockNumber = (byte)(cas_length / 128); upm_header.LastBlockBytes = (byte)(cas_length % 128); m_write_file.Seek(0, SeekOrigin.Begin); m_write_file.Write(upm_header); } m_write_file.Close(); m_write_stream.Close(); EndFunction(CoProcessorConstants.ERR_OK); } catch { EndFunction(CoProcessorConstants.ERR_DISK_FULL); } finally { m_write_file = null; m_write_stream = null; } } else { EndFunction(CoProcessorConstants.ERR_NO_OPENED_FILE); } }
public void Open() { int buffer_pos = 0; int dir_length = 0; // path display length StoreRootPathInfo(ref buffer_pos); dir_length += buffer_pos; buffer_pos = 0; // files display length DirectoryInfo directory_info = new DirectoryInfo(Cas.CoProc.Cart.Settings.FilesystemFolder); m_directory_info = directory_info.GetFiles(); foreach (FileInfo file_info in m_directory_info) { StoreFileInfo(ref buffer_pos, file_info); dir_length += buffer_pos; buffer_pos = 0; } dir_length += 1; // Terminator Basic program zero // Create CAS header TVCFileTypes.CASProgramFileHeaderType cas_header = new TVCFileTypes.CASProgramFileHeaderType(); cas_header.FileType = 1; cas_header.FileLength = (ushort)dir_length; // Length of the file cas_header.Autorun = 0xff; // Autostart: 0xff, no autostart: 0x00 using (MemoryStream stream = new MemoryStream()) { stream.Write(cas_header); stream.ToArray().CopyTo(m_directory_buffer, 0); } m_directory_pos = 0; m_directory_index = 0; }
/////////////////////////////////////////////////////////////////////////////// // Loads CAS file public static void CASLoad(Stream in_file, ProgramStorage in_storage) { TVCFileTypes.CASUPMHeaderType upm_header = new TVCFileTypes.CASUPMHeaderType(); TVCFileTypes.CASProgramFileHeaderType program_header = new TVCFileTypes.CASProgramFileHeaderType(); // open CAS file using (BinaryReader cas_file = new BinaryReader(in_file)) { // load UPM header cas_file.Read(upm_header); // load program header cas_file.Read(program_header); // Check validity if (!CASCheckHeaderValidity(program_header)) { throw new FileFormatException("Invalid CAS header"); } if (!CASCheckUPMHeaderValidity(upm_header)) { throw new FileFormatException("Invalid UPM header"); } cas_file.Read(in_storage.Data, 0, program_header.FileLength); in_storage.Length = program_header.FileLength; in_storage.CopyProtect = (upm_header.CopyProtect != 0); in_storage.AutoStart = (program_header.Autorun != 0); in_storage.SetFileTypeByte(program_header.FileType); } // generate TVC filename //PCToTVCFilename(g_db_file_name, in_file_name); }
/////////////////////////////////////////////////////////////////////////////// // Cheks CAS header validity public static bool CASCheckHeaderValidity(TVCFileTypes.CASProgramFileHeaderType in_header) { return(in_header.Zero == 0 && (in_header.FileType == 0x00 || in_header.FileType == 0x01)); }