/// <summary> /// Decompresses the specified data. /// </summary> /// <param name="data">The data.</param> /// <param name="index">The index.</param> /// <param name="originalDataLength">Length of the original data.</param> protected byte[] Decompress(byte[] data, int index, int originalDataLength) { // PayBySquare limit is 550 utf-8 characters // longest utf-8 character is 4 bytes var buffer = new byte[originalDataLength]; // decompress data using (var input = new MemoryStream(data, index, data.Length - index)) using (var decompressor = new SharpCompress.Compressors.LZMA.LzmaStream(this.LzmaDecoderProperties, input, input.Length, originalDataLength)) { decompressor.Read(buffer, 0, originalDataLength); } // return the compressed data return(buffer); }
//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.Compressors.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); }