Skip to content

Commit 79740e7

Browse files
committed
Add optional verification to BLTE load
1 parent d0f8057 commit 79740e7

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

TACTSharp/BLTE.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
using System.Buffers.Binary;
22
using System.IO.Compression;
3+
using System.Security.Cryptography;
34

45
namespace TACTSharp
56
{
67
public static class BLTE
78
{
8-
public static byte[] Decode(ReadOnlySpan<byte> data, ulong totalDecompSize = 0)
9+
public static byte[] Decode(ReadOnlySpan<byte> data, ulong totalDecompSize = 0, bool verify = false)
910
{
1011
var fixedHeaderSize = 8;
1112

@@ -26,7 +27,7 @@ public static byte[] Decode(ReadOnlySpan<byte> data, ulong totalDecompSize = 0)
2627
}
2728

2829
var tableFormat = data[(fixedHeaderSize + 0)];
29-
if(tableFormat != 0xF)
30+
if (tableFormat != 0xF)
3031
throw new Exception("Unexpected BLTE table format");
3132

3233
// If tableFormat is 0x10 this might be 40 instead of 24. Only seen in Avowed (aqua) product. Likely another key.
@@ -46,6 +47,7 @@ public static byte[] Decode(ReadOnlySpan<byte> data, ulong totalDecompSize = 0)
4647
}
4748

4849
var decompData = new byte[totalDecompSize];
50+
4951
var infoOffset = infoStart;
5052
int compOffset = headerSize;
5153
int decompOffset = 0;
@@ -54,10 +56,17 @@ public static byte[] Decode(ReadOnlySpan<byte> data, ulong totalDecompSize = 0)
5456
{
5557
var compSize = data[(infoOffset + 0)..].ReadInt32BE();
5658
var decompSize = data[(infoOffset + 4)..].ReadInt32BE();
57-
// var checkSum = data[(infoOffset+8)..(infoOffset+8+16)];
5859

5960
HandleDataBlock((char)data[compOffset], data[(compOffset + 1)..(compOffset + compSize)], chunkIndex, decompData.AsSpan().Slice(decompOffset, decompSize));
6061

62+
if (verify)
63+
{
64+
var checkSum = data[(infoOffset + 8)..(infoOffset + 8 + 16)];
65+
var compHash = MD5.HashData(data[(compOffset)..(compOffset + compSize)]);
66+
if (!compHash.AsSpan().SequenceEqual(checkSum))
67+
throw new InvalidDataException($"Checksum mismatch for compressed chunk {chunkIndex}");
68+
}
69+
6170
infoOffset += blockInfoSize;
6271
compOffset += compSize;
6372
decompOffset += decompSize;

0 commit comments

Comments
 (0)