Exemplo n.º 1
0
        public static byte[] BCGZipDecompress(BufLen buf)
        {
            var reader = new BufRefLen(buf);

            using (var ms = new MemoryStream())
            {
                // Skip gzip header
                var gzheader = reader.ReadBufLen(10);
                var flag     = gzheader.Peek8(3);
                if ((flag & 0x04) != 0)
                {
                    reader.Seek(reader.Read16());                           // "Extra"
                }
                if ((flag & 0x08) != 0)
                {
                    while (reader.Read8() != 0)
                    {
                        ;                                                   // "Name"
                    }
                }
                if ((flag & 0x10) != 0)
                {
                    while (reader.Read8() != 0)
                    {
                        ;                                                   // "Comment"
                    }
                }
                if ((flag & 0x02) != 0)
                {
                    reader.Read16();                         // "CRC16"
                }
                ms.Write(reader.BaseArray, reader.BaseArrayOffset, reader.Length);
                ms.Position = 0;

                using (var gzs = new ZInputStream(ms, true))
                {
                    var gzdata = StreamUtils.Read(gzs);
                    return(gzdata);
                }
            }
        }
Exemplo n.º 2
0
        public static string Encode(BufLen data)
        {
            var result = new StringBuilder();
            var reader = new BufRefLen(data);

            byte v1, v2;

            for (int i = 0; i < data.Length / 3; ++i)
            {
                v1   = reader.Read8();
                v2   = (byte)((v1 << 4) & 0x30);
                v1 >>= 2;
                result.Append(Domain[v1]);
                v1  = reader.Read8();
                v2 |= (byte)(v1 >> 4);
                result.Append(Domain[v2]);
                v1  = (byte)((v1 & 0x0f) << 2);
                v2  = reader.Read8();
                v1 |= (byte)(v2 >> 6);
                result.Append(Domain[v1]);
                v2 &= 0x3f;
                result.Append(Domain[v2]);
            }

            switch (data.Length % 3)
            {
            case 1:
                v1   = reader.Read8();
                v2   = (byte)((v1 << 4) & 0x3f);
                v1 >>= 2;
                result.Append(Domain[v1]);
                result.Append(Domain[v2]);
                result.Append("==");
                break;

            case 2:
                v1   = reader.Read8();
                v2   = (byte)((v1 << 4) & 0x3f);
                v1 >>= 2;
                result.Append(Domain[v1]);
                v1  = reader.Read8();
                v2 |= (byte)(v1 >> 4);
                result.Append(Domain[v2]);
                v1 = (byte)((v1 & 0x0f) << 2);
                result.Append(Domain[v1]);
                result.Append("=");
                break;
            }

            return(result.ToString());
        }
Exemplo n.º 3
0
        public static BufLen BCGZipDecompressNew(BufLen buf)
        {
            var reader = new BufRefLen(buf);

            // Skip gzip header
            var gzheader = reader.ReadBufLen(10);
            var flag     = gzheader.Peek8(3);

            if ((flag & 0x04) != 0)
            {
                reader.Seek(reader.Read16());                           // "Extra"
            }
            if ((flag & 0x08) != 0)
            {
                while (reader.Read8() != 0)
                {
                    ;                                                   // "Name"
                }
            }
            if ((flag & 0x10) != 0)
            {
                while (reader.Read8() != 0)
                {
                    ;                                                   // "Comment"
                }
            }
            if ((flag & 0x02) != 0)
            {
                reader.Read16();                         // "CRC16"
            }
            var z = new ZStream();
            z.inflateInit(true);

            var dest   = new byte[buf.Length * 2];
            var destix = 0;

            z.next_in_index = reader.BaseArrayOffset;
            z.next_in       = reader.BaseArray;
            z.avail_in      = reader.Length - 8;

bigger_dest:

            z.next_out       = dest;
            z.next_out_index = destix;
            z.avail_out      = dest.Length - destix;
            var err = z.inflate(JZlib.Z_FINISH);
            if (err != JZlib.Z_BUF_ERROR && err != JZlib.Z_OK && err != JZlib.Z_STREAM_END)
            {
                throw new IOException("inflating: " + z.msg);
            }

            if (z.avail_out == 0)
            {
                var newdest = new byte[dest.Length * 2];
                Array.Copy(dest, newdest, dest.Length);
                destix = dest.Length;
                dest   = newdest;
                goto bigger_dest;
            }

            var result = new BufLen(dest, 0, dest.Length - z.avail_out);
            z.inflateEnd();

            return(result);
        }