2015-02-26 08:22:54 +04:00
|
|
|
//! \file ArcNitro.cs
|
|
|
|
//! \date Wed Feb 25 20:28:14 2015
|
|
|
|
//! \brief Nitro+ PAK archives implementation.
|
|
|
|
//
|
2016-02-04 05:59:55 +04:00
|
|
|
// Copyright (C) 2015-2016 by morkt
|
2015-02-26 08:22:54 +04:00
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to
|
|
|
|
// deal in the Software without restriction, including without limitation the
|
|
|
|
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
// sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
// IN THE SOFTWARE.
|
|
|
|
//
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.ComponentModel.Composition;
|
|
|
|
using System.IO;
|
|
|
|
using System.Text;
|
2015-07-28 12:20:20 +04:00
|
|
|
using GameRes.Compression;
|
2015-02-26 08:22:54 +04:00
|
|
|
using GameRes.Utility;
|
|
|
|
|
|
|
|
namespace GameRes.Formats.NitroPlus
|
|
|
|
{
|
2016-10-14 12:35:56 +04:00
|
|
|
internal class PakEntry : PackedEntry
|
2015-02-26 08:22:54 +04:00
|
|
|
{
|
|
|
|
public uint Key;
|
|
|
|
}
|
|
|
|
|
2015-08-11 17:44:18 +04:00
|
|
|
internal class NitroPak : ArcFile
|
|
|
|
{
|
|
|
|
public int Version;
|
|
|
|
|
|
|
|
public NitroPak (ArcView arc, ArchiveFormat impl, ICollection<Entry> dir, int version)
|
|
|
|
: base (arc, impl, dir)
|
|
|
|
{
|
|
|
|
Version = version;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-26 08:22:54 +04:00
|
|
|
[Export(typeof(ArchiveFormat))]
|
|
|
|
public class PakOpener : ArchiveFormat
|
|
|
|
{
|
2015-07-25 10:10:36 +04:00
|
|
|
public override string Tag { get { return "PAK/NITRO+"; } }
|
2015-02-26 08:22:54 +04:00
|
|
|
public override string Description { get { return "Nitro+ resource archive"; } }
|
|
|
|
public override uint Signature { get { return 0x03; } }
|
2015-08-11 17:44:18 +04:00
|
|
|
public override bool IsHierarchic { get { return true; } }
|
2016-10-11 00:05:22 +04:00
|
|
|
public override bool CanWrite { get { return false; } }
|
2015-02-26 08:22:54 +04:00
|
|
|
|
|
|
|
public PakOpener ()
|
|
|
|
{
|
|
|
|
Extensions = new string[] { "pak" };
|
2015-08-11 17:44:18 +04:00
|
|
|
Signatures = new uint[] { 2, 3 };
|
2015-02-26 08:22:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public override ArcFile TryOpen (ArcView file)
|
|
|
|
{
|
2015-08-11 17:44:18 +04:00
|
|
|
int version = file.View.ReadInt32 (0);
|
|
|
|
List<Entry> dir = null;
|
|
|
|
if (2 == version)
|
|
|
|
dir = OpenPakV2 (file);
|
|
|
|
else if (3 == version)
|
|
|
|
dir = OpenPakV3 (file);
|
|
|
|
if (null == dir)
|
|
|
|
return null;
|
|
|
|
return new NitroPak (file, this, dir, version);
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<Entry> OpenPakV2 (ArcView file)
|
|
|
|
{
|
|
|
|
int count = file.View.ReadInt32 (4);
|
|
|
|
if (!IsSaneCount (count))
|
|
|
|
return null;
|
|
|
|
int unpacked_size = file.View.ReadInt32 (8);
|
|
|
|
uint packed_size = file.View.ReadUInt32 (0xC);
|
2016-05-21 10:31:09 +04:00
|
|
|
using (var input = file.CreateStream (0x114, packed_size))
|
2015-08-11 17:44:18 +04:00
|
|
|
using (var header_stream = new ZLibStream (input, CompressionMode.Decompress))
|
|
|
|
using (var header = new BinaryReader (header_stream, Encoding.ASCII, true))
|
|
|
|
{
|
|
|
|
long base_offset = 0x114 + packed_size;
|
|
|
|
var name_buf = new byte[0x40];
|
|
|
|
var dir = new List<Entry> (count);
|
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
{
|
|
|
|
int name_length = header.ReadInt32();
|
|
|
|
if (name_length <= 0)
|
|
|
|
return null;
|
|
|
|
if (name_length > name_buf.Length)
|
|
|
|
name_buf = new byte[name_length];
|
|
|
|
if (name_length != header.Read (name_buf, 0, name_length))
|
|
|
|
return null;
|
|
|
|
var name = Encodings.cp932.GetString (name_buf, 0, name_length);
|
2015-08-30 22:34:06 +04:00
|
|
|
var entry = FormatCatalog.Instance.Create<PackedEntry> (name);
|
2016-10-14 12:35:56 +04:00
|
|
|
|
|
|
|
entry.Offset = base_offset + header.ReadUInt32();
|
|
|
|
entry.UnpackedSize = header.ReadUInt32();
|
|
|
|
entry.Size = header.ReadUInt32();
|
|
|
|
entry.IsPacked = header.ReadInt32() != 0;
|
|
|
|
uint psize = header.ReadUInt32();
|
2015-08-11 17:44:18 +04:00
|
|
|
if (entry.IsPacked)
|
|
|
|
entry.Size = psize;
|
2016-10-14 12:35:56 +04:00
|
|
|
|
2015-08-11 17:44:18 +04:00
|
|
|
if (!entry.CheckPlacement (file.MaxOffset))
|
|
|
|
return null;
|
|
|
|
dir.Add (entry);
|
|
|
|
}
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<Entry> OpenPakV3 (ArcView file)
|
|
|
|
{
|
2015-02-26 08:22:54 +04:00
|
|
|
if (0x110 > file.View.Reserve (4, 0x110))
|
|
|
|
return null;
|
|
|
|
|
|
|
|
uint size_xor = file.View.ReadUInt32 (0x104);
|
|
|
|
if (0x64 != size_xor)
|
|
|
|
return null;
|
2016-02-04 05:59:55 +04:00
|
|
|
byte[] name_buf = file.View.ReadBytes (4, 0x100);
|
2015-02-26 08:22:54 +04:00
|
|
|
int name_len = 0;
|
|
|
|
for (int i = 0; i < name_buf.Length; ++i)
|
|
|
|
{
|
|
|
|
if (0 == name_buf[i])
|
|
|
|
break;
|
|
|
|
if (name_buf[i] >= 0x80 || name_buf[i] < 0x20)
|
|
|
|
return null;
|
|
|
|
++name_len;
|
|
|
|
}
|
|
|
|
if (0 == name_len || name_len > 0x10)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
uint header_key = GetKey (name_buf, name_len);
|
|
|
|
uint unpacked = file.View.ReadUInt32 (0x108) ^ header_key;
|
|
|
|
int count = (int)(file.View.ReadUInt32 (0x10c) ^ header_key);
|
2015-08-11 17:44:18 +04:00
|
|
|
if (!IsSaneCount (count))
|
2015-02-26 08:22:54 +04:00
|
|
|
return null;
|
|
|
|
|
|
|
|
var dir = new List<Entry> (count);
|
|
|
|
uint header_size = file.View.ReadUInt32 (0x110) ^ size_xor;
|
|
|
|
long base_offset = 0x114 + header_size;
|
2016-05-21 10:31:09 +04:00
|
|
|
using (var input = file.CreateStream (0x114, header_size))
|
2015-02-26 08:22:54 +04:00
|
|
|
using (var header_stream = new ZLibStream (input, CompressionMode.Decompress))
|
|
|
|
using (var header = new BinaryReader (header_stream, Encoding.ASCII, true))
|
|
|
|
{
|
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
{
|
|
|
|
name_len = header.ReadInt32();
|
|
|
|
if (name_len <= 0 || name_len > name_buf.Length)
|
|
|
|
return null;
|
|
|
|
if (name_len != header.Read (name_buf, 0, name_len))
|
|
|
|
return null;
|
|
|
|
uint key = GetKey (name_buf, name_len);
|
2016-10-14 12:35:56 +04:00
|
|
|
var name = Encodings.cp932.GetString (name_buf, 0, name_len);
|
|
|
|
var entry = FormatCatalog.Instance.Create<PakEntry> (name);
|
|
|
|
entry.Offset = (header.ReadUInt32() ^ key) + base_offset;
|
|
|
|
entry.UnpackedSize = (header.ReadUInt32() ^ key);
|
|
|
|
uint ignored = (header.ReadUInt32() ^ key);
|
|
|
|
entry.IsPacked = (header.ReadUInt32() ^ key) != 0;
|
|
|
|
uint packed_size = (header.ReadUInt32() ^ key);
|
|
|
|
entry.Key = key;
|
|
|
|
entry.Size = entry.IsPacked ? packed_size : entry.UnpackedSize;
|
2015-02-26 08:22:54 +04:00
|
|
|
if (!entry.CheckPlacement (file.MaxOffset))
|
|
|
|
return null;
|
|
|
|
dir.Add (entry);
|
|
|
|
}
|
2015-08-11 17:44:18 +04:00
|
|
|
return dir;
|
2015-02-26 08:22:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint GetKey (byte[] name, int length)
|
|
|
|
{
|
|
|
|
int key = 0;
|
|
|
|
for (int i = 0; i < length; ++i)
|
|
|
|
{
|
|
|
|
key *= 0x89;
|
|
|
|
key += (sbyte)name[i];
|
|
|
|
}
|
|
|
|
return (uint)key;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override Stream OpenEntry (ArcFile arc, Entry entry)
|
|
|
|
{
|
|
|
|
var pak_entry = entry as PakEntry;
|
2016-10-14 12:35:56 +04:00
|
|
|
if (pak_entry != null && !pak_entry.IsPacked)
|
2015-08-11 17:44:18 +04:00
|
|
|
return OpenV3Entry (arc, pak_entry);
|
|
|
|
|
2016-10-14 12:35:56 +04:00
|
|
|
Stream input = arc.File.CreateStream (entry.Offset, entry.Size);
|
2015-08-11 17:44:18 +04:00
|
|
|
var packed_entry = entry as PackedEntry;
|
|
|
|
if (packed_entry != null && packed_entry.IsPacked)
|
2016-10-14 12:35:56 +04:00
|
|
|
input = new ZLibStream (input, CompressionMode.Decompress);
|
|
|
|
return input;
|
2015-08-11 17:44:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private Stream OpenV3Entry (ArcFile arc, PakEntry entry)
|
|
|
|
{
|
2015-02-26 08:22:54 +04:00
|
|
|
uint enc_size = Math.Min (entry.Size, 0x10u);
|
|
|
|
if (0 == enc_size)
|
|
|
|
return Stream.Null;
|
2016-02-04 05:59:55 +04:00
|
|
|
var buf = arc.File.View.ReadBytes (entry.Offset, enc_size);
|
2015-08-11 17:44:18 +04:00
|
|
|
uint key = entry.Key;
|
2015-02-26 08:22:54 +04:00
|
|
|
for (int i = 0; i < buf.Length; ++i)
|
|
|
|
{
|
|
|
|
buf[i] ^= (byte)key;
|
2016-02-04 05:59:55 +04:00
|
|
|
key = Binary.RotR (key, 8);
|
2015-02-26 08:22:54 +04:00
|
|
|
}
|
|
|
|
if (enc_size == entry.Size)
|
2016-10-16 09:22:53 +04:00
|
|
|
return new BinMemoryStream (buf, entry.Name);
|
2015-02-26 22:07:49 +04:00
|
|
|
return new PrefixStream (buf, arc.File.CreateStream (entry.Offset+enc_size, entry.Size-enc_size));
|
2015-02-26 08:22:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|