1 /*
2 Very dirty method to offer runtime selection of QuickLZ compression level
3 
4     for each compression level L in (1, 2, 3) do
5     {
6        redefine publics+privates of quicklz.c by appending compression level to their names
7        define QLZ_COMPRESSION_LEVEL to L
8        include quicklz.c
9        undefine publics+privates
10     }
11 */
12 
13 #include <string.h>
14 
15 #define QLZ_STREAMING_BUFFER 0
16 //#define QLZ_STREAMING_BUFFER 100000
17 //#define QLZ_STREAMING_BUFFER 1000000
18 
19 #define QLZ_MEMORY_SAFE
20 
21 // ---------------------------------------
22 #define fast_read FAST_READ_1
23 #define fast_write FAST_WRITE_1
24 #define hash_func HASH_FUNC_1
25 #define memcpy_up MEMCPY_UP_1
26 #define qlz_compress_core QLZ_COMPRESS_CORE_1
27 #define qlz_decompress_core QLZ_DECOMPRESS_CORE_1
28 #define qlz_decompress QLZ_DECOMPRESS_1
29 #define qlz_compress QLZ_COMPRESS_1
30 #define qlz_size_decompressed QLZ_SIZE_DECOMPRESSED_1
31 #define qlz_size_compressed QLZ_SIZE_COMPRESSED_1
32 #define qlz_get_setting QLZ_GET_SETTING_1
33 #define update_hash_upto UPDATE_HASH_UPTO_1
34 #define update_hash UPDATE_HASH_1
35 #define qlz_hash_entry QLZ_HASH_ENTRY_1
36 #define reset_state RESET_STATE_1
37 #define qlz_hash_compress QLZ_HASH_COMPRESS_1
38 #define qlz_hash_decompress QLZ_HASH_DECOMPRESS_1
39 
40 #define QLZ_COMPRESSION_LEVEL 1
41 
42 #include "quicklz.c"
43 
QLZ_SCRATCH_COMPRESS_1(void)44 size_t QLZ_SCRATCH_COMPRESS_1(void)
45 {
46     return QLZ_SCRATCH_COMPRESS;
47 }
QLZ_SCRATCH_DECOMPRESS_1(void)48 size_t QLZ_SCRATCH_DECOMPRESS_1(void)
49 {
50     return QLZ_SCRATCH_DECOMPRESS;
51 }
52 
53 #undef X86X64
54 #undef QLZ_HEADER
55 #undef QLZ_SCRATCH_COMPRESS
56 #undef QLZ_SCRATCH_DECOMPRESS
57 #undef fast_read
58 #undef fast_write
59 #undef hash_func
60 #undef memcpy_up
61 #undef qlz_compress_core
62 #undef qlz_decompress_core
63 #undef qlz_decompress
64 #undef qlz_compress
65 #undef qlz_size_decompressed
66 #undef qlz_size_compressed
67 #undef qlz_get_setting
68 #undef QLZ_COMPRESSION_LEVEL
69 #undef QLZ_POINTERS
70 #undef QLZ_HASH_VALUES
71 #undef update_hash_upto
72 #undef update_hash
73 #undef qlz_hash_entry
74 #undef reset_state
75 #undef qlz_hash_compress
76 #undef qlz_hash_decompress
77 
78 // ---------------------------------------
79 #define fast_read FAST_READ_2
80 #define fast_write FAST_WRITE_2
81 #define hash_func HASH_FUNC_2
82 #define memcpy_up MEMCPY_UP_2
83 #define qlz_compress_core QLZ_COMPRESS_CORE_2
84 #define qlz_decompress_core QLZ_DECOMPRESS_CORE_2
85 #define qlz_decompress QLZ_DECOMPRESS_2
86 #define qlz_compress QLZ_COMPRESS_2
87 #define qlz_size_decompressed QLZ_SIZE_DECOMPRESSED_2
88 #define qlz_size_compressed QLZ_SIZE_COMPRESSED_2
89 #define qlz_get_setting QLZ_GET_SETTING_2
90 #define update_hash_upto UPDATE_HASH_UPTO_2
91 #define update_hash UPDATE_HASH_2
92 #define qlz_hash_entry QLZ_HASH_ENTRY_2
93 #define reset_state RESET_STATE_2
94 #define qlz_hash_compress QLZ_HASH_COMPRESS_2
95 #define qlz_hash_decompress QLZ_HASH_DECOMPRESS_2
96 
97 #define QLZ_COMPRESSION_LEVEL 2
98 
99 #include "quicklz.c"
100 
QLZ_SCRATCH_COMPRESS_2(void)101 size_t QLZ_SCRATCH_COMPRESS_2(void)
102 {
103     return QLZ_SCRATCH_COMPRESS;
104 }
QLZ_SCRATCH_DECOMPRESS_2(void)105 size_t QLZ_SCRATCH_DECOMPRESS_2(void)
106 {
107     return QLZ_SCRATCH_DECOMPRESS;
108 }
109 
110 #undef X86X64
111 #undef QLZ_HEADER
112 #undef QLZ_SCRATCH_COMPRESS
113 #undef QLZ_SCRATCH_DECOMPRESS
114 #undef fast_read
115 #undef fast_write
116 #undef hash_func
117 #undef memcpy_up
118 #undef qlz_compress_core
119 #undef qlz_decompress_core
120 #undef qlz_decompress
121 #undef qlz_compress
122 #undef qlz_size_decompressed
123 #undef qlz_size_compressed
124 #undef qlz_get_setting
125 #undef QLZ_COMPRESSION_LEVEL
126 #undef QLZ_POINTERS
127 #undef QLZ_HASH_VALUES
128 #undef update_hash_upto
129 #undef update_hash
130 #undef qlz_hash_entry
131 #undef reset_state
132 #undef qlz_hash_compress
133 #undef qlz_hash_decompress
134 
135 // ---------------------------------------
136 #define fast_read FAST_READ_3
137 #define fast_write FAST_WRITE_3
138 #define hash_func HASH_FUNC_3
139 #define memcpy_up MEMCPY_UP_3
140 #define qlz_compress_core QLZ_COMPRESS_CORE_3
141 #define qlz_decompress_core QLZ_DECOMPRESS_CORE_3
142 #define qlz_decompress QLZ_DECOMPRESS_3
143 #define qlz_compress QLZ_COMPRESS_3
144 #define qlz_size_decompressed QLZ_SIZE_DECOMPRESSED_3
145 #define qlz_size_compressed QLZ_SIZE_COMPRESSED_3
146 #define qlz_get_setting QLZ_GET_SETTING_3
147 #define update_hash_upto UPDATE_HASH_UPTO_3
148 #define update_hash UPDATE_HASH_3
149 #define qlz_hash_entry QLZ_HASH_ENTRY_3
150 #define reset_state RESET_STATE_3
151 #define qlz_hash_compress QLZ_HASH_COMPRESS_3
152 #define qlz_hash_decompress QLZ_HASH_DECOMPRESS_3
153 
154 #define QLZ_COMPRESSION_LEVEL 3
155 
156 #include "quicklz.c"
157 
QLZ_SCRATCH_COMPRESS_3(void)158 size_t QLZ_SCRATCH_COMPRESS_3(void)
159 {
160     return QLZ_SCRATCH_COMPRESS;
161 }
QLZ_SCRATCH_DECOMPRESS_3(void)162 size_t QLZ_SCRATCH_DECOMPRESS_3(void)
163 {
164     return QLZ_SCRATCH_DECOMPRESS;
165 }
166 
QLZ_SIZE_COMPRESSED(char * source)167 size_t QLZ_SIZE_COMPRESSED(char *source)
168 {
169     return QLZ_SIZE_COMPRESSED_3(source);
170 }
171 
QLZ_SIZE_DECOMPRESSED(char * source)172 size_t QLZ_SIZE_DECOMPRESSED(char *source)
173 {
174     return QLZ_SIZE_DECOMPRESSED_3(source);
175 }
176 
177 #undef QLZ_HEADER
178 #undef QLZ_SCRATCH_COMPRESS
179 #undef QLZ_SCRATCH_DECOMPRESS
180 #undef fast_read
181 #undef fast_write
182 #undef hash_func
183 #undef memcpy_up
184 #undef qlz_compress_core
185 #undef qlz_decompress_core
186 #undef qlz_decompress
187 #undef qlz_compress
188 #undef qlz_size_decompressed
189 #undef qlz_size_compressed
190 #undef qlz_get_setting
191 #undef QLZ_COMPRESSION_LEVEL
192 #undef QLZ_POINTERS
193 #undef QLZ_HASH_VALUES
194 #undef update_hash_upto
195 #undef update_hash
196 #undef qlz_hash_entry
197 #undef reset_state
198 #undef qlz_hash_compress
199 #undef qlz_hash_decompress
200 
max2(size_t a,size_t b)201 static size_t max2(size_t a, size_t b)
202 {
203     return a > b ? a : b;
204 }
205 
QLZ_SCRATCH_COMPRESS(void)206 size_t QLZ_SCRATCH_COMPRESS(void)
207 {
208     return max2(QLZ_SCRATCH_COMPRESS_1(), max2(QLZ_SCRATCH_COMPRESS_2(), QLZ_SCRATCH_COMPRESS_3()));
209 }
210 
QLZ_SCRATCH_DECOMPRESS(void)211 size_t QLZ_SCRATCH_DECOMPRESS(void)
212 {
213     return max2(QLZ_SCRATCH_DECOMPRESS_1(), max2(QLZ_SCRATCH_DECOMPRESS_2(), QLZ_SCRATCH_DECOMPRESS_3()));
214 }
215 
216 
QLZ_COMPRESS(const void * source,char * destination,size_t size,unsigned int level,char * scratch)217 size_t QLZ_COMPRESS(const void *source, char *destination, size_t size, unsigned int level, char *scratch)
218 {
219     if(level == 1)
220         return QLZ_COMPRESS_1(source, destination, size, scratch);
221     else if(level == 2)
222         return QLZ_COMPRESS_2(source, destination, size, scratch);
223     else if(level == 3)
224         return QLZ_COMPRESS_3(source, destination, size, scratch);
225     else
226         return 0;
227 }
QLZ_DECOMPRESS(const char * source,void * destination,char * scratch)228 size_t QLZ_DECOMPRESS(const char *source, void *destination, char *scratch)
229 {
230     int level = ((source[0] >> 2) & 3);
231 
232     if(level == 1)
233         return QLZ_DECOMPRESS_1(source, destination, scratch);
234     else if(level == 2)
235         return QLZ_DECOMPRESS_2(source, destination, scratch);
236     else if(level == 3)
237         return QLZ_DECOMPRESS_3(source, destination, scratch);
238     else
239         return 0;
240 }
241 
242