123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- using ArcToolkitCLI.Commands.Options;
- using ArcToolkitCLI.Util;
- using COM3D2.Toolkit.Arc;
- using CommandLine;
- namespace ArcToolkitCLI.Commands
- {
- [Verb("decrypt", HelpText = "Decrypts the provided WARP files")]
- public class DecryptCommand : ICommand, IInputOptions, IDecryptionOptions, IOutputOptions
- {
- public int Run()
- {
- var errCode = Encryption.GetDecryptionKeys(this, out var keysDict);
- if (errCode != 0)
- return errCode;
- Directory.CreateDirectory(Output);
- foreach (var file in Glob.EnumerateFiles(Input))
- {
- if (!file.Exists)
- return Errors.Error(Errors.ErrorCodes.NotAFile, file.Name);
- using var stream = File.OpenRead(file.FullName);
- var header = new byte[4];
- stream.Read(header, 0, header.Length);
- var headerString = Encoding.ASCII.GetString(header);
- stream.Position = 0;
- if (headerString == "warc")
- Console.WriteLine($"{file.Name} is a WARC file, skipping...");
- else if (headerString == "warp")
- try
- {
- var arcStream = WarpArc.DecryptWarp(stream, requestedFile =>
- {
- if (keysDict.TryGetValue("*", out var key))
- return key;
- if (keysDict.TryGetValue(requestedFile, out key))
- return key;
- if (Directory.Exists(ArcDirectory) &&
- File.Exists(Path.Combine(ArcDirectory, requestedFile)))
- return Encryption.ReadKeyFromFile(Path.Combine(ArcDirectory, requestedFile));
- if (File.Exists(WarcFile))
- return Encryption.ReadKeyFromFile(WarcFile);
- throw new FileNotFoundException("No key found for the requested ARC", requestedFile);
- });
- Console.WriteLine($"Writing {Path.Combine(Output, file.Name)} with length {arcStream.Length}");
- using (var output = File.Create(Path.Combine(Output, file.Name)))
- arcStream.CopyTo(output);
- }
- catch (FileNotFoundException fe)
- {
- return Errors.Error(Errors.ErrorCodes.NoCorrectKey, file.Name, fe.FileName);
- }
- else
- return Errors.Error(Errors.ErrorCodes.UnknownArcType, file.Name);
- }
- return 0;
- }
- public string ArcDirectory { get; set; }
- public string DecryptionKey { get; set; }
- public string KeyFile { get; set; }
- public string WarcFile { get; set; }
- public IEnumerable<string> Input { get; set; }
- public string Output { get; set; }
- }
- }
|