zopfli 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,77 @@
1
+ /*
2
+ Copyright 2011 Google Inc. All Rights Reserved.
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+
16
+ Author: lode.vandevenne@gmail.com (Lode Vandevenne)
17
+ Author: jyrki.alakuijala@gmail.com (Jyrki Alakuijala)
18
+ */
19
+
20
+ /*
21
+ Functions to choose good boundaries for block splitting. Deflate allows encoding
22
+ the data in multiple blocks, with a separate Huffman tree for each block. The
23
+ Huffman tree itself requires some bytes to encode, so by choosing certain
24
+ blocks, you can either hurt, or enhance compression. These functions choose good
25
+ ones that enhance it.
26
+ */
27
+
28
+ #ifndef ZOPFLI_BLOCKSPLITTER_H_
29
+ #define ZOPFLI_BLOCKSPLITTER_H_
30
+
31
+ #include <stdlib.h>
32
+
33
+ #include "zopfli.h"
34
+
35
+
36
+ /*
37
+ Does blocksplitting on LZ77 data.
38
+ The output splitpoints are indices in the LZ77 data.
39
+ litlens: lz77 lit/lengths
40
+ dists: lz77 distances
41
+ llsize: size of litlens and dists
42
+ maxblocks: set a limit to the amount of blocks. Set to 0 to mean no limit.
43
+ */
44
+ void ZopfliBlockSplitLZ77(const ZopfliOptions* options,
45
+ const unsigned short* litlens,
46
+ const unsigned short* dists,
47
+ size_t llsize, size_t maxblocks,
48
+ size_t** splitpoints, size_t* npoints);
49
+
50
+ /*
51
+ Does blocksplitting on uncompressed data.
52
+ The output splitpoints are indices in the uncompressed bytes.
53
+
54
+ options: general program options.
55
+ in: uncompressed input data
56
+ instart: where to start splitting
57
+ inend: where to end splitting (not inclusive)
58
+ maxblocks: maximum amount of blocks to split into, or 0 for no limit
59
+ splitpoints: dynamic array to put the resulting split point coordinates into.
60
+ The coordinates are indices in the input array.
61
+ npoints: pointer to amount of splitpoints, for the dynamic array. The amount of
62
+ blocks is the amount of splitpoitns + 1.
63
+ */
64
+ void ZopfliBlockSplit(const ZopfliOptions* options,
65
+ const unsigned char* in, size_t instart, size_t inend,
66
+ size_t maxblocks, size_t** splitpoints, size_t* npoints);
67
+
68
+ /*
69
+ Divides the input into equal blocks, does not even take LZ77 lengths into
70
+ account.
71
+ */
72
+ void ZopfliBlockSplitSimple(const unsigned char* in,
73
+ size_t instart, size_t inend,
74
+ size_t blocksize,
75
+ size_t** splitpoints, size_t* npoints);
76
+
77
+ #endif /* ZOPFLI_BLOCKSPLITTER_H_ */
@@ -0,0 +1,119 @@
1
+ /*
2
+ Copyright 2011 Google Inc. All Rights Reserved.
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+
16
+ Author: lode.vandevenne@gmail.com (Lode Vandevenne)
17
+ Author: jyrki.alakuijala@gmail.com (Jyrki Alakuijala)
18
+ */
19
+
20
+ #include "cache.h"
21
+
22
+ #include <assert.h>
23
+ #include <stdio.h>
24
+ #include <stdlib.h>
25
+
26
+ #ifdef ZOPFLI_LONGEST_MATCH_CACHE
27
+
28
+ void ZopfliInitCache(size_t blocksize, ZopfliLongestMatchCache* lmc) {
29
+ size_t i;
30
+ lmc->length = (unsigned short*)malloc(sizeof(unsigned short) * blocksize);
31
+ lmc->dist = (unsigned short*)malloc(sizeof(unsigned short) * blocksize);
32
+ /* Rather large amount of memory. */
33
+ lmc->sublen = (unsigned char*)malloc(ZOPFLI_CACHE_LENGTH * 3 * blocksize);
34
+
35
+ /* length > 0 and dist 0 is invalid combination, which indicates on purpose
36
+ that this cache value is not filled in yet. */
37
+ for (i = 0; i < blocksize; i++) lmc->length[i] = 1;
38
+ for (i = 0; i < blocksize; i++) lmc->dist[i] = 0;
39
+ for (i = 0; i < ZOPFLI_CACHE_LENGTH * blocksize * 3; i++) lmc->sublen[i] = 0;
40
+ }
41
+
42
+ void ZopfliCleanCache(ZopfliLongestMatchCache* lmc) {
43
+ free(lmc->length);
44
+ free(lmc->dist);
45
+ free(lmc->sublen);
46
+ }
47
+
48
+ void ZopfliSublenToCache(const unsigned short* sublen,
49
+ size_t pos, size_t length,
50
+ ZopfliLongestMatchCache* lmc) {
51
+ size_t i;
52
+ size_t j = 0;
53
+ unsigned bestlength = 0;
54
+ unsigned char* cache;
55
+
56
+ #if ZOPFLI_CACHE_LENGTH == 0
57
+ return;
58
+ #endif
59
+
60
+ cache = &lmc->sublen[ZOPFLI_CACHE_LENGTH * pos * 3];
61
+ if (length < 3) return;
62
+ for (i = 3; i <= length; i++) {
63
+ if (i == length || sublen[i] != sublen[i + 1]) {
64
+ cache[j * 3] = i - 3;
65
+ cache[j * 3 + 1] = sublen[i] % 256;
66
+ cache[j * 3 + 2] = (sublen[i] >> 8) % 256;
67
+ bestlength = i;
68
+ j++;
69
+ if (j >= ZOPFLI_CACHE_LENGTH) break;
70
+ }
71
+ }
72
+ if (j < ZOPFLI_CACHE_LENGTH) {
73
+ assert(bestlength == length);
74
+ cache[(ZOPFLI_CACHE_LENGTH - 1) * 3] = bestlength - 3;
75
+ } else {
76
+ assert(bestlength <= length);
77
+ }
78
+ assert(bestlength == ZopfliMaxCachedSublen(lmc, pos, length));
79
+ }
80
+
81
+ void ZopfliCacheToSublen(const ZopfliLongestMatchCache* lmc,
82
+ size_t pos, size_t length,
83
+ unsigned short* sublen) {
84
+ size_t i, j;
85
+ unsigned maxlength = ZopfliMaxCachedSublen(lmc, pos, length);
86
+ unsigned prevlength = 0;
87
+ unsigned char* cache;
88
+ #if ZOPFLI_CACHE_LENGTH == 0
89
+ return;
90
+ #endif
91
+ if (length < 3) return;
92
+ cache = &lmc->sublen[ZOPFLI_CACHE_LENGTH * pos * 3];
93
+ for (j = 0; j < ZOPFLI_CACHE_LENGTH; j++) {
94
+ unsigned length = cache[j * 3] + 3;
95
+ unsigned dist = cache[j * 3 + 1] + 256 * cache[j * 3 + 2];
96
+ for (i = prevlength; i <= length; i++) {
97
+ sublen[i] = dist;
98
+ }
99
+ if (length == maxlength) break;
100
+ prevlength = length + 1;
101
+ }
102
+ }
103
+
104
+ /*
105
+ Returns the length up to which could be stored in the cache.
106
+ */
107
+ unsigned ZopfliMaxCachedSublen(const ZopfliLongestMatchCache* lmc,
108
+ size_t pos, size_t length) {
109
+ unsigned char* cache;
110
+ #if ZOPFLI_CACHE_LENGTH == 0
111
+ return 0;
112
+ #endif
113
+ cache = &lmc->sublen[ZOPFLI_CACHE_LENGTH * pos * 3];
114
+ (void)length;
115
+ if (cache[1] == 0 && cache[2] == 0) return 0; /* No sublen cached. */
116
+ return cache[(ZOPFLI_CACHE_LENGTH - 1) * 3] + 3;
117
+ }
118
+
119
+ #endif /* ZOPFLI_LONGEST_MATCH_CACHE */
@@ -0,0 +1,66 @@
1
+ /*
2
+ Copyright 2011 Google Inc. All Rights Reserved.
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+
16
+ Author: lode.vandevenne@gmail.com (Lode Vandevenne)
17
+ Author: jyrki.alakuijala@gmail.com (Jyrki Alakuijala)
18
+ */
19
+
20
+ /*
21
+ The cache that speeds up ZopfliFindLongestMatch of lz77.c.
22
+ */
23
+
24
+ #ifndef ZOPFLI_CACHE_H_
25
+ #define ZOPFLI_CACHE_H_
26
+
27
+ #include "util.h"
28
+
29
+ #ifdef ZOPFLI_LONGEST_MATCH_CACHE
30
+
31
+ /*
32
+ Cache used by ZopfliFindLongestMatch to remember previously found length/dist
33
+ values.
34
+ This is needed because the squeeze runs will ask these values multiple times for
35
+ the same position.
36
+ Uses large amounts of memory, since it has to remember the distance belonging
37
+ to every possible shorter-than-the-best length (the so called "sublen" array).
38
+ */
39
+ typedef struct ZopfliLongestMatchCache {
40
+ unsigned short* length;
41
+ unsigned short* dist;
42
+ unsigned char* sublen;
43
+ } ZopfliLongestMatchCache;
44
+
45
+ /* Initializes the ZopfliLongestMatchCache. */
46
+ void ZopfliInitCache(size_t blocksize, ZopfliLongestMatchCache* lmc);
47
+
48
+ /* Frees up the memory of the ZopfliLongestMatchCache. */
49
+ void ZopfliCleanCache(ZopfliLongestMatchCache* lmc);
50
+
51
+ /* Stores sublen array in the cache. */
52
+ void ZopfliSublenToCache(const unsigned short* sublen,
53
+ size_t pos, size_t length,
54
+ ZopfliLongestMatchCache* lmc);
55
+
56
+ /* Extracts sublen array from the cache. */
57
+ void ZopfliCacheToSublen(const ZopfliLongestMatchCache* lmc,
58
+ size_t pos, size_t length,
59
+ unsigned short* sublen);
60
+ /* Returns the length up to which could be stored in the cache. */
61
+ unsigned ZopfliMaxCachedSublen(const ZopfliLongestMatchCache* lmc,
62
+ size_t pos, size_t length);
63
+
64
+ #endif /* ZOPFLI_LONGEST_MATCH_CACHE */
65
+
66
+ #endif /* ZOPFLI_CACHE_H_ */
@@ -0,0 +1,697 @@
1
+ /*
2
+ Copyright 2011 Google Inc. All Rights Reserved.
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+
16
+ Author: lode.vandevenne@gmail.com (Lode Vandevenne)
17
+ Author: jyrki.alakuijala@gmail.com (Jyrki Alakuijala)
18
+ */
19
+
20
+ /*
21
+ Modified by madler@alumni.caltech.edu (Mark Adler)
22
+ Exposed DeflatePart() as an external function.
23
+ */
24
+
25
+ #include "deflate.h"
26
+
27
+ #include <assert.h>
28
+ #include <stdio.h>
29
+ #include <stdlib.h>
30
+
31
+ #include "blocksplitter.h"
32
+ #include "lz77.h"
33
+ #include "squeeze.h"
34
+ #include "tree.h"
35
+
36
+ static void AddBit(int bit,
37
+ unsigned char* bp, unsigned char** out, size_t* outsize) {
38
+ if (((*bp) & 7) == 0) ZOPFLI_APPEND_DATA(0, out, outsize);
39
+ (*out)[*outsize - 1] |= bit << ((*bp) & 7);
40
+ (*bp)++;
41
+ }
42
+
43
+ static void AddBits(unsigned symbol, unsigned length,
44
+ unsigned char* bp, unsigned char** out, size_t* outsize) {
45
+ /* TODO(lode): make more efficient (add more bits at once). */
46
+ unsigned i;
47
+ for (i = 0; i < length; i++) {
48
+ unsigned bit = (symbol >> i) & 1;
49
+ if (((*bp) & 7) == 0) ZOPFLI_APPEND_DATA(0, out, outsize);
50
+ (*out)[*outsize - 1] |= bit << ((*bp) & 7);
51
+ (*bp)++;
52
+ }
53
+ }
54
+
55
+ /*
56
+ Adds bits, like AddBits, but the order is inverted. The deflate specification
57
+ uses both orders in one standard.
58
+ */
59
+ static void AddHuffmanBits(unsigned symbol, unsigned length,
60
+ unsigned char* bp, unsigned char** out,
61
+ size_t* outsize) {
62
+ /* TODO(lode): make more efficient (add more bits at once). */
63
+ unsigned i;
64
+ for (i = 0; i < length; i++) {
65
+ unsigned bit = (symbol >> (length - i - 1)) & 1;
66
+ if (((*bp) & 7) == 0) ZOPFLI_APPEND_DATA(0, out, outsize);
67
+ (*out)[*outsize - 1] |= bit << ((*bp) & 7);
68
+ (*bp)++;
69
+ }
70
+ }
71
+
72
+ /*
73
+ Ensures there are at least 2 distance codes to support buggy decoders.
74
+ Zlib 1.2.1 and below have a bug where it fails if there isn't at least 1
75
+ distance code (with length > 0), even though it's valid according to the
76
+ deflate spec to have 0 distance codes. On top of that, some mobile phones
77
+ require at least two distance codes. To support these decoders too (but
78
+ potentially at the cost of a few bytes), add dummy code lengths of 1.
79
+ References to this bug can be found in the changelog of
80
+ Zlib 1.2.2 and here: http://www.jonof.id.au/forum/index.php?topic=515.0.
81
+
82
+ d_lengths: the 32 lengths of the distance codes.
83
+ */
84
+ static void PatchDistanceCodesForBuggyDecoders(unsigned* d_lengths) {
85
+ int num_dist_codes = 0; /* Amount of non-zero distance codes */
86
+ int i;
87
+ for (i = 0; i < 30 /* Ignore the two unused codes from the spec */; i++) {
88
+ if (d_lengths[i]) num_dist_codes++;
89
+ if (num_dist_codes >= 2) return; /* Two or more codes is fine. */
90
+ }
91
+
92
+ if (num_dist_codes == 0) {
93
+ d_lengths[0] = d_lengths[1] = 1;
94
+ } else if (num_dist_codes == 1) {
95
+ d_lengths[d_lengths[0] ? 1 : 0] = 1;
96
+ }
97
+ }
98
+
99
+ static void AddDynamicTree(const unsigned* ll_lengths,
100
+ const unsigned* d_lengths,
101
+ unsigned char* bp,
102
+ unsigned char** out, size_t* outsize) {
103
+ unsigned* lld_lengths = 0; /* All litlen and dist lengthts with ending zeros
104
+ trimmed together in one array. */
105
+ unsigned lld_total; /* Size of lld_lengths. */
106
+ unsigned* rle = 0; /* Runlength encoded version of lengths of litlen and dist
107
+ trees. */
108
+ unsigned* rle_bits = 0; /* Extra bits for rle values 16, 17 and 18. */
109
+ size_t rle_size = 0; /* Size of rle array. */
110
+ size_t rle_bits_size = 0; /* Should have same value as rle_size. */
111
+ unsigned hlit = 29; /* 286 - 257 */
112
+ unsigned hdist = 29; /* 32 - 1, but gzip does not like hdist > 29.*/
113
+ unsigned hclen;
114
+ size_t i, j;
115
+ size_t clcounts[19];
116
+ unsigned clcl[19]; /* Code length code lengths. */
117
+ unsigned clsymbols[19];
118
+ /* The order in which code length code lengths are encoded as per deflate. */
119
+ unsigned order[19] = {
120
+ 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
121
+ };
122
+
123
+ /* Trim zeros. */
124
+ while (hlit > 0 && ll_lengths[257 + hlit - 1] == 0) hlit--;
125
+ while (hdist > 0 && d_lengths[1 + hdist - 1] == 0) hdist--;
126
+
127
+ lld_total = hlit + 257 + hdist + 1;
128
+ lld_lengths = (unsigned*)malloc(sizeof(*lld_lengths) * lld_total);
129
+ if (!lld_lengths) exit(-1); /* Allocation failed. */
130
+
131
+ for (i = 0; i < lld_total; i++) {
132
+ lld_lengths[i] = i < 257 + hlit
133
+ ? ll_lengths[i] : d_lengths[i - 257 - hlit];
134
+ assert(lld_lengths[i] < 16);
135
+ }
136
+
137
+ for (i = 0; i < lld_total; i++) {
138
+ size_t count = 0;
139
+ for (j = i; j < lld_total && lld_lengths[i] == lld_lengths[j]; j++) {
140
+ count++;
141
+ }
142
+ if (count >= 4 || (count >= 3 && lld_lengths[i] == 0)) {
143
+ if (lld_lengths[i] == 0) {
144
+ if (count > 10) {
145
+ if (count > 138) count = 138;
146
+ ZOPFLI_APPEND_DATA(18, &rle, &rle_size);
147
+ ZOPFLI_APPEND_DATA(count - 11, &rle_bits, &rle_bits_size);
148
+ } else {
149
+ ZOPFLI_APPEND_DATA(17, &rle, &rle_size);
150
+ ZOPFLI_APPEND_DATA(count - 3, &rle_bits, &rle_bits_size);
151
+ }
152
+ } else {
153
+ unsigned repeat = count - 1; /* Since the first one is hardcoded. */
154
+ ZOPFLI_APPEND_DATA(lld_lengths[i], &rle, &rle_size);
155
+ ZOPFLI_APPEND_DATA(0, &rle_bits, &rle_bits_size);
156
+ while (repeat >= 6) {
157
+ ZOPFLI_APPEND_DATA(16, &rle, &rle_size);
158
+ ZOPFLI_APPEND_DATA(6 - 3, &rle_bits, &rle_bits_size);
159
+ repeat -= 6;
160
+ }
161
+ if (repeat >= 3) {
162
+ ZOPFLI_APPEND_DATA(16, &rle, &rle_size);
163
+ ZOPFLI_APPEND_DATA(3 - 3, &rle_bits, &rle_bits_size);
164
+ repeat -= 3;
165
+ }
166
+ while (repeat != 0) {
167
+ ZOPFLI_APPEND_DATA(lld_lengths[i], &rle, &rle_size);
168
+ ZOPFLI_APPEND_DATA(0, &rle_bits, &rle_bits_size);
169
+ repeat--;
170
+ }
171
+ }
172
+
173
+ i += count - 1;
174
+ } else {
175
+ ZOPFLI_APPEND_DATA(lld_lengths[i], &rle, &rle_size);
176
+ ZOPFLI_APPEND_DATA(0, &rle_bits, &rle_bits_size);
177
+ }
178
+ assert(rle[rle_size - 1] <= 18);
179
+ }
180
+
181
+ for (i = 0; i < 19; i++) {
182
+ clcounts[i] = 0;
183
+ }
184
+ for (i = 0; i < rle_size; i++) {
185
+ clcounts[rle[i]]++;
186
+ }
187
+
188
+ ZopfliCalculateBitLengths(clcounts, 19, 7, clcl);
189
+ ZopfliLengthsToSymbols(clcl, 19, 7, clsymbols);
190
+
191
+ hclen = 15;
192
+ /* Trim zeros. */
193
+ while (hclen > 0 && clcounts[order[hclen + 4 - 1]] == 0) hclen--;
194
+
195
+ AddBits(hlit, 5, bp, out, outsize);
196
+ AddBits(hdist, 5, bp, out, outsize);
197
+ AddBits(hclen, 4, bp, out, outsize);
198
+
199
+ for (i = 0; i < hclen + 4; i++) {
200
+ AddBits(clcl[order[i]], 3, bp, out, outsize);
201
+ }
202
+
203
+ for (i = 0; i < rle_size; i++) {
204
+ unsigned symbol = clsymbols[rle[i]];
205
+ AddHuffmanBits(symbol, clcl[rle[i]], bp, out, outsize);
206
+ /* Extra bits. */
207
+ if (rle[i] == 16) AddBits(rle_bits[i], 2, bp, out, outsize);
208
+ else if (rle[i] == 17) AddBits(rle_bits[i], 3, bp, out, outsize);
209
+ else if (rle[i] == 18) AddBits(rle_bits[i], 7, bp, out, outsize);
210
+ }
211
+
212
+ free(lld_lengths);
213
+ free(rle);
214
+ free(rle_bits);
215
+ }
216
+
217
+ /*
218
+ Gives the exact size of the tree, in bits, as it will be encoded in DEFLATE.
219
+ */
220
+ static size_t CalculateTreeSize(const unsigned* ll_lengths,
221
+ const unsigned* d_lengths,
222
+ size_t* ll_counts, size_t* d_counts) {
223
+ unsigned char* dummy = 0;
224
+ size_t dummysize = 0;
225
+ unsigned char bp = 0;
226
+
227
+ (void)ll_counts;
228
+ (void)d_counts;
229
+
230
+ AddDynamicTree(ll_lengths, d_lengths, &bp, &dummy, &dummysize);
231
+ free(dummy);
232
+
233
+ return dummysize * 8 + (bp & 7);
234
+ }
235
+
236
+ /*
237
+ Adds all lit/len and dist codes from the lists as huffman symbols. Does not add
238
+ end code 256. expected_data_size is the uncompressed block size, used for
239
+ assert, but you can set it to 0 to not do the assertion.
240
+ */
241
+ static void AddLZ77Data(const unsigned short* litlens,
242
+ const unsigned short* dists,
243
+ size_t lstart, size_t lend,
244
+ size_t expected_data_size,
245
+ const unsigned* ll_symbols, const unsigned* ll_lengths,
246
+ const unsigned* d_symbols, const unsigned* d_lengths,
247
+ unsigned char* bp,
248
+ unsigned char** out, size_t* outsize) {
249
+ size_t testlength = 0;
250
+ size_t i;
251
+
252
+ for (i = lstart; i < lend; i++) {
253
+ unsigned dist = dists[i];
254
+ unsigned litlen = litlens[i];
255
+ if (dist == 0) {
256
+ assert(litlen < 256);
257
+ assert(ll_lengths[litlen] > 0);
258
+ AddHuffmanBits(ll_symbols[litlen], ll_lengths[litlen], bp, out, outsize);
259
+ testlength++;
260
+ } else {
261
+ unsigned lls = ZopfliGetLengthSymbol(litlen);
262
+ unsigned ds = ZopfliGetDistSymbol(dist);
263
+ assert(litlen >= 3 && litlen <= 288);
264
+ assert(ll_lengths[lls] > 0);
265
+ assert(d_lengths[ds] > 0);
266
+ AddHuffmanBits(ll_symbols[lls], ll_lengths[lls], bp, out, outsize);
267
+ AddBits(ZopfliGetLengthExtraBitsValue(litlen),
268
+ ZopfliGetLengthExtraBits(litlen),
269
+ bp, out, outsize);
270
+ AddHuffmanBits(d_symbols[ds], d_lengths[ds], bp, out, outsize);
271
+ AddBits(ZopfliGetDistExtraBitsValue(dist),
272
+ ZopfliGetDistExtraBits(dist),
273
+ bp, out, outsize);
274
+ testlength += litlen;
275
+ }
276
+ }
277
+ assert(expected_data_size == 0 || testlength == expected_data_size);
278
+ }
279
+
280
+ static void GetFixedTree(unsigned* ll_lengths, unsigned* d_lengths) {
281
+ size_t i;
282
+ for (i = 0; i < 144; i++) ll_lengths[i] = 8;
283
+ for (i = 144; i < 256; i++) ll_lengths[i] = 9;
284
+ for (i = 256; i < 280; i++) ll_lengths[i] = 7;
285
+ for (i = 280; i < 288; i++) ll_lengths[i] = 8;
286
+ for (i = 0; i < 32; i++) d_lengths[i] = 5;
287
+ }
288
+
289
+ /*
290
+ Calculates size of the part after the header and tree of an LZ77 block, in bits.
291
+ */
292
+ static size_t CalculateBlockSymbolSize(const unsigned* ll_lengths,
293
+ const unsigned* d_lengths,
294
+ const unsigned short* litlens,
295
+ const unsigned short* dists,
296
+ size_t lstart, size_t lend) {
297
+ size_t result = 0;
298
+ size_t i;
299
+ for (i = lstart; i < lend; i++) {
300
+ if (dists[i] == 0) {
301
+ result += ll_lengths[litlens[i]];
302
+ } else {
303
+ result += ll_lengths[ZopfliGetLengthSymbol(litlens[i])];
304
+ result += d_lengths[ZopfliGetDistSymbol(dists[i])];
305
+ result += ZopfliGetLengthExtraBits(litlens[i]);
306
+ result += ZopfliGetDistExtraBits(dists[i]);
307
+ }
308
+ }
309
+ result += ll_lengths[256]; /*end symbol*/
310
+ return result;
311
+ }
312
+
313
+ double ZopfliCalculateBlockSize(const unsigned short* litlens,
314
+ const unsigned short* dists,
315
+ size_t lstart, size_t lend, int btype) {
316
+ size_t ll_counts[288];
317
+ size_t d_counts[32];
318
+
319
+ unsigned ll_lengths[288];
320
+ unsigned d_lengths[32];
321
+
322
+ double result = 3; /*bfinal and btype bits*/
323
+
324
+ assert(btype == 1 || btype == 2); /* This is not for uncompressed blocks. */
325
+
326
+ if(btype == 1) {
327
+ GetFixedTree(ll_lengths, d_lengths);
328
+ } else {
329
+ ZopfliLZ77Counts(litlens, dists, lstart, lend, ll_counts, d_counts);
330
+ ZopfliCalculateBitLengths(ll_counts, 288, 15, ll_lengths);
331
+ ZopfliCalculateBitLengths(d_counts, 32, 15, d_lengths);
332
+ PatchDistanceCodesForBuggyDecoders(d_lengths);
333
+ result += CalculateTreeSize(ll_lengths, d_lengths, ll_counts, d_counts);
334
+ }
335
+
336
+ result += CalculateBlockSymbolSize(
337
+ ll_lengths, d_lengths, litlens, dists, lstart, lend);
338
+
339
+ return result;
340
+ }
341
+
342
+ /*
343
+ Adds a deflate block with the given LZ77 data to the output.
344
+ options: global program options
345
+ btype: the block type, must be 1 or 2
346
+ final: whether to set the "final" bit on this block, must be the last block
347
+ litlens: literal/length array of the LZ77 data, in the same format as in
348
+ ZopfliLZ77Store.
349
+ dists: distance array of the LZ77 data, in the same format as in
350
+ ZopfliLZ77Store.
351
+ lstart: where to start in the LZ77 data
352
+ lend: where to end in the LZ77 data (not inclusive)
353
+ expected_data_size: the uncompressed block size, used for assert, but you can
354
+ set it to 0 to not do the assertion.
355
+ bp: output bit pointer
356
+ out: dynamic output array to append to
357
+ outsize: dynamic output array size
358
+ */
359
+ static void AddLZ77Block(const ZopfliOptions* options, int btype, int final,
360
+ const unsigned short* litlens,
361
+ const unsigned short* dists,
362
+ size_t lstart, size_t lend,
363
+ size_t expected_data_size,
364
+ unsigned char* bp, unsigned char** out, size_t* outsize) {
365
+ size_t ll_counts[288];
366
+ size_t d_counts[32];
367
+ unsigned ll_lengths[288];
368
+ unsigned d_lengths[32];
369
+ unsigned ll_symbols[288];
370
+ unsigned d_symbols[32];
371
+ size_t detect_block_size = *outsize;
372
+ size_t compressed_size;
373
+ size_t uncompressed_size = 0;
374
+ size_t i;
375
+
376
+ AddBit(final, bp, out, outsize);
377
+ AddBit(btype & 1, bp, out, outsize);
378
+ AddBit((btype & 2) >> 1, bp, out, outsize);
379
+
380
+ if (btype == 1) {
381
+ /* Fixed block. */
382
+ GetFixedTree(ll_lengths, d_lengths);
383
+ } else {
384
+ /* Dynamic block. */
385
+ unsigned detect_tree_size;
386
+ assert(btype == 2);
387
+ ZopfliLZ77Counts(litlens, dists, lstart, lend, ll_counts, d_counts);
388
+ ZopfliCalculateBitLengths(ll_counts, 288, 15, ll_lengths);
389
+ ZopfliCalculateBitLengths(d_counts, 32, 15, d_lengths);
390
+ PatchDistanceCodesForBuggyDecoders(d_lengths);
391
+ detect_tree_size = *outsize;
392
+ AddDynamicTree(ll_lengths, d_lengths, bp, out, outsize);
393
+ if (options->verbose) {
394
+ fprintf(stderr, "treesize: %d\n", (int)(*outsize - detect_tree_size));
395
+ }
396
+
397
+ /* Assert that for every present symbol, the code length is non-zero. */
398
+ /* TODO(lode): remove this in release version. */
399
+ for (i = 0; i < 288; i++) assert(ll_counts[i] == 0 || ll_lengths[i] > 0);
400
+ for (i = 0; i < 32; i++) assert(d_counts[i] == 0 || d_lengths[i] > 0);
401
+ }
402
+
403
+ ZopfliLengthsToSymbols(ll_lengths, 288, 15, ll_symbols);
404
+ ZopfliLengthsToSymbols(d_lengths, 32, 15, d_symbols);
405
+
406
+ detect_block_size = *outsize;
407
+ AddLZ77Data(litlens, dists, lstart, lend, expected_data_size,
408
+ ll_symbols, ll_lengths, d_symbols, d_lengths,
409
+ bp, out, outsize);
410
+ /* End symbol. */
411
+ AddHuffmanBits(ll_symbols[256], ll_lengths[256], bp, out, outsize);
412
+
413
+ for (i = lstart; i < lend; i++) {
414
+ uncompressed_size += dists[i] == 0 ? 1 : litlens[i];
415
+ }
416
+ compressed_size = *outsize - detect_block_size;
417
+ if (options->verbose) {
418
+ fprintf(stderr, "compressed block size: %d (%dk) (unc: %d)\n",
419
+ (int)compressed_size, (int)(compressed_size / 1024),
420
+ (int)(uncompressed_size));
421
+ }
422
+ }
423
+
424
+ static void DeflateDynamicBlock(const ZopfliOptions* options, int final,
425
+ const unsigned char* in,
426
+ size_t instart, size_t inend,
427
+ unsigned char* bp,
428
+ unsigned char** out, size_t* outsize) {
429
+ ZopfliBlockState s;
430
+ size_t blocksize = inend - instart;
431
+ ZopfliLZ77Store store;
432
+ int btype = 2;
433
+
434
+ ZopfliInitLZ77Store(&store);
435
+
436
+ s.options = options;
437
+ s.blockstart = instart;
438
+ s.blockend = inend;
439
+ #ifdef ZOPFLI_LONGEST_MATCH_CACHE
440
+ s.lmc = (ZopfliLongestMatchCache*)malloc(sizeof(ZopfliLongestMatchCache));
441
+ ZopfliInitCache(blocksize, s.lmc);
442
+ #endif
443
+
444
+ ZopfliLZ77Optimal(&s, in, instart, inend, &store);
445
+
446
+ /* For small block, encoding with fixed tree can be smaller. For large block,
447
+ don't bother doing this expensive test, dynamic tree will be better.*/
448
+ if (store.size < 1000) {
449
+ double dyncost, fixedcost;
450
+ ZopfliLZ77Store fixedstore;
451
+ ZopfliInitLZ77Store(&fixedstore);
452
+ ZopfliLZ77OptimalFixed(&s, in, instart, inend, &fixedstore);
453
+ dyncost = ZopfliCalculateBlockSize(store.litlens, store.dists,
454
+ 0, store.size, 2);
455
+ fixedcost = ZopfliCalculateBlockSize(fixedstore.litlens, fixedstore.dists,
456
+ 0, fixedstore.size, 1);
457
+ if (fixedcost < dyncost) {
458
+ btype = 1;
459
+ ZopfliCleanLZ77Store(&store);
460
+ store = fixedstore;
461
+ } else {
462
+ ZopfliCleanLZ77Store(&fixedstore);
463
+ }
464
+ }
465
+
466
+ AddLZ77Block(s.options, btype, final,
467
+ store.litlens, store.dists, 0, store.size,
468
+ blocksize, bp, out, outsize);
469
+
470
+ #ifdef ZOPFLI_LONGEST_MATCH_CACHE
471
+ ZopfliCleanCache(s.lmc);
472
+ free(s.lmc);
473
+ #endif
474
+ ZopfliCleanLZ77Store(&store);
475
+ }
476
+
477
+ static void DeflateFixedBlock(const ZopfliOptions* options, int final,
478
+ const unsigned char* in,
479
+ size_t instart, size_t inend,
480
+ unsigned char* bp,
481
+ unsigned char** out, size_t* outsize) {
482
+ ZopfliBlockState s;
483
+ size_t blocksize = inend - instart;
484
+ ZopfliLZ77Store store;
485
+
486
+ ZopfliInitLZ77Store(&store);
487
+
488
+ s.options = options;
489
+ s.blockstart = instart;
490
+ s.blockend = inend;
491
+ #ifdef ZOPFLI_LONGEST_MATCH_CACHE
492
+ s.lmc = (ZopfliLongestMatchCache*)malloc(sizeof(ZopfliLongestMatchCache));
493
+ ZopfliInitCache(blocksize, s.lmc);
494
+ #endif
495
+
496
+ ZopfliLZ77OptimalFixed(&s, in, instart, inend, &store);
497
+
498
+ AddLZ77Block(s.options, 1, final, store.litlens, store.dists, 0, store.size,
499
+ blocksize, bp, out, outsize);
500
+
501
+ #ifdef ZOPFLI_LONGEST_MATCH_CACHE
502
+ ZopfliCleanCache(s.lmc);
503
+ free(s.lmc);
504
+ #endif
505
+ ZopfliCleanLZ77Store(&store);
506
+ }
507
+
508
+ static void DeflateNonCompressedBlock(const ZopfliOptions* options, int final,
509
+ const unsigned char* in, size_t instart,
510
+ size_t inend,
511
+ unsigned char* bp,
512
+ unsigned char** out, size_t* outsize) {
513
+ size_t i;
514
+ size_t blocksize = inend - instart;
515
+ unsigned short nlen = ~blocksize;
516
+
517
+ (void)options;
518
+ assert(blocksize < 65536); /* Non compressed blocks are max this size. */
519
+
520
+ AddBit(final, bp, out, outsize);
521
+ /* BTYPE 00 */
522
+ AddBit(0, bp, out, outsize);
523
+ AddBit(0, bp, out, outsize);
524
+
525
+ /* Any bits of input up to the next byte boundary are ignored. */
526
+ *bp = 0;
527
+
528
+ ZOPFLI_APPEND_DATA(blocksize % 256, out, outsize);
529
+ ZOPFLI_APPEND_DATA((blocksize / 256) % 256, out, outsize);
530
+ ZOPFLI_APPEND_DATA(nlen % 256, out, outsize);
531
+ ZOPFLI_APPEND_DATA((nlen / 256) % 256, out, outsize);
532
+
533
+ for (i = instart; i < inend; i++) {
534
+ ZOPFLI_APPEND_DATA(in[i], out, outsize);
535
+ }
536
+ }
537
+
538
+ static void DeflateBlock(const ZopfliOptions* options,
539
+ int btype, int final,
540
+ const unsigned char* in, size_t instart, size_t inend,
541
+ unsigned char* bp,
542
+ unsigned char** out, size_t* outsize) {
543
+ if (btype == 0) {
544
+ DeflateNonCompressedBlock(
545
+ options, final, in, instart, inend, bp, out, outsize);
546
+ } else if (btype == 1) {
547
+ DeflateFixedBlock(options, final, in, instart, inend, bp, out, outsize);
548
+ } else {
549
+ assert (btype == 2);
550
+ DeflateDynamicBlock(options, final, in, instart, inend, bp, out, outsize);
551
+ }
552
+ }
553
+
554
+ /*
555
+ Does squeeze strategy where first block splitting is done, then each block is
556
+ squeezed.
557
+ Parameters: see description of the ZopfliDeflate function.
558
+ */
559
+ static void DeflateSplittingFirst(const ZopfliOptions* options,
560
+ int btype, int final,
561
+ const unsigned char* in,
562
+ size_t instart, size_t inend,
563
+ unsigned char* bp,
564
+ unsigned char** out, size_t* outsize) {
565
+ size_t i;
566
+ size_t* splitpoints = 0;
567
+ size_t npoints = 0;
568
+ if (btype == 0) {
569
+ ZopfliBlockSplitSimple(in, instart, inend, 65535, &splitpoints, &npoints);
570
+ } else if (btype == 1) {
571
+ /* If all blocks are fixed tree, splitting into separate blocks only
572
+ increases the total size. Leave npoints at 0, this represents 1 block. */
573
+ } else {
574
+ ZopfliBlockSplit(options, in, instart, inend,
575
+ options->blocksplittingmax, &splitpoints, &npoints);
576
+ }
577
+
578
+ for (i = 0; i <= npoints; i++) {
579
+ size_t start = i == 0 ? instart : splitpoints[i - 1];
580
+ size_t end = i == npoints ? inend : splitpoints[i];
581
+ DeflateBlock(options, btype, i == npoints && final, in, start, end,
582
+ bp, out, outsize);
583
+ }
584
+
585
+ free(splitpoints);
586
+ }
587
+
588
+ /*
589
+ Does squeeze strategy where first the best possible lz77 is done, and then based
590
+ on that data, block splitting is done.
591
+ Parameters: see description of the ZopfliDeflate function.
592
+ */
593
+ static void DeflateSplittingLast(const ZopfliOptions* options,
594
+ int btype, int final,
595
+ const unsigned char* in,
596
+ size_t instart, size_t inend,
597
+ unsigned char* bp,
598
+ unsigned char** out, size_t* outsize) {
599
+ size_t i;
600
+ ZopfliBlockState s;
601
+ ZopfliLZ77Store store;
602
+ size_t* splitpoints = 0;
603
+ size_t npoints = 0;
604
+
605
+ if (btype == 0) {
606
+ /* This function only supports LZ77 compression. DeflateSplittingFirst
607
+ supports the special case of noncompressed data. Punt it to that one. */
608
+ DeflateSplittingFirst(options, btype, final,
609
+ in, instart, inend,
610
+ bp, out, outsize);
611
+ }
612
+ assert(btype == 1 || btype == 2);
613
+
614
+ ZopfliInitLZ77Store(&store);
615
+
616
+ s.options = options;
617
+ s.blockstart = instart;
618
+ s.blockend = inend;
619
+ #ifdef ZOPFLI_LONGEST_MATCH_CACHE
620
+ s.lmc = (ZopfliLongestMatchCache*)malloc(sizeof(ZopfliLongestMatchCache));
621
+ ZopfliInitCache(inend - instart, s.lmc);
622
+ #endif
623
+
624
+ if (btype == 2) {
625
+ ZopfliLZ77Optimal(&s, in, instart, inend, &store);
626
+ } else {
627
+ assert (btype == 1);
628
+ ZopfliLZ77OptimalFixed(&s, in, instart, inend, &store);
629
+ }
630
+
631
+ if (btype == 1) {
632
+ /* If all blocks are fixed tree, splitting into separate blocks only
633
+ increases the total size. Leave npoints at 0, this represents 1 block. */
634
+ } else {
635
+ ZopfliBlockSplitLZ77(options, store.litlens, store.dists, store.size,
636
+ options->blocksplittingmax, &splitpoints, &npoints);
637
+ }
638
+
639
+ for (i = 0; i <= npoints; i++) {
640
+ size_t start = i == 0 ? 0 : splitpoints[i - 1];
641
+ size_t end = i == npoints ? store.size : splitpoints[i];
642
+ AddLZ77Block(options, btype, i == npoints && final,
643
+ store.litlens, store.dists, start, end, 0,
644
+ bp, out, outsize);
645
+ }
646
+
647
+ #ifdef ZOPFLI_LONGEST_MATCH_CACHE
648
+ ZopfliCleanCache(s.lmc);
649
+ free(s.lmc);
650
+ #endif
651
+
652
+ ZopfliCleanLZ77Store(&store);
653
+ }
654
+
655
+ /*
656
+ Deflate a part, to allow ZopfliDeflate() to use multiple master blocks if
657
+ needed.
658
+ It is possible to call this function multiple times in a row, shifting
659
+ instart and inend to next bytes of the data. If instart is larger than 0, then
660
+ previous bytes are used as the initial dictionary for LZ77.
661
+ This function will usually output multiple deflate blocks. If final is 1, then
662
+ the final bit will be set on the last block.
663
+ */
664
+ void ZopfliDeflatePart(const ZopfliOptions* options, int btype, int final,
665
+ const unsigned char* in, size_t instart, size_t inend,
666
+ unsigned char* bp, unsigned char** out,
667
+ size_t* outsize) {
668
+ if (options->blocksplitting) {
669
+ if (options->blocksplittinglast) {
670
+ DeflateSplittingLast(options, btype, final, in, instart, inend,
671
+ bp, out, outsize);
672
+ } else {
673
+ DeflateSplittingFirst(options, btype, final, in, instart, inend,
674
+ bp, out, outsize);
675
+ }
676
+ } else {
677
+ DeflateBlock(options, btype, final, in, instart, inend, bp, out, outsize);
678
+ }
679
+ }
680
+
681
+ void ZopfliDeflate(const ZopfliOptions* options, int btype, int final,
682
+ const unsigned char* in, size_t insize,
683
+ unsigned char* bp, unsigned char** out, size_t* outsize) {
684
+ #if ZOPFLI_MASTER_BLOCK_SIZE == 0
685
+ ZopfliDeflatePart(options, btype, final, in, 0, insize, bp, out, outsize);
686
+ #else
687
+ size_t i = 0;
688
+ while (i < insize) {
689
+ int masterfinal = (i + ZOPFLI_MASTER_BLOCK_SIZE >= insize);
690
+ int final2 = final && masterfinal;
691
+ size_t size = masterfinal ? insize - i : ZOPFLI_MASTER_BLOCK_SIZE;
692
+ ZopfliDeflatePart(options, btype, final2,
693
+ in, i, i + size, bp, out, outsize);
694
+ i += size;
695
+ }
696
+ #endif
697
+ }