From 85fe8049117eb725a6a933f5e8766bcf23b0db96 Mon Sep 17 00:00:00 2001 From: Mino Date: Wed, 25 Mar 2020 10:41:42 +0900 Subject: [PATCH] Fix blowfish implementation and other things --- Dalamud.Bootstrap/Bootstrapper.cs | 2 - Dalamud.Bootstrap/SqexArg/EncodedArgument.cs | 39 +++++++++++++------ Dalamud.Bootstrap/Windows/ProcessException.cs | 3 ++ Dalamud/EntryPoint.cs | 2 +- 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/Dalamud.Bootstrap/Bootstrapper.cs b/Dalamud.Bootstrap/Bootstrapper.cs index 4b192f356..a9f63297d 100644 --- a/Dalamud.Bootstrap/Bootstrapper.cs +++ b/Dalamud.Bootstrap/Bootstrapper.cs @@ -65,8 +65,6 @@ namespace Dalamud.Bootstrap // EncodeSqexArg(str, newKey) process.Terminate(); - - throw new NotImplementedException("TODO"); } /// diff --git a/Dalamud.Bootstrap/SqexArg/EncodedArgument.cs b/Dalamud.Bootstrap/SqexArg/EncodedArgument.cs index 1af2d577b..32be71571 100644 --- a/Dalamud.Bootstrap/SqexArg/EncodedArgument.cs +++ b/Dalamud.Bootstrap/SqexArg/EncodedArgument.cs @@ -57,20 +57,24 @@ namespace Dalamud.Bootstrap.SqexArg /// /// /// + /// + /// Thrown when the function could not parse the encoded argument. + /// Message property will carry additional information. + /// public static EncodedArgument Parse(string argument) { - if (argument.Length <= 17) + // check if argument contains is large enough to contain start marker, checksum and end marker. + if (argument.Length < "//**sqex0003!**//".Length) { - // does not contain: //**sqex0003 + payload + checksum + **// var exMessage = $"The string ({argument}) is too short to parse the encoded argument." - + $" It should be atleast large enough to contain the start marker, end marker, payload and checksum."; + + $" It should be atleast large enough to store the start marker,checksum and end marker.."; throw new SqexArgException(exMessage); } - if (!argument.StartsWith("//**sqex0003") || !argument.EndsWith("**//")) + if (!argument.StartsWith("//**sqex0003") || !argument[13..].EndsWith("**//")) { var exMessage = $"The string ({argument}) doesn't look like the valid argument." - + $" It should start with //**sqeex003 and end with **// string."; + + $" It should start with //**sqex0003 and end with **// string."; throw new SqexArgException(exMessage); } @@ -102,11 +106,7 @@ namespace Dalamud.Bootstrap.SqexArg while (true) { - if (!CreateKey(keyBytes, keyCandicate)) - { - var message = $"BUG: Could not create a key"; // This should not fail but.. - throw new InvalidOperationException(message); - } + CreateKey(keyBytes, keyCandicate); var blowfish = new Blowfish(keyBytes); blowfish.Decrypt(encryptedData, decryptedData); @@ -130,13 +130,30 @@ namespace Dalamud.Bootstrap.SqexArg } } + /// + /// + /// + /// + /// private static bool CheckDecryptedData(ReadOnlySpan decryptedData) { // TODO return false; } - private static bool CreateKey(Span destination, uint key) => Utf8Formatter.TryFormat(key, destination, out var _, new StandardFormat('X', 8)); + /// + /// Formats the key. + /// + /// A secret key. + /// A buffer where formatted key will be stored. This must be larger than 8 bytes. + private static void CreateKey(uint key, Span destination) + { + if (!Utf8Formatter.TryFormat(key, destination, out var _, new StandardFormat('X', 8))) + { + var message = $"BUG: Could not create a key"; // This should not fail but.. + throw new InvalidOperationException(message); + } + } /// /// Deduces a partial key from the checksum. diff --git a/Dalamud.Bootstrap/Windows/ProcessException.cs b/Dalamud.Bootstrap/Windows/ProcessException.cs index 4ee9d602e..5d7c426df 100644 --- a/Dalamud.Bootstrap/Windows/ProcessException.cs +++ b/Dalamud.Bootstrap/Windows/ProcessException.cs @@ -3,6 +3,9 @@ using System.ComponentModel; namespace Dalamud.Bootstrap.Windows { + /// + /// An exception that is thrown when there was an error while interacting with the process. + /// public class ProcessException : Exception { public uint Pid { get; } diff --git a/Dalamud/EntryPoint.cs b/Dalamud/EntryPoint.cs index 37ecdb5a6..2dc11bf84 100644 --- a/Dalamud/EntryPoint.cs +++ b/Dalamud/EntryPoint.cs @@ -8,7 +8,7 @@ namespace Dalamud public void Run(IContext context, string rootDirectory) { - + // Current goal is to make just enough to run this function and see if it works. (as a proof of concept.. thing.) } } }