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