/// <summary> /// Get a path table entry's data /// </summary> /// <param name="entry">The entry</param> /// <param name="type">The type of the entry (little endian or big endian)</param> private byte[] GetPathTableEntryBuffer(DirectoryEntry entry, PathTableType type, ushort parentNumber) { int entrySize = 8 + entry.Name.Length + (entry.Name.Length % 2 != 0 ? 1 : 0); byte[] buffer = new byte[entrySize]; using (var stream = new CBinaryWriter(buffer)) { stream.Write((byte)entry.Name.Length); stream.Write(entry.ExtendedAttributeRecordlength); if (type == PathTableType.LE) { stream.Write(entry.ExtentLba); stream.Write(parentNumber); } else { stream.WriteBE(entry.ExtentLba); stream.WriteBE(parentNumber); } stream.WriteAsciiString(entry.Name); if (entry.Name.Length % 2 != 0) { stream.Write((byte)0); } } return(buffer); }
/// <summary> /// Write out the directory entry /// </summary> private void WriteDirectoryEntry(DataTrackIndexEntry entry) { SeekSector(entry.Lba); int size = (int)entry.Size; int sectorSize = GetSectorDataSize(_defaultSectorMode); byte[] data = new byte[size]; using (var stream = new CBinaryWriter(data)) { // First directory entry of a directory entry is the directory itself stream.Write(GetDirectoryEntryBuffer(entry.DirectoryEntry, true, false)); // Second directory entry is the parent directory entry. if (entry.ParentEntry != null) { stream.Write(GetDirectoryEntryBuffer(entry.ParentEntry.DirectoryEntry, false, true)); } else { stream.Write(GetDirectoryEntryBuffer(entry.DirectoryEntry, false, true)); } foreach (var subEntry in entry.SubEntries) { // DirectoryEntry cannot be "splitted" on two sectors if ((stream.Position - (stream.Position / sectorSize) * sectorSize) + subEntry.Length >= sectorSize) { stream.Position = ((stream.Position / sectorSize) + 1) * sectorSize; } if (stream.Position + subEntry.DirectoryEntry.Length < size) { stream.Write(GetDirectoryEntryBuffer(subEntry.DirectoryEntry)); } else { throw new FrameworkException("Error while finalizing disk : directory \"{0}\" is too small", entry.FullPath); } } } for (int i = 0; i < size; i += sectorSize) { WriteSector ( CBuffer.Create(data, i, sectorSize), _defaultSectorMode, (i + sectorSize >= size) ? XaSubHeader.EndOfFile : XaSubHeader.Basic ); } }
private void writeGDFsizes(CBinaryWriter bw) { long length = bw.BaseStream.Length; bw.Seek(8L, SeekOrigin.Begin); bw.Write(length - 0x400L); uint num2 = (uint)(length / 2048.0); bw.Seek(0x8050L, SeekOrigin.Begin); bw.Write(num2); bw.Endian = EndianType.BigEndian; bw.Seek(0x8054L, SeekOrigin.Begin); bw.Write(num2); bw.Endian = EndianType.LittleEndian; }
public void Write(CBinaryWriter bw) { bw.Write(Size); bw.Write((uint)Flags); bw.Write(Height); bw.Write(Width); bw.Write(Depth); bw.Write(Pitch); bw.Write(MipMapCount); bw.Write(Reserved); PixelFormat.Write(bw); Caps.Write(bw); bw.Write(Reserved2); }
public void Write(CBinaryWriter bw) { bw.Write(this.Size); bw.Write((uint)this.Flags); bw.Write(this.Height); bw.Write(this.Width); bw.Write(this.Depth); bw.Write(this.Pitch); bw.Write(this.MipMapCount); bw.Write(this.Reserved); this.PixelFormat.Write(bw); this.Caps.Write(bw); bw.Write(this.Reserved2); }
/// <summary> /// Write header /// </summary> /// <param name="stream">The stream to write to</param> internal void WriteHeader(Stream stream) { try { CBinaryWriter writer = new CBinaryWriter(stream); writer.Write(SIGNATURE); writer.Write((byte)_method); writer.Write(_flags); writer.Write((uint)((_date - new DateTime(1970, 1, 1).ToLocalTime()).TotalSeconds)); writer.Write(_xfl); writer.Write(_os); if (HasExtra) { writer.Write((ushort)_extra.Length); writer.WriteAsciiString(_extra); } if (HasName) { writer.WriteAsciiString(_name); writer.Write((byte)0); } if (HasComment) { writer.WriteAsciiString(_comment); writer.Write((byte)0); } if (HasCrc) { writer.Write(_crc); } } catch (FrameworkException ex) { throw ex; } catch (Exception) { throw new FrameworkException("Error while writing gzip data : unable to write header"); } }
private string createUniqueName(IsoEntry Iso) { MemoryStream s = new MemoryStream(); CBinaryWriter writer = new CBinaryWriter(EndianType.LittleEndian, s); writer.Write(Iso.ID.TitleID); writer.Write(Iso.ID.MediaID); writer.Write(Iso.ID.DiscNumber); writer.Write(Iso.ID.DiscCount); byte[] buffer = sha1.ComputeHash(s.ToArray()); string str = ""; for (int i = 0; i < (buffer.Length / 2); i++) { str = str + buffer[i].ToString("X02"); } return(str); }
/// <summary> /// Write footer /// </summary> /// <param name="stream">The stream to write to</param> internal void WriteFooter(Stream stream) { try { var writer = new CBinaryWriter(stream); writer.Write(_crc32); writer.Write(_dataRealSize); } catch (FrameworkException ex) { throw ex; } catch (Exception) { throw new FrameworkException("Error while writing gzip data : unable to write footer"); } }
public void Write(CBinaryWriter bw) { bw.Write(Size); bw.Write((uint)Flags); bw.Write((uint)FourCC); bw.Write(RGBBitCount); bw.Write(BitMaskRed); bw.Write(BitMaskGreen); bw.Write(BitMaskBlue); bw.Write(BitMaskRGBAlpha); }
public void Write(CBinaryWriter bw) { bw.Write(this.Size); bw.Write((uint)this.Flags); bw.Write((uint)this.FourCC); bw.Write(this.RGBBitCount); bw.Write(this.BitMaskRed); bw.Write(this.BitMaskGreen); bw.Write(this.BitMaskBlue); bw.Write(this.BitMaskRGBAlpha); }
public long WriteFileToStream(string Path, CBinaryWriter Writer) { try { GDFDirTable folder = this.GetFolder(this.rootDir, Path); string str = Path.Contains(@"\") ? Path.Substring(Path.LastIndexOf(@"\") + 1) : Path; foreach (GDFDirEntry entry in folder) { if (entry.Name.ToLower() == str.ToLower()) { this.fr.Seek((long)(this.volDesc.RootOffset + (entry.Sector * this.volDesc.SectorSize)), SeekOrigin.Begin); uint num = (uint)Math.Ceiling((double)(((double)entry.Size) / ((double)this.volDesc.SectorSize))); long num2 = 0L; for (uint i = 0; i < num; i++) { byte[] buffer; if ((num2 + this.volDesc.SectorSize) > entry.Size) { buffer = this.fr.ReadBytes((int)(entry.Size - num2)); Writer.Write(buffer); int num4 = ((int)this.volDesc.SectorSize) - buffer.Length; for (int j = 0; j < num4; j++) { Writer.Write((byte)0); } } else { buffer = this.fr.ReadBytes((int)this.volDesc.SectorSize); Writer.Write(buffer); } num2 += this.volDesc.SectorSize; } return((long)entry.Size); } } } catch (Exception exception) { this.Exceptions.Add(exception); } return(-1L); }
public void Write(CBinaryWriter bw) { bw.Endian = EndianType.LittleEndian; bw.WriteUint32(this.magicBytes); this.Header.Write(bw); if (this.Data != null) { bw.Write(this.Data); } }
private void writeGDFheader(GDF src, CBinaryWriter bw) { bw.Seek(0L, SeekOrigin.Begin); bw.Write((uint)0x1a465358); bw.Write((uint)0x400); bw.Seek(0x8000L, SeekOrigin.Begin); bw.Write(gdf_sector); bw.Seek(0x10000L, SeekOrigin.Begin); bw.Write(src.VolDesc.Identifier); bw.Write(rootDir.Sector); bw.Write(sizeToSectors(src, rootDir.Size) * src.VolDesc.SectorSize); bw.Write(src.VolDesc.ImageCreationTime); bw.Write((byte)1); bw.Seek(0x107ecL, SeekOrigin.Begin); bw.Write(src.VolDesc.Identifier); }
/// <summary> /// Write a sector /// </summary> public void WriteSector(byte[] data) { try { _stream.Write(data); } catch (Exception) { throw new FrameworkException("Errow while writing sector : unable to write sector"); } }
/// <summary> /// Write a sector /// </summary> /// <param name="data">The sector's data to write</param> /// <param name="mode">Sector's mode</param> /// <param name="subHeader">Subheader (if mode XA_FORM1 or XA_FORM2)</param> public void WriteSector(byte[] data, SectorMode mode, XaSubHeader subHeader = null) { try { byte[] buffer = new byte[_sectorSize]; using (var bufferStream = new CBinaryWriter(buffer)) { if (mode != SectorMode.RAW) { bufferStream.Write(SYNC); long position = SectorPosition + 150; byte[] header = new byte[4]; header[3] = (byte)(mode == SectorMode.MODE0 ? 0 : mode == SectorMode.MODE1 ? 1 : 2); header[2] = Converter.DecToBcd((byte)(position % 75)); position /= 75; header[1] = Converter.DecToBcd((byte)(position % 60)); position /= 60; header[0] = Converter.DecToBcd((byte)(position % 60)); bufferStream.Write(header); } if (mode == SectorMode.XA_FORM1 || mode == SectorMode.XA_FORM2) { if (subHeader == null) { subHeader = new XaSubHeader(); } subHeader.IsForm2 = (mode == SectorMode.XA_FORM2); bufferStream.Write(subHeader.File); bufferStream.Write(subHeader.Channel); bufferStream.Write(subHeader.SubMode); bufferStream.Write(subHeader.DataType); // Subheader is written twice bufferStream.Write(subHeader.File); bufferStream.Write(subHeader.Channel); bufferStream.Write(subHeader.SubMode); bufferStream.Write(subHeader.DataType); } bufferStream.Write(data, 0, data.Length); if (mode == SectorMode.MODE1 || mode == SectorMode.XA_FORM1 || mode == SectorMode.XA_FORM2) { EccEdc.EccEdcCompute(buffer, mode); } _stream.Write(buffer); } } catch (Exception) { throw new FrameworkException("Errow while writing sector : unable to write sector"); } }
/// <summary> /// Get a set terminator volume descriptor data /// </summary> private byte[] GetSetTerminatorVolumeDescriptorBuffer() { byte[] buffer = new byte[GetSectorDataSize(_defaultSectorMode)]; try { using (var stream = new CBinaryWriter(buffer)) { var descriptor = new SetTerminatorVolumeDescriptor(); stream.Write((byte)descriptor.Type); stream.WriteAsciiString(descriptor.Id); stream.Write(descriptor.Version); } return(buffer); } catch (Exception) { throw new FrameworkException("Error while writing SetTerminatorVolumeDescriptor : unable to write the descriptor"); } }
/// <summary> /// Extract the audio track /// </summary> /// <param name="outFilePath">The full path of the disk's file (eg : /FOO/BAR/FILE.EXT)</param> /// <param name="container">The container used for the file</param> public void Extract(string outFilePath, AudioFileContainer container) { try { Directory.CreateDirectory(Path.GetDirectoryName(outFilePath)); using (var fs = new FileStream(outFilePath, FileMode.Create, FileAccess.Write)) { if (container == AudioFileContainer.WAVE) { using (var stream = new CBinaryWriter(fs)) { // WAVE Header uint dataSize = (uint)(_size * _sectorSize); stream.WriteAsciiString("RIFF"); // RIFF sign stream.Write((uint)(dataSize + 44 - 8)); // File size - 8, wave header is 44 bytes long stream.WriteAsciiString("WAVE"); // Format ID stream.WriteAsciiString("fmt", 4); // Format bloc ID stream.Write((uint)16); // Bloc size stream.Write((ushort)0x01); // PCM stream.Write((ushort)2); // Channels stream.Write((uint)44100); // Frequency stream.Write((uint)(44100 * 2 * 16 / 8)); // Bytes per sec (frequency * bytes per bloc) stream.Write((ushort)(2 * 16 / 8)); // Bytes per bloc (channels * bits per sample / 8) stream.Write((ushort)16); // Bits per sample stream.WriteAsciiString("data"); // data bloc sign stream.Write((uint)dataSize); // data size Read(fs); } } else { Read(fs); } } } catch (FrameworkException ex) { throw ex; } catch (Exception ex) { throw new FrameworkException("Error while writing audio file : unable to write file \"{0}\"", outFilePath); } }
private void writeGDFtable(GDF src, CBinaryWriter bw, GDFDirTable table) { bw.Seek(table.Sector * src.VolDesc.SectorSize, SeekOrigin.Begin); byte[] buffer = table.ToByteArray(); bw.Write(buffer); foreach (GDFDirEntry entry in table) { if (entry.IsDirectory && (entry.SubDir != null)) { writeGDFtable(src, bw, entry.SubDir); } } }
private void writeGDFheader(MemoryStream ms) { CBinaryWriter writer = new CBinaryWriter(EndianType.LittleEndian, ms); writer.Seek(0L, SeekOrigin.Begin); writer.Write((uint)0x1a465358); writer.Write((uint)0x400); writer.Seek(0x8000L, SeekOrigin.Begin); writer.Write(gdf_sector); writer.Seek(0x10000L, SeekOrigin.Begin); writer.Write(this.src.VolDesc.Identifier); writer.Write(this.tables[this.tables.Count - 1].Sector); writer.Write((uint)(this.sizeToSectors(this.tables[this.tables.Count - 1].Size) * this.src.VolDesc.SectorSize)); writer.Write(this.src.VolDesc.ImageCreationTime); writer.Write((byte)1); writer.Seek(0x107ecL, SeekOrigin.Begin); writer.Write(this.src.VolDesc.Identifier); writer.Close(); }
/// <summary> /// Write out the path tables /// </summary> private void WritePathTables() { int sectorSize = GetSectorDataSize(_defaultSectorMode); byte[] lePathTableData = new byte[_primaryVolumeDescriptor.PathTableSize]; byte[] bePathTableData = new byte[_primaryVolumeDescriptor.PathTableSize]; var dNums = new Dictionary <string, ushort>(); ushort dNum = 0, refNum; int totalSize = 0; using (var lePathTableStream = new CBinaryWriter(lePathTableData)) using (var bePathTableStream = new CBinaryWriter(bePathTableData)) { lePathTableStream.Write(GetPathTableEntryBuffer(_index.Root.DirectoryEntry, PathTableType.LE, 1)); bePathTableStream.Write(GetPathTableEntryBuffer(_index.Root.DirectoryEntry, PathTableType.BE, 1)); dNums.Add(_index.Root.FullPath, ++dNum); totalSize += (8 + _index.Root.DirectoryEntry.Name.Length + (_index.Root.DirectoryEntry.Name.Length % 2 != 0 ? 1 : 0)); foreach (var entry in _index.GetDirectories(DataTrackEntriesOrder.DEFAULT)) { refNum = dNums[entry.ParentEntry.FullPath]; lePathTableStream.Write(GetPathTableEntryBuffer(entry.DirectoryEntry, PathTableType.LE, refNum)); bePathTableStream.Write(GetPathTableEntryBuffer(entry.DirectoryEntry, PathTableType.BE, refNum)); dNums.Add(entry.FullPath, ++dNum); totalSize += (8 + entry.DirectoryEntry.Name.Length + (entry.DirectoryEntry.Name.Length % 2 != 0 ? 1 : 0)); } } WritePathTable(_primaryVolumeDescriptor.TypeLPathTableLBA, lePathTableData); WritePathTable(_primaryVolumeDescriptor.TypeMPathTableLBA, bePathTableData); if (_hasOptionalPathTable) { WritePathTable(_primaryVolumeDescriptor.OptTypeLPathTableLBA, lePathTableData); WritePathTable(_primaryVolumeDescriptor.OptTypeMPathTableLBA, bePathTableData); } _primaryVolumeDescriptor.PathTableSize = (uint)totalSize; }
public void WriteGDF(CBinaryWriter bw) { MemoryStream ms = new MemoryStream(); ms.Seek(0x12000L, SeekOrigin.Begin); for (int i = this.tables.Count - 1; i >= 0; i--) { if (this.tables[i].Size > 0) { byte[] buffer = this.tables[i].ToByteArray(); ms.Write(buffer, 0, buffer.Length); } } this.writeGDFheader(ms); bw.Seek(0L, SeekOrigin.Begin); bw.Write(ms.ToArray()); ms.Close(); }
public byte[] ToByteArray() { if (Padding == null) { calcPadding(); } byte[] buffer = new byte[EntrySize]; CBinaryWriter writer = new CBinaryWriter(EndianType.LittleEndian, new MemoryStream(buffer)); writer.Write(SubTreeL); writer.Write(SubTreeR); writer.Write(Sector); writer.Write(Size); writer.Write((byte)Attributes); writer.Write(NameLength); writer.Write(Name.ToCharArray(), 0, Name.Length); writer.Write(Padding); writer.Close(); return(buffer); }
public void Write(CBinaryWriter bw) { bw.Write((uint)this.Caps1); bw.Write((uint)this.Caps2); bw.Write(this.Reserved); }
public void Write(string FilePath) { bw.Seek(0x35bL, SeekOrigin.Begin); bw.Write((byte)0); bw.Seek(0x35fL, SeekOrigin.Begin); bw.Write((byte)0); bw.Seek(0x391L, SeekOrigin.Begin); bw.Write((byte)0); WriteHash(); FileStream stream = new FileStream(FilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.None); stream.Write(buffer, 0, buffer.Length); stream.Close(); }
/// <summary> /// Get a directory entry's data /// </summary> /// <param name="entry">The entry</param> /// <param name="selfRef">Is a self reference</param> /// <param name="parentRef">Is a parent reference</param> private byte[] GetDirectoryEntryBuffer(DirectoryEntry entry, bool selfRef = false, bool parentRef = false) { byte entryLength = entry.Length; // If parent or self reference, name is replaced by 0x00 for self or 0x01 for parent if (selfRef || parentRef) { entryLength -= (byte)(entry.Name.Length - 1); entryLength -= (byte)(entry.Name.Length % 2 == 0 ? 1 : 0); } byte[] buffer = new byte[entryLength]; try { using (var stream = new CBinaryWriter(buffer)) { stream.Write(entryLength); stream.Write(entry.ExtendedAttributeRecordlength); stream.Write(entry.ExtentLba); stream.WriteBE(entry.ExtentLba); stream.Write(entry.ExtentSize); stream.WriteBE(entry.ExtentSize); byte[] dateBuffer = new byte[7]; dateBuffer[0] = (byte)(entry.Date.Year - 1900); dateBuffer[1] = (byte)(entry.Date.Month); dateBuffer[2] = (byte)(entry.Date.Day); dateBuffer[3] = (byte)(entry.Date.Hour); dateBuffer[4] = (byte)(entry.Date.Minute); dateBuffer[5] = (byte)(entry.Date.Second); stream.Write(dateBuffer); stream.Write(entry.Flags); stream.Write(entry.FileUnitSize); stream.Write(entry.Interleave); stream.Write(entry.VolumeSequenceNumber); stream.WriteBE(entry.VolumeSequenceNumber); if (!(selfRef || parentRef)) { stream.Write((byte)entry.Name.Length); stream.WriteAsciiString(entry.Name); if (entry.Name.Length % 2 == 0) { stream.Write((byte)0); } } else { stream.Write((byte)1); stream.Write((byte)(selfRef ? 0 : 1)); } if (entry.HasXa) { stream.WriteBE(entry.XaEntry.GroupId); stream.WriteBE(entry.XaEntry.UserId); stream.WriteBE(entry.XaEntry.Attributes); stream.WriteAsciiString(entry.XaEntry.Signature); stream.Write(entry.XaEntry.FileNumber); stream.Write(entry.XaEntry.Unused); } } return(buffer); } catch (Exception) { throw new FrameworkException("Error while writing DirectoryEntry : unable to write the entry"); } }
/// <summary> /// Get a primary volume descriptor data /// </summary> /// <param name="m_primaryVolumeDescriptor">The primary volume descriptor</param> private byte[] GetPrimaryVolumeDescriptorBuffer() { byte[] buffer = new byte[GetSectorDataSize(_defaultSectorMode)]; try { using (var stream = new CBinaryWriter(buffer)) { stream.Write((byte)_primaryVolumeDescriptor.Type); stream.WriteAsciiString(_primaryVolumeDescriptor.Id); stream.Write(_primaryVolumeDescriptor.Version); stream.Write(_primaryVolumeDescriptor.Unused1); stream.WriteAsciiString(_primaryVolumeDescriptor.SystemId, 32, " "); stream.WriteAsciiString(_primaryVolumeDescriptor.VolumeId, 32, " "); stream.Write(_primaryVolumeDescriptor.Unused2); stream.Write(_primaryVolumeDescriptor.VolumeSpaceSize); stream.WriteBE(_primaryVolumeDescriptor.VolumeSpaceSize); stream.Write(_primaryVolumeDescriptor.Unused3); stream.Write(_primaryVolumeDescriptor.VolumeSetSize); stream.WriteBE(_primaryVolumeDescriptor.VolumeSetSize); stream.Write(_primaryVolumeDescriptor.VolumeSequenceNumber); stream.WriteBE(_primaryVolumeDescriptor.VolumeSequenceNumber); stream.Write(_primaryVolumeDescriptor.LogicalBlockSize); stream.WriteBE(_primaryVolumeDescriptor.LogicalBlockSize); stream.Write(_primaryVolumeDescriptor.PathTableSize); stream.WriteBE(_primaryVolumeDescriptor.PathTableSize); stream.Write(_primaryVolumeDescriptor.TypeLPathTableLBA); stream.Write(_primaryVolumeDescriptor.OptTypeLPathTableLBA); stream.WriteBE(_primaryVolumeDescriptor.TypeMPathTableLBA); stream.WriteBE(_primaryVolumeDescriptor.OptTypeMPathTableLBA); stream.Write(GetDirectoryEntryBuffer(_primaryVolumeDescriptor.RootDirectoryEntry)); // TODO : cas des fichiers stream.WriteAsciiString(_primaryVolumeDescriptor.VolumeSetId, 128, " "); stream.WriteAsciiString(_primaryVolumeDescriptor.PublisherId, 128, " "); stream.WriteAsciiString(_primaryVolumeDescriptor.PreparerId, 128, " "); stream.WriteAsciiString(_primaryVolumeDescriptor.ApplicationId, 128, " "); stream.WriteAsciiString(_primaryVolumeDescriptor.CopyrightFileId, 38, " "); stream.WriteAsciiString(_primaryVolumeDescriptor.AbstractFileId, 36, " "); stream.WriteAsciiString(_primaryVolumeDescriptor.BibliographicFileId, 37, " "); // stream.Write(VolumeDescriptor.FromDateTime(_primaryVolumeDescriptor.CreationDate)); stream.Write(VolumeDescriptor.FromDateTime(_primaryVolumeDescriptor.ModificationDate)); stream.Write(VolumeDescriptor.FromDateTime(_primaryVolumeDescriptor.ExpirationDate)); stream.Write(VolumeDescriptor.FromDateTime(_primaryVolumeDescriptor.EffectiveDate)); stream.Write(_primaryVolumeDescriptor.FileStructureVersion); stream.Write(_primaryVolumeDescriptor.Unused4); if (_isXa) { using (CBinaryWriter appDataStream = new CBinaryWriter(_primaryVolumeDescriptor.ApplicationData)) { appDataStream.Position = 0x8D; appDataStream.WriteAsciiString(VolumeDescriptor.VOLUME_XA); } } stream.Write(_primaryVolumeDescriptor.ApplicationData); stream.Write(_primaryVolumeDescriptor.Reserved); } return(buffer); } catch (Exception) { throw new FrameworkException("Error while writing PrimaryVolumeDescriptor : unable to write the descriptor"); } }
public void Write(CBinaryWriter bw) { bw.Write((uint)Caps1); bw.Write((uint)Caps2); bw.Write(Reserved); }