public async Task Writing() { var ms = new MemoryStream(); var muxer = new Muxer { Channel = ms }; var stream = new Substream { Muxer = muxer }; var m1 = new byte[1]; stream.AddData(new byte[] { 10 }); Assert.IsTrue(stream.CanRead); Assert.IsTrue(stream.CanWrite); Assert.AreEqual(1, await stream.ReadAsync(m1, 0, 1)); await stream.WriteAsync(m1, 0, 1); stream.WriteByte(11); await stream.FlushAsync(); ms.Position = 0; var header = await Header.ReadAsync(ms); var length = await Varint.ReadVarint32Async(ms); var payload = new byte[length]; ms.Read(payload, 0, length); Assert.AreEqual(stream.Id, header.StreamId); Assert.AreEqual(2, payload.Length); CollectionAssert.AreEqual(new byte[] { 10, 11 }, payload); }
private static async Task WriteAsync(Stream stream, byte[] buffer, CancellationToken cancellationToken) { using Substream s = stream.WriteSubstream(); await s.WriteAsync(buffer, cancellationToken); await s.FlushAsync(cancellationToken); }
internal CliMetadataMethodHeader(ICliMetadataRoot metadataRoot, uint relativeVirtualAddress, Action <byte[]> bodyBuilder) { var image = metadataRoot.SourceImage; var rvaLocationScan = image.ResolveRelativeVirtualAddress(relativeVirtualAddress); if (rvaLocationScan.Resolved) { var section = rvaLocationScan.Section; var bodySubstream = new Substream(section.SectionData, rvaLocationScan.Offset, 65536, false); var bodyReader = new EndianAwareBinaryReader(bodySubstream, Endianness.LittleEndian, false); var peekedChar = bodyReader.PeekByte(); if (peekedChar != -1) { MethodHeaderFlags headerType = ((MethodHeaderFlags)peekedChar) & MethodHeaderFlags.WideFormat; if (headerType == MethodHeaderFlags.NarrowFormat) { this.ReadNarrow(bodyReader, metadataRoot, bodyBuilder); } else { this.ReadWide(bodyReader, metadataRoot, bodyBuilder); } } } }
public Stream CreateDecryptionStream(TmdContent content, Stream data) { byte[] iv = new byte[0x10]; BigEndianConverter.GetBytes(content.Index).CopyTo(iv, 0); Stream stream = new Substream(new AesStream(data, Key, iv), 0, content.Size); return(stream); }
public async Task Reading_ClosedStream() { var m1 = new byte[10]; var stream = new Substream(); stream.NoMoreData(); Assert.AreEqual(0, await stream.ReadAsync(m1, 0, 10)); }
public void Dispose() { if (Stream != null) { Stream.Dispose(); Stream = null; } }
public void Length() { var stream = new Substream(); ExceptionAssert.Throws <NotSupportedException>(() => { stream.SetLength(0); }); ExceptionAssert.Throws <NotSupportedException>(() => { var _ = stream.Length; }); }
private async Task DisposeSyncOrAsync(Substream stream, bool async) { if (async) { await stream.DisposeAsync(); } else { stream.Dispose(); } }
public void Disposable() { var s = new Substream(); Assert.IsTrue(s.CanRead); Assert.IsTrue(s.CanWrite); s.Dispose(); Assert.IsFalse(s.CanRead); Assert.IsFalse(s.CanWrite); }
public void Reading_Empty() { var stream = new Substream(); var _ = Task.Run(async() => { await Task.Delay(100); stream.NoMoreData(); }); Assert.AreEqual(-1, stream.ReadByte()); }
public void Seeking() { var stream = new Substream(); Assert.IsFalse(stream.CanSeek); ExceptionAssert.Throws <NotSupportedException>(() => { stream.Seek(0, SeekOrigin.Begin); }); ExceptionAssert.Throws <NotSupportedException>(() => { stream.Position = 0; }); ExceptionAssert.Throws <NotSupportedException>(() => { var _ = stream.Position; }); }
//#region IDisposable Members public void Dispose() { if (this.sectionDataReader != null) { sectionDataReader.Dispose(); this.sectionDataReader = null; } if (this.sectionData != null) { this.sectionData.Close(); this.sectionData.Dispose(); this.sectionData = null; } }
public async Task Reading_Partial() { var m1 = new byte[] { 1, 2, 3, 4 }; var m2 = new byte[m1.Length]; var stream = new Substream(); stream.AddData(m1); stream.NoMoreData(); Assert.AreEqual(4, await stream.ReadAsync(m2, 0, 5)); CollectionAssert.AreEqual(m1, m2); Assert.AreEqual(-1, stream.ReadByte()); Assert.IsFalse(stream.CanRead); }
public void Timeout() { var stream = new Substream(); Assert.IsFalse(stream.CanTimeout); ExceptionAssert.Throws <InvalidOperationException>(() => { stream.ReadTimeout = 0; }); ExceptionAssert.Throws <InvalidOperationException>(() => { var _ = stream.ReadTimeout; }); ExceptionAssert.Throws <InvalidOperationException>(() => { stream.WriteTimeout = 0; }); ExceptionAssert.Throws <InvalidOperationException>(() => { var _ = stream.WriteTimeout; }); }
//#endregion internal virtual void Read(EndianAwareBinaryReader reader) { lock (syncObject) { var newStream = new Substream(reader.BaseStream, reader.BaseStream.Position, base.Size); this.reader = new EndianAwareBinaryReader(newStream, Endianness.LittleEndian, false); byte firstNull = this.reader.ReadByte(); this.AddData(this.GetData(new byte[] { }), 1); if (firstNull != 0) { throw new BadImageFormatException(string.Format("The first item of a {0} heap must be null.", this.Name)); } } //while (this.ReadEntry()) // ; }
public async Task Reading() { var m1 = new byte[] { 1, 2, 3, 4 }; var m2 = new byte[m1.Length]; var stream = new Substream(); stream.AddData(new byte[] { 1, 2 }); stream.AddData(new byte[] { 3, 4 }); stream.NoMoreData(); Assert.IsTrue(stream.CanRead); m2[0] = (byte)stream.ReadByte(); Assert.AreEqual(1, stream.Read(m2, 1, 1)); Assert.AreEqual(2, await stream.ReadAsync(m2, 2, 2)); CollectionAssert.AreEqual(m1, m2); Assert.AreEqual(-1, stream.ReadByte()); Assert.IsFalse(stream.CanRead); }
public async Task Reading_Delayed_Partial() { var m1 = new byte[] { 1, 2, 3, 4 }; var m2 = new byte[m1.Length]; var stream = new Substream(); #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed Task.Run(async() => { await Task.Delay(100); stream.AddData(new byte[] { 1, 2 }); await Task.Delay(100); stream.AddData(new byte[] { 3, 4 }); await Task.Delay(100); stream.NoMoreData(); }); #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed Assert.AreEqual(4, await stream.ReadAsync(m2, 0, 5)); CollectionAssert.AreEqual(m1, m2); }
public Milo(EndianReader reader) : this() { uint magic = new EndianReader(reader.Base, Endianness.BigEndian).ReadUInt32(); if (magic == MagicCompressed) { Compressed = true; } else if (magic == MagicUncompressed) { Compressed = false; } else { throw new NotSupportedException(); } DataOffset = reader.ReadInt32(); int parts = reader.ReadInt32(); int uncompressedsize = reader.ReadInt32(); // Uncompressed data size int offset = DataOffset; for (int i = 0; i < parts; i++) { reader.Position = 0x10 + i * 0x04; int size = reader.ReadInt32(); Stream data = new Substream(reader.Base, offset, size); offset += size; if (Compressed) { Stream old = data; data = new TemporaryStream(); InflateStream(old, data); data.Position = 0; } Data.Add(data); } }
public CPK(Stream data) { EndianReader reader = new EndianReader(data, Endianness.BigEndian); EndianReader lereader = new EndianReader(data, Endianness.LittleEndian); if (reader.ReadUInt32() != CpkMagic) { throw new FormatException(); } for (int i = 0; i < 3; i++) { reader.ReadUInt32(); // Little Endian: 0xFF; section size; 0x00; } Header = new UTF(data); if (Header.Rows.Count != 1) { throw new FormatException(); } UTF.Row mainrow = Header.Rows[0]; long tocoffset = (long)(mainrow.FindValue("TocOffset") as UTF.LongValue).Value; long etocoffset = (long)(mainrow.FindValue("EtocOffset") as UTF.LongValue).Value; long itocoffset = (long)(mainrow.FindValue("ItocOffset") as UTF.LongValue).Value; //long dpkitocoffset = (long)(mainrow.FindValue("DpkItoc") as UTF.IntValue).Value; long contentoffset = (long)(mainrow.FindValue("ContentOffset") as UTF.LongValue).Value; uint filecount = (mainrow.FindValue("Files") as UTF.IntValue).Value; UTF.ShortValue version = mainrow.FindValue("Version") as UTF.ShortValue; UTF.IntValue mode = mainrow.FindValue("Mode") as UTF.IntValue; if (version != null) { Version = version.Value; } if (mode != null) { Mode = mode.Value; } reader.Position = tocoffset; if (reader.ReadUInt32() != TocMagic) { throw new FormatException(); } for (int i = 0; i < 3; i++) { reader.ReadUInt32(); // Little Endian: 0xFF; size; 0x00; } ToC = new UTF(data); reader.Position = etocoffset; if (reader.ReadUInt32() != ETocMagic) { throw new FormatException(); } for (int i = 0; i < 3; i++) { reader.ReadUInt32(); // Little Endian: 0xFF; size; 0x00; } EToC = new UTF(data); reader.Position = itocoffset; if (reader.ReadUInt32() != ITocMagic) { throw new FormatException(); } for (int i = 0; i < 3; i++) { reader.ReadUInt32(); // Little Endian: 0xFF; size; 0x00; } IToC = new UTF(data); Cache = new DelayedStreamCache(); Root = new DirectoryNode(); foreach (var filerow in ToC.Rows) { UTF.StringValue dirnamevalue = filerow.FindValue("DirName") as UTF.StringValue; string dirname = string.Empty; if (dirnamevalue != null) { dirname = dirnamevalue.Value; } long offset = (long)(filerow.FindValue("FileOffset") as UTF.LongValue).Value; string filename = (filerow.FindValue("FileName") as UTF.StringValue).Value; uint packedsize = (filerow.FindValue("FileSize") as UTF.IntValue).Value; uint filesize = packedsize; if (Version == 7) // ToGf { offset += tocoffset; } else // Need to check ToG:Wii's version/mode { offset += contentoffset; } UTF.IntValue filesizevalue = filerow.FindValue("ExtractSize") as UTF.IntValue; if (filesizevalue != null) { filesize = filesizevalue.Value; } else // If we must, read the uncompressed size from the internal compressed file itself { data.Position = offset; EndianReader littlereader = new EndianReader(data, Endianness.LittleEndian); if (littlereader.ReadUInt64() == 0) { filesize = littlereader.ReadUInt32() + 0x100; } } DirectoryNode dir = Root.Navigate(dirname, true) as DirectoryNode; Stream substream = new Substream(data, offset, packedsize); try { if (filesize > packedsize) { //throw new Exception(); Stream compressed = substream; substream = new DelayedStream(delegate() { Stream ret = new TemporaryStream(); CpkCompression.Decompress(compressed, ret); ret.Position = 0; return(ret); }); Cache.AddStream(substream); } dir.AddChild(new FileNode(filename, filesize, substream)); } catch { dir.AddChild(new FileNode(filename, packedsize, substream)); } } // TODO: EToc: Groups // TODO: IToc: Attributes }
internal void Read(EndianAwareBinaryReader reader) { var newStream = new Substream(reader.BaseStream, reader.BaseStream.Position, base.Size); this.reader = new EndianAwareBinaryReader(newStream, Endianness.LittleEndian, false); }
private static void Main(string[] args) { Console.WriteLine("OpenKH Core / Debug Console"); Console.WriteLine("Early Version by GovanifY"); Console.Write("Please enter the name of the game you want to load(KH2 or KH1):"); string game = Console.ReadLine(); Console.Write("Please enter the name of the iso you want to load:"); string isoname = Console.ReadLine(); Console.Write("Please enter the name of the file you want to load:"); string inputname = @Console.ReadLine(); if (game == "KH2") { Console.Write("Trying to load the iso..."); FileStream isoStream = File.Open(isoname, FileMode.Open, FileAccess.Read, FileShare.Read); var iso = new ISOFileReader(isoStream); Console.WriteLine("Done!"); Console.Write("Searching the IDX and IMG files..."); Stream dumb = new FileStream(@"libKh.dll", FileMode.Open, FileAccess.Read); var IDXStream = new Substream(dumb); //Anti CS0165 var IMGStream = new Substream(dumb); //Anti CS0165 var OVLIMGStream = new Substream(dumb); //Anti CS0165 var OVLIDXStream = new Substream(dumb); //Anti CS0165 foreach (FileDescriptor file in iso) { string file2 = file.FullName; if (file2.EndsWith("OVL.IDX")) { OVLIDXStream = iso.GetFileStream(file); } if (file2.EndsWith("OVL.IMG")) { OVLIMGStream = iso.GetFileStream(file); } if (file2.EndsWith("KH2.IDX")) { IDXStream = iso.GetFileStream(file); } if (file2.EndsWith("KH2.IMG")) { IMGStream = iso.GetFileStream(file); } } if (IDXStream == IMGStream) { throw new Exception("IDX or IMG Stream isn't loaded correctly!"); } Console.WriteLine("Done!"); var LIBKHIDX = new IDX(IDXStream, IMGStream); // TODO add a f*****g support for the OVL! Console.Write("Opening the internal file..."); Stream internalfile = LIBKHIDX.OpenFile(inputname); Console.WriteLine("Done!"); Console.Write("Extracting the file..."); string inputname2 = Path.GetFullPath("output/" + inputname); Directory.CreateDirectory(Path.GetDirectoryName(inputname2)); var output = new FileStream(inputname2, FileMode.Create, FileAccess.ReadWrite, FileShare.None); internalfile.CopyTo(output); Console.WriteLine("Done!"); var br = new BinaryReader(output); if (br.ReadUInt32() != 0x01524142) { Console.WriteLine("Not a BAR file, continue anyway"); } else { Console.WriteLine("BAR file detected! Unbarring him..."); var msgSys = new BAR(internalfile); } Console.Read(); } else { throw new Exception("NOT IMPLEMENTED"); } }