zopfli-bin 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
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,86 @@
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_DEFLATE_H_
21
+ #define ZOPFLI_DEFLATE_H_
22
+
23
+ /*
24
+ Functions to compress according to the DEFLATE specification, using the
25
+ "squeeze" LZ77 compression backend.
26
+ */
27
+
28
+ #include "zopfli.h"
29
+
30
+ #ifdef __cplusplus
31
+ extern "C" {
32
+ #endif
33
+
34
+ /*
35
+ Compresses according to the deflate specification and append the compressed
36
+ result to the output.
37
+ This function will usually output multiple deflate blocks. If final is 1, then
38
+ the final bit will be set on the last block.
39
+
40
+ options: global program options
41
+ btype: the deflate block type. Use 2 for best compression.
42
+ -0: non compressed blocks (00)
43
+ -1: blocks with fixed tree (01)
44
+ -2: blocks with dynamic tree (10)
45
+ final: whether this is the last section of the input, sets the final bit to the
46
+ last deflate block.
47
+ in: the input bytes
48
+ insize: number of input bytes
49
+ bp: bit pointer for the output array. This must initially be 0, and for
50
+ consecutive calls must be reused (it can have values from 0-7). This is
51
+ because deflate appends blocks as bit-based data, rather than on byte
52
+ boundaries.
53
+ out: pointer to the dynamic output array to which the result is appended. Must
54
+ be freed after use.
55
+ outsize: pointer to the dynamic output array size.
56
+ */
57
+ void ZopfliDeflate(const ZopfliOptions* options, int btype, int final,
58
+ const unsigned char* in, size_t insize,
59
+ unsigned char* bp, unsigned char** out, size_t* outsize);
60
+
61
+ /*
62
+ Like ZopfliDeflate, but allows to specify start and end byte with instart and
63
+ inend. Only that part is compressed, but earlier bytes are still used for the
64
+ back window.
65
+ */
66
+ void ZopfliDeflatePart(const ZopfliOptions* options, int btype, int final,
67
+ const unsigned char* in, size_t instart, size_t inend,
68
+ unsigned char* bp, unsigned char** out,
69
+ size_t* outsize);
70
+
71
+ /*
72
+ Calculates block size in bits.
73
+ litlens: lz77 lit/lengths
74
+ dists: ll77 distances
75
+ lstart: start of block
76
+ lend: end of block (not inclusive)
77
+ */
78
+ double ZopfliCalculateBlockSize(const unsigned short* litlens,
79
+ const unsigned short* dists,
80
+ size_t lstart, size_t lend, int btype);
81
+
82
+ #ifdef __cplusplus
83
+ } // extern "C"
84
+ #endif
85
+
86
+ #endif /* ZOPFLI_DEFLATE_H_ */
@@ -0,0 +1,117 @@
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 "gzip_container.h"
21
+ #include "util.h"
22
+
23
+ #include <stdio.h>
24
+
25
+ #include "deflate.h"
26
+
27
+ /* Table of CRCs of all 8-bit messages. */
28
+ static unsigned long crc_table[256];
29
+
30
+ /* Flag: has the table been computed? Initially false. */
31
+ static int crc_table_computed = 0;
32
+
33
+ /* Makes the table for a fast CRC. */
34
+ static void MakeCRCTable() {
35
+ unsigned long c;
36
+ int n, k;
37
+ for (n = 0; n < 256; n++) {
38
+ c = (unsigned long) n;
39
+ for (k = 0; k < 8; k++) {
40
+ if (c & 1) {
41
+ c = 0xedb88320L ^ (c >> 1);
42
+ } else {
43
+ c = c >> 1;
44
+ }
45
+ }
46
+ crc_table[n] = c;
47
+ }
48
+ crc_table_computed = 1;
49
+ }
50
+
51
+
52
+ /*
53
+ Updates a running crc with the bytes buf[0..len-1] and returns
54
+ the updated crc. The crc should be initialized to zero.
55
+ */
56
+ static unsigned long UpdateCRC(unsigned long crc,
57
+ const unsigned char *buf, size_t len) {
58
+ unsigned long c = crc ^ 0xffffffffL;
59
+ unsigned n;
60
+
61
+ if (!crc_table_computed)
62
+ MakeCRCTable();
63
+ for (n = 0; n < len; n++) {
64
+ c = crc_table[(c ^ buf[n]) & 0xff] ^ (c >> 8);
65
+ }
66
+ return c ^ 0xffffffffL;
67
+ }
68
+
69
+ /* Returns the CRC of the bytes buf[0..len-1]. */
70
+ static unsigned long CRC(const unsigned char* buf, int len) {
71
+ return UpdateCRC(0L, buf, len);
72
+ }
73
+
74
+ /*
75
+ Compresses the data according to the gzip specification.
76
+ */
77
+ void ZopfliGzipCompress(const ZopfliOptions* options,
78
+ const unsigned char* in, size_t insize,
79
+ unsigned char** out, size_t* outsize) {
80
+ unsigned long crcvalue = CRC(in, insize);
81
+ unsigned char bp = 0;
82
+
83
+ ZOPFLI_APPEND_DATA(31, out, outsize); /* ID1 */
84
+ ZOPFLI_APPEND_DATA(139, out, outsize); /* ID2 */
85
+ ZOPFLI_APPEND_DATA(8, out, outsize); /* CM */
86
+ ZOPFLI_APPEND_DATA(0, out, outsize); /* FLG */
87
+ /* MTIME */
88
+ ZOPFLI_APPEND_DATA(0, out, outsize);
89
+ ZOPFLI_APPEND_DATA(0, out, outsize);
90
+ ZOPFLI_APPEND_DATA(0, out, outsize);
91
+ ZOPFLI_APPEND_DATA(0, out, outsize);
92
+
93
+ ZOPFLI_APPEND_DATA(2, out, outsize); /* XFL, 2 indicates best compression. */
94
+ ZOPFLI_APPEND_DATA(3, out, outsize); /* OS follows Unix conventions. */
95
+
96
+ ZopfliDeflate(options, 2 /* Dynamic block */, 1,
97
+ in, insize, &bp, out, outsize);
98
+
99
+ /* CRC */
100
+ ZOPFLI_APPEND_DATA(crcvalue % 256, out, outsize);
101
+ ZOPFLI_APPEND_DATA((crcvalue >> 8) % 256, out, outsize);
102
+ ZOPFLI_APPEND_DATA((crcvalue >> 16) % 256, out, outsize);
103
+ ZOPFLI_APPEND_DATA((crcvalue >> 24) % 256, out, outsize);
104
+
105
+ /* ISIZE */
106
+ ZOPFLI_APPEND_DATA(insize % 256, out, outsize);
107
+ ZOPFLI_APPEND_DATA((insize >> 8) % 256, out, outsize);
108
+ ZOPFLI_APPEND_DATA((insize >> 16) % 256, out, outsize);
109
+ ZOPFLI_APPEND_DATA((insize >> 24) % 256, out, outsize);
110
+
111
+ if (options->verbose) {
112
+ fprintf(stderr,
113
+ "Original Size: %d, Gzip: %d, Compression: %f%% Removed\n",
114
+ (int)insize, (int)*outsize,
115
+ 100.0 * (double)(insize - *outsize) / (double)insize);
116
+ }
117
+ }
@@ -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_GZIP_H_
21
+ #define ZOPFLI_GZIP_H_
22
+
23
+ /*
24
+ Functions to compress according to the Gzip specification.
25
+ */
26
+
27
+ #include "zopfli.h"
28
+
29
+ #ifdef __cplusplus
30
+ extern "C" {
31
+ #endif
32
+
33
+ /*
34
+ Compresses according to the gzip 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 ZopfliGzipCompress(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_GZIP_H_ */
@@ -0,0 +1,135 @@
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 "hash.h"
21
+
22
+ #include <assert.h>
23
+ #include <stdio.h>
24
+ #include <stdlib.h>
25
+
26
+ #define HASH_SHIFT 5
27
+ #define HASH_MASK 32767
28
+
29
+ void ZopfliInitHash(size_t window_size, ZopfliHash* h) {
30
+ size_t i;
31
+
32
+ h->val = 0;
33
+ h->head = (int*)malloc(sizeof(*h->head) * 65536);
34
+ h->prev = (unsigned short*)malloc(sizeof(*h->prev) * window_size);
35
+ h->hashval = (int*)malloc(sizeof(*h->hashval) * window_size);
36
+ for (i = 0; i < 65536; i++) {
37
+ h->head[i] = -1; /* -1 indicates no head so far. */
38
+ }
39
+ for (i = 0; i < window_size; i++) {
40
+ h->prev[i] = i; /* If prev[j] == j, then prev[j] is uninitialized. */
41
+ h->hashval[i] = -1;
42
+ }
43
+
44
+ #ifdef ZOPFLI_HASH_SAME
45
+ h->same = (unsigned short*)malloc(sizeof(*h->same) * window_size);
46
+ for (i = 0; i < window_size; i++) {
47
+ h->same[i] = 0;
48
+ }
49
+ #endif
50
+
51
+ #ifdef ZOPFLI_HASH_SAME_HASH
52
+ h->val2 = 0;
53
+ h->head2 = (int*)malloc(sizeof(*h->head2) * 65536);
54
+ h->prev2 = (unsigned short*)malloc(sizeof(*h->prev2) * window_size);
55
+ h->hashval2 = (int*)malloc(sizeof(*h->hashval2) * window_size);
56
+ for (i = 0; i < 65536; i++) {
57
+ h->head2[i] = -1;
58
+ }
59
+ for (i = 0; i < window_size; i++) {
60
+ h->prev2[i] = i;
61
+ h->hashval2[i] = -1;
62
+ }
63
+ #endif
64
+ }
65
+
66
+ void ZopfliCleanHash(ZopfliHash* h) {
67
+ free(h->head);
68
+ free(h->prev);
69
+ free(h->hashval);
70
+
71
+ #ifdef ZOPFLI_HASH_SAME_HASH
72
+ free(h->head2);
73
+ free(h->prev2);
74
+ free(h->hashval2);
75
+ #endif
76
+
77
+ #ifdef ZOPFLI_HASH_SAME
78
+ free(h->same);
79
+ #endif
80
+ }
81
+
82
+ /*
83
+ Update the sliding hash value with the given byte. All calls to this function
84
+ must be made on consecutive input characters. Since the hash value exists out
85
+ of multiple input bytes, a few warmups with this function are needed initially.
86
+ */
87
+ static void UpdateHashValue(ZopfliHash* h, unsigned char c) {
88
+ h->val = (((h->val) << HASH_SHIFT) ^ (c)) & HASH_MASK;
89
+ }
90
+
91
+ void ZopfliUpdateHash(const unsigned char* array, size_t pos, size_t end,
92
+ ZopfliHash* h) {
93
+ unsigned short hpos = pos & ZOPFLI_WINDOW_MASK;
94
+ #ifdef ZOPFLI_HASH_SAME
95
+ size_t amount = 0;
96
+ #endif
97
+
98
+ UpdateHashValue(h, pos + ZOPFLI_MIN_MATCH <= end ?
99
+ array[pos + ZOPFLI_MIN_MATCH - 1] : 0);
100
+ h->hashval[hpos] = h->val;
101
+ if (h->head[h->val] != -1 && h->hashval[h->head[h->val]] == h->val) {
102
+ h->prev[hpos] = h->head[h->val];
103
+ }
104
+ else h->prev[hpos] = hpos;
105
+ h->head[h->val] = hpos;
106
+
107
+ #ifdef ZOPFLI_HASH_SAME
108
+ /* Update "same". */
109
+ if (h->same[(pos - 1) & ZOPFLI_WINDOW_MASK] > 1) {
110
+ amount = h->same[(pos - 1) & ZOPFLI_WINDOW_MASK] - 1;
111
+ }
112
+ while (pos + amount + 1 < end &&
113
+ array[pos] == array[pos + amount + 1] && amount < (unsigned short)(-1)) {
114
+ amount++;
115
+ }
116
+ h->same[hpos] = amount;
117
+ #endif
118
+
119
+ #ifdef ZOPFLI_HASH_SAME_HASH
120
+ h->val2 = ((h->same[hpos] - ZOPFLI_MIN_MATCH) & 255) ^ h->val;
121
+ h->hashval2[hpos] = h->val2;
122
+ if (h->head2[h->val2] != -1 && h->hashval2[h->head2[h->val2]] == h->val2) {
123
+ h->prev2[hpos] = h->head2[h->val2];
124
+ }
125
+ else h->prev2[hpos] = hpos;
126
+ h->head2[h->val2] = hpos;
127
+ #endif
128
+ }
129
+
130
+ void ZopfliWarmupHash(const unsigned char* array, size_t pos, size_t end,
131
+ ZopfliHash* h) {
132
+ (void)end;
133
+ UpdateHashValue(h, array[pos + 0]);
134
+ UpdateHashValue(h, array[pos + 1]);
135
+ }
@@ -0,0 +1,70 @@
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 hash for ZopfliFindLongestMatch of lz77.c.
22
+ */
23
+
24
+ #ifndef ZOPFLI_HASH_H_
25
+ #define ZOPFLI_HASH_H_
26
+
27
+ #include "util.h"
28
+
29
+ typedef struct ZopfliHash {
30
+ int* head; /* Hash value to index of its most recent occurance. */
31
+ unsigned short* prev; /* Index to index of prev. occurance of same hash. */
32
+ int* hashval; /* Index to hash value at this index. */
33
+ int val; /* Current hash value. */
34
+
35
+ #ifdef ZOPFLI_HASH_SAME_HASH
36
+ /* Fields with similar purpose as the above hash, but for the second hash with
37
+ a value that is calculated differently. */
38
+ int* head2; /* Hash value to index of its most recent occurance. */
39
+ unsigned short* prev2; /* Index to index of prev. occurance of same hash. */
40
+ int* hashval2; /* Index to hash value at this index. */
41
+ int val2; /* Current hash value. */
42
+ #endif
43
+
44
+ #ifdef ZOPFLI_HASH_SAME
45
+ unsigned short* same; /* Amount of repetitions of same byte after this .*/
46
+ #endif
47
+ } ZopfliHash;
48
+
49
+ /* Allocates and initializes all fields of ZopfliHash. */
50
+ void ZopfliInitHash(size_t window_size, ZopfliHash* h);
51
+
52
+ /* Frees all fields of ZopfliHash. */
53
+ void ZopfliCleanHash(ZopfliHash* h);
54
+
55
+ /*
56
+ Updates the hash values based on the current position in the array. All calls
57
+ to this must be made for consecutive bytes.
58
+ */
59
+ void ZopfliUpdateHash(const unsigned char* array, size_t pos, size_t end,
60
+ ZopfliHash* h);
61
+
62
+ /*
63
+ Prepopulates hash:
64
+ Fills in the initial values in the hash, before ZopfliUpdateHash can be used
65
+ correctly.
66
+ */
67
+ void ZopfliWarmupHash(const unsigned char* array, size_t pos, size_t end,
68
+ ZopfliHash* h);
69
+
70
+ #endif /* ZOPFLI_HASH_H_ */