From 4ddd87206bacf4ce7d803b40ff3bd2663327b083 Mon Sep 17 00:00:00 2001 From: barubary Date: Thu, 22 Dec 2011 22:59:37 +0000 Subject: [PATCH] C#: the composite formats that were added in r48 should now be supported by the program. Not yet tested though. Added support for the 'NULL' compression format; a built-in Nitro format that only contains plaintext (but includes the nitro compression header with magic byte 0) --- CSharp/DSDecmp/DSDecmp.csproj | 1 + CSharp/DSDecmp/Formats/Nitro/NitroCFormat.cs | 2 +- .../DSDecmp/Formats/Nitro/NullCompression.cs | 158 ++++++++++++++++++ CSharp/DSDecmp/NewestProgram.cs | 9 +- CSharp/PluginDistro/DSDecmp.xml | 48 +++++- CSharp/PluginDistro/GoldenSunDD.dll | Bin 6656 -> 6656 bytes CSharp/PluginDistro/LuminousArc.dll | Bin 8704 -> 8704 bytes 7 files changed, 213 insertions(+), 5 deletions(-) create mode 100644 CSharp/DSDecmp/Formats/Nitro/NullCompression.cs diff --git a/CSharp/DSDecmp/DSDecmp.csproj b/CSharp/DSDecmp/DSDecmp.csproj index afdfb04..778bfcf 100644 --- a/CSharp/DSDecmp/DSDecmp.csproj +++ b/CSharp/DSDecmp/DSDecmp.csproj @@ -56,6 +56,7 @@ + diff --git a/CSharp/DSDecmp/Formats/Nitro/NitroCFormat.cs b/CSharp/DSDecmp/Formats/Nitro/NitroCFormat.cs index 5621cfc..e00e7d1 100644 --- a/CSharp/DSDecmp/Formats/Nitro/NitroCFormat.cs +++ b/CSharp/DSDecmp/Formats/Nitro/NitroCFormat.cs @@ -39,7 +39,7 @@ namespace DSDecmp.Formats.Nitro } /// - /// Checks if the first four bytes match the format used in nitro compression formats. + /// Checks if the first four (or eight) bytes match the format used in nitro compression formats. /// public override bool Supports(System.IO.Stream stream, long inLength) { diff --git a/CSharp/DSDecmp/Formats/Nitro/NullCompression.cs b/CSharp/DSDecmp/Formats/Nitro/NullCompression.cs new file mode 100644 index 0000000..0fc966d --- /dev/null +++ b/CSharp/DSDecmp/Formats/Nitro/NullCompression.cs @@ -0,0 +1,158 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.IO; + +namespace DSDecmp.Formats.Nitro +{ + /// + /// 'Compression' format without any compression whatsoever. + /// Compression using this format will only prepend 0x00 plus the original file size to the file. + /// + public class NullCompression : NitroCFormat + { + /// + /// Gets a short string identifying this compression format. + /// + public override string ShortFormatString + { + get { return "NULL"; } + } + + /// + /// Gets a short description of this compression format (used in the program usage). + /// + public override string Description + { + get { return "NULL-'compression' format. Prefixes file with 0x00 and filesize."; } + } + + /// + /// Gets if this format supports compressing a file. + /// + public override bool SupportsCompression + { + get { return true; } + } + + /// + /// Gets the value that must be given on the command line in order to compress using this format. + /// + public override string CompressionFlag + { + get { return "null"; } + } + + /// + /// Creates a new instance of the NULL-compression format. + /// + public NullCompression() + : base(0) { } + + /// + /// Checks if the given stream is (or could be) 'compressed' using the NULL compression format. + /// + public override bool Supports(System.IO.Stream stream, long inLength) + { + long startPosition = stream.Position; + try + { + int firstByte = stream.ReadByte(); + if (firstByte != 0) + return false; + byte[] sizeBytes = new byte[3]; + stream.Read(sizeBytes, 0, 3); + int outSize = IOUtils.ToNDSu24(sizeBytes, 0); + int headerSize = 4; + if (outSize == 0) + { + sizeBytes = new byte[4]; + stream.Read(sizeBytes, 0, 4); + outSize = (int)IOUtils.ToNDSu32(sizeBytes, 0); + headerSize = 8; + } + return outSize == inLength - headerSize; + } + finally + { + stream.Position = startPosition; + } + } + + /// + /// 'Decompresses' the given input stream using the NULL format. + /// + public override long Decompress(System.IO.Stream instream, long inLength, System.IO.Stream outstream) + { + long readBytes = 0; + + byte type = (byte)instream.ReadByte(); + if (type != base.magicByte) + throw new InvalidDataException("The provided stream is not a valid Null " + + "compressed stream (invalid type 0x" + type.ToString("X") + ")"); + byte[] sizeBytes = new byte[3]; + instream.Read(sizeBytes, 0, 3); + int decompressedSize = IOUtils.ToNDSu24(sizeBytes, 0); + readBytes += 4; + if (decompressedSize == 0) + { + sizeBytes = new byte[4]; + instream.Read(sizeBytes, 0, 4); + decompressedSize = IOUtils.ToNDSs32(sizeBytes, 0); + readBytes += 4; + } + + byte[] data = new byte[decompressedSize]; + int readLength = instream.Read(data, 0, decompressedSize); + outstream.Write(data, 0, readLength); + if (readLength < decompressedSize) + throw new NotEnoughDataException(readLength, decompressedSize); + + return readLength; + } + + /// + /// 'Compresses' the given input stream using the NULL format. + /// + public override int Compress(System.IO.Stream instream, long inLength, System.IO.Stream outstream) + { + if (inLength > 0xFFFFFFFF) + throw new InputTooLargeException(); + + long outSize = 4; + + outstream.WriteByte(0); + if (inLength <= 0xFFFFFF) + { + outstream.WriteByte((byte)(inLength & 0xFF)); + outstream.WriteByte((byte)((inLength >> 8) & 0xFF)); + outstream.WriteByte((byte)((inLength >> 16) & 0xFF)); + } + else + { + outstream.WriteByte(0); + outstream.WriteByte(0); + outstream.WriteByte(0); + outstream.WriteByte((byte)(inLength & 0xFF)); + outstream.WriteByte((byte)((inLength >> 8) & 0xFF)); + outstream.WriteByte((byte)((inLength >> 16) & 0xFF)); + outstream.WriteByte((byte)((inLength >> 24) & 0xFF)); + outSize = 8; + } + + byte[] buffer = new byte[Math.Min(int.MaxValue, inLength)]; + long remaining = inLength; + while (remaining > 0) + { + int readLength = instream.Read(buffer, 0, (int)Math.Min(buffer.Length, remaining)); + if (readLength == 0) + throw new StreamTooShortException(); + remaining -= readLength; + outstream.Write(buffer, 0, readLength); + outSize += readLength; + } + + return (int)Math.Min(int.MaxValue, outSize); + } + } +} diff --git a/CSharp/DSDecmp/NewestProgram.cs b/CSharp/DSDecmp/NewestProgram.cs index ffe20eb..8010cd2 100644 --- a/CSharp/DSDecmp/NewestProgram.cs +++ b/CSharp/DSDecmp/NewestProgram.cs @@ -8,7 +8,7 @@ using DSDecmp.Formats.Nitro; namespace DSDecmp { - class NewestProgram + public class NewestProgram { #if DEBUG public static string PluginFolder = "./Plugins/Debug"; @@ -177,7 +177,7 @@ namespace DSDecmp using (MemoryStream inStr = new MemoryStream(inputData), outStr = new MemoryStream()) { - if (!format.Supports(inStr, input.Length)) + if (!format.Supports(inStr, inputData.Length)) continue; try { @@ -549,9 +549,12 @@ namespace DSDecmp yield return new Huffman4(); yield return new Huffman8(); yield return new RLE(); + yield return new NullCompression(); if (alsoCompositeFormats) { - + yield return new HuffmanAny(); + yield return new CompositeGBAFormat(); + yield return new CompositeNDSFormat(); } } /// diff --git a/CSharp/PluginDistro/DSDecmp.xml b/CSharp/PluginDistro/DSDecmp.xml index 8a02d3f..67746ba 100644 --- a/CSharp/PluginDistro/DSDecmp.xml +++ b/CSharp/PluginDistro/DSDecmp.xml @@ -257,7 +257,7 @@ - Checks if the first four bytes match the format used in nitro compression formats. + Checks if the first four (or eight) bytes match the format used in nitro compression formats. @@ -1090,5 +1090,51 @@ Gets the value that must be given on the command line in order to compress using this format. + + + 'Compression' format without any compression whatsoever. + Compression using this format will only prepend 0x00 plus the original file size to the file. + + + + + Creates a new instance of the NULL-compression format. + + + + + Checks if the given stream is (or could be) 'compressed' using the NULL compression format. + + + + + 'Decompresses' the given input stream using the NULL format. + + + + + 'Compresses' the given input stream using the NULL format. + + + + + Gets a short string identifying this compression format. + + + + + Gets a short description of this compression format (used in the program usage). + + + + + Gets if this format supports compressing a file. + + + + + Gets the value that must be given on the command line in order to compress using this format. + + diff --git a/CSharp/PluginDistro/GoldenSunDD.dll b/CSharp/PluginDistro/GoldenSunDD.dll index 66dac93e82a537f72787f8162482092e9c5662a6..0b07164b823015df97791dd8892f7be2610d365f 100644 GIT binary patch delta 51 zcmZoLX)u}4!5qEm^TzIz`~u5oiAaB3+2b-Nee$)|UCQ2@Qv`e%L9&xK3u-dzZ@w$2 G%mDyYxD}27 delta 51 zcmZoLX)u}4!F>JJsg2zy`2{va?>q3w$=iGSY}Jb_Yb^^lrwI5kf@CLe7Sv?a+I&|~ GnF9cE5f)JZ diff --git a/CSharp/PluginDistro/LuminousArc.dll b/CSharp/PluginDistro/LuminousArc.dll index 3c6ea5dc2d444980ab369b7cf071a2bfc65a2b4c..8b9708635d111c2035dd8405f9b6a01cb7b54524 100644 GIT binary patch delta 51 zcmZp0X>gg)!5qEm^TuvZF@e+ZZ72Uet@oKKeWgcKdqL&qCt?9iAlb?0lA4T;n-eAH Ga{vHl7#0Em delta 51 zcmZp0X>gg)!7Tph)W&X4F#)N>cUNkrzVqEuw@7fu{Xg3_KM@OH0?AG`m(*mm*_