public void CreateFromSubStreamKeepingOwnershipThrowIfInvalidArgument() { var stream = new MemoryStream(); stream.WriteByte(0xCA); stream.WriteByte(0xFE); Assert.That( () => DataStreamFactory.FromStreamKeepingOwnership(null, 0, 0), Throws.ArgumentNullException); Assert.That( () => DataStreamFactory.FromStreamKeepingOwnership(stream, -1, 0), Throws.InstanceOf <ArgumentOutOfRangeException>()); Assert.That( () => DataStreamFactory.FromStreamKeepingOwnership(stream, 0, -1), Throws.InstanceOf <ArgumentOutOfRangeException>()); Assert.That( () => DataStreamFactory.FromStreamKeepingOwnership(stream, 3, 0), Throws.InstanceOf <ArgumentOutOfRangeException>()); Assert.That( () => DataStreamFactory.FromStreamKeepingOwnership(stream, 1, 2), Throws.InstanceOf <ArgumentOutOfRangeException>()); stream.Dispose(); }
public void CanReadFiles() { var reader = new DlcBinReader(); byte[] data = { 0x64, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBA, 0xAD, 0xF0, 0x0D, 0xBA, 0xAD, 0xF0, 0x0D, 0xBA, 0xAD, 0xF0, 0x0D, 0xBA, 0xAD, 0xF0, 0x0D, }; using DataStream stream = DataStreamFactory.FromArray(data, 0, data.Length); var format = new BinaryFormat(stream); var result = reader.Convert(format); Assert.AreEqual(1, result.Root.Children.Count); Assert.AreEqual(0x10, result.Root.Children[0].Stream.Length); }
public void CanSetFileNames() { var reader = new StandardBinReader(); var parameters = new ReaderParameters { Endianness = EndiannessMode.LittleEndian, FileNames = new[] { "test/file1" }, }; reader.Initialize(parameters); byte[] data = { 0xF9, 0x7D, 0x07, 0x00, 0x01, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBA, 0xAD, 0xF0, 0x0D, 0xBA, 0xAD, 0xF0, 0x0D, 0xBA, 0xAD, 0xF0, 0x0D, 0xBA, 0xAD, 0xF0, 0x0D, }; using DataStream stream = DataStreamFactory.FromArray(data, 0, data.Length); var format = new BinaryFormat(stream); var result = reader.Convert(format); Assert.AreEqual(1, result.Root.Children.Count); Assert.AreEqual("test", result.Root.Children[0].Name); Assert.AreEqual(1, result.Root.Children[0].Children.Count); Assert.AreEqual(FileType.Normal, result.Root.Children[0].Children[0].Tags["Type"]); Assert.AreEqual("file1", result.Root.Children[0].Children[0].Name); }
public override void ImportPo(string inputFile, bool save = true) { var dataStream = DataStreamFactory.FromFile(inputFile, FileOpenMode.Read); var binary = new BinaryFormat(dataStream); var binary2Po = new Yarhl.Media.Text.Po2Binary(); var po = binary2Po.Convert(binary); _text = GetText(); var tmp = _text.Text.Replace(LineEnding.ShownLineEnding, LineEnding.PoLineEnding); if (string.IsNullOrEmpty(tmp)) { tmp = "<!empty>"; } var entry = po.FindEntry(tmp); if (!string.IsNullOrEmpty(entry.Translated)) { _text.Translation = entry.Translated.Replace(LineEnding.PoLineEnding, LineEnding.ShownLineEnding); } if (save) { SaveChanges(); } }
public static void TranslatePOwithAnotherPO(string BasePO, string TargetPo) { Po BPo = null, TPo = null; using (DataStream name = DataStreamFactory.FromFile(BasePO, FileOpenMode.Read)) using (BinaryFormat binaryname = new BinaryFormat(name)) { BPo = (Po)ConvertFormat.With <Po2Binary>(binaryname); } using (DataStream name = DataStreamFactory.FromFile(TargetPo, FileOpenMode.Read)) using (BinaryFormat binaryname = new BinaryFormat(name)) { TPo = (Po)ConvertFormat.With <Po2Binary>(binaryname); } foreach (PoEntry entryBPo in BPo.Entries) { foreach (PoEntry entryTPo in TPo.Entries) { if (entryBPo.Original == entryTPo.Original && (entryBPo.Translated != null && entryBPo.Translated != "")) { entryTPo.Translated = entryBPo.Translated; if (entryBPo.TranslatorComment != string.Empty && entryBPo.TranslatorComment != null && entryBPo.TranslatorComment.Trim() != "") { entryTPo.TranslatorComment = entryBPo.TranslatorComment; } } } } ConvertFormat.To <BinaryFormat>(TPo).Stream.WriteTo(TargetPo); }
private static DataStream Inflate(DataStream source, EndiannessMode endianness) { DataStream dest = DataStreamFactory.FromMemory(); source.Seek(0); var reader = new DataReader(source) { Endianness = endianness, }; int size = reader.ReadInt32(); int chunkSize = reader.ReadInt32(); while (chunkSize != 0) { using var zlibStream = new ZlibStream(dest, CompressionMode.Decompress, true); source.WriteSegmentTo(source.Position, chunkSize, zlibStream); zlibStream.Close(); source.Seek(chunkSize, SeekOrigin.Current); chunkSize = reader.ReadInt32(); } if (dest.Length != size) { throw new ExtractionException("Result size doesn't match with expected size."); } return(dest); }
public void BadCompressedFilesThrowsException() { byte[] data = { 0x01, 0x02, 0x00, 0x00, 0x31, 0x01, 0x00, 0x00, 0x78, 0xDA, 0x4D, 0x91, 0xCB, 0x6D, 0xC4, 0x30, 0x0C, 0x44, 0x5B, 0x99, 0x02, 0x8C, 0xED, 0x21, 0xF7, 0x24, 0xC8, 0x25, 0x05, 0xD0, 0x32, 0xE3, 0x25, 0x40, 0x49, 0x0E, 0x3F, 0x8B, 0x94, 0x1F, 0x7A, 0xF3, 0xBD, 0x48, 0x10, 0x35, 0x22, 0xE7, 0x8D, 0x1E, 0xA7, 0x71, 0x87, 0x1C, 0x9E, 0x1D, 0xDB, 0xD4, 0x69, 0x70, 0x09, 0x50, 0xE7, 0x58, 0xD0, 0xE6, 0x70, 0x6E, 0xC1, 0x91, 0x06, 0xDA, 0xE4, 0x10, 0x6F, 0x32, 0x76, 0xB0, 0x4A, 0x5C, 0xF0, 0x34, 0x6D, 0x15, 0x1C, 0x57, 0x32, 0x0E, 0x23, 0x64, 0xE0, 0x76, 0x5E, 0x80, 0x77, 0x0E, 0x1C, 0xD3, 0x82, 0x2E, 0x78, 0x0D, 0xA4, 0x2A, 0xF5, 0x36, 0xED, 0x60, 0x5B, 0x30, 0x64, 0xBD, 0x82, 0x20, 0xBD, 0x4E, 0x9B, 0x94, 0x4E, 0xA8, 0xA5, 0x8A, 0x2F, 0xE8, 0xB4, 0x0F, 0x82, 0x89, 0xA7, 0xA3, 0xA5, 0x9D, 0xDB, 0xE0, 0xF7, 0xE4, 0x05, 0xA5, 0x72, 0xDA, 0x25, 0x42, 0xBC, 0x54, 0xEE, 0x54, 0xC7, 0x43, 0x78, 0x80, 0x5A, 0xF5, 0x73, 0xF9, 0x71, 0xF2, 0x63, 0x1B, 0xEC, 0x7F, 0x2F, 0x96, 0x2F, 0x27, 0x88, 0x5A, 0x4F, 0x34, 0xDE, 0x16, 0xF8, 0x54, 0x95, 0x26, 0x91, 0x9B, 0x0C, 0xBC, 0x95, 0x67, 0xBF, 0xE0, 0x99, 0x7A, 0xD9, 0xF7, 0x90, 0x35, 0xB5, 0x82, 0x50, 0xD9, 0x53, 0x09, 0x54, 0x8D, 0xF8, 0xF4, 0x8A, 0x9B, 0xDC, 0xD8, 0xAC, 0x88, 0x5E, 0xAE, 0xE4, 0xAC, 0x7A, 0xB7, 0xD7, 0xA0, 0x54, 0xE9, 0x9D, 0xB8, 0x69, 0x59, 0x5D, 0x9E, 0x88, 0x1B, 0x0F, 0xF2, 0x33, 0x89, 0xAF, 0x1A, 0x64, 0x44, 0xA1, 0x66, 0x5F, 0xB0, 0x2A, 0x8D, 0xAD, 0x3C, 0xAA, 0xAC, 0x6C, 0xB3, 0x2E, 0x16, 0xEC, 0x46, 0x37, 0xD9, 0x6A, 0x4E, 0xEE, 0xC9, 0xD5, 0xDB, 0x66, 0x39, 0x62, 0xE5, 0xCE, 0x23, 0xCE, 0xEF, 0x48, 0x81, 0x6C, 0xDF, 0xD1, 0x44, 0x65, 0x13, 0x52, 0x89, 0xFC, 0xB2, 0x9D, 0xF3, 0xD2, 0xEE, 0xA9, 0x7C, 0x48, 0xAF, 0x59, 0xFF, 0x24, 0x5C, 0x03, 0x2B, 0x9F, 0x43, 0xA9, 0xB1, 0x15, 0x86, 0xF2, 0x44, 0x14, 0x4A, 0xA9, 0x0A, 0xE2, 0x41, 0x4B, 0x55, 0xC4, 0x55, 0xBD, 0x7C, 0x02, 0x79, 0xB7, 0xBB, 0xBF, 0x00, 0x00, 0x00, 0x00, }; using DataStream stream = DataStreamFactory.FromArray(data, 0, data.Length); var format = new BinaryFormat(stream); var decompressor = new Decompressor(); Assert.Throws <ExtractionException>(() => decompressor.Convert(format)); }
public void CreateFromSectionPathInvalidArgsThrows() { string tempFile = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); try { using (var stream = new FileStream(tempFile, FileMode.CreateNew)) { stream.WriteByte(0xCA); stream.WriteByte(0xFE); } Assert.Throws <ArgumentNullException>(() => DataStreamFactory.FromFile(null, FileOpenMode.Append, 0, 0)); Assert.Throws <ArgumentNullException>(() => DataStreamFactory.FromFile(string.Empty, FileOpenMode.Append, 0, 0)); Assert.Throws <ArgumentOutOfRangeException>(() => DataStreamFactory.FromFile(tempFile, FileOpenMode.Read, -1, 0)); Assert.Throws <ArgumentOutOfRangeException>(() => DataStreamFactory.FromFile(tempFile, FileOpenMode.Read, 3, 0)); Assert.Throws <ArgumentOutOfRangeException>(() => DataStreamFactory.FromFile(tempFile, FileOpenMode.Read, 0, -1)); Assert.Throws <ArgumentOutOfRangeException>(() => DataStreamFactory.FromFile(tempFile, FileOpenMode.Read, 0, 3)); Assert.Throws <ArgumentOutOfRangeException>(() => DataStreamFactory.FromFile(tempFile, FileOpenMode.Read, 1, 2)); } finally { File.Delete(tempFile); } }
private static void Decrypt(Options.Decrypt opts) { WriteHeader(); if (!File.Exists(opts.InputFile)) { Console.WriteLine($"ERROR: \"{opts.InputFile}\" not found!!!!"); return; } if (File.Exists(opts.OutputFile)) { Console.WriteLine($"WARNING: \"{opts.OutputFile}\" already exists. It will be overwritten."); Console.Write("Continue? (y/N) "); string answer = Console.ReadLine(); if (!string.IsNullOrEmpty(answer) && answer.ToUpperInvariant() != "Y") { Console.WriteLine("CANCELLED BY USER."); return; } } // Encryption uses 2 keys: // 1. First bytes in file, XORed with input file name without extension in upper case // 2. Fixed byte array. string fileWithoutExtension = Path.GetFileNameWithoutExtension(opts.InputFile).ToUpperInvariant(); using DataStream input = DataStreamFactory.FromFile(opts.InputFile, FileOpenMode.Read); using DataStream output = DataStreamFactory.FromMemory(); Console.Write($"Decrypting '{opts.InputFile}'..."); Decrypt(input, output, fileWithoutExtension); output.WriteTo(opts.OutputFile); Console.WriteLine(" DONE!"); }
public void CanWriteEmptyFiles() { byte[] expected = { 0xF9, 0x7D, 0x07, 0x00, 0x01, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; var expectedStream = DataStreamFactory.FromArray(expected, 0, expected.Length); var writer = new StandardBinWriter(); var parameters = new WriterParameters { BlockSize = 0x20 }; writer.Initialize(parameters); var container = new NodeContainerFormat(); var node = NodeFactory.FromMemory("empty"); node.Tags["Type"] = FileType.Empty; node.Tags["Index"] = 1; container.Root.Add(node); var binaryFormat = writer.Convert(container); Assert.AreEqual(expected.Length, binaryFormat.Stream.Length); Assert.IsTrue(binaryFormat.Stream.Compare(expectedStream)); }
public void CreateFromSectionPathReadsFile() { string tempFile = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); Assert.That(File.Exists(tempFile), Is.False); int beforeCount = DataStream.ActiveStreams; FileStream writeStream = null; DataStream readStream = null; try { writeStream = new FileStream(tempFile, FileMode.CreateNew); writeStream.WriteByte(0xCA); writeStream.WriteByte(0xFE); writeStream.WriteByte(0xAA); writeStream.Dispose(); writeStream = null; // prevent two disposes readStream = DataStreamFactory.FromFile(tempFile, FileOpenMode.Read, 1, 2); Assert.IsNull(readStream.ParentDataStream); Assert.AreEqual(0x1, readStream.Offset); Assert.AreEqual(0x2, readStream.Length); Assert.AreEqual(0x0, readStream.Position); Assert.AreEqual(0xFE, readStream.ReadByte()); } finally { writeStream?.Dispose(); readStream?.Dispose(); File.Delete(tempFile); } Assert.AreEqual(beforeCount, DataStream.ActiveStreams); }
public void CreateFromSectionPathDoesNotAllowToExpand() { string tempFile = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); int beforeCount = DataStream.ActiveStreams; FileStream createStream = null; DataStream writeStream = null; try { createStream = new FileStream(tempFile, FileMode.CreateNew); createStream.WriteByte(0xCA); createStream.WriteByte(0xFE); createStream.WriteByte(0xAA); createStream.Dispose(); createStream = null; // prevent two disposes writeStream = DataStreamFactory.FromFile(tempFile, FileOpenMode.Write, 1, 2); writeStream.Position = 2; Assert.That(() => writeStream.WriteByte(0xB4), Throws.InvalidOperationException); } finally { createStream?.Dispose(); writeStream?.Dispose(); File.Delete(tempFile); } Assert.AreEqual(beforeCount, DataStream.ActiveStreams); }
public void CreateFromPathWritesFile() { string tempFile = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); Assert.That(File.Exists(tempFile), Is.False); int beforeCount = DataStream.ActiveStreams; DataStream writeStream = null; FileStream readStream = null; try { writeStream = DataStreamFactory.FromFile(tempFile, FileOpenMode.Write); Assert.AreEqual(0x0, writeStream.Offset); Assert.AreEqual(0x0, writeStream.Length); Assert.AreEqual(0x0, writeStream.Position); writeStream.WriteByte(0xCA); writeStream.Dispose(); writeStream = null; // prevent two dispose Assert.That(File.Exists(tempFile), Is.True); readStream = new FileStream(tempFile, FileMode.Open); Assert.AreEqual(0x1, readStream.Length); Assert.AreEqual(0xCA, readStream.ReadByte()); } finally { writeStream?.Dispose(); readStream?.Dispose(); File.Delete(tempFile); } Assert.AreEqual(beforeCount, DataStream.ActiveStreams); }
public void CanReadDummyFiles() { var reader = new StandardBinReader(); var parameters = new ReaderParameters { Endianness = EndiannessMode.BigEndian, FileNames = Array.Empty <string>(), }; reader.Initialize(parameters); byte[] data = { 0x00, 0x07, 0x7D, 0xF9, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x70, 0x83, 0x5F, 0x83, 0x7E, 0x83, 0x5F, 0x83, 0x7E, 0x81, 0x5B, 0x82, 0xC5, 0x82, 0xB7, 0x2E, 0x0D, 0x0A, 0x90, 0xB3, 0x8E, 0xAE, 0x82, 0xC8, 0x83, 0x66, 0x81, 0x5B, 0x83, 0x5E, 0x82, 0xAA, 0x93, 0xFC, 0x82, 0xE9, 0x82, 0xDC, 0x82, 0xC5, 0x81, 0x41, 0x82, 0xD0, 0x82, 0xC6, 0x82, 0xDC, 0x82, 0xB8, 0x83, 0x8A, 0x83, 0x93, 0x83, 0x4E, 0x83, 0x66, 0x81, 0x5B, 0x83, 0x5E, 0x82, 0xF0, 0x8D, 0xEC, 0x90, 0xAC, 0x82, 0xB7, 0x82, 0xE9, 0x82, 0xBD, 0x82, 0xDF, 0x82, 0xCC, 0x83, 0x5F, 0x83, 0x7E, 0x81, 0x5B, 0x83, 0x74, 0x83, 0x40, 0x83, 0x43, 0x83, 0x8B, 0x82, 0xC6, 0x82, 0xB5, 0x82, 0xC4, 0x8D, 0xEC, 0x90, 0xAC, 0x82, 0xB3, 0x82, 0xEA, 0x82, 0xC4, 0x82, 0xA2, 0x82, 0xDC, 0x82, 0xB7, 0x2E, 0x0D, 0x0A, }; using DataStream stream = DataStreamFactory.FromArray(data, 0, data.Length); var format = new BinaryFormat(stream); var result = reader.Convert(format); Assert.AreEqual(1, result.Root.Children.Count); Assert.AreEqual(FileType.Dummy, result.Root.Children[0].Tags["Type"]); }
private static DataStream CompressV2(DataStream inputDataStream) { var input = new byte[inputDataStream.Length]; inputDataStream.Read(input, 0, input.Length); DataStream outputDataStream = DataStreamFactory.FromMemory(); var writer = new DataWriter(outputDataStream) { Endianness = EndiannessMode.BigEndian, }; int currentPosition = 0; while (currentPosition < input.Length) { int decompressedChunkSize = Math.Min(input.Length - currentPosition, 0x10000); var decompressedData = new byte[decompressedChunkSize]; Array.Copy(input, currentPosition, decompressedData, 0, decompressedChunkSize); byte[] compressedData = ZlibCompress(decompressedData); int compressedDataLength = compressedData.Length + 5; writer.Write((byte)(compressedDataLength >> 16)); writer.Write((byte)(compressedDataLength >> 8)); writer.Write((byte)compressedDataLength); writer.Write((ushort)(decompressedChunkSize - 1)); writer.Write(compressedData); writer.WriteTimes(0, 5); currentPosition += decompressedChunkSize; } return(outputDataStream); }
public void CreateFromMemoryAllowToExpand() { var stream = DataStreamFactory.FromMemory(); Assert.That(() => stream.WriteByte(0xFE), Throws.Nothing); stream.Dispose(); }
public BinaryFormat Convert(BinaryFormat source) { if (source == null) { throw new ArgumentNullException(nameof(source)); } // Read the header DataReader reader = new DataReader(source.Stream) { Endianness = EndiannessMode.BigEndian }; uint format = reader.ReadUInt32(); uint decompressedSize = reader.ReadUInt32(); // decompressed size reader.ReadUInt32(); // reserved reader.ReadUInt32(); // reserved if (format != 2) { throw new FormatException("Unknown format " + format); } byte[] data = new byte[source.Stream.Length - HeaderSize]; source.Stream.Read(data, 0, data.Length); // Round 0 only applies to the latest 0x800 bytes int round0Size = (data.Length > 0x800) ? 0x800 : data.Length; int round0Pos = data.Length - round0Size; Round0(0x6d73, 0, 0x100, data, round0Pos); Compare(data, 0); Round1(data); Compare(data, 1); // Last Int32 in big endian is used as part of the multiplier // in the generation of random numbers. // The other are the expected checksums. int multiplierInit = GetInt32BE(data, data.Length - 0x4); uint checksum1 = (uint)GetInt32BE(data, data.Length - 0x8); uint checksum2 = (uint)GetInt32BE(data, data.Length - 0xC); int newSize = Round2(data, multiplierInit); Array.Resize(ref data, newSize); Compare(data, 2); Validate(data, checksum1, checksum2); Round0(0x728F, multiplierInit, 0x80, data, 0); Compare(data, 3); data = Round4(data); Compare(data, 4); return(new BinaryFormat(DataStreamFactory.FromArray(data, 0, data.Length))); }
public void CreateFromMemoryUseMemoryStream() { var dataStream = DataStreamFactory.FromMemory(); Assert.That(dataStream.BaseStream, Is.AssignableFrom <RecyclableMemoryStream>()); dataStream.Dispose(); }
public static BinaryFormat Lzss(BinaryFormat bf, string mode) { string tempFile = Path.GetTempFileName(); if (mode == "-d") { using (var substream = new DataStream(bf.Stream, 4, bf.Stream.Length - 4)) { substream.WriteTo(tempFile); } } else { using (var substream = new DataStream(bf.Stream, 0, bf.Stream.Length)) { substream.WriteTo(tempFile); } } string program = System.IO.Path.GetFullPath(@"..\..\") + @"\lib\NDS_Compressors_CUE\lzss.exe"; string arguments = mode + " " + tempFile; if (Environment.OSVersion.Platform != PlatformID.Win32NT) { program = System.IO.Path.GetFullPath(@"../../") + "/lib/NDS_Compressors_CUE/lzss"; } Process process = new Process(); process.StartInfo.FileName = program; process.StartInfo.Arguments = arguments; process.StartInfo.UseShellExecute = false; process.StartInfo.CreateNoWindow = true; process.StartInfo.ErrorDialog = false; process.StartInfo.RedirectStandardOutput = true; process.Start(); process.WaitForExit(); DataStream fileStream = DataStreamFactory.FromFile(tempFile, FileOpenMode.Read); DataStream memoryStream = new DataStream(); if (mode != "-d") { memoryStream.Seek(0); memoryStream.Write(Encoding.ASCII.GetBytes("DSCP"), 0, 4); } fileStream.WriteTo(memoryStream); fileStream.Dispose(); File.Delete(tempFile); return(new BinaryFormat(memoryStream)); }
/// <summary> /// Decompresses an ARC container. /// </summary> /// <param name="source">Source format.</param> /// <returns>The uncompressed format.</returns> public BinaryFormat Convert(BinaryFormat source) { if (source == null) { throw new ArgumentNullException(nameof(source)); } source.Stream.Position = 0; Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); var reader = new DataReader(source.Stream) { DefaultEncoding = Encoding.GetEncoding(1252), Endianness = EndiannessMode.LittleEndian, }; // Read the file header ArcHeader header = reader.Read <ArcHeader>() as ArcHeader; this.CheckHeader(header); switch (header.CompressionType) { case 0x00: // Not compressed return(source); case 0x02: // LZ4 Compression { DataStream outputStream = DataStreamFactory.FromMemory(); var writer = new DataWriter(outputStream) { DefaultEncoding = Encoding.GetEncoding(1252), Endianness = EndiannessMode.LittleEndian, }; header.CompressionType = 0x00; writer.WriteOfType(header); byte[] filesInfo = reader.ReadBytes(0x70 * header.FileCount); byte[] compressedData = reader.ReadBytes((int)(source.Stream.Length - source.Stream.Position)); var decompressedData = new byte[header.OriginalSize]; int bytesWritten = LZ4Codec.Decode(compressedData, decompressedData); if (bytesWritten != header.OriginalSize) { throw new FormatException($"ARC: Bad LZ4 compression."); } writer.Write(filesInfo); writer.Write(decompressedData); return(new BinaryFormat(outputStream)); } default: throw new FormatException($"ARC: Unknown compression {header.CompressionType:X4}"); } }
public void CreateFromArrayWritesToArray() { byte[] data = new byte[] { 0x01, 0x2, 0x3 }; var stream = DataStreamFactory.FromArray(data, 1, 2); stream.WriteByte(0xFE); Assert.That(data[1], Is.EqualTo(0xFE)); stream.Dispose(); }
public void CreateFromArrayDoesNotAllowToExpand() { byte[] data = new byte[] { 0x01, 0x2, 0x3 }; var stream = DataStreamFactory.FromArray(data, 1, 2); stream.Position = 2; Assert.That(() => stream.WriteByte(0xFE), Throws.InvalidOperationException); stream.Dispose(); }
public void CreateFromPathWithInvalidArgThrows() { Assert.That( () => DataStreamFactory.FromFile(null, FileOpenMode.Append), Throws.ArgumentNullException); Assert.That( () => DataStreamFactory.FromFile(string.Empty, FileOpenMode.Append), Throws.ArgumentNullException); }
public void CreateFromStreamAllowsToExpand() { using var stream = new MemoryStream(); var dataStream = DataStreamFactory.FromStream(stream); Assert.That(() => stream.WriteByte(0xFE), Throws.Nothing); Assert.That(stream.Length, Is.EqualTo(1)); dataStream.Dispose(); }
public void BadMagicThrowsException() { var reader = new DlcBinReader(); byte[] data = { 0x64, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; using DataStream stream = DataStreamFactory.FromArray(data, 0, data.Length); var format = new BinaryFormat(stream); Assert.Throws <FormatException>(() => reader.Convert(format)); }
public void IncompleteHeaderThrowsException() { var reader = new StandardBinReader(); byte[] data = { 0xF9, 0x7D, 0x07, 0x00 }; using DataStream stream = DataStreamFactory.FromArray(data, 0, data.Length); var format = new BinaryFormat(stream); Assert.Throws <FormatException>(() => reader.Convert(format)); }
public void CompressFilesBigEndian() { var compressor = new StandardBinCompressor(); compressor.Initialize(EndiannessMode.BigEndian); byte[] data = { 0x4C, 0x6F, 0x72, 0x65, 0x6D, 0x20, 0x69, 0x70, 0x73, 0x75, 0x6D, 0x20, 0x64, 0x6F, 0x6C, 0x6F, 0x72, 0x20, 0x73, 0x69, 0x74, 0x20, 0x61, 0x6D, 0x65, 0x74, 0x2C, 0x20, 0x63, 0x6F, 0x6E, 0x73, 0x65, 0x63, 0x74, 0x65, 0x74, 0x75, 0x72, 0x20, 0x61, 0x64, 0x69, 0x70, 0x69, 0x73, 0x63, 0x69, 0x6E, 0x67, 0x20, 0x65, 0x6C, 0x69, 0x74, 0x2E, 0x20, 0x4D, 0x6F, 0x72, 0x62, 0x69, 0x20, 0x70, 0x68, 0x61, 0x72, 0x65, 0x74, 0x72, 0x61, 0x20, 0x75, 0x74, 0x20, 0x76, 0x65, 0x6C, 0x69, 0x74, 0x20, 0x65, 0x67, 0x65, 0x74, 0x20, 0x70, 0x6F, 0x72, 0x74, 0x61, 0x2E, 0x20, 0x55, 0x74, 0x20, 0x75, 0x6C, 0x6C, 0x61, 0x6D, 0x63, 0x6F, 0x72, 0x70, 0x65, 0x72, 0x2C, 0x20, 0x6E, 0x69, 0x62, 0x68, 0x20, 0x61, 0x20, 0x69, 0x6D, 0x70, 0x65, 0x72, 0x64, 0x69, 0x65, 0x74, 0x20, 0x69, 0x61, 0x63, 0x75, 0x6C, 0x69, 0x73, 0x2C, 0x20, 0x6D, 0x61, 0x67, 0x6E, 0x61, 0x20, 0x72, 0x69, 0x73, 0x75, 0x73, 0x20, 0x63, 0x75, 0x72, 0x73, 0x75, 0x73, 0x20, 0x6E, 0x65, 0x71, 0x75, 0x65, 0x2C, 0x20, 0x65, 0x74, 0x20, 0x73, 0x61, 0x67, 0x69, 0x74, 0x74, 0x69, 0x73, 0x20, 0x6D, 0x61, 0x73, 0x73, 0x61, 0x20, 0x73, 0x61, 0x70, 0x69, 0x65, 0x6E, 0x20, 0x61, 0x63, 0x20, 0x6E, 0x69, 0x73, 0x69, 0x2E, 0x20, 0x4D, 0x6F, 0x72, 0x62, 0x69, 0x20, 0x73, 0x69, 0x74, 0x20, 0x61, 0x6D, 0x65, 0x74, 0x20, 0x65, 0x73, 0x74, 0x20, 0x73, 0x61, 0x67, 0x69, 0x74, 0x74, 0x69, 0x73, 0x2C, 0x20, 0x70, 0x6F, 0x72, 0x74, 0x61, 0x20, 0x74, 0x6F, 0x72, 0x74, 0x6F, 0x72, 0x20, 0x73, 0x65, 0x64, 0x2C, 0x20, 0x73, 0x6F, 0x6C, 0x6C, 0x69, 0x63, 0x69, 0x74, 0x75, 0x64, 0x69, 0x6E, 0x20, 0x66, 0x65, 0x6C, 0x69, 0x73, 0x2E, 0x20, 0x4E, 0x61, 0x6D, 0x20, 0x76, 0x65, 0x73, 0x74, 0x69, 0x62, 0x75, 0x6C, 0x75, 0x6D, 0x20, 0x6C, 0x69, 0x67, 0x75, 0x6C, 0x61, 0x20, 0x61, 0x74, 0x20, 0x73, 0x65, 0x6D, 0x70, 0x65, 0x72, 0x20, 0x76, 0x69, 0x76, 0x65, 0x72, 0x72, 0x61, 0x2E, 0x20, 0x50, 0x68, 0x61, 0x73, 0x65, 0x6C, 0x6C, 0x75, 0x73, 0x20, 0x6E, 0x65, 0x63, 0x20, 0x6C, 0x61, 0x6F, 0x72, 0x65, 0x65, 0x74, 0x20, 0x70, 0x75, 0x72, 0x75, 0x73, 0x2E, 0x20, 0x4D, 0x61, 0x65, 0x63, 0x65, 0x6E, 0x61, 0x73, 0x20, 0x76, 0x65, 0x6C, 0x20, 0x70, 0x75, 0x72, 0x75, 0x73, 0x20, 0x69, 0x6E, 0x74, 0x65, 0x72, 0x64, 0x75, 0x6D, 0x2C, 0x20, 0x62, 0x6C, 0x61, 0x6E, 0x64, 0x69, 0x74, 0x20, 0x6C, 0x69, 0x62, 0x65, 0x72, 0x6F, 0x20, 0x69, 0x6E, 0x2C, 0x20, 0x67, 0x72, 0x61, 0x76, 0x69, 0x64, 0x61, 0x20, 0x61, 0x75, 0x67, 0x75, 0x65, 0x2E, 0x20, 0x50, 0x72, 0x6F, 0x69, 0x6E, 0x20, 0x65, 0x6C, 0x65, 0x6D, 0x65, 0x6E, 0x74, 0x75, 0x6D, 0x20, 0x64, 0x75, 0x69, 0x20, 0x69, 0x64, 0x20, 0x6D, 0x61, 0x67, 0x6E, 0x61, 0x20, 0x74, 0x72, 0x69, 0x73, 0x74, 0x69, 0x71, 0x75, 0x65, 0x20, 0x73, 0x61, 0x67, 0x69, 0x74, 0x74, 0x69, 0x73, 0x2E, 0x20, 0x4D, 0x61, 0x75, 0x72, 0x69, 0x73, 0x20, 0x6D, 0x61, 0x78, 0x69, 0x6D, 0x75, 0x73, 0x20, 0x74, 0x72, 0x69, 0x73, 0x74, 0x69, 0x71, 0x75, 0x65, 0x20, 0x73, 0x65, 0x6D, 0x2C, 0x20, 0x61, 0x63, 0x20, 0x70, 0x6C, 0x61, 0x63, 0x65, 0x72, 0x61, 0x74, 0x20, 0x6C, 0x65, 0x6F, 0x20, 0x74, 0x65, 0x6D, 0x70, 0x75, 0x73, 0x20, 0x61, 0x2E, 0x20, 0x41, 0x6C, 0x69, 0x71, 0x75, 0x61, 0x6D, 0x20, 0x6C, 0x65, 0x6F, 0x2E, }; using DataStream stream = DataStreamFactory.FromArray(data, 0, data.Length); using Node n = NodeFactory.FromSubstream("compress", stream, 0, stream.Length); n.Tags["Type"] = FileType.Compressed; var container = NodeFactory.CreateContainer("container"); container.Add(n); var result = compressor.Convert(container.GetFormatAs <NodeContainerFormat>()); Assert.AreEqual(1, result.Root.Children.Count); Assert.AreEqual(0x13D, result.Root.Children[0].Stream.Length); }
public void InstanceWriter() { // Get the new header and calculate the absolute position. var tradHeader = PeFile.Sections.First(x => x.Name == ".trad"); // Initialize the writer. Writer = new DataWriter(DataStreamFactory.FromStream(GenerateStream(PeFile))) { Stream = { Position = (long)tradHeader.Offset } }; }
private static DataStream Compress(DataStream inputDataStream, CompressorParameters parameters) { DataStream outputDataStream = DataStreamFactory.FromMemory(); var writer = new DataWriter(outputDataStream) { DefaultEncoding = Encoding.ASCII, }; if (parameters == null) { parameters = new CompressorParameters { Version = 0x01, Endianness = 0x00, }; } writer.Endianness = parameters.Endianness == 0 ? EndiannessMode.LittleEndian : EndiannessMode.BigEndian; writer.Write("SLLZ", false); writer.Write(parameters.Endianness); writer.Write(parameters.Version); writer.Write((ushort)0x10); // Header size writer.Write((int)inputDataStream.Length); writer.Stream.PushCurrentPosition(); writer.Write(0x00000000); // Compressed size DataStream compressedDataStream; if (parameters.Version == 1) { compressedDataStream = CompressV1(inputDataStream); } else if (parameters.Version == 2) { if (inputDataStream.Length < 0x1B) { throw new FormatException($"SLLZv2: Input size must more than 0x1A."); } compressedDataStream = CompressV2(inputDataStream); } else { throw new FormatException($"SLLZ: Unknown compression version {parameters.Version}."); } compressedDataStream.WriteTo(outputDataStream); writer.Stream.PopPosition(); writer.Write((int)(compressedDataStream.Length + 0x10)); // data + header compressedDataStream.Dispose(); return(outputDataStream); }
public void CreateFromSubStreamDoesNotAllowToExpand() { using var stream = new MemoryStream(); stream.WriteByte(0xCA); stream.WriteByte(0xFE); stream.WriteByte(0xBE); var dataStream = DataStreamFactory.FromStream(stream, 1, 2); dataStream.Position = 2; Assert.That(() => dataStream.WriteByte(0xAA), Throws.InvalidOperationException); }