Merge pull request #3 from rewjx/pmm

update Astronauts to support new encryption
This commit is contained in:
Crsky 2022-02-06 04:56:17 +08:00 committed by GitHub
commit 1bae108109
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -41,19 +41,39 @@ namespace GameRes.Formats.Astronauts
public override bool IsHierarchic { get { return true; } }
public override bool CanWrite { get { return false; } }
private bool UseNameAsKey = false;
static readonly byte[] KnownKey = {
0x40, 0x21, 0x28, 0x38, 0xA6, 0x6E, 0x43, 0xA5, 0x40, 0x21, 0x28, 0x38, 0xA6, 0x43, 0xA5, 0x64,
0x3E, 0x65, 0x24, 0x20, 0x46, 0x6E, 0x74,
};
public override ArcFile TryOpen (ArcView file)
public override ArcFile TryOpen(ArcView file)
{
UseNameAsKey = false;
ArcFile arc = TryOpenGxpFile(file);
if(null == arc)
{
UseNameAsKey = true; //try use arc name as key
arc = TryOpenGxpFile(file);
}
return arc;
}
private ArcFile TryOpenGxpFile (ArcView file)
{
int count = file.View.ReadInt32 (0x18);
if (!IsSaneCount (count))
return null;
return null;
string arcname = Path.GetFileName(file.Name);
byte[] arcname_bytes = Encoding.ASCII.GetBytes(arcname);
long base_offset = file.View.ReadInt64 (0x28);
uint entry_key = KnownKey[0] | (1u^KnownKey[1]) << 8 | (2u^KnownKey[2]) << 16 | (3u^KnownKey[3]) << 24;
uint entry_key = KnownKey[0] | (1u ^ KnownKey[1]) << 8 | (2u ^ KnownKey[2]) << 16 | (3u ^ KnownKey[3]) << 24;
if (UseNameAsKey)
{
uint arcname_key = (uint)(arcname_bytes[0] | (arcname_bytes[1 % arcname_bytes.Length]) << 8 | (arcname_bytes[2 % arcname_bytes.Length]) << 16 | (arcname_bytes[3 % arcname_bytes.Length]) << 24);
entry_key ^= arcname_key;
}
uint index_offset = 0x30;
var entry_buffer = new byte[0x100];
var dir = new List<Entry> (count);
@ -66,7 +86,10 @@ namespace GameRes.Formats.Astronauts
entry_buffer = new byte[entry_length];
if (entry_length != file.View.Read (index_offset, entry_buffer, 0, entry_length))
return null;
Decrypt (entry_buffer, entry_length);
if (UseNameAsKey)
Decrypt(entry_buffer, entry_length, arcname_bytes);
else
Decrypt(entry_buffer, entry_length);
int name_length = LittleEndian.ToInt32 (entry_buffer, 0xC) * 2; // length in characters
if (name_length >= entry_length)
return null;
@ -85,15 +108,25 @@ namespace GameRes.Formats.Astronauts
public override Stream OpenEntry (ArcFile arc, Entry entry)
{
var data = arc.File.View.ReadBytes (entry.Offset, entry.Size);
Decrypt (data, entry.Size);
if (UseNameAsKey)
{
string arcname = Path.GetFileName(arc.File.Name);
byte[] arcname_bytes = Encoding.ASCII.GetBytes(arcname);
Decrypt(data, entry.Size, arcname_bytes);
}
else
Decrypt(data, entry.Size);
return new BinMemoryStream (data, entry.Name);
}
static void Decrypt (byte[] data, uint length)
static void Decrypt (byte[] data, uint length, byte[] key=null)
{
for (uint i = 0; i < length; ++i)
{
data[i] ^= (byte)(i ^ KnownKey[i % KnownKey.Length]);
byte xorkey = (byte)(i ^ KnownKey[i % KnownKey.Length]);
if(null != key)
xorkey ^= key[i % key.Length];
data[i] ^= xorkey;
}
}
}