diff --git a/Dalamud.Bootstrap/Bootstrapper.cs b/Dalamud.Bootstrap/Bootstrapper.cs index a307a51b3..9cd0bffe3 100644 --- a/Dalamud.Bootstrap/Bootstrapper.cs +++ b/Dalamud.Bootstrap/Bootstrapper.cs @@ -19,6 +19,8 @@ namespace Dalamud.Bootstrap public void Launch(string exePath, string? commandLine) { + commandLine = commandLine ?? ""; + throw new NotImplementedException("TODO"); } @@ -46,7 +48,7 @@ namespace Dalamud.Bootstrap using var process = Process.Open(pid); var commandLine = process.ReadCommandLine(); - if (!ArgumentContainer.Parse(commandLine[1], out var container)) + if (!EncodedArgument.Parse(commandLine[1], out var container)) { } diff --git a/Dalamud.Bootstrap/Crypto/Blowfish.cs b/Dalamud.Bootstrap/Crypto/Blowfish.cs index 8d58c1cbb..eba6d1277 100644 --- a/Dalamud.Bootstrap/Crypto/Blowfish.cs +++ b/Dalamud.Bootstrap/Crypto/Blowfish.cs @@ -4,7 +4,7 @@ using System.Runtime.CompilerServices; namespace Dalamud.Bootstrap.Crypto { - internal unsafe partial struct BlowfishState + internal unsafe struct BlowfishState { // References: // https://www.schneier.com/academic/archives/1994/09/description_of_a_new.html @@ -171,14 +171,9 @@ namespace Dalamud.Bootstrap.Crypto private fixed uint m_s1[SSize]; private fixed uint m_s2[SSize]; private fixed uint m_s3[SSize]; - } - internal unsafe partial struct BlowfishState - { public BlowfishState(ReadOnlySpan key) { - CheckKeyLength(key); - // initializes P-array and S-boxes to initial values. fixed (uint* pSrc = PInit) fixed (uint* pDst = m_p) @@ -213,15 +208,15 @@ namespace Dalamud.Bootstrap.Crypto InitKey(key); } - private void CheckKeyLength(ReadOnlySpan key) - { - // Supported key sizes: 32–448 bits - // https://en.wikipedia.org/wiki/Blowfish_(cipher)#The_algorithm - if (key.Length < 4 || key.Length > 56) - { - throw new ArgumentException("Key length must be between from 32 to 448 bits.", nameof(key)); - } - } + // private void CheckKeyLength(ReadOnlySpan key) + // { + // // Supported key sizes: 32–448 bits + // // https://en.wikipedia.org/wiki/Blowfish_(cipher)#The_algorithm + // if (key.Length < 4 || key.Length > 56) + // { + // throw new ArgumentException("Key length must be between from 32 to 448 bits.", nameof(key)); + // } + // } /// /// Encrypts a block. @@ -363,13 +358,10 @@ namespace Dalamud.Bootstrap.Crypto } } - internal sealed partial class Blowfish + internal sealed class Blowfish { private BlowfishState m_state; - } - - internal sealed partial class Blowfish - { + /// /// Initializes a new instance of the Blowfish class. /// diff --git a/Dalamud.Bootstrap/SqexArg/ArgumentContainer.cs b/Dalamud.Bootstrap/SqexArg/ArgumentContainer.cs deleted file mode 100644 index 4bd85fdad..000000000 --- a/Dalamud.Bootstrap/SqexArg/ArgumentContainer.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; - -namespace Dalamud.Bootstrap.SqexArg -{ - internal sealed class ArgumentContainer - { - /// - /// - /// - /// - /// - /// - public static bool Parse(string argument, out ArgumentContainer? container) - { - if (argument.Length < 17) - { - // does not contain: //**sqex003 + payload + checksum + **// - container = null; - return false; - } - - if (!argument.StartsWith("//**sqex003") || !argument.EndsWith("**//")) - { - container = null; - return false; - } - - var checksum = argument[^5]; - var payload = argument[11..^5]; - - // decode - - //Convert.FromBase64String(); - //container = new ArgumentContainer(payload, checksum); - - container = null; - return true; - } - } -} diff --git a/Dalamud.Bootstrap/SqexArg/EncodedArgument.cs b/Dalamud.Bootstrap/SqexArg/EncodedArgument.cs new file mode 100644 index 000000000..3e740b215 --- /dev/null +++ b/Dalamud.Bootstrap/SqexArg/EncodedArgument.cs @@ -0,0 +1,87 @@ +using System; +using System.Buffers; +using System.Buffers.Text; +using System.Runtime.InteropServices; +using System.Text; +using Dalamud.Bootstrap.Crypto; + +namespace Dalamud.Bootstrap.SqexArg +{ + internal sealed class EncodedArgument + { + private static char[] ChecksumTable = new char[] + { + 'f', 'X', '1', 'p', 'G', 't', 'd', 'S', + '5', 'C', 'A', 'P', '4', '_', 'V', 'L' + }; + + private readonly ReadOnlyMemory m_data; + + private readonly uint m_key; + + //private readonly Blowfish m_cachedBlowfish; + + public EncodedArgument(ReadOnlyMemory data, uint key) + { + m_data = data; + m_key = key; + } + + public static bool TryParse(string argument, out EncodedArgument? value) + { + if (argument.Length <= 17) + { + // does not contain: //**sqex0003 + payload + checksum + **// + value = null; + return false; + } + + if (!argument.StartsWith("//**sqex0003") || !argument.EndsWith("**//")) + { + value = null; + return false; + } + + var checksum = argument[^5]; + var payload = DecodeUrlSafeBase64(argument.Substring(12, argument.Length - 1 - 12 - 4)); // //**sqex0003, checksum, **// + + if (!FindPartialKey(checksum, out var partialKey)) + { + value = null; + return false; + } + + for (var i = 0u; i <= 0xFFF; i++) + { + var key = (i << 20) | partialKey; + } + } + + private static bool FindPartialKey(char checksum, out uint recoveredKey) + { + var index = MemoryExtensions.IndexOf(ChecksumTable, checksum); + + if (index < 0) + { + recoveredKey = 0; + return false; + } + + recoveredKey = (uint) (index << 16); + return true; + } + + /// + /// Converts url safe variant of base64 string to bytes. + /// + /// A url-safe variant of base64 string. + private static byte[] DecodeUrlSafeBase64(string payload) + { + var base64Str = payload + .Replace('-', '+') + .Replace('_', '/'); + + return Convert.FromBase64String(base64Str); + } + } +} diff --git a/build/DalamudBuild.cs b/build/DalamudBuild.cs index 4787552e4..4fec2b496 100644 --- a/build/DalamudBuild.cs +++ b/build/DalamudBuild.cs @@ -35,6 +35,8 @@ class DalamudBuild : NukeBuild AbsolutePath OutputDirectory => RootDirectory / "output"; + //AbsolutePath DefaultDalamudRoot => + Target Clean => _ => _ .Before(Restore) .Executes(() => @@ -59,7 +61,7 @@ class DalamudBuild : NukeBuild .SetAssemblyVersion(GitVersion.AssemblySemVer) .SetFileVersion(GitVersion.AssemblySemFileVer) .SetInformationalVersion(GitVersion.InformationalVersion) - /*.EnableNoRestore()*/); + .EnableNoRestore()); }); Target Install => _ => _ @@ -67,5 +69,6 @@ class DalamudBuild : NukeBuild .Executes(() => { // TODO + Direct }); }