zopfli-bin 0.1.0

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.
Files changed (55) hide show
  1. checksums.yaml +7 -0
  2. data/.document +5 -0
  3. data/.gitmodules +3 -0
  4. data/.testguardrc +1 -0
  5. data/Gemfile +17 -0
  6. data/Gemfile.lock +111 -0
  7. data/LICENSE.txt +20 -0
  8. data/README.rdoc +19 -0
  9. data/Rakefile +39 -0
  10. data/VERSION +1 -0
  11. data/ext/Makefile +8 -0
  12. data/ext/extconf.rb +4 -0
  13. data/lib/zopfli-bin.rb +5 -0
  14. data/lib/zopfli/bin.rb +34 -0
  15. data/test/helper.rb +19 -0
  16. data/test/test_zopfli-bin.rb +33 -0
  17. data/vendor/zopfli/CONTRIBUTORS +7 -0
  18. data/vendor/zopfli/COPYING +201 -0
  19. data/vendor/zopfli/Makefile +37 -0
  20. data/vendor/zopfli/README +32 -0
  21. data/vendor/zopfli/README.zopflipng +35 -0
  22. data/vendor/zopfli/src/zopfli/blocksplitter.c +342 -0
  23. data/vendor/zopfli/src/zopfli/blocksplitter.h +77 -0
  24. data/vendor/zopfli/src/zopfli/cache.c +119 -0
  25. data/vendor/zopfli/src/zopfli/cache.h +66 -0
  26. data/vendor/zopfli/src/zopfli/deflate.c +866 -0
  27. data/vendor/zopfli/src/zopfli/deflate.h +86 -0
  28. data/vendor/zopfli/src/zopfli/gzip_container.c +117 -0
  29. data/vendor/zopfli/src/zopfli/gzip_container.h +50 -0
  30. data/vendor/zopfli/src/zopfli/hash.c +135 -0
  31. data/vendor/zopfli/src/zopfli/hash.h +70 -0
  32. data/vendor/zopfli/src/zopfli/katajainen.c +251 -0
  33. data/vendor/zopfli/src/zopfli/katajainen.h +42 -0
  34. data/vendor/zopfli/src/zopfli/lz77.c +482 -0
  35. data/vendor/zopfli/src/zopfli/lz77.h +129 -0
  36. data/vendor/zopfli/src/zopfli/squeeze.c +546 -0
  37. data/vendor/zopfli/src/zopfli/squeeze.h +60 -0
  38. data/vendor/zopfli/src/zopfli/tree.c +101 -0
  39. data/vendor/zopfli/src/zopfli/tree.h +51 -0
  40. data/vendor/zopfli/src/zopfli/util.c +213 -0
  41. data/vendor/zopfli/src/zopfli/util.h +175 -0
  42. data/vendor/zopfli/src/zopfli/zlib_container.c +79 -0
  43. data/vendor/zopfli/src/zopfli/zlib_container.h +50 -0
  44. data/vendor/zopfli/src/zopfli/zopfli.h +97 -0
  45. data/vendor/zopfli/src/zopfli/zopfli_bin.c +203 -0
  46. data/vendor/zopfli/src/zopfli/zopfli_lib.c +42 -0
  47. data/vendor/zopfli/src/zopflipng/lodepng/lodepng.cpp +6260 -0
  48. data/vendor/zopfli/src/zopflipng/lodepng/lodepng.h +1716 -0
  49. data/vendor/zopfli/src/zopflipng/lodepng/lodepng_util.cpp +656 -0
  50. data/vendor/zopfli/src/zopflipng/lodepng/lodepng_util.h +151 -0
  51. data/vendor/zopfli/src/zopflipng/zopflipng_bin.cc +407 -0
  52. data/vendor/zopfli/src/zopflipng/zopflipng_lib.cc +425 -0
  53. data/vendor/zopfli/src/zopflipng/zopflipng_lib.h +79 -0
  54. data/zopfli-bin.gemspec +119 -0
  55. metadata +225 -0
@@ -0,0 +1,79 @@
1
+ /*
2
+ Copyright 2013 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 "zlib_container.h"
21
+ #include "util.h"
22
+
23
+ #include <stdio.h>
24
+
25
+ #include "deflate.h"
26
+
27
+
28
+ /* Calculates the adler32 checksum of the data */
29
+ static unsigned adler32(const unsigned char* data, size_t size)
30
+ {
31
+ static const unsigned sums_overflow = 5550;
32
+ unsigned s1 = 1;
33
+ unsigned s2 = 1 >> 16;
34
+
35
+ while (size > 0) {
36
+ size_t amount = size > sums_overflow ? sums_overflow : size;
37
+ size -= amount;
38
+ while (amount > 0) {
39
+ s1 += (*data++);
40
+ s2 += s1;
41
+ amount--;
42
+ }
43
+ s1 %= 65521;
44
+ s2 %= 65521;
45
+ }
46
+
47
+ return (s2 << 16) | s1;
48
+ }
49
+
50
+ void ZopfliZlibCompress(const ZopfliOptions* options,
51
+ const unsigned char* in, size_t insize,
52
+ unsigned char** out, size_t* outsize) {
53
+ unsigned char bitpointer = 0;
54
+ unsigned checksum = adler32(in, (unsigned)insize);
55
+ unsigned cmf = 120; /* CM 8, CINFO 7. See zlib spec.*/
56
+ unsigned flevel = 0;
57
+ unsigned fdict = 0;
58
+ unsigned cmfflg = 256 * cmf + fdict * 32 + flevel * 64;
59
+ unsigned fcheck = 31 - cmfflg % 31;
60
+ cmfflg += fcheck;
61
+
62
+ ZOPFLI_APPEND_DATA(cmfflg / 256, out, outsize);
63
+ ZOPFLI_APPEND_DATA(cmfflg % 256, out, outsize);
64
+
65
+ ZopfliDeflate(options, 2 /* dynamic block */, 1 /* final */,
66
+ in, insize, &bitpointer, out, outsize);
67
+
68
+ ZOPFLI_APPEND_DATA((checksum >> 24) % 256, out, outsize);
69
+ ZOPFLI_APPEND_DATA((checksum >> 16) % 256, out, outsize);
70
+ ZOPFLI_APPEND_DATA((checksum >> 8) % 256, out, outsize);
71
+ ZOPFLI_APPEND_DATA(checksum % 256, out, outsize);
72
+
73
+ if (options->verbose) {
74
+ fprintf(stderr,
75
+ "Original Size: %d, Zlib: %d, Compression: %f%% Removed\n",
76
+ (int)insize, (int)*outsize,
77
+ 100.0 * (double)(insize - *outsize) / (double)insize);
78
+ }
79
+ }
@@ -0,0 +1,50 @@
1
+ /*
2
+ Copyright 2013 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
+ #ifndef ZOPFLI_ZLIB_H_
21
+ #define ZOPFLI_ZLIB_H_
22
+
23
+ /*
24
+ Functions to compress according to the Zlib specification.
25
+ */
26
+
27
+ #include "zopfli.h"
28
+
29
+ #ifdef __cplusplus
30
+ extern "C" {
31
+ #endif
32
+
33
+ /*
34
+ Compresses according to the zlib specification and append the compressed
35
+ result to the output.
36
+
37
+ options: global program options
38
+ out: pointer to the dynamic output array to which the result is appended. Must
39
+ be freed after use.
40
+ outsize: pointer to the dynamic output array size.
41
+ */
42
+ void ZopfliZlibCompress(const ZopfliOptions* options,
43
+ const unsigned char* in, size_t insize,
44
+ unsigned char** out, size_t* outsize);
45
+
46
+ #ifdef __cplusplus
47
+ } // extern "C"
48
+ #endif
49
+
50
+ #endif /* ZOPFLI_ZLIB_H_ */
@@ -0,0 +1,97 @@
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
+ #ifndef ZOPFLI_ZOPFLI_H_
21
+ #define ZOPFLI_ZOPFLI_H_
22
+
23
+ #include <stddef.h>
24
+ #include <stdlib.h> /* for size_t */
25
+
26
+ #ifdef __cplusplus
27
+ extern "C" {
28
+ #endif
29
+
30
+ /*
31
+ Options used throughout the program.
32
+ */
33
+ typedef struct ZopfliOptions {
34
+ /* Whether to print output */
35
+ int verbose;
36
+
37
+ /* Whether to print more detailed output */
38
+ int verbose_more;
39
+
40
+ /*
41
+ Maximum amount of times to rerun forward and backward pass to optimize LZ77
42
+ compression cost. Good values: 10, 15 for small files, 5 for files over
43
+ several MB in size or it will be too slow.
44
+ */
45
+ int numiterations;
46
+
47
+ /*
48
+ If true, splits the data in multiple deflate blocks with optimal choice
49
+ for the block boundaries. Block splitting gives better compression. Default:
50
+ true (1).
51
+ */
52
+ int blocksplitting;
53
+
54
+ /*
55
+ If true, chooses the optimal block split points only after doing the iterative
56
+ LZ77 compression. If false, chooses the block split points first, then does
57
+ iterative LZ77 on each individual block. Depending on the file, either first
58
+ or last gives the best compression. Default: false (0).
59
+ */
60
+ int blocksplittinglast;
61
+
62
+ /*
63
+ Maximum amount of blocks to split into (0 for unlimited, but this can give
64
+ extreme results that hurt compression on some files). Default value: 15.
65
+ */
66
+ int blocksplittingmax;
67
+ } ZopfliOptions;
68
+
69
+ /* Initializes options with default values. */
70
+ void ZopfliInitOptions(ZopfliOptions* options);
71
+
72
+ /* Output format */
73
+ typedef enum {
74
+ ZOPFLI_FORMAT_GZIP,
75
+ ZOPFLI_FORMAT_ZLIB,
76
+ ZOPFLI_FORMAT_DEFLATE
77
+ } ZopfliFormat;
78
+
79
+ /*
80
+ Compresses according to the given output format and appends the result to the
81
+ output.
82
+
83
+ options: global program options
84
+ output_type: the output format to use
85
+ out: pointer to the dynamic output array to which the result is appended. Must
86
+ be freed after use
87
+ outsize: pointer to the dynamic output array size
88
+ */
89
+ void ZopfliCompress(const ZopfliOptions* options, ZopfliFormat output_type,
90
+ const unsigned char* in, size_t insize,
91
+ unsigned char** out, size_t* outsize);
92
+
93
+ #ifdef __cplusplus
94
+ } // extern "C"
95
+ #endif
96
+
97
+ #endif /* ZOPFLI_ZOPFLI_H_ */
@@ -0,0 +1,203 @@
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
+ Zopfli compressor program. It can output gzip-, zlib- or deflate-compatible
22
+ data. By default it creates a .gz file. This tool can only compress, not
23
+ decompress. Decompression can be done by any standard gzip, zlib or deflate
24
+ decompressor.
25
+ */
26
+
27
+ #include <assert.h>
28
+ #include <stdio.h>
29
+ #include <stdlib.h>
30
+ #include <string.h>
31
+
32
+ #include "deflate.h"
33
+ #include "gzip_container.h"
34
+ #include "zlib_container.h"
35
+
36
+ /*
37
+ Loads a file into a memory array.
38
+ */
39
+ static void LoadFile(const char* filename,
40
+ unsigned char** out, size_t* outsize) {
41
+ FILE* file;
42
+
43
+ *out = 0;
44
+ *outsize = 0;
45
+ file = fopen(filename, "rb");
46
+ if (!file) return;
47
+
48
+ fseek(file , 0 , SEEK_END);
49
+ *outsize = ftell(file);
50
+ rewind(file);
51
+
52
+ *out = (unsigned char*)malloc(*outsize);
53
+
54
+ if (*outsize && (*out)) {
55
+ size_t testsize = fread(*out, 1, *outsize, file);
56
+ if (testsize != *outsize) {
57
+ /* It could be a directory */
58
+ free(*out);
59
+ *out = 0;
60
+ *outsize = 0;
61
+ }
62
+ }
63
+
64
+ assert(!(*outsize) || out); /* If size is not zero, out must be allocated. */
65
+ fclose(file);
66
+ }
67
+
68
+ /*
69
+ Saves a file from a memory array, overwriting the file if it existed.
70
+ */
71
+ static void SaveFile(const char* filename,
72
+ const unsigned char* in, size_t insize) {
73
+ FILE* file = fopen(filename, "wb" );
74
+ assert(file);
75
+ fwrite((char*)in, 1, insize, file);
76
+ fclose(file);
77
+ }
78
+
79
+ /*
80
+ outfilename: filename to write output to, or 0 to write to stdout instead
81
+ */
82
+ static void CompressFile(const ZopfliOptions* options,
83
+ ZopfliFormat output_type,
84
+ const char* infilename,
85
+ const char* outfilename) {
86
+ unsigned char* in;
87
+ size_t insize;
88
+ unsigned char* out = 0;
89
+ size_t outsize = 0;
90
+ LoadFile(infilename, &in, &insize);
91
+ if (insize == 0) {
92
+ fprintf(stderr, "Invalid filename: %s\n", infilename);
93
+ return;
94
+ }
95
+
96
+ ZopfliCompress(options, output_type, in, insize, &out, &outsize);
97
+
98
+ if (outfilename) {
99
+ SaveFile(outfilename, out, outsize);
100
+ } else {
101
+ size_t i;
102
+ for (i = 0; i < outsize; i++) {
103
+ /* Works only if terminal does not convert newlines. */
104
+ printf("%c", out[i]);
105
+ }
106
+ }
107
+
108
+ free(out);
109
+ free(in);
110
+ }
111
+
112
+ /*
113
+ Add two strings together. Size does not matter. Result must be freed.
114
+ */
115
+ static char* AddStrings(const char* str1, const char* str2) {
116
+ size_t len = strlen(str1) + strlen(str2);
117
+ char* result = (char*)malloc(len + 1);
118
+ if (!result) exit(-1); /* Allocation failed. */
119
+ strcpy(result, str1);
120
+ strcat(result, str2);
121
+ return result;
122
+ }
123
+
124
+ static char StringsEqual(const char* str1, const char* str2) {
125
+ return strcmp(str1, str2) == 0;
126
+ }
127
+
128
+ int main(int argc, char* argv[]) {
129
+ ZopfliOptions options;
130
+ ZopfliFormat output_type = ZOPFLI_FORMAT_GZIP;
131
+ const char* filename = 0;
132
+ int output_to_stdout = 0;
133
+ int i;
134
+
135
+ ZopfliInitOptions(&options);
136
+
137
+ for (i = 1; i < argc; i++) {
138
+ const char* arg = argv[i];
139
+ if (StringsEqual(arg, "-v")) options.verbose = 1;
140
+ else if (StringsEqual(arg, "-c")) output_to_stdout = 1;
141
+ else if (StringsEqual(arg, "--deflate")) {
142
+ output_type = ZOPFLI_FORMAT_DEFLATE;
143
+ }
144
+ else if (StringsEqual(arg, "--zlib")) output_type = ZOPFLI_FORMAT_ZLIB;
145
+ else if (StringsEqual(arg, "--gzip")) output_type = ZOPFLI_FORMAT_GZIP;
146
+ else if (StringsEqual(arg, "--splitlast")) options.blocksplittinglast = 1;
147
+ else if (arg[0] == '-' && arg[1] == '-' && arg[2] == 'i'
148
+ && arg[3] >= '0' && arg[3] <= '9') {
149
+ options.numiterations = atoi(arg + 3);
150
+ }
151
+ else if (StringsEqual(arg, "-h")) {
152
+ fprintf(stderr,
153
+ "Usage: zopfli [OPTION]... FILE\n"
154
+ " -h gives this help\n"
155
+ " -c write the result on standard output, instead of disk"
156
+ " filename + '.gz'\n"
157
+ " -v verbose mode\n"
158
+ " --i# perform # iterations (default 15). More gives"
159
+ " more compression but is slower."
160
+ " Examples: --i10, --i50, --i1000\n");
161
+ fprintf(stderr,
162
+ " --gzip output to gzip format (default)\n"
163
+ " --zlib output to zlib format instead of gzip\n"
164
+ " --deflate output to deflate format instead of gzip\n"
165
+ " --splitlast do block splitting last instead of first\n");
166
+ return 0;
167
+ }
168
+ }
169
+
170
+ if (options.numiterations < 1) {
171
+ fprintf(stderr, "Error: must have 1 or more iterations");
172
+ return 0;
173
+ }
174
+
175
+ for (i = 1; i < argc; i++) {
176
+ if (argv[i][0] != '-') {
177
+ char* outfilename;
178
+ filename = argv[i];
179
+ if (output_to_stdout) {
180
+ outfilename = 0;
181
+ } else if (output_type == ZOPFLI_FORMAT_GZIP) {
182
+ outfilename = AddStrings(filename, ".gz");
183
+ } else if (output_type == ZOPFLI_FORMAT_ZLIB) {
184
+ outfilename = AddStrings(filename, ".zlib");
185
+ } else {
186
+ assert(output_type == ZOPFLI_FORMAT_DEFLATE);
187
+ outfilename = AddStrings(filename, ".deflate");
188
+ }
189
+ if (options.verbose && outfilename) {
190
+ fprintf(stderr, "Saving to: %s\n", outfilename);
191
+ }
192
+ CompressFile(&options, output_type, filename, outfilename);
193
+ free(outfilename);
194
+ }
195
+ }
196
+
197
+ if (!filename) {
198
+ fprintf(stderr,
199
+ "Please provide filename\nFor help, type: %s -h\n", argv[0]);
200
+ }
201
+
202
+ return 0;
203
+ }
@@ -0,0 +1,42 @@
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 "zopfli.h"
21
+
22
+ #include "deflate.h"
23
+ #include "gzip_container.h"
24
+ #include "zlib_container.h"
25
+
26
+ #include <assert.h>
27
+
28
+ void ZopfliCompress(const ZopfliOptions* options, ZopfliFormat output_type,
29
+ const unsigned char* in, size_t insize,
30
+ unsigned char** out, size_t* outsize) {
31
+ if (output_type == ZOPFLI_FORMAT_GZIP) {
32
+ ZopfliGzipCompress(options, in, insize, out, outsize);
33
+ } else if (output_type == ZOPFLI_FORMAT_ZLIB) {
34
+ ZopfliZlibCompress(options, in, insize, out, outsize);
35
+ } else if (output_type == ZOPFLI_FORMAT_DEFLATE) {
36
+ unsigned char bp = 0;
37
+ ZopfliDeflate(options, 2 /* Dynamic block */, 1,
38
+ in, insize, &bp, out, outsize);
39
+ } else {
40
+ assert(0);
41
+ }
42
+ }