zopfli 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +17 -0
- data/.gitmodules +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +34 -0
- data/Rakefile +25 -0
- data/ext/extconf.rb +43 -0
- data/ext/zopfli.c +38 -0
- data/lib/zopfli/version.rb +3 -0
- data/test/fixtures/alice29.txt +3609 -0
- data/test/test_zopfli_deflate.rb +24 -0
- data/vendor/zopfli/CONTRIBUTORS +6 -0
- data/vendor/zopfli/COPYING +201 -0
- data/vendor/zopfli/README +24 -0
- data/vendor/zopfli/blocksplitter.c +344 -0
- data/vendor/zopfli/blocksplitter.h +77 -0
- data/vendor/zopfli/cache.c +119 -0
- data/vendor/zopfli/cache.h +66 -0
- data/vendor/zopfli/deflate.c +697 -0
- data/vendor/zopfli/deflate.h +77 -0
- data/vendor/zopfli/gzip_container.c +117 -0
- data/vendor/zopfli/gzip_container.h +42 -0
- data/vendor/zopfli/hash.c +135 -0
- data/vendor/zopfli/hash.h +70 -0
- data/vendor/zopfli/katajainen.c +251 -0
- data/vendor/zopfli/katajainen.h +42 -0
- data/vendor/zopfli/lz77.c +468 -0
- data/vendor/zopfli/lz77.h +126 -0
- data/vendor/zopfli/makefile +5 -0
- data/vendor/zopfli/squeeze.c +543 -0
- data/vendor/zopfli/squeeze.h +60 -0
- data/vendor/zopfli/tree.c +101 -0
- data/vendor/zopfli/tree.h +51 -0
- data/vendor/zopfli/util.c +202 -0
- data/vendor/zopfli/util.h +175 -0
- data/vendor/zopfli/zlib_container.c +79 -0
- data/vendor/zopfli/zlib_container.h +42 -0
- data/vendor/zopfli/zopfli.h +71 -0
- data/vendor/zopfli/zopfli_bin.c +204 -0
- data/vendor/zopfli/zopfli_lib.c +37 -0
- data/zopfli.gemspec +49 -0
- metadata +130 -0
@@ -0,0 +1,60 @@
|
|
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 squeeze functions do enhanced LZ77 compression by optimal parsing with a
|
22
|
+
cost model, rather than greedily choosing the longest length or using a single
|
23
|
+
step of lazy matching like regular implementations.
|
24
|
+
|
25
|
+
Since the cost model is based on the Huffman tree that can only be calculated
|
26
|
+
after the LZ77 data is generated, there is a chicken and egg problem, and
|
27
|
+
multiple runs are done with updated cost models to converge to a better
|
28
|
+
solution.
|
29
|
+
*/
|
30
|
+
|
31
|
+
#ifndef ZOPFLI_SQUEEZE_H_
|
32
|
+
#define ZOPFLI_SQUEEZE_H_
|
33
|
+
|
34
|
+
#include "lz77.h"
|
35
|
+
|
36
|
+
/*
|
37
|
+
Calculates lit/len and dist pairs for given data.
|
38
|
+
If instart is larger than 0, it uses values before instart as starting
|
39
|
+
dictionary.
|
40
|
+
*/
|
41
|
+
void ZopfliLZ77Optimal(ZopfliBlockState *s,
|
42
|
+
const unsigned char* in, size_t instart, size_t inend,
|
43
|
+
ZopfliLZ77Store* store);
|
44
|
+
|
45
|
+
/*
|
46
|
+
Does the same as ZopfliLZ77Optimal, but optimized for the fixed tree of the
|
47
|
+
deflate standard.
|
48
|
+
The fixed tree never gives the best compression. But this gives the best
|
49
|
+
possible LZ77 encoding possible with the fixed tree.
|
50
|
+
This does not create or output any fixed tree, only LZ77 data optimized for
|
51
|
+
using with a fixed tree.
|
52
|
+
If instart is larger than 0, it uses values before instart as starting
|
53
|
+
dictionary.
|
54
|
+
*/
|
55
|
+
void ZopfliLZ77OptimalFixed(ZopfliBlockState *s,
|
56
|
+
const unsigned char* in,
|
57
|
+
size_t instart, size_t inend,
|
58
|
+
ZopfliLZ77Store* store);
|
59
|
+
|
60
|
+
#endif /* ZOPFLI_SQUEEZE_H_ */
|
@@ -0,0 +1,101 @@
|
|
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 "tree.h"
|
21
|
+
|
22
|
+
#include <assert.h>
|
23
|
+
#include <math.h>
|
24
|
+
#include <stdio.h>
|
25
|
+
#include <stdlib.h>
|
26
|
+
|
27
|
+
#include "katajainen.h"
|
28
|
+
#include "util.h"
|
29
|
+
|
30
|
+
void ZopfliLengthsToSymbols(const unsigned* lengths, size_t n, unsigned maxbits,
|
31
|
+
unsigned* symbols) {
|
32
|
+
size_t* bl_count = (size_t*)malloc(sizeof(size_t) * (maxbits + 1));
|
33
|
+
size_t* next_code = (size_t*)malloc(sizeof(size_t) * (maxbits + 1));
|
34
|
+
unsigned bits, i;
|
35
|
+
unsigned code;
|
36
|
+
|
37
|
+
for (i = 0; i < n; i++) {
|
38
|
+
symbols[i] = 0;
|
39
|
+
}
|
40
|
+
|
41
|
+
/* 1) Count the number of codes for each code length. Let bl_count[N] be the
|
42
|
+
number of codes of length N, N >= 1. */
|
43
|
+
for (bits = 0; bits <= maxbits; bits++) {
|
44
|
+
bl_count[bits] = 0;
|
45
|
+
}
|
46
|
+
for (i = 0; i < n; i++) {
|
47
|
+
assert(lengths[i] <= maxbits);
|
48
|
+
bl_count[lengths[i]]++;
|
49
|
+
}
|
50
|
+
/* 2) Find the numerical value of the smallest code for each code length. */
|
51
|
+
code = 0;
|
52
|
+
bl_count[0] = 0;
|
53
|
+
for (bits = 1; bits <= maxbits; bits++) {
|
54
|
+
code = (code + bl_count[bits-1]) << 1;
|
55
|
+
next_code[bits] = code;
|
56
|
+
}
|
57
|
+
/* 3) Assign numerical values to all codes, using consecutive values for all
|
58
|
+
codes of the same length with the base values determined at step 2. */
|
59
|
+
for (i = 0; i < n; i++) {
|
60
|
+
unsigned len = lengths[i];
|
61
|
+
if (len != 0) {
|
62
|
+
symbols[i] = next_code[len];
|
63
|
+
next_code[len]++;
|
64
|
+
}
|
65
|
+
}
|
66
|
+
|
67
|
+
free(bl_count);
|
68
|
+
free(next_code);
|
69
|
+
}
|
70
|
+
|
71
|
+
void ZopfliCalculateEntropy(const size_t* count, size_t n, double* bitlengths) {
|
72
|
+
static const double kInvLog2 = 1.4426950408889; /* 1.0 / log(2.0) */
|
73
|
+
unsigned sum = 0;
|
74
|
+
unsigned i;
|
75
|
+
double log2sum;
|
76
|
+
for (i = 0; i < n; ++i) {
|
77
|
+
sum += count[i];
|
78
|
+
}
|
79
|
+
log2sum = (sum == 0 ? log(n) : log(sum)) * kInvLog2;
|
80
|
+
for (i = 0; i < n; ++i) {
|
81
|
+
/* When the count of the symbol is 0, but its cost is requested anyway, it
|
82
|
+
means the symbol will appear at least once anyway, so give it the cost as if
|
83
|
+
its count is 1.*/
|
84
|
+
if (count[i] == 0) bitlengths[i] = log2sum;
|
85
|
+
else bitlengths[i] = log2sum - log(count[i]) * kInvLog2;
|
86
|
+
/* Depending on compiler and architecture, the above subtraction of two
|
87
|
+
floating point numbers may give a negative result very close to zero
|
88
|
+
instead of zero (e.g. -5.973954e-17 with gcc 4.1.2 on Ubuntu 11.4). Clamp
|
89
|
+
it to zero. These floating point imprecisions do not affect the cost model
|
90
|
+
significantly so this is ok. */
|
91
|
+
if (bitlengths[i] < 0 && bitlengths[i] > -1e-5) bitlengths[i] = 0;
|
92
|
+
assert(bitlengths[i] >= 0);
|
93
|
+
}
|
94
|
+
}
|
95
|
+
|
96
|
+
void ZopfliCalculateBitLengths(const size_t* count, size_t n, int maxbits,
|
97
|
+
unsigned* bitlengths) {
|
98
|
+
int error = ZopfliLengthLimitedCodeLengths(count, n, maxbits, bitlengths);
|
99
|
+
(void) error;
|
100
|
+
assert(!error);
|
101
|
+
}
|
@@ -0,0 +1,51 @@
|
|
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
|
+
Utilities for creating and using Huffman trees.
|
22
|
+
*/
|
23
|
+
|
24
|
+
#ifndef ZOPFLI_TREE_H_
|
25
|
+
#define ZOPFLI_TREE_H_
|
26
|
+
|
27
|
+
#include <string.h>
|
28
|
+
|
29
|
+
/*
|
30
|
+
Calculates the bitlengths for the Huffman tree, based on the counts of each
|
31
|
+
symbol.
|
32
|
+
*/
|
33
|
+
void ZopfliCalculateBitLengths(const size_t* count, size_t n, int maxbits,
|
34
|
+
unsigned *bitlengths);
|
35
|
+
|
36
|
+
/*
|
37
|
+
Converts a series of Huffman tree bitlengths, to the bit values of the symbols.
|
38
|
+
*/
|
39
|
+
void ZopfliLengthsToSymbols(const unsigned* lengths, size_t n, unsigned maxbits,
|
40
|
+
unsigned* symbols);
|
41
|
+
|
42
|
+
/*
|
43
|
+
Calculates the entropy of each symbol, based on the counts of each symbol. The
|
44
|
+
result is similar to the result of ZopfliCalculateBitLengths, but with the
|
45
|
+
actual theoritical bit lengths according to the entropy. Since the resulting
|
46
|
+
values are fractional, they cannot be used to encode the tree specified by
|
47
|
+
DEFLATE.
|
48
|
+
*/
|
49
|
+
void ZopfliCalculateEntropy(const size_t* count, size_t n, double* bitlengths);
|
50
|
+
|
51
|
+
#endif /* ZOPFLI_TREE_H_ */
|
@@ -0,0 +1,202 @@
|
|
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 "util.h"
|
21
|
+
|
22
|
+
#include <assert.h>
|
23
|
+
#include <stdio.h>
|
24
|
+
#include <stdlib.h>
|
25
|
+
|
26
|
+
int ZopfliGetDistExtraBits(int dist) {
|
27
|
+
#ifdef __GNUC__
|
28
|
+
if (dist < 5) return 0;
|
29
|
+
return (31 ^ __builtin_clz(dist - 1)) - 1; /* log2(dist - 1) - 1 */
|
30
|
+
#else
|
31
|
+
if (dist < 5) return 0;
|
32
|
+
else if (dist < 9) return 1;
|
33
|
+
else if (dist < 17) return 2;
|
34
|
+
else if (dist < 33) return 3;
|
35
|
+
else if (dist < 65) return 4;
|
36
|
+
else if (dist < 129) return 5;
|
37
|
+
else if (dist < 257) return 6;
|
38
|
+
else if (dist < 513) return 7;
|
39
|
+
else if (dist < 1025) return 8;
|
40
|
+
else if (dist < 2049) return 9;
|
41
|
+
else if (dist < 4097) return 10;
|
42
|
+
else if (dist < 8193) return 11;
|
43
|
+
else if (dist < 16385) return 12;
|
44
|
+
else return 13;
|
45
|
+
#endif
|
46
|
+
}
|
47
|
+
|
48
|
+
int ZopfliGetDistExtraBitsValue(int dist) {
|
49
|
+
#ifdef __GNUC__
|
50
|
+
if (dist < 5) {
|
51
|
+
return 0;
|
52
|
+
} else {
|
53
|
+
int l = 31 ^ __builtin_clz(dist - 1); /* log2(dist - 1) */
|
54
|
+
return (dist - (1 + (1 << l))) & ((1 << (l - 1)) - 1);
|
55
|
+
}
|
56
|
+
#else
|
57
|
+
if (dist < 5) return 0;
|
58
|
+
else if (dist < 9) return (dist - 5) & 1;
|
59
|
+
else if (dist < 17) return (dist - 9) & 3;
|
60
|
+
else if (dist < 33) return (dist - 17) & 7;
|
61
|
+
else if (dist < 65) return (dist - 33) & 15;
|
62
|
+
else if (dist < 129) return (dist - 65) & 31;
|
63
|
+
else if (dist < 257) return (dist - 129) & 63;
|
64
|
+
else if (dist < 513) return (dist - 257) & 127;
|
65
|
+
else if (dist < 1025) return (dist - 513) & 255;
|
66
|
+
else if (dist < 2049) return (dist - 1025) & 511;
|
67
|
+
else if (dist < 4097) return (dist - 2049) & 1023;
|
68
|
+
else if (dist < 8193) return (dist - 4097) & 2047;
|
69
|
+
else if (dist < 16385) return (dist - 8193) & 4095;
|
70
|
+
else return (dist - 16385) & 8191;
|
71
|
+
#endif
|
72
|
+
}
|
73
|
+
|
74
|
+
int ZopfliGetDistSymbol(int dist) {
|
75
|
+
#ifdef __GNUC__
|
76
|
+
if (dist < 5) {
|
77
|
+
return dist - 1;
|
78
|
+
} else {
|
79
|
+
int l = (31 ^ __builtin_clz(dist - 1)); /* log2(dist - 1) */
|
80
|
+
int r = ((dist - 1) >> (l - 1)) & 1;
|
81
|
+
return l * 2 + r;
|
82
|
+
}
|
83
|
+
#else
|
84
|
+
if (dist < 193) {
|
85
|
+
if (dist < 13) { /* dist 0..13. */
|
86
|
+
if (dist < 5) return dist - 1;
|
87
|
+
else if (dist < 7) return 4;
|
88
|
+
else if (dist < 9) return 5;
|
89
|
+
else return 6;
|
90
|
+
} else { /* dist 13..193. */
|
91
|
+
if (dist < 17) return 7;
|
92
|
+
else if (dist < 25) return 8;
|
93
|
+
else if (dist < 33) return 9;
|
94
|
+
else if (dist < 49) return 10;
|
95
|
+
else if (dist < 65) return 11;
|
96
|
+
else if (dist < 97) return 12;
|
97
|
+
else if (dist < 129) return 13;
|
98
|
+
else return 14;
|
99
|
+
}
|
100
|
+
} else {
|
101
|
+
if (dist < 2049) { /* dist 193..2049. */
|
102
|
+
if (dist < 257) return 15;
|
103
|
+
else if (dist < 385) return 16;
|
104
|
+
else if (dist < 513) return 17;
|
105
|
+
else if (dist < 769) return 18;
|
106
|
+
else if (dist < 1025) return 19;
|
107
|
+
else if (dist < 1537) return 20;
|
108
|
+
else return 21;
|
109
|
+
} else { /* dist 2049..32768. */
|
110
|
+
if (dist < 3073) return 22;
|
111
|
+
else if (dist < 4097) return 23;
|
112
|
+
else if (dist < 6145) return 24;
|
113
|
+
else if (dist < 8193) return 25;
|
114
|
+
else if (dist < 12289) return 26;
|
115
|
+
else if (dist < 16385) return 27;
|
116
|
+
else if (dist < 24577) return 28;
|
117
|
+
else return 29;
|
118
|
+
}
|
119
|
+
}
|
120
|
+
#endif
|
121
|
+
}
|
122
|
+
|
123
|
+
int ZopfliGetLengthExtraBits(int l) {
|
124
|
+
static const int table[259] = {
|
125
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
|
126
|
+
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
|
127
|
+
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
|
128
|
+
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
|
129
|
+
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
|
130
|
+
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
|
131
|
+
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
|
132
|
+
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
|
133
|
+
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
|
134
|
+
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
|
135
|
+
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
|
136
|
+
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
|
137
|
+
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
|
138
|
+
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
|
139
|
+
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
|
140
|
+
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0
|
141
|
+
};
|
142
|
+
return table[l];
|
143
|
+
}
|
144
|
+
|
145
|
+
int ZopfliGetLengthExtraBitsValue(int l) {
|
146
|
+
static const int table[259] = {
|
147
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 2, 3, 0,
|
148
|
+
1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5,
|
149
|
+
6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6,
|
150
|
+
7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
|
151
|
+
13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2,
|
152
|
+
3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
153
|
+
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
|
154
|
+
29, 30, 31, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
|
155
|
+
18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 0, 1, 2, 3, 4, 5, 6,
|
156
|
+
7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
|
157
|
+
27, 28, 29, 30, 31, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
|
158
|
+
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 0
|
159
|
+
};
|
160
|
+
return table[l];
|
161
|
+
}
|
162
|
+
|
163
|
+
/*
|
164
|
+
Returns symbol in range [257-285] (inclusive).
|
165
|
+
*/
|
166
|
+
int ZopfliGetLengthSymbol(int l) {
|
167
|
+
static const int table[259] = {
|
168
|
+
0, 0, 0, 257, 258, 259, 260, 261, 262, 263, 264,
|
169
|
+
265, 265, 266, 266, 267, 267, 268, 268,
|
170
|
+
269, 269, 269, 269, 270, 270, 270, 270,
|
171
|
+
271, 271, 271, 271, 272, 272, 272, 272,
|
172
|
+
273, 273, 273, 273, 273, 273, 273, 273,
|
173
|
+
274, 274, 274, 274, 274, 274, 274, 274,
|
174
|
+
275, 275, 275, 275, 275, 275, 275, 275,
|
175
|
+
276, 276, 276, 276, 276, 276, 276, 276,
|
176
|
+
277, 277, 277, 277, 277, 277, 277, 277,
|
177
|
+
277, 277, 277, 277, 277, 277, 277, 277,
|
178
|
+
278, 278, 278, 278, 278, 278, 278, 278,
|
179
|
+
278, 278, 278, 278, 278, 278, 278, 278,
|
180
|
+
279, 279, 279, 279, 279, 279, 279, 279,
|
181
|
+
279, 279, 279, 279, 279, 279, 279, 279,
|
182
|
+
280, 280, 280, 280, 280, 280, 280, 280,
|
183
|
+
280, 280, 280, 280, 280, 280, 280, 280,
|
184
|
+
281, 281, 281, 281, 281, 281, 281, 281,
|
185
|
+
281, 281, 281, 281, 281, 281, 281, 281,
|
186
|
+
281, 281, 281, 281, 281, 281, 281, 281,
|
187
|
+
281, 281, 281, 281, 281, 281, 281, 281,
|
188
|
+
282, 282, 282, 282, 282, 282, 282, 282,
|
189
|
+
282, 282, 282, 282, 282, 282, 282, 282,
|
190
|
+
282, 282, 282, 282, 282, 282, 282, 282,
|
191
|
+
282, 282, 282, 282, 282, 282, 282, 282,
|
192
|
+
283, 283, 283, 283, 283, 283, 283, 283,
|
193
|
+
283, 283, 283, 283, 283, 283, 283, 283,
|
194
|
+
283, 283, 283, 283, 283, 283, 283, 283,
|
195
|
+
283, 283, 283, 283, 283, 283, 283, 283,
|
196
|
+
284, 284, 284, 284, 284, 284, 284, 284,
|
197
|
+
284, 284, 284, 284, 284, 284, 284, 284,
|
198
|
+
284, 284, 284, 284, 284, 284, 284, 284,
|
199
|
+
284, 284, 284, 284, 284, 284, 284, 285
|
200
|
+
};
|
201
|
+
return table[l];
|
202
|
+
}
|
@@ -0,0 +1,175 @@
|
|
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
|
+
Several utilities, including: #defines to try different compression results,
|
22
|
+
basic deflate specification values and generic program options.
|
23
|
+
*/
|
24
|
+
|
25
|
+
#ifndef ZOPFLI_UTIL_H_
|
26
|
+
#define ZOPFLI_UTIL_H_
|
27
|
+
|
28
|
+
#include <string.h>
|
29
|
+
#include <stdlib.h>
|
30
|
+
|
31
|
+
/* Minimum and maximum length that can be encoded in deflate. */
|
32
|
+
#define ZOPFLI_MAX_MATCH 258
|
33
|
+
#define ZOPFLI_MIN_MATCH 3
|
34
|
+
|
35
|
+
/*
|
36
|
+
The window size for deflate. Must be a power of two. This should be 32768, the
|
37
|
+
maximum possible by the deflate spec. Anything less hurts compression more than
|
38
|
+
speed.
|
39
|
+
*/
|
40
|
+
#define ZOPFLI_WINDOW_SIZE 32768
|
41
|
+
|
42
|
+
/*
|
43
|
+
The window mask used to wrap indices into the window. This is why the
|
44
|
+
window size must be a power of two.
|
45
|
+
*/
|
46
|
+
#define ZOPFLI_WINDOW_MASK (ZOPFLI_WINDOW_SIZE - 1)
|
47
|
+
|
48
|
+
/*
|
49
|
+
A block structure of huge, non-smart, blocks to divide the input into, to allow
|
50
|
+
operating on huge files without exceeding memory, such as the 1GB wiki9 corpus.
|
51
|
+
The whole compression algorithm, including the smarter block splitting, will
|
52
|
+
be executed independently on each huge block.
|
53
|
+
Dividing into huge blocks hurts compression, but not much relative to the size.
|
54
|
+
Set this to, for example, 20MB (20000000). Set it to 0 to disable master blocks.
|
55
|
+
*/
|
56
|
+
#define ZOPFLI_MASTER_BLOCK_SIZE 20000000
|
57
|
+
|
58
|
+
/*
|
59
|
+
Used to initialize costs for example
|
60
|
+
*/
|
61
|
+
#define ZOPFLI_LARGE_FLOAT 1e30
|
62
|
+
|
63
|
+
/*
|
64
|
+
For longest match cache. max 256. Uses huge amounts of memory but makes it
|
65
|
+
faster. Uses this many times three bytes per single byte of the input data.
|
66
|
+
This is so because longest match finding has to find the exact distance
|
67
|
+
that belongs to each length for the best lz77 strategy.
|
68
|
+
Good values: e.g. 5, 8.
|
69
|
+
*/
|
70
|
+
#define ZOPFLI_CACHE_LENGTH 8
|
71
|
+
|
72
|
+
/*
|
73
|
+
limit the max hash chain hits for this hash value. This has an effect only
|
74
|
+
on files where the hash value is the same very often. On these files, this
|
75
|
+
gives worse compression (the value should ideally be 32768, which is the
|
76
|
+
ZOPFLI_WINDOW_SIZE, while zlib uses 4096 even for best level), but makes it
|
77
|
+
faster on some specific files.
|
78
|
+
Good value: e.g. 8192.
|
79
|
+
*/
|
80
|
+
#define ZOPFLI_MAX_CHAIN_HITS 8192
|
81
|
+
|
82
|
+
/*
|
83
|
+
Whether to use the longest match cache for ZopfliFindLongestMatch. This cache
|
84
|
+
consumes a lot of memory but speeds it up. No effect on compression size.
|
85
|
+
*/
|
86
|
+
#define ZOPFLI_LONGEST_MATCH_CACHE
|
87
|
+
|
88
|
+
/*
|
89
|
+
Enable to remember amount of successive identical bytes in the hash chain for
|
90
|
+
finding longest match
|
91
|
+
required for ZOPFLI_HASH_SAME_HASH and ZOPFLI_SHORTCUT_LONG_REPETITIONS
|
92
|
+
This has no effect on the compression result, and enabling it increases speed.
|
93
|
+
*/
|
94
|
+
#define ZOPFLI_HASH_SAME
|
95
|
+
|
96
|
+
/*
|
97
|
+
Switch to a faster hash based on the info from ZOPFLI_HASH_SAME once the
|
98
|
+
best length so far is long enough. This is way faster for files with lots of
|
99
|
+
identical bytes, on which the compressor is otherwise too slow. Regular files
|
100
|
+
are unaffected or maybe a tiny bit slower.
|
101
|
+
This has no effect on the compression result, only on speed.
|
102
|
+
*/
|
103
|
+
#define ZOPFLI_HASH_SAME_HASH
|
104
|
+
|
105
|
+
/*
|
106
|
+
Enable this, to avoid slowness for files which are a repetition of the same
|
107
|
+
character more than a multiple of ZOPFLI_MAX_MATCH times. This should not affect
|
108
|
+
the compression result.
|
109
|
+
*/
|
110
|
+
#define ZOPFLI_SHORTCUT_LONG_REPETITIONS
|
111
|
+
|
112
|
+
/*
|
113
|
+
Whether to use lazy matching in the greedy LZ77 implementation. This gives a
|
114
|
+
better result of ZopfliLZ77Greedy, but the effect this has on the optimal LZ77
|
115
|
+
varies from file to file.
|
116
|
+
*/
|
117
|
+
#define ZOPFLI_LAZY_MATCHING
|
118
|
+
|
119
|
+
/*
|
120
|
+
Gets the symbol for the given length, cfr. the DEFLATE spec.
|
121
|
+
Returns the symbol in the range [257-285] (inclusive)
|
122
|
+
*/
|
123
|
+
int ZopfliGetLengthSymbol(int l);
|
124
|
+
|
125
|
+
/* Gets the amount of extra bits for the given length, cfr. the DEFLATE spec. */
|
126
|
+
int ZopfliGetLengthExtraBits(int l);
|
127
|
+
|
128
|
+
/* Gets value of the extra bits for the given length, cfr. the DEFLATE spec. */
|
129
|
+
int ZopfliGetLengthExtraBitsValue(int l);
|
130
|
+
|
131
|
+
/* Gets the symbol for the given dist, cfr. the DEFLATE spec. */
|
132
|
+
int ZopfliGetDistSymbol(int dist);
|
133
|
+
|
134
|
+
/* Gets the amount of extra bits for the given dist, cfr. the DEFLATE spec. */
|
135
|
+
int ZopfliGetDistExtraBits(int dist);
|
136
|
+
|
137
|
+
/* Gets value of the extra bits for the given dist, cfr. the DEFLATE spec. */
|
138
|
+
int ZopfliGetDistExtraBitsValue(int dist);
|
139
|
+
|
140
|
+
/*
|
141
|
+
Appends value to dynamically allocated memory, doubling its allocation size
|
142
|
+
whenever needed.
|
143
|
+
|
144
|
+
value: the value to append, type T
|
145
|
+
data: pointer to the dynamic array to append to, type T**
|
146
|
+
size: pointer to the size of the array to append to, type size_t*. This is the
|
147
|
+
size that you consider the array to be, not the internal allocation size.
|
148
|
+
Precondition: allocated size of data is at least a power of two greater than or
|
149
|
+
equal than *size.
|
150
|
+
*/
|
151
|
+
#ifdef __cplusplus /* C++ cannot assign void* from malloc to *data */
|
152
|
+
#define ZOPFLI_APPEND_DATA(/* T */ value, /* T** */ data, /* size_t* */ size) {\
|
153
|
+
if (!((*size) & ((*size) - 1))) {\
|
154
|
+
/*double alloc size if it's a power of two*/\
|
155
|
+
void** data_void = reinterpret_cast<void**>(data);\
|
156
|
+
*data_void = (*size) == 0 ? malloc(sizeof(**data))\
|
157
|
+
: realloc((*data), (*size) * 2 * sizeof(**data));\
|
158
|
+
}\
|
159
|
+
(*data)[(*size)] = (value);\
|
160
|
+
(*size)++;\
|
161
|
+
}
|
162
|
+
#else /* C gives problems with strict-aliasing rules for (void**) cast */
|
163
|
+
#define ZOPFLI_APPEND_DATA(/* T */ value, /* T** */ data, /* size_t* */ size) {\
|
164
|
+
if (!((*size) & ((*size) - 1))) {\
|
165
|
+
/*double alloc size if it's a power of two*/\
|
166
|
+
(*data) = (*size) == 0 ? malloc(sizeof(**data))\
|
167
|
+
: realloc((*data), (*size) * 2 * sizeof(**data));\
|
168
|
+
}\
|
169
|
+
(*data)[(*size)] = (value);\
|
170
|
+
(*size)++;\
|
171
|
+
}
|
172
|
+
#endif
|
173
|
+
|
174
|
+
|
175
|
+
#endif /* ZOPFLI_UTIL_H_ */
|