C#: Tested the LZ-10 compression algorithm, and adjusted it to work the same as the original implementation. Decompress->Compress of an original file should thus result in the same original file.

This commit is contained in:
barubary 2011-05-14 12:24:44 +00:00
parent 1c2722315a
commit 2c507a5765

View File

@ -110,7 +110,7 @@ namespace DSDecmp.Formats.Nitro
if (disp > currentOutSize) if (disp > currentOutSize)
throw new InvalidDataException("Cannot go back more than already written. " throw new InvalidDataException("Cannot go back more than already written. "
+ "DISP = " + disp + ", #written bytes = 0x" + currentOutSize.ToString("X") + "DISP = 0x" + disp.ToString("X") + ", #written bytes = 0x" + currentOutSize.ToString("X")
+ " at 0x" + instream.Position.ToString("X")); + " at 0x" + instream.Position.ToString("X"));
#endregion #endregion
@ -223,12 +223,12 @@ namespace DSDecmp.Formats.Nitro
{ {
outstream.Write(outbuffer, 0, bufferlength); outstream.Write(outbuffer, 0, bufferlength);
compressedLength += bufferlength; compressedLength += bufferlength;
// make the compressed file 4-byte aligned. /*/ make the compressed file 4-byte aligned.
while ((compressedLength % 4) != 0) while ((compressedLength % 4) != 0)
{ {
outstream.WriteByte(0); outstream.WriteByte(0);
compressedLength++; compressedLength++;
} }/**/
} }
} }
@ -251,13 +251,16 @@ namespace DSDecmp.Formats.Nitro
if (newLength == 0) if (newLength == 0)
return 0; return 0;
int maxLength = 0; int maxLength = 0;
for (int i = 1; i < oldLength; i++) //for (int i = 1; i < oldLength; i++)
for (int i = 0; i < oldLength - 1; i++)
{ {
// work from the end of the old data to the start, to mimic the original implementation's behaviour // work from the end of the old data to the start, to mimic the original implementation's behaviour
byte* currentOldStart = oldPtr + oldLength - i; //byte* currentOldStart = oldPtr + oldLength - i;
// WRONG: original works from start
byte* currentOldStart = oldPtr + i;
int currentLength = 0; int currentLength = 0;
// determine the length we can copy if we go back i bytes // determine the length we can copy if we go back i bytes
for (int j = 0; j < i && j < newLength; j++) for (int j = 0; j < newLength; j++)
{ {
// stop when the bytes are no longer the same // stop when the bytes are no longer the same
if (*(currentOldStart + j) != *(newPtr + j)) if (*(currentOldStart + j) != *(newPtr + j))
@ -268,7 +271,7 @@ namespace DSDecmp.Formats.Nitro
if (currentLength > maxLength) if (currentLength > maxLength)
{ {
maxLength = currentLength; maxLength = currentLength;
disp = i; disp = oldLength - i;
} }
} }
return maxLength; return maxLength;