zopfli 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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, Compressed: %d, Compression: %f%% Removed\n",
76
+ (int)insize, (int)*outsize,
77
+ 100.0f * (float)(insize - *outsize) / (float)insize);
78
+ }
79
+ }
@@ -0,0 +1,42 @@
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
+ /*
30
+ Compresses according to the zlib specification and append the compressed
31
+ result to the output.
32
+
33
+ options: global program options
34
+ out: pointer to the dynamic output array to which the result is appended. Must
35
+ be freed after use.
36
+ outsize: pointer to the dynamic output array size.
37
+ */
38
+ void ZopfliZlibCompress(const ZopfliOptions* options,
39
+ const unsigned char* in, size_t insize,
40
+ unsigned char** out, size_t* outsize);
41
+
42
+ #endif /* ZOPFLI_ZLIB_H_ */
@@ -0,0 +1,71 @@
1
+ /*
2
+ Copyright 2013 Google Inc. All Rights Reserved.
3
+ Author: lode@google.com (Lode Vandevenne)
4
+ */
5
+
6
+ #ifndef UTIL_COMPRESSION_ZOPFLI_INTERNAL_ZOPFLI_H_
7
+ #define UTIL_COMPRESSION_ZOPFLI_INTERNAL_ZOPFLI_H_
8
+
9
+ #include <stdlib.h> /* for size_t */
10
+
11
+ /*
12
+ Options used throughout the program.
13
+ */
14
+ typedef struct ZopfliOptions {
15
+ /* Whether to print output */
16
+ int verbose;
17
+
18
+ /*
19
+ Maximum amount of times to rerun forward and backward pass to optimize LZ77
20
+ compression cost. Good values: 10, 15 for small files, 5 for files over
21
+ several MB in size or it will be too slow.
22
+ */
23
+ int numiterations;
24
+
25
+ /*
26
+ If true, splits the data in multiple deflate blocks with optimal choice
27
+ for the block boundaries. Block splitting gives better compression. Default:
28
+ true (1).
29
+ */
30
+ int blocksplitting;
31
+
32
+ /*
33
+ If true, chooses the optimal block split points only after doing the iterative
34
+ LZ77 compression. If false, chooses the block split points first, then does
35
+ iterative LZ77 on each individual block. Depending on the file, either first
36
+ or last gives the best compression. Default: false (0).
37
+ */
38
+ int blocksplittinglast;
39
+
40
+ /*
41
+ Maximum amount of blocks to split into (0 for unlimited, but this can give
42
+ extreme results that hurt compression on some files). Default value: 15.
43
+ */
44
+ int blocksplittingmax;
45
+ } ZopfliOptions;
46
+
47
+ /* Initializes options with default values. */
48
+ void ZopfliInitOptions(ZopfliOptions* options);
49
+
50
+ /* Output format */
51
+ typedef enum {
52
+ ZOPFLI_FORMAT_GZIP,
53
+ ZOPFLI_FORMAT_ZLIB,
54
+ ZOPFLI_FORMAT_DEFLATE
55
+ } ZopfliFormat;
56
+
57
+ /*
58
+ Compresses according to the given output format and appends the result to the
59
+ output.
60
+
61
+ options: global program options
62
+ output_type: the output format to use
63
+ out: pointer to the dynamic output array to which the result is appended. Must
64
+ be freed after use
65
+ outsize: pointer to the dynamic output array size
66
+ */
67
+ void ZopfliCompress(const ZopfliOptions* options, ZopfliFormat output_type,
68
+ const unsigned char* in, size_t insize,
69
+ unsigned char** out, size_t* outsize);
70
+
71
+ #endif /* UTIL_COMPRESSION_ZOPFLI_INTERNAL_ZOPFLI_H_ */
@@ -0,0 +1,204 @@
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
+ if (StringsEqual(argv[i], "-v")) options.verbose = 1;
139
+ else if (StringsEqual(argv[i], "-c")) output_to_stdout = 1;
140
+ else if (StringsEqual(argv[i], "--deflate")) {
141
+ output_type = ZOPFLI_FORMAT_DEFLATE;
142
+ }
143
+ else if (StringsEqual(argv[i], "--zlib")) output_type = ZOPFLI_FORMAT_ZLIB;
144
+ else if (StringsEqual(argv[i], "--gzip")) output_type = ZOPFLI_FORMAT_GZIP;
145
+ else if (StringsEqual(argv[i], "--i5")) options.numiterations = 5;
146
+ else if (StringsEqual(argv[i], "--i10")) options.numiterations = 10;
147
+ else if (StringsEqual(argv[i], "--i15")) options.numiterations = 15;
148
+ else if (StringsEqual(argv[i], "--i25")) options.numiterations = 25;
149
+ else if (StringsEqual(argv[i], "--i50")) options.numiterations = 50;
150
+ else if (StringsEqual(argv[i], "--i100")) options.numiterations = 100;
151
+ else if (StringsEqual(argv[i], "--i250")) options.numiterations = 250;
152
+ else if (StringsEqual(argv[i], "--i500")) options.numiterations = 500;
153
+ else if (StringsEqual(argv[i], "--i1000")) options.numiterations = 1000;
154
+ else if (StringsEqual(argv[i], "-h")) {
155
+ fprintf(stderr, "Usage: zopfli [OPTION]... FILE\n"
156
+ " -h gives this help\n"
157
+ " -c write the result on standard output, instead of disk"
158
+ " filename + '.gz'\n"
159
+ " -v verbose mode\n"
160
+ " --gzip output to gzip format (default)\n"
161
+ " --deflate output to deflate format instead of gzip\n"
162
+ " --zlib output to zlib format instead of gzip\n");
163
+ fprintf(stderr, " --i5 less compression, but faster\n"
164
+ " --i10 less compression, but faster\n"
165
+ " --i15 default compression, 15 iterations\n"
166
+ " --i25 more compression, but slower\n"
167
+ " --i50 more compression, but slower\n"
168
+ " --i100 more compression, but slower\n"
169
+ " --i250 more compression, but slower\n"
170
+ " --i500 more compression, but slower\n"
171
+ " --i1000 more compression, but slower\n");
172
+ return 0;
173
+ }
174
+ }
175
+
176
+ for (i = 1; i < argc; i++) {
177
+ if (argv[i][0] != '-') {
178
+ char* outfilename;
179
+ filename = argv[i];
180
+ if (output_to_stdout) {
181
+ outfilename = 0;
182
+ } else if (output_type == ZOPFLI_FORMAT_GZIP) {
183
+ outfilename = AddStrings(filename, ".gz");
184
+ } else if (output_type == ZOPFLI_FORMAT_ZLIB) {
185
+ outfilename = AddStrings(filename, ".zlib");
186
+ } else {
187
+ assert(output_type == ZOPFLI_FORMAT_DEFLATE);
188
+ outfilename = AddStrings(filename, ".deflate");
189
+ }
190
+ if (options.verbose && outfilename) {
191
+ fprintf(stderr, "Saving to: %s\n", outfilename);
192
+ }
193
+ CompressFile(&options, output_type, filename, outfilename);
194
+ free(outfilename);
195
+ }
196
+ }
197
+
198
+ if (!filename) {
199
+ fprintf(stderr,
200
+ "Please provide filename\nFor help, type: %s -h\n", argv[0]);
201
+ }
202
+
203
+ return 0;
204
+ }
@@ -0,0 +1,37 @@
1
+ /*
2
+ Copyright 2013 Google Inc. All Rights Reserved.
3
+ Author: lode@google.com (Lode Vandevenne)
4
+ */
5
+
6
+ #include "zopfli.h"
7
+
8
+ #include "deflate.h"
9
+ #include "gzip_container.h"
10
+ #include "zlib_container.h"
11
+
12
+ #include <assert.h>
13
+
14
+ void ZopfliInitOptions(ZopfliOptions* options) {
15
+ options->verbose = 0;
16
+ options->numiterations = 15;
17
+ options->blocksplitting = 1;
18
+ options->blocksplittinglast = 0;
19
+ options->blocksplittingmax = 15;
20
+ }
21
+
22
+ void ZopfliCompress(const ZopfliOptions* options, ZopfliFormat output_type,
23
+ const unsigned char* in, size_t insize,
24
+ unsigned char** out, size_t* outsize)
25
+ {
26
+ if (output_type == ZOPFLI_FORMAT_GZIP) {
27
+ ZopfliGzipCompress(options, in, insize, out, outsize);
28
+ } else if (output_type == ZOPFLI_FORMAT_ZLIB) {
29
+ ZopfliZlibCompress(options, in, insize, out, outsize);
30
+ } else if (output_type == ZOPFLI_FORMAT_DEFLATE) {
31
+ unsigned char bp = 0;
32
+ ZopfliDeflate(options, 2 /* Dynamic block */, 1,
33
+ in, insize, &bp, out, outsize);
34
+ } else {
35
+ assert(0);
36
+ }
37
+ }
@@ -0,0 +1,49 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'zopfli/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "zopfli"
8
+ spec.version = Zopfli::VERSION
9
+ spec.authors = ["miyucy"]
10
+ spec.email = ["fistfvck@gmail.com"]
11
+ spec.description = %q{zopfli}
12
+ spec.summary = %q{zopfli}
13
+ spec.homepage = "http://github.com/miyucy/zopfli"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+ spec.extensions = ["ext/extconf.rb"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "minitest"
25
+
26
+ # get an array of submodule dirs by executing 'pwd' inside each submodule
27
+ `git submodule --quiet foreach pwd`.split($\).each do |submodule_path|
28
+ # for each submodule, change working directory to that submodule
29
+ Dir.chdir(submodule_path) do
30
+
31
+ # issue git ls-files in submodule's directory
32
+ submodule_files = `git ls-files`.split($\)
33
+
34
+ # prepend the submodule path to create absolute file paths
35
+ submodule_files_fullpaths = submodule_files.map do |filename|
36
+ "#{submodule_path}/#{filename}"
37
+ end
38
+
39
+ # remove leading path parts to get paths relative to the gem's root dir
40
+ # (this assumes, that the gemspec resides in the gem's root dir)
41
+ submodule_files_paths = submodule_files_fullpaths.map do |filename|
42
+ filename.gsub "#{File.dirname(__FILE__)}/", ""
43
+ end
44
+
45
+ # add relative paths to gem.files
46
+ spec.files += submodule_files_paths
47
+ end
48
+ end
49
+ end