diff --git a/BuildIRD/BuildIRD.csproj b/BuildIRD/BuildIRD.csproj index 9b87868..0e2902a 100644 --- a/BuildIRD/BuildIRD.csproj +++ b/BuildIRD/BuildIRD.csproj @@ -2,7 +2,7 @@ Exe - net48;net6.0;net7.0 + net6.0 win-x86;win-x64;linux-x64;osx-x64 0.1 diff --git a/LibIRD/IRD.cs b/LibIRD/IRD.cs index df07eec..da246b9 100644 --- a/LibIRD/IRD.cs +++ b/LibIRD/IRD.cs @@ -145,30 +145,23 @@ namespace LibIRD throw new ArgumentException("Disc Key must be a byte array of length 16", nameof(key)); // AES decryption - using (Aes aes = Aes.Create()) - { - // Validate aes is available - if (aes == null) - throw new InvalidOperationException("AES not available. Change your system settings"); + using Aes aes = Aes.Create() ?? throw new InvalidOperationException("AES not available. Change your system settings"); - // Set AES settings - aes.Key = D1AesKey; - aes.IV = D1AesIV; - aes.Padding = PaddingMode.None; - aes.Mode = CipherMode.CBC; + // Set AES settings + aes.Key = D1AesKey; + aes.IV = D1AesIV; + aes.Padding = PaddingMode.None; + aes.Mode = CipherMode.CBC; - // Perform AES decryption - using (ICryptoTransform decryptor = aes.CreateDecryptor()) - { - MemoryStream ms = new MemoryStream(); - CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write); - cs.Write(key, 0, 16); - cs.FlushFinalBlock(); - Data1Key = ms.ToArray(); - ms.Close(); - cs.Close(); - } - } + // Perform AES decryption + using ICryptoTransform decryptor = aes.CreateDecryptor(); + MemoryStream ms = new(); + CryptoStream cs = new(ms, decryptor, CryptoStreamMode.Write); + cs.Write(key, 0, 16); + cs.FlushFinalBlock(); + Data1Key = ms.ToArray(); + ms.Close(); + cs.Close(); } /// @@ -186,30 +179,23 @@ namespace LibIRD throw new ArgumentException("Disc ID must be a byte array of length 16", nameof(id)); // AES encryption - using (Aes aes = Aes.Create()) - { - // Validate aes is available - if (aes == null) - throw new InvalidOperationException("AES not available. Change your system settings"); + using Aes aes = Aes.Create() ?? throw new InvalidOperationException("AES not available. Change your system settings"); - // Set AES settings - aes.Key = D2AesKey; - aes.IV = D2AesIV; - aes.Padding = PaddingMode.None; - aes.Mode = CipherMode.CBC; + // Set AES settings + aes.Key = D2AesKey; + aes.IV = D2AesIV; + aes.Padding = PaddingMode.None; + aes.Mode = CipherMode.CBC; - // Perform AES encryption - using (ICryptoTransform encryptor = aes.CreateEncryptor()) - { - MemoryStream ms = new MemoryStream(); - CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write); - cs.Write(id, 0, 16); - cs.FlushFinalBlock(); - Data2Key = ms.ToArray(); - ms.Close(); - cs.Close(); - } - } + // Perform AES encryption + using ICryptoTransform encryptor = aes.CreateEncryptor(); + MemoryStream ms = new(); + CryptoStream cs = new(ms, encryptor, CryptoStreamMode.Write); + cs.Write(id, 0, 16); + cs.FlushFinalBlock(); + Data2Key = ms.ToArray(); + ms.Close(); + cs.Close(); } #endregion @@ -270,7 +256,7 @@ namespace LibIRD if (line == null) throw new InvalidDataException("Could not find Disc Key in .getkey.log"); // Get Disc Key from log - string discKeyStr = line.Substring("disc_key = ".Length); + string discKeyStr = line["disc_key = ".Length..]; // Validate Disc Key from log if (discKeyStr.Length != 32) throw new InvalidDataException("Unexpected Disc Key in .getkey.log"); @@ -282,12 +268,12 @@ namespace LibIRD if (line == null) throw new InvalidDataException("Could not find Disc ID in .getkey.log"); // Get Disc ID from log - string discIDStr = line.Substring("disc_id = ".Length); + string discIDStr = line["disc_id = ".Length..]; // Validate Disc ID from log if (discIDStr.Length != 32) throw new InvalidDataException("Unexpected Disc ID in .getkey.log"); // Replace X's in Disc ID with 00000001 - discIDStr = discIDStr.Substring(0, 24) + "00000001"; + discIDStr = discIDStr[..24] + "00000001"; // Convert Disc ID to byte array discID = Utilities.HexToBytes(discIDStr); @@ -303,7 +289,7 @@ namespace LibIRD if (discPICStr.Length != 256) throw new InvalidDataException("Unexpected PIC in .getkey.log"); // Convert PIC to byte array - discPIC = Utilities.HexToBytes(discPICStr.Substring(0, 230)); + discPIC = Utilities.HexToBytes(discPICStr[..230]); // Check for warnings in .getkey.log while ((line = sr.ReadLine()) != null && line.Trim().StartsWith("WARNING") == false && line.Trim().StartsWith("SUCCESS") == false) @@ -337,10 +323,10 @@ namespace LibIRD throw new ArgumentNullException(nameof(irdPath)); // Create new stream to write to - Stream stream = new MemoryStream(); + MemoryStream stream = new(); // Write IRD data to stream in order - using (BinaryWriter bw = new BinaryWriter(stream, Encoding.UTF8, true)) + using (BinaryWriter bw = new(stream, Encoding.UTF8, true)) { // IRD File Signature bw.Write(Magic); @@ -426,7 +412,7 @@ namespace LibIRD // Calculate the little-endian 32-bit "IEEE 802.3" CRC value of the entire stream stream.Position = 0; - Crc32 crc32 = new Crc32(); + Crc32 crc32 = new(); crc32.Append(stream); byte[] crc = crc32.GetCurrentHash(); @@ -434,17 +420,13 @@ namespace LibIRD stream.Write(crc, 0, 4); // Create the IRD file stream - using (FileStream fs = new FileStream(irdPath, FileMode.Create, FileAccess.Write)) - { - // Create a GZipped IRD file stream - using (GZipStream gzStream = new GZipStream(fs, CompressionLevel.Optimal)) - { - // Write entire gzipped IRD stream to file - stream.Position = 0; - stream.CopyTo(gzStream); - stream.Close(); - } - } + using FileStream fs = new(irdPath, FileMode.Create, FileAccess.Write); + // Create a GZipped IRD file stream + using GZipStream gzStream = new(fs, CompressionLevel.SmallestSize); + // Write entire gzipped IRD stream to file + stream.Position = 0; + stream.CopyTo(gzStream); + stream.Close(); } #endregion diff --git a/LibIRD/LibIRD.csproj b/LibIRD/LibIRD.csproj index e5e2b9d..72b691c 100644 --- a/LibIRD/LibIRD.csproj +++ b/LibIRD/LibIRD.csproj @@ -1,7 +1,7 @@ - net48;net6.0;net7.0 + net6.0 win-x86;win-x64;linux-x64;osx-x64 0.1 diff --git a/LibIRD/PS3ISO.cs b/LibIRD/PS3ISO.cs index 7147df3..e69e4fc 100644 --- a/LibIRD/PS3ISO.cs +++ b/LibIRD/PS3ISO.cs @@ -3,7 +3,6 @@ using DiscUtils.Iso9660; using System; using System.IO; using System.IO.Compression; -using System.Linq; using System.Text; namespace LibIRD @@ -138,7 +137,7 @@ namespace LibIRD /// /// /// - private void GetSystemVersion(Stream fs, CDReader reader) + private void GetSystemVersion(FileStream fs, CDReader reader) { //DiscUtils.Streams.StreamExtent[] a = reader.PathToExtents("\\PS3_UPDATE\\PS3UPDAT.PUP"); @@ -205,94 +204,89 @@ namespace LibIRD FileHashes[i] = NullMD5; // Process ISO file - using (Stream fs = new FileStream(isoPath, FileMode.Open, FileAccess.Read)) + using FileStream fs = new FileStream(isoPath, FileMode.Open, FileAccess.Read) ?? throw new FileNotFoundException(isoPath); + // Validate ISO file + if (!CDReader.Detect(fs)) + throw new InvalidDataException("Not a valid ISO file"); + + // New ISO Reader from DiscUtils + CDReader reader = new(fs, true, true); + + // Read PS3 Metadata from PARAM.SFO + using (DiscUtils.Streams.SparseStream s = reader.OpenFile("PS3_GAME\\PARAM.SFO", FileMode.Open, FileAccess.Read)) { - // Validate ISO filestream - if (fs == null) - throw new ArgumentNullException("Failed to open ISO"); - if (!CDReader.Detect(fs)) - throw new InvalidDataException("Not a valid ISO file"); - - // New ISO Reader from DiscUtils - CDReader reader = new CDReader(fs, true, true); - - // Read PS3 Metadata from PARAM.SFO - using (Stream s = reader.OpenFile("PS3_GAME\\PARAM.SFO", FileMode.Open, FileAccess.Read)) - { - // Parse PARAM.SFO file - ParamSFO paramSFO = new ParamSFO(s); - - // Store required values for IRD - TitleID = paramSFO["TITLE_ID"]; - Title = paramSFO["TITLE"]; - GameVersion = paramSFO["VERSION"]; - AppVersion = paramSFO["APP_VER"]; - } - - // Determine system update version - GetSystemVersion(fs, reader); - - - // Determine the extent of the Header (Sector 0 to first data sector) - //DiscUtils.Streams.Range clusters = reader.PathToClusters("\\").First(); - //long firstSector = clusters.Offset; - //long totalSectors = (long) Math.Ceiling(clusters.Count / 2048.0); - - // Compress the header and store - DiscUtils.Streams.Range[] sfbClusters = reader.PathToClusters("\\PS3_DISC.SFB"); - long firstSector = sfbClusters[0] != null ? sfbClusters[0].Offset : 0; - using (MemoryStream headerStream = new MemoryStream()) - { - using (GZipStream gzs = new GZipStream(headerStream, CompressionLevel.Optimal)) - { - fs.Seek(0, SeekOrigin.Begin); - byte[] buf = new byte[2048]; - // Read all sectors before the first data sector - for (int i = 0; i < firstSector; i++) - { - int numBytes = fs.Read(buf, 0, buf.Length); - gzs.Write(buf, 0, numBytes); - } - } - Header = headerStream.ToArray(); - HeaderLength = (uint) Header.Length; - } - - // Compress the footer and store - DiscUtils.Streams.StreamExtent[] updateBytes = reader.PathToExtents("\\PS3_UPDATE\\PS3UPDAT.PUP"); - // TODO: check if updateBytes array is not empty? - long lastByte = updateBytes[updateBytes.Length - 1].Start + updateBytes[updateBytes.Length - 1].Length; - using (MemoryStream footerStream = new MemoryStream()) - { - using (GZipStream gzs = new GZipStream(footerStream, CompressionLevel.Optimal)) - { - // Start saving data from after last file - fs.Seek(lastByte, SeekOrigin.Begin); - byte[] buf = new byte[2048]; - int numBytes = 2048; - // Keep reading data until there is none left to read - while (numBytes != 0) - { - numBytes = fs.Read(buf, 0, buf.Length); - gzs.Write(buf, 0, numBytes); - } - } - Footer = footerStream.ToArray(); - FooterLength = (uint) Footer.Length; - } - - - - // Get info on root directory - //DiscDirectoryInfo rootDir = reader.GetDirectoryInfo("\\"); - // Recursively process all subdirectories - ParseDir(reader, "\\"); - - // Calculate last sector - //DiscDirectoryInfo lastFile = reader. - //lastSector = lastFile.StartSector + lastFile.TotalSectors; + // Parse PARAM.SFO file + ParamSFO paramSFO = new(s); + // Store required values for IRD + TitleID = paramSFO["TITLE_ID"]; + Title = paramSFO["TITLE"]; + GameVersion = paramSFO["VERSION"]; + AppVersion = paramSFO["APP_VER"]; } + + // Determine system update version + GetSystemVersion(fs, reader); + + + // Determine the extent of the Header (Sector 0 to first data sector) + //DiscUtils.Streams.Range clusters = reader.PathToClusters("\\").First(); + //long firstSector = clusters.Offset; + //long totalSectors = (long) Math.Ceiling(clusters.Count / 2048.0); + + // Compress the header and store + DiscUtils.Streams.Range[] sfbClusters = reader.PathToClusters("\\PS3_DISC.SFB"); + long firstSector = sfbClusters[0] != null ? sfbClusters[0].Offset : 0; + using (MemoryStream headerStream = new()) + { + using (GZipStream gzs = new(headerStream, CompressionLevel.SmallestSize)) + { + fs.Seek(0, SeekOrigin.Begin); + byte[] buf = new byte[2048]; + // Read all sectors before the first data sector + for (int i = 0; i < firstSector; i++) + { + int numBytes = fs.Read(buf, 0, buf.Length); + gzs.Write(buf, 0, numBytes); + } + } + Header = headerStream.ToArray(); + HeaderLength = (uint)Header.Length; + } + + // Compress the footer and store + DiscUtils.Streams.StreamExtent[] updateBytes = reader.PathToExtents("\\PS3_UPDATE\\PS3UPDAT.PUP"); + long lastByte = updateBytes[^1].Start + updateBytes[^1].Length; + using (MemoryStream footerStream = new()) + { + using (GZipStream gzs = new(footerStream, CompressionLevel.SmallestSize)) + { + // Start saving data from after last file + fs.Seek(lastByte, SeekOrigin.Begin); + byte[] buf = new byte[2048]; + int numBytes = 2048; + // Keep reading data until there is none left to read + while (numBytes != 0) + { + numBytes = fs.Read(buf, 0, buf.Length); + gzs.Write(buf, 0, numBytes); + } + } + Footer = footerStream.ToArray(); + FooterLength = (uint)Footer.Length; + } + + + + // Get info on root directory + //DiscDirectoryInfo rootDir = reader.GetDirectoryInfo("\\"); + // Recursively process all subdirectories + ParseDir(reader, "\\"); + + // Calculate last sector + //DiscDirectoryInfo lastFile = reader. + //lastSector = lastFile.StartSector + lastFile.TotalSectors; + } } } diff --git a/PrintParams/PrintParams.csproj b/PrintParams/PrintParams.csproj index 0b9508c..0e2902a 100644 --- a/PrintParams/PrintParams.csproj +++ b/PrintParams/PrintParams.csproj @@ -1,8 +1,8 @@ - + Exe - net48;net6.0;net7.0 + net6.0 win-x86;win-x64;linux-x64;osx-x64 0.1