[ArchiveTools] Fix size handling for TGZ

This commit is contained in:
Matt Nadareski
2016-08-17 16:17:10 -07:00
parent cefd9135d8
commit d4342059c2

View File

@@ -507,7 +507,7 @@ namespace SabreTools.Helper
{ {
string datum = Path.GetFileName(input).ToLowerInvariant(); string datum = Path.GetFileName(input).ToLowerInvariant();
long filesize = new FileInfo(input).Length; long filesize = new FileInfo(input).Length;
// Check if the name is the right length // Check if the name is the right length
if (!Regex.IsMatch(datum, @"^[0-9a-f]{40}\.gz")) if (!Regex.IsMatch(datum, @"^[0-9a-f]{40}\.gz"))
{ {
@@ -516,32 +516,31 @@ namespace SabreTools.Helper
} }
// Check if the file is at least the minimum length // Check if the file is at least the minimum length
if (filesize < 36 /* bytes */) if (filesize < 40 /* bytes */)
{ {
logger.Warning("Possibly corrupt file '" + input + "' with size " + Style.GetBytesReadable(filesize)); logger.Warning("Possibly corrupt file '" + input + "' with size " + Style.GetBytesReadable(filesize));
return new Rom(); return new Rom();
} }
// Get the Romba-specific header data // Get the Romba-specific header data
byte[] header; // MD5 and CRC byte[] headermd5; // MD5
byte[] headersz; // MSB of long-size byte[] headercrc; // CRC
byte[] footer; // Internal CRC and isize byte[] headersz; // Int64 size
using (FileStream itemstream = File.OpenRead(input)) using (FileStream itemstream = File.OpenRead(input))
{ {
using (BinaryReader br = new BinaryReader(itemstream)) using (BinaryReader br = new BinaryReader(itemstream))
{ {
header = br.ReadBytes(32); br.BaseStream.Seek(12, SeekOrigin.Begin);
headersz = br.ReadBytes(4); headermd5 = br.ReadBytes(16);
br.BaseStream.Seek(-4, SeekOrigin.End); headercrc = br.ReadBytes(4);
footer = br.ReadBytes(4); headersz = br.ReadBytes(8);
} }
} }
// Now convert the data and get the right positions // Now convert the data and get the right position
string headerstring = BitConverter.ToString(header).Replace("-", string.Empty); string gzmd5 = BitConverter.ToString(headermd5).Replace("-", string.Empty);
string gzmd5 = headerstring.Substring(24, 32); string gzcrc = BitConverter.ToString(headercrc).Replace("-", string.Empty);
string gzcrc = headerstring.Substring(56, 8); string gzsize = BitConverter.ToString(headersz.Reverse().ToArray()).Replace("-", string.Empty);
string gzsize = BitConverter.ToString(headersz.Reverse().ToArray()).Replace("-", string.Empty) + BitConverter.ToString(footer.Reverse().ToArray()).Replace("-", string.Empty);
long extractedsize = Convert.ToInt64(gzsize, 16); long extractedsize = Convert.ToInt64(gzsize, 16);
Rom rom = new Rom Rom rom = new Rom