zlib123/deflate.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 23 Mar 2009 01:43:05 -0400
branchstable-1.0
changeset 966 816a83f04a27
parent 719 7b4677a8de6e
permissions -rw-r--r--
Updated version to 1.0.2 in configure.in ...
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
719
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/* deflate.c -- compress data using the deflation algorithm
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
 * Copyright (C) 1995-2005 Jean-loup Gailly.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
 * For conditions of distribution and use, see copyright notice in zlib.h
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
 */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
/*
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
 *  ALGORITHM
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
 *
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
 *      The "deflation" process depends on being able to identify portions
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
 *      of the input text which are identical to earlier input (within a
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
 *      sliding window trailing behind the input currently being processed).
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
 *
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
 *      The most straightforward technique turns out to be the fastest for
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
 *      most input files: try all possible matches and select the longest.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
 *      The key feature of this algorithm is that insertions into the string
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
 *      dictionary are very simple and thus fast, and deletions are avoided
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
 *      completely. Insertions are performed at each input character, whereas
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
 *      string matches are performed only when the previous match ends. So it
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
 *      is preferable to spend more time in matches to allow very fast string
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
 *      insertions and avoid deletions. The matching algorithm for small
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
 *      strings is inspired from that of Rabin & Karp. A brute force approach
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
 *      is used to find longer strings when a small match has been found.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
 *      A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
 *      (by Leonid Broukhis).
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
 *         A previous version of this file used a more sophisticated algorithm
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
 *      (by Fiala and Greene) which is guaranteed to run in linear amortized
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
 *      time, but has a larger average cost, uses more memory and is patented.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
 *      However the F&G algorithm may be faster for some highly redundant
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
 *      files if the parameter max_chain_length (described below) is too large.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
 *
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
 *  ACKNOWLEDGEMENTS
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
 *
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
 *      The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
 *      I found it in 'freeze' written by Leonid Broukhis.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
 *      Thanks to many people for bug reports and testing.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
 *
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
 *  REFERENCES
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
 *
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
 *      Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
 *      Available in http://www.ietf.org/rfc/rfc1951.txt
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
 *
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
 *      A description of the Rabin and Karp algorithm is given in the book
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
 *         "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
 *
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
 *      Fiala,E.R., and Greene,D.H.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
 *         Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
 *
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
 */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
/* @(#) $Id$ */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
#include "deflate.h"
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
const char deflate_copyright[] =
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
   " deflate 1.2.3 Copyright 1995-2005 Jean-loup Gailly ";
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
/*
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
  If you use the zlib library in a product, an acknowledgment is welcome
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
  in the documentation of your product. If for some reason you cannot
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
  include such an acknowledgment, I would appreciate that you keep this
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
  copyright string in the executable of your product.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
 */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
/* ===========================================================================
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
 *  Function prototypes.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
 */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
typedef enum {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
    need_more,      /* block not completed, need more input or more output */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
    block_done,     /* block flush performed */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
    finish_started, /* finish started, need only more output at next deflate */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
    finish_done     /* finish done, accept no more input or output */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
} block_state;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
typedef block_state (*compress_func) OF((deflate_state *s, int flush));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
/* Compression function. Returns the block state after the call. */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
local void fill_window    OF((deflate_state *s));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
local block_state deflate_stored OF((deflate_state *s, int flush));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
local block_state deflate_fast   OF((deflate_state *s, int flush));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
#ifndef FASTEST
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
local block_state deflate_slow   OF((deflate_state *s, int flush));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
#endif
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
local void lm_init        OF((deflate_state *s));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
local void putShortMSB    OF((deflate_state *s, uInt b));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
local void flush_pending  OF((z_streamp strm));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
local int read_buf        OF((z_streamp strm, Bytef *buf, unsigned size));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
#ifndef FASTEST
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
#ifdef ASMV
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
      void match_init OF((void)); /* asm code initialization */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
      uInt longest_match  OF((deflate_state *s, IPos cur_match));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
#else
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
local uInt longest_match  OF((deflate_state *s, IPos cur_match));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
#endif
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
#endif
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
local uInt longest_match_fast OF((deflate_state *s, IPos cur_match));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
#ifdef DEBUG
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
local  void check_match OF((deflate_state *s, IPos start, IPos match,
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
                            int length));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
#endif
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
/* ===========================================================================
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
 * Local data
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
 */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
#define NIL 0
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
/* Tail of hash chains */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
#ifndef TOO_FAR
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
#  define TOO_FAR 4096
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
#endif
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
/* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
/* Minimum amount of lookahead, except at the end of the input file.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
 * See deflate.c for comments about the MIN_MATCH+1.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
 */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
/* Values for max_lazy_match, good_match and max_chain_length, depending on
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
 * the desired pack level (0..9). The values given below have been tuned to
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
 * exclude worst case performance for pathological files. Better values may be
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
 * found for specific files.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
 */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
typedef struct config_s {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
   ush good_length; /* reduce lazy search above this match length */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
   ush max_lazy;    /* do not perform lazy search above this match length */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   126
   ush nice_length; /* quit search above this match length */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   127
   ush max_chain;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   128
   compress_func func;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   129
} config;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   130
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
#ifdef FASTEST
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
local const config configuration_table[2] = {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   133
/*      good lazy nice chain */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134
/* 0 */ {0,    0,  0,    0, deflate_stored},  /* store only */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   135
/* 1 */ {4,    4,  8,    4, deflate_fast}}; /* max speed, no lazy matches */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   136
#else
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
local const config configuration_table[10] = {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
/*      good lazy nice chain */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
/* 0 */ {0,    0,  0,    0, deflate_stored},  /* store only */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
/* 1 */ {4,    4,  8,    4, deflate_fast}, /* max speed, no lazy matches */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
/* 2 */ {4,    5, 16,    8, deflate_fast},
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   142
/* 3 */ {4,    6, 32,   32, deflate_fast},
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
/* 4 */ {4,    4, 16,   16, deflate_slow},  /* lazy matches */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
/* 5 */ {8,   16, 32,   32, deflate_slow},
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
/* 6 */ {8,   16, 128, 128, deflate_slow},
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   147
/* 7 */ {8,   32, 128, 256, deflate_slow},
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
/* 8 */ {32, 128, 258, 1024, deflate_slow},
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   149
/* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
#endif
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   151
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   153
 * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   154
 * meaning.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   155
 */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   156
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   157
#define EQUAL 0
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
/* result of memcmp for equal strings */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   159
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   160
#ifndef NO_DUMMY_DECL
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
struct static_tree_desc_s {int dummy;}; /* for buggy compilers */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
#endif
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   163
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   164
/* ===========================================================================
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   165
 * Update a hash value with the given input byte
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   166
 * IN  assertion: all calls to to UPDATE_HASH are made with consecutive
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   167
 *    input characters, so that a running hash key can be computed from the
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   168
 *    previous key instead of complete recalculation each time.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   169
 */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   170
#define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   172
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   173
/* ===========================================================================
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   174
 * Insert string str in the dictionary and set match_head to the previous head
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   175
 * of the hash chain (the most recent string with same hash key). Return
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   176
 * the previous length of the hash chain.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   177
 * If this file is compiled with -DFASTEST, the compression level is forced
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   178
 * to 1, and no hash chains are maintained.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   179
 * IN  assertion: all calls to to INSERT_STRING are made with consecutive
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   180
 *    input characters and the first MIN_MATCH bytes of str are valid
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   181
 *    (except for the last MIN_MATCH-1 bytes of the input file).
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   182
 */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   183
#ifdef FASTEST
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   184
#define INSERT_STRING(s, str, match_head) \
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   185
   (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   186
    match_head = s->head[s->ins_h], \
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   187
    s->head[s->ins_h] = (Pos)(str))
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   188
#else
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   189
#define INSERT_STRING(s, str, match_head) \
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   190
   (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   191
    match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
    s->head[s->ins_h] = (Pos)(str))
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   193
#endif
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   194
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   195
/* ===========================================================================
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   196
 * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   197
 * prev[] will be initialized on the fly.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   198
 */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   199
#define CLEAR_HASH(s) \
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   200
    s->head[s->hash_size-1] = NIL; \
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   201
    zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   202
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   203
/* ========================================================================= */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   204
int ZEXPORT deflateInit_(strm, level, version, stream_size)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   205
    z_streamp strm;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   206
    int level;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   207
    const char *version;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   208
    int stream_size;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   209
{
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   210
    return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   211
                         Z_DEFAULT_STRATEGY, version, stream_size);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   212
    /* To do: ignore strm->next_in if we use it as window */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   213
}
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   214
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   215
/* ========================================================================= */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   216
int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   217
                  version, stream_size)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   218
    z_streamp strm;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   219
    int  level;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   220
    int  method;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   221
    int  windowBits;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   222
    int  memLevel;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   223
    int  strategy;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   224
    const char *version;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   225
    int stream_size;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   226
{
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   227
    deflate_state *s;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   228
    int wrap = 1;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   229
    static const char my_version[] = ZLIB_VERSION;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   230
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   231
    ushf *overlay;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   232
    /* We overlay pending_buf and d_buf+l_buf. This works since the average
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   233
     * output size for (length,distance) codes is <= 24 bits.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   234
     */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   235
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   236
    if (version == Z_NULL || version[0] != my_version[0] ||
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   237
        stream_size != sizeof(z_stream)) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   238
        return Z_VERSION_ERROR;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   239
    }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   240
    if (strm == Z_NULL) return Z_STREAM_ERROR;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   241
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   242
    strm->msg = Z_NULL;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   243
    if (strm->zalloc == (alloc_func)0) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   244
        strm->zalloc = zcalloc;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   245
        strm->opaque = (voidpf)0;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   246
    }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   247
    if (strm->zfree == (free_func)0) strm->zfree = zcfree;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   248
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   249
#ifdef FASTEST
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   250
    if (level != 0) level = 1;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   251
#else
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   252
    if (level == Z_DEFAULT_COMPRESSION) level = 6;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   253
#endif
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   254
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   255
    if (windowBits < 0) { /* suppress zlib wrapper */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   256
        wrap = 0;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   257
        windowBits = -windowBits;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   258
    }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   259
#ifdef GZIP
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   260
    else if (windowBits > 15) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   261
        wrap = 2;       /* write gzip wrapper instead */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   262
        windowBits -= 16;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   263
    }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   264
#endif
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   265
    if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   266
        windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   267
        strategy < 0 || strategy > Z_FIXED) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   268
        return Z_STREAM_ERROR;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   269
    }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   270
    if (windowBits == 8) windowBits = 9;  /* until 256-byte window bug fixed */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   271
    s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   272
    if (s == Z_NULL) return Z_MEM_ERROR;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   273
    strm->state = (struct internal_state FAR *)s;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   274
    s->strm = strm;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   275
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   276
    s->wrap = wrap;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   277
    s->gzhead = Z_NULL;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   278
    s->w_bits = windowBits;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   279
    s->w_size = 1 << s->w_bits;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   280
    s->w_mask = s->w_size - 1;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   281
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   282
    s->hash_bits = memLevel + 7;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   283
    s->hash_size = 1 << s->hash_bits;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   284
    s->hash_mask = s->hash_size - 1;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   285
    s->hash_shift =  ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   286
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   287
    s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   288
    s->prev   = (Posf *)  ZALLOC(strm, s->w_size, sizeof(Pos));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   289
    s->head   = (Posf *)  ZALLOC(strm, s->hash_size, sizeof(Pos));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   290
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   291
    s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   292
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   293
    overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   294
    s->pending_buf = (uchf *) overlay;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   295
    s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   296
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   297
    if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   298
        s->pending_buf == Z_NULL) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   299
        s->status = FINISH_STATE;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   300
        strm->msg = (char*)ERR_MSG(Z_MEM_ERROR);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   301
        deflateEnd (strm);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   302
        return Z_MEM_ERROR;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   303
    }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   304
    s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   305
    s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   306
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   307
    s->level = level;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   308
    s->strategy = strategy;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   309
    s->method = (Byte)method;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   310
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   311
    return deflateReset(strm);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   312
}
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   313
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   314
/* ========================================================================= */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   315
int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   316
    z_streamp strm;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   317
    const Bytef *dictionary;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   318
    uInt  dictLength;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   319
{
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   320
    deflate_state *s;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   321
    uInt length = dictLength;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   322
    uInt n;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   323
    IPos hash_head = 0;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   324
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   325
    if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL ||
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   326
        strm->state->wrap == 2 ||
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   327
        (strm->state->wrap == 1 && strm->state->status != INIT_STATE))
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   328
        return Z_STREAM_ERROR;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   329
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   330
    s = strm->state;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   331
    if (s->wrap)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   332
        strm->adler = adler32(strm->adler, dictionary, dictLength);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   333
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   334
    if (length < MIN_MATCH) return Z_OK;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   335
    if (length > MAX_DIST(s)) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   336
        length = MAX_DIST(s);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   337
        dictionary += dictLength - length; /* use the tail of the dictionary */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   338
    }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   339
    zmemcpy(s->window, dictionary, length);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   340
    s->strstart = length;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   341
    s->block_start = (long)length;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   342
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   343
    /* Insert all strings in the hash table (except for the last two bytes).
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   344
     * s->lookahead stays null, so s->ins_h will be recomputed at the next
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   345
     * call of fill_window.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   346
     */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   347
    s->ins_h = s->window[0];
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   348
    UPDATE_HASH(s, s->ins_h, s->window[1]);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   349
    for (n = 0; n <= length - MIN_MATCH; n++) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   350
        INSERT_STRING(s, n, hash_head);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   351
    }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   352
    if (hash_head) hash_head = 0;  /* to make compiler happy */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   353
    return Z_OK;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   354
}
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   355
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   356
/* ========================================================================= */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   357
int ZEXPORT deflateReset (strm)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   358
    z_streamp strm;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   359
{
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   360
    deflate_state *s;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   361
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   362
    if (strm == Z_NULL || strm->state == Z_NULL ||
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   363
        strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   364
        return Z_STREAM_ERROR;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   365
    }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   366
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   367
    strm->total_in = strm->total_out = 0;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   368
    strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   369
    strm->data_type = Z_UNKNOWN;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   370
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   371
    s = (deflate_state *)strm->state;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   372
    s->pending = 0;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   373
    s->pending_out = s->pending_buf;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   374
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   375
    if (s->wrap < 0) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   376
        s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   377
    }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   378
    s->status = s->wrap ? INIT_STATE : BUSY_STATE;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   379
    strm->adler =
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   380
#ifdef GZIP
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   381
        s->wrap == 2 ? crc32(0L, Z_NULL, 0) :
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   382
#endif
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   383
        adler32(0L, Z_NULL, 0);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   384
    s->last_flush = Z_NO_FLUSH;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   385
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   386
    _tr_init(s);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   387
    lm_init(s);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   388
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   389
    return Z_OK;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   390
}
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   391
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   392
/* ========================================================================= */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   393
int ZEXPORT deflateSetHeader (strm, head)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   394
    z_streamp strm;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   395
    gz_headerp head;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   396
{
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   397
    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   398
    if (strm->state->wrap != 2) return Z_STREAM_ERROR;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   399
    strm->state->gzhead = head;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   400
    return Z_OK;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   401
}
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   402
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   403
/* ========================================================================= */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   404
int ZEXPORT deflatePrime (strm, bits, value)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   405
    z_streamp strm;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   406
    int bits;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   407
    int value;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   408
{
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   409
    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   410
    strm->state->bi_valid = bits;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   411
    strm->state->bi_buf = (ush)(value & ((1 << bits) - 1));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   412
    return Z_OK;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   413
}
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   414
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   415
/* ========================================================================= */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   416
int ZEXPORT deflateParams(strm, level, strategy)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   417
    z_streamp strm;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   418
    int level;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   419
    int strategy;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   420
{
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   421
    deflate_state *s;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   422
    compress_func func;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   423
    int err = Z_OK;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   424
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   425
    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   426
    s = strm->state;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   427
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   428
#ifdef FASTEST
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   429
    if (level != 0) level = 1;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   430
#else
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   431
    if (level == Z_DEFAULT_COMPRESSION) level = 6;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   432
#endif
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   433
    if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   434
        return Z_STREAM_ERROR;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   435
    }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   436
    func = configuration_table[s->level].func;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   437
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   438
    if (func != configuration_table[level].func && strm->total_in != 0) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   439
        /* Flush the last buffer: */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   440
        err = deflate(strm, Z_PARTIAL_FLUSH);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   441
    }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   442
    if (s->level != level) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   443
        s->level = level;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   444
        s->max_lazy_match   = configuration_table[level].max_lazy;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   445
        s->good_match       = configuration_table[level].good_length;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   446
        s->nice_match       = configuration_table[level].nice_length;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   447
        s->max_chain_length = configuration_table[level].max_chain;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   448
    }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   449
    s->strategy = strategy;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   450
    return err;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   451
}
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   452
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   453
/* ========================================================================= */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   454
int ZEXPORT deflateTune(strm, good_length, max_lazy, nice_length, max_chain)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   455
    z_streamp strm;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   456
    int good_length;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   457
    int max_lazy;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   458
    int nice_length;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   459
    int max_chain;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   460
{
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   461
    deflate_state *s;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   462
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   463
    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   464
    s = strm->state;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   465
    s->good_match = good_length;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   466
    s->max_lazy_match = max_lazy;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   467
    s->nice_match = nice_length;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   468
    s->max_chain_length = max_chain;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   469
    return Z_OK;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   470
}
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   471
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   472
/* =========================================================================
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   473
 * For the default windowBits of 15 and memLevel of 8, this function returns
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   474
 * a close to exact, as well as small, upper bound on the compressed size.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   475
 * They are coded as constants here for a reason--if the #define's are
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   476
 * changed, then this function needs to be changed as well.  The return
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   477
 * value for 15 and 8 only works for those exact settings.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   478
 *
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   479
 * For any setting other than those defaults for windowBits and memLevel,
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   480
 * the value returned is a conservative worst case for the maximum expansion
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   481
 * resulting from using fixed blocks instead of stored blocks, which deflate
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   482
 * can emit on compressed data for some combinations of the parameters.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   483
 *
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   484
 * This function could be more sophisticated to provide closer upper bounds
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   485
 * for every combination of windowBits and memLevel, as well as wrap.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   486
 * But even the conservative upper bound of about 14% expansion does not
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   487
 * seem onerous for output buffer allocation.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   488
 */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   489
uLong ZEXPORT deflateBound(strm, sourceLen)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   490
    z_streamp strm;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   491
    uLong sourceLen;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   492
{
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   493
    deflate_state *s;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   494
    uLong destLen;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   495
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   496
    /* conservative upper bound */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   497
    destLen = sourceLen +
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   498
              ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 11;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   499
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   500
    /* if can't get parameters, return conservative bound */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   501
    if (strm == Z_NULL || strm->state == Z_NULL)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   502
        return destLen;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   503
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   504
    /* if not default parameters, return conservative bound */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   505
    s = strm->state;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   506
    if (s->w_bits != 15 || s->hash_bits != 8 + 7)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   507
        return destLen;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   508
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   509
    /* default settings: return tight bound for that case */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   510
    return compressBound(sourceLen);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   511
}
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   512
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   513
/* =========================================================================
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   514
 * Put a short in the pending buffer. The 16-bit value is put in MSB order.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   515
 * IN assertion: the stream state is correct and there is enough room in
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   516
 * pending_buf.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   517
 */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   518
local void putShortMSB (s, b)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   519
    deflate_state *s;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   520
    uInt b;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   521
{
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   522
    put_byte(s, (Byte)(b >> 8));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   523
    put_byte(s, (Byte)(b & 0xff));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   524
}
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   525
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   526
/* =========================================================================
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   527
 * Flush as much pending output as possible. All deflate() output goes
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   528
 * through this function so some applications may wish to modify it
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   529
 * to avoid allocating a large strm->next_out buffer and copying into it.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   530
 * (See also read_buf()).
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   531
 */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   532
local void flush_pending(strm)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   533
    z_streamp strm;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   534
{
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   535
    unsigned len = strm->state->pending;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   536
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   537
    if (len > strm->avail_out) len = strm->avail_out;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   538
    if (len == 0) return;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   539
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   540
    zmemcpy(strm->next_out, strm->state->pending_out, len);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   541
    strm->next_out  += len;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   542
    strm->state->pending_out  += len;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   543
    strm->total_out += len;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   544
    strm->avail_out  -= len;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   545
    strm->state->pending -= len;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   546
    if (strm->state->pending == 0) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   547
        strm->state->pending_out = strm->state->pending_buf;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   548
    }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   549
}
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   550
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   551
/* ========================================================================= */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   552
int ZEXPORT deflate (strm, flush)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   553
    z_streamp strm;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   554
    int flush;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   555
{
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   556
    int old_flush; /* value of flush param for previous deflate call */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   557
    deflate_state *s;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   558
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   559
    if (strm == Z_NULL || strm->state == Z_NULL ||
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   560
        flush > Z_FINISH || flush < 0) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   561
        return Z_STREAM_ERROR;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   562
    }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   563
    s = strm->state;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   564
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   565
    if (strm->next_out == Z_NULL ||
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   566
        (strm->next_in == Z_NULL && strm->avail_in != 0) ||
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   567
        (s->status == FINISH_STATE && flush != Z_FINISH)) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   568
        ERR_RETURN(strm, Z_STREAM_ERROR);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   569
    }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   570
    if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   571
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   572
    s->strm = strm; /* just in case */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   573
    old_flush = s->last_flush;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   574
    s->last_flush = flush;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   575
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   576
    /* Write the header */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   577
    if (s->status == INIT_STATE) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   578
#ifdef GZIP
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   579
        if (s->wrap == 2) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   580
            strm->adler = crc32(0L, Z_NULL, 0);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   581
            put_byte(s, 31);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   582
            put_byte(s, 139);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   583
            put_byte(s, 8);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   584
            if (s->gzhead == NULL) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   585
                put_byte(s, 0);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   586
                put_byte(s, 0);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   587
                put_byte(s, 0);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   588
                put_byte(s, 0);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   589
                put_byte(s, 0);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   590
                put_byte(s, s->level == 9 ? 2 :
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   591
                            (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   592
                             4 : 0));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   593
                put_byte(s, OS_CODE);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   594
                s->status = BUSY_STATE;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   595
            }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   596
            else {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   597
                put_byte(s, (s->gzhead->text ? 1 : 0) +
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   598
                            (s->gzhead->hcrc ? 2 : 0) +
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   599
                            (s->gzhead->extra == Z_NULL ? 0 : 4) +
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   600
                            (s->gzhead->name == Z_NULL ? 0 : 8) +
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   601
                            (s->gzhead->comment == Z_NULL ? 0 : 16)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   602
                        );
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   603
                put_byte(s, (Byte)(s->gzhead->time & 0xff));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   604
                put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   605
                put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   606
                put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   607
                put_byte(s, s->level == 9 ? 2 :
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   608
                            (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   609
                             4 : 0));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   610
                put_byte(s, s->gzhead->os & 0xff);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   611
                if (s->gzhead->extra != NULL) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   612
                    put_byte(s, s->gzhead->extra_len & 0xff);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   613
                    put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   614
                }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   615
                if (s->gzhead->hcrc)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   616
                    strm->adler = crc32(strm->adler, s->pending_buf,
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   617
                                        s->pending);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   618
                s->gzindex = 0;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   619
                s->status = EXTRA_STATE;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   620
            }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   621
        }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   622
        else
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   623
#endif
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   624
        {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   625
            uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   626
            uInt level_flags;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   627
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   628
            if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   629
                level_flags = 0;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   630
            else if (s->level < 6)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   631
                level_flags = 1;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   632
            else if (s->level == 6)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   633
                level_flags = 2;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   634
            else
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   635
                level_flags = 3;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   636
            header |= (level_flags << 6);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   637
            if (s->strstart != 0) header |= PRESET_DICT;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   638
            header += 31 - (header % 31);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   639
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   640
            s->status = BUSY_STATE;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   641
            putShortMSB(s, header);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   642
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   643
            /* Save the adler32 of the preset dictionary: */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   644
            if (s->strstart != 0) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   645
                putShortMSB(s, (uInt)(strm->adler >> 16));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   646
                putShortMSB(s, (uInt)(strm->adler & 0xffff));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   647
            }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   648
            strm->adler = adler32(0L, Z_NULL, 0);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   649
        }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   650
    }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   651
#ifdef GZIP
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   652
    if (s->status == EXTRA_STATE) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   653
        if (s->gzhead->extra != NULL) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   654
            uInt beg = s->pending;  /* start of bytes to update crc */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   655
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   656
            while (s->gzindex < (s->gzhead->extra_len & 0xffff)) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   657
                if (s->pending == s->pending_buf_size) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   658
                    if (s->gzhead->hcrc && s->pending > beg)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   659
                        strm->adler = crc32(strm->adler, s->pending_buf + beg,
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   660
                                            s->pending - beg);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   661
                    flush_pending(strm);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   662
                    beg = s->pending;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   663
                    if (s->pending == s->pending_buf_size)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   664
                        break;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   665
                }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   666
                put_byte(s, s->gzhead->extra[s->gzindex]);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   667
                s->gzindex++;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   668
            }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   669
            if (s->gzhead->hcrc && s->pending > beg)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   670
                strm->adler = crc32(strm->adler, s->pending_buf + beg,
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   671
                                    s->pending - beg);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   672
            if (s->gzindex == s->gzhead->extra_len) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   673
                s->gzindex = 0;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   674
                s->status = NAME_STATE;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   675
            }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   676
        }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   677
        else
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   678
            s->status = NAME_STATE;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   679
    }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   680
    if (s->status == NAME_STATE) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   681
        if (s->gzhead->name != NULL) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   682
            uInt beg = s->pending;  /* start of bytes to update crc */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   683
            int val;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   684
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   685
            do {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   686
                if (s->pending == s->pending_buf_size) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   687
                    if (s->gzhead->hcrc && s->pending > beg)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   688
                        strm->adler = crc32(strm->adler, s->pending_buf + beg,
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   689
                                            s->pending - beg);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   690
                    flush_pending(strm);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   691
                    beg = s->pending;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   692
                    if (s->pending == s->pending_buf_size) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   693
                        val = 1;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   694
                        break;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   695
                    }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   696
                }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   697
                val = s->gzhead->name[s->gzindex++];
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   698
                put_byte(s, val);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   699
            } while (val != 0);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   700
            if (s->gzhead->hcrc && s->pending > beg)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   701
                strm->adler = crc32(strm->adler, s->pending_buf + beg,
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   702
                                    s->pending - beg);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   703
            if (val == 0) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   704
                s->gzindex = 0;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   705
                s->status = COMMENT_STATE;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   706
            }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   707
        }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   708
        else
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   709
            s->status = COMMENT_STATE;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   710
    }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   711
    if (s->status == COMMENT_STATE) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   712
        if (s->gzhead->comment != NULL) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   713
            uInt beg = s->pending;  /* start of bytes to update crc */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   714
            int val;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   715
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   716
            do {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   717
                if (s->pending == s->pending_buf_size) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   718
                    if (s->gzhead->hcrc && s->pending > beg)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   719
                        strm->adler = crc32(strm->adler, s->pending_buf + beg,
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   720
                                            s->pending - beg);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   721
                    flush_pending(strm);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   722
                    beg = s->pending;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   723
                    if (s->pending == s->pending_buf_size) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   724
                        val = 1;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   725
                        break;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   726
                    }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   727
                }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   728
                val = s->gzhead->comment[s->gzindex++];
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   729
                put_byte(s, val);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   730
            } while (val != 0);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   731
            if (s->gzhead->hcrc && s->pending > beg)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   732
                strm->adler = crc32(strm->adler, s->pending_buf + beg,
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   733
                                    s->pending - beg);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   734
            if (val == 0)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   735
                s->status = HCRC_STATE;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   736
        }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   737
        else
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   738
            s->status = HCRC_STATE;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   739
    }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   740
    if (s->status == HCRC_STATE) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   741
        if (s->gzhead->hcrc) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   742
            if (s->pending + 2 > s->pending_buf_size)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   743
                flush_pending(strm);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   744
            if (s->pending + 2 <= s->pending_buf_size) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   745
                put_byte(s, (Byte)(strm->adler & 0xff));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   746
                put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   747
                strm->adler = crc32(0L, Z_NULL, 0);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   748
                s->status = BUSY_STATE;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   749
            }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   750
        }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   751
        else
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   752
            s->status = BUSY_STATE;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   753
    }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   754
#endif
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   755
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   756
    /* Flush as much pending output as possible */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   757
    if (s->pending != 0) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   758
        flush_pending(strm);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   759
        if (strm->avail_out == 0) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   760
            /* Since avail_out is 0, deflate will be called again with
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   761
             * more output space, but possibly with both pending and
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   762
             * avail_in equal to zero. There won't be anything to do,
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   763
             * but this is not an error situation so make sure we
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   764
             * return OK instead of BUF_ERROR at next call of deflate:
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   765
             */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   766
            s->last_flush = -1;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   767
            return Z_OK;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   768
        }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   769
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   770
    /* Make sure there is something to do and avoid duplicate consecutive
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   771
     * flushes. For repeated and useless calls with Z_FINISH, we keep
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   772
     * returning Z_STREAM_END instead of Z_BUF_ERROR.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   773
     */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   774
    } else if (strm->avail_in == 0 && flush <= old_flush &&
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   775
               flush != Z_FINISH) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   776
        ERR_RETURN(strm, Z_BUF_ERROR);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   777
    }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   778
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   779
    /* User must not provide more input after the first FINISH: */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   780
    if (s->status == FINISH_STATE && strm->avail_in != 0) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   781
        ERR_RETURN(strm, Z_BUF_ERROR);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   782
    }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   783
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   784
    /* Start a new block or continue the current one.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   785
     */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   786
    if (strm->avail_in != 0 || s->lookahead != 0 ||
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   787
        (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   788
        block_state bstate;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   789
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   790
        bstate = (*(configuration_table[s->level].func))(s, flush);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   791
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   792
        if (bstate == finish_started || bstate == finish_done) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   793
            s->status = FINISH_STATE;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   794
        }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   795
        if (bstate == need_more || bstate == finish_started) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   796
            if (strm->avail_out == 0) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   797
                s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   798
            }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   799
            return Z_OK;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   800
            /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   801
             * of deflate should use the same flush parameter to make sure
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   802
             * that the flush is complete. So we don't have to output an
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   803
             * empty block here, this will be done at next call. This also
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   804
             * ensures that for a very small output buffer, we emit at most
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   805
             * one empty block.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   806
             */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   807
        }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   808
        if (bstate == block_done) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   809
            if (flush == Z_PARTIAL_FLUSH) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   810
                _tr_align(s);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   811
            } else { /* FULL_FLUSH or SYNC_FLUSH */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   812
                _tr_stored_block(s, (char*)0, 0L, 0);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   813
                /* For a full flush, this empty block will be recognized
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   814
                 * as a special marker by inflate_sync().
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   815
                 */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   816
                if (flush == Z_FULL_FLUSH) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   817
                    CLEAR_HASH(s);             /* forget history */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   818
                }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   819
            }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   820
            flush_pending(strm);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   821
            if (strm->avail_out == 0) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   822
              s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   823
              return Z_OK;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   824
            }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   825
        }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   826
    }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   827
    Assert(strm->avail_out > 0, "bug2");
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   828
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   829
    if (flush != Z_FINISH) return Z_OK;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   830
    if (s->wrap <= 0) return Z_STREAM_END;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   831
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   832
    /* Write the trailer */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   833
#ifdef GZIP
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   834
    if (s->wrap == 2) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   835
        put_byte(s, (Byte)(strm->adler & 0xff));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   836
        put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   837
        put_byte(s, (Byte)((strm->adler >> 16) & 0xff));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   838
        put_byte(s, (Byte)((strm->adler >> 24) & 0xff));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   839
        put_byte(s, (Byte)(strm->total_in & 0xff));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   840
        put_byte(s, (Byte)((strm->total_in >> 8) & 0xff));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   841
        put_byte(s, (Byte)((strm->total_in >> 16) & 0xff));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   842
        put_byte(s, (Byte)((strm->total_in >> 24) & 0xff));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   843
    }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   844
    else
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   845
#endif
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   846
    {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   847
        putShortMSB(s, (uInt)(strm->adler >> 16));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   848
        putShortMSB(s, (uInt)(strm->adler & 0xffff));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   849
    }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   850
    flush_pending(strm);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   851
    /* If avail_out is zero, the application will call deflate again
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   852
     * to flush the rest.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   853
     */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   854
    if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   855
    return s->pending != 0 ? Z_OK : Z_STREAM_END;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   856
}
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   857
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   858
/* ========================================================================= */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   859
int ZEXPORT deflateEnd (strm)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   860
    z_streamp strm;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   861
{
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   862
    int status;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   863
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   864
    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   865
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   866
    status = strm->state->status;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   867
    if (status != INIT_STATE &&
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   868
        status != EXTRA_STATE &&
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   869
        status != NAME_STATE &&
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   870
        status != COMMENT_STATE &&
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   871
        status != HCRC_STATE &&
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   872
        status != BUSY_STATE &&
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   873
        status != FINISH_STATE) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   874
      return Z_STREAM_ERROR;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   875
    }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   876
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   877
    /* Deallocate in reverse order of allocations: */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   878
    TRY_FREE(strm, strm->state->pending_buf);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   879
    TRY_FREE(strm, strm->state->head);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   880
    TRY_FREE(strm, strm->state->prev);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   881
    TRY_FREE(strm, strm->state->window);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   882
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   883
    ZFREE(strm, strm->state);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   884
    strm->state = Z_NULL;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   885
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   886
    return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   887
}
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   888
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   889
/* =========================================================================
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   890
 * Copy the source state to the destination state.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   891
 * To simplify the source, this is not supported for 16-bit MSDOS (which
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   892
 * doesn't have enough memory anyway to duplicate compression states).
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   893
 */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   894
int ZEXPORT deflateCopy (dest, source)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   895
    z_streamp dest;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   896
    z_streamp source;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   897
{
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   898
#ifdef MAXSEG_64K
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   899
    return Z_STREAM_ERROR;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   900
#else
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   901
    deflate_state *ds;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   902
    deflate_state *ss;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   903
    ushf *overlay;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   904
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   905
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   906
    if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   907
        return Z_STREAM_ERROR;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   908
    }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   909
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   910
    ss = source->state;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   911
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   912
    zmemcpy(dest, source, sizeof(z_stream));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   913
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   914
    ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   915
    if (ds == Z_NULL) return Z_MEM_ERROR;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   916
    dest->state = (struct internal_state FAR *) ds;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   917
    zmemcpy(ds, ss, sizeof(deflate_state));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   918
    ds->strm = dest;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   919
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   920
    ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   921
    ds->prev   = (Posf *)  ZALLOC(dest, ds->w_size, sizeof(Pos));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   922
    ds->head   = (Posf *)  ZALLOC(dest, ds->hash_size, sizeof(Pos));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   923
    overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   924
    ds->pending_buf = (uchf *) overlay;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   925
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   926
    if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   927
        ds->pending_buf == Z_NULL) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   928
        deflateEnd (dest);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   929
        return Z_MEM_ERROR;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   930
    }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   931
    /* following zmemcpy do not work for 16-bit MSDOS */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   932
    zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   933
    zmemcpy(ds->prev, ss->prev, ds->w_size * sizeof(Pos));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   934
    zmemcpy(ds->head, ss->head, ds->hash_size * sizeof(Pos));
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   935
    zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   936
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   937
    ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   938
    ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   939
    ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   940
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   941
    ds->l_desc.dyn_tree = ds->dyn_ltree;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   942
    ds->d_desc.dyn_tree = ds->dyn_dtree;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   943
    ds->bl_desc.dyn_tree = ds->bl_tree;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   944
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   945
    return Z_OK;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   946
#endif /* MAXSEG_64K */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   947
}
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   948
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   949
/* ===========================================================================
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   950
 * Read a new buffer from the current input stream, update the adler32
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   951
 * and total number of bytes read.  All deflate() input goes through
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   952
 * this function so some applications may wish to modify it to avoid
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   953
 * allocating a large strm->next_in buffer and copying from it.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   954
 * (See also flush_pending()).
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   955
 */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   956
local int read_buf(strm, buf, size)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   957
    z_streamp strm;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   958
    Bytef *buf;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   959
    unsigned size;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   960
{
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   961
    unsigned len = strm->avail_in;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   962
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   963
    if (len > size) len = size;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   964
    if (len == 0) return 0;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   965
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   966
    strm->avail_in  -= len;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   967
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   968
    if (strm->state->wrap == 1) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   969
        strm->adler = adler32(strm->adler, strm->next_in, len);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   970
    }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   971
#ifdef GZIP
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   972
    else if (strm->state->wrap == 2) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   973
        strm->adler = crc32(strm->adler, strm->next_in, len);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   974
    }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   975
#endif
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   976
    zmemcpy(buf, strm->next_in, len);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   977
    strm->next_in  += len;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   978
    strm->total_in += len;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   979
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   980
    return (int)len;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   981
}
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   982
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   983
/* ===========================================================================
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   984
 * Initialize the "longest match" routines for a new zlib stream
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   985
 */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   986
local void lm_init (s)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   987
    deflate_state *s;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   988
{
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   989
    s->window_size = (ulg)2L*s->w_size;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   990
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   991
    CLEAR_HASH(s);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   992
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   993
    /* Set the default configuration parameters:
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   994
     */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   995
    s->max_lazy_match   = configuration_table[s->level].max_lazy;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   996
    s->good_match       = configuration_table[s->level].good_length;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   997
    s->nice_match       = configuration_table[s->level].nice_length;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   998
    s->max_chain_length = configuration_table[s->level].max_chain;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   999
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1000
    s->strstart = 0;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1001
    s->block_start = 0L;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1002
    s->lookahead = 0;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1003
    s->match_length = s->prev_length = MIN_MATCH-1;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1004
    s->match_available = 0;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1005
    s->ins_h = 0;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1006
#ifndef FASTEST
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1007
#ifdef ASMV
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1008
    match_init(); /* initialize the asm code */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1009
#endif
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1010
#endif
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1011
}
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1012
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1013
#ifndef FASTEST
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1014
/* ===========================================================================
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1015
 * Set match_start to the longest match starting at the given string and
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1016
 * return its length. Matches shorter or equal to prev_length are discarded,
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1017
 * in which case the result is equal to prev_length and match_start is
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1018
 * garbage.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1019
 * IN assertions: cur_match is the head of the hash chain for the current
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1020
 *   string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1021
 * OUT assertion: the match length is not greater than s->lookahead.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1022
 */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1023
#ifndef ASMV
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1024
/* For 80x86 and 680x0, an optimized version will be provided in match.asm or
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1025
 * match.S. The code will be functionally equivalent.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1026
 */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1027
local uInt longest_match(s, cur_match)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1028
    deflate_state *s;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1029
    IPos cur_match;                             /* current match */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1030
{
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1031
    unsigned chain_length = s->max_chain_length;/* max hash chain length */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1032
    register Bytef *scan = s->window + s->strstart; /* current string */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1033
    register Bytef *match;                       /* matched string */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1034
    register int len;                           /* length of current match */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1035
    int best_len = s->prev_length;              /* best match length so far */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1036
    int nice_match = s->nice_match;             /* stop if match long enough */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1037
    IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1038
        s->strstart - (IPos)MAX_DIST(s) : NIL;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1039
    /* Stop when cur_match becomes <= limit. To simplify the code,
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1040
     * we prevent matches with the string of window index 0.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1041
     */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1042
    Posf *prev = s->prev;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1043
    uInt wmask = s->w_mask;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1044
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1045
#ifdef UNALIGNED_OK
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1046
    /* Compare two bytes at a time. Note: this is not always beneficial.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1047
     * Try with and without -DUNALIGNED_OK to check.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1048
     */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1049
    register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1050
    register ush scan_start = *(ushf*)scan;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1051
    register ush scan_end   = *(ushf*)(scan+best_len-1);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1052
#else
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1053
    register Bytef *strend = s->window + s->strstart + MAX_MATCH;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1054
    register Byte scan_end1  = scan[best_len-1];
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1055
    register Byte scan_end   = scan[best_len];
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1056
#endif
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1057
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1058
    /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1059
     * It is easy to get rid of this optimization if necessary.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1060
     */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1061
    Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1062
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1063
    /* Do not waste too much time if we already have a good match: */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1064
    if (s->prev_length >= s->good_match) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1065
        chain_length >>= 2;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1066
    }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1067
    /* Do not look for matches beyond the end of the input. This is necessary
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1068
     * to make deflate deterministic.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1069
     */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1070
    if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1071
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1072
    Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1073
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1074
    do {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1075
        Assert(cur_match < s->strstart, "no future");
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1076
        match = s->window + cur_match;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1077
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1078
        /* Skip to next match if the match length cannot increase
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1079
         * or if the match length is less than 2.  Note that the checks below
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1080
         * for insufficient lookahead only occur occasionally for performance
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1081
         * reasons.  Therefore uninitialized memory will be accessed, and
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1082
         * conditional jumps will be made that depend on those values.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1083
         * However the length of the match is limited to the lookahead, so
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1084
         * the output of deflate is not affected by the uninitialized values.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1085
         */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1086
#if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1087
        /* This code assumes sizeof(unsigned short) == 2. Do not use
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1088
         * UNALIGNED_OK if your compiler uses a different size.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1089
         */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1090
        if (*(ushf*)(match+best_len-1) != scan_end ||
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1091
            *(ushf*)match != scan_start) continue;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1092
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1093
        /* It is not necessary to compare scan[2] and match[2] since they are
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1094
         * always equal when the other bytes match, given that the hash keys
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1095
         * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1096
         * strstart+3, +5, ... up to strstart+257. We check for insufficient
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1097
         * lookahead only every 4th comparison; the 128th check will be made
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1098
         * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1099
         * necessary to put more guard bytes at the end of the window, or
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1100
         * to check more often for insufficient lookahead.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1101
         */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1102
        Assert(scan[2] == match[2], "scan[2]?");
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1103
        scan++, match++;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1104
        do {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1105
        } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1106
                 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1107
                 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1108
                 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1109
                 scan < strend);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1110
        /* The funny "do {}" generates better code on most compilers */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1111
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1112
        /* Here, scan <= window+strstart+257 */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1113
        Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1114
        if (*scan == *match) scan++;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1115
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1116
        len = (MAX_MATCH - 1) - (int)(strend-scan);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1117
        scan = strend - (MAX_MATCH-1);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1118
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1119
#else /* UNALIGNED_OK */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1120
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1121
        if (match[best_len]   != scan_end  ||
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1122
            match[best_len-1] != scan_end1 ||
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1123
            *match            != *scan     ||
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1124
            *++match          != scan[1])      continue;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1125
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1126
        /* The check at best_len-1 can be removed because it will be made
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1127
         * again later. (This heuristic is not always a win.)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1128
         * It is not necessary to compare scan[2] and match[2] since they
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1129
         * are always equal when the other bytes match, given that
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1130
         * the hash keys are equal and that HASH_BITS >= 8.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1131
         */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1132
        scan += 2, match++;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1133
        Assert(*scan == *match, "match[2]?");
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1134
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1135
        /* We check for insufficient lookahead only every 8th comparison;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1136
         * the 256th check will be made at strstart+258.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1137
         */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1138
        do {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1139
        } while (*++scan == *++match && *++scan == *++match &&
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1140
                 *++scan == *++match && *++scan == *++match &&
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1141
                 *++scan == *++match && *++scan == *++match &&
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1142
                 *++scan == *++match && *++scan == *++match &&
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1143
                 scan < strend);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1144
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1145
        Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1146
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1147
        len = MAX_MATCH - (int)(strend - scan);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1148
        scan = strend - MAX_MATCH;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1149
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1150
#endif /* UNALIGNED_OK */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1151
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1152
        if (len > best_len) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1153
            s->match_start = cur_match;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1154
            best_len = len;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1155
            if (len >= nice_match) break;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1156
#ifdef UNALIGNED_OK
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1157
            scan_end = *(ushf*)(scan+best_len-1);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1158
#else
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1159
            scan_end1  = scan[best_len-1];
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1160
            scan_end   = scan[best_len];
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1161
#endif
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1162
        }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1163
    } while ((cur_match = prev[cur_match & wmask]) > limit
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1164
             && --chain_length != 0);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1165
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1166
    if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1167
    return s->lookahead;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1168
}
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1169
#endif /* ASMV */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1170
#endif /* FASTEST */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1171
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1172
/* ---------------------------------------------------------------------------
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1173
 * Optimized version for level == 1 or strategy == Z_RLE only
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1174
 */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1175
local uInt longest_match_fast(s, cur_match)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1176
    deflate_state *s;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1177
    IPos cur_match;                             /* current match */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1178
{
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1179
    register Bytef *scan = s->window + s->strstart; /* current string */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1180
    register Bytef *match;                       /* matched string */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1181
    register int len;                           /* length of current match */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1182
    register Bytef *strend = s->window + s->strstart + MAX_MATCH;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1183
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1184
    /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1185
     * It is easy to get rid of this optimization if necessary.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1186
     */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1187
    Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1188
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1189
    Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1190
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1191
    Assert(cur_match < s->strstart, "no future");
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1192
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1193
    match = s->window + cur_match;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1194
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1195
    /* Return failure if the match length is less than 2:
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1196
     */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1197
    if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1198
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1199
    /* The check at best_len-1 can be removed because it will be made
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1200
     * again later. (This heuristic is not always a win.)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1201
     * It is not necessary to compare scan[2] and match[2] since they
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1202
     * are always equal when the other bytes match, given that
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1203
     * the hash keys are equal and that HASH_BITS >= 8.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1204
     */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1205
    scan += 2, match += 2;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1206
    Assert(*scan == *match, "match[2]?");
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1207
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1208
    /* We check for insufficient lookahead only every 8th comparison;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1209
     * the 256th check will be made at strstart+258.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1210
     */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1211
    do {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1212
    } while (*++scan == *++match && *++scan == *++match &&
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1213
             *++scan == *++match && *++scan == *++match &&
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1214
             *++scan == *++match && *++scan == *++match &&
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1215
             *++scan == *++match && *++scan == *++match &&
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1216
             scan < strend);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1217
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1218
    Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1219
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1220
    len = MAX_MATCH - (int)(strend - scan);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1221
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1222
    if (len < MIN_MATCH) return MIN_MATCH - 1;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1223
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1224
    s->match_start = cur_match;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1225
    return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1226
}
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1227
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1228
#ifdef DEBUG
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1229
/* ===========================================================================
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1230
 * Check that the match at match_start is indeed a match.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1231
 */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1232
local void check_match(s, start, match, length)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1233
    deflate_state *s;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1234
    IPos start, match;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1235
    int length;
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1236
{
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1237
    /* check that the match is indeed a match */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1238
    if (zmemcmp(s->window + match,
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1239
                s->window + start, length) != EQUAL) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1240
        fprintf(stderr, " start %u, match %u, length %d\n",
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1241
                start, match, length);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1242
        do {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1243
            fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1244
        } while (--length != 0);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1245
        z_error("invalid match");
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1246
    }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1247
    if (z_verbose > 1) {
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1248
        fprintf(stderr,"\\[%d,%d]", start-match, length);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1249
        do { putc(s->window[start++], stderr); } while (--length != 0);
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1250
    }
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1251
}
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1252
#else
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1253
#  define check_match(s, start, match, length)
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1254
#endif /* DEBUG */
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1255
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1256
/* ===========================================================================
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1257
 * Fill the window when the lookahead becomes insufficient.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1258
 * Updates strstart and lookahead.
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1259
 *
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1260
 * IN assertion: lookahead < MIN_LOOKAHEAD
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1261
 * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1262
 *    At least one byte has been read, or avail_in == 0; reads are
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1263
 *    performed for at least two bytes (required for the zip translate_eol
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1264
 *    option -- not supported here).
7b4677a8de6e Moved to zlib 1.2.3 proper.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1265
 */