Debian Codegraph @

compression/wrap.c

raw symbols ◀ ▶ SCIP
3 Debian patches touch this file
1 /* wrap.c */
2
3 /*
4 This Software is distributed with the following X11 License,
5 sometimes also known as MIT license.
6
7 Copyright (c) 2010 Miguel A. Ballicora
8
9 Permission is hereby granted, free of charge, to any person
10 obtaining a copy of this software and associated documentation
11 files (the "Software"), to deal in the Software without
12 restriction, including without limitation the rights to use,
13 copy, modify, merge, publish, distribute, sublicense, and/or sell
14 copies of the Software, and to permit persons to whom the
15 Software is furnished to do so, subject to the following
16 conditions:
17
18 The above copyright notice and this permission notice shall be
19 included in all copies or substantial portions of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 OTHER DEALINGS IN THE SOFTWARE.
29 */
30
31 #include "wrap.h"
32
33 #define LZMA86
34 #define ZLIB
35 #define HUFFMAN
36 #define LIBLZF
37 /*#define LIBBZIP2*/
38
39 #if defined(LZMA86)
40 #include "Lzma86Enc.h"
41 #include "Lzma86Dec.h"
42 #endif
43
44 #if defined(ZLIB)
•45 #include "zlib.h"
46 #endif
47
48 #if defined(HUFFMAN)
49 #include "hzip.h"
50 #endif
51
52 #if defined(LIBLZF)
53 #include "lzf.h"
54 #endif
55
56 #if defined(LIBBZIP2)
57 #include "bzlib.h"
58 #endif
59
60 #if !defined(NDEBUG)
61 #define NDEBUG
62 #endif
63 #ifdef DEBUG
64 #undef NDEBUG
65 #endif
66 #include "assert.h"
67
68 /* external, so the compiler can be silenced */
69 size_t TB_DUMMY_unused;
70
71 /***********************************************************************************************************/
72
73 extern int
74 zlib_encode
75 (const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max)
76 {
77 enum COMPRESSION_LEVELS {ZLIB_MAXIMUM_COMPRESSION = 9};
78 int outcome;
79 unsigned long zz = (unsigned long)out_max;
•80 outcome = compress2 (out_start, &zz, in_start, (uLong)in_len, ZLIB_MAXIMUM_COMPRESSION);
81 *pout_len = (size_t) zz;
82 return outcome == Z_OK;
83 }
84
85 extern int
86 zlib_decode
87 (const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max)
88 {
89 int outcome;
90 unsigned long nn = (unsigned long) out_max /* *pout_len */;
91 outcome = uncompress (out_start, &nn, in_start, (unsigned long)in_len);
92 *pout_len = (size_t)nn;
93 return outcome == Z_OK;
94 }
95
96 /***********************************************************************************************************/
97
98 extern int
99 lzf_encode
100 (const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max)
101 {
102 size_t x = lzf_compress (in_start, (unsigned)in_len, out_start, (unsigned)(in_len-1) /* ensures best compression */);
103 TB_DUMMY_unused = out_max;
104 if (x != 0)
105 *pout_len = (size_t) x;
106 return x != 0;
107 }
108
109 extern int
110 lzf_decode
111 (const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max)
112 {
113 *pout_len = (size_t)lzf_decompress (in_start, (unsigned)in_len, out_start, (unsigned)out_max);
114 return *pout_len != 0;
115 }
116
117 /***********************************************************************************************************/
118
119 extern int
120 lzma_encode
121 (const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max)
122 {
123 int level = 5; /* 5 => default compression level */
124 unsigned int memory = 4096; /* dictionary size */
125 int filter = SZ_FILTER_NO; /* => 0, use LZMA, do not try to optimize with x86 filter */
126 size_t zz = out_max; /* maximum memory allowed, receives back the actual size */
127 int x = Lzma86_Encode(out_start, &zz, in_start, in_len, level, memory, filter);
128 *pout_len = zz;
129 return x == 0;
130 }
131
132 extern int
133 lzma_decode
134 (const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max)
135 {
136 size_t nn = out_max;
137 int x = Lzma86_Decode(out_start, &nn, in_start, &in_len);
138 *pout_len = nn;
139 return x == SZ_OK;
140 }
141
142 /***********************************************************************************************************/
143
144 #if defined (LIBBZIP2)
145
146 extern int
147 bzip2_encode
148 (const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max)
149 {
150 int blockSize100k = 9;
151 int verbosity = 0;
152 int workFactor = 30;
153 size_t destlen = out_max;
154
155 int x = BZ2_bzBuffToBuffCompress( (char*)out_start, &destlen, (char*)in_start, in_len,
156 blockSize100k, verbosity, workFactor);
157 *pout_len = destlen;
158 return x == BZ_OK;
159 }
160
161 extern int
162 bzip2_decode
163 (const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max)
164 {
165 int small = 1;
166 int verbosity = 0;
167 size_t destlen = n;
168
169 int x = BZ2_bzBuffToBuffDecompress( (char*)out_start, &destlen, (char*)in_start, in_len,
170 small, verbosity);
171 *pout_len = destlen;
172 return x == BZ_OK;
173 }
174
175 #endif
176
177 /***********************************************************************************************************/
178
179 extern int
180 justcopy_encode
181 (const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max)
182 {
183 size_t i;
184 const unsigned char *in = in_start;
185 unsigned char *out = out_start;
186
187 if (in_len > out_max)
188 return 0;
189
190 for (i = 0; i < in_len; i++) {
191 *out++ = *in++;
192 }
193 *pout_len = in_len;
194 return 1;
195 }
196
197 extern int
198 justcopy_decode
199 (const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max)
200 {
201 size_t i;
202 const unsigned char *in = in_start;
203 unsigned char *out = out_start;
204
205 if (in_len > out_max)
206 return 0;
207
208 for (i = 0; i < in_len; i++) {
209 *out++ = *in++;
210 }
211 *pout_len = in_len;
212 return 1;
213 }
214
215 /***********************************************************************************************************/
216
217 #define RLE_ESC 253
218 #define RLE_TER 254
219 #define RLE_MAX 252
220 #define TRUE 1
221 #define FALSE 0
222
223 extern int
224 rle_encode
225 (const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max)
226 {
227 const unsigned char *p;
228 const unsigned char *in = in_start;
229 const unsigned char *in_end = in + in_len;
230 unsigned char *out = out_start;
231 int ok = TRUE;
232 int ch;
233 ptrdiff_t out_len;
234
235 while (in < in_end)
236 {
237 if (*in == RLE_ESC) {
238
239 *out++ = RLE_ESC;
240 *out++ = RLE_ESC;
241 in++;
242
243 } else {
244
245 ch = *in;
246
247 if ( (in_end-in) >= 3 /* enough space for a run */
248 && ch == in[1] && ch == in[2] && ch == in[3] /* enough length */) {
249
250 p = in;
251 while (p < in_end && *p == ch && (p-in) < RLE_MAX) {
252 p++;
253 }
254
255 *out++ = RLE_ESC;
256 assert (RLE_MAX < 256);
257 *out++ = (unsigned char)(p - in);
258 *out++ = (unsigned char)ch;
259 in = p;
260
261 } else {
262 *out++ = *in++;
263 }
264 }
265 }
266
267 if (ok) {
268 /* *out++ = RLE_ESC; *out++ = RLE_TER; */
269 out_len = out - out_start;
270 *pout_len = (size_t)out_len;
271 ok = (size_t)out_len <= out_max;
272 }
273
274 return ok;
275 }
276
277 extern int
278 rle_decode
279 (const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max)
280 {
281 const unsigned char *in = in_start;
282 const unsigned char *in_end = in + in_len;
283 unsigned char *out = out_start;
284 unsigned char *out_end = out + *pout_len;
285 int ok = TRUE;
286 int ch;
287 int n;
288 ptrdiff_t out_len;
289
290 while (in < in_end)
291 {
292 if (in >= in_end) { ok = FALSE; break;}
293 if (out >= out_end) { ok = FALSE; break;}
294
295 if (*in == RLE_ESC) {
296 ++in; if (in >= in_end) { ok = FALSE; break;}
297
298 if (*in == RLE_ESC) {
299 *out++ = *in++;
300 } /*else if (*in == RLE_TER) {ok = TRUE;break;} */ else {
301
302 /* rle */
303 n = *in++; if (in >= in_end) { ok = FALSE; break;}
304 ch = *in++;
305 while (n-->0) { if (out >= out_end) { ok = FALSE; break;}
306 *out++ = (unsigned char)ch;
307 }
308 }
309 } else {
310 *out++ = *in++;
311 }
312 }
313
314 out_len = out - out_start;
315
316 if (ok)
317 *pout_len = (size_t)out_len;
318
319 ok = ok && (out_max >= (size_t)out_len);
320
321 return ok;
322 }
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338