Skip to content
This repository was archived by the owner on Jun 10, 2022. It is now read-only.

Commit ac18894

Browse files
committed
P5SPC -> Scramble
1 parent e7f87c3 commit ac18894

File tree

9 files changed

+58
-43
lines changed

9 files changed

+58
-43
lines changed

Cethleann.Structure/KTID/RDBFlags.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ public enum RDBFlags
99
Internal = 0x00020000,
1010
ZlibCompressed = 0x00100000,
1111
Lz4Compressed = 0x00200000,
12-
Encrypted = 0x00200000, // reused in p5s pc
12+
Encrypted = 0x00200000, // reused in Scramble
1313
}
1414
}

Cethleann/Archive/RDB.cs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Cethleann.Compression;
2-
using Cethleann.Compression.P5SPC;
32
using Cethleann.KTID;
43
using Cethleann.Structure;
54
using Cethleann.Structure.KTID;
@@ -13,6 +12,8 @@
1312
using System.Runtime.InteropServices;
1413
using System.Text;
1514
using System.Text.RegularExpressions;
15+
using ScrambleRDBEncryption = Cethleann.Compression.Scramble.RDBEncryption;
16+
using ScrambleSRSTEncryption = Cethleann.Compression.Scramble.SRSTEncryption;
1617

1718
namespace Cethleann.Archive
1819
{
@@ -36,9 +37,11 @@ public class RDB : IDisposable
3637
/// <param name="buffer"></param>
3738
/// <param name="name"></param>
3839
/// <param name="directory"></param>
39-
public RDB(Span<byte> buffer, string name, string directory)
40+
/// <param name="game"></param>
41+
public RDB(Span<byte> buffer, string name, string directory, string game)
4042
{
4143
Name = name;
44+
Game = game;
4245

4346
if(File.Exists(Path.Combine(directory, name + ".rdx")))
4447
External = new RDX(File.ReadAllBytes(Path.Combine(directory, name + ".rdx")), directory);
@@ -82,10 +85,15 @@ public RDB(Span<byte> buffer, string name, string directory)
8285
public string DataDirectory { get; set; }
8386

8487
/// <summary>
85-
/// Name of this archive
88+
/// Name of this archive
8689
/// </summary>
8790
public string Name { get; set; }
8891

92+
/// <summary>
93+
/// Name of this game
94+
/// </summary>
95+
public string Game { get; set; }
96+
8997
/// <summary>
9098
/// RDB Header
9199
/// </summary>
@@ -256,15 +264,18 @@ public Memory<byte> ReadEntry(int index)
256264
var fileEntryA = fileEntry.GetValueOrDefault();
257265
if (fileEntryA.Size == 0) return Memory<byte>.Empty;
258266

259-
if (entry.Flags.HasFlag(RDBFlags.External) && MemoryMarshal.Read<uint>(buffer) == 0x53525354)
260-
{
261-
SRSTEncryption.Decrypt(buffer);
262-
}
263-
else if (entry.Flags.HasFlag(RDBFlags.Encrypted))
267+
if (Game == "Scramble")
264268
{
265-
RDBEncryption.Decrypt(buffer, entry.FileKTID.KTID);
266-
267-
entry.Flags ^= RDBFlags.Encrypted;
269+
// todo: check KTID Type.
270+
if (MemoryMarshal.Read<uint>(buffer) == 0x53525354)
271+
{
272+
ScrambleSRSTEncryption.Decrypt(buffer);
273+
}
274+
else if (entry.Flags.HasFlag(RDBFlags.ZlibCompressed) && entry.Flags.HasFlag(RDBFlags.Encrypted))
275+
{
276+
ScrambleRDBEncryption.Decrypt(buffer, entry.FileKTID.KTID);
277+
entry.Flags ^= RDBFlags.Encrypted;
278+
}
268279
}
269280

270281
if (entry.Flags.HasFlag(RDBFlags.ZlibCompressed) || entry.Flags.HasFlag(RDBFlags.Lz4Compressed))

Cethleann/Cethleann.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
</ItemGroup>
2929

3030
<ItemGroup>
31-
<None Update="filelist-P5SPC-link.csv">
31+
<None Update="filelist-Scramble-link.csv">
3232
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
3333
</None>
3434
<None Update="TXTH\.ktgcadpcm.txth">

Cethleann/Compression/P5SPC/LINKDATAEncryption.cs renamed to Cethleann/Compression/Scramble/LINKDATAEncryption.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
using System;
22

3-
namespace Cethleann.Compression.P5SPC
3+
namespace Cethleann.Compression.Scramble
44
{
55
/// <summary>
6-
/// Implements P5S PC LINKDATA file encryption/decryption.
6+
/// Implements Scramble LINKDATA file encryption/decryption.
77
/// </summary>
8-
public static class LINKDATAEncryption
8+
public static class LinkEncryption
99
{
1010
/// <summary>
11-
/// Encrypts a P5S PC LINKDATA file.
11+
/// Encrypts a Scramble LINKDATA file.
1212
/// </summary>
1313
/// <param name="data">The data to encrypt.</param>
1414
/// <param name="id">The ID of the LINKDATA file.</param>
1515
public static void Encrypt(Span<byte> data, uint id)
1616
=> Decrypt(data, id);
1717

1818
/// <summary>
19-
/// Decrypts a P5S PC LINKDATA file.
19+
/// Decrypts a Scramble LINKDATA file.
2020
/// </summary>
2121
/// <param name="data">The data to decrypt.</param>
2222
/// <param name="id">The ID of the LINKDATA file.</param>
@@ -42,7 +42,7 @@ public static void Decrypt(Span<byte> data, uint id)
4242
/// </summary>
4343
public class Mersenne
4444
{
45-
readonly uint[] _state = new uint[4];
45+
readonly uint[] State = new uint[4];
4646

4747
/// <summary>
4848
/// Initializes a generator object using the provided seed value.
@@ -59,10 +59,10 @@ public Mersenne(uint seed)
5959
/// <param name="seed">Value to initialize with.</param>
6060
public void Init(uint seed)
6161
{
62-
_state[0] = 0x6C078965 * (seed ^ (seed >> 30));
62+
State[0] = 0x6C078965 * (seed ^ (seed >> 30));
6363

6464
for (int i = 1; i < 4; i++)
65-
_state[i] = (uint) (0x6C078965 * (_state[i - 1] ^ (_state[i - 1] >> 30)) + i);
65+
State[i] = (uint) (0x6C078965 * (State[i - 1] ^ (State[i - 1] >> 30)) + i);
6666
}
6767

6868
/// <summary>
@@ -71,13 +71,13 @@ public void Init(uint seed)
7171
/// <returns>Next generator state.</returns>
7272
public uint Next()
7373
{
74-
var temp = _state[0] ^ (_state[0] << 11);
75-
_state[0] = _state[1];
76-
_state[1] = _state[2];
77-
_state[2] = _state[3];
78-
_state[3] ^= temp ^ ((temp ^ (_state[3] >> 11)) >> 8);
74+
var temp = State[0] ^ (State[0] << 11);
75+
State[0] = State[1];
76+
State[1] = State[2];
77+
State[2] = State[3];
78+
State[3] ^= temp ^ ((temp ^ (State[3] >> 11)) >> 8);
7979

80-
return _state[3];
80+
return State[3];
8181
}
8282
}
8383
}

Cethleann/Compression/P5SPC/RDBEncryption.cs renamed to Cethleann/Compression/Scramble/RDBEncryption.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
using System;
22
using System.Runtime.InteropServices;
33

4-
namespace Cethleann.Compression.P5SPC
4+
namespace Cethleann.Compression.Scramble
55
{
66
/// <summary>
7-
/// Implements P5S PC RDB encryption/decryption.
7+
/// Implements Scramble RDB encryption/decryption.
88
/// Only used on ZLib compressed data.
99
/// </summary>
1010
public static class RDBEncryption
1111
{
1212
/// <summary>
13-
/// Encrypts a P5S PC RDB ZLib compressed file.
13+
/// Encrypts a Scramble RDB ZLib compressed file.
1414
/// </summary>
1515
/// <param name="data">The compressed data to encrypt.</param>
1616
/// <param name="ktid">The KTID of the RDB file.</param>
1717
public static void Encrypt(Span<byte> data, uint ktid)
1818
=> Decrypt(data, ktid);
1919

2020
/// <summary>
21-
/// Decrypts a P5S PC RDB ZLib compressed file.
21+
/// Decrypts a Scramble RDB ZLib compressed file.
2222
/// </summary>
2323
/// <param name="data">The compressed data to decrypt.</param>
2424
/// <param name="ktid">The KTID of the RDB file.</param>

Cethleann/Compression/P5SPC/SRSTEncryption.cs renamed to Cethleann/Compression/Scramble/SRSTEncryption.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
using System.Buffers.Binary;
33
using System.Runtime.InteropServices;
44

5-
namespace Cethleann.Compression.P5SPC
5+
namespace Cethleann.Compression.Scramble
66
{
77
/// <summary>
8-
/// Implements P5S PC SRST file encryption/decryption.
8+
/// Implements Scramble SRST file encryption/decryption.
99
/// </summary>
1010
public static class SRSTEncryption
1111
{
1212
/// <summary>
13-
/// Encrypts a P5S PC SRST file.
13+
/// Encrypts a Scramble SRST file.
1414
/// The key length should be at offset 0x30 (relative to SRST header).
1515
/// The key itself should follow from offset 0x31 and on.
1616
/// If the key isn't present, no encryption is performed.
@@ -21,7 +21,7 @@ public static bool Encrypt(Span<byte> data)
2121
=> Crypt(data, false);
2222

2323
/// <summary>
24-
/// Decrypts a P5S PC SRST file.
24+
/// Decrypts a Scramble SRST file.
2525
/// The key length should be at offset 0x30 (relative to SRST header).
2626
/// The key itself should follow from offset 0x31 and on.
2727
/// If the key isn't present, no decryption is performed.
@@ -32,7 +32,7 @@ public static bool Decrypt(Span<byte> data)
3232
=> Crypt(data, true);
3333

3434
/// <summary>
35-
/// (En/De)crypts a P5S PC SRST file.
35+
/// (En/De)crypts a Scramble SRST file.
3636
/// </summary>
3737
/// <param name="data">The SRST file to decrypt.</param>
3838
/// <param name="decrypt">True to decrypt, otherwise encrypt.</param>
@@ -69,6 +69,10 @@ private static bool Crypt(Span<byte> data, bool decrypt)
6969
}
7070
}
7171

72+
//Blowfish encryption (ECB, CBC and CTR mode) as defined by Bruce Schneier here: http://www.schneier.com/paper-blowfish-fse.html
73+
//Complies with test vectors found here: http://www.schneier.com/code/vectors.txt
74+
//non-standard mode provided to be usable with the javascript crypto library found here: http://etherhack.co.uk/symmetric/blowfish/blowfish.html
75+
//By Taylor Hornby, 1/7/1010
7276
/// <summary>
7377
/// Implements a basic Blowfish cipher.
7478
/// Only ECB mode is supported.

Cethleann/ManagedFS/Flayn.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Cethleann.Archive;
2-
using Cethleann.Compression.P5SPC;
32
using Cethleann.ManagedFS.Options;
43
using Cethleann.ManagedFS.Options.Default;
54
using Cethleann.Structure;
@@ -9,6 +8,7 @@
98
using System.Collections.Generic;
109
using System.IO;
1110
using System.Linq;
11+
using ScrambleLinkEncryption = Cethleann.Compression.Scramble.LinkEncryption;
1212

1313
namespace Cethleann.ManagedFS
1414
{
@@ -122,10 +122,10 @@ public Memory<byte> ReadEntry(int index)
122122
var buffer = data.ReadEntry(stream, localId);
123123
if (GameId == "ThreeHouses" && i > 0 && buffer.Length == 0) continue;
124124

125-
// for p5s pc, all non-compressed entries are encrypted
125+
// for Scramble, all non-compressed entries are encrypted
126126
// conversly, if an entry is compressed, it can't also be encrypted
127-
if (GameId == "P5SPC" && localId < data.Entries.Count && !data.Entries[localId].IsCompressed)
128-
LINKDATAEncryption.Decrypt(buffer.Span, (uint) localId);
127+
if (GameId == "Scramble" && localId < data.Entries.Count && !data.Entries[localId].IsCompressed)
128+
ScrambleLinkEncryption.Decrypt(buffer.Span, (uint) localId);
129129
return buffer;
130130
}
131131

@@ -190,7 +190,7 @@ public string GetFilename(int index, string? ext = "bin", DataType dataType = Da
190190
id = GameId switch
191191
{
192192
"ThreeHouses" => $"{(i == 0 ? string.Empty : "DLC_")}{localId}",
193-
"P5SPC" => $"{localId}",
193+
"Scramble" => $"{localId}",
194194
_ => $"{linkname}_{localId}"
195195
};
196196
prefix = GameId switch
@@ -209,7 +209,7 @@ public string GetFilename(int index, string? ext = "bin", DataType dataType = Da
209209
{
210210
ext = GameId switch
211211
{
212-
"P5SPC" => Path.GetExtension(path) ?? ext,
212+
"Scramble" => Path.GetExtension(path) ?? ext,
213213
_ => $".{ext}"
214214
};
215215
path = Path.Combine(Path.GetDirectoryName(path) ?? string.Empty, Path.GetFileNameWithoutExtension(path) + $"{ext}");

Cethleann/ManagedFS/Nyotengu.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public Memory<byte> ReadEntry(int index)
8282
public void AddDataFS(string path)
8383
{
8484
Logger.Success("Nyotengu", $"Loading {Path.GetFileName(path)}...");
85-
var rdb = new RDB(File.ReadAllBytes(path), Path.GetFileNameWithoutExtension(path), Path.GetDirectoryName(path) ?? string.Empty);
85+
var rdb = new RDB(File.ReadAllBytes(path), Path.GetFileNameWithoutExtension(path), Path.GetDirectoryName(path) ?? string.Empty, GameId);
8686
foreach (var file in Directory.GetFiles(Path.GetDirectoryName(path) ?? "./", rdb.Name + "*.info"))
8787
rdb.NameDatabase.Union(new RDBINFO(File.ReadAllBytes(file)));
8888
EntryCount += rdb.Entries.Count;

0 commit comments

Comments
 (0)