diff --git a/SabreTools.FileTypes/Aaru/AaruFormat.cs b/SabreTools.FileTypes/Aaru/AaruFormat.cs index 3861a162..c4bda79c 100644 --- a/SabreTools.FileTypes/Aaru/AaruFormat.cs +++ b/SabreTools.FileTypes/Aaru/AaruFormat.cs @@ -2,6 +2,7 @@ using System.Text; using SabreTools.IO.Extensions; using SabreTools.Matching; +using static SabreTools.FileTypes.Constants; namespace SabreTools.FileTypes.Aaru { diff --git a/SabreTools.FileTypes/Archives/GZipArchive.cs b/SabreTools.FileTypes/Archives/GZipArchive.cs index 7577326f..9e17263a 100644 --- a/SabreTools.FileTypes/Archives/GZipArchive.cs +++ b/SabreTools.FileTypes/Archives/GZipArchive.cs @@ -239,7 +239,7 @@ namespace SabreTools.FileTypes.Archives var gz = new gZip(); ZipReturn ret = gz.ZipFileOpen(Filename); ret = gz.ZipFileOpenReadStream(0, out Stream? gzstream, out ulong streamSize); - gzipEntryRom = GetInfo(gzstream, hashes: _hashTypes); + gzipEntryRom = FileTypeTool.GetInfo(gzstream, hashes: _hashTypes); gzipEntryRom.Filename = gz.GetLocalFile(0).Filename; gzipEntryRom.Parent = gamename; gzipEntryRom.Date = (gz.TimeStamp > 0 ? gz.TimeStamp.ToString() : null); @@ -284,7 +284,7 @@ namespace SabreTools.FileTypes.Archives } // Check if the name is the right length - if (!Regex.IsMatch(datum, @"^[0-9a-f]{" + Constants.SHA1Length + @"}\.gz")) + if (!Regex.IsMatch(datum, @"^[0-9a-f]{" + Hashing.Constants.SHA1Length + @"}\.gz")) { _logger.Warning($"Non SHA-1 filename found, skipping: '{Path.GetFullPath(Filename)}'"); return false; @@ -345,7 +345,7 @@ namespace SabreTools.FileTypes.Archives } // Check if the name is the right length - if (!Regex.IsMatch(datum, @"^[0-9a-f]{" + Constants.SHA1Length + @"}\.gz")) + if (!Regex.IsMatch(datum, @"^[0-9a-f]{" + Hashing.Constants.SHA1Length + @"}\.gz")) { _logger.Warning($"Non SHA-1 filename found, skipping: '{Path.GetFullPath(Filename)}'"); return null; @@ -440,7 +440,7 @@ namespace SabreTools.FileTypes.Archives outDir = Path.GetFullPath(outDir); // Now get the Rom info for the file so we have hashes and size - baseFile = GetInfo(inputStream, keepReadOpen: true); + baseFile = FileTypeTool.GetInfo(inputStream, keepReadOpen: true); // Get the output file name string outfile = Path.Combine(outDir, Utilities.GetDepotPath(baseFile.SHA1, Depth) ?? string.Empty); diff --git a/SabreTools.FileTypes/Archives/RarArchive.cs b/SabreTools.FileTypes/Archives/RarArchive.cs index 28af6221..e1e45321 100644 --- a/SabreTools.FileTypes/Archives/RarArchive.cs +++ b/SabreTools.FileTypes/Archives/RarArchive.cs @@ -210,7 +210,7 @@ namespace SabreTools.FileTypes.Archives else { using Stream entryStream = entry.OpenEntryStream(); - rarEntryRom = GetInfo(entryStream, size: entry.Size, hashes: _hashTypes); + rarEntryRom = FileTypeTool.GetInfo(entryStream, size: entry.Size, hashes: _hashTypes); } // Fill in common details and add to the list diff --git a/SabreTools.FileTypes/Archives/SevenZipArchive.cs b/SabreTools.FileTypes/Archives/SevenZipArchive.cs index 0a906779..66423c35 100644 --- a/SabreTools.FileTypes/Archives/SevenZipArchive.cs +++ b/SabreTools.FileTypes/Archives/SevenZipArchive.cs @@ -282,7 +282,7 @@ namespace SabreTools.FileTypes.Archives // Otherwise, use the stream directly else { - zipEntryRom = GetInfo(readStream, size: (long)zf.GetLocalFile(i).UncompressedSize, hashes: _hashTypes, keepReadOpen: true); + zipEntryRom = FileTypeTool.GetInfo(readStream, size: (long)zf.GetLocalFile(i).UncompressedSize, hashes: _hashTypes, keepReadOpen: true); } // Fill in common details and add to the list diff --git a/SabreTools.FileTypes/Archives/TapeArchive.cs b/SabreTools.FileTypes/Archives/TapeArchive.cs index b5f8ddb3..7542012f 100644 --- a/SabreTools.FileTypes/Archives/TapeArchive.cs +++ b/SabreTools.FileTypes/Archives/TapeArchive.cs @@ -203,7 +203,7 @@ namespace SabreTools.FileTypes.Archives else { using Stream entryStream = entry.OpenEntryStream(); - tarEntryRom = GetInfo(entryStream, size: entry.Size, hashes: _hashTypes); + tarEntryRom = FileTypeTool.GetInfo(entryStream, size: entry.Size, hashes: _hashTypes); } // Fill in common details and add to the list diff --git a/SabreTools.FileTypes/Archives/XZArchive.cs b/SabreTools.FileTypes/Archives/XZArchive.cs index 1516d00b..d1920c8c 100644 --- a/SabreTools.FileTypes/Archives/XZArchive.cs +++ b/SabreTools.FileTypes/Archives/XZArchive.cs @@ -2,7 +2,9 @@ using System.Collections.Generic; using System.IO; using System.Text.RegularExpressions; +#if NET462_OR_GREATER || NETCOREAPP using SabreTools.Hashing; +#endif using SabreTools.IO.Extensions; #if NET462_OR_GREATER || NETCOREAPP using SharpCompress.Compressors.Xz; @@ -213,7 +215,7 @@ namespace SabreTools.FileTypes.Archives else { var xzStream = new XZStream(File.OpenRead(Filename!)); - xzEntryRom = GetInfo(xzStream, hashes: _hashTypes); + xzEntryRom = FileTypeTool.GetInfo(xzStream, hashes: _hashTypes); xzEntryRom.Filename = gamename; xzStream.Dispose(); } @@ -252,7 +254,7 @@ namespace SabreTools.FileTypes.Archives string datum = Path.GetFileName(Filename).ToLowerInvariant(); // Check if the name is the right length - if (!Regex.IsMatch(datum, @"^[0-9a-f]{" + Constants.SHA1Length + @"}\.xz")) + if (!Regex.IsMatch(datum, @"^[0-9a-f]{" + Hashing.Constants.SHA1Length + @"}\.xz")) { _logger.Warning($"Non SHA-1 filename found, skipping: '{Path.GetFullPath(Filename)}'"); return false; @@ -274,7 +276,7 @@ namespace SabreTools.FileTypes.Archives string datum = Path.GetFileName(Filename).ToLowerInvariant(); // Check if the name is the right length - if (!Regex.IsMatch(datum, @"^[0-9a-f]{" + Constants.SHA1Length + @"}\.xz")) + if (!Regex.IsMatch(datum, @"^[0-9a-f]{" + Hashing.Constants.SHA1Length + @"}\.xz")) { _logger.Warning($"Non SHA-1 filename found, skipping: '{Path.GetFullPath(Filename)}'"); return null; @@ -328,7 +330,7 @@ namespace SabreTools.FileTypes.Archives outDir = Path.GetFullPath(outDir); // Now get the Rom info for the file so we have hashes and size - baseFile = GetInfo(inputStream, keepReadOpen: true); + baseFile = FileTypeTool.GetInfo(inputStream, keepReadOpen: true); // Get the output file name string outfile = Path.Combine(outDir, Core.Tools.Utilities.GetDepotPath(baseFile.SHA1, Depth)!); diff --git a/SabreTools.FileTypes/Archives/ZipArchive.cs b/SabreTools.FileTypes/Archives/ZipArchive.cs index 57fa6e62..a722f4f4 100644 --- a/SabreTools.FileTypes/Archives/ZipArchive.cs +++ b/SabreTools.FileTypes/Archives/ZipArchive.cs @@ -361,7 +361,7 @@ namespace SabreTools.FileTypes.Archives // Otherwise, use the stream directly else { - zipEntryRom = GetInfo(readStream, + zipEntryRom = FileTypeTool.GetInfo(readStream, size: (long)localFile.UncompressedSize, hashes: _hashTypes, keepReadOpen: true); @@ -418,7 +418,7 @@ namespace SabreTools.FileTypes.Archives // Otherwise, use the stream directly else { - zipEntryRom = GetInfo(readStream, + zipEntryRom = FileTypeTool.GetInfo(readStream, size: localFile.Length, hashes: _hashTypes, keepReadOpen: false); diff --git a/SabreTools.FileTypes/Constants.cs b/SabreTools.FileTypes/Constants.cs new file mode 100644 index 00000000..df3849ff --- /dev/null +++ b/SabreTools.FileTypes/Constants.cs @@ -0,0 +1,23 @@ +namespace SabreTools.FileTypes +{ + internal static class Constants + { + public static readonly byte[] SevenZipSignature = [0x37, 0x7a, 0xbc, 0xaf, 0x27, 0x1c]; + + public static readonly byte[] AaruFormatSignature = [0x41, 0x41, 0x52, 0x55, 0x46, 0x52, 0x4d, 0x54]; + + public static readonly byte[] CHDSignature = [0x4d, 0x43, 0x6f, 0x6d, 0x70, 0x72, 0x48, 0x44]; + + public static readonly byte[] GzSignature = [0x1f, 0x8b, 0x08]; + + public static readonly byte[] RarSignature = [0x52, 0x61, 0x72, 0x21, 0x1a, 0x07, 0x00]; + + public static readonly byte[] RarFiveSignature = [0x52, 0x61, 0x72, 0x21, 0x1a, 0x07, 0x01, 0x00]; + + public static readonly byte[] TarSignature = [0x75, 0x73, 0x74, 0x61, 0x72, 0x20, 0x20, 0x00]; + + public static readonly byte[] TarZeroSignature = [0x75, 0x73, 0x74, 0x61, 0x72, 0x00, 0x30, 0x30]; + + public static readonly byte[] XZSignature = [0xfd, 0x37, 0x7a, 0x58, 0x5a, 0x00, 0x00]; + } +} \ No newline at end of file diff --git a/SabreTools.FileTypes/FileTypeTool.cs b/SabreTools.FileTypes/FileTypeTool.cs index 9dc12cb7..ee3658e5 100644 --- a/SabreTools.FileTypes/FileTypeTool.cs +++ b/SabreTools.FileTypes/FileTypeTool.cs @@ -7,25 +7,12 @@ using SabreTools.Hashing; using SabreTools.IO.Extensions; using SabreTools.Matching; using SabreTools.Skippers; +using static SabreTools.FileTypes.Constants; namespace SabreTools.FileTypes { public static class FileTypeTool { - #region Constants - - private static readonly byte[] SevenZipSignature = [0x37, 0x7a, 0xbc, 0xaf, 0x27, 0x1c]; - private static readonly byte[] AaruFormatSignature = [0x41, 0x41, 0x52, 0x55, 0x46, 0x52, 0x4d, 0x54]; - private static readonly byte[] CHDSignature = [0x4d, 0x43, 0x6f, 0x6d, 0x70, 0x72, 0x48, 0x44]; - private static readonly byte[] GzSignature = [0x1f, 0x8b, 0x08]; - private static readonly byte[] RarSignature = [0x52, 0x61, 0x72, 0x21, 0x1a, 0x07, 0x00]; - private static readonly byte[] RarFiveSignature = [0x52, 0x61, 0x72, 0x21, 0x1a, 0x07, 0x01, 0x00]; - private static readonly byte[] TarSignature = [0x75, 0x73, 0x74, 0x61, 0x72, 0x20, 0x20, 0x00]; - private static readonly byte[] TarZeroSignature = [0x75, 0x73, 0x74, 0x61, 0x72, 0x00, 0x30, 0x30]; - private static readonly byte[] XZSignature = [0xfd, 0x37, 0x7a, 0x58, 0x5a, 0x00, 0x00]; - - #endregion - #region File Info ///