diff --git a/SabreTools.Library/DatFiles/DatFile.cs b/SabreTools.Library/DatFiles/DatFile.cs index 4316a5c9..224ac556 100644 --- a/SabreTools.Library/DatFiles/DatFile.cs +++ b/SabreTools.Library/DatFiles/DatFile.cs @@ -3614,7 +3614,8 @@ namespace SabreTools.Library.DatFiles // Special case for if we are in Romba mode (all names are supposed to be SHA-1 hashes) if (Romba) { - Rom rom = ArchiveTools.GetTorrentGZFileInfo(item); + GZipArchive archive = new GZipArchive(item); + Rom rom = archive.GetTorrentGZFileInfo(); // If the rom is valid, write it out if (rom != null && rom.Name != null) @@ -4014,7 +4015,8 @@ namespace SabreTools.Library.DatFiles } // If we have a path, we want to try to get the rom information - Rom fileinfo = ArchiveTools.GetTorrentGZFileInfo(foundpath); + GZipArchive archive = new GZipArchive(foundpath); + Rom fileinfo = archive.GetTorrentGZFileInfo(); // If the file information is null, then we continue if (fileinfo == null) @@ -4225,7 +4227,8 @@ namespace SabreTools.Library.DatFiles usedInternally = true; // Get the TGZ status for later - bool isTorrentGzip = (ArchiveTools.GetTorrentGZFileInfo(file) != null); + GZipArchive tgz = new GZipArchive(file); + bool isTorrentGzip = tgz.IsTorrent(); // Get the base archive first BaseArchive archive = ArchiveTools.CreateArchiveFromExistingInput(file); @@ -4319,7 +4322,9 @@ namespace SabreTools.Library.DatFiles } // If we have a very specifc TGZ->TGZ case, just copy it accordingly - if (isZip == false && ArchiveTools.GetTorrentGZFileInfo(file) != null && outputFormat == OutputFormat.TorrentGzip) + GZipArchive tgz = new GZipArchive(file); + Rom rom = tgz.GetTorrentGZFileInfo(); + if (isZip == false && rom != null && outputFormat == OutputFormat.TorrentGzip) { // Get the proper output path if (romba) @@ -4399,7 +4404,9 @@ namespace SabreTools.Library.DatFiles string machinename = null; // If we have a very specifc TGZ->TGZ case, just copy it accordingly - if (isZip == false && ArchiveTools.GetTorrentGZFileInfo(file) != null && outputFormat == OutputFormat.TorrentGzip) + GZipArchive tgz = new GZipArchive(file); + Rom rom = tgz.GetTorrentGZFileInfo(); + if (isZip == false && rom != null && outputFormat == OutputFormat.TorrentGzip) { // Get the proper output path if (romba) @@ -4674,7 +4681,8 @@ namespace SabreTools.Library.DatFiles } // If we have a path, we want to try to get the rom information - Rom fileinfo = ArchiveTools.GetTorrentGZFileInfo(foundpath); + GZipArchive tgz = new GZipArchive(foundpath); + Rom fileinfo = tgz.GetTorrentGZFileInfo(); // If the file information is null, then we continue if (fileinfo == null) diff --git a/SabreTools.Library/FileTypes/BaseArchive.cs b/SabreTools.Library/FileTypes/BaseArchive.cs index 39858144..4986dd84 100644 --- a/SabreTools.Library/FileTypes/BaseArchive.cs +++ b/SabreTools.Library/FileTypes/BaseArchive.cs @@ -90,6 +90,11 @@ namespace SabreTools.Library.FileTypes /// List of empty folders in the archive public abstract List GetEmptyFolders(); + /// + /// Check whether the input file is a standardized format + /// + public abstract bool IsTorrent(); + #endregion #region Writing diff --git a/SabreTools.Library/FileTypes/Folder.cs b/SabreTools.Library/FileTypes/Folder.cs index 0f8ceb53..12c03f94 100644 --- a/SabreTools.Library/FileTypes/Folder.cs +++ b/SabreTools.Library/FileTypes/Folder.cs @@ -182,6 +182,14 @@ namespace SabreTools.Library.FileTypes throw new NotImplementedException(); } + /// + /// Check whether the input file is a standardized format + /// + public override bool IsTorrent() + { + throw new NotImplementedException(); + } + #endregion #region Writing diff --git a/SabreTools.Library/FileTypes/GZipArchive.cs b/SabreTools.Library/FileTypes/GZipArchive.cs index dde26fab..59d59c2e 100644 --- a/SabreTools.Library/FileTypes/GZipArchive.cs +++ b/SabreTools.Library/FileTypes/GZipArchive.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Text.RegularExpressions; using SabreTools.Library.Data; using SabreTools.Library.Items; @@ -198,7 +199,7 @@ namespace SabreTools.Library.FileTypes List found = new List(); string gamename = Path.GetFileNameWithoutExtension(_filename); - Rom possibleTgz = ArchiveTools.GetTorrentGZFileInfo(_filename); + Rom possibleTgz = GetTorrentGZFileInfo(); // If it was, then add it to the outputs and continue if (possibleTgz != null && possibleTgz.Name != null) @@ -255,6 +256,156 @@ namespace SabreTools.Library.FileTypes return new List(); } + /// + /// Check whether the input file is a standardized format + /// + public override bool IsTorrent() + { + // Check for the file existing first + if (!File.Exists(_filename)) + { + return false; + } + + string datum = Path.GetFileName(_filename).ToLowerInvariant(); + long filesize = new FileInfo(_filename).Length; + + // If we have the romba depot files, just skip them gracefully + if (datum == ".romba_size" || datum == ".romba_size.backup") + { + Globals.Logger.Verbose("Romba depot file found, skipping: {0}", _filename); + return false; + } + + // Check if the name is the right length + if (!Regex.IsMatch(datum, @"^[0-9a-f]{" + Constants.SHA1Length + @"}\.gz")) // TODO: When updating to SHA-256, this needs to update to Constants.SHA256Length + { + Globals.Logger.Warning("Non SHA-1 filename found, skipping: '{0}'", Path.GetFullPath(_filename)); + return false; + } + + // Check if the file is at least the minimum length + if (filesize < 40 /* bytes */) + { + Globals.Logger.Warning("Possibly corrupt file '{0}' with size {1}", Path.GetFullPath(_filename), Style.GetBytesReadable(filesize)); + return false; + } + + // Get the Romba-specific header data + byte[] header; // Get preamble header for checking + byte[] headermd5; // MD5 + byte[] headercrc; // CRC + ulong headersz; // Int64 size + BinaryReader br = new BinaryReader(FileTools.TryOpenRead(_filename)); + header = br.ReadBytes(12); + headermd5 = br.ReadBytes(16); + headercrc = br.ReadBytes(4); + headersz = br.ReadUInt64(); + br.Dispose(); + + // If the header is not correct, return a blank rom + bool correct = true; + for (int i = 0; i < header.Length; i++) + { + // This is a temp fix to ignore the modification time and OS until romba can be fixed + if (i == 4 || i == 5 || i == 6 || i == 7 || i == 9) + { + continue; + } + correct &= (header[i] == Constants.TorrentGZHeader[i]); + } + if (!correct) + { + return false; + } + + return true; + } + + /// + /// Retrieve file information for a single torrent GZ file + /// + /// Populated DatItem object if success, empty one on error + public Rom GetTorrentGZFileInfo() + { + // Check for the file existing first + if (!File.Exists(_filename)) + { + return null; + } + + string datum = Path.GetFileName(_filename).ToLowerInvariant(); + long filesize = new FileInfo(_filename).Length; + + // If we have the romba depot files, just skip them gracefully + if (datum == ".romba_size" || datum == ".romba_size.backup") + { + Globals.Logger.Verbose("Romba depot file found, skipping: {0}", _filename); + return null; + } + + // Check if the name is the right length + if (!Regex.IsMatch(datum, @"^[0-9a-f]{" + Constants.SHA1Length + @"}\.gz")) // TODO: When updating to SHA-256, this needs to update to Constants.SHA256Length + { + Globals.Logger.Warning("Non SHA-1 filename found, skipping: '{0}'", Path.GetFullPath(_filename)); + return null; + } + + // Check if the file is at least the minimum length + if (filesize < 40 /* bytes */) + { + Globals.Logger.Warning("Possibly corrupt file '{0}' with size {1}", Path.GetFullPath(_filename), Style.GetBytesReadable(filesize)); + return null; + } + + // Get the Romba-specific header data + byte[] header; // Get preamble header for checking + byte[] headermd5; // MD5 + byte[] headercrc; // CRC + ulong headersz; // Int64 size + BinaryReader br = new BinaryReader(FileTools.TryOpenRead(_filename)); + header = br.ReadBytes(12); + headermd5 = br.ReadBytes(16); + headercrc = br.ReadBytes(4); + headersz = br.ReadUInt64(); + br.Dispose(); + + // If the header is not correct, return a blank rom + bool correct = true; + for (int i = 0; i < header.Length; i++) + { + // This is a temp fix to ignore the modification time and OS until romba can be fixed + if (i == 4 || i == 5 || i == 6 || i == 7 || i == 9) + { + continue; + } + correct &= (header[i] == Constants.TorrentGZHeader[i]); + } + if (!correct) + { + return null; + } + + // Now convert the data and get the right position + string gzmd5 = BitConverter.ToString(headermd5).Replace("-", string.Empty); + string gzcrc = BitConverter.ToString(headercrc).Replace("-", string.Empty); + long extractedsize = (long)headersz; + + Rom rom = new Rom + { + Type = ItemType.Rom, + Name = Path.GetFileNameWithoutExtension(_filename).ToLowerInvariant(), + Size = extractedsize, + CRC = gzcrc.ToLowerInvariant(), + MD5 = gzmd5.ToLowerInvariant(), + SHA1 = Path.GetFileNameWithoutExtension(_filename).ToLowerInvariant(), // TODO: When updating to SHA-256, this needs to update to SHA256 + + MachineName = Path.GetFileNameWithoutExtension(_filename).ToLowerInvariant(), + }; + + return rom; + } + #endregion #region Writing diff --git a/SabreTools.Library/FileTypes/LRZArchive.cs b/SabreTools.Library/FileTypes/LRZArchive.cs index 597eff77..06fb54d7 100644 --- a/SabreTools.Library/FileTypes/LRZArchive.cs +++ b/SabreTools.Library/FileTypes/LRZArchive.cs @@ -102,6 +102,14 @@ namespace SabreTools.Library.FileTypes throw new NotImplementedException(); } + /// + /// Check whether the input file is a standardized format + /// + public override bool IsTorrent() + { + throw new NotImplementedException(); + } + #endregion #region Writing diff --git a/SabreTools.Library/FileTypes/RarArchive.cs b/SabreTools.Library/FileTypes/RarArchive.cs index 972abbb8..6c288969 100644 --- a/SabreTools.Library/FileTypes/RarArchive.cs +++ b/SabreTools.Library/FileTypes/RarArchive.cs @@ -275,6 +275,14 @@ namespace SabreTools.Library.FileTypes return empties; } + /// + /// Check whether the input file is a standardized format + /// + public override bool IsTorrent() + { + throw new NotImplementedException(); + } + #endregion #region Writing diff --git a/SabreTools.Library/FileTypes/SevenZipArchive.cs b/SabreTools.Library/FileTypes/SevenZipArchive.cs index c7b98ce8..e9a58d3e 100644 --- a/SabreTools.Library/FileTypes/SevenZipArchive.cs +++ b/SabreTools.Library/FileTypes/SevenZipArchive.cs @@ -280,6 +280,48 @@ namespace SabreTools.Library.FileTypes return empties; } + /// + /// Check whether the input file is a standardized format + /// + /// TODO: Finish reading T7z information + public override bool IsTorrent() + { + bool ist7z = false; + + if (File.Exists(_filename)) + { + try + { + Stream fread = FileTools.TryOpenRead(_filename); + uint ar, offs = 0; + fread.Seek(0, SeekOrigin.Begin); + byte[] buffer = new byte[128]; + ar = (uint)fread.Read(buffer, 0, 4 + Constants.Torrent7ZipSignature.Length + 4); + if (ar < (4 + Constants.Torrent7ZipSignature.Length + 4)) + { + if (ar >= Constants.Torrent7ZipSignature.Length + 4) + { + ar -= (uint)(Constants.Torrent7ZipSignature.Length + 4); + } + if (ar <= Constants.Torrent7ZipHeader.Length) + { + ar = (uint)Constants.Torrent7ZipHeader.Length; + } + // memset(buffer+offs+ar,0,crcsz-ar) + } + + fread.Dispose(); + } + catch + { + Globals.Logger.Warning("File '{0}' could not be opened", _filename); + ist7z = false; + } + } + + return ist7z; + } + #endregion #region Writing diff --git a/SabreTools.Library/FileTypes/TapeArchive.cs b/SabreTools.Library/FileTypes/TapeArchive.cs index 98361715..154b9265 100644 --- a/SabreTools.Library/FileTypes/TapeArchive.cs +++ b/SabreTools.Library/FileTypes/TapeArchive.cs @@ -278,6 +278,14 @@ namespace SabreTools.Library.FileTypes return empties; } + /// + /// Check whether the input file is a standardized format + /// + public override bool IsTorrent() + { + throw new NotImplementedException(); + } + #endregion #region Writing diff --git a/SabreTools.Library/FileTypes/TorrentZipArchive.cs b/SabreTools.Library/FileTypes/TorrentZipArchive.cs index ad40fb72..b9da405a 100644 --- a/SabreTools.Library/FileTypes/TorrentZipArchive.cs +++ b/SabreTools.Library/FileTypes/TorrentZipArchive.cs @@ -382,6 +382,14 @@ namespace SabreTools.Library.FileTypes return empties; } + /// + /// Check whether the input file is a standardized format + /// + public override bool IsTorrent() + { + throw new NotImplementedException(); + } + #endregion #region Writing diff --git a/SabreTools.Library/FileTypes/XZArchive.cs b/SabreTools.Library/FileTypes/XZArchive.cs index 4ad4a09c..f99c6afc 100644 --- a/SabreTools.Library/FileTypes/XZArchive.cs +++ b/SabreTools.Library/FileTypes/XZArchive.cs @@ -111,6 +111,14 @@ namespace SabreTools.Library.FileTypes throw new NotImplementedException(); } + /// + /// Check whether the input file is a standardized format + /// + public override bool IsTorrent() + { + throw new NotImplementedException(); + } + #endregion #region Writing diff --git a/SabreTools.Library/Tools/ArchiveTools.cs b/SabreTools.Library/Tools/ArchiveTools.cs index c31c359c..4e36a249 100644 --- a/SabreTools.Library/Tools/ArchiveTools.cs +++ b/SabreTools.Library/Tools/ArchiveTools.cs @@ -131,91 +131,6 @@ namespace SabreTools.Library.Tools #region Information - /// - /// Retrieve file information for a single torrent GZ file - /// - /// Filename to get information from - /// Populated DatItem object if success, empty one on error - public static Rom GetTorrentGZFileInfo(string input) - { - // Check for the file existing first - if (!File.Exists(input)) - { - return null; - } - - string datum = Path.GetFileName(input).ToLowerInvariant(); - long filesize = new FileInfo(input).Length; - - // If we have the romba depot files, just skip them gracefully - if (datum == ".romba_size" || datum == ".romba_size.backup") - { - Globals.Logger.Verbose("Romba depot file found, skipping: {0}", input); - return null; - } - - // Check if the name is the right length - if (!Regex.IsMatch(datum, @"^[0-9a-f]{" + Constants.SHA1Length + @"}\.gz")) // TODO: When updating to SHA-256, this needs to update to Constants.SHA256Length - { - Globals.Logger.Warning("Non SHA-1 filename found, skipping: '{0}'", Path.GetFullPath(input)); - return null; - } - - // Check if the file is at least the minimum length - if (filesize < 40 /* bytes */) - { - Globals.Logger.Warning("Possibly corrupt file '{0}' with size {1}", Path.GetFullPath(input), Style.GetBytesReadable(filesize)); - return null; - } - - // Get the Romba-specific header data - byte[] header; // Get preamble header for checking - byte[] headermd5; // MD5 - byte[] headercrc; // CRC - ulong headersz; // Int64 size - BinaryReader br = new BinaryReader(FileTools.TryOpenRead(input)); - header = br.ReadBytes(12); - headermd5 = br.ReadBytes(16); - headercrc = br.ReadBytes(4); - headersz = br.ReadUInt64(); - br.Dispose(); - - // If the header is not correct, return a blank rom - bool correct = true; - for (int i = 0; i < header.Length; i++) - { - // This is a temp fix to ignore the modification time and OS until romba can be fixed - if (i == 4 || i == 5 || i == 6 || i == 7 || i == 9) - { - continue; - } - correct &= (header[i] == Constants.TorrentGZHeader[i]); - } - if (!correct) - { - return null; - } - - // Now convert the data and get the right position - string gzmd5 = BitConverter.ToString(headermd5).Replace("-", string.Empty); - string gzcrc = BitConverter.ToString(headercrc).Replace("-", string.Empty); - long extractedsize = (long)headersz; - - Rom rom = new Rom - { - Type = ItemType.Rom, - Name = Path.GetFileNameWithoutExtension(input).ToLowerInvariant(), - Size = extractedsize, - CRC = gzcrc.ToLowerInvariant(), - MD5 = gzmd5.ToLowerInvariant(), - SHA1 = Path.GetFileNameWithoutExtension(input).ToLowerInvariant(), // TODO: When updating to SHA-256, this needs to update to SHA256 - - MachineName = Path.GetFileNameWithoutExtension(input).ToLowerInvariant(), - }; - - return rom; - } - /// /// Returns the archive type of an input file /// @@ -621,49 +536,6 @@ namespace SabreTools.Library.Tools } } - /// - /// (INCOMPLETE) Get the T7Z status of the file - /// - /// Name of the file to check - /// 0 if the file isn't 7z, 1 if the file is t7z, 2 if the file is 7z - public static int IsT7z(string filename) - { - int ist7z = 0; - - if (File.Exists(filename)) - { - try - { - Stream fread = FileTools.TryOpenRead(filename); - uint ar, offs = 0; - fread.Seek(0, SeekOrigin.Begin); - byte[] buffer = new byte[128]; - ar = (uint)fread.Read(buffer, 0, 4 + Constants.Torrent7ZipSignature.Length + 4); - if (ar < (4 + Constants.Torrent7ZipSignature.Length + 4)) - { - if (ar >= Constants.Torrent7ZipSignature.Length + 4) - { - ar -= (uint)(Constants.Torrent7ZipSignature.Length + 4); - } - if (ar <= Constants.Torrent7ZipHeader.Length) - { - ar = (uint)Constants.Torrent7ZipHeader.Length; - } - // memset(buffer+offs+ar,0,crcsz-ar) - } - - fread.Dispose(); - } - catch - { - Globals.Logger.Warning("File '{0}' could not be opened", filename); - ist7z = 0; - } - } - - return ist7z; - } - #endregion } }