(fPK): implemented archives version 2.

This commit is contained in:
morkt
2017-11-09 00:57:32 +04:00
parent dcedc79678
commit cb536f040e

View File

@@ -2,7 +2,7 @@
//! \date Mon Sep 12 01:39:49 2016 //! \date Mon Sep 12 01:39:49 2016
//! \brief 'fPK' resource archive. //! \brief 'fPK' resource archive.
// //
// Copyright (C) 2016 by morkt // Copyright (C) 2016-2017 by morkt
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy // Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to // of this software and associated documentation files (the "Software"), to
@@ -44,76 +44,96 @@ namespace GameRes.Formats.Ivory
public override bool IsHierarchic { get { return true; } } public override bool IsHierarchic { get { return true; } }
public override bool CanWrite { get { return false; } } public override bool CanWrite { get { return false; } }
public PakOpener ()
{
Signatures = new uint[] { 0x204B5066, 0x324B5066 };
}
public override ArcFile TryOpen (ArcView file) public override ArcFile TryOpen (ArcView file)
{ {
if (file.MaxOffset != file.View.ReadUInt32 (4)) int version = '2' == file.View.ReadByte (3) ? 2 : 1;
return null;
long base_offset = 0; long base_offset = 0;
List<Entry> dir = null; List<Entry> dir = null;
bool got_names = false; byte[] names = null;
long offset = 8; using (var stream = file.CreateStream())
while (offset < file.MaxOffset)
{ {
var id = new AsciiString (file.View.ReadBytes (offset, 4)); Func<IBinaryStream, long> read_long;
uint section_size = file.View.ReadUInt32 (offset+4); if (2 == version)
if (0 == section_size) read_long = s => s.ReadInt64();
else
read_long = s => s.ReadUInt32();
stream.Position = 4;
if (file.MaxOffset != read_long (stream))
return null; return null;
uint header_size = file.View.ReadUInt32 (offset+8); for (;;)
{
long section_start = stream.Position;
var id_bytes = stream.ReadBytes (4);
if (0 == id_bytes.Length)
break;
var id = new AsciiString (id_bytes);
var section_size = read_long (stream);
var header_size = read_long (stream);
if (section_size < 4 || header_size > section_size)
return null;
var content_pos = section_start + header_size;
if ("cLST" == id) if ("cLST" == id)
{ {
int count = file.View.ReadInt32 (offset+0x10); stream.ReadUInt32();
int count = stream.ReadInt32();
if (!IsSaneCount (count)) if (!IsSaneCount (count))
return null; return null;
uint key = file.View.ReadUInt32 (offset+0x14); uint key = stream.ReadUInt32();
var clst = file.View.ReadBytes (offset+header_size, section_size - header_size); stream.Position = content_pos;
var clst = stream.ReadBytes ((int)(section_size - header_size));
Decrypt (clst, key); Decrypt (clst, key);
dir = new List<Entry> (count); dir = new List<Entry> (count);
int index_offset = 0; using (var index = new BinMemoryStream (clst))
{
for (int i = 0; i < count; ++i) for (int i = 0; i < count; ++i)
{ {
var entry = new PkEntry { var entry = new PkEntry {
NameOffset = LittleEndian.ToInt32 (clst, index_offset), NameOffset = (int)read_long (index),
Offset = LittleEndian.ToUInt32 (clst, index_offset+4), Offset = (long)read_long (index),
Size = LittleEndian.ToUInt32 (clst, index_offset+8), Size = (uint)read_long (index),
}; };
dir.Add (entry); dir.Add (entry);
index_offset += 12; }
} }
} }
else if ("cNAM" == id) else if ("cNAM" == id)
{ {
if (null == dir) if (null == dir)
return null; return null;
uint key = file.View.ReadUInt32 (offset+0x10); stream.ReadUInt32();
var cnam = file.View.ReadBytes (offset+header_size, section_size - header_size); uint key = stream.ReadUInt32();
Decrypt (cnam, key); stream.Position = content_pos;
names = stream.ReadBytes ((int)(section_size - header_size));
Decrypt (names, key);
}
else if ("cDAT" == id)
{
base_offset = content_pos;
}
stream.Position = section_start + section_size;
}
}
if (null == dir || null == names || 0 == base_offset)
return null;
foreach (PkEntry entry in dir) foreach (PkEntry entry in dir)
{ {
var name = Binary.GetCString (cnam, entry.NameOffset); entry.Offset += base_offset;
if (!entry.CheckPlacement (file.MaxOffset))
return null;
var name = Binary.GetCString (names, entry.NameOffset);
entry.Name = name; entry.Name = name;
if (name.HasExtension (".px")) if (name.HasExtension (".px"))
entry.Type = "audio"; entry.Type = "audio";
else else
entry.Type = FormatCatalog.Instance.GetTypeFromName (name); entry.Type = FormatCatalog.Instance.GetTypeFromName (name);
} }
got_names = true;
}
else if ("cDAT" == id)
{
base_offset = offset + header_size;
}
offset += section_size;
}
if (null == dir || !got_names || 0 == base_offset)
return null;
foreach (var entry in dir)
{
entry.Offset += base_offset;
if (!entry.CheckPlacement (file.MaxOffset))
return null;
}
return new ArcFile (file, this, dir); return new ArcFile (file, this, dir);
} }