Exemplo n.º 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);
        }
Exemplo n.º 2
0
        public static void gz_error(gz_state *state, int err, sbyte *msg)
        {
            if (state->msg != ((void *)0))
            {
                if (state->err != (-4))
                {
                    free(state->msg);
                }
                state->msg = (void *)0;
            }

            if ((err != 0) && (err != (-5)))
            {
                state->x.have = 0;
            }
            state->err = err;
            if (msg == ((void *)0))
            {
                return;
            }
            if (err == (-4))
            {
                return;
            }
            if ((state->msg = (sbyte *)malloc(strlen(state->path) + strlen(msg) + 3)) == ((void *)0))
            {
                state->err = -4;
                return;
            }

            (void)snprintf(state->msg, (ulong)(strlen(state->path) + strlen(msg) + 3), "%s%s%s", state->path, ": ", msg);
        }
Exemplo n.º 3
0
        public static int gz_load(gz_state *state, byte *buf, uint len, uint *have)
        {
            int  ret = 0;
            uint get = 0; uint max = (uint)(((uint)-1 >> 2) + 1);

            *have = 0;
            do
            {
                get = len - *have;
                if (get > max)
                {
                    get = max;
                }
                ret = (int)read(state->fd, buf + *have, get); if (ret <= 0)
                {
                    break;
                }
                *have += (uint)ret;
            }while ((*have) < len);
            if (ret < 0)
            {
                gz_error(state, -1, strerror((int)*_errno())); return(-1);
            }

            if (ret == 0)
            {
                state->eof = 1;
            }
            return(0);
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 9
0
        public static void gz_reset(gz_state *state)
        {
            state->x.have = 0;
            if (state->mode == 7247)
            {
                state->eof  = 0;
                state->past = 0;
                state->how  = 0;
            }

            state->seek = 0;
            gz_error(state, (int)0, (void *)0);
            state->x.pos         = 0;
            state->strm.avail_in = 0;
        }
Exemplo n.º 10
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);
        }
Exemplo n.º 11
0
        public static int gz_skip(gz_state *state, long len)
        {
            uint n = 0;

            while (len != 0)
            {
                if (state->x.have != 0)
                {
                    n = (uint)(((==) && (state->x.have > 2147483647)) || (((long)state->x.have) > len)?(uint)len:state->x.have); state->x.have -= (uint)n; state->x.next += n; state->x.pos += (long)n; len -= (long)n;
                }
                else if ((state->eof != 0) && (state->strm.avail_in == 0))
                {
                    break;
                }
                else
                {
                    if (gz_fetch(state) == (-1))
                    {
                        return((int)-1);
                    }
                }
            }
            return(0);
        }
Exemplo n.º 12
0
        public static ulong gz_read(gz_state *state, void *buf, ulong len)
        {
            ulong got = 0;
            uint  n   = 0;

            if (len == 0)
            {
                return(0);
            }
            if (state->seek != 0)
            {
                state->seek = 0;
                if (gz_skip(state, state->skip) == (-1))
                {
                    return(0);
                }
            }

            got = 0;
            do
            {
                n = (uint)-1; if (n > len)
                {
                    n = (uint)len;
                }
                if (state->x.have != 0)
                {
                    if (state->x.have < n)
                    {
                        n = state->x.have;
                    }
                    memcpy(buf, state->x.next, (ulong)n); state->x.next += n; state->x.have -= n;
                }
                else if ((state->eof != 0) && (state->strm.avail_in == 0))
                {
                    state->past = 1;
                    break;
                }
                else if ((state->how == 0) || (n < (state->size << 1)))
                {
                    if (gz_fetch(state) == (-1))
                    {
                        return(0);
                    }
                    continue;
                }
                else if (state->how == 1)
                {
                    if (gz_load(state, (byte *)buf, n, &n) == (-1))
                    {
                        return(0);
                    }
                }
                else
                {
                    state->strm.avail_out = n;
                    state->strm.next_out  = (byte *)buf; if (gz_decomp(state) == (-1))
                    {
                        return(0);
                    }
                    n             = state->x.have;
                    state->x.have = 0;
                }
                len          -= n;
                buf           = (sbyte *)buf + n; got += n;
                state->x.pos += n;
            }while (len != 0);
            return(got);
        }
Exemplo n.º 13
0
        public static ulong gz_write(gz_state *state, void *buf, ulong len)
        {
            ulong put = len;

            if (len == 0)
            {
                return(0);
            }
            if ((state->size == 0) && (gz_init(state) == (-1)))
            {
                return(0);
            }
            if (state->seek != 0)
            {
                state->seek = 0;
                if (gz_zero(state, state->skip) == (-1))
                {
                    return(0);
                }
            }

            if (len < state->size)
            {
                do
                {
                    uint have = 0; uint copy = 0; if (state->strm.avail_in == 0)
                    {
                        state->strm.next_in = state->_in_;
                    }
                    have = (uint)(state->strm.next_in + state->strm.avail_in - state->_in_); copy = state->size - have;
                    if (copy > len)
                    {
                        copy = (uint)len;
                    }
                    memcpy(state->_in_ + have, buf, (ulong)copy); state->strm.avail_in += copy;
                    state->x.pos += copy;
                    buf           = (sbyte *)buf + copy; len -= copy;
                    if ((len != 0) && (gz_comp(state, 0) == (-1)))
                    {
                        return(0);
                    }
                }while (len != 0);
            }
            else
            {
                if ((state->strm.avail_in != 0) && (gz_comp(state, 0) == (-1)))
                {
                    return(0);
                }
                state->strm.next_in = (byte *)buf; do
                {
                    uint n = (uint)-1; if (n > len)
                    {
                        n = (uint)len;
                    }
                    state->strm.avail_in = n;
                    state->x.pos        += n;
                    if (gz_comp(state, 0) == (-1))
                    {
                        return(0);
                    }
                    len -= n;
                }while (len != 0);
            }

            return(put);
        }