diff --git a/README.md b/README.md index 7a1c2d2..2cd4ea9 100644 --- a/README.md +++ b/README.md @@ -19,19 +19,23 @@ The library can be used in any .NET application built with `net462`, `netcoreapp **Obfuscation** ```csharp -using var vpax = File.Open(@"C:\path\to\file.vpax", FileMode.Open); +using var vpax = new MemoryStream(File.ReadAllBytes(@"C:\path\to\unobfuscated.vpax")); var obfuscator = new VpaxObfuscator(); var dictionary = obfuscator.Obfuscate(vpax); -dictionary.WriteTo(@"C:\path\to\dictionary.json"); + +dictionary.WriteTo(@"C:\path\to\dictionary.dict"); +File.WriteAllBytes(@"C:\path\to\obfuscated.ovpax", vpax.ToArray()); ``` **Deobfuscation** ```csharp -using var vpax = File.Open(@"C:\path\to\obfuscated.vpax", FileMode.Open); -var dictionary = ObfuscationDictionary.ReadFrom(@"C:\path\to\dictionary.json"); +using var vpax = new MemoryStream(File.ReadAllBytes(@"C:\path\to\obfuscated.ovpax")); +var dictionary = ObfuscationDictionary.ReadFrom(@"C:\path\to\dictionary.dict"); var obfuscator = new VpaxObfuscator(); obfuscator.Deobfuscate(vpax, dictionary); + +File.WriteAllBytes(@"C:\path\to\deobfuscated.vpax", vpax.ToArray()); ``` **Incremental Obfuscation** @@ -39,11 +43,13 @@ obfuscator.Deobfuscate(vpax, dictionary); > Incremental obfuscation keeps the same obfuscated names across different VPAX versions of the same model. ```csharp -using var vpax = File.Open(@"C:\path\to\file.vpax", FileMode.Open); -var dictionaryV1 = ObfuscationDictionary.ReadFrom(@"C:\path\to\dictionary-v1.json"); +using var vpax = new MemoryStream(File.ReadAllBytes(@"C:\path\to\unobfuscated-v2.vpax")); +var dictionaryV1 = ObfuscationDictionary.ReadFrom(@"C:\path\to\dictionary-v1.dict"); var obfuscator = new VpaxObfuscator(); -var dictionaryV2 = obfuscator.Obfuscate(vpax, dictionaryV1); -dictionaryV2.WriteTo(@"C:\path\to\dictionary-v2.json"); +var dictionary = obfuscator.Obfuscate(vpax, dictionaryV1); + +dictionary.WriteTo(@"C:\path\to\dictionary-v2.dict"); +File.WriteAllBytes(@"C:\path\to\obfuscated-v2.ovpax", vpax.ToArray()); ``` ## CLI @@ -66,11 +72,11 @@ Usage: vpax-obfuscator obfuscate [options] Options: - --vpax (REQUIRED) Path to the unobfuscated VPAX file. - --dictionary Path to the dictionary file to be used for incremental obfuscation. If not provided, a new dictionary will be created. - --output-vpax (REQUIRED) Path to write the obfuscated VPAX file. - --output-dictionary (REQUIRED) Path to write the obfuscation dictionary file. - --track-unobfuscated Specifies whether to include unobfuscated values in the output dictionary. - --allow-overwrite Allow output files to be overwritten. If not provided, the command will fail if an output file already exists. - -?, -h, --help Show help and usage information + --vpax (REQUIRED) Path to the unobfuscated VPAX file. + --dictionary Path to the dictionary file to be used for incremental obfuscation. If not provided, a new dictionary will be created. + --output-vpax Path to write the obfuscated VPAX file. If not provided, the file will be written to the same folder as the '--vpax' file, using the default file extension for obfuscated VPAX files, which is '.ovpax'. + --output-dictionary Path to write the obfuscation dictionary file. If not provided, the file will be written to the same folder as the '--vpax' file, using the default file extension for obfuscation dictionary files, which is '.dict'. + --track-unobfuscated Specifies whether to include unobfuscated values in the output dictionary. + --allow-overwrite Allow output files to be overwritten. If not provided, the command will fail if an output file already exists. + -?, -h, --help Show help and usage information ``` diff --git a/src/Dax.Vpax.Obfuscator.CLI/Program.cs b/src/Dax.Vpax.Obfuscator.CLI/Program.cs index 0902bfe..90b56b5 100644 --- a/src/Dax.Vpax.Obfuscator.CLI/Program.cs +++ b/src/Dax.Vpax.Obfuscator.CLI/Program.cs @@ -35,13 +35,13 @@ private static Command BuildCommandObfuscate(Option allowOverwriteOption) }; var outputVpaxOption = new Option(name: "--output-vpax") { - Description = "Path to write the obfuscated VPAX file.", - IsRequired = true, + Description = "Path to write the obfuscated VPAX file. If not provided, the file will be written to the same folder as the '--vpax' file, using the default file extension for obfuscated VPAX files, which is '.ovpax'.", + IsRequired = false, }; var outputDictionaryOption = new Option(name: "--output-dictionary") { - Description = "Path to write the obfuscation dictionary file.", - IsRequired = true, + Description = "Path to write the obfuscation dictionary file. If not provided, the file will be written to the same folder as the '--vpax' file, using the default file extension for obfuscation dictionary files, which is '.dict'.", + IsRequired = false, }; var trackUnobfuscatedOption = new Option(name: "--track-unobfuscated") { @@ -73,8 +73,8 @@ private static Command BuildCommandDeobfuscate(Option allowOverwriteOption }; var outputVpaxOption = new Option(name: "--output-vpax") { - Description = "Path to write the deobfuscated VPAX file.", - IsRequired = true, + Description = "Path to write the deobfuscated VPAX file. If not provided, the file will be written to the same folder as the '--vpax' file, using the default file extension for unobfuscated VPAX files, which is '.vpax'.", + IsRequired = false, }; var command = new Command("deobfuscate", "Deobfuscate the DaxModel.json file from an obfuscated VPAX file using the provided dictionary."); command.AddOption(vpaxOption); @@ -84,7 +84,7 @@ private static Command BuildCommandDeobfuscate(Option allowOverwriteOption return command; } - private static void Obfuscate(FileInfo vpaxFile, FileInfo? dictionaryOption, FileInfo outputVpaxFile, FileInfo outputDictionaryFile, bool allowOverwrite, bool? trackUnobfuscated) + private static void Obfuscate(FileInfo vpaxFile, FileInfo? dictionaryOption, FileInfo? outputVpaxFile, FileInfo? outputDictionaryFile, bool allowOverwrite, bool? trackUnobfuscated) { using var stream = Read(vpaxFile); var obfuscator = new VpaxObfuscator(); @@ -96,18 +96,22 @@ private static void Obfuscate(FileInfo vpaxFile, FileInfo? dictionaryOption, Fil if (obfuscator.Options.TrackUnobfuscated) NotifyUnobfuscated(outputDictionary); + + var dictPath = outputDictionaryFile?.FullName ?? Path.ChangeExtension(vpaxFile.FullName, ".dict"); + var ovpaxPath = outputVpaxFile?.FullName ?? Path.ChangeExtension(vpaxFile.FullName, ".ovpax"); - outputDictionary.WriteTo(outputDictionaryFile.FullName, overwrite: allowOverwrite, indented: true); - Write(stream, outputVpaxFile, allowOverwrite); + outputDictionary.WriteTo(dictPath, overwrite: allowOverwrite, indented: true); + Write(stream, ovpaxPath, allowOverwrite); } private static void Deobfuscate(FileInfo vpaxFile, FileInfo dictionaryFile, FileInfo outputVpaxFile, bool allowOverwrite) { using var stream = Read(vpaxFile); var obfuscator = new VpaxObfuscator(); + var ovpaxPath = outputVpaxFile?.FullName ?? Path.ChangeExtension(vpaxFile.FullName, ".vpax"); obfuscator.Deobfuscate(stream, dictionary: ObfuscationDictionary.ReadFrom(dictionaryFile.FullName)); - Write(stream, outputVpaxFile, allowOverwrite); + Write(stream, ovpaxPath, allowOverwrite); } private static MemoryStream Read(FileInfo file) @@ -118,11 +122,11 @@ private static MemoryStream Read(FileInfo file) return stream; } - private static void Write(MemoryStream stream, FileInfo file, bool allowOverwrite) + private static void Write(MemoryStream stream, string path, bool allowOverwrite) { var bytes = stream.ToArray(); var mode = allowOverwrite ? FileMode.Create : FileMode.CreateNew; - using var fileStream = new FileStream(file.FullName, mode, FileAccess.Write, FileShare.Read); + using var fileStream = new FileStream(path, mode, FileAccess.Write, FileShare.Read); fileStream.Write(bytes, 0, bytes.Length); }