zopfli 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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,24 @@
|
|
1
|
+
require "minitest/spec"
|
2
|
+
require "minitest/autorun"
|
3
|
+
require "zopfli"
|
4
|
+
require "stringio"
|
5
|
+
require "zlib"
|
6
|
+
|
7
|
+
describe Zopfli do
|
8
|
+
it "should compatible to gzip" do
|
9
|
+
fixture = fixtures("alice29.txt").read
|
10
|
+
|
11
|
+
sio = StringIO.new(Zopfli.deflate fixture)
|
12
|
+
|
13
|
+
uncompressed = nil
|
14
|
+
Zlib::GzipReader.wrap(sio) do |gz|
|
15
|
+
uncompressed = gz.read
|
16
|
+
end
|
17
|
+
|
18
|
+
fixture.must_equal uncompressed
|
19
|
+
end
|
20
|
+
|
21
|
+
def fixtures(name)
|
22
|
+
File.open(File.join File.dirname(__FILE__), "fixtures", name)
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,201 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright 2011 Google Inc.
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
@@ -0,0 +1,24 @@
|
|
1
|
+
Zopfli Compression Algorithm is a compression library programmed in C to perform
|
2
|
+
very good, but slow, deflate or zlib compression.
|
3
|
+
|
4
|
+
zopfli.c is separate from the library and contains an example program to create
|
5
|
+
very well compressed gzip files.
|
6
|
+
|
7
|
+
The basic functions to compress data are ZopfliDeflate in deflate.h,
|
8
|
+
ZopfliZlibCompress in zlib_container.h and ZopfliGzipCompress in
|
9
|
+
gzip_container.h. Use the ZopfliOptions object to set parameters that affect the
|
10
|
+
speed and compression. Use the ZopfliInitOptions function to place the default
|
11
|
+
values in the ZopfliOptions first.
|
12
|
+
|
13
|
+
Deflate creates a valid deflate stream in memory, see:
|
14
|
+
http://www.ietf.org/rfc/rfc1951.txt
|
15
|
+
ZlibCompress creates a valid zlib stream in memory, see:
|
16
|
+
http://www.ietf.org/rfc/rfc1950.txt
|
17
|
+
GzipCompress creates a valid gzip stream in memory, see:
|
18
|
+
http://www.ietf.org/rfc/rfc1952.txt
|
19
|
+
|
20
|
+
This library can only compress, not decompress. Existing zlib or deflate
|
21
|
+
libraries can decompress the data.
|
22
|
+
|
23
|
+
Zopfli Compression Algorithm was created by Lode Vandevenne and Jyrki
|
24
|
+
Alakuijala, based on an algorithm by Jyrki Alakuijala.
|
@@ -0,0 +1,344 @@
|
|
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 "blocksplitter.h"
|
21
|
+
|
22
|
+
#include <assert.h>
|
23
|
+
#include <stdio.h>
|
24
|
+
#include <stdlib.h>
|
25
|
+
|
26
|
+
#include "deflate.h"
|
27
|
+
#include "lz77.h"
|
28
|
+
#include "squeeze.h"
|
29
|
+
#include "tree.h"
|
30
|
+
#include "util.h"
|
31
|
+
|
32
|
+
/*
|
33
|
+
The "f" for the FindMinimum function below.
|
34
|
+
i: the current parameter of f(i)
|
35
|
+
context: for your implementation
|
36
|
+
*/
|
37
|
+
typedef double FindMinimumFun(size_t i, void* context);
|
38
|
+
|
39
|
+
/*
|
40
|
+
Finds minimum of function f(i) where is is of type size_t, f(i) is of type
|
41
|
+
double, i is in range start-end (excluding end).
|
42
|
+
*/
|
43
|
+
static size_t FindMinimum(FindMinimumFun f, void* context,
|
44
|
+
size_t start, size_t end) {
|
45
|
+
if (end - start < 1024) {
|
46
|
+
double best = ZOPFLI_LARGE_FLOAT;
|
47
|
+
size_t result = start;
|
48
|
+
size_t i;
|
49
|
+
for (i = start; i < end; i++) {
|
50
|
+
double v = f(i, context);
|
51
|
+
if (v < best) {
|
52
|
+
best = v;
|
53
|
+
result = i;
|
54
|
+
}
|
55
|
+
}
|
56
|
+
return result;
|
57
|
+
} else {
|
58
|
+
/* Try to find minimum faster by recursively checking multiple points. */
|
59
|
+
#define NUM 9 /* Good value: 9. */
|
60
|
+
size_t i;
|
61
|
+
size_t p[NUM];
|
62
|
+
double vp[NUM];
|
63
|
+
size_t besti;
|
64
|
+
double best;
|
65
|
+
double lastbest = ZOPFLI_LARGE_FLOAT;
|
66
|
+
size_t pos = start;
|
67
|
+
|
68
|
+
for (;;) {
|
69
|
+
if (end - start <= NUM) break;
|
70
|
+
|
71
|
+
for (i = 0; i < NUM; i++) {
|
72
|
+
p[i] = start + (i + 1) * ((end - start) / (NUM + 1));
|
73
|
+
vp[i] = f(p[i], context);
|
74
|
+
}
|
75
|
+
besti = 0;
|
76
|
+
best = vp[0];
|
77
|
+
for (i = 1; i < NUM; i++) {
|
78
|
+
if (vp[i] < best) {
|
79
|
+
best = vp[i];
|
80
|
+
besti = i;
|
81
|
+
}
|
82
|
+
}
|
83
|
+
if (best > lastbest) break;
|
84
|
+
|
85
|
+
start = besti == 0 ? start : p[besti - 1];
|
86
|
+
end = besti == NUM - 1 ? end : p[besti + 1];
|
87
|
+
|
88
|
+
pos = p[besti];
|
89
|
+
lastbest = best;
|
90
|
+
}
|
91
|
+
return pos;
|
92
|
+
#undef NUM
|
93
|
+
}
|
94
|
+
}
|
95
|
+
|
96
|
+
/*
|
97
|
+
Returns estimated cost of a block in bits. It includes the size to encode the
|
98
|
+
tree and the size to encode all literal, length and distance symbols and their
|
99
|
+
extra bits.
|
100
|
+
|
101
|
+
litlens: lz77 lit/lengths
|
102
|
+
dists: ll77 distances
|
103
|
+
lstart: start of block
|
104
|
+
lend: end of block (not inclusive)
|
105
|
+
*/
|
106
|
+
static double EstimateCost(const unsigned short* litlens,
|
107
|
+
const unsigned short* dists,
|
108
|
+
size_t lstart, size_t lend) {
|
109
|
+
return ZopfliCalculateBlockSize(litlens, dists, lstart, lend, 2);
|
110
|
+
}
|
111
|
+
|
112
|
+
typedef struct SplitCostContext {
|
113
|
+
const unsigned short* litlens;
|
114
|
+
const unsigned short* dists;
|
115
|
+
size_t llsize;
|
116
|
+
size_t start;
|
117
|
+
size_t end;
|
118
|
+
} SplitCostContext;
|
119
|
+
|
120
|
+
|
121
|
+
/*
|
122
|
+
Gets the cost which is the sum of the cost of the left and the right section
|
123
|
+
of the data.
|
124
|
+
type: FindMinimumFun
|
125
|
+
*/
|
126
|
+
static double SplitCost(size_t i, void* context) {
|
127
|
+
SplitCostContext* c = (SplitCostContext*)context;
|
128
|
+
return EstimateCost(c->litlens, c->dists, c->start, i) +
|
129
|
+
EstimateCost(c->litlens, c->dists, i, c->end);
|
130
|
+
}
|
131
|
+
|
132
|
+
static void AddSorted(size_t value, size_t** out, size_t* outsize) {
|
133
|
+
size_t i;
|
134
|
+
ZOPFLI_APPEND_DATA(value, out, outsize);
|
135
|
+
if (*outsize > 0) {
|
136
|
+
for (i = 0; i < *outsize - 1; i++) {
|
137
|
+
if ((*out)[i] > value) {
|
138
|
+
size_t j;
|
139
|
+
for (j = *outsize - 1; j > i; j--) {
|
140
|
+
(*out)[j] = (*out)[j - 1];
|
141
|
+
}
|
142
|
+
(*out)[i] = value;
|
143
|
+
break;
|
144
|
+
}
|
145
|
+
}
|
146
|
+
}
|
147
|
+
}
|
148
|
+
|
149
|
+
/*
|
150
|
+
Prints the block split points as decimal and hex values in the terminal.
|
151
|
+
*/
|
152
|
+
static void PrintBlockSplitPoints(const unsigned short* litlens,
|
153
|
+
const unsigned short* dists,
|
154
|
+
size_t llsize, const size_t* lz77splitpoints,
|
155
|
+
size_t nlz77points) {
|
156
|
+
size_t* splitpoints = 0;
|
157
|
+
size_t npoints = 0;
|
158
|
+
size_t i;
|
159
|
+
/* The input is given as lz77 indices, but we want to see the uncompressed
|
160
|
+
index values. */
|
161
|
+
size_t pos = 0;
|
162
|
+
if (nlz77points > 0) {
|
163
|
+
for (i = 0; i < llsize; i++) {
|
164
|
+
size_t length = dists[i] == 0 ? 1 : litlens[i];
|
165
|
+
if (lz77splitpoints[npoints] == i) {
|
166
|
+
ZOPFLI_APPEND_DATA(pos, &splitpoints, &npoints);
|
167
|
+
if (npoints == nlz77points) break;
|
168
|
+
}
|
169
|
+
pos += length;
|
170
|
+
}
|
171
|
+
}
|
172
|
+
assert(npoints == nlz77points);
|
173
|
+
|
174
|
+
fprintf(stderr, "block split points: ");
|
175
|
+
for (i = 0; i < npoints; i++) {
|
176
|
+
fprintf(stderr, "%d ", (int)splitpoints[i]);
|
177
|
+
}
|
178
|
+
fprintf(stderr, "(hex:");
|
179
|
+
for (i = 0; i < npoints; i++) {
|
180
|
+
fprintf(stderr, " %x", (int)splitpoints[i]);
|
181
|
+
}
|
182
|
+
fprintf(stderr, ")\n");
|
183
|
+
|
184
|
+
free(splitpoints);
|
185
|
+
}
|
186
|
+
|
187
|
+
/*
|
188
|
+
Finds next block to try to split, the largest of the available ones.
|
189
|
+
The largest is chosen to make sure that if only a limited amount of blocks is
|
190
|
+
requested, their sizes are spread evenly.
|
191
|
+
llsize: the size of the LL77 data, which is the size of the done array here.
|
192
|
+
done: array indicating which blocks starting at that position are no longer
|
193
|
+
splittable (splitting them increases rather than decreases cost).
|
194
|
+
splitpoints: the splitpoints found so far.
|
195
|
+
npoints: the amount of splitpoints found so far.
|
196
|
+
lstart: output variable, giving start of block.
|
197
|
+
lend: output variable, giving end of block.
|
198
|
+
returns 1 if a block was found, 0 if no block found (all are done).
|
199
|
+
*/
|
200
|
+
static int FindLargestSplittableBlock(
|
201
|
+
size_t llsize, const unsigned char* done,
|
202
|
+
const size_t* splitpoints, size_t npoints,
|
203
|
+
size_t* lstart, size_t* lend) {
|
204
|
+
size_t longest = 0;
|
205
|
+
int found = 0;
|
206
|
+
size_t i;
|
207
|
+
for (i = 0; i <= npoints; i++) {
|
208
|
+
size_t start = i == 0 ? 0 : splitpoints[i - 1];
|
209
|
+
size_t end = i == npoints ? llsize - 1 : splitpoints[i];
|
210
|
+
if (!done[start] && end - start > longest) {
|
211
|
+
*lstart = start;
|
212
|
+
*lend = end;
|
213
|
+
found = 1;
|
214
|
+
longest = end - start;
|
215
|
+
}
|
216
|
+
}
|
217
|
+
return found;
|
218
|
+
}
|
219
|
+
|
220
|
+
void ZopfliBlockSplitLZ77(const ZopfliOptions* options,
|
221
|
+
const unsigned short* litlens,
|
222
|
+
const unsigned short* dists,
|
223
|
+
size_t llsize, size_t maxblocks,
|
224
|
+
size_t** splitpoints, size_t* npoints) {
|
225
|
+
size_t lstart, lend;
|
226
|
+
size_t i;
|
227
|
+
size_t llpos = 0;
|
228
|
+
size_t numblocks = 1;
|
229
|
+
unsigned char* done;
|
230
|
+
double splitcost, origcost;
|
231
|
+
|
232
|
+
if (llsize < 10) return; /* This code fails on tiny files. */
|
233
|
+
|
234
|
+
done = (unsigned char*)malloc(llsize);
|
235
|
+
if (!done) exit(-1); /* Allocation failed. */
|
236
|
+
for (i = 0; i < llsize; i++) done[i] = 0;
|
237
|
+
|
238
|
+
lstart = 0;
|
239
|
+
lend = llsize;
|
240
|
+
for (;;) {
|
241
|
+
SplitCostContext c;
|
242
|
+
|
243
|
+
if (maxblocks > 0 && numblocks >= maxblocks) {
|
244
|
+
break;
|
245
|
+
}
|
246
|
+
|
247
|
+
c.litlens = litlens;
|
248
|
+
c.dists = dists;
|
249
|
+
c.llsize = llsize;
|
250
|
+
c.start = lstart;
|
251
|
+
c.end = lend;
|
252
|
+
assert(lstart < lend);
|
253
|
+
llpos = FindMinimum(SplitCost, &c, lstart + 1, lend);
|
254
|
+
|
255
|
+
assert(llpos > lstart);
|
256
|
+
assert(llpos < lend);
|
257
|
+
|
258
|
+
splitcost = EstimateCost(litlens, dists, lstart, llpos) +
|
259
|
+
EstimateCost(litlens, dists, llpos, lend);
|
260
|
+
origcost = EstimateCost(litlens, dists, lstart, lend);
|
261
|
+
|
262
|
+
if (splitcost > origcost || llpos == lstart + 1 || llpos == lend) {
|
263
|
+
done[lstart] = 1;
|
264
|
+
} else {
|
265
|
+
AddSorted(llpos, splitpoints, npoints);
|
266
|
+
numblocks++;
|
267
|
+
}
|
268
|
+
|
269
|
+
if (!FindLargestSplittableBlock(
|
270
|
+
llsize, done, *splitpoints, *npoints, &lstart, &lend)) {
|
271
|
+
break; /* No further split will probably reduce compression. */
|
272
|
+
}
|
273
|
+
|
274
|
+
if (lend - lstart < 10) {
|
275
|
+
break;
|
276
|
+
}
|
277
|
+
}
|
278
|
+
|
279
|
+
if (options->verbose) {
|
280
|
+
PrintBlockSplitPoints(litlens, dists, llsize, *splitpoints, *npoints);
|
281
|
+
}
|
282
|
+
|
283
|
+
free(done);
|
284
|
+
}
|
285
|
+
|
286
|
+
void ZopfliBlockSplit(const ZopfliOptions* options,
|
287
|
+
const unsigned char* in, size_t instart, size_t inend,
|
288
|
+
size_t maxblocks, size_t** splitpoints, size_t* npoints) {
|
289
|
+
size_t pos = 0;
|
290
|
+
size_t i;
|
291
|
+
ZopfliBlockState s;
|
292
|
+
size_t* lz77splitpoints = 0;
|
293
|
+
size_t nlz77points = 0;
|
294
|
+
ZopfliLZ77Store store;
|
295
|
+
|
296
|
+
ZopfliInitLZ77Store(&store);
|
297
|
+
|
298
|
+
s.options = options;
|
299
|
+
s.blockstart = instart;
|
300
|
+
s.blockend = inend;
|
301
|
+
#ifdef ZOPFLI_LONGEST_MATCH_CACHE
|
302
|
+
s.lmc = 0;
|
303
|
+
#endif
|
304
|
+
|
305
|
+
*npoints = 0;
|
306
|
+
*splitpoints = 0;
|
307
|
+
|
308
|
+
/* Unintuitively, Using a simple LZ77 method here instead of ZopfliLZ77Optimal
|
309
|
+
results in better blocks. */
|
310
|
+
ZopfliLZ77Greedy(&s, in, instart, inend, &store);
|
311
|
+
|
312
|
+
ZopfliBlockSplitLZ77(options,
|
313
|
+
store.litlens, store.dists, store.size, maxblocks,
|
314
|
+
&lz77splitpoints, &nlz77points);
|
315
|
+
|
316
|
+
/* Convert LZ77 positions to positions in the uncompressed input. */
|
317
|
+
pos = instart;
|
318
|
+
if (nlz77points > 0) {
|
319
|
+
for (i = 0; i < store.size; i++) {
|
320
|
+
size_t length = store.dists[i] == 0 ? 1 : store.litlens[i];
|
321
|
+
if (lz77splitpoints[*npoints] == i) {
|
322
|
+
ZOPFLI_APPEND_DATA(pos, splitpoints, npoints);
|
323
|
+
if (*npoints == nlz77points) break;
|
324
|
+
}
|
325
|
+
pos += length;
|
326
|
+
}
|
327
|
+
}
|
328
|
+
assert(*npoints == nlz77points);
|
329
|
+
|
330
|
+
free(lz77splitpoints);
|
331
|
+
ZopfliCleanLZ77Store(&store);
|
332
|
+
}
|
333
|
+
|
334
|
+
void ZopfliBlockSplitSimple(const unsigned char* in,
|
335
|
+
size_t instart, size_t inend,
|
336
|
+
size_t blocksize,
|
337
|
+
size_t** splitpoints, size_t* npoints) {
|
338
|
+
size_t i = instart;
|
339
|
+
while (i < inend) {
|
340
|
+
ZOPFLI_APPEND_DATA(i, splitpoints, npoints);
|
341
|
+
i += blocksize;
|
342
|
+
}
|
343
|
+
(void)in;
|
344
|
+
}
|