Beispiel #1
0
        public override Stream OpenEntry(ArcFile arc, Entry entry)
        {
            var earc = arc as EpkArchive;
            var eent = entry as EpkEntry;

            if (null == earc || null == eent)
            {
                return(base.OpenEntry(arc, entry));
            }
            long    entry_offset = entry.Offset;
            ArcView file         = arc.File;

            if (eent.ArcNumber > 0)
            {
                file = earc.Parts[eent.ArcNumber - 1];
            }
            if (entry_offset + entry.Size <= file.MaxOffset)
            {
                return(file.CreateStream(entry_offset, entry.Size));
            }
            uint first_part_size = (uint)(file.MaxOffset - entry_offset);
            var  begin           = file.CreateStream(entry_offset, first_part_size);
            var  end             = earc.Parts[eent.ArcNumber].CreateStream(0, entry.Size - first_part_size);

            return(new ConcatStream(begin, end));
        }
Beispiel #2
0
        Stream OpenLstIndex(ArcView file, string dat_name, HibikiDatScheme scheme)
        {
            var lst_name = Path.ChangeExtension(file.Name, ".lst");

            if (VFS.FileExists(lst_name))
            {
                return(VFS.OpenStream(lst_name));
            }
            else if ("init.dat" == dat_name)
            {
                return(file.CreateStream());
            }
            // try to open 'init.dat' archive in the same directory
            var init_dat = VFS.CombinePath(VFS.GetDirectoryName(file.Name), "init.dat");

            if (!VFS.FileExists(init_dat))
            {
                return(file.CreateStream());
            }
            try
            {
                using (var init = VFS.OpenView(init_dat))
                    using (var init_arc = TryOpenWithScheme(init, ReadCount(init), scheme))
                    {
                        lst_name = Path.GetFileName(lst_name);
                        var lst_entry = init_arc.Dir.First(e => e.Name == lst_name);
                        return(init_arc.OpenEntry(lst_entry));
                    }
            }
            catch
            {
                return(file.CreateStream());
            }
        }
Beispiel #3
0
        IList <string> ReadFileNames(ArcView index, int arc_no)
        {
            if (index.View.ReadUInt32(0) != Signature)
            {
                return(null);
            }
            if (!index.View.AsciiEqual(4, "OFST"))
            {
                return(null);
            }
            int index_size = index.View.ReadInt32(8);
            int arc_count  = index_size / 4;

            if (arc_no >= arc_count)
            {
                return(null);
            }
            uint index_offset = index.View.ReadUInt32(0xC + arc_no * 4);

            if (index_offset >= index.MaxOffset)
            {
                return(null);
            }
            Stream input;

            if (index.View.AsciiEqual(index_offset, "DFLT"))
            {
                uint packed_size = index.View.ReadUInt32(index_offset + 4);
                input = index.CreateStream(index_offset + 12, packed_size);
                input = new ZLibStream(input, CompressionMode.Decompress);
            }
            else if (index.View.AsciiEqual(index_offset, "DATA"))
            {
                uint data_size = index.View.ReadUInt32(index_offset + 4);
                input = index.CreateStream(index_offset + 8, data_size);
            }
            else
            {
                return(null);
            }
            using (input)
                using (var reader = new StreamReader(input, Encodings.cp932))
                {
                    var    list = new List <string>();
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        list.Add(line.Trim());
                    }
                    return(list);
                }
        }
Beispiel #4
0
        static internal Dictionary <string, Dictionary <int, Entry> > FindSec5Resr(string arc_name)
        {
            string dir_name = Path.GetDirectoryName(arc_name);
            var    match    = Directory.GetFiles(dir_name, "*.sec5");

            if (0 == match.Length)
            {
                string parent = Path.GetDirectoryName(dir_name);
                if (!string.IsNullOrEmpty(parent))
                {
                    match = Directory.GetFiles(parent, "*.sec5");
                }
            }
            if (0 == match.Length)
            {
                return(null);
            }
            using (var sec5 = new ArcView(match[0]))
            {
                if (!sec5.View.AsciiEqual(0, "SEC5"))
                {
                    return(null);
                }
                uint offset = 8;
                while (offset < sec5.MaxOffset)
                {
                    string id = sec5.View.ReadString(offset, 4, Encoding.ASCII);
                    if ("ENDS" == id)
                    {
                        break;
                    }
                    uint section_size = sec5.View.ReadUInt32(offset + 4);
                    offset += 8;
                    if ("RESR" == id)
                    {
                        using (var resr = sec5.CreateStream(offset, section_size))
                            return(ReadResrSection(resr));
                    }
                    if ("RES2" == id)
                    {
                        using (var res2 = sec5.CreateStream(offset, section_size))
                            return(ReadRes2Section(res2));
                    }
                    offset += section_size;
                }
            }
            return(null);
        }
Beispiel #5
0
 List <Entry> ReadIndex <EntryDef> (ArcView file, int count, uint index_offset)
     where EntryDef : IEntryDefinition, new()
 {
     using (var input = file.CreateStream())
     {
         input.Position = index_offset;
         var dir = new List <Entry> (count);
         var def = new EntryDef();
         for (int i = 0; i < count; ++i)
         {
             input.ReadStruct(out def);
             if (def.Size != 0)
             {
                 var entry = Create <PackedEntry> (def.Name);
                 entry.Offset = def.Offset;
                 entry.Size   = def.Size;
                 if (!entry.CheckPlacement(file.MaxOffset))
                 {
                     return(null);
                 }
                 entry.IsPacked = def.IsPacked;
                 dir.Add(entry);
             }
         }
         return(dir);
     }
 }
Beispiel #6
0
        public override ArcFile TryOpen(ArcView file)
        {
            if (!file.View.AsciiEqual(4, "ver 1.00"))
            {
                return(null);
            }
            int count = file.View.ReadInt32(0x10);

            if (!IsSaneCount(count))
            {
                return(null);
            }
            uint index_size = (uint)count * 0x2C;

            using (var index = file.CreateStream(file.MaxOffset - index_size, index_size))
            {
                var dir = new List <Entry> (count);
                for (int i = 0; i < count; ++i)
                {
                    var name  = index.ReadCString(0x20);
                    var entry = FormatCatalog.Instance.Create <PackedEntry> (name);
                    entry.UnpackedSize = index.ReadUInt32();
                    entry.Offset       = index.ReadUInt32();
                    entry.Size         = index.ReadUInt32();
                    entry.IsPacked     = true;
                    if (!entry.CheckPlacement(file.MaxOffset))
                    {
                        return(null);
                    }
                    dir.Add(entry);
                }
                return(new ArcFile(file, this, dir));
            }
        }
Beispiel #7
0
        public override ArcFile TryOpen(ArcView file)
        {
            if (!file.Name.HasExtension("PEL"))
            {
                return(null);
            }
            int count = file.View.ReadInt16(0);

            if (!IsSaneCount(count))
            {
                return(null);
            }
            using (var input = file.CreateStream())
            {
                input.Position = 2;
                var dir = new List <Entry> (count);
                for (int i = 0; i < count; ++i)
                {
                    var  name  = input.ReadCString(0x14);
                    uint size  = input.ReadUInt32();
                    var  entry = Create <Entry> (name);
                    entry.Offset = input.Position;
                    entry.Size   = size;
                    if (!entry.CheckPlacement(file.MaxOffset))
                    {
                        return(null);
                    }
                    dir.Add(entry);
                    input.Seek(size, SeekOrigin.Current);
                }
                return(new ArcFile(file, this, dir));
            }
        }
Beispiel #8
0
        public List <Entry> Read()
        {
            long data_size    = m_file.View.ReadInt64(0x10);
            long index_offset = m_file.View.ReadInt64(0x28);

            if (data_size >= m_file.MaxOffset || index_offset >= m_file.MaxOffset)
            {
                return(null);
            }
            uint index_size = m_file.View.ReadUInt32(0x30);
            uint flags      = m_file.View.ReadUInt32(0x38);
            var  key        = m_file.View.ReadBytes(0x58, 8);

            m_base_offset = m_file.MaxOffset - data_size;
            foreach (var scheme in KnownSchemes)
            {
                m_dir.Clear();
                try
                {
                    using (var stream = m_file.CreateStream(m_base_offset + index_offset, index_size))
                        using (var index = scheme.TransformStream(stream, key, flags))
                        {
                            if (ReadIndex(index))
                            {
                                this.Scheme = scheme;
                                return(m_dir);
                            }
                        }
                }
                catch { /* invalid scheme, retry */ }
            }
            return(null);
        }
Beispiel #9
0
        public override ArcFile TryOpen(ArcView file)
        {
            if (!file.View.AsciiEqual(4, "AD\0"))
            {
                return(null);
            }
            int version = file.View.ReadByte(7);

            using (var index = file.CreateStream())
            {
                List <Entry> dir = null;
                if (3 == version)
                {
                    dir = ReadIndexV3(index);
                }
                else if (1 == version)
                {
                    dir = ReadIndexV1(index);
                }
                if (null == dir || 0 == dir.Count)
                {
                    return(null);
                }
                return(new ArcFile(file, this, dir));
            }
        }
Beispiel #10
0
            public List <Entry> ReadIndex()
            {
                if (!IsSaneCount(m_count) || m_packed_size + 0x20L > m_file.MaxOffset)
                {
                    return(null);
                }
                m_index = new byte[m_unpacked_size];
                using (var input = m_file.CreateStream(0x20, m_packed_size))
                {
                    try
                    {
                        if (ReadV0(input))
                        {
                            return(m_dir.Value);
                        }
                    }
                    catch { /* ignore V0 parse errors, try V1 */ }

                    if (m_dir.IsValueCreated)
                    {
                        m_dir.Value.Clear();
                    }
                    input.Position = 0;
                    if (ReadV1(input))
                    {
                        return(m_dir.Value);
                    }
                    return(null);
                }
            }
Beispiel #11
0
        public override ArcFile TryOpen(ArcView file)
        {
            if (-1 == SearchForSignature(file))
            {
                return(null);
            }
            var input = file.CreateStream();

            try
            {
                var zip = new ZipArchive(input, ZipArchiveMode.Read, false, Encodings.cp932);
                try
                {
                    var dir = zip.Entries.Where(z => !z.FullName.EndsWith("/"))
                              .Select(z => new ZipEntry(z) as Entry).ToList();
                    return(new PkZipArchive(file, this, dir, zip));
                }
                catch
                {
                    zip.Dispose();
                    throw;
                }
            }
            catch
            {
                input.Dispose();
                throw;
            }
        }
Beispiel #12
0
        IBinaryStream Decompress(ArcView file, long offset, uint packed_size)
        {
            uint unpacked_size = Binary.BigEndian(file.View.ReadUInt32(offset));
            bool is_packed     = file.View.ReadByte(offset + 8) != 0;

            if (!is_packed)
            {
                return(file.CreateStream(offset + 9, unpacked_size));
            }
            using (var input = file.CreateStream(offset + 9, packed_size - 9))
            {
                var compr = new MpkCompression(input, (int)unpacked_size);
                var data  = compr.Unpack();
                return(new BinMemoryStream(data));
            }
        }
Beispiel #13
0
 public override ArcFile TryOpen(ArcView file)
 {
     if (!file.Name.HasAnyOfExtensions("cdt", "spt"))
     {
         return(null);
     }
     using (var input = file.CreateStream())
     {
         var dir = new List <Entry>();
         while (input.PeekByte() != -1)
         {
             var name = input.ReadCString(0x10);
             if (string.IsNullOrWhiteSpace(name))
             {
                 return(null);
             }
             var entry = FormatCatalog.Instance.Create <PackedEntry> (name);
             entry.UnpackedSize = input.ReadUInt32();
             entry.Size         = input.ReadUInt32();
             entry.IsPacked     = input.ReadInt32() != 0;
             entry.Offset       = input.Position;
             if (!entry.CheckPlacement(file.MaxOffset))
             {
                 return(null);
             }
             dir.Add(entry);
             input.Seek(entry.Size, SeekOrigin.Current);
         }
         return(new ArcFile(file, this, dir));
     }
 }
Beispiel #14
0
 public override ArcFile TryOpen(ArcView file)
 {
     using (var input = file.CreateStream())
         using (var reader = new PsbReader(input))
         {
             foreach (var key in KnownKeys)
             {
                 if (reader.Parse(key))
                 {
                     var dir = reader.GetTextures();
                     if (null == dir || 0 == dir.Count)
                     {
                         return(null);
                     }
                     else
                     {
                         return(new ArcFile(file, this, dir));
                     }
                 }
                 if (!reader.IsEncrypted)
                 {
                     break;
                 }
             }
             return(null);
         }
 }
Beispiel #15
0
        public override ArcFile TryOpen(ArcView file)
        {
            int count = file.View.ReadInt32(8);

            if (!IsSaneCount(count))
            {
                return(null);
            }

            using (var index = file.CreateStream())
            {
                index.Position = 0xC;
                var dir = new List <Entry> (count);
                for (int i = 0; i < count; ++i)
                {
                    uint   offset    = index.ReadUInt32();
                    uint   size      = index.ReadUInt32();
                    bool   is_packed = index.ReadInt32() != 1;
                    string name      = index.ReadCString();
                    var    entry     = FormatCatalog.Instance.Create <PackedEntry> (name);
                    entry.Offset   = offset;
                    entry.Size     = size;
                    entry.IsPacked = is_packed;
                    if (!entry.CheckPlacement(file.MaxOffset))
                    {
                        return(null);
                    }
                    dir.Add(entry);
                }
                return(new ArcFile(file, this, dir));
            }
        }
Beispiel #16
0
        public override ArcFile TryOpen(ArcView file)
        {
            uint        header_size = file.View.ReadUInt32(8);
            HzcMetaData image_info;

            using (var header = file.CreateStream(0, 0xC + header_size))
            {
                image_info = Hzc.Value.ReadMetaData(header) as HzcMetaData;
                if (null == image_info)
                {
                    return(null);
                }
            }
            int count = file.View.ReadInt32(0x20);

            if (0 == count)
            {
                count = 1;
            }
            string base_name  = Path.GetFileNameWithoutExtension(file.Name);
            int    frame_size = image_info.UnpackedSize / count;
            var    dir        = new List <Entry> (count);

            for (int i = 0; i < count; ++i)
            {
                var entry = new Entry {
                    Name   = string.Format("{0}#{1:D3}", base_name, i),
                    Type   = "image",
                    Offset = frame_size * i,
                    Size   = (uint)frame_size,
                };
                dir.Add(entry);
            }
            return(new HzcArchive(file, this, dir, image_info));
        }
Beispiel #17
0
        internal ArcFile ReadOldIndex(ArcView file)
        {
            int count = file.View.ReadInt32(4);

            if (!IsSaneCount(count))
            {
                return(null);
            }

            var dir = new List <Entry> (count);

            using (var index = file.CreateStream())
            {
                index.Position = 8;
                uint names_size = index.ReadUInt32();
                for (int i = 0; i < count; ++i)
                {
                    var name  = index.ReadCString();
                    var entry = FormatCatalog.Instance.Create <Entry> (name);
                    dir.Add(entry);
                }
                index.Position = 12 + names_size;
                foreach (var entry in dir)
                {
                    entry.Offset = index.ReadUInt32();
                    entry.Size   = index.ReadUInt32();
                    if (!entry.CheckPlacement(file.MaxOffset))
                    {
                        return(null);
                    }
                }
            }
            return(new ArcFile(file, this, dir));
        }
Beispiel #18
0
            public List <Entry> ReadIndex()
            {
                if (!IsSaneCount(m_count) || m_packed_size + 0x20L > m_file.MaxOffset)
                {
                    return(null);
                }
                m_index = new byte[m_unpacked_size];
                var readers = new Func <Stream, bool>[] {
                    ReadV0,
                    s => ReadV1(s, 0x18),
                    s => ReadV1(s, 0x10),
                };

                using (var input = m_file.CreateStream(0x20, m_packed_size))
                {
                    foreach (var read in readers)
                    {
                        try
                        {
                            if (read(input))
                            {
                                return(m_dir.Value);
                            }
                        }
                        catch { /* ignore parse errors */ }
                        input.Position = 0;
                        if (m_dir.IsValueCreated)
                        {
                            m_dir.Value.Clear();
                        }
                    }
                    return(null);
                }
            }
Beispiel #19
0
        public override ArcFile TryOpen(ArcView file)
        {
            uint data_size = file.View.ReadUInt32(4);
            int  count     = file.View.ReadInt32(8);

            if (!IsSaneCount(count))
            {
                return(null);
            }

            uint index_offset = 0x20;
            uint index_size   = file.View.ReadUInt32(0xC);

            if (data_size + index_size != file.MaxOffset)
            {
                return(null);
            }
            long data_offset = index_offset + index_size;

            if (data_offset >= file.MaxOffset || data_offset <= index_offset)
            {
                return(null);
            }
            int version     = file.View.ReadByte(3) - '0';
            int name_length = version > 0 ? 4 : 2;

            using (var index = file.CreateStream(index_offset, index_size))
            {
                var dir = new List <Entry> (count);
                for (int i = 0; i < count; ++i)
                {
                    var name = index.ReadCString(name_length);
                    if (0 == name.Length)
                    {
                        return(null);
                    }
                    uint n1 = index.ReadUInt16();
                    if (version > 0)
                    {
                        uint n2 = index.ReadUInt16();
                        uint n3 = index.ReadUInt32();
                        name = string.Format("{0}_{1:D2}_{2}_{3:D3}.wav", name, n1, n2, n3);
                    }
                    else
                    {
                        uint n2 = index.ReadUInt32();
                        name = string.Format("{0}_{1:D2}_{2:D3}.wav", name, n1, n2);
                    }
                    var entry = FormatCatalog.Instance.Create <Entry> (name);
                    entry.Offset = index.ReadUInt32();
                    entry.Size   = index.ReadUInt32();
                    if (!entry.CheckPlacement(file.MaxOffset))
                    {
                        return(null);
                    }
                    dir.Add(entry);
                }
                return(new ArcFile(file, this, dir));
            }
        }
Beispiel #20
0
        public override ArcFile TryOpen(ArcView file)
        {
            int count = file.View.ReadInt32(4);

            if (!IsSaneCount(count))
            {
                return(null);
            }
            byte key = file.View.ReadByte(0x87);

            using (var input = file.CreateStream(8, 0x88 * (uint)count))
                using (var dec = new XoredStream(input, key))
                    using (var index = new BinaryStream(dec, file.Name))
                    {
                        var dir = new List <Entry> (count);
                        for (int i = 0; i < count; ++i)
                        {
                            var name = index.ReadCString(0x80);
                            if (string.IsNullOrWhiteSpace(name))
                            {
                                return(null);
                            }
                            var entry = FormatCatalog.Instance.Create <Entry> (name);
                            entry.Size   = index.ReadUInt32();
                            entry.Offset = index.ReadUInt32();
                            if (!entry.CheckPlacement(file.MaxOffset))
                            {
                                return(null);
                            }
                            dir.Add(entry);
                        }
                        return(new PkdArchive(file, this, dir, key));
                    }
        }
Beispiel #21
0
 ArcFile ReadIndex(ArcView file, uint[] key, int count)
 {
     using (var input = file.CreateStream(8, (uint)count * 0x80u))
         using (var dec = new ByteStringEncryptedStream(input, GetKeyBytes(key)))
             using (var index = new BinaryStream(dec, file.Name))
             {
                 var dir = new List <Entry> (count);
                 for (int i = 0; i < count; ++i)
                 {
                     var name = index.ReadCString(0x74);
                     if (0 == name.Length)
                     {
                         return(null);
                     }
                     var entry = FormatCatalog.Instance.Create <Pkg2Entry> (name);
                     entry.Size          = index.ReadUInt32();
                     entry.Offset        = index.ReadUInt32();
                     entry.EncryptedSize = index.ReadUInt32();
                     if (!entry.CheckPlacement(file.MaxOffset))
                     {
                         return(null);
                     }
                     dir.Add(entry);
                 }
                 return(new Pkg2Archive(file, this, dir, key));
             }
 }
Beispiel #22
0
 public override ArcFile TryOpen(ArcView file)
 {
     using (var input = file.CreateStream())
         using (var reader = new PsbReader(input))
         {
             foreach (var key in KnownKeys)
             {
                 try
                 {
                     if (reader.Parse(key))
                     {
                         return(OpenArcFile(reader, file));
                     }
                     if (!reader.IsEncrypted)
                     {
                         break;
                     }
                 }
                 catch { /* ignore parse errors caused by invalid key */ }
             }
             if (reader.ParseNonEncrypted())
             {
                 return(OpenArcFile(reader, file));
             }
             return(null);
         }
 }
Beispiel #23
0
        List <Entry> ReadIndex(ArcView file, int version, byte[] key)
        {
            var header = file.View.ReadBytes(4, 0x18);

            if (0x18 != header.Length)
            {
                return(null);
            }
            Decrypt(header, 0, header.Length, 4, key);
            var dx = new DxHeader {
                IndexSize   = LittleEndian.ToUInt32(header, 0),
                BaseOffset  = LittleEndian.ToUInt32(header, 4),
                IndexOffset = LittleEndian.ToUInt32(header, 8),
                FileTable   = LittleEndian.ToUInt32(header, 0x0c),
                DirTable    = LittleEndian.ToUInt32(header, 0x10),
            };

            if (dx.DirTable >= dx.IndexSize || dx.FileTable >= dx.IndexSize)
            {
                return(null);
            }
            using (var encrypted = file.CreateStream(dx.IndexOffset, dx.IndexSize))
                using (var index = new EncryptedStream(encrypted, dx.IndexOffset, key))
                    using (var reader = new IndexReader(dx, version, index))
                    {
                        return(reader.Read());
                    }
        }
Beispiel #24
0
        public override ArcFile TryOpen(ArcView file)
        {
            if (!file.Name.HasExtension(".dat"))
            {
                return(null);
            }
            int count = file.View.ReadInt32(0) ^ 0x26ACA46E;

            if (!IsSaneCount(count))
            {
                return(null);
            }

            var scheme = QueryScheme(file.Name);

            if (null == scheme)
            {
                return(null);
            }

            var dir = new List <Entry> (count);

            using (var input = file.CreateStream(4, (uint)count * 0x15))
            {
                foreach (var entry in ReadIndex(input, count, scheme))
                {
                    if (!entry.CheckPlacement(file.MaxOffset))
                    {
                        return(null);
                    }
                    dir.Add(entry);
                }
            }
            return(new ArcDatArchive(file, this, dir, scheme.Hash));
        }
Beispiel #25
0
        public override ArcFile TryOpen(ArcView file)
        {
            uint index_offset = file.View.ReadUInt16(4);

            if (0 != file.View.ReadUInt16(10))
            {
                return(null);
            }

            uint info_size     = file.View.ReadUInt32(index_offset);
            int  root_count    = file.View.ReadInt32(index_offset + 8);
            uint unpacked_size = file.View.ReadUInt32(index_offset + 0xC);
            uint packed_size   = file.View.ReadUInt32(index_offset + 0x10);

            using (var packed = file.CreateStream(index_offset + info_size, packed_size))
                using (var unpacked = new DeflateStream(packed, CompressionMode.Decompress))
                    using (var reader = new IndexReader(unpacked, index_offset + info_size + packed_size))
                    {
                        var dir = reader.ReadRoot(root_count);
                        if (null == dir)
                        {
                            return(null);
                        }
                        return(new ArcFile(file, this, dir));
                    }
        }
Beispiel #26
0
        public override ArcFile TryOpen(ArcView file)
        {
            var key = FindKey(file);

            if (null == key)
            {
                return(null);
            }
            uint signature_key = key.ToUInt32(0);
            int  count         = (int)(file.View.ReadUInt32(0) ^ signature_key);

            if (!IsSaneCount(count))
            {
                return(null);
            }
            uint index_size = 32 * (uint)count;

            using (var enc = file.CreateStream(0x10, index_size))
                using (var input = new ByteStringEncryptedStream(enc, key))
                    using (var index = new BinaryStream(input, file.Name))
                    {
                        var reader = new IndexReader(file.MaxOffset);
                        var dir    = reader.Read(index, count);
                        if (null == dir)
                        {
                            return(null);
                        }
                        return(new ActressArchive(file, this, dir, key));
                    }
        }
Beispiel #27
0
        public override ArcFile TryOpen(ArcView file)
        {
            uint header_size = Binary.BigEndian(file.View.ReadUInt32(0));
            uint file_size   = Binary.BigEndian(file.View.ReadUInt32(4));

            if (file_size != file.MaxOffset || header_size > file_size || 0 == header_size)
            {
                return(null);
            }
            int  format      = Binary.BigEndian(file.View.ReadInt32(8));
            uint data_offset = Binary.BigEndian(file.View.ReadUInt32(12));

            if (format <= 0 || format > 0x100 || data_offset >= file_size || data_offset < header_size)
            {
                return(null);
            }
            using (var stream = file.CreateStream())
                using (var input = new AssetReader(stream))
                {
                    var index = new ResourcesAssetsDeserializer(file);
                    var dir   = index.Parse(input);
                    if (null == dir || 0 == dir.Count)
                    {
                        return(null);
                    }
                    var res_map = index.GenerateResourceMap(dir);
                    return(new UnityResourcesAsset(file, this, dir, res_map));
                }
        }
Beispiel #28
0
        public override ArcFile TryOpen(ArcView file)
        {
            if (file.MaxOffset <= 8)
            {
                return(null);
            }
            int count = file.View.ReadInt32(file.MaxOffset - 8);

            if (!IsSaneCount(count))
            {
                return(null);
            }
            uint index_size = file.View.ReadUInt32(file.MaxOffset - 4);

            if (index_size >= file.MaxOffset - 8)
            {
                return(null);
            }

            long index_offset = file.MaxOffset - 8 - index_size;

            using (var input = file.CreateStream(index_offset, index_size))
                using (var index = new BinaryReader(input, Encoding.Unicode))
                {
                    char[] name_buffer = new char[0x40];
                    var    dir         = new List <Entry> (count);
                    for (int i = 0; i < count; ++i)
                    {
                        uint offset        = index.ReadUInt32();
                        uint packed_size   = index.ReadUInt32();
                        uint unpacked_size = index.ReadUInt32();
                        int  name_length   = index.ReadInt32();
                        if (name_length <= 0 || name_length > 0x100)
                        {
                            return(null);
                        }
                        if (name_length > name_buffer.Length)
                        {
                            name_buffer = new char[name_length];
                        }
                        if (name_length != index.Read(name_buffer, 0, name_length))
                        {
                            return(null);
                        }
                        var name  = new string (name_buffer, 0, name_length);
                        var entry = FormatCatalog.Instance.Create <PackedEntry> (name);
                        entry.Offset = offset;
                        entry.Size   = packed_size;
                        if (!entry.CheckPlacement(file.MaxOffset))
                        {
                            return(null);
                        }
                        entry.IsPacked     = uint.MaxValue != unpacked_size;
                        entry.UnpackedSize = entry.IsPacked ? unpacked_size : packed_size;
                        dir.Add(entry);
                    }
                    return(new ArcFile(file, this, dir));
                }
        }
Beispiel #29
0
        public override ArcFile TryOpen(ArcView file)
        {
            int count = file.View.ReadInt32(0);

            if (!IsSaneCount(count))
            {
                return(null);
            }
            var  arc_name = Path.GetFileName(file.Name);
            bool is_ucg   = arc_name.StartsWith("UCG", StringComparison.OrdinalIgnoreCase);
            bool is_cg    = is_ucg || arc_name == "CG";

            using (var index = file.CreateStream())
            {
                index.Position = 4;
                var dir = new List <Entry> (count);
                for (int i = 0; i < count; ++i)
                {
                    var name = index.ReadCString();
                    if (string.IsNullOrWhiteSpace(name) || name.Length > 0x100)
                    {
                        return(null);
                    }
                    var entry = Create <PackedEntry> (name);
                    entry.Offset = index.ReadUInt32();
                    if (entry.Offset >= file.MaxOffset)
                    {
                        return(null);
                    }
                    entry.IsPacked = is_ucg;
                    dir.Add(entry);
                }
                long index_end = index.Position;
                for (int i = 0; i < count; ++i)
                {
                    var entry = dir[i];
                    if (entry.Offset < index_end)
                    {
                        return(null);
                    }
                    long next_offset = i + 1 < count ? dir[i + 1].Offset : file.MaxOffset;
                    entry.Size = (uint)(next_offset - entry.Offset);
                    if (is_cg)
                    {
                        entry.Type = "image";
                    }
                }
                if (is_cg && !is_ucg)
                {
                    var palette_entry = dir.Find(e => e.Name.Equals("Palette", StringComparison.OrdinalIgnoreCase));
                    if (palette_entry != null && 1 == file.View.ReadByte(palette_entry.Offset + 8))
                    {
                        var palette = ImageFormat.ReadPalette(file, palette_entry.Offset + 9, 0x100, PaletteFormat.Rgb);
                        return(new CgArchive(file, this, dir, palette));
                    }
                }
                return(new ArcFile(file, this, dir));
            }
        }
Beispiel #30
0
 protected NcIndexReaderBase(ArcView file, int count)
 {
     m_input       = file.CreateStream();
     m_dir         = new List <Entry> (count);
     m_count       = count;
     m_max_offset  = file.MaxOffset;
     IndexPosition = 4;
 }
Beispiel #31
0
 ArcFile OpenV2(ArcView file, int count, int entry_size, int index_size)
 {
     int index_offset = 0x10;
     int filenames_offset = index_offset + entry_size * count;
     int filenames_length = file.View.ReadInt32 (filenames_offset);
     char[] filenames;
     using (var fn_stream = file.CreateStream (filenames_offset+8, (uint)filenames_length*2))
     using (var fn_reader = new BinaryReader (fn_stream, Encoding.Unicode))
         filenames = fn_reader.ReadChars (filenames_length);
     var dir = new List<Entry> (count);
     for (int i = 0; i < count; ++i)
     {
         int name_offset = file.View.ReadInt32 (index_offset);
         if (name_offset < 0 || name_offset >= filenames.Length)
             return null;
         var name = GetName (filenames, name_offset);
         if (0 == name.Length)
             return null;
         var entry = FormatCatalog.Instance.Create<PackedEntry> (name);
         entry.Offset = file.View.ReadUInt32 (index_offset+0xA);
         entry.Size   = file.View.ReadUInt32 (index_offset+0xE);
         entry.UnpackedSize = file.View.ReadUInt32 (index_offset+0x12);
         entry.IsPacked     = 0 != file.View.ReadByte (index_offset+0x16);
         if (!entry.CheckPlacement (file.MaxOffset))
             return null;
         dir.Add (entry);
         index_offset += entry_size;
     }
     return new ArcFile (file, this, dir);
 }
Beispiel #32
0
 public override ArcFile TryOpen(ArcView file)
 {
     int count = file.View.ReadInt32 (0x18);
     if (!IsSaneCount (count))
         return null;
     var key = QueryEncryption();
     if (null == key)
         return null;
     var aes = Aes.Create();
     try
     {
         aes.Mode = CipherMode.CBC;
         aes.Padding = PaddingMode.PKCS7;
         aes.Key = key;
         aes.IV = file.View.ReadBytes (8, 0x10);
         uint index_size = file.View.ReadUInt32 (0x1C);
         using (var decryptor = aes.CreateDecryptor())
         using (var enc_index = file.CreateStream (0x20, index_size))
         using (var dec_index = new CryptoStream (enc_index, decryptor, CryptoStreamMode.Read))
         using (var index = new ArcView.Reader (dec_index))
         {
             var dir = ReadIndex (index, count, file.MaxOffset);
             if (null == dir)
                 return null;
             var arc = new NpkArchive (file, this, dir, aes);
             aes = null; // object ownership passed to NpkArchive, don't dispose
             return arc;
         }
     }
     finally
     {
         if (aes != null)
             aes.Dispose();
     }
 }
Beispiel #33
0
 List<Entry> ReadIndex(ArcView file, int version, byte[] key)
 {
     var header = file.View.ReadBytes (4, 0x18);
     if (0x18 != header.Length)
         return null;
     Decrypt (header, 0, header.Length, 4, key);
     var dx = new DxHeader {
         IndexSize  = LittleEndian.ToUInt32 (header, 0),
         BaseOffset = LittleEndian.ToUInt32 (header, 4),
         IndexOffset = LittleEndian.ToUInt32 (header, 8),
         FileTable  = LittleEndian.ToUInt32 (header, 0x0c),
         DirTable   = LittleEndian.ToUInt32 (header, 0x10),
     };
     if (dx.DirTable >= dx.IndexSize || dx.FileTable >= dx.IndexSize)
         return null;
     using (var encrypted = file.CreateStream (dx.IndexOffset, dx.IndexSize))
     using (var index = new EncryptedStream (encrypted, dx.IndexOffset, key))
     using (var reader = new IndexReader (dx, version, index))
     {
         return reader.Read();
     }
 }
Beispiel #34
0
        public override ArcFile TryOpen(ArcView file)
        {
            if (!file.View.AsciiEqual (8, "AlicArch"))
                return null;
            if (!file.View.AsciiEqual (0x1C, "INFO"))
                return null;
            int version = file.View.ReadInt32 (0x10);
            long base_offset = file.View.ReadUInt32 (0x18);
            uint packed_size = file.View.ReadUInt32 (0x20);
            int unpacked_size = file.View.ReadInt32 (0x24);
            int count = file.View.ReadInt32 (0x28);
            if (!IsSaneCount (count))
                return null;

            var dir = new List<Entry> (count);
            var name_buf = new byte[0x40];
            using (var input = file.CreateStream (0x2C, packed_size))
            using (var zstream = new ZLibStream (input, CompressionMode.Decompress))
            using (var index = new BinaryReader (zstream))
            {
                for (int i = 0; i < count; ++i)
                {
                    int name_length = index.ReadInt32();
                    int index_step = index.ReadInt32();
                    if (name_length <= 0 || name_length > index_step || index_step > unpacked_size)
                        return null;
                    if (index_step > name_buf.Length)
                        name_buf = new byte[index_step];
                    if (index_step != index.Read (name_buf, 0, index_step))
                        return null;
                    var name = Encodings.cp932.GetString (name_buf, 0, name_length);
                    var entry = FormatCatalog.Instance.Create<Entry> (name);
                    index.ReadInt32();
                    index.ReadInt32();
                    if (version < 2)
                        index.ReadInt32();
                    entry.Offset = index.ReadUInt32() + base_offset;
                    entry.Size   = index.ReadUInt32();
                    if (!entry.CheckPlacement (file.MaxOffset))
                        return null;
                    dir.Add (entry);
                }
                return new ArcFile (file, this, dir);
            }
        }
Beispiel #35
0
        public override ArcFile TryOpen(ArcView file)
        {
            int type = file.View.ReadUInt16 (4);
            int count = file.View.ReadInt32 (6);
            if (!IsSaneCount (count))
                return null;

            using (var enc = file.CreateStream())
            using (var dec = new EncryptedStream (enc))
            using (var index = new BinaryReader (dec))
            {
                dec.Position = 0x4A;
                var offsets = new uint[count];
                for (int i = 0; i < count; ++i)
                {
                    offsets[i] = index.ReadUInt32();
                }
                var name_buffer = new byte[0x100];
                var dir = new List<Entry> (count);
                for (int i = 0; i < count; ++i)
                {
                    index.BaseStream.Position = offsets[i];
                    uint signature = index.ReadUInt32();
                    if (signature != 0x41544144) // 'DATA'
                        continue;
                    int section_count = index.ReadInt32();
                    index.ReadInt16();
                    var entry = new Entry { Offset = offsets[i] };
                    for (int s = 0; s < section_count; ++s)
                    {
                        signature = index.ReadUInt32();
                        if (0x44474D49 == signature) // 'IMGD'
                        {
                            entry.Offset = index.BaseStream.Position - 4;
                            uint imgd_size = index.ReadUInt32();
                            entry.Size = imgd_size + 0x10;
                            index.BaseStream.Seek (imgd_size + 2, SeekOrigin.Current);
                        }
                        else if (0x454E4E46 == signature) // 'FNNE'
                        {
                            int name_length = index.ReadInt32()-2;
                            index.ReadInt16();
                            if (name_length > name_buffer.Length)
                                name_buffer = new byte[name_length];
                            index.Read (name_buffer, 0, name_length);
                            entry.Name = Encodings.cp932.GetString (name_buffer, 0, name_length);
                            entry.Type = FormatCatalog.Instance.GetTypeFromName (entry.Name);
                            index.ReadInt16();
                        }
                        else
                        {
                            var section_size = index.ReadUInt32();
                            // section not supported, skip
                            index.BaseStream.Seek (section_size+2, SeekOrigin.Current);
                            if (0x415A4F4D != signature) // 'MOZA'
                                Trace.WriteLine (string.Format ("Unknown section 0x{0:X8}", signature), "[WAG/IAF]");
                        }
                    }
                    if (entry.Size > 0 && !string.IsNullOrEmpty (entry.Name))
                        dir.Add (entry);
                }
                if (0 == dir.Count)
                    return null;
                return new ArcFile (file, this, dir);
            }
        }
Beispiel #36
0
 protected static void DetectFileTypes(ArcView file, List<Entry> dir)
 {
     byte[] signature_buffer = new byte[4];
     foreach (PackedEntry entry in dir)
     {
         uint packed_size   = file.View.ReadUInt32 (entry.Offset);
         uint unpacked_size = file.View.ReadUInt32 (entry.Offset+4);
         if (0 == packed_size)
             packed_size = unpacked_size;
         entry.IsPacked = packed_size != unpacked_size;
         entry.Size = packed_size;
         entry.UnpackedSize = unpacked_size;
         entry.Offset += 8;
         uint signature;
         if (entry.IsPacked)
         {
             using (var input = file.CreateStream (entry.Offset, Math.Min (packed_size, 0x20u)))
             using (var reader = new ShsCompression (input))
             {
                 reader.Unpack (signature_buffer);
                 signature = LittleEndian.ToUInt32 (signature_buffer, 0);
             }
         }
         else
         {
             signature = file.View.ReadUInt32 (entry.Offset);
         }
         if (0 != signature)
         {
             IResource res;
             if (0x4D42 == (signature & 0xFFFF))
                 res = ImageFormat.Bmp;
             else if (0x020000 == signature || 0x0A0000 == signature)
                 res = ImageFormat.Tga;
             else
                 res = FormatCatalog.Instance.LookupSignature (signature).FirstOrDefault();
             if (res != null)
             {
                 entry.Type = res.Type;
                 var ext = res.Extensions.FirstOrDefault();
                 if (!string.IsNullOrEmpty (ext))
                     entry.Name = Path.ChangeExtension (entry.Name, ext);
             }
         }
     }
 }
Beispiel #37
0
        public override ArcFile TryOpen(ArcView file)
        {
            int count = file.View.ReadInt32 (4);
            if (!IsSaneCount (count))
                return null;
            uint index_size = file.View.ReadUInt32 (0xC);
            if (index_size < 2 || index_size > file.MaxOffset)
                return null;

            long base_offset = 0x118 + index_size;

            using (var mem = file.CreateStream (0x118, index_size))
            using (var z = new ZLibStream (mem, CompressionMode.Decompress))
            using (var index = new BinaryReader (z))
            {
                var name_buffer = new byte[0x100];
                var dir = new List<Entry> (count);
                for (int i = 0; i < count; ++i)
                {
                    int name_length = index.ReadInt32();
                    if (name_length <= 0 || name_length > name_buffer.Length)
                        return null;
                    if (name_length != index.Read (name_buffer, 0, name_length))
                        return null;
                    var name = Encodings.cp932.GetString (name_buffer, 0, name_length);
                    var entry = FormatCatalog.Instance.Create<PackedEntry> (name);
                    entry.Offset = index.ReadUInt32() + base_offset;
                    uint size    = index.ReadUInt32();
                    index.ReadUInt32();
                    uint flags = index.ReadUInt32();
                    uint packed_size = index.ReadUInt32();
                    entry.IsPacked = flags != 0 && packed_size != 0;
                    if (entry.IsPacked)
                    {
                        entry.Size = packed_size;
                        entry.UnpackedSize = size;
                    }
                    else
                    {
                        entry.Size = size;
                        entry.UnpackedSize = size;
                    }
                    if (!entry.CheckPlacement (file.MaxOffset))
                        return null;
                    dir.Add (entry);
                }
                return new ArcFile (file, this, dir);
            }
        }
Beispiel #38
0
 void DetectFileTypes(ArcView file, List<Entry> dir)
 {
     using (var input = file.CreateStream())
     using (var reader = new ArcView.Reader (input))
     {
         var buffer = new byte[0x10];
         foreach (PackedEntry entry in dir)
         {
             input.Position = entry.Offset;
             uint packed_size = reader.ReadUInt32();
             entry.UnpackedSize = reader.ReadUInt32();
             entry.Offset += 8;
             if (0 == packed_size)
             {
                 entry.Size = entry.UnpackedSize;
             }
             else
             {
                 entry.IsPacked = true;
                 entry.Size = packed_size;
             }
             if (entry.Size < 0x10)
                 continue;
             uint signature;
             if (entry.IsPacked)
             {
                 UnpackEntry (input, buffer);
                 signature = LittleEndian.ToUInt32 (buffer, 0);
             }
             else
                 signature = reader.ReadUInt32();
             IResource res;
             if (0x020000 == signature || 0x0A0000 == signature)
                 res = ImageFormat.Tga;
             else
                 res = AutoEntry.DetectFileType (signature);
             if (null != res)
             {
                 entry.Type = res.Type;
                 var ext = res.Extensions.FirstOrDefault();
                 if (!string.IsNullOrEmpty (ext))
                     entry.Name = Path.ChangeExtension (entry.Name, ext);
             }
         }
     }
 }
Beispiel #39
0
        public override ArcFile TryOpen(ArcView file)
        {
            List<Entry> dir = null;
            bool zero_signature = 0 == file.View.ReadInt16 (0);
            try
            {
                using (var input = file.CreateStream())
                {
                    if (zero_signature)
                        input.Seek (2, SeekOrigin.Begin);
                    dir = ReadIndex (input);
                    if (null != dir)
                        return new ArcFile (file, this, dir);
                }
            }
            catch { /* ignore parse errors */ }
            if (zero_signature || !file.Name.EndsWith (".nsa", StringComparison.InvariantCultureIgnoreCase))
                return null;

            var password = QueryPassword();
            if (string.IsNullOrEmpty (password))
                return null;
            var key = Encoding.ASCII.GetBytes (password);

            using (var input = new EncryptedViewStream (file, key))
            {
                dir = ReadIndex (input);
                if (null == dir)
                    return null;
                return new NsaEncryptedArchive (file, this, dir, key);
            }
        }
Beispiel #40
0
 public SteinsGateEncryptedStream(ArcView file, long offset, uint size)
 {
     m_stream = file.CreateStream (offset, size);
     m_should_dispose = true;
     m_base_pos = 0;
 }
Beispiel #41
0
 internal static Dictionary<string, Dictionary<int, Entry>> FindSec5Resr(string arc_name)
 {
     string dir_name = Path.GetDirectoryName (arc_name);
     var match = Directory.GetFiles (dir_name, "*.sec5");
     if (0 == match.Length)
     {
         string parent = Path.GetDirectoryName (dir_name);
         if (!string.IsNullOrEmpty (parent))
             match = Directory.GetFiles (parent, "*.sec5");
     }
     if (0 == match.Length)
         return null;
     using (var sec5 = new ArcView (match[0]))
     {
         if (!sec5.View.AsciiEqual (0, "SEC5"))
             return null;
         uint offset = 8;
         while (offset < sec5.MaxOffset)
         {
             string id = sec5.View.ReadString (offset, 4, Encoding.ASCII);
             if ("ENDS" == id)
                 break;
             uint section_size = sec5.View.ReadUInt32 (offset+4);
             offset += 8;
             if ("RESR" == id)
             {
                 using (var resr = sec5.CreateStream (offset, section_size))
                     return ReadResrSection (resr);
             }
             if ("RES2" == id)
             {
                 using (var res2 = sec5.CreateStream (offset, section_size))
                     return ReadRes2Section (res2);
             }
             offset += section_size;
         }
     }
     return null;
 }
Beispiel #42
0
        public override ArcFile TryOpen(ArcView file)
        {
            if (!file.Name.EndsWith (".g00", StringComparison.InvariantCultureIgnoreCase))
                return null;
            if (file.View.ReadByte (0) != 2)
                return null;
            uint width  = file.View.ReadUInt16 (1);
            uint height = file.View.ReadUInt16 (3);
            if (0 == width || width > 0x8000 || 0 == height || height > 0x8000)
                return null;
            int count = file.View.ReadInt16 (5);
            if (count <= 1 || count > 0x100)
                return null;
            var base_name = Path.GetFileNameWithoutExtension (file.Name);
            uint size = width * height * 4 + 0x12; // virtual TGA image size

            uint index_offset = 9;
            var dir = new List<Entry> (count);
            for (int i = 0; i < count; ++i)
            {
                var entry = new G00Entry {
                    Name = string.Format ("{0}#{1:D3}.tga", base_name, i),
                    X = file.View.ReadInt32 (index_offset),
                    Y = file.View.ReadInt32 (index_offset+4),
                    IsPacked = true,
                    UnpackedSize = size,
                };
                dir.Add (entry);
                index_offset += 0x18;
            }
            byte[] bitmap;
            using (var input = file.CreateStream (index_offset))
            using (var bin = new BinaryReader (input))
                bitmap = G00Reader.LzDecompress (bin, 2, 1);

            using (var input = new MemoryStream (bitmap))
            using (var reader = new BinaryReader (input))
            {
                if (reader.ReadInt32() != count)
                    return null;
                for (int i = 0; i < count; ++i)
                {
                    dir[i].Offset = reader.ReadUInt32();
                    dir[i].Size   = reader.ReadUInt32();
                }
            }
            dir = dir.Where (e => e.Size != 0).ToList();
            if (0 == dir.Count)
                return null;
            var info = new ImageMetaData { Width = width, Height = height, BPP = 32 };
            return new G00Archive (file, this, dir, info, bitmap);
        }
Beispiel #43
0
 public override ArcFile TryOpen(ArcView file)
 {
     uint header_size = file.View.ReadUInt32 (8);
     HzcMetaData image_info;
     using (var header = file.CreateStream (0, 0xC+header_size))
     {
         image_info = Hzc.Value.ReadMetaData (header) as HzcMetaData;
         if (null == image_info)
             return null;
     }
     int count = file.View.ReadInt32 (0x20);
     if (0 == count)
         count = 1;
     string base_name = Path.GetFileNameWithoutExtension (file.Name);
     int frame_size = image_info.UnpackedSize / count;
     var dir = new List<Entry> (count);
     for (int i = 0; i < count; ++i)
     {
         var entry = new Entry {
             Name = string.Format ("{0}#{1:D3}.tga", base_name, i),
             Type = "image",
             Offset = frame_size * i,
             Size = 0x12 + (uint)frame_size,
         };
         dir.Add (entry);
     }
     return new HzcArchive (file, this, dir, image_info);
 }