2011-03-21 20:18:07 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace DSDecmp.Formats.Nitro
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Base class for Nitro-based decompressors. Uses the 1-byte magic and 3-byte decompression
|
|
|
|
|
/// size format.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public abstract class NitroCFormat : CompressionFormat
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// If true, Nitro Decompressors will not decompress files that have a decompressed
|
|
|
|
|
/// size (plaintext size) larger than MaxPlaintextSize.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static bool SkipLargePlaintexts = true;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The maximum allowed size of the decompressed file (plaintext size) allowed for Nitro
|
|
|
|
|
/// Decompressors. Only used when SkipLargePlaintexts = true.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static int MaxPlaintextSize = 0x180000;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The first byte of every file compressed with the format for this particular
|
|
|
|
|
/// Nitro Dcompressor instance.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected byte magicByte;
|
|
|
|
|
|
2011-11-14 18:33:30 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a new instance of the Nitro Compression Format base class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="magicByte">The expected first byte of the file for this format.</param>
|
|
|
|
|
protected NitroCFormat(byte magicByte)
|
2011-03-21 20:18:07 +01:00
|
|
|
|
{
|
|
|
|
|
this.magicByte = magicByte;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-14 18:33:30 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Checks if the first four bytes match the format used in nitro compression formats.
|
|
|
|
|
/// </summary>
|
2011-04-05 18:50:17 +02:00
|
|
|
|
public override bool Supports(System.IO.Stream stream, long inLength)
|
2011-03-21 20:18:07 +01:00
|
|
|
|
{
|
|
|
|
|
long startPosition = stream.Position;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
int firstByte = stream.ReadByte();
|
|
|
|
|
if (firstByte != this.magicByte)
|
|
|
|
|
return false;
|
|
|
|
|
// no need to read the size info as well if it's used anyway.
|
|
|
|
|
if (!SkipLargePlaintexts)
|
|
|
|
|
return true;
|
|
|
|
|
byte[] sizeBytes = new byte[3];
|
|
|
|
|
stream.Read(sizeBytes, 0, 3);
|
2011-05-18 14:24:16 +02:00
|
|
|
|
int outSize = IOUtils.ToNDSu24(sizeBytes, 0);
|
2011-03-21 20:18:07 +01:00
|
|
|
|
if (outSize == 0)
|
|
|
|
|
{
|
|
|
|
|
sizeBytes = new byte[4];
|
|
|
|
|
stream.Read(sizeBytes, 0, 4);
|
2011-05-18 14:24:16 +02:00
|
|
|
|
outSize = (int)IOUtils.ToNDSu32(sizeBytes, 0);
|
2011-03-21 20:18:07 +01:00
|
|
|
|
}
|
|
|
|
|
return outSize <= MaxPlaintextSize;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
stream.Position = startPosition;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|