static void Main(string[] args) { // TODO: Add extra option so that the checksums become invalid? Or that you can overwrite them with a own value? bool result = true; if (args.Length < 2) { Console.WriteLine("bin-archiver version {0}", GitVersionRetreiver.getVersion()); Console.WriteLine("Archives one or more binary files into a single file. You can find help and usage examples on: https://github.com/Sunib/bin-archiver"); } else { // Using enumerator so that we can read more parameters when we need them inside the loop. var enumerator = args.Cast <string>().GetEnumerator(); ArchiveAppender archiveAppender = new ArchiveAppender(); string archiveFilename = null; result = Process(enumerator, out archiveFilename, archiveAppender); if (result) { if (archiveAppender.Finish()) { Console.WriteLine("Succesfully written bytes to {0}", archiveFilename); } } else { Console.WriteLine("Program terminating."); } } }
/// <summary> /// Prints some debug info about a bin file. /// </summary> public static void PrintBinFile(BinFile binFile) { Console.WriteLine("Valid BinFile found (version={0}, size={1}, time={2})", binFile.version, binFile.size, Helpers.convertUnixTimeMillisecondsToDateTime(binFile.time).ToString()); Console.WriteLine("File can be applied to type values: "); binFile.typeValues.ForEach(v => Console.Write("{0} ", v)); Console.Write("\n"); var savedCrc = binFile.crc; var calculatedCrc = ArchiveAppender.calculateCrc32(binFile.content); var savedSha = Convert.ToBase64String(binFile.sha1); var calculatedSha = Convert.ToBase64String(ArchiveAppender.calculateSha1(binFile.content)); Console.WriteLine("Hashes: CRC-32={0}, SHA-1={1}, Check={2}", savedCrc, savedSha, binFile.check); Console.WriteLine("Expected hashes: CRC-32={0}, SHA-1={1}, Check=3141592653", calculatedCrc, calculatedSha); if (savedCrc != calculatedCrc) { Console.WriteLine("CRC ERROR!!!"); } if (savedSha != calculatedSha) { Console.WriteLine("SHA1 ERROR!!!"); } }
static bool Process(IEnumerator <string> parameters, out string archiveFilename, ArchiveAppender archiveAppender) { bool fileExists = false; bool result = true; archiveFilename = null; while (parameters.MoveNext() && result) { var arg = parameters.Current; if (archiveFilename == null) { // Check if the file exists and then start using it! archiveFilename = arg.ToString(); fileExists = File.Exists(archiveFilename); } else { if (arg == "-c") { if (parameters.MoveNext()) { if (!fileExists) { if (CreateArchive(archiveFilename, parameters.Current)) { fileExists = true; archiveAppender.Prepare(archiveFilename); } else { Console.WriteLine("Failed to create archive for unknown reason."); result = false; } } else { Console.WriteLine("Cannot create archive, file already exists!"); } } else { Console.WriteLine("Failed to create archive, version string not given."); result = false; } } else if (arg == "-o") { if (fileExists) { File.Delete(archiveFilename); fileExists = File.Exists(archiveFilename); if (fileExists) { Console.WriteLine("Failed to remove file, locked?"); result = false; } } } else if (arg == "-k") { if (parameters.MoveNext()) { archiveAppender.Key = Convert.FromBase64String(parameters.Current); } else { result = false; } } else if (arg == "-r") { if (parameters.MoveNext()) { archiveAppender.RestrictToValue = Convert.ToUInt32(parameters.Current); } else { result = false; } } else if (arg == "-a") { if (parameters.MoveNext()) { archiveAppender.Filename = parameters.Current; if (parameters.MoveNext()) { archiveAppender.Version = parameters.Current; // Read the type values from the command line until you can't parse it anymore. archiveAppender.TypeValues = new List <UInt32>(); bool isReading = true; while (isReading && parameters.MoveNext()) { UInt32 value; if (UInt32.TryParse(parameters.Current, out value)) { archiveAppender.TypeValues.Add(value); } else { Console.WriteLine("Multiple actions in one go not yet supported."); archiveAppender.TypeValues.Clear(); isReading = false; result = false; } } if (archiveAppender.TypeValues.Count > 0) { archiveAppender.Prepare(archiveFilename); if (archiveAppender.Append()) { Console.WriteLine("Appended {0} to {1}", archiveAppender.Filename, archiveFilename); } else { Console.WriteLine("Failed to append {0} to {1}", archiveAppender.Filename, archiveFilename); result = false; } } else { Console.WriteLine("Append failed, no valid type values found"); result = false; } } else { Console.WriteLine("Append failed, version not given"); result = false; } } else { Console.WriteLine("Append failed, filename not given"); result = false; } } else if (arg == "-d") { TestArchive(archiveFilename, archiveAppender.Key); } else { Console.WriteLine("Unknown/invalid input parameter {0}", arg); } } } return(result); }