ComponentAce.Compression.Libs.ZLib.Deflate.deflate_stored C# (CSharp) Method

deflate_stored() private method

Copy without compression as much as possible from the input stream, return the current block state. This function does not insert new strings in the dictionary since uncompressible data is probably not useful. This function is used only for the level=0 compression option. NOTE: this function should be optimized to avoid extra copying from Window to Pending_buf.
private deflate_stored ( int flush ) : int
flush int
return int
        private int deflate_stored(int flush)
        {
            // Stored blocks are limited to 0xffff bytes, Pending_buf is limited
            // to pending_buf_size, and each stored block has a 5 byte header:

            int max_block_size = 0xffff;
            int max_start;

            if (max_block_size > pending_buf_size - 5)
            {
                max_block_size = pending_buf_size - 5;
            }

            // Copy as much as possible from input to output:
            while (true)
            {
                // Fill the Window as much as possible:
                if (lookahead <= 1)
                {
                    fill_window();
                    if (lookahead == 0 && flush == (int)FlushStrategy.Z_NO_FLUSH)
                        return NeedMore;
                    if (lookahead == 0)
                        break; // internalFlush the current block
                }

                strstart += lookahead;
                lookahead = 0;

                // Emit a stored block if Pending_buf will be full:
                max_start = block_start + max_block_size;
                if (strstart == 0 || strstart >= max_start)
                {
                    // strstart == 0 is possible when wraparound on 16-bit machine
                    lookahead = (int)(strstart - max_start);
                    strstart = (int)max_start;

                    flush_block_only(false);
                    if (strm.avail_out == 0)
                        return NeedMore;
                }

                // Flush if we may have to slide, otherwise block_start may become
                // negative and the data will be gone:
                if (strstart - block_start >= w_size - MIN_LOOKAHEAD)
                {
                    flush_block_only(false);
                    if (strm.avail_out == 0)
                        return NeedMore;
                }
            }

            flush_block_only(flush == (int)FlushStrategy.Z_FINISH);
            if (strm.avail_out == 0)
                return (flush == (int)FlushStrategy.Z_FINISH) ? FinishStarted : NeedMore;

            return flush == (int)FlushStrategy.Z_FINISH ? FinishDone : BlockDone;
        }