Skip to content

Commit

Permalink
added forced checks to make sure the files generated are the correct …
Browse files Browse the repository at this point in the history
…one.

you can pass size 0 to skip size check. but if we know what the size should be we can confirm it is right.

#151
  • Loading branch information
Sebanisu committed Feb 25, 2020
1 parent 0bdbd5e commit 9f03935
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Core/Archive/ArchiveMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ byte[] open(int skip = 0)
case 1:
buffer = open(4);
offset = 0;
return new MemoryStream(LZSS.DecompressAllNew(buffer, false));
return new MemoryStream(LZSS.DecompressAllNew(buffer, @in.UncompressedSize, false));

case 2:
buffer = open();
Expand Down Expand Up @@ -182,7 +182,7 @@ public byte[] GetBinaryFile(string input, Stream data)
return buffer;

case 1:
return LZSS.DecompressAllNew(buffer);
return LZSS.DecompressAllNew(buffer, fi.UncompressedSize);

case 2:
return LZ4Uncompress(buffer, fi.UncompressedSize);
Expand Down
2 changes: 1 addition & 1 deletion Core/Archive/ArchiveWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ private byte[] GetBinaryFile(string fileName, int loc, bool cache)
temp = GetCompressedData(FI, out int size);

Memory.Log.WriteLine($"{nameof(ArchiveWorker)}::{nameof(GetBinaryFile)} :: extracting: {fileName}");
temp = temp == null ? null : FI.CompressionType == 2 ? ArchiveMap.LZ4Uncompress(temp, FI.UncompressedSize) : FI.CompressionType == 1 ? LZSS.DecompressAllNew(temp) : temp;
temp = temp == null ? null : FI.CompressionType == 2 ? ArchiveMap.LZ4Uncompress(temp, FI.UncompressedSize) : FI.CompressionType == 1 ? LZSS.DecompressAllNew(temp,FI.UncompressedSize) : temp;
if (temp != null && cache && LocalTryAdd(fileName, temp))
{
Memory.Log.WriteLine($"{nameof(ArchiveWorker)}::{nameof(GetBinaryFile)} :: cached: {fileName}");
Expand Down
4 changes: 3 additions & 1 deletion Core/Archive/Compression/LZSS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ Please send me your improved versions.
private static readonly int THRESHOLD = 2;
private static readonly int EOF = -1;

public static byte[] DecompressAllNew(byte[] data, bool skip = false)
public static byte[] DecompressAllNew(byte[] data, int uncompressedsize, bool skip = false)
{
//Memory.Log.WriteLine($"{nameof(LZSS)}::{nameof(DecompressAllNew)} :: decompressing data");
byte[] outfilearray;
using (MemoryStream infile = new MemoryStream(!skip?data:data.Skip(4).ToArray()))
{
Decode(infile, out outfilearray);
}
if (uncompressedsize > 0 && outfilearray.Length != uncompressedsize)
throw new InvalidDataException($"{nameof(LZSS)}::{nameof(DecompressAllNew)} Expected size ({uncompressedsize}) != ({outfilearray.Length})");
return outfilearray;
}

Expand Down
10 changes: 6 additions & 4 deletions Core/Saves/Saves.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,15 @@ private static void Read(string file, out Data d)
if (fs.Length >= 5)
{
size = br.ReadInt32();
byte[] tmp = br.ReadBytes((int)fs.Length - sizeof(uint));
decmp = LZSS.DecompressAllNew(tmp);
if ((int)fs.Length - sizeof(uint) == size)
{
byte[] tmp = br.ReadBytes((int)fs.Length - sizeof(uint));
decmp = LZSS.DecompressAllNew(tmp, 0);
}
}
fs = null;
}

if (decmp == null || decmp.Length < size)
if (decmp == null)
{
Memory.Log.WriteLine($"{nameof(Saves)}::{nameof(Read)} Invalid file: {file}");
}
Expand Down
19 changes: 15 additions & 4 deletions Core/module_overture_debug.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ public Splash(int splashNum, bool bNames = true, bool bLogo = false)
return;
}
ArchiveBase aw = ArchiveWorker.Load(Memory.Archives.A_MAIN);
var lof = aw.GetListOfFiles();
string[] lof = aw.GetListOfFiles();
loopsnames = lof.Where(x => x.IndexOf(loops, StringComparison.OrdinalIgnoreCase) > -1).OrderBy(x => x, StringComparer.OrdinalIgnoreCase).ToList();
namesnames = lof.Where(x => x.IndexOf(names, StringComparison.OrdinalIgnoreCase) > -1).OrderBy(x => x, StringComparer.OrdinalIgnoreCase).ToList();
logonames = lof.Where(x => x.IndexOf("ff8.lzs", StringComparison.OrdinalIgnoreCase) > -1).OrderBy(x => x, StringComparer.OrdinalIgnoreCase).ToList();
Expand Down Expand Up @@ -385,10 +385,21 @@ private void ReadSplash()
if (string.IsNullOrWhiteSpace(filename)) return;
ArchiveBase aw = ArchiveWorker.Load(Memory.Archives.A_MAIN);
byte[] buffer = aw.GetBinaryFile(filename);

string fn = Path.GetFileNameWithoutExtension(filename);
uint uncompSize = BitConverter.ToUInt32(buffer, 0);
buffer = buffer.Skip(4).ToArray(); //hotfix for new LZSS
buffer = LZSS.DecompressAllNew(buffer);

if (buffer.Length <= 4)
{
Memory.Log.WriteLine($"{nameof(Module_overture_debug)}::{nameof(ReadSplash)} \"{filename}\" too small to read any data.");
return;
}
int compSize = BitConverter.ToInt32(buffer, 0);
if (compSize != buffer.Length - sizeof(int))
{
Memory.Log.WriteLine($"{nameof(Module_overture_debug)}::{nameof(ReadSplash)} \"{filename}\" wrong size ({buffer.Length - sizeof(int)}) to be ({compSize}).");
return;
}
buffer = LZSS.DecompressAllNew(buffer, 0, true);
TIM_OVERTURE tim = new TIM_OVERTURE(buffer);
if ((fn.Equals("ff8", StringComparison.OrdinalIgnoreCase)) || (fn.IndexOf("loop", StringComparison.OrdinalIgnoreCase) >= 0))
{
Expand Down

0 comments on commit 9f03935

Please sign in to comment.