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 /* Number of distinct literal/length and distance symbols in DEFLATE */
36 #define ZOPFLI_NUM_LL 288
37 #define ZOPFLI_NUM_D 32
38 
39 /*
40 The window size for deflate. Must be a power of two. This should be 32768, the
41 maximum possible by the deflate spec. Anything less hurts compression more than
42 speed.
43 */
44 #define ZOPFLI_WINDOW_SIZE 32768
45 
46 /*
47 The window mask used to wrap indices into the window. This is why the
48 window size must be a power of two.
49 */
50 #define ZOPFLI_WINDOW_MASK (ZOPFLI_WINDOW_SIZE - 1)
51 
52 /*
53 A block structure of huge, non-smart, blocks to divide the input into, to allow
54 operating on huge files without exceeding memory, such as the 1GB wiki9 corpus.
55 The whole compression algorithm, including the smarter block splitting, will
56 be executed independently on each huge block.
57 Dividing into huge blocks hurts compression, but not much relative to the size.
58 Set it to 0 to disable master blocks.
59 */
60 #define ZOPFLI_MASTER_BLOCK_SIZE 1000000
61 
62 /*
63 Used to initialize costs for example
64 */
65 #define ZOPFLI_LARGE_FLOAT 1e30
66 
67 /*
68 For longest match cache. max 256. Uses huge amounts of memory but makes it
69 faster. Uses this many times three bytes per single byte of the input data.
70 This is so because longest match finding has to find the exact distance
71 that belongs to each length for the best lz77 strategy.
72 Good values: e.g. 5, 8.
73 */
74 #define ZOPFLI_CACHE_LENGTH 8
75 
76 /*
77 limit the max hash chain hits for this hash value. This has an effect only
78 on files where the hash value is the same very often. On these files, this
79 gives worse compression (the value should ideally be 32768, which is the
80 ZOPFLI_WINDOW_SIZE, while zlib uses 4096 even for best level), but makes it
81 faster on some specific files.
82 Good value: e.g. 8192.
83 */
84 #define ZOPFLI_MAX_CHAIN_HITS 8192
85 
86 /*
87 Whether to use the longest match cache for ZopfliFindLongestMatch. This cache
88 consumes a lot of memory but speeds it up. No effect on compression size.
89 */
90 #define ZOPFLI_LONGEST_MATCH_CACHE
91 
92 /*
93 Enable to remember amount of successive identical bytes in the hash chain for
94 finding longest match
95 required for ZOPFLI_HASH_SAME_HASH and ZOPFLI_SHORTCUT_LONG_REPETITIONS
96 This has no effect on the compression result, and enabling it increases speed.
97 */
98 #define ZOPFLI_HASH_SAME
99 
100 /*
101 Switch to a faster hash based on the info from ZOPFLI_HASH_SAME once the
102 best length so far is long enough. This is way faster for files with lots of
103 identical bytes, on which the compressor is otherwise too slow. Regular files
104 are unaffected or maybe a tiny bit slower.
105 This has no effect on the compression result, only on speed.
106 */
107 #define ZOPFLI_HASH_SAME_HASH
108 
109 /*
110 Enable this, to avoid slowness for files which are a repetition of the same
111 character more than a multiple of ZOPFLI_MAX_MATCH times. This should not affect
112 the compression result.
113 */
114 #define ZOPFLI_SHORTCUT_LONG_REPETITIONS
115 
116 /*
117 Whether to use lazy matching in the greedy LZ77 implementation. This gives a
118 better result of ZopfliLZ77Greedy, but the effect this has on the optimal LZ77
119 varies from file to file.
120 */
121 #define ZOPFLI_LAZY_MATCHING
122 
123 /*
124 Appends value to dynamically allocated memory, doubling its allocation size
125 whenever needed.
126 
127 value: the value to append, type T
128 data: pointer to the dynamic array to append to, type T**
129 size: pointer to the size of the array to append to, type size_t*. This is the
130 size that you consider the array to be, not the internal allocation size.
131 Precondition: allocated size of data is at least a power of two greater than or
132 equal than *size.
133 */
134 #ifdef __cplusplus /* C++ cannot assign void* from malloc to *data */
135 #define ZOPFLI_APPEND_DATA(/* T */ value, /* T** */ data, /* size_t* */ size) {\
136   if (!((*size) & ((*size) - 1))) {\
137     /*double alloc size if it's a power of two*/\
138     void** data_void = reinterpret_cast<void**>(data);\
139     *data_void = (*size) == 0 ? malloc(sizeof(**data))\
140                               : realloc((*data), (*size) * 2 * sizeof(**data));\
141   }\
142   (*data)[(*size)] = (value);\
143   (*size)++;\
144 }
145 #else /* C gives problems with strict-aliasing rules for (void**) cast */
146 #define ZOPFLI_APPEND_DATA(/* T */ value, /* T** */ data, /* size_t* */ size) {\
147   if (!((*size) & ((*size) - 1))) {\
148     /*double alloc size if it's a power of two*/\
149     (*data) = (*size) == 0 ? malloc(sizeof(**data))\
150                            : realloc((*data), (*size) * 2 * sizeof(**data));\
151   }\
152   (*data)[(*size)] = (value);\
153   (*size)++;\
154 }
155 #endif
156 
157 
158 #endif  /* ZOPFLI_UTIL_H_ */
159