Ejemplo n.º 1
0
    public static byte[] Decompress(byte[] data)
    {
        try
        {
            string result = string.Empty;
            byte[] buffer = { };

            MemoryStream ms = new MemoryStream(data);
            Stream s = new DeflateStream(ms, CompressionMode.Decompress);

            int len = 4096;

            while (true)
            {
                int oldLen = buffer.Length;
                Array.Resize(ref buffer, oldLen + len);
                int size = s.Read(buffer, oldLen, len);
                if (size != len)
                {
                    Array.Resize(ref buffer, buffer.Length - (len - size));
                    break;
                }
                if (size <= 0)
                    break;
            }
            s.Close();

            return buffer;
        }
        catch
        {
            return null;
        }
    }
    public static byte[] Inflate(this byte[] data)
    {
        using (MemoryStream input = new MemoryStream(data))
        {
            input.Write(data, 0, data.Length);
            input.Position = 0;

            using (DeflateStream gzip = new DeflateStream(input, CompressionMode.Decompress))
            {
                using (MemoryStream output = new MemoryStream())
                {
                    var buffer = new byte[ushort.MaxValue]; int count;

                    while ((count = gzip.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        output.Write(buffer, 0, count);
                    }

                    return output.ToArray();
                }
            }
        }
    }
Ejemplo n.º 3
0
        string GetHttpResponse(HttpWebResponse res)
        {
            const int WorkingBufferSize = 1024;
            string requestTokenResponse = string.Empty;
            string contentEncoding = string.Empty;

            using (var respStream = res.GetResponseStream())
            {
            #if !SILVERLIGHT || WINDOWS_PHONE
                contentEncoding = res.Headers["Content-Encoding"] ?? "";
            #endif
                if (contentEncoding.ToLower().Contains("gzip"))
                {
                    using (var gzip = new GZipStream(respStream, CompressionMode.Decompress))
                    {
                        using (var memStr = new MemoryStream())
                        {
                            byte[] buffer = new byte[WorkingBufferSize];
                            int n;
                            while ((n = gzip.Read(buffer, 0, buffer.Length)) != 0)
                            {
                                memStr.Write(buffer, 0, n);
                            }
                            memStr.Position = 0;
                            using (var strmRdr = new StreamReader(memStr))
                            {
                                requestTokenResponse = strmRdr.ReadToEnd();
                            }
                        }
                    }
                }
                else if (contentEncoding.ToLower().Contains("deflate"))
                {
                    using (var gzip = new DeflateStream(respStream, CompressionMode.Decompress))
                    {
                        using (var memStr = new MemoryStream())
                        {
                            byte[] buffer = new byte[WorkingBufferSize];
                            int n;
                            while ((n = gzip.Read(buffer, 0, buffer.Length)) != 0)
                            {
                                memStr.Write(buffer, 0, n);
                            }
                            memStr.Position = 0;
                            using (var strmRdr = new StreamReader(memStr))
                            {
                                requestTokenResponse = strmRdr.ReadToEnd();
                            }
                        }
                    }
                }
                else
                {
                    using (var respReader = new StreamReader(respStream))
                    {
                        requestTokenResponse = respReader.ReadToEnd();
                    }
                }
            }

            return requestTokenResponse;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 读取请求返回的数据
        /// </summary>
        /// <param name="request">请求对象</param>
        /// <returns></returns>
        private byte[] GetData(HttpWebRequest request)
        {
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream          stream   = response.GetResponseStream();

            responseHeaders = response.Headers;
            //SaveCookiesToDisk();

            DownloadEventArgs args = new DownloadEventArgs();

            if (responseHeaders[HttpResponseHeader.ContentLength] != null)
            {
                args.TotalBytes = Convert.ToInt32(responseHeaders[HttpResponseHeader.ContentLength]);
            }

            MemoryStream ms    = new MemoryStream();
            int          count = 0;

            byte[] buf = new byte[bufferSize];
            while ((count = stream.Read(buf, 0, buf.Length)) > 0)
            {
                ms.Write(buf, 0, count);
                if (this.DownloadProgressChanged != null)
                {
                    args.BytesReceived += count;
                    args.ReceivedData   = new byte[count];
                    Array.Copy(buf, args.ReceivedData, count);
                    this.DownloadProgressChanged(this, args);
                }
            }
            stream.Close();
            //解压
            if (ResponseHeaders[HttpResponseHeader.ContentEncoding] != null)
            {
                MemoryStream msTemp = new MemoryStream();
                count = 0;
                buf   = new byte[100];
                switch (ResponseHeaders[HttpResponseHeader.ContentEncoding].ToLower())
                {
                case "gzip":
                    GZipStream gzip = new GZipStream(ms, CompressionMode.Decompress);
                    while ((count = gzip.Read(buf, 0, buf.Length)) > 0)
                    {
                        msTemp.Write(buf, 0, count);
                    }
                    return(msTemp.ToArray());

                case "deflate":
                    DeflateStream deflate = new DeflateStream(ms, CompressionMode.Decompress);
                    while ((count = deflate.Read(buf, 0, buf.Length)) > 0)
                    {
                        msTemp.Write(buf, 0, count);
                    }
                    return(msTemp.ToArray());

                default:
                    break;
                }
            }
            return(ms.ToArray());
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Gets the texture data of an item
        /// </summary>
        /// <param name="offset">The offset of the item</param>
        /// <returns>The texture data</returns>
        public static TEXData GetTex(int offset, string datName)
        {
            int datNum = ((offset / 8) & 0x0F) / 2;

            var datPath = string.Format(Info.datDir, datName, datNum);

            var storeOffset = offset;

            offset = Helper.OffsetCorrection(datNum, offset);

            List <byte> decompressedData = new List <byte>();

            TEXData texData = new TEXData();

            using (BinaryReader br = new BinaryReader(File.OpenRead(datPath)))
            {
                br.BaseStream.Seek(offset, SeekOrigin.Begin);

                int headerLength         = br.ReadInt32();
                int fileType             = br.ReadInt32();
                int uncompressedFileSize = br.ReadInt32();
                br.ReadBytes(8);
                texData.MipCount = br.ReadInt32();

                int endOfHeader      = offset + headerLength;
                int mipMapInfoOffset = offset + 24;

                br.BaseStream.Seek(endOfHeader + 4, SeekOrigin.Begin);

                texData.Type   = br.ReadInt32();
                texData.Width  = br.ReadInt16();
                texData.Height = br.ReadInt16();

                for (int i = 0, j = 0; i < texData.MipCount; i++)
                {
                    br.BaseStream.Seek(mipMapInfoOffset + j, SeekOrigin.Begin);

                    int offsetFromHeaderEnd = br.ReadInt32();
                    int mipMapLength        = br.ReadInt32();
                    int mipMapSize          = br.ReadInt32();
                    int mipMapStart         = br.ReadInt32();
                    int mipMapParts         = br.ReadInt32();

                    int mipMapPartOffset = endOfHeader + offsetFromHeaderEnd;

                    br.BaseStream.Seek(mipMapPartOffset, SeekOrigin.Begin);

                    br.ReadBytes(8);
                    int compressedSize   = br.ReadInt32();
                    int uncompressedSize = br.ReadInt32();

                    if (mipMapParts > 1)
                    {
                        byte[] compressedData       = br.ReadBytes(compressedSize);
                        byte[] decompressedPartData = new byte[uncompressedSize];

                        using (MemoryStream ms = new MemoryStream(compressedData))
                        {
                            using (DeflateStream ds = new DeflateStream(ms, CompressionMode.Decompress))
                            {
                                ds.Read(decompressedPartData, 0, uncompressedSize);
                            }
                        }

                        decompressedData.AddRange(decompressedPartData);

                        for (int k = 1; k < mipMapParts; k++)
                        {
                            byte check = br.ReadByte();
                            while (check != 0x10)
                            {
                                check = br.ReadByte();
                            }

                            br.ReadBytes(7);
                            compressedSize   = br.ReadInt32();
                            uncompressedSize = br.ReadInt32();

                            if (compressedSize != 32000)
                            {
                                compressedData       = br.ReadBytes(compressedSize);
                                decompressedPartData = new byte[uncompressedSize];
                                using (MemoryStream ms = new MemoryStream(compressedData))
                                {
                                    using (DeflateStream ds = new DeflateStream(ms, CompressionMode.Decompress))
                                    {
                                        ds.Read(decompressedPartData, 0, uncompressedSize);
                                    }
                                }
                                decompressedData.AddRange(decompressedPartData);
                            }
                            else
                            {
                                decompressedPartData = br.ReadBytes(uncompressedSize);
                                decompressedData.AddRange(decompressedPartData);
                            }
                        }
                    }
                    else
                    {
                        if (compressedSize != 32000)
                        {
                            var compressedData   = br.ReadBytes(compressedSize);
                            var uncompressedData = new byte[uncompressedSize];

                            using (MemoryStream ms = new MemoryStream(compressedData))
                            {
                                using (DeflateStream ds = new DeflateStream(ms, CompressionMode.Decompress))
                                {
                                    ds.Read(uncompressedData, 0, uncompressedSize);
                                }
                            }

                            decompressedData.AddRange(uncompressedData);
                        }
                        else
                        {
                            var decompressedPartData = br.ReadBytes(uncompressedSize);
                            decompressedData.AddRange(decompressedPartData);
                        }
                    }
                    j = j + 20;
                }

                if (decompressedData.Count < uncompressedFileSize)
                {
                    int    difference = uncompressedFileSize - decompressedData.Count;
                    byte[] padding    = new byte[difference];
                    Array.Clear(padding, 0, difference);
                    decompressedData.AddRange(padding);
                }
            }

            texData.BMP        = TextureToBitmap(decompressedData.ToArray(), texData.Type, texData.Width, texData.Height);
            texData.TypeString = Info.TextureTypes[texData.Type];
            texData.TexOffset  = storeOffset;
            texData.TEXDatName = datName;

            return(texData);
        }
Ejemplo n.º 6
0
        public void Read(BinaryReader br)
        {
            if (br.BaseStream.Length < 4)
            {
                throw new Exception("Fragmented receive, should not happen! (4 size header)");
            }

            Size    = br.ReadUInt16();
            Channel = br.ReadByte();

            br.ReadByte(); // padding

            if (Size > br.BaseStream.Length)
            {
                throw new Exception("Fragmented receive, should not happen!");
            }

            if (Channel == 0xFF) // Internal channel: Send timeout checking, ignore the packet
            {
                return;
            }

            if (Channel != 0) // 0 == ReliableStreamChannel (no extra data), Move message uses channels
            {
                Debugger.Break();

                SequenceNumber = br.ReadUInt32(); // Sequence number? if (previousValue - newValue < 0) { process packet; previousValue = newValue; }
                br.ReadInt32();                   // 0xDEADBEEF
                br.ReadInt32();                   // skip
            }

            var packetBeginPosition = br.BaseStream.Position;

            using (var reader = new ProtocolBufferReader(br, ProtocolBufferFlags.DontFragment))
            {
                reader.ReadProtocolFlags();

                ushort type;
                bool   compress;

                reader.ReadPacketType(out type, out compress);

                Type     = (ClientMessageOpcode)type;
                Compress = compress;

                reader.ReadXORCheck((int)(br.BaseStream.Position - packetBeginPosition));
            }

            var xorCheckPosition = (int)br.BaseStream.Position;

            var readBr = br;

            BufferData buffer = null;

            if (Compress)
            {
                var someType = br.ReadByte(); // 0 = No compression
                if (someType >= 2)
                {
                    throw new Exception("Invalid compress type received!");
                }

                if (someType == 1)
                {
                    Debugger.Break(); // TODO: test

                    var uncompressedSize = br.ReadInt32();

                    byte[] uncompressedData;
                    var    offset = 0;

                    if (uncompressedSize > BufferManager.BlockSize)
                    {
                        uncompressedData = new byte[uncompressedSize];
                    }
                    else
                    {
                        buffer = BufferManager.RequestBuffer();

                        uncompressedData = buffer.Buffer;
                        offset           = buffer.BaseOffset;
                    }

                    using (var deflateStream = new DeflateStream(br.BaseStream, CompressionMode.Decompress, true)) // TODO: test if the br.BaseStream is cool as the Stream input for the DeflateStream
                        deflateStream.Read(uncompressedData, offset, uncompressedSize);

                    readBr = buffer != null?buffer.GetReader() : new BinaryReader(new MemoryStream(uncompressedData, 0, uncompressedSize, false), Encoding.UTF8, false);
                }
            }

            // ReSharper disable SwitchStatementMissingSomeCases
            switch (Type)
            {
            case ClientMessageOpcode.Login:
                Message = new LoginMessage();
                break;

            case ClientMessageOpcode.Move:
                Message = new MoveMessage();
                break;

            case ClientMessageOpcode.CallServerMethod:
                Message = new CallServerMethodMessage();
                break;

            case ClientMessageOpcode.Ping:
                Message = new PingMessage();
                break;

            default:
                throw new Exception($"Unable to handle packet type {Type}, because it's a Server -> Client packet!");
            }
            // ReSharper restore SwitchStatementMissingSomeCases

            using (var reader = new ProtocolBufferReader(readBr, ProtocolBufferFlags.DontFragment))
            {
                reader.ReadProtocolFlags();

                // Subtype and Message.Read()
                reader.ReadDebugByte(41);

                if ((Message.SubtypeFlags & ClientMessageSubtypeFlag.HasSubtype) == ClientMessageSubtypeFlag.HasSubtype)
                {
                    Message.RawSubtype = reader.ReadByte();
                    if (Message.RawSubtype < Message.MinSubtype || Message.RawSubtype > Message.MaxSubtype)
                    {
                        throw new Exception("Invalid Subtype found!");
                    }
                }

                Message.Read(reader);

                reader.ReadDebugByte(42);

                reader.ReadXORCheck((int)br.BaseStream.Position - xorCheckPosition);
            }

            if (buffer != null) // If we requested a buffer for decompressing, free it
            {
                BufferManager.FreeBuffer(buffer);
            }
        }
Ejemplo n.º 7
0
        public virtual void Load(Stream stream)
        {
            var reader = new DataReader(stream);

            reader.Position = 0;

            var ident = reader.ReadUInt32();

            Version = reader.ReadInt32();
            var systemFlags   = reader.ReadUInt32();
            var graphicsFlags = reader.ReadUInt32();

            SystemPagesDiv16     = (int)(systemFlags >> 27) & 0x1;
            SystemPagesDiv8      = (int)(systemFlags >> 26) & 0x1;
            SystemPagesDiv4      = (int)(systemFlags >> 25) & 0x1;
            SystemPagesDiv2      = (int)(systemFlags >> 24) & 0x1;
            SystemPagesMul1      = (int)(systemFlags >> 17) & 0x7F;
            SystemPagesMul2      = (int)(systemFlags >> 11) & 0x3F;
            SystemPagesMul4      = (int)(systemFlags >> 7) & 0xF;
            SystemPagesMul8      = (int)(systemFlags >> 5) & 0x3;
            SystemPagesMul16     = (int)(systemFlags >> 4) & 0x1;
            SystemPagesSizeShift = (int)(systemFlags >> 0) & 0xF;
            var systemBaseSize = BASE_SIZE << SystemPagesSizeShift;
            var systemSize     =
                systemBaseSize * SystemPagesDiv16 / 16 +
                systemBaseSize * SystemPagesDiv8 / 8 +
                systemBaseSize * SystemPagesDiv4 / 4 +
                systemBaseSize * SystemPagesDiv2 / 2 +
                systemBaseSize * SystemPagesMul1 * 1 +
                systemBaseSize * SystemPagesMul2 * 2 +
                systemBaseSize * SystemPagesMul4 * 4 +
                systemBaseSize * SystemPagesMul8 * 8 +
                systemBaseSize * SystemPagesMul16 * 16;

            GraphicsPagesDiv16     = (int)(graphicsFlags >> 27) & 0x1;
            GraphicsPagesDiv8      = (int)(graphicsFlags >> 26) & 0x1;
            GraphicsPagesDiv4      = (int)(graphicsFlags >> 25) & 0x1;
            GraphicsPagesDiv2      = (int)(graphicsFlags >> 24) & 0x1;
            GraphicsPagesMul1      = (int)(graphicsFlags >> 17) & 0x7F;
            GraphicsPagesMul2      = (int)(graphicsFlags >> 11) & 0x3F;
            GraphicsPagesMul4      = (int)(graphicsFlags >> 7) & 0xF;
            GraphicsPagesMul8      = (int)(graphicsFlags >> 5) & 0x3;
            GraphicsPagesMul16     = (int)(graphicsFlags >> 4) & 0x1;
            GraphicsPagesSizeShift = (int)(graphicsFlags >> 0) & 0xF;
            var graphicsBaseSize = BASE_SIZE << GraphicsPagesSizeShift;
            var graphicsSize     =
                graphicsBaseSize * GraphicsPagesDiv16 / 16 +
                graphicsBaseSize * GraphicsPagesDiv8 / 8 +
                graphicsBaseSize * GraphicsPagesDiv4 / 4 +
                graphicsBaseSize * GraphicsPagesDiv2 / 2 +
                graphicsBaseSize * GraphicsPagesMul1 * 1 +
                graphicsBaseSize * GraphicsPagesMul2 * 2 +
                graphicsBaseSize * GraphicsPagesMul4 * 4 +
                graphicsBaseSize * GraphicsPagesMul8 * 8 +
                graphicsBaseSize * GraphicsPagesMul16 * 16;

            SystemData   = new byte[systemSize];
            GraphicsData = new byte[graphicsSize];

            var deflateStream = new DeflateStream(stream, CompressionMode.Decompress, true);

            deflateStream.Read(SystemData, 0, systemSize);
            deflateStream.Read(GraphicsData, 0, graphicsSize);
            deflateStream.Close();
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 获取解压缩后的字符串
        /// </summary>
        public static DataTable ToDataTable(byte[] arrbts)
        {
            DataTable dt = new DataTable();

            dt.TableName = "dt";
            try
            {
                MemoryStream ms = new MemoryStream();
                ms.Write(arrbts, 0, arrbts.Length);
                ms.Position = 0;
                DeflateStream ZipStream     = new DeflateStream(ms, CompressionMode.Decompress);
                MemoryStream  UnzipStream   = new MemoryStream();
                byte[]        sDecompressed = new byte[128];
                while (true)
                {
                    int bytesRead = ZipStream.Read(sDecompressed, 0, 128);
                    if (bytesRead == 0)
                    {
                        break;
                    }
                    UnzipStream.Write(sDecompressed, 0, bytesRead);
                }
                ZipStream.Close();
                ms.Close();
                UnzipStream.Position = 0;// 解压起始位置设置为头
                StreamReader sr    = new StreamReader(UnzipStream);
                string       strDS = sr.ReadToEnd();


                string[] ss        = strDS.Split('й');
                string[] ssColumns = ss[0].Split('ж');
                string[] sc;
                for (int i = 0; i < ssColumns.Length; i++)
                {
                    sc = ssColumns[i].Split('ю');
                    if (sc[0] != string.Empty)
                    {
                        dt.Columns.Add(sc[0], System.Type.GetType(sc[1]));
                    }
                }
                if (ss.Length > 1)
                {
                    for (int i = 1; i < ss.Length; i++)
                    {
                        DataRow dr = dt.NewRow();
                        sc = ss[i].Split('ж');
                        if (sc[0] != string.Empty)
                        {
                            for (int j = 0; j < dt.Columns.Count; j++)
                            {
                                try
                                {
                                    dr[j] = sc[j];
                                }
                                catch (Exception ex)
                                {
                                    dr[j] = DBNull.Value;
                                }
                            }
                            dt.Rows.Add(dr);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return(dt);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// HttpGet请求(同步,短连接)
        /// </summary>
        /// <param name="strUrl">Url</param>
        /// <param name="strContentType">ContentType(默认:text/html;charset=UTF-8)</param>
        /// <param name="strAccept">Accept(默认:application/json,text/javascript,*/*;q=0.01)</param>
        /// <param name="strUserAgent">UserAgent(默认:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36)</param>
        /// <param name="cookieCollection">CookieCollection</param>
        /// <param name="intTimeOut">TimeOut(默认:100秒)</param>
        /// <param name="strEncoding">Encoding(默认:utf-8)</param>
        /// <returns></returns>
        public static string HttpGet(string strUrl, string strContentType, string strAccept, string strUserAgent, CookieCollection cookieCollection, int intTimeOut, string strEncoding)
        {
            //响应字符串
            string strResult = string.Empty;

            if (string.IsNullOrEmpty(strUrl))
            {
                return(strResult);
            }
            if (string.IsNullOrEmpty(strEncoding))
            {
                //默认响应编码为 utf-8
                strEncoding = "utf-8";
            }

            HttpWebRequest request = null;

            try
            {
                request = (HttpWebRequest)HttpWebRequest.Create(strUrl);
            }
            catch (Exception ex)
            {
                Log4NetUtil.Error(typeof(HttpUtil), ex.ToString());
                return(strResult);
            }

            if (request != null)
            {
                //GET请求
                request.Method = "GET";
                //短连接
                request.KeepAlive = false;

                if (string.IsNullOrEmpty(strContentType))
                {
                    request.ContentType = @"text/html;charset=UTF-8";
                }
                else
                {
                    request.ContentType = strContentType;
                }

                if (string.IsNullOrEmpty(strAccept))
                {
                    //request.Accept = @"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                    request.Accept = @"application/json,text/javascript,*/*;q=0.01";
                }
                else
                {
                    request.Accept = strAccept;
                }

                if (string.IsNullOrEmpty(strUserAgent))
                {
                    request.UserAgent = @"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36";
                }
                else
                {
                    request.UserAgent = strUserAgent;
                }

                if (cookieCollection != null)
                {
                    request.CookieContainer = new CookieContainer();
                    request.CookieContainer.Add(cookieCollection);
                }

                //超时时间,单位:毫秒(最小10秒)
                if (intTimeOut < 10)
                {
                    request.Timeout          = 10 * 1000;
                    request.ReadWriteTimeout = 10 * 1000;
                }
                else
                {
                    request.Timeout          = intTimeOut * 1000;
                    request.ReadWriteTimeout = intTimeOut * 1000;
                }

                //AcceptEncoding
                request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");

                //代理方式
                //WebProxy proxy = new WebProxy("127.0.0.1", 8000);//指定的ip和端口
                //request.Proxy = proxy;
                //request.AllowAutoRedirect = true;

                //取得响应数据
                HttpWebResponse response = null;
                try
                {
                    response = (HttpWebResponse)request.GetResponse();
                }
                catch (Exception ex)
                {
                    Log4NetUtil.Error(typeof(HttpUtil), ex.ToString());
                    request.Abort();
                    return(strResult);
                }

                if (response != null)
                {
                    byte[] buffer = new byte[4096];
                    int    size   = 0;
                    try
                    {
                        if (!string.IsNullOrEmpty(response.ContentEncoding))
                        {
                            if (response.ContentEncoding.ToLower().Contains("gzip"))
                            {
                                using (Stream rpsStream = response.GetResponseStream())
                                {
                                    if (rpsStream != null)
                                    {
                                        using (GZipStream stream = new GZipStream(rpsStream, CompressionMode.Decompress))
                                        {
                                            using (MemoryStream memoryStream = new MemoryStream())
                                            {
                                                while ((size = stream.Read(buffer, 0, buffer.Length)) > 0)
                                                {
                                                    memoryStream.Write(buffer, 0, size);
                                                }
                                                strResult = Encoding.GetEncoding(strEncoding).GetString(memoryStream.ToArray());
                                            }
                                        }
                                    }
                                }
                            }
                            else if (response.ContentEncoding.ToLower().Contains("deflate"))
                            {
                                using (Stream rpsStream = response.GetResponseStream())
                                {
                                    if (rpsStream != null)
                                    {
                                        using (DeflateStream stream = new DeflateStream(rpsStream, CompressionMode.Decompress))
                                        {
                                            using (MemoryStream memoryStream = new MemoryStream())
                                            {
                                                while ((size = stream.Read(buffer, 0, buffer.Length)) > 0)
                                                {
                                                    memoryStream.Write(buffer, 0, size);
                                                }
                                                strResult = Encoding.GetEncoding(strEncoding).GetString(memoryStream.ToArray());
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                using (Stream rpsStream = response.GetResponseStream())
                                {
                                    if (rpsStream != null)
                                    {
                                        using (MemoryStream memoryStream = new MemoryStream())
                                        {
                                            while ((size = rpsStream.Read(buffer, 0, buffer.Length)) > 0)
                                            {
                                                memoryStream.Write(buffer, 0, size);
                                            }
                                            strResult = Encoding.GetEncoding(strEncoding).GetString(memoryStream.ToArray());
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            using (Stream rpsStream = response.GetResponseStream())
                            {
                                if (rpsStream != null)
                                {
                                    using (MemoryStream memoryStream = new MemoryStream())
                                    {
                                        while ((size = rpsStream.Read(buffer, 0, buffer.Length)) > 0)
                                        {
                                            memoryStream.Write(buffer, 0, size);
                                        }
                                        strResult = Encoding.GetEncoding(strEncoding).GetString(memoryStream.ToArray());
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Log4NetUtil.Error(typeof(HttpUtil), ex.ToString());
                        request.Abort();
                        return(string.Empty);
                    }
                    finally
                    {
                        if (response != null)
                        {
                            response.Close();
                        }
                        if (request != null)
                        {
                            request.Abort();
                        }
                    }
                }
            }
            return(strResult);
        }
Ejemplo n.º 10
0
        protected byte[] LoadCompressedChunk(BinaryReader reader)
        {
            try
            {
                FCompressedChunkInfo PackageFileTag = new FCompressedChunkInfo();
                PackageFileTag.CompressedSize   = reader.ReadInt64();
                PackageFileTag.UncompressedSize = reader.ReadInt64();
                FCompressedChunkInfo Summary = new FCompressedChunkInfo();
                Summary.CompressedSize   = reader.ReadInt64();
                Summary.UncompressedSize = reader.ReadInt64();

                bool bWasByteSwapped = PackageFileTag.CompressedSize != PACKAGE_FILE_TAG;
                bool bHeaderWasValid = true;

                if (bWasByteSwapped)
                {
                    bHeaderWasValid = PackageFileTag.CompressedSize == PACKAGE_FILE_TAG_SWAPPED;
                    if (bHeaderWasValid)
                    {
                        // not supported
                        //Summary.CompressedSize = BYTESWAP_ORDER64(Summary.CompressedSize);
                        //Summary.UncompressedSize = BYTESWAP_ORDER64(Summary.UncompressedSize);
                        //PackageFileTag.UncompressedSize = BYTESWAP_ORDER64(PackageFileTag.UncompressedSize);
                    }
                }
                else
                {
                    bHeaderWasValid = PackageFileTag.CompressedSize == PACKAGE_FILE_TAG;
                }

                // Handle change in compression chunk size in backward compatible way.
                long LoadingCompressionChunkSize = PackageFileTag.UncompressedSize;
                if (LoadingCompressionChunkSize == PACKAGE_FILE_TAG)
                {
                    LoadingCompressionChunkSize = LOADING_COMPRESSION_CHUNK_SIZE;
                }

                // Figure out how many chunks there are going to be based on uncompressed size and compression chunk size.
                long TotalChunkCount = (Summary.UncompressedSize + LoadingCompressionChunkSize - 1) / LoadingCompressionChunkSize;

                // Allocate compression chunk infos and serialize them, keeping track of max size of compression chunks used.
                FCompressedChunkInfo[] CompressionChunks = new FCompressedChunkInfo[TotalChunkCount];
                long MaxCompressedSize = 0;
                for (int ChunkIndex = 0; ChunkIndex < TotalChunkCount; ChunkIndex++)
                {
                    CompressionChunks[ChunkIndex].CompressedSize   = reader.ReadInt64();
                    CompressionChunks[ChunkIndex].UncompressedSize = reader.ReadInt64();
                    if (bWasByteSwapped)
                    {
                        // not supported
                        //CompressionChunks[ChunkIndex].CompressedSize = BYTESWAP_ORDER64(CompressionChunks[ChunkIndex].CompressedSize);
                        //CompressionChunks[ChunkIndex].UncompressedSize = BYTESWAP_ORDER64(CompressionChunks[ChunkIndex].UncompressedSize);
                    }
                    MaxCompressedSize = Math.Max(CompressionChunks[ChunkIndex].CompressedSize, MaxCompressedSize);
                }

                byte[] DeCompressedBuffer = new byte[Summary.UncompressedSize];

                int offset = 0;
                // Iterate over all chunks, serialize them into memory and decompress them directly into the destination pointer
                for (long ChunkIndex = 0; ChunkIndex < TotalChunkCount; ChunkIndex++)
                {
                    // Read compressed data.
                    byte[]        initialSkip      = reader.ReadBytes(2);
                    byte[]        CompressedBuffer = reader.ReadBytes((int)CompressionChunks[ChunkIndex].CompressedSize - 6);
                    byte[]        finalSkip        = reader.ReadBytes(4);
                    MemoryStream  ms   = new MemoryStream(CompressedBuffer);
                    DeflateStream gzip = new DeflateStream(ms, CompressionMode.Decompress);
                    gzip.Read(DeCompressedBuffer, offset, (int)CompressionChunks[ChunkIndex].UncompressedSize);
                    // Decompress into dest pointer directly.
                    offset += (int)CompressionChunks[ChunkIndex].UncompressedSize;
                }
                return(DeCompressedBuffer);
            }
            catch
            {
                return(null);
            }
        }
Ejemplo n.º 11
0
 public async void Decompress(CompressionType type)
 {
     string testFilePath = CreateCompressedFile(type);
     int _bufferSize = 1024;
     int retCount = -1;
     var bytes = new byte[_bufferSize];
     using (MemoryStream gzStream = await LocalMemoryStream.readAppFileAsync(testFilePath))
     using (MemoryStream strippedMs = StripHeaderAndFooter.Strip(gzStream))
         foreach (var iteration in Benchmark.Iterations)
             using (iteration.StartMeasurement())
                 for (int i = 0; i < 20000; i++)
                 {
                     using (DeflateStream zip = new DeflateStream(strippedMs, CompressionMode.Decompress, leaveOpen: true))
                     {
                         while (retCount != 0)
                         {
                             retCount = zip.Read(bytes, 0, _bufferSize);
                         }
                     }
                     strippedMs.Seek(0, SeekOrigin.Begin);
                 }
     File.Delete(testFilePath);
 }
Ejemplo n.º 12
0
 public override int Read(byte[] buffer, int offset, int count)
 {
     return(_deflate.Read(buffer, offset, count));
 }
Ejemplo n.º 13
0
            private static Stream Decompress(Stream stream)
            {
                if (stream == null)
                {
                    throw new ArgumentNullException(nameof(stream));
                }

                try
                {
                    stream.Seek(0, SeekOrigin.Begin);

                    int type = (int)Varint.GetUInt64(stream);

                    if (type == (int)ConvertCompressionAlgorithm.None)
                    {
                        return(new RangeStream(stream));
                    }
                    else if (type == (int)ConvertCompressionAlgorithm.Deflate)
                    {
                        RecyclableMemoryStream deflateBufferStream = null;

                        try
                        {
                            deflateBufferStream = new RecyclableMemoryStream(_bufferPool);

                            using (var deflateStream = new DeflateStream(stream, CompressionMode.Decompress))
                                using (var safeBuffer = _bufferPool.CreateSafeBuffer(1024 * 4))
                                {
                                    int length;

                                    while ((length = deflateStream.Read(safeBuffer.Value, 0, safeBuffer.Value.Length)) > 0)
                                    {
                                        deflateBufferStream.Write(safeBuffer.Value, 0, length);

                                        if (deflateBufferStream.Length > 1024 * 1024 * 256)
                                        {
                                            throw new Exception("too large");
                                        }
                                    }
                                }

                            deflateBufferStream.Seek(0, SeekOrigin.Begin);

                            return(deflateBufferStream);
                        }
                        catch (Exception)
                        {
                            if (deflateBufferStream != null)
                            {
                                deflateBufferStream.Dispose();
                            }

                            throw;
                        }
                    }
                    else
                    {
                        throw new ArgumentException("ArgumentException");
                    }
                }
                catch (Exception e)
                {
                    if (stream != null)
                    {
                        stream.Dispose();
                    }

                    throw new ArgumentException(e.Message, e);
                }
            }
Ejemplo n.º 14
0
        public Png(Stream s)
        {
            byte[] internalBuffer = new byte[8];

            // Check header signature
            s.Read(internalBuffer, 0, Signature.Length);

            for (int i = 0; i < Signature.Length; i++)
            {
                if (internalBuffer[i] != Signature[i])
                {
                    throw new InvalidDataException("Invalid PNG file - header signature mismatch");
                }
            }

            // Load image
            bool           headerParsed = false;
            bool           isPaletted   = false;
            bool           is24Bit      = false;
            RawList <byte> data         = new RawList <byte>();

            EmbeddedData = new Dictionary <string, string>();

            while (true)
            {
                int length = ReadInt32BigEndian(s, ref internalBuffer);
                s.Read(internalBuffer, 0, 4);
                string type = Encoding.ASCII.GetString(internalBuffer, 0, 4);

                if (!headerParsed && type != "IHDR")
                {
                    throw new InvalidDataException("Invalid PNG file - header does not appear first");
                }

                int blockEndPosition = (int)s.Position + length;

                switch (type)
                {
                case "IHDR": {
                    if (headerParsed)
                    {
                        throw new InvalidDataException("Invalid PNG file - duplicate header");
                    }

                    Width  = ReadInt32BigEndian(s, ref internalBuffer);
                    Height = ReadInt32BigEndian(s, ref internalBuffer);

                    byte         bitDepth  = s.ReadUInt8(ref internalBuffer);
                    PngColorType colorType = (PngColorType)s.ReadUInt8(ref internalBuffer);
                    isPaletted = IsPaletted(bitDepth, colorType);
                    is24Bit    = (colorType == PngColorType.Color);

                    var dataLength = Width * Height;
                    if (!isPaletted)
                    {
                        dataLength *= 4;
                    }

                    Data = new byte[dataLength];

                    byte compression = s.ReadUInt8(ref internalBuffer);
                    /*byte filter = */ s.ReadUInt8(ref internalBuffer);
                    byte interlace = s.ReadUInt8(ref internalBuffer);

                    if (compression != 0)
                    {
                        throw new InvalidDataException("Compression method (" + compression + ") not supported");
                    }
                    if (interlace != 0)
                    {
                        throw new InvalidDataException("Interlacing (" + interlace + ") not supported");
                    }

                    headerParsed = true;
                    break;
                }

                case "PLTE": {
                    Palette = new ColorRgba[256];
                    for (int i = 0; i < length / 3; i++)
                    {
                        byte r = s.ReadUInt8(ref internalBuffer);
                        byte g = s.ReadUInt8(ref internalBuffer);
                        byte b = s.ReadUInt8(ref internalBuffer);
                        Palette[i] = new ColorRgba(r, g, b);
                    }
                    break;
                }

                case "tRNS": {
                    if (Palette == null)
                    {
                        throw new InvalidDataException("Non-palette indexed images are not supported");
                    }
                    for (var i = 0; i < length; i++)
                    {
                        Palette[i].A = s.ReadUInt8(ref internalBuffer);
                    }
                    break;
                }

                case "IDAT": {
                    int newLength = data.Count + length;
                    data.Count = newLength;
                    s.Read(data.Data, newLength - length, length);
                    break;
                }

                case "tEXt": {
                    byte[] content = new byte[length];
                    s.Read(content, 0, length);

                    for (int i = 0; i < length; i++)
                    {
                        if (content[i] == 0)
                        {
                            string key   = Encoding.ASCII.GetString(content, 0, i);
                            string value = Encoding.ASCII.GetString(content, i + 1, length - (i + 1));
                            EmbeddedData.Add(key, value);
                            break;
                        }
                    }
                    break;
                }

                case "IEND": {
                    using (var ms = new MemoryStream(data.Data)) {
                        ms.Position += 2;

                        using (var ds = new DeflateStream(ms, CompressionMode.Decompress, true)) {
                            int pxStride  = (isPaletted ? 1 : (is24Bit ? 3 : 4));
                            int srcStride = Width * pxStride;
                            int dstStride = Width * (isPaletted ? 1 : 4);

                            byte[] buffer     = new byte[srcStride];
                            byte[] bufferPrev = new byte[srcStride];

                            for (var y = 0; y < Height; y++)
                            {
                                // Read filter
                                PngFilter filter = (PngFilter)ds.ReadUInt8(ref internalBuffer);

                                // Read data
                                ds.Read(buffer, 0, srcStride);

                                for (var i = 0; i < srcStride; i++)
                                {
                                    if (i < pxStride)
                                    {
                                        buffer[i] = UnapplyFilter(filter, buffer[i], 0, bufferPrev[i], 0);
                                    }
                                    else
                                    {
                                        buffer[i] = UnapplyFilter(filter, buffer[i], buffer[i - pxStride], bufferPrev[i], bufferPrev[i - pxStride]);
                                    }
                                }

                                if (is24Bit)
                                {
                                    for (var i = 0; i < buffer.Length / 3; i++)
                                    {
                                        Buffer.BlockCopy(buffer, 3 * i, Data, y * dstStride + 4 * i, 3);
                                        Data[y * dstStride + 4 * i + 3] = 255;
                                    }
                                }
                                else
                                {
                                    Buffer.BlockCopy(buffer, 0, Data, y * dstStride, srcStride);
                                }

                                bufferPrev = buffer;
                            }
                        }
                    }
                    return;
                }

                default: {
                    //Console.WriteLine("Unknown PNG section: " + type);
                    s.Position += length;
                    break;
                }
                }

                if (s.Position != blockEndPosition)
                {
                    throw new InvalidDataException("Block " + type + " has incorrect length");
                }

                // Skip CRC
                s.Position += 4;
            }
        }
Ejemplo n.º 15
0
 /// <inheritdoc />
 public int Read(byte[] array, int offset, int count)
 {
     return(DeflateStreamInstance.Read(array, offset, count));
 }
Ejemplo n.º 16
0
        private static void DecompressSecondGen(string file)
        {
            using (MemoryStream msOutput = new MemoryStream())
            {
                using (FileStream fsInput = new FileStream(file, FileMode.Open))
                {
                    using (BinaryReader brInput = new BinaryReader(fsInput))
                    {
                        //header is uncompressed
                        msOutput.Write(brInput.ReadBytes(0x1000), 0, 0x1000);

                        List <Tuple <int, int> > Chunks = new List <Tuple <int, int> >();

                        for (int i = 0; i < 0x400; i++)
                        {
                            int csize  = brInput.ReadInt32();
                            int offset = brInput.ReadInt32();

                            if (csize == 0)
                            {
                                break;
                            }

                            if (offset >= fsInput.Length)
                            {
                                throw new ArgumentException("Chunk " + i + " has an offset past the end of the file.");
                            }

                            Chunks.Add(new Tuple <int, int>(csize, offset));
                        }

                        //Decompress and write each chunk
                        for (int i = 0; i < Chunks.Count; i++)
                        {
                            //check for faux-compression some other tools use
                            if (Chunks[i].Item1 < 0)
                            {
                                int    invertedSize = -Chunks[i].Item1;
                                byte[] aaa          = new byte[invertedSize];

                                fsInput.Seek(Chunks[i].Item2, SeekOrigin.Begin);

                                int readSize = fsInput.Read(aaa, 0, invertedSize);

                                msOutput.Write(aaa, 0, readSize);
                            }
                            else
                            {
                                fsInput.Seek(Chunks[i].Item2 + 2, SeekOrigin.Begin);

                                int    realsize  = 0x40000;
                                byte[] chunkData = new byte[realsize];

                                using (DeflateStream ds = new DeflateStream(fsInput, CompressionMode.Decompress, true))
                                {
                                    realsize = ds.Read(chunkData, 0, chunkData.Length);
                                }

                                msOutput.Write(chunkData, 0, realsize);
                            }
                        }
                    }
                }
                File.WriteAllBytes(file, msOutput.ToArray());
            }
        }
        /// <exception cref="PngProcessingException"/>
        /// <exception cref="System.IO.IOException"/>
        private static IEnumerable<Directory> ProcessChunk([NotNull] PngChunk chunk)
        {
            // For more guidance:
            // http://www.w3.org/TR/PNG-Decoders.html#D.Text-chunk-processing
            // http://www.libpng.org/pub/png/spec/1.2/PNG-Chunks.html#C.iCCP
            // by spec, PNG is generally supposed to use this encoding
            const string defaultEncodingName = "ISO-8859-1";
            var defaultEncoding = Encoding.GetEncoding(defaultEncodingName);

            var chunkType = chunk.ChunkType;
            var bytes = chunk.Bytes;

            if (chunkType == PngChunkType.IHDR)
            {
                var header = new PngHeader(bytes);
                var directory = new PngDirectory(PngChunkType.IHDR);
                directory.Set(PngDirectory.TagImageWidth, header.ImageWidth);
                directory.Set(PngDirectory.TagImageHeight, header.ImageHeight);
                directory.Set(PngDirectory.TagBitsPerSample, header.BitsPerSample);
                directory.Set(PngDirectory.TagColorType, header.ColorType.NumericValue);
                directory.Set(PngDirectory.TagCompressionType, header.CompressionType);
                directory.Set(PngDirectory.TagFilterMethod, header.FilterMethod);
                directory.Set(PngDirectory.TagInterlaceMethod, header.InterlaceMethod);
                yield return directory;
            }
            else if (chunkType == PngChunkType.PLTE)
            {
                var directory = new PngDirectory(PngChunkType.PLTE);
                directory.Set(PngDirectory.TagPaletteSize, bytes.Length / 3);
                yield return directory;
            }
            else if (chunkType == PngChunkType.tRNS)
            {
                var directory = new PngDirectory(PngChunkType.tRNS);
                directory.Set(PngDirectory.TagPaletteHasTransparency, 1);
                yield return directory;
            }
            else if (chunkType == PngChunkType.sRGB)
            {
                int srgbRenderingIntent = unchecked((sbyte)bytes[0]);
                var directory = new PngDirectory(PngChunkType.sRGB);
                directory.Set(PngDirectory.TagSrgbRenderingIntent, srgbRenderingIntent);
                yield return directory;
            }
            else if (chunkType == PngChunkType.cHRM)
            {
                var chromaticities = new PngChromaticities(bytes);
                var directory = new PngChromaticitiesDirectory();
                directory.Set(PngChromaticitiesDirectory.TagWhitePointX, chromaticities.WhitePointX);
                directory.Set(PngChromaticitiesDirectory.TagWhitePointY, chromaticities.WhitePointY);
                directory.Set(PngChromaticitiesDirectory.TagRedX, chromaticities.RedX);
                directory.Set(PngChromaticitiesDirectory.TagRedY, chromaticities.RedY);
                directory.Set(PngChromaticitiesDirectory.TagGreenX, chromaticities.GreenX);
                directory.Set(PngChromaticitiesDirectory.TagGreenY, chromaticities.GreenY);
                directory.Set(PngChromaticitiesDirectory.TagBlueX, chromaticities.BlueX);
                directory.Set(PngChromaticitiesDirectory.TagBlueY, chromaticities.BlueY);
                yield return directory;
            }
            else if (chunkType == PngChunkType.gAMA)
            {
                var gammaInt = ByteConvert.ToInt32BigEndian(bytes);
                var directory = new PngDirectory(PngChunkType.gAMA);
                directory.Set(PngDirectory.TagGamma, gammaInt / 100000.0);
                yield return directory;
            }
            else if (chunkType == PngChunkType.iCCP)
            {
                var reader = new SequentialByteArrayReader(bytes);
                var profileName = reader.GetNullTerminatedStringValue(maxLengthBytes: 79);
                var directory = new PngDirectory(PngChunkType.iCCP);
                directory.Set(PngDirectory.TagIccProfileName, profileName);
                var compressionMethod = reader.GetSByte();
                if (compressionMethod == 0)
                {
                    // Only compression method allowed by the spec is zero: deflate
                    // This assumes 1-byte-per-char, which it is by spec.
                    var bytesLeft = bytes.Length - profileName.Bytes.Length - 2;
                    var compressedProfile = reader.GetBytes(bytesLeft);
                    using (var inflaterStream = new InflaterInputStream(new MemoryStream(compressedProfile)))
                    {
                        var iccDirectory = new IccReader().Extract(new IndexedCapturingReader(inflaterStream));
                        iccDirectory.Parent = directory;
                        yield return iccDirectory;
                    }
                }
                else
                {
                    directory.AddError("Invalid compression method value");
                }
                yield return directory;
            }
            else if (chunkType == PngChunkType.bKGD)
            {
                var directory = new PngDirectory(PngChunkType.bKGD);
                directory.Set(PngDirectory.TagBackgroundColor, bytes);
                yield return directory;
            }
            else if (chunkType == PngChunkType.tEXt)
            {
                var reader = new SequentialByteArrayReader(bytes);
                var keyword = reader.GetNullTerminatedStringValue(maxLengthBytes: 79).ToString(defaultEncoding);
                var bytesLeft = bytes.Length - keyword.Length - 1;
                var value = reader.GetNullTerminatedStringValue(bytesLeft, defaultEncoding);

                var textPairs = new List<KeyValuePair> { new KeyValuePair(keyword, value) };
                var directory = new PngDirectory(PngChunkType.iTXt);
                directory.Set(PngDirectory.TagTextualData, textPairs);
                yield return directory;
            }
            else if (chunkType == PngChunkType.iTXt)
            {
                var reader = new SequentialByteArrayReader(bytes);
                var keyword = reader.GetNullTerminatedStringValue(maxLengthBytes: 79).ToString(defaultEncoding);
                var compressionFlag = reader.GetSByte();
                var compressionMethod = reader.GetSByte();
                var languageTag = reader.GetNullTerminatedStringValue(bytes.Length, defaultEncoding);

                var translatedKeyword = reader.GetNullTerminatedStringValue(bytes.Length, defaultEncoding);

                var bytesLeft = bytes.Length - keyword.Length - 1 - 1 - 1 - languageTag.Bytes.Length - 1 - translatedKeyword.Bytes.Length - 1;
                byte[] textBytes = null;
                if (compressionFlag == 0)
                {
                    textBytes = reader.GetNullTerminatedBytes(bytesLeft);
                }
                else if (compressionFlag == 1)
                {
                    if (compressionMethod == 0)
                    {
                        using (var inflaterStream = new DeflateStream(new MemoryStream(bytes, bytes.Length - bytesLeft, bytesLeft), CompressionMode.Decompress))
                        using (var decompStream = new MemoryStream())
                        {
#if !NET35
                            inflaterStream.CopyTo(decompStream);
#else
                            byte[] buffer = new byte[256];
                            int count;
                            int totalBytes = 0;
                            while ((count = inflaterStream.Read(buffer, 0, 256)) > 0)
                            {
                                decompStream.Write(buffer, 0, count);
                                totalBytes += count;
                            }
#endif
                            textBytes = decompStream.ToArray();
                        }
                    }
                    else
                    {
                        var directory = new PngDirectory(PngChunkType.iTXt);
                        directory.AddError("Invalid compression method value");
                        yield return directory;
                    }
                }
                else
                {
                    var directory = new PngDirectory(PngChunkType.iTXt);
                    directory.AddError("Invalid compression flag value");
                    yield return directory;
                }

                if (textBytes != null)
                {
                    if (keyword == "XML:com.adobe.xmp")
                    {
                        // NOTE in testing images, the XMP has parsed successfully, but we are not extracting tags from it as necessary
                        yield return new XmpReader().Extract(textBytes);
                    }
                    else
                    {
                        var textPairs = new List<KeyValuePair> { new KeyValuePair(keyword, new StringValue(textBytes, defaultEncoding)) };
                        var directory = new PngDirectory(PngChunkType.iTXt);
                        directory.Set(PngDirectory.TagTextualData, textPairs);
                        yield return directory;
                    }
                }
            }
            else if (chunkType == PngChunkType.tIME)
            {
                var reader = new SequentialByteArrayReader(bytes);
                var year = reader.GetUInt16();
                var month = reader.GetByte();
                int day = reader.GetByte();
                int hour = reader.GetByte();
                int minute = reader.GetByte();
                int second = reader.GetByte();
                var directory = new PngDirectory(PngChunkType.tIME);
                try
                {
                    var time = new DateTime(year, month, day, hour, minute, second, DateTimeKind.Unspecified);
                    directory.Set(PngDirectory.TagLastModificationTime, time);
                }
                catch (ArgumentOutOfRangeException e)
                {
                    directory.AddError("Error constructing DateTime: " + e.Message);
                }
                yield return directory;
            }
            else if (chunkType == PngChunkType.pHYs)
            {
                var reader = new SequentialByteArrayReader(bytes);
                var pixelsPerUnitX = reader.GetInt32();
                var pixelsPerUnitY = reader.GetInt32();
                var unitSpecifier = reader.GetSByte();
                var directory = new PngDirectory(PngChunkType.pHYs);
                directory.Set(PngDirectory.TagPixelsPerUnitX, pixelsPerUnitX);
                directory.Set(PngDirectory.TagPixelsPerUnitY, pixelsPerUnitY);
                directory.Set(PngDirectory.TagUnitSpecifier, unitSpecifier);
                yield return directory;
            }
            else if (chunkType.Equals(PngChunkType.sBIT))
            {
                var directory = new PngDirectory(PngChunkType.sBIT);
                directory.Set(PngDirectory.TagSignificantBits, bytes);
                yield return directory;
            }
        }
Ejemplo n.º 18
0
        public void SequentialReadsOnMemoryStream_Return_SameBytes()
        {
            byte[] data = new byte[1024 * 10];
            new Random(42).NextBytes(data);

            var compressed = new MemoryStream();
            using (var compressor = new DeflateStream(compressed, CompressionMode.Compress, true))
            {
                for (int i = 0; i < data.Length; i += 1024)
                {
                    compressor.Write(data, i, 1024);
                }
            }
            compressed.Position = 0;

            using (var decompressor = new DeflateStream(compressed, CompressionMode.Decompress, true))
            {
                int i, j;
                byte[] array = new byte[100];
                byte[] array2 = new byte[100];

                // only read in the first 100 bytes
                decompressor.Read(array, 0, array.Length);
                for (i = 0; i < array.Length; i++)
                    Assert.Equal(data[i], array[i]);

                // read in the next 100 bytes and make sure nothing is missing
                decompressor.Read(array2, 0, array2.Length);
                for (j = 0; j < array2.Length; j++)
                    Assert.Equal(data[j], array[j]);
            }
        }
        static public XmlDocument ParseRequest(string aXmlVar, ref bool aZip, ref bool aCrypt, byte[] aEncryptionKey)
        {
            XmlDocument aRet = null;

            aEncryptionKey = (aEncryptionKey != null && aEncryptionKey.Length == 16) ? aEncryptionKey : System.Text.Encoding.ASCII.GetBytes("1234567890123456");
            try
            {
                if (string.IsNullOrEmpty(aXmlVar))
                {
                    return(aRet);
                }

                System.Xml.XmlDocument aXmlRequest = new System.Xml.XmlDocument();
                aXmlRequest.LoadXml(aXmlVar);
                byte[] aBytes     = null;
                int    aSizeByAtt = int.Parse(aXmlRequest.DocumentElement.Attributes["size"].Value);
                aZip   = (aXmlRequest.DocumentElement.HasAttribute("crypt") && aXmlRequest.DocumentElement.Attributes["crypt"].Value != "0");
                aCrypt = (aXmlRequest.DocumentElement.HasAttribute("zip") && aXmlRequest.DocumentElement.Attributes["zip"].Value != "0");

                if (aZip || aCrypt)
                {
                    string aHex = aXmlRequest.DocumentElement.InnerText;
                    aBytes = new byte[aHex.Length / 2];
                    for (int i = 0; i < aHex.Length; i += 2)
                    {
                        string a2Hex = aHex.Substring(i, 2);
                        aBytes[i / 2] = byte.Parse(a2Hex, System.Globalization.NumberStyles.AllowHexSpecifier);
                    }

                    if (aCrypt)
                    {
                        if (aEncryptionKey == null)
                        {
                            aEncryptionKey = System.Text.Encoding.ASCII.GetBytes("1234567890123456");
                        }
                        Rijndael iCryptoService = new RijndaelManaged();
                        iCryptoService.KeySize   = 128;
                        iCryptoService.BlockSize = 256;
                        iCryptoService.Mode      = CipherMode.ECB;//.CBC;//.ECB;
                        iCryptoService.Padding   = PaddingMode.None;

                        MemoryStream aMemoryStream = new MemoryStream();
                        iCryptoService.Padding = PaddingMode.None;
                        iCryptoService.GenerateIV();
                        byte[]       aIv           = aIv = iCryptoService.IV;
                        CryptoStream aCryptoStream = new CryptoStream(aMemoryStream, iCryptoService.CreateDecryptor(aEncryptionKey, aIv), CryptoStreamMode.Write);
                        aCryptoStream.Write(aBytes, 0, (int)aBytes.Length);
                        aCryptoStream.FlushFinalBlock();
                        aBytes = aMemoryStream.ToArray();
                    }

                    if (aZip)
                    {
                        // 2 first bytes are length (high first)
                        int          aZipLen;
                        int          aOff    = 0;
                        MemoryStream aOutput = new MemoryStream();
                        while (aOutput.Position < aSizeByAtt && aOff < aBytes.Length)
                        {
                            aZipLen = BitConverter.ToInt16(aBytes, aOff);
                            if (aZipLen == 0)
                            {
                                break;
                            }
                            aOff += 2;
                            MemoryStream aInput = new MemoryStream();
                            //write the incoming bytes to the MemoryStream
                            aInput.Write(aBytes, aOff, aZipLen);
                            //set our position to the start of the Stream
                            aInput.Position = 0;
                            System.IO.Compression.DeflateStream aDeflateStream = new DeflateStream(aInput, CompressionMode.Decompress, true);
                            int    aBufLen = 32000;
                            byte[] aBuf    = new byte[aBufLen];
                            Int16  aUtfLen = 0;
                            while (true)
                            {
                                int aNumRead = aDeflateStream.Read(aBuf, 0, aBufLen);
                                if (aNumRead <= 0)
                                {
                                    break;
                                }
                                if (aUtfLen == 0)
                                {
                                    aUtfLen = BitConverter.ToInt16(aBuf, 0);
                                    if (aNumRead > 2)
                                    {
                                        aOutput.Write(aBuf, 2, aNumRead - 2);
                                    }
                                }
                                else
                                {
                                    aOutput.Write(aBuf, 0, aNumRead);
                                }
                            }
                            aDeflateStream.Close();
                            aOff += aZipLen;
                        }
                        aBytes = aOutput.ToArray();
                        aOutput.Close();
                    }
                    string aUtfXml = System.Text.Encoding.UTF8.GetString(aBytes, 0, aSizeByAtt);
                    aRet = new XmlDocument();
                    aRet.LoadXml(aUtfXml);
                }
                else
                {
                    for (int aC = 0; aXmlRequest.DocumentElement.HasChildNodes && aC < aXmlRequest.DocumentElement.ChildNodes.Count; aC++)
                    {
                        if (aXmlRequest.DocumentElement.ChildNodes[aC].NodeType == XmlNodeType.Element)
                        {
                            aRet = new XmlDocument();
                            aRet.LoadXml(aXmlRequest.DocumentElement.ChildNodes[aC].OuterXml);
                            break;
                        }
                    }
                }
            }
            catch
            {
                aRet = null;
            }
            return(aRet);
        }
Ejemplo n.º 20
0
 public override int Read(byte[] buffer, int offset, int count)
 {
     return(m_stream.Read(buffer, offset, count));
 }
Ejemplo n.º 21
0
        private static void TestCtor(CompressionLevel level, bool? leaveOpen = null)
        {
            //Create the DeflateStream
            int _bufferSize = 1024;
            var bytes = new byte[_bufferSize];
            var baseStream = new MemoryStream(bytes, true);
            DeflateStream ds;

            if (leaveOpen == null)
            {
                ds = new DeflateStream(baseStream, level);
            }
            else
            {
                ds = new DeflateStream(baseStream, level, leaveOpen ?? false);
            }

            //Write some data and Close the stream
            string strData = "Test Data";
            var encoding = Encoding.UTF8;
            byte[] data = encoding.GetBytes(strData);
            ds.Write(data, 0, data.Length);
            ds.Flush();
            ds.Dispose();

            if (leaveOpen != true)
            {
                //Check that Close has really closed the underlying stream
                Assert.Throws<ObjectDisposedException>(() => { baseStream.Write(bytes, 0, bytes.Length); });
            }

            //Read the data
            byte[] data2 = new byte[_bufferSize];
            baseStream = new MemoryStream(bytes, false);
            ds = new DeflateStream(baseStream, CompressionMode.Decompress);
            int size = ds.Read(data2, 0, _bufferSize - 5);

            //Verify the data roundtripped
            for (int i = 0; i < size + 5; i++)
            {
                if (i < data.Length)
                {
                    Assert.Equal(data[i], data2[i]);
                }
                else
                {
                    Assert.Equal(data2[i], (byte)0);
                }
            }
        }
Ejemplo n.º 22
0
        // parses a LOD or physics mesh component
        private bool submesh(byte[] data, int offset, int size, out int ntriangles, out int nsides)
        {
            ntriangles = 0;
            nsides     = 0;

            OSD decodedMeshOsd = new OSD();

            try
            {
                using (MemoryStream outMs = new MemoryStream())
                {
                    using (MemoryStream inMs = new MemoryStream(data, offset, size))
                    {
                        using (DeflateStream decompressionStream = new DeflateStream(inMs, CompressionMode.Decompress))
                        {
                            byte[] readBuffer = new byte[2048];
                            inMs.Read(readBuffer, 0, 2); // skip first 2 bytes in header
                            int readLen = 0;

                            while ((readLen = decompressionStream.Read(readBuffer, 0, readBuffer.Length)) > 0)
                            {
                                outMs.Write(readBuffer, 0, readLen);
                            }
                        }
                    }
                    outMs.Seek(0, SeekOrigin.Begin);
                    decodedMeshOsd = OSDParser.DeserializeLLSDBinary(outMs);
                }
            }
            catch
            {
                return(false);
            }

            OSDArray decodedMeshOsdArray = null;

            byte[] dummy;

            decodedMeshOsdArray = (OSDArray)decodedMeshOsd;
            foreach (OSD subMeshOsd in decodedMeshOsdArray)
            {
                if (subMeshOsd is OSDMap)
                {
                    OSDMap subtmpmap = (OSDMap)subMeshOsd;
                    if (subtmpmap.ContainsKey("NoGeometry") && ((OSDBoolean)subtmpmap["NoGeometry"]))
                    {
                        continue;
                    }

                    if (!subtmpmap.ContainsKey("Position"))
                    {
                        return(false);
                    }

                    if (subtmpmap.ContainsKey("TriangleList"))
                    {
                        dummy       = subtmpmap["TriangleList"].AsBinary();
                        ntriangles += dummy.Length / bytesPerCoord;
                    }
                    else
                    {
                        return(false);
                    }
                    nsides++;
                }
            }

            return(true);
        }
Ejemplo n.º 23
0
 private static byte[] Decompress(byte[] data)
 {
     MemoryStream stream = new MemoryStream(data, 4, data.Length - 4, false);
     DeflateStream stream2 = new DeflateStream(stream, CompressionMode.Decompress);
     byte[] buffer = new byte[(BitConverter.ToInt32(data, 0) - 1) + 1];
     stream2.Read(buffer, 0, buffer.Length);
     stream2.Close();
     return buffer;
 }
Ejemplo n.º 24
0
        // parses convex hulls component
        private bool hulls(byte[] data, int offset, int size, out int nvertices, out int nhulls)
        {
            nvertices = 0;
            nhulls    = 1;

            OSD decodedMeshOsd = new OSD();

            try
            {
                using (MemoryStream outMs = new MemoryStream(4 * size))
                {
                    using (MemoryStream inMs = new MemoryStream(data, offset, size))
                    {
                        using (DeflateStream decompressionStream = new DeflateStream(inMs, CompressionMode.Decompress))
                        {
                            byte[] readBuffer = new byte[8192];
                            inMs.Read(readBuffer, 0, 2); // skip first 2 bytes in header
                            int readLen = 0;

                            while ((readLen = decompressionStream.Read(readBuffer, 0, readBuffer.Length)) > 0)
                            {
                                outMs.Write(readBuffer, 0, readLen);
                            }
                        }
                    }
                    outMs.Seek(0, SeekOrigin.Begin);
                    decodedMeshOsd = OSDParser.DeserializeLLSDBinary(outMs);
                }
            }
            catch
            {
                return(false);
            }

            OSDMap cmap = (OSDMap)decodedMeshOsd;

            if (cmap == null)
            {
                return(false);
            }

            byte[] dummy;

            // must have one of this
            if (cmap.ContainsKey("BoundingVerts"))
            {
                dummy     = cmap["BoundingVerts"].AsBinary();
                nvertices = dummy.Length / bytesPerCoord;
            }
            else
            {
                return(false);
            }

            /* upload is done with convex shape type
             *          if (cmap.ContainsKey("HullList"))
             *          {
             *              dummy = cmap["HullList"].AsBinary();
             *              nhulls += dummy.Length;
             *          }
             *
             *
             *          if (cmap.ContainsKey("Positions"))
             *          {
             *              dummy = cmap["Positions"].AsBinary();
             *              nvertices = dummy.Length / bytesPerCoord;
             *          }
             */

            return(true);
        }
Ejemplo n.º 25
0
        private TiledLayer[] ParseLayers(XmlNodeList nodesLayer, XmlNodeList nodesObjectGroup, XmlNodeList nodesImageLayer)
        {
            var result = new List <TiledLayer>();

            foreach (XmlNode node in nodesLayer)
            {
                var nodeData      = node.SelectSingleNode("data");
                var nodesProperty = node.SelectNodes("properties/property");
                var encoding      = nodeData.Attributes["encoding"].Value;
                var attrVisible   = node.Attributes["visible"];
                var attrLocked    = node.Attributes["locked"];
                var attrTint      = node.Attributes["tintcolor"];
                var attrOffsetX   = node.Attributes["offsetx"];
                var attrOffsetY   = node.Attributes["offsety"];

                var tiledLayer = new TiledLayer();
                tiledLayer.id      = int.Parse(node.Attributes["id"].Value);
                tiledLayer.name    = node.Attributes["name"].Value;
                tiledLayer.height  = int.Parse(node.Attributes["height"].Value);
                tiledLayer.width   = int.Parse(node.Attributes["width"].Value);
                tiledLayer.type    = "tilelayer";
                tiledLayer.visible = true;

                if (attrVisible != null)
                {
                    tiledLayer.visible = attrVisible.Value == "1";
                }
                if (attrLocked != null)
                {
                    tiledLayer.locked = attrLocked.Value == "1";
                }
                if (attrTint != null)
                {
                    tiledLayer.tintcolor = attrTint.Value;
                }
                if (attrOffsetX != null)
                {
                    tiledLayer.offsetX = int.Parse(attrOffsetX.Value);
                }
                if (attrOffsetY != null)
                {
                    tiledLayer.offsetY = int.Parse(attrOffsetY.Value);
                }
                if (nodesProperty != null)
                {
                    tiledLayer.properties = ParseProperties(nodesProperty);
                }

                if (encoding == "csv")
                {
                    var csvs = nodeData.InnerText.Split(',');

                    tiledLayer.data = new int[csvs.Length];
                    tiledLayer.dataRotationFlags = new byte[csvs.Length];

                    // Parse the comma separated csv string and update the inner data as well as the data rotation flags
                    for (var i = 0; i < csvs.Length; i++)
                    {
                        var rawID = uint.Parse(csvs[i]);
                        var hor   = ((rawID & FLIPPED_HORIZONTALLY_FLAG));
                        var ver   = ((rawID & FLIPPED_VERTICALLY_FLAG));
                        var dia   = ((rawID & FLIPPED_DIAGONALLY_FLAG));
                        tiledLayer.dataRotationFlags[i] = (byte)((hor | ver | dia) >> SHIFT_FLIP_FLAG_TO_BYTE);

                        // assign data to rawID with the rotation flags cleared
                        tiledLayer.data[i] = (int)(rawID & ~(FLIPPED_HORIZONTALLY_FLAG | FLIPPED_VERTICALLY_FLAG | FLIPPED_DIAGONALLY_FLAG));
                    }
                }
                else if (encoding == "base64")
                {
                    var compression = nodeData.Attributes["compression"]?.Value;

                    using (var base64DataStream = new MemoryStream(Convert.FromBase64String(nodeData.InnerText)))
                    {
                        if (compression == null)
                        {
                            // Parse the decoded bytes and update the inner data as well as the data rotation flags
                            var rawBytes = new byte[4];
                            tiledLayer.data = new int[base64DataStream.Length];
                            tiledLayer.dataRotationFlags = new byte[base64DataStream.Length];

                            for (var i = 0; i < base64DataStream.Length; i++)
                            {
                                base64DataStream.Read(rawBytes, 0, rawBytes.Length);
                                var rawID = BitConverter.ToUInt32(rawBytes, 0);
                                var hor   = ((rawID & FLIPPED_HORIZONTALLY_FLAG));
                                var ver   = ((rawID & FLIPPED_VERTICALLY_FLAG));
                                var dia   = ((rawID & FLIPPED_DIAGONALLY_FLAG));
                                tiledLayer.dataRotationFlags[i] = (byte)((hor | ver | dia) >> SHIFT_FLIP_FLAG_TO_BYTE);

                                // assign data to rawID with the rotation flags cleared
                                tiledLayer.data[i] = (int)(rawID & ~(FLIPPED_HORIZONTALLY_FLAG | FLIPPED_VERTICALLY_FLAG | FLIPPED_DIAGONALLY_FLAG));
                            }
                        }
                        else if (compression == "zlib")
                        {
                            // .NET doesn't play well with the headered zlib data that Tiled produces,
                            // so we have to manually skip the 2-byte header to get what DeflateStream's looking for
                            // Should an external library be used instead of this hack?
                            base64DataStream.ReadByte();
                            base64DataStream.ReadByte();

                            using (var decompressionStream = new DeflateStream(base64DataStream, CompressionMode.Decompress))
                            {
                                // Parse the raw decompressed bytes and update the inner data as well as the data rotation flags
                                var decompressedDataBuffer = new byte[4]; // size of each tile
                                var dataRotationFlagsList  = new List <byte>();
                                var layerDataList          = new List <int>();

                                while (decompressionStream.Read(decompressedDataBuffer, 0, decompressedDataBuffer.Length) == decompressedDataBuffer.Length)
                                {
                                    var rawID = BitConverter.ToUInt32(decompressedDataBuffer, 0);
                                    var hor   = ((rawID & FLIPPED_HORIZONTALLY_FLAG));
                                    var ver   = ((rawID & FLIPPED_VERTICALLY_FLAG));
                                    var dia   = ((rawID & FLIPPED_DIAGONALLY_FLAG));
                                    dataRotationFlagsList.Add((byte)((hor | ver | dia) >> SHIFT_FLIP_FLAG_TO_BYTE));

                                    // assign data to rawID with the rotation flags cleared
                                    layerDataList.Add((int)(rawID & ~(FLIPPED_HORIZONTALLY_FLAG | FLIPPED_VERTICALLY_FLAG | FLIPPED_DIAGONALLY_FLAG)));
                                }

                                tiledLayer.data = layerDataList.ToArray();
                                tiledLayer.dataRotationFlags = dataRotationFlagsList.ToArray();
                            }
                        }
                        else if (compression == "gzip")
                        {
                            using (var decompressionStream = new GZipStream(base64DataStream, CompressionMode.Decompress))
                            {
                                // Parse the raw decompressed bytes and update the inner data as well as the data rotation flags
                                var decompressedDataBuffer = new byte[4]; // size of each tile
                                var dataRotationFlagsList  = new List <byte>();
                                var layerDataList          = new List <int>();

                                while (decompressionStream.Read(decompressedDataBuffer, 0, decompressedDataBuffer.Length) == decompressedDataBuffer.Length)
                                {
                                    var rawID = BitConverter.ToUInt32(decompressedDataBuffer, 0);
                                    var hor   = ((rawID & FLIPPED_HORIZONTALLY_FLAG));
                                    var ver   = ((rawID & FLIPPED_VERTICALLY_FLAG));
                                    var dia   = ((rawID & FLIPPED_DIAGONALLY_FLAG));

                                    dataRotationFlagsList.Add((byte)((hor | ver | dia) >> SHIFT_FLIP_FLAG_TO_BYTE));

                                    // assign data to rawID with the rotation flags cleared
                                    layerDataList.Add((int)(rawID & ~(FLIPPED_HORIZONTALLY_FLAG | FLIPPED_VERTICALLY_FLAG | FLIPPED_DIAGONALLY_FLAG)));
                                }

                                tiledLayer.data = layerDataList.ToArray();
                                tiledLayer.dataRotationFlags = dataRotationFlagsList.ToArray();
                            }
                        }
                        else
                        {
                            throw new TiledException("Zstandard compression is currently not supported");
                        }
                    }
                }
                else
                {
                    throw new TiledException("Only CSV and Base64 encodings are currently supported");
                }

                result.Add(tiledLayer);
            }

            foreach (XmlNode node in nodesObjectGroup)
            {
                var nodesProperty = node.SelectNodes("properties/property");
                var nodesObject   = node.SelectNodes("object");
                var attrVisible   = node.Attributes["visible"];
                var attrLocked    = node.Attributes["locked"];
                var attrTint      = node.Attributes["tintcolor"];
                var attrOffsetX   = node.Attributes["offsetx"];
                var attrOffsetY   = node.Attributes["offsety"];

                var tiledLayer = new TiledLayer();
                tiledLayer.id      = int.Parse(node.Attributes["id"].Value);
                tiledLayer.name    = node.Attributes["name"].Value;
                tiledLayer.objects = ParseObjects(nodesObject);
                tiledLayer.type    = "objectgroup";
                tiledLayer.visible = true;

                if (attrVisible != null)
                {
                    tiledLayer.visible = attrVisible.Value == "1";
                }
                if (attrLocked != null)
                {
                    tiledLayer.locked = attrLocked.Value == "1";
                }
                if (attrTint != null)
                {
                    tiledLayer.tintcolor = attrTint.Value;
                }
                if (attrOffsetX != null)
                {
                    tiledLayer.offsetX = int.Parse(attrOffsetX.Value);
                }
                if (attrOffsetY != null)
                {
                    tiledLayer.offsetY = int.Parse(attrOffsetY.Value);
                }
                if (nodesProperty != null)
                {
                    tiledLayer.properties = ParseProperties(nodesProperty);
                }

                result.Add(tiledLayer);
            }

            foreach (XmlNode node in nodesImageLayer)
            {
                var nodesProperty = node.SelectNodes("properties/property");
                var nodeImage     = node.SelectSingleNode("image");
                var attrVisible   = node.Attributes["visible"];
                var attrLocked    = node.Attributes["locked"];
                var attrTint      = node.Attributes["tintcolor"];
                var attrOffsetX   = node.Attributes["offsetx"];
                var attrOffsetY   = node.Attributes["offsety"];

                var tiledLayer = new TiledLayer();
                tiledLayer.id      = int.Parse(node.Attributes["id"].Value);
                tiledLayer.name    = node.Attributes["name"].Value;
                tiledLayer.type    = "imagelayer";
                tiledLayer.visible = true;

                if (attrVisible != null)
                {
                    tiledLayer.visible = attrVisible.Value == "1";
                }
                if (attrLocked != null)
                {
                    tiledLayer.locked = attrLocked.Value == "1";
                }
                if (attrTint != null)
                {
                    tiledLayer.tintcolor = attrTint.Value;
                }
                if (attrOffsetX != null)
                {
                    tiledLayer.offsetX = int.Parse(attrOffsetX.Value);
                }
                if (attrOffsetY != null)
                {
                    tiledLayer.offsetY = int.Parse(attrOffsetY.Value);
                }
                if (nodesProperty != null)
                {
                    tiledLayer.properties = ParseProperties(nodesProperty);
                }
                if (nodeImage != null)
                {
                    tiledLayer.image = ParseImage(nodeImage);
                }

                result.Add(tiledLayer);
            }

            return(result.ToArray());
        }
Ejemplo n.º 26
0
        public static unsafe bool Read(Stream stream, out int width, out int height, out Color[] pixels)
        {
            // This could likely be optimized a buuunch more
            // We also ignore all checksums when reading because they don't seem super important for game usage

            width = height = 0;

            var hasTransparency = false;
            var depth           = 8;
            var color           = Colors.Truecolor;
            var compression     = 0;
            var filter          = 0;
            var interlace       = Interlace.None;
            var components      = 4;

            using MemoryStream idat = new MemoryStream();
            Span <byte> idatChunk    = stackalloc byte[4096];
            Span <byte> palette      = stackalloc byte[0];
            Span <byte> alphaPalette = stackalloc byte[0];
            Span <byte> fourbytes    = stackalloc byte[4];

            bool hasIHDR = false, hasPLTE = false, hasIDAT = false;

            // Check PNG Header
            if (!IsValid(stream))
            {
                throw new Exception("Stream is not PNG");
            }

            // Skip PNG header
            stream.Seek(8, SeekOrigin.Current);

            // Read Chunks
            while (stream.Position < stream.Length)
            {
                long chunkStartPosition = stream.Position;

                // chunk length
                stream.Read(fourbytes);
                int chunkLength = SwapEndian(BitConverter.ToInt32(fourbytes));

                // chunk type
                stream.Read(fourbytes);

                // IHDR Chunk
                if (Check("IHDR", fourbytes))
                {
                    hasIHDR = true;
                    stream.Read(fourbytes);
                    width = SwapEndian(BitConverter.ToInt32(fourbytes));
                    stream.Read(fourbytes);
                    height          = SwapEndian(BitConverter.ToInt32(fourbytes));
                    depth           = stream.ReadByte();
                    color           = (Colors)stream.ReadByte();
                    compression     = stream.ReadByte();
                    filter          = stream.ReadByte();
                    interlace       = (Interlace)stream.ReadByte();
                    hasTransparency = color == Colors.GreyscaleAlpha || color == Colors.TruecolorAlpha;

                    if (color == Colors.Greyscale || color == Colors.Indexed)
                    {
                        components = 1;
                    }
                    else if (color == Colors.GreyscaleAlpha)
                    {
                        components = 2;
                    }
                    else if (color == Colors.Truecolor)
                    {
                        components = 3;
                    }
                    else if (color == Colors.TruecolorAlpha)
                    {
                        components = 4;
                    }

                    // currently don't support interlacing as I'm actually not sure where the interlace step takes place lol
                    if (interlace == Interlace.Adam7)
                    {
                        throw new NotImplementedException("Interlaced PNGs not implemented");
                    }

                    if (depth != 1 && depth != 2 && depth != 4 && depth != 8 && depth != 16)
                    {
                        throw new NotSupportedException($"{depth}-bit depth not supported");
                    }

                    if (filter != 0)
                    {
                        throw new NotSupportedException($"filter {filter} not supported");
                    }

                    if (compression != 0)
                    {
                        throw new NotSupportedException($"compression {compression} not supported");
                    }
                }
                // PLTE Chunk (Indexed Palette)
                else if (Check("PLTE", fourbytes))
                {
                    hasPLTE = true;
                    palette = stackalloc byte[chunkLength];
                    stream.Read(palette);
                }
                // IDAT Chunk (Image Data)
                else if (Check("IDAT", fourbytes))
                {
                    hasIDAT = true;

                    for (int i = 0; i < chunkLength; i += idatChunk.Length)
                    {
                        int size = Math.Min(idatChunk.Length, chunkLength - i);
                        stream.Read(idatChunk.Slice(0, size));
                        idat.Write(idatChunk.Slice(0, size));
                    }
                }
                // tRNS Chunk (Alpha Palette)
                else if (Check("tRNS", fourbytes))
                {
                    if (color == Colors.Indexed)
                    {
                        alphaPalette = stackalloc byte[chunkLength];
                        stream.Read(alphaPalette);
                    }
                    else if (color == Colors.Greyscale)
                    {
                    }
                    else if (color == Colors.Truecolor)
                    {
                    }
                }
                // bKGD Chunk (Background)
                else if (Check("bKGD", fourbytes))
                {
                }

                // seek to end of the chunk
                stream.Seek(chunkStartPosition + chunkLength + 12, SeekOrigin.Begin);
            }

            // checks
            if (!hasIHDR)
            {
                throw new Exception("PNG Missing IHDR data");
            }

            if (!hasIDAT)
            {
                throw new Exception("PNG Missing IDAT data");
            }

            if (!hasPLTE && color == Colors.Indexed)
            {
                throw new Exception("PNG Missing PLTE data");
            }

            // Parse the IDAT data into Pixels
            // It would be cool to do this line-by-line so we don't need to create a buffer to store the decompressed stream
            {
                byte[] buffer = new byte[width * height * (depth == 16 ? 2 : 1) * 4 + height];

                // decompress the image data
                // using deflate since it's built into C# and I ~think~ PNG always requires zlib to use deflate
                {
                    idat.Seek(2, SeekOrigin.Begin);
                    using DeflateStream deflateStream = new DeflateStream(idat, CompressionMode.Decompress);
                    deflateStream.Read(buffer);
                }

                // apply filter pass - this happens in-place
                {
                    int lineLength = (int)Math.Ceiling((width * components * depth) / 8f);
                    int bpp        = Math.Max(1, (components * depth) / 8);
                    int dest       = 0;

                    // each scanline
                    for (int y = 0; y < height; y++)
                    {
                        int  source     = y * (lineLength + 1) + 1;
                        byte lineFilter = buffer[source - 1];

                        // 0 - None
                        if (lineFilter == 0)
                        {
                            Array.Copy(buffer, source, buffer, dest, lineLength);
                        }
                        // 1 - Sub
                        else if (lineFilter == 1)
                        {
                            Array.Copy(buffer, source, buffer, dest, Math.Min(bpp, lineLength));
                            for (int x = bpp; x < lineLength; x++)
                            {
                                buffer[dest + x] = (byte)(buffer[source + x] + buffer[dest + x - bpp]);
                            }
                        }
                        // 2 - Up
                        else if (lineFilter == 2)
                        {
                            if (y <= 0)
                            {
                                Array.Copy(buffer, source, buffer, dest, lineLength);
                            }
                            else
                            {
                                for (int x = 0; x < lineLength; x++)
                                {
                                    buffer[dest + x] = (byte)(buffer[source + x] + buffer[dest + x - lineLength]);
                                }
                            }
                        }
                        // 3 - Average
                        else if (lineFilter == 3)
                        {
                            if (y <= 0)
                            {
                                Array.Copy(buffer, source, buffer, dest, Math.Min(bpp, lineLength));
                                for (int x = bpp; x < lineLength; x++)
                                {
                                    buffer[dest + x] = (byte)(buffer[source + x] + ((buffer[dest + x - bpp] + 0) / 2));
                                }
                            }
                            else
                            {
                                for (int x = 0; x < bpp; x++)
                                {
                                    buffer[dest + x] = (byte)(buffer[source + x] + ((0 + buffer[dest + x - lineLength]) / 2));
                                }

                                for (int x = bpp; x < lineLength; x++)
                                {
                                    buffer[dest + x] = (byte)(buffer[source + x] + ((buffer[dest + x - bpp] + buffer[dest + x - lineLength]) / 2));
                                }
                            }
                        }
                        // 4 - Paeth
                        else if (lineFilter == 4)
                        {
                            if (y <= 0)
                            {
                                Array.Copy(buffer, source, buffer, dest, Math.Min(bpp, lineLength));
                                for (int x = bpp; x < lineLength; x++)
                                {
                                    buffer[dest + x] = (byte)(buffer[source + x] + buffer[dest + x - bpp]);
                                }
                            }
                            else
                            {
                                for (int x = 0, c = Math.Min(bpp, lineLength); x < c; x++)
                                {
                                    buffer[dest + x] = (byte)(buffer[source + x] + buffer[dest + x - lineLength]);
                                }

                                for (int x = bpp; x < lineLength; x++)
                                {
                                    buffer[dest + x] = (byte)(buffer[source + x] + PaethPredictor(buffer[dest + x - bpp], buffer[dest + x - lineLength], buffer[dest + x - bpp - lineLength]));
                                }
                            }
                        }

                        dest += lineLength;
                    }
                }

                // if the bit-depth isn't 8, convert it
                if (depth != 8)
                {
                    throw new NotImplementedException("Non 8-bit PNGs not Implemented");
                }

                // Convert bytes to RGBA data
                // We work backwards as to not overwrite data
                {
                    // Indexed Color
                    if (color == Colors.Indexed)
                    {
                        for (int p = width * height - 1, i = width * height * 4 - 4; p >= 0; p--, i -= 4)
                        {
                            int id = buffer[p] * 3;
                            buffer[i + 3] = (alphaPalette == null || buffer[p] >= alphaPalette.Length) ? (byte)255 : alphaPalette[buffer[p]];
                            buffer[i + 2] = palette[id + 2];
                            buffer[i + 1] = palette[id + 1];
                            buffer[i + 0] = palette[id + 0];
                        }
                    }
                    // Grayscale
                    else if (color == Colors.Greyscale)
                    {
                        for (int p = width * height - 1, i = width * height * 4 - 4; p >= 0; p--, i -= 4)
                        {
                            buffer[i + 3] = 255;
                            buffer[i + 2] = buffer[p];
                            buffer[i + 1] = buffer[p];
                            buffer[i + 0] = buffer[p];
                        }
                    }
                    // Grayscale-Alpha
                    else if (color == Colors.GreyscaleAlpha)
                    {
                        for (int p = width * height * 2 - 2, i = width * height * 4 - 4; p >= 0; p -= 2, i -= 4)
                        {
                            byte val = buffer[p], alpha = buffer[p + 1];
                            buffer[i + 3] = buffer[p + 1];
                            buffer[i + 2] = buffer[p];
                            buffer[i + 1] = buffer[p];
                            buffer[i + 0] = buffer[p];
                        }
                    }
                    // Truecolor
                    else if (color == Colors.Truecolor)
                    {
                        for (int p = width * height * 3 - 3, i = width * height * 4 - 4; p >= 0; p -= 3, i -= 4)
                        {
                            buffer[i + 3] = 255;
                            buffer[i + 2] = buffer[p + 2];
                            buffer[i + 1] = buffer[p + 1];
                            buffer[i + 0] = buffer[p + 0];
                        }
                    }
                }

                // set RGBA data to Color array
                {
                    pixels = new Color[width * height];

                    fixed(byte *inArray = buffer)
                    fixed(Color * outArray = pixels)
                    {
                        Buffer.MemoryCopy(inArray, outArray, width * height * 4, width * height * 4);
                    }
                }
            }

            return(true);
        }
Ejemplo n.º 27
0
        private static void DecompressSecondGen(string file, StructureValueCollection headerValues)
        {
            using (MemoryStream msOutput = new MemoryStream())
            {
                using (FileStream fsInput = new FileStream(file, FileMode.Open))
                {
                    using (BinaryReader brInput = new BinaryReader(fsInput))
                    {
                        //constants
                        int headerSize = (int)headerValues.GetInteger("_header_length_");
                        int chunkSize  = headerValues.HasInteger("compression data chunk size") ?
                                         (int)headerValues.GetInteger("compression data chunk size") : 0x40000;
                        uint chunkTableOffset = headerValues.HasInteger("compression chunk table offset") ?
                                                (uint)headerValues.GetInteger("compression chunk table offset") : (uint)headerSize;
                        int chunkCount = headerValues.HasInteger("compression chunk table count") ?
                                         (int)headerValues.GetInteger("compression chunk table count") : 0x400;

                        //header is uncompressed
                        msOutput.Write(brInput.ReadBytes(headerSize), 0, headerSize);

                        List <Tuple <int, int> > Chunks = new List <Tuple <int, int> >();

                        fsInput.Seek(chunkTableOffset, SeekOrigin.Begin);

                        for (int i = 0; i < chunkCount; i++)
                        {
                            int csize  = brInput.ReadInt32();
                            int offset = brInput.ReadInt32();

                            if (csize == 0)
                            {
                                break;
                            }

                            if (offset >= fsInput.Length)
                            {
                                throw new ArgumentException("Chunk " + i + " has an offset past the end of the file.");
                            }

                            Chunks.Add(new Tuple <int, int>(csize, offset));
                        }

                        //Decompress and write each chunk
                        for (int i = 0; i < Chunks.Count; i++)
                        {
                            //check for faux-compression some other tools use
                            if (Chunks[i].Item1 < 0)
                            {
                                int    invertedSize = -Chunks[i].Item1;
                                byte[] aaa          = new byte[invertedSize];

                                fsInput.Seek(Chunks[i].Item2, SeekOrigin.Begin);

                                int readSize = fsInput.Read(aaa, 0, invertedSize);

                                msOutput.Write(aaa, 0, readSize);
                            }
                            else
                            {
                                fsInput.Seek(Chunks[i].Item2 + 2, SeekOrigin.Begin);

                                int    realsize  = chunkSize;
                                byte[] chunkData = new byte[realsize];

                                using (DeflateStream ds = new DeflateStream(fsInput, CompressionMode.Decompress, true))
                                {
                                    realsize = ds.Read(chunkData, 0, chunkData.Length);
                                }

                                msOutput.Write(chunkData, 0, realsize);
                            }
                        }

                        //clear compressed bit so it can run ingame
                        if (headerValues.HasInteger("flags"))
                        {
                            fsInput.Seek((long)headerValues.GetInteger("_header_flags_"), SeekOrigin.Begin);
                            int flags = brInput.ReadInt16();
                            flags &= 0x7FFFFFFE;

                            msOutput.Seek((long)headerValues.GetInteger("_header_flags_"), SeekOrigin.Begin);
                            msOutput.Write(BitConverter.GetBytes((short)flags), 0, 2);
                        }
                    }
                }
                File.WriteAllBytes(file, msOutput.ToArray());
            }
        }
Ejemplo n.º 28
0
        private static NanoStruct DESdecodestruct(byte[] byte_0)
        {
            NanoStruct   struct3;
            MemoryStream memoryStream_0 = new MemoryStream();
            MemoryStream memoryStream_1 = new MemoryStream();

            BinaryReader binaryReader_0 = new BinaryReader(memoryStream_0);
            BinaryWriter binaryWriter_0 = new BinaryWriter(memoryStream_1);



            byte_0         = DESDecryptor.TransformFinalBlock(byte_0, 0, byte_0.Length);
            memoryStream_0 = new MemoryStream(byte_0);
            binaryReader_0 = new BinaryReader(memoryStream_0);
            if (binaryReader_0.ReadBoolean())
            {
                int           num    = binaryReader_0.ReadInt32();
                DeflateStream stream = new DeflateStream(memoryStream_0, CompressionMode.Decompress, false);
                byte[]        array  = new byte[(num - 1) + 1];
                stream.Read(array, 0, array.Length);
                stream.Close();
                memoryStream_0 = new MemoryStream(array);
                binaryReader_0 = new BinaryReader(memoryStream_0);
            }
            NanoStruct struct2 = new NanoStruct
            {
                byte_0 = binaryReader_0.ReadByte(),
                byte_1 = binaryReader_0.ReadByte()
            };

            if (binaryReader_0.ReadBoolean())
            {
                struct2.guid_0 = new Guid(binaryReader_0.ReadBytes(0x10));
            }
            while (memoryStream_0.Position != memoryStream_0.Length)
            {
                string[] strArray;
                int      num3;
                int      num4;
                switch (binaryReader_0.ReadByte())
                {
                case 0:
                {
                    objectlist.Add(binaryReader_0.ReadBoolean());
                    continue;
                }

                case 1:
                {
                    objectlist.Add(binaryReader_0.ReadByte());
                    continue;
                }

                case 2:
                {
                    objectlist.Add(binaryReader_0.ReadBytes(binaryReader_0.ReadInt32()));
                    continue;
                }

                case 3:
                {
                    objectlist.Add(binaryReader_0.ReadChar());
                    continue;
                }

                case 4:
                {
                    objectlist.Add(binaryReader_0.ReadString().ToCharArray());
                    continue;
                }

                case 5:
                {
                    objectlist.Add(binaryReader_0.ReadDecimal());
                    continue;
                }

                case 6:
                {
                    objectlist.Add(binaryReader_0.ReadDouble());
                    continue;
                }

                case 7:
                {
                    objectlist.Add(binaryReader_0.ReadInt32());
                    continue;
                }

                case 8:
                {
                    objectlist.Add(binaryReader_0.ReadInt64());
                    continue;
                }

                case 9:
                {
                    objectlist.Add(binaryReader_0.ReadSByte());
                    continue;
                }

                case 10:
                {
                    objectlist.Add(binaryReader_0.ReadInt16());
                    continue;
                }

                case 11:
                {
                    objectlist.Add(binaryReader_0.ReadSingle());
                    continue;
                }

                case 12:
                {
                    objectlist.Add(binaryReader_0.ReadString());
                    continue;
                }

                case 13:
                {
                    objectlist.Add(binaryReader_0.ReadUInt32());
                    continue;
                }

                case 14:
                {
                    objectlist.Add(binaryReader_0.ReadUInt64());
                    continue;
                }

                case 15:
                {
                    objectlist.Add(binaryReader_0.ReadUInt16());
                    continue;
                }

                case 0x10:
                {
                    objectlist.Add(DateTime.FromBinary(binaryReader_0.ReadInt64()));
                    continue;
                }

                case 0x11:
                    strArray = new string[(binaryReader_0.ReadInt32() - 1) + 1];
                    num3     = strArray.Length - 1;
                    num4     = 0;
                    goto Label_039A;

                case 0x12:
                {
                    Guid item = new Guid(binaryReader_0.ReadBytes(0x10));
                    objectlist.Add(item);
                    continue;
                }

                case 0x13:
                {
                    Size size = new Size(binaryReader_0.ReadInt32(), binaryReader_0.ReadInt32());
                    objectlist.Add(size);
                    continue;
                }

                case 20:
                {
                    Rectangle rectangle = new Rectangle(binaryReader_0.ReadInt32(), binaryReader_0.ReadInt32(), binaryReader_0.ReadInt32(), binaryReader_0.ReadInt32());
                    objectlist.Add(rectangle);
                    continue;
                }

                case 0x15:
                {
                    objectlist.Add(new Version(binaryReader_0.ReadString()));
                    continue;
                }

                default:
                {
                    continue;
                }
                }
Label_0385:
                strArray[num4] = binaryReader_0.ReadString();
                num4++;
Label_039A:
                if (num4 <= num3)
                {
                    goto Label_0385;
                }
                objectlist.Add(strArray);
            }
            struct2.object_0 = objectlist.ToArray();
            struct3          = struct2;
            objectlist.Clear();
            binaryReader_0.Close();

            return(struct3);
        }
Ejemplo n.º 29
0
        /// <summary>
        /// Reads data from an ARC file and puts it into a Byte array (or NULL if not found)
        /// </summary>
        /// <param name="dataId">The string ID for the data which we are retieving.</param>
        /// <returns>Returns byte array of the data corresponding to the string ID.</returns>
        public byte[] GetData(string dataId)
        {
            if (TQDebug.ArcFileDebugLevel > 0)
            {
                TQDebug.DebugWriteLine(string.Format(CultureInfo.InvariantCulture, "ARCFile.GetData({0})", dataId));
            }

            if (!this.fileHasBeenRead)
            {
                this.ReadARCToC();
            }

            if (this.directoryEntries == null)
            {
                if (TQDebug.ArcFileDebugLevel > 1)
                {
                    TQDebug.DebugWriteLine(string.Format(CultureInfo.InvariantCulture, "Error - Could not read {0}", this.FileName));
                }

                // could not read the file
                return(null);
            }

            // First normalize the filename
            dataId = TQData.NormalizeRecordPath(dataId);
            if (TQDebug.ArcFileDebugLevel > 1)
            {
                TQDebug.DebugWriteLine(string.Format(CultureInfo.InvariantCulture, "Normalized dataID = {0}", dataId));
            }

            // Find our file in the toc.
            // First strip off the leading folder since it is just the ARC name
            int firstPathDelim = dataId.IndexOf('\\');

            if (firstPathDelim != -1)
            {
                dataId = dataId.Substring(firstPathDelim + 1);
            }

            // Now see if this file is in the toc.
            ARCDirEntry directoryEntry;

            if (!this.directoryEntries.ContainsKey(dataId))
            {
                // record not found
                if (TQDebug.ArcFileDebugLevel > 1)
                {
                    TQDebug.DebugWriteLine(string.Format(CultureInfo.InvariantCulture, "Error - {0} not found.", dataId));
                }
                return(null);
            }
            else
            {
                directoryEntry = this.directoryEntries[dataId];
            }

            // Now open the ARC file and read in the record.
            using (FileStream arcFile = new FileStream(this.FileName, FileMode.Open, FileAccess.Read))
            {
                // Allocate memory for the uncompressed data
                byte[] data = new byte[directoryEntry.RealSize];

                // Now process each part of this record
                int startPosition = 0;

                // First see if the data was just stored without compression.
                if ((directoryEntry.StorageType == 1) && (directoryEntry.CompressedSize == directoryEntry.RealSize))
                {
                    if (TQDebug.ArcFileDebugLevel > 1)
                    {
                        TQDebug.DebugWriteLine(string.Format(
                                                   CultureInfo.InvariantCulture,
                                                   "Offset={0}  Size={1}",
                                                   directoryEntry.FileOffset,
                                                   directoryEntry.RealSize));
                    }

                    arcFile.Seek(directoryEntry.FileOffset, SeekOrigin.Begin);
                    arcFile.Read(data, 0, directoryEntry.RealSize);
                }
                else
                {
                    // The data was compressed so we attempt to decompress it.
                    foreach (ARCPartEntry partEntry in directoryEntry.Parts)
                    {
                        // seek to the part we want
                        arcFile.Seek(partEntry.FileOffset, SeekOrigin.Begin);

                        // Ignore the zlib compression method.
                        arcFile.ReadByte();

                        // Ignore the zlib compression flags.
                        arcFile.ReadByte();

                        // Create a deflate stream.
                        using (DeflateStream deflate = new DeflateStream(arcFile, CompressionMode.Decompress, true))
                        {
                            int bytesRead;
                            int partLength = 0;
                            while ((bytesRead = deflate.Read(data, startPosition, data.Length - startPosition)) > 0)
                            {
                                startPosition += bytesRead;
                                partLength    += bytesRead;

                                // break out of the read loop if we have processed this part completely.
                                if (partLength >= partEntry.RealSize)
                                {
                                    break;
                                }
                            }
                        }
                    }
                }

                if (TQDebug.ArcFileDebugLevel > 0)
                {
                    TQDebug.DebugWriteLine("Exiting ARCFile.GetData()");
                }

                return(data);
            }
        }
Ejemplo n.º 30
0
        private void ReceiveMessageLoop()
        {
            logger.Log(Roomid, LogLevel.Trace, "ReceiveMessageLoop Started");
            try
            {
                var stableBuffer = new byte[16];
                var buffer       = new byte[4096];
                while (dmTcpConnected)
                {
                    dmNetStream.ReadB(stableBuffer, 0, 16);
                    Parse2Protocol(stableBuffer, out DanmakuProtocol protocol);

                    if (protocol.PacketLength < 16)
                    {
                        throw new NotSupportedException("协议失败: (L:" + protocol.PacketLength + ")");
                    }

                    var payloadlength = protocol.PacketLength - 16;
                    if (payloadlength == 0)
                    {
                        continue;//没有内容了
                    }

                    if (buffer.Length < payloadlength) // 不够长再申请
                    {
                        buffer = new byte[payloadlength];
                    }

                    dmNetStream.ReadB(buffer, 0, payloadlength);

                    if (protocol.Version == 2 && protocol.Action == 5) // 处理deflate消息
                    {
                        // Skip 0x78 0xDA
                        using (DeflateStream deflate = new DeflateStream(new MemoryStream(buffer, 2, payloadlength - 2), CompressionMode.Decompress))
                        {
                            while (deflate.Read(stableBuffer, 0, 16) > 0)
                            {
                                Parse2Protocol(stableBuffer, out protocol);
                                payloadlength = protocol.PacketLength - 16;
                                if (payloadlength == 0)
                                {
                                    continue;                      // 没有内容了
                                }
                                if (buffer.Length < payloadlength) // 不够长再申请
                                {
                                    buffer = new byte[payloadlength];
                                }
                                deflate.Read(buffer, 0, payloadlength);
                                ProcessDanmaku(protocol.Action, buffer, payloadlength);
                            }
                        }
                    }
                    else
                    {
                        ProcessDanmaku(protocol.Action, buffer, payloadlength);
                    }

                    void ProcessDanmaku(int action, byte[] local_buffer, int length)
                    {
                        switch (action)
                        {
                        case 3:
                            // var viewer = BitConverter.ToUInt32(local_buffer.Take(4).Reverse().ToArray(), 0); //观众人数
                            break;

                        case 5:    //playerCommand
                            var json = Encoding.UTF8.GetString(local_buffer, 0, length);
                            try
                            {
                                ReceivedDanmaku?.Invoke(this, new ReceivedDanmakuArgs()
                                {
                                    Danmaku = new DanmakuModel(json)
                                });
                            }
                            catch (Exception ex)
                            {
                                logger.Log(Roomid, LogLevel.Warn, "", ex);
                            }
                            break;

                        default:
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                dmError = ex;
                // logger.Error(ex);

                logger.Log(Roomid, LogLevel.Debug, "Disconnected");
                dmClient?.Close();
                dmNetStream = null;
                if (!(dmTokenSource?.IsCancellationRequested ?? true))
                {
                    logger.Log(Roomid, LogLevel.Warn, "弹幕连接被断开,将尝试重连", ex);
                    ConnectWithRetry();
                }
            }
        }
Ejemplo n.º 31
0
        public byte[] Decrypt(Stream resourceStream)
        {
            byte flags = (byte)resourceStream.ReadByte();

            Stream sourceStream       = resourceStream;
            int    sourceStreamOffset = 1;
            bool   didSomething       = false;

            sourceStream.Position += skipBytes;
            sourceStreamOffset    += skipBytes;

            byte allFlags = (byte)(desEncryptedFlag | deflatedFlag | bitwiseNotEncryptedFlag);

            if ((flags & ~allFlags) != 0)
            {
                Logger.w("Found unknown resource encryption flags: 0x{0:X2}", flags);
            }

            if ((flags & desEncryptedFlag) != 0)
            {
                var memStream = new MemoryStream((int)resourceStream.Length);
                using (var provider = new DESCryptoServiceProvider()) {
                    var iv = new byte[8];
                    sourceStream.Read(iv, 0, 8);
                    provider.IV  = iv;
                    provider.Key = GetKey(sourceStream);

                    using (var transform = provider.CreateDecryptor()) {
                        while (true)
                        {
                            int count = sourceStream.Read(buffer1, 0, buffer1.Length);
                            if (count <= 0)
                            {
                                break;
                            }
                            int count2 = transform.TransformBlock(buffer1, 0, count, buffer2, 0);
                            memStream.Write(buffer2, 0, count2);
                        }
                        var finalData = transform.TransformFinalBlock(buffer1, 0, 0);
                        memStream.Write(finalData, 0, finalData.Length);
                    }
                }
                sourceStream       = memStream;
                sourceStreamOffset = 0;
                didSomething       = true;
            }

            if ((flags & deflatedFlag) != 0)
            {
                var memStream = new MemoryStream((int)resourceStream.Length);
                sourceStream.Position = sourceStreamOffset;
                using (var inflater = new DeflateStream(sourceStream, CompressionMode.Decompress)) {
                    while (true)
                    {
                        int count = inflater.Read(buffer1, 0, buffer1.Length);
                        if (count <= 0)
                        {
                            break;
                        }
                        memStream.Write(buffer1, 0, count);
                    }
                }

                sourceStream       = memStream;
                sourceStreamOffset = 0;
                didSomething       = true;
            }

            if ((flags & bitwiseNotEncryptedFlag) != 0)
            {
                var memStream = new MemoryStream((int)resourceStream.Length);
                sourceStream.Position = sourceStreamOffset;
                for (int i = sourceStreamOffset; i < sourceStream.Length; i++)
                {
                    memStream.WriteByte((byte)~sourceStream.ReadByte());
                }

                sourceStream       = memStream;
                sourceStreamOffset = 0;
                didSomething       = true;
            }

            if (didSomething && sourceStream is MemoryStream)
            {
                var memStream = (MemoryStream)sourceStream;
                return(memStream.ToArray());
            }
            else
            {
                int    len  = (int)(sourceStream.Length - sourceStream.Position);
                byte[] data = new byte[len];
                sourceStream.Read(data, 0, len);
                return(data);
            }
        }
Ejemplo n.º 32
0
        private static T FromStream <T>(int version, Stream stream)
            where T : ItemBase <T>
        {
            try
            {
                stream.Seek(0, SeekOrigin.Begin);

                // Check
                {
                    var verifyCrc  = Crc32_Castagnoli.ComputeHash(new RangeStream(stream, 0, stream.Length - 4, true));
                    var orignalCrc = new byte[4];

                    using (var crcStream = new RangeStream(stream, stream.Length - 4, 4, true))
                    {
                        crcStream.Read(orignalCrc, 0, orignalCrc.Length);
                    }

                    if (!Unsafe.Equals(verifyCrc, orignalCrc))
                    {
                        throw new ArgumentException("Crc Error");
                    }
                }

                stream.Seek(0, SeekOrigin.Begin);

                if (version != (int)Varint.GetUInt64(stream))
                {
                    throw new ArgumentException("version");
                }
                int type = (int)Varint.GetUInt64(stream);

                using (var dataStream = new RangeStream(stream, stream.Position, stream.Length - stream.Position - 4, true))
                {
                    if (type == (int)ConvertCompressionAlgorithm.None)
                    {
                        return(ItemBase <T> .Import(dataStream, _bufferManager));
                    }
                    else if (type == (int)ConvertCompressionAlgorithm.Deflate)
                    {
                        using (var deflateBufferStream = new BufferStream(_bufferManager))
                        {
                            using (var deflateStream = new DeflateStream(dataStream, CompressionMode.Decompress, true))
                                using (var safeBuffer = _bufferManager.CreateSafeBuffer(1024 * 4))
                                {
                                    int length;

                                    while ((length = deflateStream.Read(safeBuffer.Value, 0, safeBuffer.Value.Length)) > 0)
                                    {
                                        deflateBufferStream.Write(safeBuffer.Value, 0, length);

                                        if (deflateBufferStream.Length > 1024 * 1024 * 32)
                                        {
                                            throw new Exception("too large");
                                        }
                                    }
                                }

                            deflateBufferStream.Seek(0, SeekOrigin.Begin);

                            return(ItemBase <T> .Import(deflateBufferStream, _bufferManager));
                        }
                    }
                    else
                    {
                        throw new ArgumentException("ArgumentException");
                    }
                }
            }
            catch (Exception e)
            {
                throw new ArgumentException(e.Message, e);
            }
            finally
            {
                if (stream != null)
                {
                    stream.Dispose();
                }
            }
        }
Ejemplo n.º 33
0
        /// <summary>
        /// Decompress and display the update contents
        /// </summary>
        /// <param name="data">data bytes</param>
        private void DecompressDisplay(byte[] data)
        {
            var compressedStream = new MemoryStream(data);
            Int32 x, y, w, h;

            using (MemoryStream clearStream = new MemoryStream()) {
                using (DeflateStream zipStream = new DeflateStream(compressedStream, CompressionMode.Decompress)) {
                    byte[] buffer = new byte[4096];
                    byte[] szBuf = new byte[4];
                    int readAmt;

                    readAmt = zipStream.Read(szBuf, 0, 4);
                    Debug.Assert(readAmt == 4);
                    UInt32 hdr = System.BitConverter.ToUInt32(szBuf, 0);
                    hdr = (UInt32)System.Net.IPAddress.NetworkToHostOrder((Int32)hdr);
                    width = (Int32)(hdr >> 16);
                    height = (Int32)(hdr & 0xFFFF);

                    readAmt = zipStream.Read(szBuf, 0, 4);
                    Debug.Assert(readAmt == 4);
                    hdr = System.BitConverter.ToUInt32(szBuf, 0);
                    hdr = (UInt32)System.Net.IPAddress.NetworkToHostOrder((Int32)hdr);
                    maskX = (Int32)(hdr >> 16);
                    maskY = (Int32)(hdr & 0xFFFF);

                    readAmt = zipStream.Read(szBuf, 0, 4);
                    Debug.Assert(readAmt == 4);
                    hdr = System.BitConverter.ToUInt32(szBuf, 0);
                    hdr = (UInt32)System.Net.IPAddress.NetworkToHostOrder((Int32)hdr);
                    maskWidth = (Int32)(hdr >> 16);
                    maskHeight = (Int32)(hdr & 0xFFFF);

                    if (!((prevMX == maskX) && (prevMY == maskY) && (prevMW == maskWidth) && (prevMH == maskHeight))) {
                        DisplayMask(maskX, maskY, maskWidth, maskHeight, width, height);

                        prevMX = maskX;
                        prevMY = maskY;
                        prevMW = maskWidth;
                        prevMH = maskHeight;
                    }

                    readAmt = zipStream.Read(szBuf, 0, 4);
                    Debug.Assert(readAmt == 4);
                    hdr = System.BitConverter.ToUInt32(szBuf, 0);
                    hdr = (UInt32)System.Net.IPAddress.NetworkToHostOrder((Int32)hdr);
                    x = (Int32)(hdr >> 16);
                    y = (Int32)(hdr & 0xFFFF);

                    readAmt = zipStream.Read(szBuf, 0, 4);
                    Debug.Assert(readAmt == 4);
                    hdr = System.BitConverter.ToUInt32(szBuf, 0);
                    hdr = (UInt32)System.Net.IPAddress.NetworkToHostOrder((Int32)hdr);
                    w = (Int32)(hdr >> 16);
                    h = (Int32)(hdr & 0xFFFF);

                    int read = 0;
                    while (true) {
                        try {
                            read = zipStream.Read(buffer, 0, buffer.Length);
                        } catch (Exception e) {
                            // Trace.WriteLine("{0} Error code: {}.", e.Message, e.ErrorCode);
                            MessageBox.Show("Message: " + e.Message, "FATAL");
                        }
                        if (read > 0)
                            clearStream.Write(buffer, 0, read);
                        else
                            break;
                    }
                    zipStream.Close();
                }

                DisplayUpdate(x, y, w, h, clearStream.ToArray());
            }
        }
Ejemplo n.º 34
0
        /// <summary>. </summary>
        public bool SyncFile(Lucene.Net.Store.Directory directory, string fileName, bool CompressBlobs)
        {
            var success = false;

            try
            {
                var blob = _blobContainer.GetBlobClient(_rootFolderName + fileName);
                _loggingService.Log(new LogEntry(LogLevel.Info, null,
                                                 $"Syncing file {fileName} for {_rootFolderName}"));
                // then we will get it fresh into local deflatedName
                // StreamOutput deflatedStream = new StreamOutput(CacheDirectory.CreateOutput(deflatedName));
                using (var deflatedStream = new MemoryStream())
                {
                    // get the deflated blob
                    blob.DownloadTo(deflatedStream);

#if FULLDEBUG
                    _loggingService.Log(new LogEntry(LogLevel.Info, null,
                                                     $"GET {fileName} RETREIVED {deflatedStream.Length} bytes"));
#endif

                    // seek back to begininng
                    deflatedStream.Seek(0, SeekOrigin.Begin);

                    if (ShouldCompressFile(fileName, CompressBlobs))
                    {
                        // open output file for uncompressed contents
                        using (var fileStream = new StreamOutput(directory.CreateOutput(fileName)))
                            using (var decompressor = new DeflateStream(deflatedStream, CompressionMode.Decompress))
                            {
                                var bytes = new byte[65535];
                                var nRead = 0;
                                do
                                {
                                    nRead = decompressor.Read(bytes, 0, 65535);
                                    if (nRead > 0)
                                    {
                                        fileStream.Write(bytes, 0, nRead);
                                    }
                                } while (nRead == 65535);
                            }
                    }
                    else
                    {
                        using (var fileStream = new StreamOutput(directory.CreateOutput(fileName)))
                        {
                            // get the blob
                            blob.DownloadTo(fileStream);

                            fileStream.Flush();
#if FULLDEBUG
                            _loggingService.Log(new LogEntry(LogLevel.Info, null,
                                                             $"GET {fileName} RETREIVED {fileStream.Length} bytes"));
#endif
                        }
                    }
                }

                success = true;
            }
            catch (Exception e)
            {
                _loggingService.Log(new LogEntry(LogLevel.Error, e,
                                                 $"GET {fileName} RETREIVED failed"));
            }

            return(success);
        }
Ejemplo n.º 35
0
        void ReadPacket( byte opcode )
        {
            reader.Remove( 1 ); // remove opcode
            lastOpcode = (PacketId)opcode;

            switch( (PacketId)opcode ) {
                case PacketId.Handshake:
                    {
                        byte protocolVer = reader.ReadUInt8();
                        ServerName = reader.ReadAsciiString();
                        ServerMotd = reader.ReadAsciiString();
                        game.LocalPlayer.SetUserType( reader.ReadUInt8() );
                        receivedFirstPosition = false;
                        game.LocalPlayer.ParseHackFlags( ServerName, ServerMotd );
                    } break;

                case PacketId.Ping:
                    break;

                case PacketId.LevelInitialise:
                    {
                        game.Map.Reset();
                        game.SetNewScreen( new LoadingMapScreen( game, ServerName, ServerMotd ) );
                        if( ServerMotd.Contains( "cfg=" ) ) {
                            ReadWomConfigurationAsync();
                        }
                        receivedFirstPosition = false;
                        gzipHeader = new GZipHeaderReader();
                        // Workaround because built in mono stream assumes that the end of stream
                        // has been reached the first time a read call returns 0. (MS.NET doesn't)
                        #if __MonoCS__
                        gzipStream = new DeflateStream( gzippedMap, true );
                        #else
                        gzipStream = new DeflateStream( gzippedMap, CompressionMode.Decompress );
                        if( OpenTK.Configuration.RunningOnMono ) {
                            Utils.LogWarning( "You are running on Mono, but this build does not support the Mono workaround." );
                            Utils.LogWarning( "You should either download the Mono compatible build or define '__MonoCS__' when targeting Mono. " +
                                             "(The Mono compiler already defines this by default)" );
                            Utils.LogWarning( "You will likely experience an 'Internal error (no progress possible) ReadInternal' exception when decompressing the map." );
                        }
                        #endif
                        mapSizeIndex = 0;
                        mapIndex = 0;
                        receiveStart = DateTime.UtcNow;
                    } break;

                case PacketId.LevelDataChunk:
                    {
                        int usedLength = reader.ReadInt16();
                        gzippedMap.Position = 0;
                        gzippedMap.SetLength( usedLength );

                        if( gzipHeader.done || gzipHeader.ReadHeader( gzippedMap ) ) {
                            if( mapSizeIndex < 4 ) {
                                mapSizeIndex += gzipStream.Read( mapSize, mapSizeIndex, 4 - mapSizeIndex );
                            }

                            if( mapSizeIndex == 4 ) {
                                if( map == null ) {
                                    int size = mapSize[0] << 24 | mapSize[1] << 16 | mapSize[2] << 8 | mapSize[3];
                                    map = new byte[size];
                                }
                                mapIndex += gzipStream.Read( map, mapIndex, map.Length - mapIndex );
                            }
                        }
                        reader.Remove( 1024 );
                        byte progress = reader.ReadUInt8();
                        game.Events.RaiseMapLoading( progress );
                    } break;

                case PacketId.LevelFinalise:
                    {
                        game.SetNewScreen( new NormalScreen( game ) );
                        int mapWidth = reader.ReadInt16();
                        int mapHeight = reader.ReadInt16();
                        int mapLength = reader.ReadInt16();

                        double loadingMs = ( DateTime.UtcNow - receiveStart ).TotalMilliseconds;
                        Utils.LogDebug( "map loading took:" + loadingMs );
                        game.Map.UseRawMap( map, mapWidth, mapHeight, mapLength );
                        game.Events.RaiseOnNewMapLoaded();
                        map = null;
                        gzipStream.Close();
                        if( sendWomId && !sentWomId ) {
                            SendChat( "/womid WoMClient-2.0.7" );
                            sentWomId = true;
                        }
                        gzipStream = null;
                        GC.Collect( 0 );
                    } break;

                case PacketId.SetBlock:
                    {
                        int x = reader.ReadInt16();
                        int y = reader.ReadInt16();
                        int z = reader.ReadInt16();
                        byte type = reader.ReadUInt8();
                        if( !game.Map.IsNotLoaded )
                            game.UpdateBlock( x, y, z, type );
                        else
                            Utils.LogWarning( "Server tried to update a block while still sending us the map!" );
                    } break;

                case PacketId.AddEntity:
                    {
                        byte entityId = reader.ReadUInt8();
                        string name = reader.ReadAsciiString();
                        AddEntity( entityId, name, name, true );
                    }  break;

                case PacketId.EntityTeleport:
                    {
                        byte entityId = reader.ReadUInt8();
                        ReadAbsoluteLocation( entityId, true );
                    } break;

                case PacketId.RelPosAndOrientationUpdate:
                    ReadRelativeLocation();
                    break;

                case PacketId.RelPosUpdate:
                    ReadRelativePosition();
                    break;

                case PacketId.OrientationUpdate:
                    ReadOrientation();
                    break;

                case PacketId.RemoveEntity:
                    {
                        byte entityId = reader.ReadUInt8();
                        Player player = game.Players[entityId];
                        if( entityId != 0xFF && player != null ) {
                            game.Events.RaiseEntityRemoved( entityId );
                            player.Despawn();
                            game.Players[entityId] = null;
                        }
                    } break;

                case PacketId.Message:
                    {
                        byte messageType = reader.ReadUInt8();
                        string text = reader.ReadChatString( ref messageType, useMessageTypes );
                        game.Chat.Add( text, (CpeMessage)messageType );
                    } break;

                case PacketId.Kick:
                    {
                        string reason = reader.ReadAsciiString();
                        game.Disconnect( "&eLost connection to the server", reason );
                        Dispose();
                    } break;

                case PacketId.SetPermission:
                    {
                        game.LocalPlayer.SetUserType( reader.ReadUInt8() );
                    } break;

                case PacketId.CpeExtInfo:
                    {
                        string appName = reader.ReadAsciiString();
                        Utils.LogDebug( "Server identified itself as: " + appName );
                        cpeServerExtensionsCount = reader.ReadInt16();
                    } break;

                case PacketId.CpeExtEntry:
                    {
                        string extName = reader.ReadAsciiString();
                        int extVersion = reader.ReadInt32();
                        Utils.LogDebug( "cpe ext: " + extName + " , " + extVersion );
                        if( extName == "HeldBlock" ) {
                            sendHeldBlock = true;
                        } else if( extName == "MessageTypes" ) {
                            useMessageTypes = true;
                        } else if( extName == "ExtPlayerList" ) {
                            UsingExtPlayerList = true;
                        } else if( extName == "PlayerClick" ) {
                            UsingPlayerClick = true;
                        } else if( extName == "EnvMapAppearance" && extVersion == 2 ) {
                            usingTexturePack = true;
                        }
                        cpeServerExtensionsCount--;

                        if( cpeServerExtensionsCount == 0 ) {
                            MakeExtInfo( Utils.AppName, clientExtensions.Length );
                            SendPacket();
                            for( int i = 0; i < clientExtensions.Length; i++ ) {
                                string name = clientExtensions[i];
                                int version = (name == "ExtPlayerList" || name == "EnvMapApperance") ? 2 : 1;
                                MakeExtEntry( name, version );
                                SendPacket();
                            }
                        }
                    } break;

                case PacketId.CpeSetClickDistance:
                    {
                        game.LocalPlayer.ReachDistance = reader.ReadInt16() / 32f;
                    } break;

                case PacketId.CpeCustomBlockSupportLevel:
                    {
                        byte supportLevel = reader.ReadUInt8();
                        MakeCustomBlockSupportLevel( 1 );
                        SendPacket();

                        if( supportLevel == 1 ) {
                            for( int i = (int)Block.CobblestoneSlab; i <= (int)Block.StoneBrick; i++ ) {
                                game.Inventory.CanPlace[i] = true;
                                game.Inventory.CanDelete[i] = true;
                            }
                            game.Events.RaiseBlockPermissionsChanged();
                        } else {
                            Utils.LogWarning( "Server's block support level is {0}, this client only supports level 1.", supportLevel );
                            Utils.LogWarning( "You won't be able to see or use blocks from levels above level 1" );
                        }
                    } break;

                case PacketId.CpeHoldThis:
                    {
                        byte blockType = reader.ReadUInt8();
                        bool canChange = reader.ReadUInt8() == 0;
                        game.Inventory.CanChangeHeldBlock = true;
                        game.Inventory.HeldBlock = (Block)blockType;
                        game.Inventory.CanChangeHeldBlock = canChange;
                    } break;

                case PacketId.CpeExtAddPlayerName:
                    {
                        short nameId = reader.ReadInt16();
                        string playerName = Utils.StripColours( reader.ReadAsciiString() );
                        string listName = reader.ReadAsciiString();
                        string groupName = reader.ReadAsciiString();
                        byte groupRank = reader.ReadUInt8();
                        if( nameId >= 0 && nameId <= 255 ) {
                            CpeListInfo oldInfo = game.CpePlayersList[nameId];
                            CpeListInfo info = new CpeListInfo( (byte)nameId, playerName, listName, groupName, groupRank );
                            game.CpePlayersList[nameId] = info;

                            if( oldInfo != null ) {
                                game.Events.RaiseCpeListInfoChanged( (byte)nameId );
                            } else {
                                game.Events.RaiseCpeListInfoAdded( (byte)nameId );
                            }
                        }
                    } break;

                case PacketId.CpeExtAddEntity:
                    {
                        byte entityId = reader.ReadUInt8();
                        string displayName = reader.ReadAsciiString();
                        string skinName = reader.ReadAsciiString();
                        AddEntity( entityId, displayName, skinName, false );
                    } break;

                case PacketId.CpeExtRemovePlayerName:
                    {
                        short nameId = reader.ReadInt16();
                        if( nameId >= 0 && nameId <= 255 ) {
                            game.Events.RaiseCpeListInfoRemoved( (byte)nameId );
                            game.CpePlayersList[nameId] = null;
                        }
                    } break;

                case PacketId.CpeMakeSelection:
                    {
                        byte selectionId = reader.ReadUInt8();
                        string label = reader.ReadAsciiString();
                        short startX = reader.ReadInt16();
                        short startY = reader.ReadInt16();
                        short startZ = reader.ReadInt16();
                        short endX = reader.ReadInt16();
                        short endY = reader.ReadInt16();
                        short endZ = reader.ReadInt16();

                        byte r = (byte)reader.ReadInt16();
                        byte g = (byte)reader.ReadInt16();
                        byte b = (byte)reader.ReadInt16();
                        byte a = (byte)reader.ReadInt16();

                        Vector3I p1 = Vector3I.Min( startX, startY, startZ, endX, endY, endZ );
                        Vector3I p2 = Vector3I.Max( startX, startY, startZ, endX, endY, endZ );
                        FastColour col = new FastColour( r, g, b, a );
                        game.SelectionManager.AddSelection( selectionId, p1, p2, col );
                    } break;

                case PacketId.CpeRemoveSelection:
                    {
                        byte selectionId = reader.ReadUInt8();
                        game.SelectionManager.RemoveSelection( selectionId );
                    } break;

                case PacketId.CpeEnvColours:
                    {
                        byte variable = reader.ReadUInt8();
                        short red = reader.ReadInt16();
                        short green = reader.ReadInt16();
                        short blue = reader.ReadInt16();
                        bool invalid = red < 0 || red > 255 || green < 0 || green > 255 || blue < 0 || blue > 255;
                        FastColour col = new FastColour( red, green, blue );

                        if( variable == 0 ) { // sky colour
                            game.Map.SetSkyColour( invalid ? Map.DefaultSkyColour : col );
                        } else if( variable == 1 ) { // clouds colour
                            game.Map.SetCloudsColour( invalid ? Map.DefaultCloudsColour : col );
                        } else if( variable == 2 ) { // fog colour
                            game.Map.SetFogColour( invalid ? Map.DefaultFogColour : col );
                        } else if( variable == 3 ) { // ambient light (shadow light)
                            game.Map.SetShadowlight( invalid ? Map.DefaultShadowlight : col );
                        } else if( variable == 4 ) { // diffuse light (sun light)
                            game.Map.SetSunlight( invalid ? Map.DefaultSunlight : col );
                        }
                    } break;

                case PacketId.CpeSetBlockPermission:
                    {
                        byte blockId = reader.ReadUInt8();
                        bool canPlace = reader.ReadUInt8() != 0;
                        bool canDelete = reader.ReadUInt8() != 0;
                        Inventory inv = game.Inventory;

                        if( blockId == 0 ) {
                            for( int i = 1; i < BlockInfo.CpeBlocksCount; i++ ) {
                                inv.CanPlace.SetNotOverridable( canPlace, i );
                                inv.CanDelete.SetNotOverridable( canDelete, i );
                            }
                        } else {
                            inv.CanPlace.SetNotOverridable( canPlace, blockId );
                            inv.CanDelete.SetNotOverridable( canDelete, blockId );
                        }
                        game.Events.RaiseBlockPermissionsChanged();
                    } break;

                case PacketId.CpeChangeModel:
                    {
                        byte playerId = reader.ReadUInt8();
                        string modelName = reader.ReadAsciiString().ToLowerInvariant();
                        Player player = game.Players[playerId];
                        if( player != null ) {
                            player.SetModel( modelName );
                        }
                    } break;

                case PacketId.CpeEnvSetMapApperance:
                    {
                        string url = reader.ReadAsciiString();
                        byte sideBlock = reader.ReadUInt8();
                        byte edgeBlock = reader.ReadUInt8();
                        short waterLevel = reader.ReadInt16();
                        game.Map.SetWaterLevel( waterLevel );
                        game.Map.SetEdgeBlock( (Block)edgeBlock );
                        game.Map.SetSidesBlock( (Block)sideBlock );
                        if( url == String.Empty ) {
                            TexturePackExtractor extractor = new TexturePackExtractor();
                            extractor.Extract( game.defaultTexPack, game );
                        } else {
                            game.Animations.Dispose();
                            if( usingTexturePack )
                                game.AsyncDownloader.DownloadData( url, true, "texturePack" );
                            else
                                game.AsyncDownloader.DownloadImage( url, true, "terrain" );

                        }
                        Utils.LogDebug( "Image url: " + url );
                    } break;

                case PacketId.CpeEnvWeatherType:
                    game.Map.SetWeather( (Weather)reader.ReadUInt8() );
                    break;

                case PacketId.CpeHackControl:
                    {
                        game.LocalPlayer.CanFly = reader.ReadUInt8() != 0;
                        game.LocalPlayer.CanNoclip = reader.ReadUInt8() != 0;
                        game.LocalPlayer.CanSpeed = reader.ReadUInt8() != 0;
                        game.LocalPlayer.CanRespawn = reader.ReadUInt8() != 0;
                        game.CanUseThirdPersonCamera = reader.ReadUInt8() != 0;
                        if( !game.CanUseThirdPersonCamera ) {
                            game.SetCamera( false );
                        }
                        float jumpHeight = reader.ReadInt16() / 32f;
                        if( jumpHeight < 0 ) jumpHeight = 1.4f;
                        game.LocalPlayer.CalculateJumpVelocity( jumpHeight );
                    } break;

                case PacketId.CpeExtAddEntity2:
                    {
                        byte entityId = reader.ReadUInt8();
                        string displayName = reader.ReadAsciiString();
                        string skinName = reader.ReadAsciiString();
                        AddEntity( entityId, displayName, skinName, true );
                    } break;

                case PacketId.CpeDefineBlock:
                case PacketId.CpeDefineLiquid:
                    {
                        byte block = reader.ReadUInt8();
                        BlockInfo info = game.BlockInfo;
                        info.ResetBlockInfo( block );

                        info.Name[block] = reader.ReadAsciiString();
                        info.CollideType[block] = (BlockCollideType)reader.ReadUInt8();
                        // TODO: Liquid collide type not properly supported.
                        info.SpeedMultiplier[block] = (float)Math.Pow( 2, (reader.ReadUInt8() - 128) / 64f );
                        info.SetTop( reader.ReadUInt8(), (Block)block );
                        info.SetSide( reader.ReadUInt8(), (Block)block );
                        info.SetBottom( reader.ReadUInt8(), (Block)block );
                        reader.ReadUInt8(); // opacity hint, but we ignore this.
                        info.BlocksLight[block] = reader.ReadUInt8() == 0;
                        reader.ReadUInt8(); // walk sound, but we ignore this.
                        info.EmitsLight[block] = reader.ReadUInt8() != 0;

                        if( opcode == (byte)PacketId.CpeDefineBlock ) {
                            byte shape = reader.ReadUInt8();
                            if( shape == 1 ) info.Height[block] = 1;
                            else if( shape == 2 ) info.Height[block] = 0.5f;
                            // TODO: upside down slab not properly supported
                            else if( shape == 3 ) info.Height[block] = 0.5f;
                            else if( shape == 4 ) info.IsSprite[block] = true;

                            byte blockDraw = reader.ReadUInt8();
                            if( blockDraw == 0 ) info.IsOpaque[block] = true;
                            else if( blockDraw == 1 ) info.IsTransparent[block] = true;
                            else if( blockDraw == 2 ) info.IsTranslucent[block] = true;
                            else if( blockDraw == 3 ) info.IsTranslucent[block] = true;

                            Console.WriteLine( block + " : " + shape + "," + blockDraw );
                        } else {
                            byte fogDensity = reader.ReadUInt8();
                            info.FogDensity[block] = fogDensity == 0 ? 0 : (fogDensity + 1) / 128f;
                            info.FogColour[block] = new FastColour(
                                reader.ReadUInt8(), reader.ReadUInt8(), reader.ReadUInt8() );
                        }
                        info.SetupCullingCache();
                    } break;

                case PacketId.CpeRemoveBlockDefinition:
                    game.BlockInfo.ResetBlockInfo( reader.ReadUInt8() );
                    break;

                default:
                    throw new NotImplementedException( "Unsupported packet:" + (PacketId)opcode );
            }
        }
Ejemplo n.º 36
0
        private static T FromStream <T>(Stream stream)
            where T : ItemBase <T>
        {
            try
            {
                using (Stream verifyStream = new RangeStream(stream, 0, stream.Length - 4, true))
                {
                    byte[] verifyCrc  = Crc32_Castagnoli.ComputeHash(verifyStream);
                    byte[] orignalCrc = new byte[4];

                    using (RangeStream crcStream = new RangeStream(stream, stream.Length - 4, 4, true))
                    {
                        crcStream.Read(orignalCrc, 0, orignalCrc.Length);
                    }

                    if (!Collection.Equals(verifyCrc, orignalCrc))
                    {
                        throw new ArgumentException("Crc Error");
                    }
                }

                stream.Seek(0, SeekOrigin.Begin);
                byte version = (byte)stream.ReadByte();

                using (Stream dataStream = new RangeStream(stream, stream.Position, stream.Length - stream.Position - 4, true))
                {
                    if (version == (byte)CompressionAlgorithm.None)
                    {
                        return(ItemBase <T> .Import(dataStream, _bufferManager));
                    }
                    else if (version == (byte)CompressionAlgorithm.Deflate)
                    {
                        using (BufferStream deflateBufferStream = new BufferStream(_bufferManager))
                        {
                            byte[] decompressBuffer = null;

                            try
                            {
                                decompressBuffer = _bufferManager.TakeBuffer(1024 * 1024);

                                using (DeflateStream deflateStream = new DeflateStream(dataStream, CompressionMode.Decompress, true))
                                {
                                    int i = -1;

                                    while ((i = deflateStream.Read(decompressBuffer, 0, decompressBuffer.Length)) > 0)
                                    {
                                        deflateBufferStream.Write(decompressBuffer, 0, i);
                                    }
                                }
                            }
                            finally
                            {
                                _bufferManager.ReturnBuffer(decompressBuffer);
                            }

#if DEBUG
                            Debug.WriteLine("RosaConverter FromStream : {0}→{1} {2}",
                                            NetworkConverter.ToSizeString(dataStream.Length),
                                            NetworkConverter.ToSizeString(deflateBufferStream.Length),
                                            NetworkConverter.ToSizeString(dataStream.Length - deflateBufferStream.Length));
#endif

                            deflateBufferStream.Seek(0, SeekOrigin.Begin);

                            return(ItemBase <T> .Import(deflateBufferStream, _bufferManager));
                        }
                    }
                    else
                    {
                        throw new ArgumentException("ArgumentException");
                    }
                }
            }
            catch (Exception e)
            {
                throw new ArgumentException(e.Message, e);
            }
        }
Ejemplo n.º 37
0
        /// <summary>
        /// Deflates the specified bytes.
        /// </summary>
        /// <param name="bytes">The bytes.</param>
        /// <returns>The deflated bytes.</returns>
        private static byte[] Deflate(byte[] bytes)
        {
            //// http://en.wikipedia.org/wiki/DEFLATE
            //// http://www.nuget.org/packages/Microsoft.Bcl.Compression/
#if NET
            var inputStream = new MemoryStream(bytes);
            var deflateStream = new DeflateStream(inputStream, CompressionMode.Decompress);
            var buffer = new byte[4096];
            var outputStream = new MemoryStream();
            while (true)
            {
                var l = deflateStream.Read(buffer, 0, buffer.Length);
                outputStream.Write(buffer, 0, l);

                if (l < buffer.Length)
                {
                    break;
                }
            }

            return outputStream.ToArray();
#elif BCL_COMPRESSION
            // TODO: use Microsoft.Bcl.Compression
#else
            return OxyPlot.Deflate.Decompress(bytes);
#endif
        }
Ejemplo n.º 38
0
        public unsafe void CreateTexture(BinaryReader reader)
        {
            if (Width == 0 || Height == 0)
            {
                return;
            }

            var img       = new Bitmap(Width, Height);
            var MaskImage = new Bitmap(1, 1);

            BitmapData data = img.LockBits(new System.Drawing.Rectangle(0, 0, Width, Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);

            byte[] bytes     = new byte[0];
            byte[] maskbytes = new byte[0];

            MemoryStream output;

            byte Compressed = reader.ReadByte();

            reader.ReadBytes(5);


            var input = new MemoryStream(reader.ReadBytes(Size - 6));

            output = new MemoryStream();
            byte[] buffer = new byte[10];

            DeflateStream decompress = new DeflateStream(input, CompressionMode.Decompress);

            int len;

            //try
            //{
            while ((len = decompress.Read(buffer, 0, buffer.Length)) > 0)
            {
                output.Write(buffer, 0, len);
            }
            //}
            //catch (Exception ex)
            //{
            //    var stop = true;
            //}

            bytes = output.ToArray();
            decompress.Close();
            output.Close();
            input.Close();

            int  index = 0;
            int *scan0 = (int *)data.Scan0;

            {
                for (int y = Height - 1; y >= 0; y--)
                {
                    for (int x = 0; x < Width; x++)
                    {
                        scan0[y * Width + x] = convert16bitTo32bit(bytes[index++] + (bytes[index++] << 8));
                    }

                    if (Width % 4 > 0)
                    {
                        index += WidthBytes(16, Width) - (Width * 2);
                    }
                }
            }

            img.UnlockBits(data);

            Image = img;
        }
Ejemplo n.º 39
0
    public override void FromXML(XmlNode node)
    {
        if (node != null && node.Name == "layer")
        {
            XmlAttributeCollection attrs = node.Attributes;
            m_name = attrs["name"].Value;
            m_layerDimensions.first = Convert.ToInt32(attrs["width"].Value);
            m_layerDimensions.second = Convert.ToInt32(attrs["height"].Value);
            foreach(XmlNode child in node.ChildNodes)
            {
                if (child.Name == "properties")
                {
                    foreach (XmlNode propertyNode in child)
                    {
                        if (propertyNode.Name != "property")
                            continue;
                        XmlAttributeCollection propertyAtts = propertyNode.Attributes;
                        m_properties[propertyAtts["name"].Value] = propertyAtts["value"].Value;
                    }
                }
                else if (child.Name == "data")
                {
                    m_data = new TileData();
                    attrs = child.Attributes;
                    if (attrs["encoding"]!= null)
                    {
                        string[] encodings = { "", "csv", "base64" };
                        string encodingValue = attrs["encoding"].Value;
                        int encodingIdx = Array.IndexOf(encodings, encodingValue);
                        if (encodingIdx >= 0)
                        {
                            m_data.m_encoding = (TileEncodingType)encodingIdx;
                        }

                        string[] compressions = { "", "gzip", "zlib" };
                        string compression = attrs["compression"].Value;
                        int compressionIdx = Array.IndexOf(compressions, compression);
                        if (compressionIdx >= 0)
                        {
                            m_data.m_compression = (TileCompressionType)compressionIdx;
                        }

                        switch(m_data.m_encoding)
                        {
                            case TileEncodingType.kCSV:
                                {
                                    string text = child.InnerText;
                                    string[] values = text.Split(',');
                                    foreach (string v in values)
                                    {
                                        uint value = Convert.ToUInt32(v);
                                        TileInfo info = new TileInfo();
                                        info.m_horizontalFlip = (value & FLIPPED_HORIZONTALLY_FLAG) != 0;
                                        info.m_verticalFlip = (value & FLIPPED_VERTICALLY_FLAG) != 0;
                                        info.m_diagonalFlip = (value & FLIPPED_DIAGONALLY_FLAG) != 0;
                                        value = value & ~(FLIPPED_DIAGONALLY_FLAG | FLIPPED_HORIZONTALLY_FLAG | FLIPPED_VERTICALLY_FLAG);
                                        info.m_gid = value;
                                        m_data.m_tiles.Add(info);
                                    }
                                    break;
                                }
                            case TileEncodingType.kBase64:
                                {
                                    byte[] bytes = null;
                                    switch(m_data.m_compression)
                                    {
                                        case TileCompressionType.kNone:
                                            {
                                                bytes = Convert.FromBase64String(child.InnerText);
                                                break;
                                            }
                                        case TileCompressionType.kGzip:
                                            {
                                                //Transform string into byte[]
                                                string str = child.InnerText;
                                                byte[] byteArray = new byte[str.Length];
                                                int indexBA = 0;
                                                foreach (char item in str.ToCharArray())
                                                {
                                                    byteArray[indexBA++] = (byte)item;
                                                }

                                                MemoryStream ms = new MemoryStream(byteArray);
                                                GZipStream gzip = new GZipStream(ms, CompressionMode.Decompress);

                                                byteArray = new byte[byteArray.Length];
                                                int rBytes = gzip.Read(byteArray, 0, byteArray.Length);

                                                StringBuilder sb = new StringBuilder(rBytes);
                                                for (int i = 0; i < rBytes; ++i)
                                                {
                                                    sb.Append((char)byteArray[i]);
                                                }

                                                gzip.Close();
                                                ms.Close();
                                                gzip.Dispose();
                                                ms.Dispose();

                                                bytes = Convert.FromBase64String(sb.ToString());
                                                break;
                                            }
                                        case TileCompressionType.kZlib:
                                            {
                                                //Transform string into byte[]
                                                string str = child.InnerText;
                                                byte[] byteArray = new byte[str.Length];
                                                int indexBA = 0;
                                                foreach (char item in str.ToCharArray())
                                                {
                                                    byteArray[indexBA++] = (byte)item;
                                                }

                                                MemoryStream ms = new MemoryStream(byteArray);
                                                DeflateStream zlib = new DeflateStream(ms, CompressionMode.Decompress);

                                                byteArray = new byte[byteArray.Length];
                                                int rBytes = zlib.Read(byteArray, 0, byteArray.Length);

                                                StringBuilder sb = new StringBuilder(rBytes);
                                                for (int i = 0; i < rBytes; ++i)
                                                {
                                                    sb.Append((char)byteArray[i]);
                                                }

                                                zlib.Close();
                                                ms.Close();
                                                zlib.Dispose();
                                                ms.Dispose();

                                                bytes = Convert.FromBase64String(sb.ToString());
                                                break;
                                            }
                                    }
                                    for (int i = 0; i < bytes.Length; i += 4)
                                    {
                                        uint value = (uint)bytes[i] | ((uint)bytes[i + 1] << 8) | ((uint)bytes[i + 2] << 16) | ((uint)bytes[i + 3] << 24);
                                        TileInfo info = new TileInfo();
                                        info.m_horizontalFlip = (value & FLIPPED_HORIZONTALLY_FLAG) != 0;
                                        info.m_verticalFlip = (value & FLIPPED_VERTICALLY_FLAG) != 0;
                                        info.m_diagonalFlip = (value & FLIPPED_DIAGONALLY_FLAG) != 0;
                                        value = value & ~(FLIPPED_DIAGONALLY_FLAG | FLIPPED_HORIZONTALLY_FLAG | FLIPPED_VERTICALLY_FLAG);
                                        info.m_gid = value;
                                        m_data.m_tiles.Add(info);
                                    }
                                    break;
                                }
                            default:
                                {
                                    break;
                                }
                        }
                    }
                    else
                    {
                        m_data.m_encoding = TileEncodingType.kNone;
                        m_data.m_compression = TileCompressionType.kNone;

                        m_data.m_tiles.Clear();
                        foreach(XmlNode tileNode in child.ChildNodes)
                        {
                            if (tileNode.Name != "tile")
                            {
                                continue;
                            }
                            TileInfo info = new TileInfo();
                            uint value = Convert.ToUInt32(tileNode.Attributes["gid"].Value);

                            info.m_horizontalFlip = (value & FLIPPED_HORIZONTALLY_FLAG) != 0;
                            info.m_verticalFlip = (value & FLIPPED_VERTICALLY_FLAG) != 0;
                            info.m_diagonalFlip = (value & FLIPPED_DIAGONALLY_FLAG) != 0;
                            value = value & ~(FLIPPED_DIAGONALLY_FLAG | FLIPPED_HORIZONTALLY_FLAG |FLIPPED_VERTICALLY_FLAG);
                            info.m_gid = value;
                            m_data.m_tiles.Add(info);
                        }
                    }
                }
            }
        }
    }
Ejemplo n.º 40
0
        public void Zlib_ParallelDeflateStream()
        {
            var sw = new System.Diagnostics.Stopwatch();
            sw.Start();
            TestContext.WriteLine("{0}: Zlib_ParallelDeflateStream Start", sw.Elapsed);

            int sz = 256*1024 + this.rnd.Next(120000);
            string FileToCompress = System.IO.Path.Combine(TopLevelDir, String.Format("Zlib_ParallelDeflateStream.{0}.txt", sz));

            CreateAndFillFileText( FileToCompress, sz);

            TestContext.WriteLine("{0}: Created file: {1}", sw.Elapsed, FileToCompress );

            byte[] original = File.ReadAllBytes(FileToCompress);

            int crc1 = DoCrc(FileToCompress);

            TestContext.WriteLine("{0}: Original CRC: {1:X8}", sw.Elapsed, crc1 );

            byte[] working = new byte[WORKING_BUFFER_SIZE];
            int n = -1;
            long originalLength;
            MemoryStream ms1 = new MemoryStream();
            {
                using (FileStream fs1 = File.OpenRead(FileToCompress))
                {
                    originalLength = fs1.Length;
                    using (var compressor = new Ionic.Zlib.ParallelDeflateOutputStream(ms1, true))
                    {
                        while ((n = fs1.Read(working, 0, working.Length)) != 0)
                        {
                            compressor.Write(working, 0, n);
                        }
                    }
                }
                ms1.Seek(0, SeekOrigin.Begin);
            }

            TestContext.WriteLine("{0}: Compressed {1} bytes into {2} bytes", sw.Elapsed,
                                  originalLength, ms1.Length);

            var crc = new Ionic.Crc.CRC32();
            int crc2= 0;
            byte[] decompressedBytes= null;
            using (MemoryStream ms2 = new MemoryStream())
            {
                using (var decompressor = new DeflateStream(ms1, CompressionMode.Decompress, false))
                {
                    while ((n = decompressor.Read(working, 0, working.Length)) != 0)
                    {
                        ms2.Write(working, 0, n);
                    }
                }
                TestContext.WriteLine("{0}: Decompressed", sw.Elapsed);
                TestContext.WriteLine("{0}: Decompressed length: {1}", sw.Elapsed, ms2.Length);
                ms2.Seek(0, SeekOrigin.Begin);
                crc2 = crc.GetCrc32(ms2);
                decompressedBytes = ms2.ToArray();
                TestContext.WriteLine("{0}: Decompressed CRC: {1:X8}", sw.Elapsed, crc2 );
            }


            TestContext.WriteLine("{0}: Checking...", sw.Elapsed );

            bool check = true;
            if (originalLength != decompressedBytes.Length)
            {
                TestContext.WriteLine("Different lengths.");
                check = false;
            }
            else
            {
                for (int i = 0; i < decompressedBytes.Length; i++)
                {
                    if (original[i] != decompressedBytes[i])
                    {
                        TestContext.WriteLine("byte {0} differs", i);
                        check = false;
                        break;
                    }
                }
            }

            Assert.IsTrue(check,"Data check failed");
            TestContext.WriteLine("{0}: Done...", sw.Elapsed );
        }
Ejemplo n.º 41
0
 public async Task DecompressFailsWithRealGzStream()
 {
     string[] files = { gzTestFile("GZTestDocument.doc.gz"), gzTestFile("GZTestDocument.txt.gz") };
     foreach (string fileName in files)
     {
         var baseStream = await LocalMemoryStream.readAppFileAsync(fileName);
         var zip = new DeflateStream(baseStream, CompressionMode.Decompress);
         int _bufferSize = 2048;
         var bytes = new byte[_bufferSize];
         Assert.Throws<InvalidDataException>(() => { zip.Read(bytes, 0, _bufferSize); });
         zip.Dispose();
     }
 }
        private ValueTask <ArraySegment <byte> > ReadBytesAsync(int count, ProtocolErrorBehavior protocolErrorBehavior, IOBehavior ioBehavior)
        {
            // satisfy the read from cache if possible
            if (m_remainingData.Count > 0)
            {
                int bytesToRead = Math.Min(m_remainingData.Count, count);
                var result      = new ArraySegment <byte>(m_remainingData.Array, m_remainingData.Offset, bytesToRead);
                m_remainingData = m_remainingData.Slice(bytesToRead);
                return(new ValueTask <ArraySegment <byte> >(result));
            }

            // read the compressed header (seven bytes)
            return(m_bufferedByteReader.ReadBytesAsync(m_byteHandler, 7, ioBehavior)
                   .ContinueWith(headerReadBytes =>
            {
                if (headerReadBytes.Count < 7)
                {
                    return protocolErrorBehavior == ProtocolErrorBehavior.Ignore ?
                    default(ValueTask <ArraySegment <byte> >) :
                    ValueTaskExtensions.FromException <ArraySegment <byte> >(new EndOfStreamException("Wanted to read 7 bytes but only read {0} when reading compressed packet header".FormatInvariant(headerReadBytes.Count)));
                }

                var payloadLength = (int)SerializationUtility.ReadUInt32(headerReadBytes.Array, headerReadBytes.Offset, 3);
                int packetSequenceNumber = headerReadBytes.Array[headerReadBytes.Offset + 3];
                var uncompressedLength = (int)SerializationUtility.ReadUInt32(headerReadBytes.Array, headerReadBytes.Offset + 4, 3);

                // verify the compressed packet sequence number
                var expectedSequenceNumber = GetNextCompressedSequenceNumber() % 256;
                if (packetSequenceNumber != expectedSequenceNumber)
                {
                    if (protocolErrorBehavior == ProtocolErrorBehavior.Ignore)
                    {
                        return default(ValueTask <ArraySegment <byte> >);
                    }

                    var exception = new InvalidOperationException("Packet received out-of-order. Expected {0}; got {1}.".FormatInvariant(expectedSequenceNumber, packetSequenceNumber));
                    return ValueTaskExtensions.FromException <ArraySegment <byte> >(exception);
                }

                // MySQL protocol resets the uncompressed sequence number back to the sequence number of this compressed packet.
                // This isn't in the documentation, but the code explicitly notes that uncompressed packets are modified by compression:
                //  - https://github.com/mysql/mysql-server/blob/c28e258157f39f25e044bb72e8bae1ff00989a3d/sql/net_serv.cc#L276
                //  - https://github.com/mysql/mysql-server/blob/c28e258157f39f25e044bb72e8bae1ff00989a3d/sql/net_serv.cc#L225-L227
                if (!m_isContinuationPacket)
                {
                    m_uncompressedSequenceNumber = packetSequenceNumber;
                }

                // except this doesn't happen when uncompressed packets need to be broken up across multiple compressed packets
                m_isContinuationPacket = payloadLength == ProtocolUtility.MaxPacketSize || uncompressedLength == ProtocolUtility.MaxPacketSize;

                return m_bufferedByteReader.ReadBytesAsync(m_byteHandler, payloadLength, ioBehavior)
                .ContinueWith(payloadReadBytes =>
                {
                    if (payloadReadBytes.Count < payloadLength)
                    {
                        return protocolErrorBehavior == ProtocolErrorBehavior.Ignore ?
                        default(ValueTask <ArraySegment <byte> >) :
                        ValueTaskExtensions.FromException <ArraySegment <byte> >(new EndOfStreamException("Wanted to read {0} bytes but only read {1} when reading compressed payload".FormatInvariant(payloadLength, payloadReadBytes.Count)));
                    }

                    if (uncompressedLength == 0)
                    {
                        // data is uncompressed
                        m_remainingData = payloadReadBytes;
                    }
                    else
                    {
                        // check CMF (Compression Method and Flags) and FLG (Flags) bytes for expected values
                        var cmf = payloadReadBytes.Array[payloadReadBytes.Offset];
                        var flg = payloadReadBytes.Array[payloadReadBytes.Offset + 1];
                        if (cmf != 0x78 || ((flg & 0x40) == 0x40) || ((cmf * 256 + flg) % 31 != 0))
                        {
                            // CMF = 0x78: 32K Window Size + deflate compression
                            // FLG & 0x40: has preset dictionary (not supported)
                            // CMF*256+FLG is a multiple of 31: header checksum
                            return protocolErrorBehavior == ProtocolErrorBehavior.Ignore ?
                            default(ValueTask <ArraySegment <byte> >) :
                            ValueTaskExtensions.FromException <ArraySegment <byte> >(new NotSupportedException("Unsupported zlib header: {0:X2}{1:X2}".FormatInvariant(cmf, flg)));
                        }

                        // zlib format (https://www.ietf.org/rfc/rfc1950.txt) is: [two header bytes] [deflate-compressed data] [four-byte checksum]
                        // .NET implements the middle part with DeflateStream; need to handle header and checksum explicitly
                        const int headerSize = 2;
                        const int checksumSize = 4;
                        var uncompressedData = new byte[uncompressedLength];
                        using (var compressedStream = new MemoryStream(payloadReadBytes.Array, payloadReadBytes.Offset + headerSize, payloadReadBytes.Count - headerSize - checksumSize))
                            using (var decompressingStream = new DeflateStream(compressedStream, CompressionMode.Decompress))
                            {
                                var bytesRead = decompressingStream.Read(uncompressedData, 0, uncompressedLength);
                                m_remainingData = new ArraySegment <byte>(uncompressedData, 0, bytesRead);

                                var checksum = ComputeAdler32Checksum(uncompressedData, 0, bytesRead);
                                int adlerStartOffset = payloadReadBytes.Offset + payloadReadBytes.Count - 4;
                                if (payloadReadBytes.Array[adlerStartOffset + 0] != ((checksum >> 24) & 0xFF) ||
                                    payloadReadBytes.Array[adlerStartOffset + 1] != ((checksum >> 16) & 0xFF) ||
                                    payloadReadBytes.Array[adlerStartOffset + 2] != ((checksum >> 8) & 0xFF) ||
                                    payloadReadBytes.Array[adlerStartOffset + 3] != (checksum & 0xFF))
                                {
                                    return protocolErrorBehavior == ProtocolErrorBehavior.Ignore ?
                                    default(ValueTask <ArraySegment <byte> >) :
                                    ValueTaskExtensions.FromException <ArraySegment <byte> >(new NotSupportedException("Invalid Adler-32 checksum of uncompressed data."));
                                }
                            }
                    }

                    var result = m_remainingData.Slice(0, count);
                    m_remainingData = m_remainingData.Slice(count);
                    return new ValueTask <ArraySegment <byte> >(result);
                });
            }));
        }
Ejemplo n.º 43
0
        public void ReadWriteArgumentValidation()
        {
            using (var ds = new DeflateStream(new MemoryStream(), CompressionMode.Compress))
            {
                Assert.Throws<ArgumentNullException>(() => ds.Write(null, 0, 0));
                Assert.Throws<ArgumentOutOfRangeException>(() => ds.Write(new byte[1], -1, 0));
                Assert.Throws<ArgumentOutOfRangeException>(() => ds.Write(new byte[1], 0, -1));
                Assert.Throws<ArgumentException>(() => ds.Write(new byte[1], 0, 2));
                Assert.Throws<ArgumentException>(() => ds.Write(new byte[1], 1, 1));
                Assert.Throws<InvalidOperationException>(() => ds.Read(new byte[1], 0, 1));
                ds.Write(new byte[1], 0, 0);
            }
            using (var ds = new DeflateStream(new MemoryStream(), CompressionMode.Compress))
            {
                Assert.Throws<ArgumentNullException>(() => { ds.WriteAsync(null, 0, 0); });
                Assert.Throws<ArgumentOutOfRangeException>(() => { ds.WriteAsync(new byte[1], -1, 0); });
                Assert.Throws<ArgumentOutOfRangeException>(() => { ds.WriteAsync(new byte[1], 0, -1); });
                Assert.Throws<ArgumentException>(() => { ds.WriteAsync(new byte[1], 0, 2); });
                Assert.Throws<ArgumentException>(() => { ds.WriteAsync(new byte[1], 1, 1); });
                Assert.Throws<InvalidOperationException>(() => { ds.Read(new byte[1], 0, 1); });
            }

            using (var ds = new DeflateStream(new MemoryStream(), CompressionMode.Decompress))
            {
                Assert.Throws<ArgumentNullException>(() => ds.Read(null, 0, 0));
                Assert.Throws<ArgumentOutOfRangeException>(() => ds.Read(new byte[1], -1, 0));
                Assert.Throws<ArgumentOutOfRangeException>(() => ds.Read(new byte[1], 0, -1));
                Assert.Throws<ArgumentException>(() => ds.Read(new byte[1], 0, 2));
                Assert.Throws<ArgumentException>(() => ds.Read(new byte[1], 1, 1));
                Assert.Throws<InvalidOperationException>(() => ds.Write(new byte[1], 0, 1));

                var data = new byte[1] { 42 };
                Assert.Equal(0, ds.Read(data, 0, 0));
                Assert.Equal(42, data[0]);
            }
            using (var ds = new DeflateStream(new MemoryStream(), CompressionMode.Decompress))
            {
                Assert.Throws<ArgumentNullException>(() => { ds.ReadAsync(null, 0, 0); });
                Assert.Throws<ArgumentOutOfRangeException>(() => { ds.ReadAsync(new byte[1], -1, 0); });
                Assert.Throws<ArgumentOutOfRangeException>(() => { ds.ReadAsync(new byte[1], 0, -1); });
                Assert.Throws<ArgumentException>(() => { ds.ReadAsync(new byte[1], 0, 2); });
                Assert.Throws<ArgumentException>(() => { ds.ReadAsync(new byte[1], 1, 1); });
                Assert.Throws<InvalidOperationException>(() => { ds.Write(new byte[1], 0, 1); });
            }
        }
        internal static byte[] smethod_4(long long_0, Stream stream_0)
        {
            Stream       stream       = stream_0;
            MemoryStream memoryStream = null;
            ushort       num          = (ushort)stream_0.ReadByte();

            num = ~num;
            for (int i = 1; i < 3; i++)
            {
                stream_0.ReadByte();
            }
            if ((num & 2) != 0)
            {
                DESCryptoServiceProvider dESCryptoServiceProvider = new DESCryptoServiceProvider();
                byte[] array = new byte[8];
                stream_0.Read(array, 0, 8);
                dESCryptoServiceProvider.IV = array;
                byte[] array2 = new byte[8];
                stream_0.Read(array2, 0, 8);
                bool   flag   = true;
                byte[] array3 = array2;
                for (int j = 0; j < array3.Length; j++)
                {
                    if (array3[j] != 0)
                    {
                        flag = false;
IL_8B:
                        if (flag)
                        {
                            array2 = Class23.smethod_1(Assembly.GetExecutingAssembly());
                        }
                        dESCryptoServiceProvider.Key = array2;
                        if (Class23.memoryStream_0 == null)
                        {
                            if (Class23.int_0 == 2147483647)
                            {
                                Class23.memoryStream_0.Capacity = (int)stream_0.Length;
                            }
                            else
                            {
                                Class23.memoryStream_0.Capacity = Class23.int_0;
                            }
                        }
                        Class23.memoryStream_0.Position = 0L;
                        ICryptoTransform cryptoTransform = dESCryptoServiceProvider.CreateDecryptor();
                        int    inputBlockSize            = cryptoTransform.InputBlockSize;
                        int    arg_105_0 = cryptoTransform.OutputBlockSize;
                        byte[] array4    = new byte[cryptoTransform.OutputBlockSize];
                        byte[] array5    = new byte[cryptoTransform.InputBlockSize];
                        int    num2      = (int)stream_0.Position;
                        while ((long)(num2 + inputBlockSize) < stream_0.Length)
                        {
                            stream_0.Read(array5, 0, inputBlockSize);
                            int count = cryptoTransform.TransformBlock(array5, 0, inputBlockSize, array4, 0);
                            Class23.memoryStream_0.Write(array4, 0, count);
                            num2 += inputBlockSize;
                        }
                        stream_0.Read(array5, 0, (int)(stream_0.Length - (long)num2));
                        byte[] array6 = cryptoTransform.TransformFinalBlock(array5, 0, (int)(stream_0.Length - (long)num2));
                        Class23.memoryStream_0.Write(array6, 0, array6.Length);
                        stream          = Class23.memoryStream_0;
                        stream.Position = 0L;
                        memoryStream    = Class23.memoryStream_0;
                        goto IL_1C6;
                    }
                }
                goto IL_8B;
            }
IL_1C6:
            if ((num & 8) != 0)
            {
                try
                {
                    if (Class23.memoryStream_1 == null)
                    {
                        if (Class23.int_1 == -2147483648)
                        {
                            Class23.memoryStream_1.Capacity = (int)stream.Length * 2;
                        }
                        else
                        {
                            Class23.memoryStream_1.Capacity = Class23.int_1;
                        }
                    }
                    Class23.memoryStream_1.Position = 0L;
                    DeflateStream deflateStream = new DeflateStream(stream, CompressionMode.Decompress);
                    int           num3          = 1000;
                    byte[]        buffer        = new byte[1000];
                    int           num4;
                    do
                    {
                        num4 = deflateStream.Read(buffer, 0, num3);
                        if (num4 > 0)
                        {
                            Class23.memoryStream_1.Write(buffer, 0, num4);
                        }
                    }while (num4 >= num3);
                    memoryStream = Class23.memoryStream_1;
                }
                catch (Exception)
                {
                }
            }
            if (memoryStream != null)
            {
                return(memoryStream.ToArray());
            }
            byte[] array7 = new byte[stream_0.Length - stream_0.Position];
            stream_0.Read(array7, 0, array7.Length);
            return(array7);
        }
Ejemplo n.º 45
0
        public async Task WrapStreamReturningBadReadValues()
        {
            using (var ds = new DeflateStream(new BadWrappedStream(BadWrappedStream.Mode.ReturnTooLargeCounts), CompressionMode.Decompress))
                Assert.Throws<InvalidDataException>(() => ds.Read(new byte[1024], 0, 1024));
            using (var ds = new DeflateStream(new BadWrappedStream(BadWrappedStream.Mode.ReturnTooLargeCounts), CompressionMode.Decompress))
                await Assert.ThrowsAsync<InvalidDataException>(() => ds.ReadAsync(new byte[1024], 0, 1024));

            using (var ds = new DeflateStream(new BadWrappedStream(BadWrappedStream.Mode.ReturnTooSmallCounts), CompressionMode.Decompress))
                Assert.Equal(0, ds.Read(new byte[1024], 0, 1024));
            using (var ds = new DeflateStream(new BadWrappedStream(BadWrappedStream.Mode.ReturnTooSmallCounts), CompressionMode.Decompress))
                Assert.Equal(0, await ds.ReadAsync(new byte[1024], 0, 1024));
        }
Ejemplo n.º 46
0
        /// <summary>
        /// HttpPost请求(同步,短连接)
        /// </summary>
        /// <param name="strUrl">Url</param>
        /// <param name="strPostData">Post数据</param>
        /// <param name="strContentType">ContentType(默认:application/x-www-form-urlencoded)</param>
        /// <param name="strUserAgent">UserAgent(默认:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36)</param>
        /// <param name="strAccept">Accept(默认:application/json,text/javascript,*/*;q=0.01)</param>
        /// <param name="cookieCollection">CookieCollection</param>
        /// <param name="intTimeOut">TimeOut(默认:30秒)</param>
        /// <param name="strEncoding">Encoding(默认:utf-8)</param>
        /// <returns></returns>
        public static string HttpPost(string strUrl, string strPostData, string strContentType, string strUserAgent, string strAccept, CookieCollection cookieCollection, int intTimeOut, string strEncoding)
        {
            //响应字符串
            string strResult = string.Empty;

            if (string.IsNullOrEmpty(strUrl))
            {
                return(strResult);
            }
            if (string.IsNullOrEmpty(strEncoding))
            {
                //默认响应编码为 utf-8
                strEncoding = "utf-8";
            }

            HttpWebRequest request = null;

            try
            {
                if (strUrl.StartsWith("https", StringComparison.OrdinalIgnoreCase))
                {
                    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                    request = WebRequest.Create(strUrl) as HttpWebRequest;
                    request.ProtocolVersion = HttpVersion.Version10;
                }
                else
                {
                    request = (HttpWebRequest)HttpWebRequest.Create(strUrl);
                }
            }
            catch (Exception ex)
            {
                Log4NetUtil.Error(typeof(HttpUtil), ex.ToString());
                return(strResult);
            }

            if (request != null)
            {
                //POST请求
                request.Method    = "POST";
                request.KeepAlive = false;
                //request.ServicePoint.Expect100Continue = false;

                if (string.IsNullOrEmpty(strContentType))
                {
                    request.ContentType = @"application/x-www-form-urlencoded";
                }
                else
                {
                    request.ContentType = strContentType;
                }

                if (string.IsNullOrEmpty(strUserAgent))
                {
                    request.UserAgent = @"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36";
                }
                else
                {
                    request.UserAgent = strUserAgent;
                }

                if (string.IsNullOrEmpty(strAccept))
                {
                    //Requester.Accept = @"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                    request.Accept = @"application/json,text/javascript,*/*;q=0.01";
                }
                else
                {
                    request.Accept = strAccept;
                }

                if (cookieCollection != null)
                {
                    request.CookieContainer = new CookieContainer();
                    request.CookieContainer.Add(cookieCollection);
                }

                //超时时间,单位:毫秒(默认30秒)
                if (intTimeOut < 30)
                {
                    request.Timeout          = 30 * 1000;
                    request.ReadWriteTimeout = 30 * 1000;
                }
                else
                {
                    request.Timeout          = intTimeOut * 1000;
                    request.ReadWriteTimeout = intTimeOut * 1000;
                }

                //AcceptEncoding
                request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");

                //代理方式
                //WebProxy proxy = new WebProxy("127.0.0.1", 8000);//指定的ip和端口
                //request.Proxy = proxy;
                //request.AllowAutoRedirect = true;

                //Post数据
                if (!string.IsNullOrEmpty(strPostData))
                {
                    //参数编码
                    //byte[] data = Encoding.UTF8.GetBytes(buffer.ToString());
                    byte[] data = Encoding.GetEncoding(strEncoding).GetBytes(strPostData);

                    using (Stream writeStream = request.GetRequestStream())
                    {
                        if (writeStream != null)
                        {
                            writeStream.Write(data, 0, data.Length);
                        }
                    }
                }

                //取得响应数据
                HttpWebResponse response = null;
                try
                {
                    response = (HttpWebResponse)request.GetResponse();
                }
                catch (Exception ex)
                {
                    Log4NetUtil.Error(typeof(HttpUtil), ex.ToString());
                    request.Abort();
                    return(strResult);
                }

                if (response != null)
                {
                    byte[] buffer = new byte[4096];
                    int    size   = 0;
                    try
                    {
                        if (!string.IsNullOrEmpty(response.ContentEncoding))
                        {
                            if (response.ContentEncoding.ToLower().Contains("gzip"))
                            {
                                using (Stream rpsStream = response.GetResponseStream())
                                {
                                    if (rpsStream != null)
                                    {
                                        using (GZipStream stream = new GZipStream(rpsStream, CompressionMode.Decompress))
                                        {
                                            using (MemoryStream memoryStream = new MemoryStream())
                                            {
                                                while ((size = stream.Read(buffer, 0, buffer.Length)) > 0)
                                                {
                                                    memoryStream.Write(buffer, 0, size);
                                                }
                                                strResult = Encoding.GetEncoding(strEncoding).GetString(memoryStream.ToArray());
                                            }
                                        }
                                    }
                                }
                            }
                            else if (response.ContentEncoding.ToLower().Contains("deflate"))
                            {
                                using (Stream rpsStream = response.GetResponseStream())
                                {
                                    if (rpsStream != null)
                                    {
                                        using (DeflateStream stream = new DeflateStream(rpsStream, CompressionMode.Decompress))
                                        {
                                            using (MemoryStream memoryStream = new MemoryStream())
                                            {
                                                while ((size = stream.Read(buffer, 0, buffer.Length)) > 0)
                                                {
                                                    memoryStream.Write(buffer, 0, size);
                                                }
                                                strResult = Encoding.GetEncoding(strEncoding).GetString(memoryStream.ToArray());
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                using (Stream rpsStream = response.GetResponseStream())
                                {
                                    if (rpsStream != null)
                                    {
                                        using (MemoryStream memoryStream = new MemoryStream())
                                        {
                                            while ((size = rpsStream.Read(buffer, 0, buffer.Length)) > 0)
                                            {
                                                memoryStream.Write(buffer, 0, size);
                                            }
                                            strResult = Encoding.GetEncoding(strEncoding).GetString(memoryStream.ToArray());
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            using (Stream rpsStream = response.GetResponseStream())
                            {
                                if (rpsStream != null)
                                {
                                    using (MemoryStream memoryStream = new MemoryStream())
                                    {
                                        while ((size = rpsStream.Read(buffer, 0, buffer.Length)) > 0)
                                        {
                                            memoryStream.Write(buffer, 0, size);
                                        }
                                        strResult = Encoding.GetEncoding(strEncoding).GetString(memoryStream.ToArray());
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Log4NetUtil.Error(typeof(HttpUtil), ex.ToString());
                        request.Abort();
                        return(string.Empty);
                    }
                    finally
                    {
                        if (response != null)
                        {
                            response.Close();
                        }
                        if (request != null)
                        {
                            request.Abort();
                        }
                    }
                }
            }
            return(strResult);
        }