C#: added an untested compressed algorithm for RLE. (all new decompression algorithms are also untested)

This commit is contained in:
barubary 2011-04-04 13:07:07 +00:00
parent da75d115e0
commit edc3d2da24
7 changed files with 132 additions and 12 deletions

View File

@ -43,6 +43,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Exceptions\InputTooLargeException.cs" />
<Compile Include="Exceptions\StreamTooShortException.cs" />
<Compile Include="Formats\CompressionFormat.cs" />
<Compile Include="Formats\Nitro\Huffman.cs" />

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace DSDecmp
{
public class InputTooLargeException : Exception
{
public InputTooLargeException()
: base("The compression ratio is not high enough to fit the input "
+ "in a single compressed file.") { }
}
}

View File

@ -80,13 +80,14 @@ namespace DSDecmp.Formats
/// </summary>
/// <param name="infile">The file to compress.</param>
/// <param name="outfile">The file to write the compressed data to.</param>
public void Compress(string infile, string outfile)
/// <returns>The size of the compressed file.</returns>
public int Compress(string infile, string outfile)
{
// open the proper Streams, and delegate to the format-specific code.
using (FileStream inStream = File.Open(infile, FileMode.Open),
outStream = File.Create(outfile))
{
this.Compress(inStream, inStream.Length, outStream);
return this.Compress(inStream, inStream.Length, outStream);
}
}
@ -97,6 +98,7 @@ namespace DSDecmp.Formats
/// <param name="instream">The stream to read plaintext data from.</param>
/// <param name="inLength">The length of the plaintext data.</param>
/// <param name="outstream">The stream to write the compressed data to.</param>
public abstract void Compress(Stream instream, long inLength, Stream outstream);
/// <returns>The size of the compressed stream.</returns>
public abstract int Compress(Stream instream, long inLength, Stream outstream);
}
}

View File

@ -156,7 +156,7 @@ namespace DSDecmp.Formats.Nitro
}
}
public override void Compress(Stream instream, long inLength, Stream outstream)
public override int Compress(Stream instream, long inLength, Stream outstream)
{
throw new NotImplementedException();
}
@ -174,10 +174,6 @@ namespace DSDecmp.Formats.Nitro
/// </summary>
public byte Data { get { return this.data; } }
/// <summary>
/// A flag indicating if this ia a 'child1' of another node.
/// </summary>
private bool isOne;
/// <summary>
/// A flag indicating if this node contains data. If not, this is not a leaf node.
/// </summary>
private bool isData;

View File

@ -142,7 +142,7 @@ namespace DSDecmp.Formats.Nitro
}
public override void Compress(Stream instream, long inLength, Stream outstream)
public override int Compress(Stream instream, long inLength, Stream outstream)
{
throw new NotImplementedException();
}

View File

@ -214,7 +214,7 @@ namespace DSDecmp.Formats.Nitro
}
}
public override void Compress(Stream instream, long inLength, Stream outstream)
public override int Compress(Stream instream, long inLength, Stream outstream)
{
throw new NotImplementedException();
}

View File

@ -119,9 +119,117 @@ namespace DSDecmp.Formats.Nitro
}
}
public override void Compress(Stream instream, long inLength, Stream outstream)
public override int Compress(Stream instream, long inLength, Stream outstream)
{
throw new NotImplementedException();
List<byte> compressedData = new List<byte>();
// at most 0x7F+3=130 bytes are compressed into a single block.
// (and at most 0x7F+1=128 in an uncompressed block, however we need to read 2
// more, since the last byte may be part of a repetition).
byte[] dataBlock = new byte[130];
// the length of the valid content in the current data block
int currentBlockLength = 0;
int readLength = 0;
int nextByte;
int repCount = 1;
while (readLength < inLength)
{
bool foundRepetition = false;
while (currentBlockLength < dataBlock.Length)
{
nextByte = instream.ReadByte();
if (nextByte < 0)
throw new StreamTooShortException();
dataBlock[currentBlockLength++] = (byte)nextByte;
if (currentBlockLength > 1)
{
if (nextByte == dataBlock[currentBlockLength - 2])
repCount++;
else
repCount = 1;
}
foundRepetition = repCount > 2;
if (foundRepetition)
break;
}
int numUncompToCopy = 0;
if (foundRepetition)
{
// if a repetition was found, copy block size - 3 bytes as compressed data
numUncompToCopy = currentBlockLength - 3;
}
else
{
// if no repetition was found, copy min(block size, max block size - 2) bytes as uncompressed data.
numUncompToCopy = Math.Min(currentBlockLength, dataBlock.Length - 2);
}
#region insert uncompressed block
if (numUncompToCopy > 0)
{
byte flag = (byte)(numUncompToCopy - 1);
compressedData.Add(flag);
for (int i = 0; i < numUncompToCopy; i++)
compressedData.Add(dataBlock[i]);
// shift some possibly remaining bytes to the start
for (int i = numUncompToCopy; i < currentBlockLength; i++)
dataBlock[i - numUncompToCopy] = dataBlock[i];
currentBlockLength -= numUncompToCopy;
}
#endregion
if (foundRepetition)
{
// if a repetition was found, continue until the first different byte
// (or until the buffer is full)
while (currentBlockLength < dataBlock.Length)
{
nextByte = instream.ReadByte();
if (nextByte < 0)
throw new StreamTooShortException();
dataBlock[currentBlockLength++] = (byte)nextByte;
if (nextByte != dataBlock[0])
break;
else
repCount++;
}
// the next repCount bytes are the same.
#region insert compressed block
byte flag = (byte)(0x80 | (repCount - 3));
compressedData.Add(flag);
compressedData.Add(dataBlock[0]);
// make sure to shift the possible extra byte to the start
if (repCount != currentBlockLength)
dataBlock[0] = dataBlock[currentBlockLength - 1];
currentBlockLength -= repCount;
#endregion
}
}
if (compressedData.Count > 0xFFFFFF)
throw new InputTooLargeException();
// write the RLE marker and the decompressed size
outstream.WriteByte(0x30);
int compLen = compressedData.Count;
outstream.WriteByte((byte)(compLen & 0xFF));
outstream.WriteByte((byte)((compLen >> 8) & 0xFF));
outstream.WriteByte((byte)((compLen >> 16) & 0xFF));
// write the compressed data
outstream.Write(compressedData.ToArray(), 0, compLen);
// the total compressed stream length is the compressed data length + the 4-byte header
return compLen + 4;
}
}
}