Beispiel #1
0
        public static T Decompress <T>(byte[] bytes)
        {
            var ba = MiniLZO.Decompress(bytes);
            var s  = Encoding.UTF8.GetString(ba);

            return(Newtonsoft.Json.JsonConvert.DeserializeObject <T>(s));
        }
        public byte[] Decode(byte[] input, int decodedSize)
        {
            if (decodedSize < 0)
            {
                throw new LoadDataException("Decoded size must be specified for MiniLZO encoding");
            }
            var result = new byte[decodedSize];

            MiniLZO.Decompress(input, result);
            return(result);
        }
Beispiel #3
0
        public static T Decompress <T>(Stream msi)
        {
            using (var mso = new MemoryStream())
            {
                msi.CopyTo(mso);
                var ba = mso.ToArray();

                var s = Encoding.UTF8.GetString(MiniLZO.Decompress(ba));
                return(Newtonsoft.Json.JsonConvert.DeserializeObject <T>(s));
            }
        }
Beispiel #4
0
            private void ReadBlock(Stream s, int index)
            {
                var compSize = _compressedBlockSizes[index];

                byte[] compressedData = new byte[compSize];
                s.Position = _blockStarts[index];
                s.Read(compressedData, 0, compSize);
                _curBlockData  = new byte[_uncompressedBlockSizes[index]];
                _curBlockIndex = index;
                MiniLZO.Decompress(compressedData, _curBlockData);
            }
Beispiel #5
0
        public object Send(object data)
        {
            try
            {
                CheckConnection();

                byte[] hdr = new byte[5];
                hdr[0] = (UseBJSON ? (byte)3 : (byte)0);
                byte[] dat        = fastBinaryJSON.BJSON.ToBJSON(data);
                bool   compressed = false;
                if (dat.Length > NetworkClient.Config.CompressDataOver)
                {
                    log.Debug("compressing data over limit : " + dat.Length.ToString("#,#"));
                    compressed = true;
                    dat        = MiniLZO.Compress(dat);
                    log.Debug("new size : " + dat.Length.ToString("#,#"));
                }
                byte[] len = Helper.GetBytes(dat.Length, false);
                hdr[0] = (byte)(3 + (compressed ? 4 : 0));
                Array.Copy(len, 0, hdr, 1, 4);
                _client.Client.Send(hdr);
                _client.Client.Send(dat);

                byte[] rechdr = new byte[5];
                using (NetworkStream n = new NetworkStream(_client.Client))
                {
                    n.Read(rechdr, 0, 5);
                    int    c         = Helper.ToInt32(rechdr, 1);
                    byte[] recd      = new byte[c];
                    int    bytesRead = 0;
                    int    chunksize = 1;
                    while (bytesRead < c && chunksize > 0)
                    {
                        bytesRead    +=
                            chunksize = n.Read
                                            (recd, bytesRead, c - bytesRead);
                    }
                    if ((rechdr[0] & (byte)4) == (byte)4)
                    {
                        recd = MiniLZO.Decompress(recd);
                    }
                    if ((rechdr[0] & (byte)3) == (byte)3)
                    {
                        return(fastBinaryJSON.BJSON.ToObject(recd));
                    }
                }
            }
            catch
            {
            }
            return(null);
        }
Beispiel #6
0
        public static bool MiniLzoDecompress(Stream fsIn, Stream fsOut, out int decompressedSize)
        {
            //_checkSum = 0;
            var decompressedData = new byte[LooseBufferSize];

            decompressedSize = 0;
            //byte b;

            try
            {
                while (true)
                {
                    var count = (int)fsIn.ReadByte();
                    count += (fsIn.ReadByte() << 8);
                    var buf = new byte[count];
                    count = fsIn.Read(buf, 0, count);
                    if (count != 0)
                    {
                        decompressedSize = MiniLZO.Decompress(buf, decompressedData);
                        //_checkSum += AcedUtils.Adler32(_decompData, 0, _decompData.Length);
                        fsOut.Write(decompressedData ?? throw new Exception("Null decompressedData"),
                                    0, decompressedSize);
                        decompressedData = null;
                    }

                    buf = null;

                    if (count != LooseBufferSize)
                    {
                        break;
                    }
                    if (count == 0)
                    {
                        break;
                    }
                }

                return(true);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }

            return(false);
        }
Beispiel #7
0
        private Stream MiniLZODecompressStream(Stream input)
        {
            MemoryStream memoryStream = new MemoryStream();

            const int BUFFERSIZE = 10000;

            byte[] buffer = new byte[BUFFERSIZE];
            while (true)
            {
                int bytesRead = input.Read(buffer, 0, BUFFERSIZE);
                if (bytesRead == 0)
                {
                    break;
                }
                memoryStream.Write(buffer, 0, bytesRead);
            }

            memoryStream.Seek(0, SeekOrigin.Begin);

            return(new MemoryStream(MiniLZO.Decompress(memoryStream.ToArray())));
        }
Beispiel #8
0
        public object Send(object data)
        {
            CheckConnection();

            byte[] hdr = new byte[5];
            hdr[0] = (UseBJSON ? (byte)3 : (byte)0);
            byte[] dat = fastBinaryJSON.BJSON.ToBJSON(data);
            byte[] len = Helper.GetBytes(dat.Length, false);
            Array.Copy(len, 0, hdr, 1, 4);
            _client.Client.Send(hdr);
            _client.Client.Send(dat);

            byte[] rechdr = new byte[5];
            using (NetworkStream n = new NetworkStream(_client.Client))
            {
                n.Read(rechdr, 0, 5);
                int    c         = Helper.ToInt32(rechdr, 1);
                byte[] recd      = new byte[c];
                int    bytesRead = 0;
                int    chunksize = 1;
                while (bytesRead < c && chunksize > 0)
                {
                    bytesRead    +=
                        chunksize = n.Read
                                        (recd, bytesRead, c - bytesRead);
                }
                if ((rechdr[0] & (byte)4) == (byte)4)
                {
                    recd = MiniLZO.Decompress(recd);
                }
                if ((rechdr[0] & (byte)3) == (byte)3)
                {
                    return(fastBinaryJSON.BJSON.ToObject(recd));
                }
            }
            return(null);
        }
Beispiel #9
0
        void Accept(TcpClient client)
        {
            using (NetworkStream n = client.GetStream())
            {
                while (client.Connected)
                {
                    this.count++;
                    byte[] c = new byte[5];
                    n.Read(c, 0, 5);
                    int    count     = BitConverter.ToInt32(c, 1);
                    byte[] data      = new byte[count];
                    int    bytesRead = 0;
                    int    chunksize = 1;
                    while (bytesRead < count && chunksize > 0)
                    {
                        bytesRead    +=
                            chunksize = n.Read
                                            (data, bytesRead, count - bytesRead);
                    }

                    object o = fastBinaryJSON.BJSON.ToObject(data);
                    if ((c[0] & (byte)4) == (byte)4)
                    {
                        data = MiniLZO.Decompress(data);
                    }

                    object r          = _handler(o);
                    bool   compressed = false;
                    var    dataret    = fastBinaryJSON.BJSON.ToBJSON(r);
                    r = null;
                    if (dataret.Length > NetworkClient.Config.CompressDataOver)
                    {
                        log.Debug("compressing data over limit : " + dataret.Length.ToString("#,#"));
                        compressed = true;
                        dataret    = MiniLZO.Compress(dataret);
                        log.Debug("new size : " + dataret.Length.ToString("#,#"));
                    }
                    if (dataret.Length > NetworkClient.Config.LogDataSizesOver)
                    {
                        log.Debug("data size (bytes) = " + dataret.Length.ToString("#,#"));
                    }

                    byte[] b   = BitConverter.GetBytes(dataret.Length);
                    byte[] hdr = new byte[5];
                    hdr[0] = (byte)(3 + (compressed ? 4 : 0));
                    Array.Copy(b, 0, hdr, 1, 4);
                    n.Write(hdr, 0, 5);
                    n.Write(dataret, 0, dataret.Length);

                    //n.Flush();
                    //return;
                    int  wait  = 0;
                    bool close = false;
                    var  dt    = FastDateTime.Now;
                    while (n.DataAvailable == false && close == false)
                    {
                        wait++;
                        if (wait < 10000) // kludge : for insert performance
                        {
                            Thread.Sleep(0);
                        }
                        else
                        {
                            Thread.Sleep(1);
                            // wait done -> close connection
                            if (FastDateTime.Now.Subtract(dt).TotalSeconds > NetworkClient.Config.KillConnectionSeconds)
                            {
                                close = true;
                            }
                        }
                    }
                    if (close)
                    {
                        break;
                    }
                }
                n.Close();
            }
            client.Close();
        }
Beispiel #10
0
        public static byte[] Compress(string str)
        {
            var bytes = Encoding.UTF8.GetBytes(str);

            return(MiniLZO.Decompress(bytes));
        }
Beispiel #11
0
        public static string Decompress(byte[] bytes)
        {
            var ba = MiniLZO.Decompress(bytes);

            return(Encoding.UTF8.GetString(ba));
        }
Beispiel #12
0
 public void Read(byte[] data, int uncompressedSize, IProgress <GameBoxReadProgress> progress = null)
 {
     byte[] buffer = new byte[uncompressedSize];
     MiniLZO.Decompress(data, buffer);
     Read(buffer, progress);
 }
Beispiel #13
0
 private MemoryStream MiniLZODecompressMemoryStream(MemoryStream input)
 {
     return(new MemoryStream(MiniLZO.Decompress(input.ToArray())));
 }