Exemplo n.º 1
0
        public static List <byte[]> Unchunk(byte[] input)
        {
            var ms  = new System.IO.MemoryStream();
            var min = new System.IO.MemoryStream(input);

            min.Position = 4;
            Lzs.Decode(min, ms);
            System.Diagnostics.Debug.WriteLine("FF:Unchunk:LZS expanded {0} bytes to {1} bytes", input.Length, ms.Length);
            byte[] scratch = new byte[4];
            ms.Position = 2;
            ms.Read(scratch, 0, 4);
            int numsection = BitConverter.ToInt32(scratch, 0);

            System.Diagnostics.Debug.WriteLine("FF:Unchunk:{0} sections", numsection, 0);
            List <byte[]> sections = new List <byte[]>();

            foreach (int i in Enumerable.Range(0, numsection))
            {
                ms.Position = 6 + i * 4;
                ms.Read(scratch, 0, 4);
                ms.Position = BitConverter.ToInt32(scratch, 0);
                ms.Read(scratch, 0, 4);
                int    len = BitConverter.ToInt32(scratch, 0);
                byte[] s   = new byte[len];
                ms.Read(s, 0, len);
                sections.Add(s);
            }
            return(sections);
        }
Exemplo n.º 2
0
        public static byte[] Chunk(List <byte[]> input)
        {
            var ms = new System.IO.MemoryStream();

            byte[] scratch = new byte[4];
            ms.Write(scratch, 0, 2);
            ms.Write(BitConverter.GetBytes(input.Count), 0, 4);
            int offset = 0x2A;

            foreach (var s in input)
            {
                Bytes.WriteInt(ms, offset);
                offset += 4;
                offset += s.Length;
            }
            foreach (var s in input)
            {
                Bytes.WriteInt(ms, s.Length);
                ms.Write(s, 0, s.Length);
            }
            ms.Position = 0;
            var compress = new System.IO.MemoryStream();

            Lzs.Encode(ms, compress);
            byte[] data = new byte[compress.Length + 4];
            compress.Position = 0;
            compress.Read(data, 4, (int)compress.Length);
            Bytes.WriteInt(data, 0, (int)compress.Length);
            return(data);
        }
Exemplo n.º 3
0
        //private int _cacheCounter = 0;

        private CacheEntry GetCache(DirectoryEntry e) {
            CacheEntry ce;
            if (!_cache.TryGetValue(e.Offset, out ce)) {
                ce = new CacheEntry() { File = e.Filename };
                byte[] data;
                lock (_data) {
                    switch (e.Flags & FileFlags.COMPRESSION_FLAGS) {
                        case FileFlags.CompressLZS:
                            data = new byte[e.Length];
                            _data.Position = e.Offset;
                            _data.Read(data, 0, e.Length);
                            var ms = new System.IO.MemoryStream(data);
                            var output = new System.IO.MemoryStream();
                            Lzs.Decode(ms, output);
                            data = new byte[output.Length];
                            output.Position = 0;
                            output.Read(data, 0, data.Length);
                            ce.Data = data;
                            break;
                        case FileFlags.CompressLZMA:
                            _data.Position = e.Offset;
                            int decSize = _data.ReadInt(), propSize = _data.ReadInt();
                            byte[] props = new byte[propSize];
                            _data.Read(props, 0, props.Length);
                            byte[] cdata = new byte[e.Length - propSize - 8];
                            _data.Read(cdata, 0, cdata.Length);
                            data = new byte[decSize];
                            /*
                            var lzma = new SharpCompress.Compressor.LZMA.LzmaStream(props, new System.IO.MemoryStream(cdata));
                            lzma.Read(data, 0, data.Length);
                             */
                            int srcSize = cdata.Length;
                            switch (LzmaUncompress(data, ref decSize, cdata, ref srcSize, props, props.Length)) {
                                case SZ_OK:
                                    //Woohoo!
                                    break;
                                default:
                                    throw new IrosArcException("Error decompressing " + e.Filename);
                            }
                            ce.Data = data;
                            break;
                        default:
                            throw new IrosArcException("Bad compression flags " + e.Flags.ToString());
                    }
                }
                _cache.AddOrUpdate(e.Offset, ce, (_, __) => ce);
            }
            ce.LastAccess = DateTime.Now;
            CleanCache();

            /*
            if ((_cacheCounter++ % 100) == 0)
                System.Diagnostics.Debug.WriteLine("IRO cache contents; " + String.Join(",", _cache.Values.Select(e => e.File)));
            */

            return ce;
        }