Beispiel #1
0
        public static int gz_fetch(gz_state *state)
        {
            z_stream_s strm = &state->strm;

            do
            {
                switch (state->how)
                {
                case 0: if (gz_look(state) == (-1))
                    {
                        return(-1);
                    }
                    if (state->how == 0)
                    {
                        return(0);
                    }
                    break;

                case 1: if (gz_load(state, state->_out_, state->size << 1, &state->x.have) == (-1))
                    {
                        return(-1);
                    }
                    state->x.next = state->_out_; return(0);

                case 2: strm.avail_out = state->size << 1;
                    strm.next_out      = state->_out_; if (gz_decomp(state) == (-1))
                    {
                        return(-1);
                    }
                }
            }while ((state->x.have == 0) && ((state->eof == 0) || (strm.avail_in != 0)));
            return(0);
        }
Beispiel #2
0
        public static int gz_avail(gz_state *state)
        {
            uint       got  = 0;
            z_stream_s strm = &state->strm;

            if ((state->err != 0) && (state->err != (-5)))
            {
                return(-1);
            }
            if (state->eof == 0)
            {
                if (strm.avail_in != 0)
                {
                    byte *p = state->_in_; byte *q = strm.next_in; uint n = strm.avail_in;
                    do
                    {
                        *p++ = *q++;
                    }while ((--n) != 0);
                }
                if (gz_load(state, state->_in_ + strm.avail_in, state->size - strm.avail_in, &got) == (-1))
                {
                    return(-1);
                }
                strm.avail_in += got;
                strm.next_in   = state->_in_;
            }

            return(0);
        }
Beispiel #3
0
        public static int gz_init(gz_state *state)
        {
            int        ret  = 0;
            z_stream_s strm = &state->strm;

            state->_in_ = (byte *)malloc((ulong)(state->want << 1));
            if (state->_in_ == ((void *)0))
            {
                gz_error(state, -4, "out of memory"); return(-1);
            }

            if (state->direct == 0)
            {
                state->_out_ = (byte *)malloc((ulong)state->want); if (state->_out_ == ((void *)0))
                {
                    free(state->_in_); gz_error(state, -4, "out of memory"); return(-1);
                }
                strm.zalloc = null; strm.zfree = null; strm.opaque = null; ret = (int)deflateInit2_(strm, state->level, 8, 15 + 16, 8, state->strategy, "1.2.11", sizeof(z_stream_s)); if (ret != 0)
                {
                    free(state->_out_); free(state->_in_); gz_error(state, -4, "out of memory"); return(-1);
                }
                strm.next_in = (void *)0;
            }

            state->size = state->want;
            if (state->direct == 0)
            {
                strm.avail_out = state->size;
                strm.next_out  = state->_out_; state->x.next = strm.next_out;
            }

            return(0);
        }
Beispiel #4
0
        public static int gz_zero(gz_state *state, long len)
        {
            int        first = 0;
            uint       n     = 0;
            z_stream_s strm  = &state->strm;

            if ((strm.avail_in != 0) && (gz_comp(state, 0) == (-1)))
            {
                return(-1);
            }
            first = 1;
            while (len != 0)
            {
                n = (uint)(((==) && (state->size > 2147483647)) || (((long)state->size) > len)?(uint)len:state->size); if (first != 0)
                {
                    memset(state->_in_, (int)0, (ulong)n); first = (int)0;
                }
                strm.avail_in = (uint)n; strm.next_in = state->_in_; state->x.pos += (long)n; if (gz_comp(state, (int)0) == (-1))
                {
                    return((int)-1);
                }
                len -= (long)n;
            }
            return(0);
        }
        public static int compress2(byte[] dest, ref int destLen, byte[] source, int sourceLen, int level)
        {
            z_stream_s stream = new z_stream_s();
            int        err    = 0;
            int        max    = int.MaxValue;
            int        left   = destLen;

            err = (int)Deflate.deflateInit_(stream, level, "1.2.11");
            if (err != 0)
            {
                return(err);
            }
            stream.next_out  = dest;
            stream.avail_out = 0;
            stream.next_in   = source;
            stream.avail_in  = 0;
            do
            {
                if (stream.avail_out == 0)
                {
                    stream.avail_out = left > ((int)max) ? max : left;
                    left            -= (int)stream.avail_out;
                }
                if (stream.avail_in == 0)
                {
                    stream.avail_in = sourceLen > ((int)max) ? max : sourceLen;
                    sourceLen      -= (int)stream.avail_in;
                }
                err = (int)Deflate.deflate(stream, sourceLen != 0 ? 0 : 4);
            }while (err == 0);
            destLen = stream.total_out;
            Deflate.deflateEnd(stream);
            return(err == 1 ? 0 : err);
        }
        public static int uncompress2(byte *dest, int *destLen, byte *source, int *sourceLen)
        {
            z_stream_s stream = new z_stream_s();
            int        err = 0;
            uint       max = (uint)-1;
            int        len = 0; int left = 0;
            byte *     buf = stackalloc byte[1];

            len = *sourceLen;
            if ((*destLen) != 0)
            {
                left = *destLen;
                *destLen = 0;
            }
            else
            {
                left = 1;
                dest = buf;
            }

            stream.next_in  = source;
            stream.avail_in = 0;
            stream.zalloc   = (zalloc_delegate)0;
            stream.zfree    = (zfree_delegate)0;
            stream.opaque   = (void *)0;
            err             = (int)inflateInit_(&stream, "1.2.11", sizeof(z_stream_s));
            if (err != 0)
            {
                return(err);
            }
            stream.next_out  = dest;
            stream.avail_out = 0;
            do
            {
                if (stream.avail_out == 0)
                {
                    stream.avail_out = left > ((int)max) ? max : (uint)left;
                    left            -= (int)stream.avail_out;
                }
                if (stream.avail_in == 0)
                {
                    stream.avail_in = len > ((int)max) ? max : (uint)len;
                    len            -= (int)stream.avail_in;
                }
                err = (int)inflate(&stream, 0);
            }while (err == 0);
            *sourceLen -= (int)(len + stream.avail_in);
            if (dest != buf)
            {
                *destLen = stream.total_out;
            }
            else if ((stream.total_out != 0) && (err == (-5)))
            {
                left = 1;
            }
            inflateEnd(&stream);
            return(err == 1 ? 0 : err == 2 ? (-3) : (err == (-5)) && (left + stream.avail_out) ? (-3) : err);
        }
Beispiel #7
0
        public static int gz_look(gz_state *state)
        {
            z_stream_s strm = &state->strm;

            if (state->size == 0)
            {
                state->_in_ = (byte *)malloc((ulong)state->want); state->_out_ = (byte *)malloc((ulong)(state->want << 1)); if ((state->_in_ == ((void *)0)) || (state->_out_ == ((void *)0)))
                {
                    free(state->_out_); free(state->_in_); gz_error(state, -4, "out of memory"); return(-1);
                }
                state->size         = state->want;
                state->strm.zalloc  = null; state->strm.zfree = null; state->strm.opaque = null; state->strm.avail_in = 0;
                state->strm.next_in = null; if (inflateInit2_(&state->strm, 15 + 16, "1.2.11", sizeof(z_stream_s)) != 0)
                {
                    free(state->_out_); free(state->_in_); state->size = 0;
                    gz_error(state, -4, "out of memory"); return(-1);
                }
            }

            if (strm.avail_in < 2)
            {
                if (gz_avail(state) == (-1))
                {
                    return(-1);
                }
                if (strm.avail_in == 0)
                {
                    return(0);
                }
            }

            if ((strm.avail_in > 1) && (strm.next_in[0] == 31) && (strm.next_in[1] == 139))
            {
                inflateReset(strm); state->how = 2;
                state->direct = 0;
                return(0);
            }

            if (state->direct == 0)
            {
                strm.avail_in = 0;
                state->eof    = 1;
                state->x.have = 0;
                return(0);
            }

            state->x.next = state->_out_;
            if (strm.avail_in != 0)
            {
                memcpy(state->x.next, strm.next_in, (ulong)strm.avail_in); state->x.have = strm.avail_in;
                strm.avail_in = 0;
            }

            state->how    = 1;
            state->direct = 1;
            return(0);
        }
Beispiel #8
0
        public static int gz_comp(gz_state *state, int flush)
        {
            int        ret = 0; int writ = 0;
            uint       have = 0; uint put = 0; uint max = (uint)(((uint)-1 >> 2) + 1);
            z_stream_s strm = &state->strm;

            if ((state->size == 0) && (gz_init(state) == (-1)))
            {
                return(-1);
            }
            if (state->direct != 0)
            {
                while (strm.avail_in != 0)
                {
                    put  = strm.avail_in > max ? max : strm.avail_in;
                    writ = (int)write(state->fd, strm.next_in, put); if (writ < 0)
                    {
                        gz_error(state, -1, strerror((int)*_errno())); return(-1);
                    }
                    strm.avail_in -= (uint)writ; strm.next_in += writ;
                }
                return(0);
            }

            ret = 0;
            do
            {
                if ((strm.avail_out == 0) || ((flush != 0) && ((flush != 4) || (ret == 1))))
                {
                    while (strm.next_out > state->x.next)
                    {
                        put  = (strm.next_out - state->x.next) > ((int)max) ? max : (uint)(strm.next_out - state->x.next);
                        writ = (int)write(state->fd, state->x.next, put); if (writ < 0)
                        {
                            gz_error(state, -1, strerror((int)*_errno())); return(-1);
                        }
                        state->x.next += writ;
                    }
                    if (strm.avail_out == 0)
                    {
                        strm.avail_out = state->size;
                        strm.next_out  = state->_out_; state->x.next = state->_out_;
                    }
                }
                have = strm.avail_out;
                ret  = (int)deflate(strm, flush); if (ret == (-2))
                {
                    gz_error(state, -2, "internal error: deflate stream corrupt"); return(-1);
                }
                have -= strm.avail_out;
            }while (have != 0);
            if (flush == 4)
            {
                deflateReset(strm);
            }
            return(0);
        }
Beispiel #9
0
        public static int gz_decomp(gz_state *state)
        {
            int        ret  = 0;
            uint       had  = 0;
            z_stream_s strm = &state->strm;

            had = strm.avail_out;
            do
            {
                if ((strm.avail_in == 0) && (gz_avail(state) == (-1)))
                {
                    return(-1);
                }
                if (strm.avail_in == 0)
                {
                    gz_error(state, -5, "unexpected end of file"); break;
                }
                ret = (int)inflate(strm, 0); if ((ret == (-2)) || (ret == 2))
                {
                    gz_error(state, -2, "internal error: inflate stream corrupt"); return(-1);
                }
                if (ret == (-4))
                {
                    gz_error(state, -4, "out of memory"); return(-1);
                }
                if (ret == (-3))
                {
                    gz_error(state, -3, strm.msg == ((void *)0)?"compressed data error":strm.msg); return(-1);
                }
            }while ((strm.avail_out != 0) && (ret != 1));
            state->x.have = had - strm.avail_out;
            state->x.next = strm.next_out - state->x.have;
            if (ret == 1)
            {
                state->how = 0;
            }
            return(0);
        }