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

Commit

Permalink
Merge branch 'main' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
yretenai committed Jun 10, 2022
2 parents 601db49 + ac18894 commit 94bc373
Show file tree
Hide file tree
Showing 27 changed files with 95 additions and 64 deletions.
2 changes: 1 addition & 1 deletion Cethleann.DataExporter/Cethleann.DataExporter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<Configurations>Release;Debug</Configurations>
<Platforms>x64</Platforms>
<RuntimeIdentifiers>win10-x64;osx-x64;linux-x64</RuntimeIdentifiers>
Expand Down
2 changes: 1 addition & 1 deletion Cethleann.DataProcessor/Cethleann.DataProcessor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<Configurations>Release;Debug</Configurations>
<Platforms>x64</Platforms>
<RuntimeIdentifiers>win10-x64;osx-x64;linux-x64</RuntimeIdentifiers>
Expand Down
2 changes: 1 addition & 1 deletion Cethleann.Downloader/Cethleann.Downloader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<Configurations>Release;Debug</Configurations>
<Platforms>x64</Platforms>
<RuntimeIdentifiers>win10-x64;osx-x64;linux-x64</RuntimeIdentifiers>
Expand Down
12 changes: 10 additions & 2 deletions Cethleann.Downloader/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using DragonLib;
using DragonLib.CLI;
using DragonLib.IO;
using System.Net.Http;

namespace Cethleann.Downloader
{
Expand Down Expand Up @@ -51,10 +52,17 @@ private static void Main(string[] args)
var (url, dest, size) = pair;
Logger.Info("Cethleann", url);
if (flags.Dry) return;
using var http = new WebClient();
using var http = new HttpClient();
try
{
http.DownloadFile(url, dest);
var folder = Path.GetDirectoryName(dest);
if (!string.IsNullOrEmpty(folder) && !Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}

using var stream = http.GetStreamAsync(url);
using var destStream = File.OpenWrite(dest);
Logger.Info("Cethleann", $"Downloaded {size} to {dest}");
}
catch (Exception e)
Expand Down
2 changes: 1 addition & 1 deletion Cethleann.Gz/Cethleann.Gz.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<Configurations>Release;Debug</Configurations>
<Platforms>x64</Platforms>
<RuntimeIdentifiers>win10-x64;osx-x64;linux-x64</RuntimeIdentifiers>
Expand Down
2 changes: 1 addition & 1 deletion Cethleann.Identify/Cethleann.Identify.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<Configurations>Release;Debug</Configurations>
<Platforms>x64</Platforms>
<RuntimeIdentifiers>win10-x64;osx-x64;linux-x64</RuntimeIdentifiers>
Expand Down
2 changes: 1 addition & 1 deletion Cethleann.Prototype/Cethleann.Prototype.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<Configurations>Debug</Configurations>
<Platforms>x64</Platforms>
<Nullable>enable</Nullable>
Expand Down
2 changes: 1 addition & 1 deletion Cethleann.Structure/Cethleann.Structure.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<Platforms>x64</Platforms>
<RuntimeIdentifiers>win10-x64;osx-x64;linux-x64</RuntimeIdentifiers>
<Version>1.0.0</Version>
Expand Down
2 changes: 1 addition & 1 deletion Cethleann.Structure/KTID/RDBFlags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ public enum RDBFlags
Internal = 0x00020000,
ZlibCompressed = 0x00100000,
Lz4Compressed = 0x00200000,
Encrypted = 0x00200000, // reused in p5s pc
Encrypted = 0x00200000, // reused in Scramble
}
}
2 changes: 1 addition & 1 deletion Cethleann.Unbundler/Cethleann.Unbundler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<Configurations>Release;Debug</Configurations>
<Platforms>x64</Platforms>
<RuntimeIdentifiers>win10-x64;osx-x64;linux-x64</RuntimeIdentifiers>
Expand Down
2 changes: 1 addition & 1 deletion Cethleann.XORTool/Cethleann.XORTool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<Configurations>Release;Debug</Configurations>
<Platforms>x64</Platforms>
<RuntimeIdentifiers>win10-x64;osx-x64;linux-x64</RuntimeIdentifiers>
Expand Down
33 changes: 22 additions & 11 deletions Cethleann/Archive/RDB.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Cethleann.Compression;
using Cethleann.Compression.P5SPC;
using Cethleann.KTID;
using Cethleann.Structure;
using Cethleann.Structure.KTID;
Expand All @@ -13,6 +12,8 @@
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using ScrambleRDBEncryption = Cethleann.Compression.Scramble.RDBEncryption;
using ScrambleSRSTEncryption = Cethleann.Compression.Scramble.SRSTEncryption;

namespace Cethleann.Archive
{
Expand All @@ -36,9 +37,11 @@ public class RDB : IDisposable
/// <param name="buffer"></param>
/// <param name="name"></param>
/// <param name="directory"></param>
public RDB(Span<byte> buffer, string name, string directory)
/// <param name="game"></param>
public RDB(Span<byte> buffer, string name, string directory, string game)
{
Name = name;
Game = game;

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

/// <summary>
/// Name of this archive
/// Name of this archive
/// </summary>
public string Name { get; set; }

/// <summary>
/// Name of this game
/// </summary>
public string Game { get; set; }

/// <summary>
/// RDB Header
/// </summary>
Expand Down Expand Up @@ -256,15 +264,18 @@ public Memory<byte> ReadEntry(int index)
var fileEntryA = fileEntry.GetValueOrDefault();
if (fileEntryA.Size == 0) return Memory<byte>.Empty;

if (entry.Flags.HasFlag(RDBFlags.External) && MemoryMarshal.Read<uint>(buffer) == 0x53525354)
{
SRSTEncryption.Decrypt(buffer);
}
else if (entry.Flags.HasFlag(RDBFlags.Encrypted))
if (Game == "Scramble")
{
RDBEncryption.Decrypt(buffer, entry.FileKTID.KTID);

entry.Flags ^= RDBFlags.Encrypted;
// todo: check KTID Type.
if (MemoryMarshal.Read<uint>(buffer) == 0x53525354)
{
ScrambleSRSTEncryption.Decrypt(buffer);
}
else if (entry.Flags.HasFlag(RDBFlags.ZlibCompressed) && entry.Flags.HasFlag(RDBFlags.Encrypted))
{
ScrambleRDBEncryption.Decrypt(buffer, entry.FileKTID.KTID);
entry.Flags ^= RDBFlags.Encrypted;
}
}

if (entry.Flags.HasFlag(RDBFlags.ZlibCompressed) || entry.Flags.HasFlag(RDBFlags.Lz4Compressed))
Expand Down
4 changes: 2 additions & 2 deletions Cethleann/Cethleann.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<Platforms>x64</Platforms>
<RuntimeIdentifiers>win10-x64;osx-x64;linux-x64</RuntimeIdentifiers>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand All @@ -28,7 +28,7 @@
</ItemGroup>

<ItemGroup>
<None Update="filelist-P5SPC-link.csv">
<None Update="filelist-Scramble-link.csv">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TXTH\.ktgcadpcm.txth">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
using System;

namespace Cethleann.Compression.P5SPC
namespace Cethleann.Compression.Scramble
{
/// <summary>
/// Implements P5S PC LINKDATA file encryption/decryption.
/// Implements Scramble LINKDATA file encryption/decryption.
/// </summary>
public static class LINKDATAEncryption
public static class LinkEncryption
{
/// <summary>
/// Encrypts a P5S PC LINKDATA file.
/// Encrypts a Scramble LINKDATA file.
/// </summary>
/// <param name="data">The data to encrypt.</param>
/// <param name="id">The ID of the LINKDATA file.</param>
public static void Encrypt(Span<byte> data, uint id)
=> Decrypt(data, id);

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

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

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

/// <summary>
Expand All @@ -71,13 +71,13 @@ public void Init(uint seed)
/// <returns>Next generator state.</returns>
public uint Next()
{
var temp = _state[0] ^ (_state[0] << 11);
_state[0] = _state[1];
_state[1] = _state[2];
_state[2] = _state[3];
_state[3] ^= temp ^ ((temp ^ (_state[3] >> 11)) >> 8);
var temp = State[0] ^ (State[0] << 11);
State[0] = State[1];
State[1] = State[2];
State[2] = State[3];
State[3] ^= temp ^ ((temp ^ (State[3] >> 11)) >> 8);

return _state[3];
return State[3];
}
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
using System;
using System.Runtime.InteropServices;

namespace Cethleann.Compression.P5SPC
namespace Cethleann.Compression.Scramble
{
/// <summary>
/// Implements P5S PC RDB encryption/decryption.
/// Implements Scramble RDB encryption/decryption.
/// Only used on ZLib compressed data.
/// </summary>
public static class RDBEncryption
{
/// <summary>
/// Encrypts a P5S PC RDB ZLib compressed file.
/// Encrypts a Scramble RDB ZLib compressed file.
/// </summary>
/// <param name="data">The compressed data to encrypt.</param>
/// <param name="ktid">The KTID of the RDB file.</param>
public static void Encrypt(Span<byte> data, uint ktid)
=> Decrypt(data, ktid);

/// <summary>
/// Decrypts a P5S PC RDB ZLib compressed file.
/// Decrypts a Scramble RDB ZLib compressed file.
/// </summary>
/// <param name="data">The compressed data to decrypt.</param>
/// <param name="ktid">The KTID of the RDB file.</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
using System.Buffers.Binary;
using System.Runtime.InteropServices;

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

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

/// <summary>
/// (En/De)crypts a P5S PC SRST file.
/// (En/De)crypts a Scramble SRST file.
/// </summary>
/// <param name="data">The SRST file to decrypt.</param>
/// <param name="decrypt">True to decrypt, otherwise encrypt.</param>
Expand Down Expand Up @@ -69,6 +69,10 @@ private static bool Crypt(Span<byte> data, bool decrypt)
}
}

//Blowfish encryption (ECB, CBC and CTR mode) as defined by Bruce Schneier here: http://www.schneier.com/paper-blowfish-fse.html
//Complies with test vectors found here: http://www.schneier.com/code/vectors.txt
//non-standard mode provided to be usable with the javascript crypto library found here: http://etherhack.co.uk/symmetric/blowfish/blowfish.html
//By Taylor Hornby, 1/7/1010
/// <summary>
/// Implements a basic Blowfish cipher.
/// Only ECB mode is supported.
Expand Down
12 changes: 6 additions & 6 deletions Cethleann/ManagedFS/Flayn.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Cethleann.Archive;
using Cethleann.Compression.P5SPC;
using Cethleann.ManagedFS.Options;
using Cethleann.ManagedFS.Options.Default;
using Cethleann.Structure;
Expand All @@ -9,6 +8,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using ScrambleLinkEncryption = Cethleann.Compression.Scramble.LinkEncryption;

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

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

Expand Down Expand Up @@ -190,7 +190,7 @@ public string GetFilename(int index, string? ext = "bin", DataType dataType = Da
id = GameId switch
{
"ThreeHouses" => $"{(i == 0 ? string.Empty : "DLC_")}{localId}",
"P5SPC" => $"{localId}",
"Scramble" => $"{localId}",
_ => $"{linkname}_{localId}"
};
prefix = GameId switch
Expand All @@ -209,7 +209,7 @@ public string GetFilename(int index, string? ext = "bin", DataType dataType = Da
{
ext = GameId switch
{
"P5SPC" => Path.GetExtension(path) ?? ext,
"Scramble" => Path.GetExtension(path) ?? ext,
_ => $".{ext}"
};
path = Path.Combine(Path.GetDirectoryName(path) ?? string.Empty, Path.GetFileNameWithoutExtension(path) + $"{ext}");
Expand Down
Loading

0 comments on commit 94bc373

Please sign in to comment.