Debian Codegraph @

compression/wrap.c

raw symbols ◀ ▶
Source reconstructed at patch state 1 of 3 — not the installed (fully patched) source.
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 #define Z_SOLO
46 #include "zlib.h"
47 #endif
48
49 #if defined(HUFFMAN)
50 #include "hzip.h"
51 #endif
52
53 #if defined(LIBLZF)
54 #include "lzf.h"
55 #endif
56
57 #if defined(LIBBZIP2)
58 #include "bzlib.h"
59 #endif
60
61 #if !defined(NDEBUG)
62 #define NDEBUG
63 #endif
64 #ifdef DEBUG
65 #undef NDEBUG
66 #endif
67 #include "assert.h"
68
69 /* external, so the compiler can be silenced */
70 size_t TB_DUMMY_unused;
71
72 /***********************************************************************************************************/
73
74 extern int
75 zlib_encode
76 (const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max)
77 {
78 enum COMPRESSION_LEVELS {ZLIB_MAXIMUM_COMPRESSION = 9};
79 int outcome;
80 unsigned long zz = (unsigned long)out_max;
81 outcome = compress2 (out_start, &zz, in_start, (z_uLong)in_len, ZLIB_MAXIMUM_COMPRESSION);
82 *pout_len = (size_t) zz;
83 return outcome == Z_OK;
84 }
85
86 extern int
87 zlib_decode
88 (const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max)
89 {
90 int outcome;
91 unsigned long nn = (unsigned long) out_max /* *pout_len */;
92 outcome = uncompress (out_start, &nn, in_start, (unsigned long)in_len);
93 *pout_len = (size_t)nn;
94 return outcome == Z_OK;
95 }
96
97 /***********************************************************************************************************/
98
99 extern int
100 lzf_encode
101 (const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max)
102 {
103 size_t x = lzf_compress (in_start, (unsigned)in_len, out_start, (unsigned)(in_len-1) /* ensures best compression */);
104 TB_DUMMY_unused = out_max;
105 if (x != 0)
106 *pout_len = (size_t) x;
107 return x != 0;
108 }
109
110 extern int
111 lzf_decode
112 (const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max)
113 {
114 *pout_len = (size_t)lzf_decompress (in_start, (unsigned)in_len, out_start, (unsigned)out_max);
115 return *pout_len != 0;
116 }
117
118 /***********************************************************************************************************/
119
120 extern int
121 lzma_encode
122 (const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max)
123 {
124 int level = 5; /* 5 => default compression level */
125 unsigned int memory = 4096; /* dictionary size */
126 int filter = SZ_FILTER_NO; /* => 0, use LZMA, do not try to optimize with x86 filter */
127 size_t zz = out_max; /* maximum memory allowed, receives back the actual size */
128 int x = Lzma86_Encode(out_start, &zz, in_start, in_len, level, memory, filter);
129 *pout_len = zz;
130 return x == 0;
131 }
132
133 extern int
134 lzma_decode
135 (const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max)
136 {
137 size_t nn = out_max;
138 int x = Lzma86_Decode(out_start, &nn, in_start, &in_len);
139 *pout_len = nn;
140 return x == SZ_OK;
141 }
142
143 /***********************************************************************************************************/
144
145 #if defined (LIBBZIP2)
146
147 extern int
148 bzip2_encode
149 (const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max)
150 {
151 int blockSize100k = 9;
152 int verbosity = 0;
153 int workFactor = 30;
154 size_t destlen = out_max;
155
156 int x = BZ2_bzBuffToBuffCompress( (char*)out_start, &destlen, (char*)in_start, in_len,
157 blockSize100k, verbosity, workFactor);
158 *pout_len = destlen;
159 return x == BZ_OK;
160 }
161
162 extern int
163 bzip2_decode
164 (const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max)
165 {
166 int small = 1;
167 int verbosity = 0;
168 size_t destlen = n;
169
170 int x = BZ2_bzBuffToBuffDecompress( (char*)out_start, &destlen, (char*)in_start, in_len,
171 small, verbosity);
172 *pout_len = destlen;
173 return x == BZ_OK;
174 }
175
176 #endif
177
178 /***********************************************************************************************************/
179
180 extern int
181 justcopy_encode
182 (const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max)
183 {
184 size_t i;
185 const unsigned char *in = in_start;
186 unsigned char *out = out_start;
187
188 if (in_len > out_max)
189 return 0;
190
191 for (i = 0; i < in_len; i++) {
192 *out++ = *in++;
193 }
194 *pout_len = in_len;
195 return 1;
196 }
197
198 extern int
199 justcopy_decode
200 (const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max)
201 {
202 size_t i;
203 const unsigned char *in = in_start;
204 unsigned char *out = out_start;
205
206 if (in_len > out_max)
207 return 0;
208
209 for (i = 0; i < in_len; i++) {
210 *out++ = *in++;
211 }
212 *pout_len = in_len;
213 return 1;
214 }
215
216 /***********************************************************************************************************/
217
218 #define RLE_ESC 253
219 #define RLE_TER 254
220 #define RLE_MAX 252
221 #define TRUE 1
222 #define FALSE 0
223
224 extern int
225 rle_encode
226 (const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max)
227 {
228 const unsigned char *p;
229 const unsigned char *in = in_start;
230 const unsigned char *in_end = in + in_len;
231 unsigned char *out = out_start;
232 int ok = TRUE;
233 int ch;
234 ptrdiff_t out_len;
235
236 while (in < in_end)
237 {
238 if (*in == RLE_ESC) {
239
240 *out++ = RLE_ESC;
241 *out++ = RLE_ESC;
242 in++;
243
244 } else {
245
246 ch = *in;
247
248 if ( (in_end-in) >= 3 /* enough space for a run */
249 && ch == in[1] && ch == in[2] && ch == in[3] /* enough length */) {
250
251 p = in;
252 while (p < in_end && *p == ch && (p-in) < RLE_MAX) {
253 p++;
254 }
255
256 *out++ = RLE_ESC;
257 assert (RLE_MAX < 256);
258 *out++ = (unsigned char)(p - in);
259 *out++ = (unsigned char)ch;
260 in = p;
261
262 } else {
263 *out++ = *in++;
264 }
265 }
266 }
267
268 if (ok) {
269 /* *out++ = RLE_ESC; *out++ = RLE_TER; */
270 out_len = out - out_start;
271 *pout_len = (size_t)out_len;
272 ok = (size_t)out_len <= out_max;
273 }
274
275 return ok;
276 }
277
278 extern int
279 rle_decode
280 (const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max)
281 {
282 const unsigned char *in = in_start;
283 const unsigned char *in_end = in + in_len;
284 unsigned char *out = out_start;
285 unsigned char *out_end = out + *pout_len;
286 int ok = TRUE;
287 int ch;
288 int n;
289 ptrdiff_t out_len;
290
291 while (in < in_end)
292 {
293 if (in >= in_end) { ok = FALSE; break;}
294 if (out >= out_end) { ok = FALSE; break;}
295
296 if (*in == RLE_ESC) {
297 ++in; if (in >= in_end) { ok = FALSE; break;}
298
299 if (*in == RLE_ESC) {
300 *out++ = *in++;
301 } /*else if (*in == RLE_TER) {ok = TRUE;break;} */ else {
302
303 /* rle */
304 n = *in++; if (in >= in_end) { ok = FALSE; break;}
305 ch = *in++;
306 while (n-->0) { if (out >= out_end) { ok = FALSE; break;}
307 *out++ = (unsigned char)ch;
308 }
309 }
310 } else {
311 *out++ = *in++;
312 }
313 }
314
315 out_len = out - out_start;
316
317 if (ok)
318 *pout_len = (size_t)out_len;
319
320 ok = ok && (out_max >= (size_t)out_len);
321
322 return ok;
323 }
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339