annotate app/src/main/java/com/jcraft/jzlib/Deflate.java @ 465:7c8aebcc882a

request permissions if not already granted
author Carl Byington <carl@five-ten-sg.com>
date Mon, 19 Aug 2019 11:12:40 -0700
parents d29cce60f393
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1 /* -*-mode:java; c-basic-offset:2; -*- */
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
2 /*
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
3 Copyright (c) 2000-2011 ymnk, JCraft,Inc. All rights reserved.
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
4
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
5 Redistribution and use in source and binary forms, with or without
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
6 modification, are permitted provided that the following conditions are met:
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
7
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
8 1. Redistributions of source code must retain the above copyright notice,
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
9 this list of conditions and the following disclaimer.
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
10
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
11 2. Redistributions in binary form must reproduce the above copyright
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
12 notice, this list of conditions and the following disclaimer in
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
13 the documentation and/or other materials provided with the distribution.
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
14
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
15 3. The names of the authors may not be used to endorse or promote products
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
16 derived from this software without specific prior written permission.
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
17
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
18 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
19 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
20 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
21 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
22 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
28 */
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
29 /*
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
30 * This program is based on zlib-1.1.3, so all credit should go authors
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
31 * Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
32 * and contributors of zlib.
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
33 */
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
34
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
35 package com.jcraft.jzlib;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
36
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
37 public
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
38 final class Deflate implements Cloneable {
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
39
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
40 static final private int MAX_MEM_LEVEL=9;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
41
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
42 static final private int Z_DEFAULT_COMPRESSION=-1;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
43
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
44 static final private int MAX_WBITS=15; // 32K LZ77 window
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
45 static final private int DEF_MEM_LEVEL=8;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
46
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
47 static class Config{
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
48 int good_length; // reduce lazy search above this match length
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
49 int max_lazy; // do not perform lazy search above this match length
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
50 int nice_length; // quit search above this match length
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
51 int max_chain;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
52 int func;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
53 Config(int good_length, int max_lazy,
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
54 int nice_length, int max_chain, int func){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
55 this.good_length=good_length;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
56 this.max_lazy=max_lazy;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
57 this.nice_length=nice_length;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
58 this.max_chain=max_chain;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
59 this.func=func;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
60 }
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
61 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
62
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
63 static final private int STORED=0;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
64 static final private int FAST=1;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
65 static final private int SLOW=2;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
66 static final private Config[] config_table;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
67 static{
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
68 config_table=new Config[10];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
69 // good lazy nice chain
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
70 config_table[0]=new Config(0, 0, 0, 0, STORED);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
71 config_table[1]=new Config(4, 4, 8, 4, FAST);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
72 config_table[2]=new Config(4, 5, 16, 8, FAST);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
73 config_table[3]=new Config(4, 6, 32, 32, FAST);
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
74
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
75 config_table[4]=new Config(4, 4, 16, 16, SLOW);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
76 config_table[5]=new Config(8, 16, 32, 32, SLOW);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
77 config_table[6]=new Config(8, 16, 128, 128, SLOW);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
78 config_table[7]=new Config(8, 32, 128, 256, SLOW);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
79 config_table[8]=new Config(32, 128, 258, 1024, SLOW);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
80 config_table[9]=new Config(32, 258, 258, 4096, SLOW);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
81 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
82
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
83 static final private String[] z_errmsg = {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
84 "need dictionary", // Z_NEED_DICT 2
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
85 "stream end", // Z_STREAM_END 1
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
86 "", // Z_OK 0
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
87 "file error", // Z_ERRNO (-1)
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
88 "stream error", // Z_STREAM_ERROR (-2)
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
89 "data error", // Z_DATA_ERROR (-3)
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
90 "insufficient memory", // Z_MEM_ERROR (-4)
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
91 "buffer error", // Z_BUF_ERROR (-5)
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
92 "incompatible version",// Z_VERSION_ERROR (-6)
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
93 ""
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
94 };
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
95
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
96 // block not completed, need more input or more output
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
97 static final private int NeedMore=0;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
98
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
99 // block flush performed
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
100 static final private int BlockDone=1;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
101
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
102 // finish started, need only more output at next deflate
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
103 static final private int FinishStarted=2;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
104
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
105 // finish done, accept no more input or output
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
106 static final private int FinishDone=3;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
107
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
108 // preset dictionary flag in zlib header
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
109 static final private int PRESET_DICT=0x20;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
110
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
111 static final private int Z_FILTERED=1;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
112 static final private int Z_HUFFMAN_ONLY=2;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
113 static final private int Z_DEFAULT_STRATEGY=0;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
114
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
115 static final private int Z_NO_FLUSH=0;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
116 static final private int Z_PARTIAL_FLUSH=1;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
117 static final private int Z_SYNC_FLUSH=2;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
118 static final private int Z_FULL_FLUSH=3;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
119 static final private int Z_FINISH=4;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
120
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
121 static final private int Z_OK=0;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
122 static final private int Z_STREAM_END=1;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
123 static final private int Z_NEED_DICT=2;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
124 static final private int Z_ERRNO=-1;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
125 static final private int Z_STREAM_ERROR=-2;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
126 static final private int Z_DATA_ERROR=-3;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
127 static final private int Z_MEM_ERROR=-4;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
128 static final private int Z_BUF_ERROR=-5;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
129 static final private int Z_VERSION_ERROR=-6;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
130
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
131 static final private int INIT_STATE=42;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
132 static final private int BUSY_STATE=113;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
133 static final private int FINISH_STATE=666;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
134
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
135 // The deflate compression method
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
136 static final private int Z_DEFLATED=8;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
137
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
138 static final private int STORED_BLOCK=0;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
139 static final private int STATIC_TREES=1;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
140 static final private int DYN_TREES=2;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
141
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
142 // The three kinds of block type
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
143 static final private int Z_BINARY=0;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
144 static final private int Z_ASCII=1;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
145 static final private int Z_UNKNOWN=2;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
146
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
147 static final private int Buf_size=8*2;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
148
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
149 // repeat previous bit length 3-6 times (2 bits of repeat count)
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
150 static final private int REP_3_6=16;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
151
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
152 // repeat a zero length 3-10 times (3 bits of repeat count)
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
153 static final private int REPZ_3_10=17;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
154
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
155 // repeat a zero length 11-138 times (7 bits of repeat count)
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
156 static final private int REPZ_11_138=18;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
157
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
158 static final private int MIN_MATCH=3;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
159 static final private int MAX_MATCH=258;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
160 static final private int MIN_LOOKAHEAD=(MAX_MATCH+MIN_MATCH+1);
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
161
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
162 static final private int MAX_BITS=15;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
163 static final private int D_CODES=30;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
164 static final private int BL_CODES=19;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
165 static final private int LENGTH_CODES=29;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
166 static final private int LITERALS=256;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
167 static final private int L_CODES=(LITERALS+1+LENGTH_CODES);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
168 static final private int HEAP_SIZE=(2*L_CODES+1);
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
169
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
170 static final private int END_BLOCK=256;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
171
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
172 ZStream strm; // pointer back to this zlib stream
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
173 int status; // as the name implies
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
174 byte[] pending_buf; // output still pending
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
175 int pending_buf_size; // size of pending_buf
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
176 int pending_out; // next pending byte to output to the stream
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
177 int pending; // nb of bytes in the pending buffer
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
178 int wrap = 1;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
179 byte data_type; // UNKNOWN, BINARY or ASCII
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
180 byte method; // STORED (for zip only) or DEFLATED
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
181 int last_flush; // value of flush param for previous deflate call
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
182
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
183 int w_size; // LZ77 window size (32K by default)
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
184 int w_bits; // log2(w_size) (8..16)
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
185 int w_mask; // w_size - 1
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
186
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
187 byte[] window;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
188 // Sliding window. Input bytes are read into the second half of the window,
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
189 // and move to the first half later to keep a dictionary of at least wSize
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
190 // bytes. With this organization, matches are limited to a distance of
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
191 // wSize-MAX_MATCH bytes, but this ensures that IO is always
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
192 // performed with a length multiple of the block size. Also, it limits
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
193 // the window size to 64K, which is quite useful on MSDOS.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
194 // To do: use the user input buffer as sliding window.
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
195
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
196 int window_size;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
197 // Actual size of window: 2*wSize, except when the user input buffer
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
198 // is directly used as sliding window.
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
199
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
200 short[] prev;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
201 // Link to older string with same hash index. To limit the size of this
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
202 // array to 64K, this link is maintained only for the last 32K strings.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
203 // An index in this array is thus a window index modulo 32K.
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
204
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
205 short[] head; // Heads of the hash chains or NIL.
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
206
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
207 int ins_h; // hash index of string to be inserted
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
208 int hash_size; // number of elements in hash table
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
209 int hash_bits; // log2(hash_size)
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
210 int hash_mask; // hash_size-1
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
211
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
212 // Number of bits by which ins_h must be shifted at each input
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
213 // step. It must be such that after MIN_MATCH steps, the oldest
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
214 // byte no longer takes part in the hash key, that is:
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
215 // hash_shift * MIN_MATCH >= hash_bits
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
216 int hash_shift;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
217
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
218 // Window position at the beginning of the current output block. Gets
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
219 // negative when the window is moved backwards.
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
220
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
221 int block_start;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
222
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
223 int match_length; // length of best match
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
224 int prev_match; // previous match
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
225 int match_available; // set if previous match exists
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
226 int strstart; // start of string to insert
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
227 int match_start; // start of matching string
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
228 int lookahead; // number of valid bytes ahead in window
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
229
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
230 // Length of the best match at previous step. Matches not greater than this
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
231 // are discarded. This is used in the lazy match evaluation.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
232 int prev_length;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
233
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
234 // To speed up deflation, hash chains are never searched beyond this
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
235 // length. A higher limit improves compression ratio but degrades the speed.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
236 int max_chain_length;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
237
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
238 // Attempt to find a better match only when the current match is strictly
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
239 // smaller than this value. This mechanism is used only for compression
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
240 // levels >= 4.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
241 int max_lazy_match;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
242
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
243 // Insert new strings in the hash table only if the match length is not
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
244 // greater than this length. This saves time but degrades compression.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
245 // max_insert_length is used only for compression levels <= 3.
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
246
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
247 int level; // compression level (1..9)
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
248 int strategy; // favor or force Huffman coding
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
249
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
250 // Use a faster search when the previous match is longer than this
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
251 int good_match;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
252
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
253 // Stop searching when current match exceeds this
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
254 int nice_match;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
255
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
256 short[] dyn_ltree; // literal and length tree
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
257 short[] dyn_dtree; // distance tree
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
258 short[] bl_tree; // Huffman tree for bit lengths
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
259
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
260 Tree l_desc=new Tree(); // desc for literal tree
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
261 Tree d_desc=new Tree(); // desc for distance tree
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
262 Tree bl_desc=new Tree(); // desc for bit length tree
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
263
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
264 // number of codes at each bit length for an optimal tree
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
265 short[] bl_count=new short[MAX_BITS+1];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
266 // working area to be used in Tree#gen_codes()
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
267 short[] next_code=new short[MAX_BITS+1];
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
268
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
269 // heap used to build the Huffman trees
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
270 int[] heap=new int[2*L_CODES+1];
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
271
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
272 int heap_len; // number of elements in the heap
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
273 int heap_max; // element of largest frequency
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
274 // The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
275 // The same heap array is used to build all trees.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
276
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
277 // Depth of each subtree used as tie breaker for trees of equal frequency
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
278 byte[] depth=new byte[2*L_CODES+1];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
279
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
280 byte[] l_buf; // index for literals or lengths */
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
281
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
282 // Size of match buffer for literals/lengths. There are 4 reasons for
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
283 // limiting lit_bufsize to 64K:
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
284 // - frequencies can be kept in 16 bit counters
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
285 // - if compression is not successful for the first block, all input
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
286 // data is still in the window so we can still emit a stored block even
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
287 // when input comes from standard input. (This can also be done for
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
288 // all blocks if lit_bufsize is not greater than 32K.)
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
289 // - if compression is not successful for a file smaller than 64K, we can
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
290 // even emit a stored file instead of a stored block (saving 5 bytes).
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
291 // This is applicable only for zip (not gzip or zlib).
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
292 // - creating new Huffman trees less frequently may not provide fast
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
293 // adaptation to changes in the input data statistics. (Take for
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
294 // example a binary file with poorly compressible code followed by
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
295 // a highly compressible string table.) Smaller buffer sizes give
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
296 // fast adaptation but have of course the overhead of transmitting
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
297 // trees more frequently.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
298 // - I can't count above 4
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
299 int lit_bufsize;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
300
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
301 int last_lit; // running index in l_buf
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
302
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
303 // Buffer for distances. To simplify the code, d_buf and l_buf have
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
304 // the same number of elements. To use different lengths, an extra flag
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
305 // array would be necessary.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
306
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
307 int d_buf; // index of pendig_buf
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
308
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
309 int opt_len; // bit length of current block with optimal trees
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
310 int static_len; // bit length of current block with static trees
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
311 int matches; // number of string matches in current block
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
312 int last_eob_len; // bit length of EOB code for last block
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
313
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
314 // Output buffer. bits are inserted starting at the bottom (least
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
315 // significant bits).
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
316 short bi_buf;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
317
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
318 // Number of valid bits in bi_buf. All bits above the last valid bit
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
319 // are always zero.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
320 int bi_valid;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
321
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
322 GZIPHeader gheader = null;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
323
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
324 Deflate(ZStream strm){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
325 this.strm=strm;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
326 dyn_ltree=new short[HEAP_SIZE*2];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
327 dyn_dtree=new short[(2*D_CODES+1)*2]; // distance tree
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
328 bl_tree=new short[(2*BL_CODES+1)*2]; // Huffman tree for bit lengths
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
329 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
330
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
331 void lm_init() {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
332 window_size=2*w_size;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
333
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
334 head[hash_size-1]=0;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
335 for(int i=0; i<hash_size-1; i++){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
336 head[i]=0;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
337 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
338
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
339 // Set the default configuration parameters:
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
340 max_lazy_match = Deflate.config_table[level].max_lazy;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
341 good_match = Deflate.config_table[level].good_length;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
342 nice_match = Deflate.config_table[level].nice_length;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
343 max_chain_length = Deflate.config_table[level].max_chain;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
344
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
345 strstart = 0;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
346 block_start = 0;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
347 lookahead = 0;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
348 match_length = prev_length = MIN_MATCH-1;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
349 match_available = 0;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
350 ins_h = 0;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
351 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
352
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
353 // Initialize the tree data structures for a new zlib stream.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
354 void tr_init(){
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
355
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
356 l_desc.dyn_tree = dyn_ltree;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
357 l_desc.stat_desc = StaticTree.static_l_desc;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
358
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
359 d_desc.dyn_tree = dyn_dtree;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
360 d_desc.stat_desc = StaticTree.static_d_desc;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
361
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
362 bl_desc.dyn_tree = bl_tree;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
363 bl_desc.stat_desc = StaticTree.static_bl_desc;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
364
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
365 bi_buf = 0;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
366 bi_valid = 0;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
367 last_eob_len = 8; // enough lookahead for inflate
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
368
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
369 // Initialize the first block of the first file:
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
370 init_block();
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
371 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
372
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
373 void init_block(){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
374 // Initialize the trees.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
375 for(int i = 0; i < L_CODES; i++) dyn_ltree[i*2] = 0;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
376 for(int i= 0; i < D_CODES; i++) dyn_dtree[i*2] = 0;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
377 for(int i= 0; i < BL_CODES; i++) bl_tree[i*2] = 0;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
378
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
379 dyn_ltree[END_BLOCK*2] = 1;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
380 opt_len = static_len = 0;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
381 last_lit = matches = 0;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
382 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
383
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
384 // Restore the heap property by moving down the tree starting at node k,
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
385 // exchanging a node with the smallest of its two sons if necessary, stopping
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
386 // when the heap property is re-established (each father smaller than its
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
387 // two sons).
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
388 void pqdownheap(short[] tree, // the tree to restore
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
389 int k // node to move down
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
390 ){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
391 int v = heap[k];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
392 int j = k << 1; // left son of k
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
393 while (j <= heap_len) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
394 // Set j to the smallest of the two sons:
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
395 if (j < heap_len &&
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
396 smaller(tree, heap[j+1], heap[j], depth)){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
397 j++;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
398 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
399 // Exit if v is smaller than both sons
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
400 if(smaller(tree, v, heap[j], depth)) break;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
401
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
402 // Exchange v with the smallest son
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
403 heap[k]=heap[j]; k = j;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
404 // And continue down the tree, setting j to the left son of k
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
405 j <<= 1;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
406 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
407 heap[k] = v;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
408 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
409
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
410 static boolean smaller(short[] tree, int n, int m, byte[] depth){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
411 short tn2=tree[n*2];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
412 short tm2=tree[m*2];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
413 return (tn2<tm2 ||
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
414 (tn2==tm2 && depth[n] <= depth[m]));
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
415 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
416
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
417 // Scan a literal or distance tree to determine the frequencies of the codes
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
418 // in the bit length tree.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
419 void scan_tree (short[] tree,// the tree to be scanned
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
420 int max_code // and its largest code of non zero frequency
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
421 ){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
422 int n; // iterates over all tree elements
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
423 int prevlen = -1; // last emitted length
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
424 int curlen; // length of current code
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
425 int nextlen = tree[0*2+1]; // length of next code
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
426 int count = 0; // repeat count of the current code
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
427 int max_count = 7; // max repeat count
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
428 int min_count = 4; // min repeat count
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
429
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
430 if (nextlen == 0){ max_count = 138; min_count = 3; }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
431 tree[(max_code+1)*2+1] = (short)0xffff; // guard
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
432
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
433 for(n = 0; n <= max_code; n++) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
434 curlen = nextlen; nextlen = tree[(n+1)*2+1];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
435 if(++count < max_count && curlen == nextlen) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
436 continue;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
437 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
438 else if(count < min_count) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
439 bl_tree[curlen*2] += count;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
440 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
441 else if(curlen != 0) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
442 if(curlen != prevlen) bl_tree[curlen*2]++;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
443 bl_tree[REP_3_6*2]++;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
444 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
445 else if(count <= 10) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
446 bl_tree[REPZ_3_10*2]++;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
447 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
448 else{
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
449 bl_tree[REPZ_11_138*2]++;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
450 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
451 count = 0; prevlen = curlen;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
452 if(nextlen == 0) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
453 max_count = 138; min_count = 3;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
454 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
455 else if(curlen == nextlen) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
456 max_count = 6; min_count = 3;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
457 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
458 else{
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
459 max_count = 7; min_count = 4;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
460 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
461 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
462 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
463
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
464 // Construct the Huffman tree for the bit lengths and return the index in
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
465 // bl_order of the last bit length code to send.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
466 int build_bl_tree(){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
467 int max_blindex; // index of last bit length code of non zero freq
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
468
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
469 // Determine the bit length frequencies for literal and distance trees
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
470 scan_tree(dyn_ltree, l_desc.max_code);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
471 scan_tree(dyn_dtree, d_desc.max_code);
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
472
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
473 // Build the bit length tree:
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
474 bl_desc.build_tree(this);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
475 // opt_len now includes the length of the tree representations, except
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
476 // the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
477
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
478 // Determine the number of bit length codes to send. The pkzip format
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
479 // requires that at least 4 bit length codes be sent. (appnote.txt says
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
480 // 3 but the actual value used is 4.)
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
481 for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
482 if (bl_tree[Tree.bl_order[max_blindex]*2+1] != 0) break;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
483 }
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
484 // Update opt_len to include the bit length tree and counts
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
485 opt_len += 3*(max_blindex+1) + 5+5+4;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
486
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
487 return max_blindex;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
488 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
489
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
490
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
491 // Send the header for a block using dynamic Huffman trees: the counts, the
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
492 // lengths of the bit length codes, the literal tree and the distance tree.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
493 // IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
494 void send_all_trees(int lcodes, int dcodes, int blcodes){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
495 int rank; // index in bl_order
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
496
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
497 send_bits(lcodes-257, 5); // not +255 as stated in appnote.txt
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
498 send_bits(dcodes-1, 5);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
499 send_bits(blcodes-4, 4); // not -3 as stated in appnote.txt
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
500 for (rank = 0; rank < blcodes; rank++) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
501 send_bits(bl_tree[Tree.bl_order[rank]*2+1], 3);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
502 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
503 send_tree(dyn_ltree, lcodes-1); // literal tree
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
504 send_tree(dyn_dtree, dcodes-1); // distance tree
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
505 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
506
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
507 // Send a literal or distance tree in compressed form, using the codes in
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
508 // bl_tree.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
509 void send_tree (short[] tree,// the tree to be sent
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
510 int max_code // and its largest code of non zero frequency
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
511 ){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
512 int n; // iterates over all tree elements
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
513 int prevlen = -1; // last emitted length
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
514 int curlen; // length of current code
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
515 int nextlen = tree[0*2+1]; // length of next code
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
516 int count = 0; // repeat count of the current code
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
517 int max_count = 7; // max repeat count
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
518 int min_count = 4; // min repeat count
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
519
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
520 if (nextlen == 0){ max_count = 138; min_count = 3; }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
521
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
522 for (n = 0; n <= max_code; n++) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
523 curlen = nextlen; nextlen = tree[(n+1)*2+1];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
524 if(++count < max_count && curlen == nextlen) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
525 continue;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
526 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
527 else if(count < min_count) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
528 do { send_code(curlen, bl_tree); } while (--count != 0);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
529 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
530 else if(curlen != 0){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
531 if(curlen != prevlen){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
532 send_code(curlen, bl_tree); count--;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
533 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
534 send_code(REP_3_6, bl_tree);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
535 send_bits(count-3, 2);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
536 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
537 else if(count <= 10){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
538 send_code(REPZ_3_10, bl_tree);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
539 send_bits(count-3, 3);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
540 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
541 else{
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
542 send_code(REPZ_11_138, bl_tree);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
543 send_bits(count-11, 7);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
544 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
545 count = 0; prevlen = curlen;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
546 if(nextlen == 0){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
547 max_count = 138; min_count = 3;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
548 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
549 else if(curlen == nextlen){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
550 max_count = 6; min_count = 3;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
551 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
552 else{
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
553 max_count = 7; min_count = 4;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
554 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
555 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
556 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
557
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
558 // Output a byte on the stream.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
559 // IN assertion: there is enough room in pending_buf.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
560 final void put_byte(byte[] p, int start, int len){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
561 System.arraycopy(p, start, pending_buf, pending, len);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
562 pending+=len;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
563 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
564
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
565 final void put_byte(byte c){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
566 pending_buf[pending++]=c;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
567 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
568 final void put_short(int w) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
569 put_byte((byte)(w/*&0xff*/));
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
570 put_byte((byte)(w>>>8));
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
571 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
572 final void putShortMSB(int b){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
573 put_byte((byte)(b>>8));
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
574 put_byte((byte)(b/*&0xff*/));
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
575 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
576
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
577 final void send_code(int c, short[] tree){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
578 int c2=c*2;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
579 send_bits((tree[c2]&0xffff), (tree[c2+1]&0xffff));
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
580 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
581
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
582 void send_bits(int value, int length){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
583 int len = length;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
584 if (bi_valid > (int)Buf_size - len) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
585 int val = value;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
586 // bi_buf |= (val << bi_valid);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
587 bi_buf |= ((val << bi_valid)&0xffff);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
588 put_short(bi_buf);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
589 bi_buf = (short)(val >>> (Buf_size - bi_valid));
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
590 bi_valid += len - Buf_size;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
591 } else {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
592 // bi_buf |= (value) << bi_valid;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
593 bi_buf |= (((value) << bi_valid)&0xffff);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
594 bi_valid += len;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
595 }
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
596 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
597
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
598 // Send one empty static block to give enough lookahead for inflate.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
599 // This takes 10 bits, of which 7 may remain in the bit buffer.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
600 // The current inflate code requires 9 bits of lookahead. If the
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
601 // last two codes for the previous block (real code plus EOB) were coded
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
602 // on 5 bits or less, inflate may have only 5+3 bits of lookahead to decode
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
603 // the last real code. In this case we send two empty static blocks instead
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
604 // of one. (There are no problems if the previous block is stored or fixed.)
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
605 // To simplify the code, we assume the worst case of last real code encoded
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
606 // on one bit only.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
607 void _tr_align(){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
608 send_bits(STATIC_TREES<<1, 3);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
609 send_code(END_BLOCK, StaticTree.static_ltree);
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
610
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
611 bi_flush();
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
612
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
613 // Of the 10 bits for the empty block, we have already sent
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
614 // (10 - bi_valid) bits. The lookahead for the last real code (before
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
615 // the EOB of the previous block) was thus at least one plus the length
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
616 // of the EOB plus what we have just sent of the empty static block.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
617 if (1 + last_eob_len + 10 - bi_valid < 9) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
618 send_bits(STATIC_TREES<<1, 3);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
619 send_code(END_BLOCK, StaticTree.static_ltree);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
620 bi_flush();
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
621 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
622 last_eob_len = 7;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
623 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
624
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
625
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
626 // Save the match info and tally the frequency counts. Return true if
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
627 // the current block must be flushed.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
628 boolean _tr_tally (int dist, // distance of matched string
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
629 int lc // match length-MIN_MATCH or unmatched char (if dist==0)
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
630 ){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
631
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
632 pending_buf[d_buf+last_lit*2] = (byte)(dist>>>8);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
633 pending_buf[d_buf+last_lit*2+1] = (byte)dist;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
634
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
635 l_buf[last_lit] = (byte)lc; last_lit++;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
636
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
637 if (dist == 0) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
638 // lc is the unmatched char
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
639 dyn_ltree[lc*2]++;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
640 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
641 else {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
642 matches++;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
643 // Here, lc is the match length - MIN_MATCH
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
644 dist--; // dist = match distance - 1
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
645 dyn_ltree[(Tree._length_code[lc]+LITERALS+1)*2]++;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
646 dyn_dtree[Tree.d_code(dist)*2]++;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
647 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
648
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
649 if ((last_lit & 0x1fff) == 0 && level > 2) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
650 // Compute an upper bound for the compressed length
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
651 int out_length = last_lit*8;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
652 int in_length = strstart - block_start;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
653 int dcode;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
654 for (dcode = 0; dcode < D_CODES; dcode++) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
655 out_length += (int)dyn_dtree[dcode*2] *
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
656 (5L+Tree.extra_dbits[dcode]);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
657 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
658 out_length >>>= 3;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
659 if ((matches < (last_lit/2)) && out_length < in_length/2) return true;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
660 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
661
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
662 return (last_lit == lit_bufsize-1);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
663 // We avoid equality with lit_bufsize because of wraparound at 64K
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
664 // on 16 bit machines and because stored blocks are restricted to
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
665 // 64K-1 bytes.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
666 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
667
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
668 // Send the block data compressed using the given Huffman trees
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
669 void compress_block(short[] ltree, short[] dtree){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
670 int dist; // distance of matched string
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
671 int lc; // match length or unmatched char (if dist == 0)
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
672 int lx = 0; // running index in l_buf
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
673 int code; // the code to send
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
674 int extra; // number of extra bits to send
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
675
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
676 if (last_lit != 0){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
677 do{
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
678 dist=((pending_buf[d_buf+lx*2]<<8)&0xff00)|
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
679 (pending_buf[d_buf+lx*2+1]&0xff);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
680 lc=(l_buf[lx])&0xff; lx++;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
681
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
682 if(dist == 0){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
683 send_code(lc, ltree); // send a literal byte
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
684 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
685 else{
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
686 // Here, lc is the match length - MIN_MATCH
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
687 code = Tree._length_code[lc];
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
688
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
689 send_code(code+LITERALS+1, ltree); // send the length code
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
690 extra = Tree.extra_lbits[code];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
691 if(extra != 0){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
692 lc -= Tree.base_length[code];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
693 send_bits(lc, extra); // send the extra length bits
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
694 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
695 dist--; // dist is now the match distance - 1
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
696 code = Tree.d_code(dist);
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
697
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
698 send_code(code, dtree); // send the distance code
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
699 extra = Tree.extra_dbits[code];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
700 if (extra != 0) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
701 dist -= Tree.base_dist[code];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
702 send_bits(dist, extra); // send the extra distance bits
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
703 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
704 } // literal or match pair ?
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
705
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
706 // Check that the overlay between pending_buf and d_buf+l_buf is ok:
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
707 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
708 while (lx < last_lit);
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
709 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
710
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
711 send_code(END_BLOCK, ltree);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
712 last_eob_len = ltree[END_BLOCK*2+1];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
713 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
714
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
715 // Set the data type to ASCII or BINARY, using a crude approximation:
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
716 // binary if more than 20% of the bytes are <= 6 or >= 128, ascii otherwise.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
717 // IN assertion: the fields freq of dyn_ltree are set and the total of all
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
718 // frequencies does not exceed 64K (to fit in an int on 16 bit machines).
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
719 void set_data_type(){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
720 int n = 0;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
721 int ascii_freq = 0;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
722 int bin_freq = 0;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
723 while(n<7){ bin_freq += dyn_ltree[n*2]; n++;}
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
724 while(n<128){ ascii_freq += dyn_ltree[n*2]; n++;}
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
725 while(n<LITERALS){ bin_freq += dyn_ltree[n*2]; n++;}
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
726 data_type=(byte)(bin_freq > (ascii_freq >>> 2) ? Z_BINARY : Z_ASCII);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
727 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
728
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
729 // Flush the bit buffer, keeping at most 7 bits in it.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
730 void bi_flush(){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
731 if (bi_valid == 16) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
732 put_short(bi_buf);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
733 bi_buf=0;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
734 bi_valid=0;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
735 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
736 else if (bi_valid >= 8) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
737 put_byte((byte)bi_buf);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
738 bi_buf>>>=8;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
739 bi_valid-=8;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
740 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
741 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
742
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
743 // Flush the bit buffer and align the output on a byte boundary
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
744 void bi_windup(){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
745 if (bi_valid > 8) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
746 put_short(bi_buf);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
747 } else if (bi_valid > 0) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
748 put_byte((byte)bi_buf);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
749 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
750 bi_buf = 0;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
751 bi_valid = 0;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
752 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
753
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
754 // Copy a stored block, storing first the length and its
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
755 // one's complement if requested.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
756 void copy_block(int buf, // the input data
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
757 int len, // its length
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
758 boolean header // true if block header must be written
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
759 ){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
760 int index=0;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
761 bi_windup(); // align on byte boundary
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
762 last_eob_len = 8; // enough lookahead for inflate
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
763
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
764 if (header) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
765 put_short((short)len);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
766 put_short((short)~len);
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
767 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
768
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
769 // while(len--!=0) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
770 // put_byte(window[buf+index]);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
771 // index++;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
772 // }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
773 put_byte(window, buf, len);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
774 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
775
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
776 void flush_block_only(boolean eof){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
777 _tr_flush_block(block_start>=0 ? block_start : -1,
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
778 strstart-block_start,
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
779 eof);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
780 block_start=strstart;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
781 strm.flush_pending();
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
782 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
783
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
784 // Copy without compression as much as possible from the input stream, return
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
785 // the current block state.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
786 // This function does not insert new strings in the dictionary since
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
787 // uncompressible data is probably not useful. This function is used
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
788 // only for the level=0 compression option.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
789 // NOTE: this function should be optimized to avoid extra copying from
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
790 // window to pending_buf.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
791 int deflate_stored(int flush){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
792 // Stored blocks are limited to 0xffff bytes, pending_buf is limited
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
793 // to pending_buf_size, and each stored block has a 5 byte header:
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
794
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
795 int max_block_size = 0xffff;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
796 int max_start;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
797
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
798 if(max_block_size > pending_buf_size - 5) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
799 max_block_size = pending_buf_size - 5;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
800 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
801
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
802 // Copy as much as possible from input to output:
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
803 while(true){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
804 // Fill the window as much as possible:
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
805 if(lookahead<=1){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
806 fill_window();
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
807 if(lookahead==0 && flush==Z_NO_FLUSH) return NeedMore;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
808 if(lookahead==0) break; // flush the current block
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
809 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
810
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
811 strstart+=lookahead;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
812 lookahead=0;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
813
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
814 // Emit a stored block if pending_buf will be full:
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
815 max_start=block_start+max_block_size;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
816 if(strstart==0|| strstart>=max_start) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
817 // strstart == 0 is possible when wraparound on 16-bit machine
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
818 lookahead = (int)(strstart-max_start);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
819 strstart = (int)max_start;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
820
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
821 flush_block_only(false);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
822 if(strm.avail_out==0) return NeedMore;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
823
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
824 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
825
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
826 // Flush if we may have to slide, otherwise block_start may become
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
827 // negative and the data will be gone:
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
828 if(strstart-block_start >= w_size-MIN_LOOKAHEAD) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
829 flush_block_only(false);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
830 if(strm.avail_out==0) return NeedMore;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
831 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
832 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
833
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
834 flush_block_only(flush == Z_FINISH);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
835 if(strm.avail_out==0)
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
836 return (flush == Z_FINISH) ? FinishStarted : NeedMore;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
837
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
838 return flush == Z_FINISH ? FinishDone : BlockDone;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
839 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
840
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
841 // Send a stored block
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
842 void _tr_stored_block(int buf, // input block
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
843 int stored_len, // length of input block
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
844 boolean eof // true if this is the last block for a file
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
845 ){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
846 send_bits((STORED_BLOCK<<1)+(eof?1:0), 3); // send block type
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
847 copy_block(buf, stored_len, true); // with header
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
848 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
849
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
850 // Determine the best encoding for the current block: dynamic trees, static
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
851 // trees or store, and output the encoded block to the zip file.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
852 void _tr_flush_block(int buf, // input block, or NULL if too old
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
853 int stored_len, // length of input block
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
854 boolean eof // true if this is the last block for a file
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
855 ) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
856 int opt_lenb, static_lenb;// opt_len and static_len in bytes
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
857 int max_blindex = 0; // index of last bit length code of non zero freq
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
858
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
859 // Build the Huffman trees unless a stored block is forced
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
860 if(level > 0) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
861 // Check if the file is ascii or binary
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
862 if(data_type == Z_UNKNOWN) set_data_type();
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
863
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
864 // Construct the literal and distance trees
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
865 l_desc.build_tree(this);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
866
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
867 d_desc.build_tree(this);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
868
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
869 // At this point, opt_len and static_len are the total bit lengths of
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
870 // the compressed block data, excluding the tree representations.
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
871
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
872 // Build the bit length tree for the above two trees, and get the index
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
873 // in bl_order of the last bit length code to send.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
874 max_blindex=build_bl_tree();
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
875
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
876 // Determine the best encoding. Compute first the block length in bytes
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
877 opt_lenb=(opt_len+3+7)>>>3;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
878 static_lenb=(static_len+3+7)>>>3;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
879
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
880 if(static_lenb<=opt_lenb) opt_lenb=static_lenb;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
881 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
882 else {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
883 opt_lenb=static_lenb=stored_len+5; // force a stored block
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
884 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
885
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
886 if(stored_len+4<=opt_lenb && buf != -1){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
887 // 4: two words for the lengths
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
888 // The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
889 // Otherwise we can't have processed more than WSIZE input bytes since
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
890 // the last block flush, because compression would have been
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
891 // successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
892 // transform a block into a stored block.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
893 _tr_stored_block(buf, stored_len, eof);
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
894 }
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
895 else if(static_lenb == opt_lenb){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
896 send_bits((STATIC_TREES<<1)+(eof?1:0), 3);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
897 compress_block(StaticTree.static_ltree, StaticTree.static_dtree);
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
898 }
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
899 else{
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
900 send_bits((DYN_TREES<<1)+(eof?1:0), 3);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
901 send_all_trees(l_desc.max_code+1, d_desc.max_code+1, max_blindex+1);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
902 compress_block(dyn_ltree, dyn_dtree);
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
903 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
904
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
905 // The above check is made mod 2^32, for files larger than 512 MB
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
906 // and uLong implemented on 32 bits.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
907
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
908 init_block();
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
909
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
910 if(eof){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
911 bi_windup();
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
912 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
913 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
914
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
915 // Fill the window when the lookahead becomes insufficient.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
916 // Updates strstart and lookahead.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
917 //
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
918 // IN assertion: lookahead < MIN_LOOKAHEAD
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
919 // OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
920 // At least one byte has been read, or avail_in == 0; reads are
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
921 // performed for at least two bytes (required for the zip translate_eol
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
922 // option -- not supported here).
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
923 void fill_window(){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
924 int n, m;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
925 int p;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
926 int more; // Amount of free space at the end of the window.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
927
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
928 do{
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
929 more = (window_size-lookahead-strstart);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
930
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
931 // Deal with !@#$% 64K limit:
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
932 if(more==0 && strstart==0 && lookahead==0){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
933 more = w_size;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
934 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
935 else if(more==-1) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
936 // Very unlikely, but possible on 16 bit machine if strstart == 0
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
937 // and lookahead == 1 (input done one byte at time)
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
938 more--;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
939
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
940 // If the window is almost full and there is insufficient lookahead,
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
941 // move the upper half to the lower one to make room in the upper half.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
942 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
943 else if(strstart >= w_size+ w_size-MIN_LOOKAHEAD) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
944 System.arraycopy(window, w_size, window, 0, w_size);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
945 match_start-=w_size;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
946 strstart-=w_size; // we now have strstart >= MAX_DIST
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
947 block_start-=w_size;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
948
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
949 // Slide the hash table (could be avoided with 32 bit values
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
950 // at the expense of memory usage). We slide even when level == 0
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
951 // to keep the hash table consistent if we switch back to level > 0
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
952 // later. (Using level 0 permanently is not an optimal usage of
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
953 // zlib, so we don't care about this pathological case.)
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
954
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
955 n = hash_size;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
956 p=n;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
957 do {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
958 m = (head[--p]&0xffff);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
959 head[p]=(m>=w_size ? (short)(m-w_size) : 0);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
960 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
961 while (--n != 0);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
962
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
963 n = w_size;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
964 p = n;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
965 do {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
966 m = (prev[--p]&0xffff);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
967 prev[p] = (m >= w_size ? (short)(m-w_size) : 0);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
968 // If n is not on any hash chain, prev[n] is garbage but
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
969 // its value will never be used.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
970 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
971 while (--n!=0);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
972 more += w_size;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
973 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
974
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
975 if (strm.avail_in == 0) return;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
976
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
977 // If there was no sliding:
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
978 // strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
979 // more == window_size - lookahead - strstart
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
980 // => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
981 // => more >= window_size - 2*WSIZE + 2
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
982 // In the BIG_MEM or MMAP case (not yet supported),
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
983 // window_size == input_size + MIN_LOOKAHEAD &&
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
984 // strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
985 // Otherwise, window_size == 2*WSIZE so more >= 2.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
986 // If there was sliding, more >= WSIZE. So in all cases, more >= 2.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
987
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
988 n = strm.read_buf(window, strstart + lookahead, more);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
989 lookahead += n;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
990
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
991 // Initialize the hash value now that we have some input:
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
992 if(lookahead >= MIN_MATCH) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
993 ins_h = window[strstart]&0xff;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
994 ins_h=(((ins_h)<<hash_shift)^(window[strstart+1]&0xff))&hash_mask;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
995 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
996 // If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
997 // but this is not important since only literal bytes will be emitted.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
998 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
999 while (lookahead < MIN_LOOKAHEAD && strm.avail_in != 0);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1000 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1001
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1002 // Compress as much as possible from the input stream, return the current
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1003 // block state.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1004 // This function does not perform lazy evaluation of matches and inserts
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1005 // new strings in the dictionary only for unmatched strings or for short
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1006 // matches. It is used only for the fast compression options.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1007 int deflate_fast(int flush){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1008 // short hash_head = 0; // head of the hash chain
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1009 int hash_head = 0; // head of the hash chain
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1010 boolean bflush; // set if current block must be flushed
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1011
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1012 while(true){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1013 // Make sure that we always have enough lookahead, except
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1014 // at the end of the input file. We need MAX_MATCH bytes
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1015 // for the next match, plus MIN_MATCH bytes to insert the
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1016 // string following the next match.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1017 if(lookahead < MIN_LOOKAHEAD){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1018 fill_window();
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1019 if(lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1020 return NeedMore;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1021 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1022 if(lookahead == 0) break; // flush the current block
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1023 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1024
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1025 // Insert the string window[strstart .. strstart+2] in the
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1026 // dictionary, and set hash_head to the head of the hash chain:
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1027 if(lookahead >= MIN_MATCH){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1028 ins_h=(((ins_h)<<hash_shift)^(window[(strstart)+(MIN_MATCH-1)]&0xff))&hash_mask;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1029
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1030 // prev[strstart&w_mask]=hash_head=head[ins_h];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1031 hash_head=(head[ins_h]&0xffff);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1032 prev[strstart&w_mask]=head[ins_h];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1033 head[ins_h]=(short)strstart;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1034 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1035
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1036 // Find the longest match, discarding those <= prev_length.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1037 // At this point we have always match_length < MIN_MATCH
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1038
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1039 if(hash_head!=0L &&
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1040 ((strstart-hash_head)&0xffff) <= w_size-MIN_LOOKAHEAD
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1041 ){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1042 // To simplify the code, we prevent matches with the string
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1043 // of window index 0 (in particular we have to avoid a match
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1044 // of the string with itself at the start of the input file).
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1045 if(strategy != Z_HUFFMAN_ONLY){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1046 match_length=longest_match (hash_head);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1047 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1048 // longest_match() sets match_start
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1049 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1050 if(match_length>=MIN_MATCH){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1051 // check_match(strstart, match_start, match_length);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1052
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1053 bflush=_tr_tally(strstart-match_start, match_length-MIN_MATCH);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1054
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1055 lookahead -= match_length;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1056
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1057 // Insert new strings in the hash table only if the match length
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1058 // is not too large. This saves time but degrades compression.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1059 if(match_length <= max_lazy_match &&
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1060 lookahead >= MIN_MATCH) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1061 match_length--; // string at strstart already in hash table
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1062 do{
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1063 strstart++;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1064
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1065 ins_h=((ins_h<<hash_shift)^(window[(strstart)+(MIN_MATCH-1)]&0xff))&hash_mask;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1066 // prev[strstart&w_mask]=hash_head=head[ins_h];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1067 hash_head=(head[ins_h]&0xffff);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1068 prev[strstart&w_mask]=head[ins_h];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1069 head[ins_h]=(short)strstart;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1070
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1071 // strstart never exceeds WSIZE-MAX_MATCH, so there are
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1072 // always MIN_MATCH bytes ahead.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1073 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1074 while (--match_length != 0);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1075 strstart++;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1076 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1077 else{
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1078 strstart += match_length;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1079 match_length = 0;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1080 ins_h = window[strstart]&0xff;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1081
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1082 ins_h=(((ins_h)<<hash_shift)^(window[strstart+1]&0xff))&hash_mask;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1083 // If lookahead < MIN_MATCH, ins_h is garbage, but it does not
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1084 // matter since it will be recomputed at next deflate call.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1085 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1086 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1087 else {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1088 // No match, output a literal byte
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1089
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1090 bflush=_tr_tally(0, window[strstart]&0xff);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1091 lookahead--;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1092 strstart++;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1093 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1094 if (bflush){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1095
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1096 flush_block_only(false);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1097 if(strm.avail_out==0) return NeedMore;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1098 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1099 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1100
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1101 flush_block_only(flush == Z_FINISH);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1102 if(strm.avail_out==0){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1103 if(flush == Z_FINISH) return FinishStarted;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1104 else return NeedMore;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1105 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1106 return flush==Z_FINISH ? FinishDone : BlockDone;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1107 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1108
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1109 // Same as above, but achieves better compression. We use a lazy
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1110 // evaluation for matches: a match is finally adopted only if there is
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1111 // no better match at the next window position.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1112 int deflate_slow(int flush){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1113 // short hash_head = 0; // head of hash chain
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1114 int hash_head = 0; // head of hash chain
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1115 boolean bflush; // set if current block must be flushed
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1116
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1117 // Process the input block.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1118 while(true){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1119 // Make sure that we always have enough lookahead, except
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1120 // at the end of the input file. We need MAX_MATCH bytes
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1121 // for the next match, plus MIN_MATCH bytes to insert the
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1122 // string following the next match.
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1123
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1124 if (lookahead < MIN_LOOKAHEAD) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1125 fill_window();
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1126 if(lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1127 return NeedMore;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1128 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1129 if(lookahead == 0) break; // flush the current block
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1130 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1131
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1132 // Insert the string window[strstart .. strstart+2] in the
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1133 // dictionary, and set hash_head to the head of the hash chain:
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1134
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1135 if(lookahead >= MIN_MATCH) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1136 ins_h=(((ins_h)<<hash_shift)^(window[(strstart)+(MIN_MATCH-1)]&0xff)) & hash_mask;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1137 // prev[strstart&w_mask]=hash_head=head[ins_h];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1138 hash_head=(head[ins_h]&0xffff);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1139 prev[strstart&w_mask]=head[ins_h];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1140 head[ins_h]=(short)strstart;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1141 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1142
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1143 // Find the longest match, discarding those <= prev_length.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1144 prev_length = match_length; prev_match = match_start;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1145 match_length = MIN_MATCH-1;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1146
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1147 if (hash_head != 0 && prev_length < max_lazy_match &&
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1148 ((strstart-hash_head)&0xffff) <= w_size-MIN_LOOKAHEAD
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1149 ){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1150 // To simplify the code, we prevent matches with the string
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1151 // of window index 0 (in particular we have to avoid a match
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1152 // of the string with itself at the start of the input file).
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1153
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1154 if(strategy != Z_HUFFMAN_ONLY) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1155 match_length = longest_match(hash_head);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1156 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1157 // longest_match() sets match_start
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1158
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1159 if (match_length <= 5 && (strategy == Z_FILTERED ||
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1160 (match_length == MIN_MATCH &&
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1161 strstart - match_start > 4096))) {
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1162
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1163 // If prev_match is also MIN_MATCH, match_start is garbage
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1164 // but we will ignore the current match anyway.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1165 match_length = MIN_MATCH-1;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1166 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1167 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1168
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1169 // If there was a match at the previous step and the current
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1170 // match is not better, output the previous match:
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1171 if(prev_length >= MIN_MATCH && match_length <= prev_length) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1172 int max_insert = strstart + lookahead - MIN_MATCH;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1173 // Do not insert strings in hash table beyond this.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1174
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1175 // check_match(strstart-1, prev_match, prev_length);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1176
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1177 bflush=_tr_tally(strstart-1-prev_match, prev_length - MIN_MATCH);
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1178
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1179 // Insert in hash table all strings up to the end of the match.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1180 // strstart-1 and strstart are already inserted. If there is not
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1181 // enough lookahead, the last two strings are not inserted in
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1182 // the hash table.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1183 lookahead -= prev_length-1;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1184 prev_length -= 2;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1185 do{
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1186 if(++strstart <= max_insert) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1187 ins_h=(((ins_h)<<hash_shift)^(window[(strstart)+(MIN_MATCH-1)]&0xff))&hash_mask;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1188 //prev[strstart&w_mask]=hash_head=head[ins_h];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1189 hash_head=(head[ins_h]&0xffff);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1190 prev[strstart&w_mask]=head[ins_h];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1191 head[ins_h]=(short)strstart;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1192 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1193 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1194 while(--prev_length != 0);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1195 match_available = 0;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1196 match_length = MIN_MATCH-1;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1197 strstart++;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1198
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1199 if (bflush){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1200 flush_block_only(false);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1201 if(strm.avail_out==0) return NeedMore;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1202 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1203 } else if (match_available!=0) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1204
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1205 // If there was no match at the previous position, output a
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1206 // single literal. If there was a match but the current match
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1207 // is longer, truncate the previous match to a single literal.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1208
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1209 bflush=_tr_tally(0, window[strstart-1]&0xff);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1210
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1211 if (bflush) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1212 flush_block_only(false);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1213 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1214 strstart++;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1215 lookahead--;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1216 if(strm.avail_out == 0) return NeedMore;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1217 } else {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1218 // There is no previous match to compare with, wait for
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1219 // the next step to decide.
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1220
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1221 match_available = 1;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1222 strstart++;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1223 lookahead--;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1224 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1225 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1226
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1227 if(match_available!=0) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1228 bflush=_tr_tally(0, window[strstart-1]&0xff);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1229 match_available = 0;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1230 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1231 flush_block_only(flush == Z_FINISH);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1232
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1233 if(strm.avail_out==0){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1234 if(flush == Z_FINISH) return FinishStarted;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1235 else return NeedMore;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1236 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1237
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1238 return flush == Z_FINISH ? FinishDone : BlockDone;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1239 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1240
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1241 int longest_match(int cur_match){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1242 int chain_length = max_chain_length; // max hash chain length
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1243 int scan = strstart; // current string
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1244 int match; // matched string
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1245 int len; // length of current match
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1246 int best_len = prev_length; // best match length so far
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1247 int limit = strstart>(w_size-MIN_LOOKAHEAD) ?
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1248 strstart-(w_size-MIN_LOOKAHEAD) : 0;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1249 int nice_match=this.nice_match;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1250
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1251 // Stop when cur_match becomes <= limit. To simplify the code,
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1252 // we prevent matches with the string of window index 0.
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1253
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1254 int wmask = w_mask;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1255
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1256 int strend = strstart + MAX_MATCH;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1257 byte scan_end1 = window[scan+best_len-1];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1258 byte scan_end = window[scan+best_len];
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1259
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1260 // The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1261 // It is easy to get rid of this optimization if necessary.
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1262
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1263 // Do not waste too much time if we already have a good match:
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1264 if (prev_length >= good_match) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1265 chain_length >>= 2;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1266 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1267
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1268 // Do not look for matches beyond the end of the input. This is necessary
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1269 // to make deflate deterministic.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1270 if (nice_match > lookahead) nice_match = lookahead;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1271
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1272 do {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1273 match = cur_match;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1274
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1275 // Skip to next match if the match length cannot increase
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1276 // or if the match length is less than 2:
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1277 if (window[match+best_len] != scan_end ||
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1278 window[match+best_len-1] != scan_end1 ||
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1279 window[match] != window[scan] ||
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1280 window[++match] != window[scan+1]) continue;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1281
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1282 // The check at best_len-1 can be removed because it will be made
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1283 // again later. (This heuristic is not always a win.)
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1284 // It is not necessary to compare scan[2] and match[2] since they
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1285 // are always equal when the other bytes match, given that
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1286 // the hash keys are equal and that HASH_BITS >= 8.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1287 scan += 2; match++;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1288
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1289 // We check for insufficient lookahead only every 8th comparison;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1290 // the 256th check will be made at strstart+258.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1291 do {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1292 } while (window[++scan] == window[++match] &&
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1293 window[++scan] == window[++match] &&
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1294 window[++scan] == window[++match] &&
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1295 window[++scan] == window[++match] &&
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1296 window[++scan] == window[++match] &&
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1297 window[++scan] == window[++match] &&
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1298 window[++scan] == window[++match] &&
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1299 window[++scan] == window[++match] &&
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1300 scan < strend);
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1301
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1302 len = MAX_MATCH - (int)(strend - scan);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1303 scan = strend - MAX_MATCH;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1304
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1305 if(len>best_len) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1306 match_start = cur_match;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1307 best_len = len;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1308 if (len >= nice_match) break;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1309 scan_end1 = window[scan+best_len-1];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1310 scan_end = window[scan+best_len];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1311 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1312
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1313 } while ((cur_match = (prev[cur_match & wmask]&0xffff)) > limit
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1314 && --chain_length != 0);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1315
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1316 if (best_len <= lookahead) return best_len;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1317 return lookahead;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1318 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1319
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1320 int deflateInit(int level, int bits, int memlevel){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1321 return deflateInit(level, Z_DEFLATED, bits, memlevel,
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1322 Z_DEFAULT_STRATEGY);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1323 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1324
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1325 int deflateInit(int level, int bits){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1326 return deflateInit(level, Z_DEFLATED, bits, DEF_MEM_LEVEL,
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1327 Z_DEFAULT_STRATEGY);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1328 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1329 int deflateInit(int level){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1330 return deflateInit(level, MAX_WBITS);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1331 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1332 private int deflateInit(int level, int method, int windowBits,
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1333 int memLevel, int strategy){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1334 int wrap = 1;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1335 // byte[] my_version=ZLIB_VERSION;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1336
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1337 //
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1338 // if (version == null || version[0] != my_version[0]
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1339 // || stream_size != sizeof(z_stream)) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1340 // return Z_VERSION_ERROR;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1341 // }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1342
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1343 strm.msg = null;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1344
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1345 if (level == Z_DEFAULT_COMPRESSION) level = 6;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1346
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1347 if (windowBits < 0) { // undocumented feature: suppress zlib header
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1348 wrap = 0;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1349 windowBits = -windowBits;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1350 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1351 else if(windowBits > 15){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1352 wrap = 2;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1353 windowBits -= 16;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1354 strm.adler=new CRC32();
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1355 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1356
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1357 if (memLevel < 1 || memLevel > MAX_MEM_LEVEL ||
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1358 method != Z_DEFLATED ||
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1359 windowBits < 9 || windowBits > 15 || level < 0 || level > 9 ||
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1360 strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1361 return Z_STREAM_ERROR;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1362 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1363
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1364 strm.dstate = (Deflate)this;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1365
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1366 this.wrap = wrap;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1367 w_bits = windowBits;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1368 w_size = 1 << w_bits;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1369 w_mask = w_size - 1;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1370
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1371 hash_bits = memLevel + 7;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1372 hash_size = 1 << hash_bits;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1373 hash_mask = hash_size - 1;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1374 hash_shift = ((hash_bits+MIN_MATCH-1)/MIN_MATCH);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1375
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1376 window = new byte[w_size*2];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1377 prev = new short[w_size];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1378 head = new short[hash_size];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1379
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1380 lit_bufsize = 1 << (memLevel + 6); // 16K elements by default
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1381
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1382 // We overlay pending_buf and d_buf+l_buf. This works since the average
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1383 // output size for (length,distance) codes is <= 24 bits.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1384 pending_buf = new byte[lit_bufsize*3];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1385 pending_buf_size = lit_bufsize*3;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1386
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1387 d_buf = lit_bufsize;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1388 l_buf = new byte[lit_bufsize];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1389
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1390 this.level = level;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1391
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1392 this.strategy = strategy;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1393 this.method = (byte)method;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1394
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1395 return deflateReset();
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1396 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1397
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1398 int deflateReset(){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1399 strm.total_in = strm.total_out = 0;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1400 strm.msg = null; //
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1401 strm.data_type = Z_UNKNOWN;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1402
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1403 pending = 0;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1404 pending_out = 0;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1405
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1406 if(wrap < 0){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1407 wrap = -wrap;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1408 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1409 status = (wrap==0) ? BUSY_STATE : INIT_STATE;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1410 strm.adler.reset();
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1411
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1412 last_flush = Z_NO_FLUSH;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1413
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1414 tr_init();
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1415 lm_init();
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1416 return Z_OK;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1417 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1418
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1419 int deflateEnd(){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1420 if(status!=INIT_STATE && status!=BUSY_STATE && status!=FINISH_STATE){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1421 return Z_STREAM_ERROR;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1422 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1423 // Deallocate in reverse order of allocations:
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1424 pending_buf=null;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1425 l_buf=null;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1426 head=null;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1427 prev=null;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1428 window=null;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1429 // free
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1430 // dstate=null;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1431 return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1432 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1433
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1434 int deflateParams(int _level, int _strategy){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1435 int err=Z_OK;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1436
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1437 if(_level == Z_DEFAULT_COMPRESSION){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1438 _level = 6;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1439 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1440 if(_level < 0 || _level > 9 ||
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1441 _strategy < 0 || _strategy > Z_HUFFMAN_ONLY) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1442 return Z_STREAM_ERROR;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1443 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1444
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1445 if(config_table[level].func!=config_table[_level].func &&
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1446 strm.total_in != 0) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1447 // Flush the last buffer:
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1448 err = strm.deflate(Z_PARTIAL_FLUSH);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1449 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1450
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1451 if(level != _level) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1452 level = _level;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1453 max_lazy_match = config_table[level].max_lazy;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1454 good_match = config_table[level].good_length;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1455 nice_match = config_table[level].nice_length;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1456 max_chain_length = config_table[level].max_chain;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1457 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1458 strategy = _strategy;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1459 return err;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1460 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1461
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1462 int deflateSetDictionary (byte[] dictionary, int dictLength){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1463 int length = dictLength;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1464 int index=0;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1465
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1466 if(dictionary == null || status != INIT_STATE)
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1467 return Z_STREAM_ERROR;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1468
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1469 strm.adler.update(dictionary, 0, dictLength);
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1470
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1471 if(length < MIN_MATCH) return Z_OK;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1472 if(length > w_size-MIN_LOOKAHEAD){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1473 length = w_size-MIN_LOOKAHEAD;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1474 index=dictLength-length; // use the tail of the dictionary
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1475 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1476 System.arraycopy(dictionary, index, window, 0, length);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1477 strstart = length;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1478 block_start = length;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1479
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1480 // Insert all strings in the hash table (except for the last two bytes).
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1481 // s->lookahead stays null, so s->ins_h will be recomputed at the next
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1482 // call of fill_window.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1483
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1484 ins_h = window[0]&0xff;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1485 ins_h=(((ins_h)<<hash_shift)^(window[1]&0xff))&hash_mask;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1486
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1487 for(int n=0; n<=length-MIN_MATCH; n++){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1488 ins_h=(((ins_h)<<hash_shift)^(window[(n)+(MIN_MATCH-1)]&0xff))&hash_mask;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1489 prev[n&w_mask]=head[ins_h];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1490 head[ins_h]=(short)n;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1491 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1492 return Z_OK;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1493 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1494
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1495 int deflate(int flush){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1496 int old_flush;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1497
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1498 if(flush>Z_FINISH || flush<0){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1499 return Z_STREAM_ERROR;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1500 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1501
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1502 if(strm.next_out == null ||
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1503 (strm.next_in == null && strm.avail_in != 0) ||
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1504 (status == FINISH_STATE && flush != Z_FINISH)) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1505 strm.msg=z_errmsg[Z_NEED_DICT-(Z_STREAM_ERROR)];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1506 return Z_STREAM_ERROR;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1507 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1508 if(strm.avail_out == 0){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1509 strm.msg=z_errmsg[Z_NEED_DICT-(Z_BUF_ERROR)];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1510 return Z_BUF_ERROR;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1511 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1512
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1513 old_flush = last_flush;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1514 last_flush = flush;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1515
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1516 // Write the zlib header
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1517 if(status == INIT_STATE) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1518 if(wrap == 2){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1519 getGZIPHeader().put(this);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1520 status=BUSY_STATE;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1521 strm.adler.reset();
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1522 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1523 else{
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1524 int header = (Z_DEFLATED+((w_bits-8)<<4))<<8;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1525 int level_flags=((level-1)&0xff)>>1;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1526
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1527 if(level_flags>3) level_flags=3;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1528 header |= (level_flags<<6);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1529 if(strstart!=0) header |= PRESET_DICT;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1530 header+=31-(header % 31);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1531
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1532 status=BUSY_STATE;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1533 putShortMSB(header);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1534
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1535
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1536 // Save the adler32 of the preset dictionary:
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1537 if(strstart!=0){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1538 long adler=strm.adler.getValue();
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1539 putShortMSB((int)(adler>>>16));
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1540 putShortMSB((int)(adler&0xffff));
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1541 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1542 strm.adler.reset();
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1543 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1544 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1545
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1546 // Flush as much pending output as possible
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1547 if(pending != 0) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1548 strm.flush_pending();
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1549 if(strm.avail_out == 0) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1550 // Since avail_out is 0, deflate will be called again with
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1551 // more output space, but possibly with both pending and
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1552 // avail_in equal to zero. There won't be anything to do,
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1553 // but this is not an error situation so make sure we
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1554 // return OK instead of BUF_ERROR at next call of deflate:
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1555 last_flush = -1;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1556 return Z_OK;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1557 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1558
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1559 // Make sure there is something to do and avoid duplicate consecutive
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1560 // flushes. For repeated and useless calls with Z_FINISH, we keep
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1561 // returning Z_STREAM_END instead of Z_BUFF_ERROR.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1562 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1563 else if(strm.avail_in==0 && flush <= old_flush &&
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1564 flush != Z_FINISH) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1565 strm.msg=z_errmsg[Z_NEED_DICT-(Z_BUF_ERROR)];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1566 return Z_BUF_ERROR;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1567 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1568
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1569 // User must not provide more input after the first FINISH:
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1570 if(status == FINISH_STATE && strm.avail_in != 0) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1571 strm.msg=z_errmsg[Z_NEED_DICT-(Z_BUF_ERROR)];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1572 return Z_BUF_ERROR;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1573 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1574
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1575 // Start a new block or continue the current one.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1576 if(strm.avail_in!=0 || lookahead!=0 ||
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1577 (flush != Z_NO_FLUSH && status != FINISH_STATE)) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1578 int bstate=-1;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1579 switch(config_table[level].func){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1580 case STORED:
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1581 bstate = deflate_stored(flush);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1582 break;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1583 case FAST:
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1584 bstate = deflate_fast(flush);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1585 break;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1586 case SLOW:
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1587 bstate = deflate_slow(flush);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1588 break;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1589 default:
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1590 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1591
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1592 if (bstate==FinishStarted || bstate==FinishDone) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1593 status = FINISH_STATE;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1594 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1595 if (bstate==NeedMore || bstate==FinishStarted) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1596 if(strm.avail_out == 0) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1597 last_flush = -1; // avoid BUF_ERROR next call, see above
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1598 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1599 return Z_OK;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1600 // If flush != Z_NO_FLUSH && avail_out == 0, the next call
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1601 // of deflate should use the same flush parameter to make sure
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1602 // that the flush is complete. So we don't have to output an
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1603 // empty block here, this will be done at next call. This also
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1604 // ensures that for a very small output buffer, we emit at most
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1605 // one empty block.
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1606 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1607
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1608 if (bstate==BlockDone) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1609 if(flush == Z_PARTIAL_FLUSH) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1610 _tr_align();
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1611 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1612 else { // FULL_FLUSH or SYNC_FLUSH
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1613 _tr_stored_block(0, 0, false);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1614 // For a full flush, this empty block will be recognized
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1615 // as a special marker by inflate_sync().
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1616 if(flush == Z_FULL_FLUSH) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1617 //state.head[s.hash_size-1]=0;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1618 for(int i=0; i<hash_size/*-1*/; i++) // forget history
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1619 head[i]=0;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1620 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1621 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1622 strm.flush_pending();
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1623 if(strm.avail_out == 0) {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1624 last_flush = -1; // avoid BUF_ERROR at next call, see above
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1625 return Z_OK;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1626 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1627 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1628 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1629
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1630 if(flush!=Z_FINISH) return Z_OK;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1631 if(wrap<=0) return Z_STREAM_END;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1632
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1633 if(wrap==2){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1634 long adler=strm.adler.getValue();
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1635 put_byte((byte)(adler&0xff));
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1636 put_byte((byte)((adler>>8)&0xff));
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1637 put_byte((byte)((adler>>16)&0xff));
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1638 put_byte((byte)((adler>>24)&0xff));
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1639 put_byte((byte)(strm.total_in&0xff));
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1640 put_byte((byte)((strm.total_in>>8)&0xff));
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1641 put_byte((byte)((strm.total_in>>16)&0xff));
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1642 put_byte((byte)((strm.total_in>>24)&0xff));
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1643
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1644 getGZIPHeader().setCRC(adler);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1645 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1646 else{
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1647 // Write the zlib trailer (adler32)
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1648 long adler=strm.adler.getValue();
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1649 putShortMSB((int)(adler>>>16));
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1650 putShortMSB((int)(adler&0xffff));
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1651 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1652
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1653 strm.flush_pending();
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1654
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1655 // If avail_out is zero, the application will call deflate again
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1656 // to flush the rest.
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1657
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1658 if(wrap > 0) wrap = -wrap; // write the trailer only once!
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1659 return pending != 0 ? Z_OK : Z_STREAM_END;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1660 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1661
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1662 static int deflateCopy(ZStream dest, ZStream src){
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1663
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1664 if(src.dstate == null){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1665 return Z_STREAM_ERROR;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1666 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1667
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1668 if(src.next_in!=null){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1669 dest.next_in = new byte[src.next_in.length];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1670 System.arraycopy(src.next_in, 0, dest.next_in, 0, src.next_in.length);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1671 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1672 dest.next_in_index = src.next_in_index;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1673 dest.avail_in = src.avail_in;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1674 dest.total_in = src.total_in;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1675
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1676 if(src.next_out!=null){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1677 dest.next_out = new byte[src.next_out.length];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1678 System.arraycopy(src.next_out, 0, dest.next_out ,0 , src.next_out.length);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1679 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1680
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1681 dest.next_out_index = src.next_out_index;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1682 dest.avail_out = src.avail_out;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1683 dest.total_out = src.total_out;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1684
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1685 dest.msg = src.msg;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1686 dest.data_type = src.data_type;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1687 dest.adler = src.adler.copy();
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1688
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1689 try{
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1690 dest.dstate = (Deflate)src.dstate.clone();
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1691 dest.dstate.strm = dest;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1692 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1693 catch(CloneNotSupportedException e){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1694 //
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1695 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1696 return Z_OK;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1697 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1698
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1699 public Object clone() throws CloneNotSupportedException {
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1700 Deflate dest = (Deflate)super.clone();
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1701
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1702 dest.pending_buf = dup(dest.pending_buf);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1703 dest.d_buf = dest.d_buf;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1704 dest.l_buf = dup(dest.l_buf);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1705 dest.window = dup(dest.window);
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1706
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1707 dest.prev = dup(dest.prev);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1708 dest.head = dup(dest.head);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1709 dest.dyn_ltree = dup(dest.dyn_ltree);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1710 dest.dyn_dtree = dup(dest.dyn_dtree);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1711 dest.bl_tree = dup(dest.bl_tree);
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1712
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1713 dest.bl_count = dup(dest.bl_count);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1714 dest.next_code = dup(dest.next_code);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1715 dest.heap = dup(dest.heap);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1716 dest.depth = dup(dest.depth);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1717
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1718 dest.l_desc.dyn_tree = dest.dyn_ltree;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1719 dest.d_desc.dyn_tree = dest.dyn_dtree;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1720 dest.bl_desc.dyn_tree = dest.bl_tree;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1721
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1722 /*
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1723 dest.l_desc.stat_desc = StaticTree.static_l_desc;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1724 dest.d_desc.stat_desc = StaticTree.static_d_desc;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1725 dest.bl_desc.stat_desc = StaticTree.static_bl_desc;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1726 */
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1727
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1728 if(dest.gheader!=null){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1729 dest.gheader = (GZIPHeader)dest.gheader.clone();
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1730 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1731
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1732 return dest;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1733 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1734
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1735 private byte[] dup(byte[] buf){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1736 byte[] foo = new byte[buf.length];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1737 System.arraycopy(buf, 0, foo, 0, foo.length);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1738 return foo;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1739 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1740 private short[] dup(short[] buf){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1741 short[] foo = new short[buf.length];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1742 System.arraycopy(buf, 0, foo, 0, foo.length);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1743 return foo;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1744 }
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1745 private int[] dup(int[] buf){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1746 int[] foo = new int[buf.length];
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1747 System.arraycopy(buf, 0, foo, 0, foo.length);
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1748 return foo;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1749 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1750
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1751 synchronized GZIPHeader getGZIPHeader(){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1752 if(gheader==null){
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1753 gheader = new GZIPHeader();
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1754 }
357
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1755 return gheader;
46c2115ae1c8 update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
1756 }
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1757 }