Add loading encrypted pkm files

Also fix 220 byte pkm file loading
This commit is contained in:
Kaphotics
2016-05-20 18:55:03 -07:00
parent eb519f683f
commit 8a4ebf461b
2 changed files with 18 additions and 2 deletions

View File

@@ -64,7 +64,7 @@ internal static byte[] shuffleArray(byte[] data, uint sv)
// Fill the Battle Stats back
if (data.Length > 136)
Array.Copy(data, 136, sdata, 136, 100);
Array.Copy(data, 136, sdata, 136, data.Length - 136);
return sdata;
}
@@ -120,6 +120,21 @@ internal static byte[] encryptArray(byte[] pkm)
// Done
return ekm;
}
/// <summary>
/// Checks to see if the PKM file is encrypted; if so, decrypts.
/// </summary>
/// <param name="pkm">Input byte array</param>
internal static void checkEncrypted(ref byte[] pkm)
{
if (pkm.Length != PK4.SIZE_STORED && pkm.Length != PK4.SIZE_PARTY && pkm.Length != PK5.SIZE_PARTY)
return; // bad
ushort chk = 0;
for (int i = 8; i < PK4.SIZE_STORED; i += 2) // Loop through the entire PKM
chk += BitConverter.ToUInt16(pkm, i);
pkm = chk != BitConverter.ToUInt16(pkm, 0x06) ? decryptArray(pkm) : pkm;
}
/// <summary>Calculates the CRC16-CCITT checksum over an input byte array.</summary>
/// <param name="chunk">Input byte array</param>

View File

@@ -617,8 +617,9 @@ private void openFile(byte[] input, string path, string ext)
#region PK3/PK4/PK5 Conversion
else if (new[] { PK3.SIZE_PARTY, PK3.SIZE_STORED, PK4.SIZE_PARTY, PK4.SIZE_STORED, PK5.SIZE_PARTY }.Contains(input.Length))
{
PKM.checkEncrypted(ref input);
if (!PKX.verifychk(input)) Util.Error("Invalid File (Checksum Error)");
try // to convert g5pkm
try // to convert pk6
{
populateFields(Converter.ConvertPKMtoPK6(input));
}