コード例 #1
0
ファイル: YuzCrypt.cs プロジェクト: crskycode/GARbro
 internal virtual void ReadYuzNames(byte[] yuz, FilenameMap filename_map)
 {
     using (var ystream = new MemoryStream(yuz))
         using (var zstream = ZLibCompressor.DeCompress(ystream))
             using (var input = new BinaryReader(zstream, Encoding.Unicode))
             {
                 long dir_offset = 0;
                 while (-1 != input.PeekChar())
                 {
                     uint entry_signature = input.ReadUInt32();
                     long entry_size      = input.ReadInt64();
                     if (entry_size < 0)
                     {
                         return;
                     }
                     dir_offset += 12 + entry_size;
                     uint hash      = input.ReadUInt32();
                     int  name_size = input.ReadInt16();
                     if (name_size > 0)
                     {
                         entry_size -= 6;
                         if (name_size * 2 <= entry_size)
                         {
                             var filename = new string (input.ReadChars(name_size));
                             filename_map.Add(hash, filename);
                         }
                     }
                     input.BaseStream.Position = dir_offset;
                 }
                 filename_map.AddShortcut("$", "startup.tjs");
             }
 }
コード例 #2
0
        byte[] file_decompress(byte[] bye)
        {
            MemoryStream ms   = new MemoryStream(bye);
            BinaryReader t_br = new BinaryReader(ms);

            byte[] unbye     = new byte[t_br.ReadInt32()];
            int    block_num = unbye.Length / 32768 + 1;

            int[] size   = new int[block_num];
            int[] offset = new int[block_num];
            int   i;

            for (i = 0; i < block_num; i++)
            {
                offset[i] = t_br.ReadInt32();
                if (i > 0)
                {
                    size[i - 1] = offset[i] - offset[i - 1];
                }
            }
            size[i - 1] = (int)ms.Length - offset[i - 1];
            int index = 0;

            for (i = 0; i < block_num; i++)
            {
                ms.Seek(offset[i], SeekOrigin.Begin);
                byte[] t_bye = ZLibCompressor.DeCompress(t_br.ReadBytes(size[i]));

                t_bye.CopyTo(unbye, index);
                index += t_bye.Length;
            }
            t_br.Close();
            ms.Close();
            return(unbye);
        }
コード例 #3
0
 /// <summary>Разжимает массив</summary>
 /// <param name="input">Входной массив.</param>
 /// <returns>Разжатый массив</returns>
 public static byte[] Decompress(byte[] input)
 {
     try
     {
         return(ZLibCompressor.DeCompress(input));
     }
     catch (Exception)
     {
         throw new ArgumentException("Ошибка декомпрессии");
     }
 }
コード例 #4
0
ファイル: QNTFormat.cs プロジェクト: cybort/ALDExplorer
        static byte[] GetQntAlpha(byte[] inputBytes, QntHeader qntHeader)
        {
            byte[] raw = null;
            using (var ms = new MemoryStream(inputBytes)) {
                ms.Position = qntHeader.headerSize + qntHeader.pixelSize;
                using (var rawMs = ZLibCompressor.DeCompress(ms))
                {
                    raw = rawMs.ToArray();
                }
            }

            int w = qntHeader.width;
            int h = qntHeader.height;

            byte[] pic = DecodeQntAlpha(raw, w, h);
            VerifyQntAlpha(raw, w, h, pic);

            return(pic);
        }
コード例 #5
0
ファイル: CDRcmpr.cs プロジェクト: dannisliang/VS
        public override void Read(BinaryReader br, int len, int[] bs = null)
        {
            if (this.Type != RiffType.LIST)
            {
                base.Read(br, len);
            }
            int zdataLen, dataLen, zblkLen, blocksLen4;

            zdataLen   = br.ReadInt32();
            dataLen    = br.ReadInt32();
            zblkLen    = br.ReadInt32();
            blocksLen4 = br.ReadInt32();
            br.ReadInt64();
            byte[] zdata = new byte[zdataLen - 8];
            byte[] zblk  = new byte[zblkLen - 8];
            br.Read(zdata, 0, zdataLen - 8);
            br.ReadInt64();
            br.Read(zblk, 0, zblkLen - 8);
            byte[] data   = ZLibCompressor.DeCompress(zdata); //Zlib.deCompressBytes(zdata);
            byte[] blocks = ZLibCompressor.DeCompress(zblk);  // Zlib.deCompressBytes(zblk);
            //byte[] data = ZLibNet.ZLibCompressor.DeCompress(zdata);
            //byte[] blocks = ZLibNet.ZLibCompressor.DeCompress(zblk);
            FileStream fs = File.Open("D:\\blocks", FileMode.Create);

            fs.Write(blocks, 0, blocks.Length);
            fs.Close();
            fs = File.Open("D:\\data", FileMode.Create);
            fs.Write(data, 0, data.Length);
            fs.Close();
            int[] blockSizes = new int[blocks.Length / 4];
            for (int i = 0, j = 0; i < blocks.Length; i += 4, j++)
            {
                blockSizes[j] = BitConverter.ToInt32(blocks, i);
            }
            MemoryStream ms  = new MemoryStream(data);
            BinaryReader br1 = new BinaryReader(ms);

            subList = RiffConn.ReadRiff(this.Parent, br1, blockSizes);
            br1.Close();
            ms.Close();
        }
コード例 #6
0
ファイル: QNTFormat.cs プロジェクト: cybort/ALDExplorer
        public static byte[] GetQntPixels(byte[] inputBytes, QntHeader qntHeader)
        {
            byte[] pic;
            if (qntHeader.pixelSize > 0)
            {
                byte[] raw = null;
                using (var ms = new MemoryStream(inputBytes, qntHeader.headerSize, qntHeader.pixelSize))
                {
                    using (var rawMs = ZLibCompressor.DeCompress(ms)) {
                        raw = rawMs.ToArray();
                    }

                    int endPosition = (int)ms.Position;
                    //int length = (int)ms.Position - qntHeader.headerSize;
                    //if (length != qntHeader.pixelSize + qntHeader.alphaSize)
                    //{
                    //}
                }

                int w = qntHeader.width;
                int h = qntHeader.height;
                if (raw != null && raw.Length < w * h * 3)
                {
                    throw new InvalidDataException("Size of decompressed data is wrong");
                }

                pic = DecodeQntPixels(raw, w, h);
                //VerifyQntPixels(raw, w, h, pic);
            }
            else
            {
                int w = qntHeader.width;
                int h = qntHeader.height;
                pic = new byte[w * h * 3];
            }

            return(pic);
        }
コード例 #7
0
ファイル: Util.cs プロジェクト: volt72/Sm4sh-Tools
 public static byte[] DeCompress(byte[] src) =>
 ZLibCompressor.DeCompress(src);
コード例 #8
0
        public override ArcFile TryOpen(ArcView file)
        {
            long base_offset = 0;

            if (0x5a4d == file.View.ReadUInt16(0))  // 'MZ'
            {
                base_offset = SkipExeHeader(file, s_xp3_header);
            }
            if (!file.View.BytesEqual(base_offset, s_xp3_header))
            {
                return(null);
            }
            long dir_offset = base_offset + file.View.ReadInt64(base_offset + 0x0b);

            if (dir_offset < 0x13 || dir_offset >= file.MaxOffset)
            {
                return(null);
            }
            if (0x80 == file.View.ReadUInt32(dir_offset))
            {
                dir_offset = base_offset + file.View.ReadInt64(dir_offset + 9);
                if (dir_offset < 0x13 || dir_offset >= file.MaxOffset)
                {
                    return(null);
                }
            }
            int header_type = file.View.ReadByte(dir_offset);

            if (0 != header_type && 1 != header_type)
            {
                return(null);
            }

            Stream header_stream;

            if (0 == header_type) // read unpacked header
            {
                long header_size = file.View.ReadInt64(dir_offset + 1);
                if (header_size > uint.MaxValue)
                {
                    return(null);
                }
                header_stream = file.CreateStream(dir_offset + 9, (uint)header_size);
            }
            else // read packed header
            {
                long packed_size = file.View.ReadInt64(dir_offset + 1);
                if (packed_size > uint.MaxValue)
                {
                    return(null);
                }
                long header_size = file.View.ReadInt64(dir_offset + 9);
                using (var input = file.CreateStream(dir_offset + 17, (uint)packed_size))
                    header_stream = ZLibCompressor.DeCompress(input);
            }

            var crypt_algorithm = new Lazy <ICrypt> (() => QueryCryptAlgorithm(file), false);

            var dir = new List <Entry>();

            dir_offset = 0;
            using (var header = new BinaryReader(header_stream, Encoding.Unicode))
                using (var filename_map = new FilenameMap())
                {
                    while (-1 != header.PeekChar())
                    {
                        uint entry_signature = header.ReadUInt32();
                        long entry_size      = header.ReadInt64();
                        if (entry_size < 0)
                        {
                            return(null);
                        }
                        dir_offset += 12 + entry_size;
                        if (0x656C6946 == entry_signature) // "File"
                        {
                            var entry = new Xp3Entry();
                            while (entry_size > 0)
                            {
                                uint section      = header.ReadUInt32();
                                long section_size = header.ReadInt64();
                                entry_size -= 12;
                                if (section_size > entry_size)
                                {
                                    break;
                                }
                                entry_size -= section_size;
                                long next_section_pos = header.BaseStream.Position + section_size;
                                switch (section)
                                {
                                case 0x6f666e69: // "info"
                                    if (entry.Size != 0 || !string.IsNullOrEmpty(entry.Name))
                                    {
                                        goto NextEntry; // ambiguous entry, ignore
                                    }
                                    entry.IsEncrypted = 0 != header.ReadUInt32();
                                    long file_size   = header.ReadInt64();
                                    long packed_size = header.ReadInt64();
                                    if (file_size >= uint.MaxValue || packed_size > uint.MaxValue || packed_size > file.MaxOffset)
                                    {
                                        goto NextEntry;
                                    }
                                    entry.IsPacked     = file_size != packed_size;
                                    entry.Size         = (uint)packed_size;
                                    entry.UnpackedSize = (uint)file_size;

                                    if (entry.IsEncrypted || ForceEncryptionQuery)
                                    {
                                        entry.Cipher = crypt_algorithm.Value;
                                    }
                                    else
                                    {
                                        entry.Cipher = NoCryptAlgorithm;
                                    }

                                    var name = entry.Cipher.ReadName(header);
                                    if (null == name)
                                    {
                                        goto NextEntry;
                                    }
                                    if (entry.Cipher.ObfuscatedIndex && ObfuscatedPathRe.IsMatch(name))
                                    {
                                        goto NextEntry;
                                    }
                                    if (filename_map.Count > 0)
                                    {
                                        name = filename_map.Get(entry.Hash, name);
                                    }
                                    if (name.Length > 0x100)
                                    {
                                        goto NextEntry;
                                    }
                                    entry.Name        = name;
                                    entry.Type        = FormatCatalog.Instance.GetTypeFromName(name);
                                    entry.IsEncrypted = !(entry.Cipher is NoCrypt) &&
                                                        !(entry.Cipher.StartupTjsNotEncrypted && "startup.tjs" == name);
                                    break;

                                case 0x6d676573: // "segm"
                                    int segment_count = (int)(section_size / 0x1c);
                                    if (segment_count > 0)
                                    {
                                        for (int i = 0; i < segment_count; ++i)
                                        {
                                            bool compressed          = 0 != header.ReadInt32();
                                            long segment_offset      = base_offset + header.ReadInt64();
                                            long segment_size        = header.ReadInt64();
                                            long segment_packed_size = header.ReadInt64();
                                            if (segment_offset > file.MaxOffset || segment_packed_size > file.MaxOffset)
                                            {
                                                goto NextEntry;
                                            }
                                            var segment = new Xp3Segment {
                                                IsCompressed = compressed,
                                                Offset       = segment_offset,
                                                Size         = (uint)segment_size,
                                                PackedSize   = (uint)segment_packed_size
                                            };
                                            entry.Segments.Add(segment);
                                        }
                                        entry.Offset = entry.Segments.First().Offset;
                                    }
                                    break;

                                case 0x726c6461: // "adlr"
                                    if (4 == section_size)
                                    {
                                        entry.Hash = header.ReadUInt32();
                                    }
                                    break;

                                default: // unknown section
                                    break;
                                }
                                header.BaseStream.Position = next_section_pos;
                            }
                            if (!string.IsNullOrEmpty(entry.Name) && entry.Segments.Any())
                            {
                                if (entry.Cipher.ObfuscatedIndex)
                                {
                                    DeobfuscateEntry(entry);
                                }
                                dir.Add(entry);
                            }
                        }
                        else if (entry_size > 7)
                        {
                            // 0x6E666E68 == entry_signature    // "hnfn"
                            // 0x6C696D73 == entry_signature    // "smil"
                            // 0x46696C65 == entry_signature    // "eliF"
                            // 0x757A7559 == entry_signature    // "Yuzu"
                            uint hash      = header.ReadUInt32();
                            int  name_size = header.ReadInt16();
                            if (name_size > 0)
                            {
                                entry_size -= 6;
                                if (name_size * 2 <= entry_size)
                                {
                                    var filename = new string (header.ReadChars(name_size));
                                    filename_map.Add(hash, filename);
                                }
                            }
                        }
NextEntry:
                        header.BaseStream.Position = dir_offset;
                    }
                }
            if (0 == dir.Count)
            {
                return(null);
            }
            var arc = new ArcFile(file, this, dir);

            try
            {
                if (crypt_algorithm.IsValueCreated)
                {
                    crypt_algorithm.Value.Init(arc);
                }
                return(arc);
            }
            catch
            {
                arc.Dispose();
                throw;
            }
        }
コード例 #9
0
 public static byte[] DeCompress(byte[] src)
 {
     return(ZLibCompressor.DeCompress(src));
 }
コード例 #10
0
        private void getNusbankIdInfo(string fname)
        {
            FileData nus3 = new FileData(fname);

            nus3.Endian = Endianness.Little;
            if (nus3.Magic() != "NUS3")  //NUS3
            {
                nus3.seek(0);
                byte[]     data       = nus3.read(nus3.eof());
                byte[]     nus3_decom = ZLibCompressor.DeCompress(data);
                FileOutput newf       = new FileOutput();
                newf.writeBytes(nus3_decom);
                newf.save(fname);
                data        = null;
                nus3_decom  = null;
                nus3        = new FileData(fname);
                nus3.Endian = Endianness.Little;
            }
            else
            {
                nus3.seek(0);
            }

            nus3.seek(4);
            uint size = nus3.readUInt();

            nus3.skip(8);
            uint tocSize = nus3.readUInt();
            uint contentCount = nus3.readUInt();
            uint offset = 0x14 + tocSize;
            uint propOffset = 0, propSize = 0, binfOffset = 0, binfSize = 0, grpOffset = 0, grpSize = 0,
                 dtonOffset = 0, dtonSize = 0, toneOffset = 0, toneSize = 0, junkOffset = 0, junkSize = 0,
                 markOffset = 0, markSize = 0, packOffset = 0, packSize = 0;

            for (int i = 0; i < contentCount; ++i)
            {
                string content = nus3.readString(nus3.pos(), 4);
                nus3.skip(4);
                uint contentSize = nus3.readUInt();
                if (content.Equals("PROP"))
                {
                    propOffset = offset;
                    propSize   = contentSize;
                }
                else if (content.Equals("BINF"))
                {
                    binfOffset = offset;
                    binfSize   = contentSize;
                }
                else if (content.Equals("GRP "))
                {
                    grpOffset = offset;
                    grpSize   = contentSize;
                }
                else if (content.Equals("DTON"))
                {
                    dtonOffset = offset;
                    dtonSize   = contentSize;
                }
                else if (content.Equals("TONE"))
                {
                    toneOffset = offset;
                    toneSize   = contentSize;
                }
                else if (content.Equals("JUNK"))
                {
                    junkOffset = offset;
                    junkSize   = contentSize;
                }
                else if (content.Equals("MARK"))
                {
                    markOffset = offset;
                    markSize   = contentSize;
                }
                else if (content.Equals("PACK"))
                {
                    packOffset = offset;
                    packSize   = contentSize;
                }
                offset += 8 + contentSize;
            }

            nus3.seek((int)binfOffset + 16);
            byte   binfStringSize = nus3.readByte();
            string binfString     = nus3.readString(nus3.pos(), binfStringSize - 1);

            nus3.skip(binfStringSize);
            int padding = (binfStringSize + 1) % 4;

            if (padding != 0)
            {
                nus3.skip(Math.Abs(padding - 4));
            }
            nusIdPos = (uint)nus3.pos();
            nusId    = nus3.readUInt();
            nus3     = null;
        }