예제 #1
0
 public void flush_block_only(bool eof)
 {
     _tr_flush_block(block_start >= 0 ? block_start : -1,
                     strstart - block_start,
                     eof);
     block_start = strstart;
     strm.flush_pending();
 }
예제 #2
0
        public int deflate(ZStream strm, int flush)
        {
            int old_flush;

            if (flush > Z_FINISH || flush < 0)
            {
                return(Z_STREAM_ERROR);
            }

            if (strm.next_out == null ||
                (strm.next_in == null && strm.avail_in != 0) ||
                (status == FINISH_STATE && flush != Z_FINISH))
            {
                strm.msg = z_errmsg[Z_NEED_DICT - (Z_STREAM_ERROR)];
                return(Z_STREAM_ERROR);
            }
            if (strm.avail_out == 0)
            {
                strm.msg = z_errmsg[Z_NEED_DICT - (Z_BUF_ERROR)];
                return(Z_BUF_ERROR);
            }

            this.strm  = strm;
            old_flush  = last_flush;
            last_flush = flush;


            if (status == INIT_STATE)
            {
                int header      = (Z_DEFLATED + ((w_bits - 8) << 4)) << 8;
                int level_flags = ((level - 1) & 0xff) >> 1;

                if (level_flags > 3)
                {
                    level_flags = 3;
                }
                header |= (level_flags << 6);
                if (strstart != 0)
                {
                    header |= PRESET_DICT;
                }
                header += 31 - (header % 31);

                status = BUSY_STATE;
                putShortMSB(header);



                if (strstart != 0)
                {
                    putShortMSB((int)(strm.adler >> 16));
                    putShortMSB((int)(strm.adler & 0xffff));
                }
                strm.adler = strm._adler.adler32(0, null, 0, 0);
            }


            if (pending != 0)
            {
                strm.flush_pending();
                if (strm.avail_out == 0)
                {
                    last_flush = -1;
                    return(Z_OK);
                }
            }
            else if (strm.avail_in == 0 && flush <= old_flush &&
                     flush != Z_FINISH)
            {
                strm.msg = z_errmsg[Z_NEED_DICT - (Z_BUF_ERROR)];
                return(Z_BUF_ERROR);
            }


            if (status == FINISH_STATE && strm.avail_in != 0)
            {
                strm.msg = z_errmsg[Z_NEED_DICT - (Z_BUF_ERROR)];
                return(Z_BUF_ERROR);
            }


            if (strm.avail_in != 0 || lookahead != 0 ||
                (flush != Z_NO_FLUSH && status != FINISH_STATE))
            {
                int bstate = -1;
                switch (config_table[level].func)
                {
                case STORED:
                    bstate = deflate_stored(flush);
                    break;

                case FAST:
                    bstate = deflate_fast(flush);
                    break;

                case SLOW:
                    bstate = deflate_slow(flush);
                    break;

                default:
                    break;
                }

                if (bstate == FinishStarted || bstate == FinishDone)
                {
                    status = FINISH_STATE;
                }
                if (bstate == NeedMore || bstate == FinishStarted)
                {
                    if (strm.avail_out == 0)
                    {
                        last_flush = -1;
                    }
                    return(Z_OK);
                }

                if (bstate == BlockDone)
                {
                    if (flush == Z_PARTIAL_FLUSH)
                    {
                        _tr_align();
                    }
                    else
                    {
                        _tr_stored_block(0, 0, false);


                        if (flush == Z_FULL_FLUSH)
                        {
                            for (int i = 0; i < hash_size; i++)
                            {
                                head[i] = 0;
                            }
                        }
                    }
                    strm.flush_pending();
                    if (strm.avail_out == 0)
                    {
                        last_flush = -1;
                        return(Z_OK);
                    }
                }
            }

            if (flush != Z_FINISH)
            {
                return(Z_OK);
            }
            if (noheader != 0)
            {
                return(Z_STREAM_END);
            }


            putShortMSB((int)(strm.adler >> 16));
            putShortMSB((int)(strm.adler & 0xffff));
            strm.flush_pending();



            noheader = -1;
            return(pending != 0 ? Z_OK : Z_STREAM_END);
        }