Exemple #1
0
        public PNGAnalyzer(byte[] data)
        {
            pngData      = data;
            index        = 0;
            foundIEND    = false;
            memoryStream = new MemoryStream();

            // zlib initialization
            zBuf    = new byte[8192];
            zStream = new zlib.ZStream();
            zStream.inflateInit();
            zStream.next_out  = zBuf;
            zStream.avail_out = zBuf.Length;
        }
Exemple #2
0
 protected ZStream(RubyClass/*!*/ cls, zlib.ZStream/*!*/ stream)
     : base(cls)
 {
     Debug.Assert(stream != null);
     _stream = stream;
 }
Exemple #3
0
            private static zlib.ZStream CreateInflateStream(int windowBits)
            {
                var zst = new zlib.ZStream();
                int result = zst.inflateInit(windowBits);
                if (result != Z_OK) {
                    throw MakeError(result, zst.msg);
                }

                return zst;
            }
Exemple #4
0
            private static zlib.ZStream CreateDeflateStream(int level, int windowBits, int memLevel, int strategy)
            {
                var stream = new zlib.ZStream();
                int result = stream.deflateInit(level, windowBits, memLevel, (zlib.CompressionStrategy)strategy);
                if (result != Z_OK) {
                    throw MakeError(result, null);
                }

                return stream;
            }
        public static byte[] ZLibBufferToString(this byte[] buf, int len)
        {
            //using (var ms = new MemoryStream(buf, 0, len))
            //using (var ds = new GZipStream(ms, CompressionMode.Decompress))
            //{
            //    byte[] decompressed = new byte[32 * 1024 + 10];
            //    int readlen = 0;
            //    int readSum = 0;
            //    do
            //    {
            //        readlen = ds.Read(decompressed, readlen, decompressed.Length - readSum);
            //        readSum += readlen;
            //    } while (readlen != 0);
            //    return decompressed.Take(readSum).ToArray();
            //}

            var strm = new zlib.ZStream();

            strm.next_in  = buf;
            strm.avail_in = len;
            if (strm.inflateInit() != zlib.zlibConst.Z_OK)
            {
                return(null);
            }
            bool done = false;

            byte[] decompressed = new byte[32 * 1024];
            int    status       = 0;

            while (!done)
            {
                // Make sure we have enough room and reset the lengths.
                strm.next_out  = decompressed;
                strm.avail_out = decompressed.Length;

                // Inflate another chunk.
                status = strm.inflate(zlib.zlibConst.Z_SYNC_FLUSH);
                if (status == zlib.zlibConst.Z_STREAM_END)
                {
                    done = true;
                }
                else if (status != zlib.zlibConst.Z_OK)
                {
                    break;
                }
            }
            if (strm.inflateEnd() != zlib.zlibConst.Z_OK)
            {
                return(null);
            }

            // Set real length.
            if (done)
            {
                //using (var ms = new MemoryStream(decompressed, 0, (int)strm.total_out))
                //{
                //    using (StreamReader reader = new StreamReader(ms, Encoding.Unicode))
                //    {
                //        var res = reader.ReadToEnd();
                //        return res;
                //    }
                //}
                if (decompressed.Length == (int)strm.total_out)
                {
                    return(decompressed);
                }
                return(decompressed.Take((int)strm.total_out).ToArray());
            }
            else
            {
                return(null);
            }
        }
Exemple #6
0
        public static byte[] ReadZPacket(this RawData data, int compressedLength, int uncompressedLength)
        {
            byte[] uncompressedData = new byte[uncompressedLength];

            var zs = new zlib.ZStream
            {
                next_in_index = data.CurrentPosition,
                next_in = data.Data,
                avail_in = 0,

                next_out = uncompressedData,
                avail_out = uncompressedLength
            };

            var res = zs.inflateInit(-15);
            if (res != zlib.zlibConst.Z_OK)
                throw new Exception("Inflate init failed: " + res);

            zs.avail_in = compressedLength;
            res = zs.inflate(zlib.zlibConst.Z_FINISH);
            if (res != zlib.zlibConst.Z_STREAM_END)
                throw new Exception("Inflate failed: " + res);

            res = zs.inflateEnd();
            if (res != zlib.zlibConst.Z_OK)
                throw new Exception("Inflate end failed: " + res);

            data.CurrentPosition += compressedLength;
            return uncompressedData;
        }