Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BG4 plugin fix & BackwardLz77 logic revert #296

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions plugins/Nintendo/plugin_alpha_dream/Archives/Bg4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Komponent.IO;
using Komponent.IO.Streams;
using Kompression.Implementations;
using Kontract.Extensions;
using Kontract.Models.Archive;
using Kryptography.Hash;

Expand Down Expand Up @@ -56,7 +57,7 @@ public void Save(Stream output, IList<IArchiveFileInfo> files)
var stringPosition = 0;
var stringDictionary = new Dictionary<string, int>();

foreach (var distinctString in files.Select(x => x.FilePath.FullName).Distinct())
foreach (var distinctString in files.Select(x => x.FilePath.ToRelative().FullName).Distinct())
{
stringDictionary[distinctString] = stringPosition;
stringPosition += Encoding.ASCII.GetByteCount(distinctString) + 1;
Expand All @@ -76,7 +77,7 @@ public void Save(Stream output, IList<IArchiveFileInfo> files)
var writtenSize = file.SaveFileData(output);

// Create entry
var fileName = file.FilePath.FullName;
var fileName = file.FilePath.ToRelative().FullName;
entries.Add(new Bg4Entry
{
FileOffset = filePosition,
Expand Down
169 changes: 135 additions & 34 deletions src/Kompression/Implementations/Decoders/Nintendo/BackwardLz77Decoder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System;
using System.Diagnostics;
using System.IO;
using Komponent.IO.Streams;
using Kompression.Extensions;
Expand All @@ -19,49 +20,149 @@ public BackwardLz77Decoder(ByteOrder byteOrder)

public void Decode(Stream input, Stream output)
{
var buffer = new byte[4];
input.Position = input.Length - 8;

input.Read(buffer, 0, 4);
var bufferTopAndBottom = _byteOrder == ByteOrder.LittleEndian ? buffer.GetInt32LittleEndian(0) : buffer.GetInt32BigEndian(0);

input.Read(buffer, 0, 4);
var decompressedOffset = _byteOrder == ByteOrder.LittleEndian ? buffer.GetInt32LittleEndian(0) : buffer.GetInt32BigEndian(0);

var footerLength = bufferTopAndBottom >> 24;
var compressedSize = bufferTopAndBottom & 0xFFFFFF;

using (var inputReverseStream = new ReverseStream(input, input.Length - footerLength))
using (var outputReverseStream = new ReverseStream(output, input.Length + decompressedOffset))
// Check if enough space exists for a footer
if (input.Length >= 8)
{
// Read footer
var buffer = new byte[4];
input.Position = input.Length - 8;

input.Read(buffer, 0, 4);
var bufferTopAndBottom = _byteOrder == ByteOrder.LittleEndian
? buffer.GetInt32LittleEndian(0)
: buffer.GetInt32BigEndian(0);

input.Read(buffer, 0, 4);
var decompressedOffset = _byteOrder == ByteOrder.LittleEndian
? buffer.GetInt32LittleEndian(0)
: buffer.GetInt32BigEndian(0);
var decompressedSize = input.Length + decompressedOffset;

var top = bufferTopAndBottom & 0xFFFFFF;
var bottom = bufferTopAndBottom >> 24 & 0xFF;

// Check footer integrity
if (bottom >= 8 && bottom <= 8 + 3 && top >= bottom && top <= input.Length &&
decompressedSize >= input.Length + decompressedOffset)
ReadCompressedData(input, output, decompressedSize, decompressedSize, input.Length - bottom,
input.Length - top);
else
{
Debugger.Break();
throw new InvalidOperationException("Something went wrong.");
}
}
else
{
var endPosition = compressedSize - footerLength;
ReadCompressedData(inputReverseStream, outputReverseStream, endPosition);
Debugger.Break();
throw new InvalidOperationException("Something went wrong.");
}

//using (var inputReverseStream = new ReverseStream(input, input.Length - footerLength))
//using (var outputReverseStream = new ReverseStream(output, input.Length + decompressedOffset))
//{
//ReadCompressedData(input, output, decompressedSize, decompressedSize, input.Length - bottom, input.Length - top);
//}
}

private void ReadCompressedData(Stream input, Stream output, long endPosition)
private void ReadCompressedData(Stream input, Stream output, long decompressedSize, long dest, long src, long end)
{
var circularBuffer = new CircularBuffer(0x1002);

var codeBlock = input.ReadByte();
var codeBlockPosition = 8;
while (input.Position < endPosition)
while (src - end > 0)
{
if (codeBlockPosition == 0)
input.Position = --src;
var flag = input.ReadByte();

for (var i = 0; i < 8; i++)
{
codeBlock = input.ReadByte();
codeBlockPosition = 8;
if (((flag << i) & 0x80) == 0)
{
if (dest - end < 1 || src - end < 1)
{
Debugger.Break();
throw new InvalidOperationException("Something went wrong.");
}

input.Position = --src;
var value = input.ReadByte();

output.Position = --dest;
output.WriteByte((byte)value);
}
else
{
if (src - end < 2)
{
Debugger.Break();
throw new InvalidOperationException("Something went wrong.");
}

input.Position = --src;
var size = input.ReadByte();

input.Position = --src;
var offset = (((size & 0x0F) << 8) | input.ReadByte()) + 3;
size = ((size >> 4) & 0x0F) + 3;

if(dest<0x60)
Debugger.Break();

if (size > dest - end)
{
Debugger.Break();
throw new InvalidOperationException("Something went wrong.");
}

var data = dest + offset;
if (data > decompressedSize)
{
Debugger.Break();
throw new InvalidOperationException("Something went wrong.");
}

for (var j = 0; j < size; j++)
{
output.Position = --data;
var value = output.ReadByte();

output.Position = --dest;
output.WriteByte((byte)value);
}
}

if (src - end <= 0)
break;
}

var flag = (codeBlock >> --codeBlockPosition) & 0x1;
if (flag == 0)
HandleUncompressedBlock(input, output, circularBuffer);
else
HandleCompressedBlock(input, output, circularBuffer);
}

while (input.Position < input.Length)
output.WriteByte((byte)input.ReadByte());
// Copy remaining bytes after end
input.Position = 0;
output.Position = 0;

var buffer = new byte[end];
input.Read(buffer);
output.Write(buffer);

//var circularBuffer = new CircularBuffer(0x1002);

//var codeBlock = input.ReadByte();
//var codeBlockPosition = 8;
//while (input.Position < endPosition)
//{
// if (codeBlockPosition == 0)
// {
// codeBlock = input.ReadByte();
// codeBlockPosition = 8;
// }

// var flag = (codeBlock >> --codeBlockPosition) & 0x1;
// if (flag == 0)
// HandleUncompressedBlock(input, output, circularBuffer);
// else
// HandleCompressedBlock(input, output, circularBuffer);
//}

//while (input.Position < input.Length)
// output.WriteByte((byte)input.ReadByte());
}

private void HandleUncompressedBlock(Stream input, Stream output, CircularBuffer circularBuffer)
Expand Down
Loading