NekoPack archives are actually hierarchical.

Instead of directory and file names, their hash sums are stored. Hash is
one-way function, so reconstructing names is impossible unless there's
some predefined set of widely-used names, like "script", "bgm" etc.
This commit is contained in:
morkt 2015-03-26 06:52:45 +04:00
parent 0442de677a
commit 60dedf0328

View File

@ -49,7 +49,7 @@ namespace GameRes.Formats.Neko
public override string Tag { get { return "NEKO"; } } public override string Tag { get { return "NEKO"; } }
public override string Description { get { return "NekoPack resource archive"; } } public override string Description { get { return "NekoPack resource archive"; } }
public override uint Signature { get { return 0x4f4b454e; } } // "NEKO" public override uint Signature { get { return 0x4f4b454e; } } // "NEKO"
public override bool IsHierarchic { get { return false; } } public override bool IsHierarchic { get { return true; } }
public override bool CanCreate { get { return false; } } public override bool CanCreate { get { return false; } }
public PakOpener () public PakOpener ()
@ -73,6 +73,7 @@ namespace GameRes.Formats.Neko
int count = LittleEndian.ToInt32 (index, index_pos+4); int count = LittleEndian.ToInt32 (index, index_pos+4);
if (count <= 0 || count > remaining/8-1) if (count <= 0 || count > remaining/8-1)
return null; return null;
uint hash = LittleEndian.ToUInt32 (index, index_pos);
total += count; total += count;
dir.Capacity = total; dir.Capacity = total;
index_pos += 8; index_pos += 8;
@ -136,6 +137,52 @@ namespace GameRes.Formats.Neko
return result | (result ^ (v3 + 1812433253u)) << 32; return result | (result ^ (v3 + 1812433253u)) << 32;
} }
static uint HashFromString (uint seed, byte[] str, int offset, int length)
{
// 0x00000000, "NEKOPACK" -> 0xAC0BF0B1
// 0xAC0BF0B1, "RBS0011" -> 0x9B75ADA7
uint result = seed;
for (int i = 0; i < length; ++i)
{
byte c = str[offset+i];
result += c + result * 37;
}
return result;
}
static uint HashFromName (uint seed, byte[] str, int offset, int length)
{
// 0x9B75ADA7, "script" -> 0x0DDB021E
// 0x9B75ADA7, "start.bin" -> 0xCB8FB53B
uint result = seed;
for (int i = 0; i < length; ++i)
{
byte c = str[offset+i];
result = 81 * (ShiftMap[c] ^ result);
}
return result;
}
static readonly byte[] ShiftMap = new byte[]
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x38, 0x2F, 0x33, 0x3C, 0x40, 0x3B, 0x2A, 0x2E, 0x31, 0x30, 0x26, 0x44, 0x35, 0x28, 0x3E, 0x12,
0x02, 0x22, 0x06, 0x20, 0x1A, 0x1C, 0x0F, 0x11, 0x18, 0x17, 0x42, 0x2B, 0x3A, 0x37, 0x34, 0x0C,
0x41, 0x08, 0x1D, 0x07, 0x15, 0x21, 0x05, 0x1E, 0x0A, 0x14, 0x0E, 0x10, 0x09, 0x27, 0x1F, 0x0B,
0x23, 0x16, 0x0D, 0x01, 0x25, 0x04, 0x1B, 0x03, 0x13, 0x24, 0x19, 0x2D, 0x12, 0x29, 0x32, 0x3F,
0x3D, 0x08, 0x1D, 0x07, 0x15, 0x21, 0x05, 0x1E, 0x0A, 0x14, 0x0E, 0x10, 0x09, 0x27, 0x1F, 0x0B,
0x23, 0x16, 0x0D, 0x01, 0x25, 0x04, 0x1B, 0x03, 0x13, 0x24, 0x19, 0x2C, 0x39, 0x43, 0x36, 0x00,
0x4B, 0xA9, 0xA7, 0xAF, 0x50, 0x52, 0x91, 0x9F, 0x47, 0x6B, 0x96, 0xAB, 0x87, 0xB5, 0x9B, 0xBB,
0x99, 0xA4, 0xBF, 0x5C, 0xC6, 0x9C, 0xC2, 0xC4, 0xB6, 0x4F, 0xB8, 0xC1, 0x85, 0xA8, 0x51, 0x7E,
0x5F, 0x82, 0x73, 0xC7, 0x90, 0x4E, 0x45, 0xA5, 0x7A, 0x63, 0x70, 0xB3, 0x79, 0x83, 0x60, 0x55,
0x5B, 0x5E, 0x68, 0xBA, 0x53, 0xA1, 0x67, 0x97, 0xAC, 0x71, 0x81, 0x59, 0x64, 0x7C, 0x9D, 0xBD,
0x9D, 0xBD, 0x95, 0xA0, 0xB2, 0xC0, 0x6F, 0x6A, 0x54, 0xB9, 0x6D, 0x88, 0x77, 0x48, 0x5D, 0x72,
0x49, 0x93, 0x57, 0x65, 0xBE, 0x4A, 0x80, 0xA2, 0x5A, 0x98, 0xA6, 0x62, 0x7F, 0x84, 0x75, 0xBC,
0xAD, 0xB1, 0x6E, 0x76, 0x8B, 0x9E, 0x8C, 0x61, 0x69, 0x8D, 0xB4, 0x78, 0xAA, 0xAE, 0x8F, 0xC3,
0x58, 0xC5, 0x74, 0xB7, 0x8E, 0x7D, 0x89, 0x8A, 0x56, 0x4D, 0x86, 0x94, 0x9A, 0x4C, 0x92, 0xB0,
};
static uint CalcParity (uint a1, uint a2) static uint CalcParity (uint a1, uint a2)
{ {
uint v1 = (a2 ^ ((a2 ^ ((a2 ^ ((a2 ^ a1) + 1566083941u)) - 899497514u)) - 1894007588u)) + 1812433253u; uint v1 = (a2 ^ ((a2 ^ ((a2 ^ ((a2 ^ a1) + 1566083941u)) - 899497514u)) - 1894007588u)) + 1812433253u;
@ -179,6 +226,9 @@ namespace GameRes.Formats.Neko
{ {
uint hash = view.ReadUInt32 (offset); uint hash = view.ReadUInt32 (offset);
length = view.ReadInt32 (offset+4); length = view.ReadInt32 (offset+4);
// parity check
// if (CalcParity (id, (uint)length) != hash)
// throw new InvalidFormatException();
int aligned_size = (length+7) & ~7; int aligned_size = (length+7) & ~7;
byte[] buffer = new byte[aligned_size]; byte[] buffer = new byte[aligned_size];