File: wrap.c

package info (click to toggle)
gaviotatb 0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 1,128 kB
  • ctags: 1,865
  • sloc: ansic: 12,303; ruby: 516; makefile: 93; sh: 8
file content (339 lines) | stat: -rw-r--r-- 8,210 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* wrap.c */

/*
This Software is distributed with the following X11 License,
sometimes also known as MIT license.
 
Copyright (c) 2010 Miguel A. Ballicora

 Permission is hereby granted, free of charge, to any person
 obtaining a copy of this software and associated documentation
 files (the "Software"), to deal in the Software without
 restriction, including without limitation the rights to use,
 copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following
 conditions:

 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
*/

#include "wrap.h"

#define LZMA86 
#define ZLIB
#define HUFFMAN
#define LIBLZF
/*#define LIBBZIP2*/

#if defined(LZMA86)
#include "Lzma86Enc.h"
#include "Lzma86Dec.h"
#endif

#if defined(ZLIB)
#define Z_SOLO
#include "zlib.h"
#endif

#if defined(HUFFMAN)
#include "hzip.h"
#endif

#if defined(LIBLZF)
#include "lzf.h"
#endif

#if defined(LIBBZIP2)
#include "bzlib.h"
#endif

#if !defined(NDEBUG)
#define NDEBUG
#endif
#ifdef DEBUG
#undef NDEBUG
#endif
#include "assert.h"

/* external, so the compiler can be silenced */
size_t TB_DUMMY_unused;

/***********************************************************************************************************/

extern int
zlib_encode
(const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max)
{
	enum COMPRESSION_LEVELS {ZLIB_MAXIMUM_COMPRESSION = 9};
	int outcome;
	unsigned long zz = (unsigned long)out_max;
	outcome = compress2 (out_start, &zz, in_start, in_len, ZLIB_MAXIMUM_COMPRESSION);
	*pout_len = (size_t) zz;
	return outcome == Z_OK;
}

extern int
zlib_decode
(const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max)
{
	int outcome;
	unsigned long nn = (unsigned long) out_max /* *pout_len */;
	outcome = uncompress (out_start, &nn, in_start, (unsigned long)in_len);
	*pout_len = (size_t)nn;
	return outcome == Z_OK;
}

/***********************************************************************************************************/

extern int
lzf_encode
(const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max)
{
	size_t x = lzf_compress (in_start, (unsigned)in_len, out_start, (unsigned)(in_len-1) /* ensures best compression */);
	TB_DUMMY_unused = out_max;
	if (x != 0)
		*pout_len = (size_t) x;
	return x != 0;
}

extern int
lzf_decode
(const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max)
{
	*pout_len = (size_t)lzf_decompress (in_start, (unsigned)in_len, out_start, (unsigned)out_max);
	return *pout_len != 0;
}

/***********************************************************************************************************/

extern int
lzma_encode
(const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max)
{
	int level  =    5; 			/* 5 => default compression level */
	unsigned int memory = 4096;	/* dictionary size */
	int filter = SZ_FILTER_NO; 	/* => 0, use LZMA, do not try to optimize with x86 filter */
	size_t zz  = out_max; 		/* maximum memory allowed, receives back the actual size */
	int x      = Lzma86_Encode(out_start, &zz, in_start, in_len, level, memory, filter);
	*pout_len  = zz;
	return x == 0;
}

extern int
lzma_decode
(const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max)
{
		size_t nn = out_max;
		int x = Lzma86_Decode(out_start, &nn, in_start, &in_len);
		*pout_len = nn;
		return x == SZ_OK;
}

/***********************************************************************************************************/

#if defined (LIBBZIP2)

extern int
bzip2_encode
(const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max)
{
	int	blockSize100k =  9;
	int verbosity     =  0;
	int workFactor    = 30;
	size_t destlen    = out_max;

	int x = BZ2_bzBuffToBuffCompress( (char*)out_start, &destlen, (char*)in_start, in_len, 
								blockSize100k, verbosity, workFactor);
	*pout_len = destlen;
	return x == BZ_OK;
}

extern int
bzip2_decode
(const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max)
{
	int	small      = 1;
	int verbosity  = 0;
	size_t destlen = n;

	int x = BZ2_bzBuffToBuffDecompress( (char*)out_start, &destlen, (char*)in_start, in_len,
                               small, verbosity);
	*pout_len = destlen;
	return x == BZ_OK;
}

#endif

/***********************************************************************************************************/

extern int
justcopy_encode
(const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max)
{
	size_t i;
	const unsigned char *in  = in_start;
		  unsigned char *out = out_start;

	if (in_len > out_max)
		return 0;

	for (i = 0; i < in_len; i++) {
		*out++ = *in++; 
	}
	*pout_len = in_len;
	return 1;
}

extern int
justcopy_decode
(const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max)
{
	size_t i;
	const unsigned char *in  = in_start;
		  unsigned char *out = out_start;

	if (in_len > out_max)
		return 0;

	for (i = 0; i < in_len; i++) {
		*out++ = *in++; 
	}
	*pout_len = in_len;
	return 1;
}

/***********************************************************************************************************/

#define RLE_ESC 253
#define RLE_TER 254
#define RLE_MAX 252
#define TRUE 1
#define FALSE 0

extern int
rle_encode
(const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max)
{
	const unsigned char *p;	
	const unsigned char *in      = in_start;
	const unsigned char *in_end  = in  + in_len;
	unsigned char       *out     = out_start;
	int ok = TRUE;
	int ch;
	ptrdiff_t out_len;

	while (in < in_end)
	{
		if (*in == RLE_ESC) {

			*out++ = RLE_ESC;
			*out++ = RLE_ESC;
			in++;	 

		} else {

			ch = *in;
	
			if ( (in_end-in) >= 3 /* enough space for a run */
				&& ch == in[1] && ch == in[2] && ch == in[3] /* enough length */) {

				p = in;
				while (p < in_end && *p == ch && (p-in) < RLE_MAX) {
					p++;
				}

				*out++ = RLE_ESC;
				assert (RLE_MAX < 256);
				*out++ = (unsigned char)(p - in);
				*out++ = (unsigned char)ch;
				in = p;

			} else {	
				*out++ = *in++;	
			}
		}
	}

	if (ok) {
		/*	*out++ = RLE_ESC; *out++ = RLE_TER; */
		out_len = out - out_start;
		*pout_len = (size_t)out_len;	
		ok = (size_t)out_len <= out_max;
	}

	return ok;
}

extern int
rle_decode
(const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max)
{
	const unsigned char *in  = in_start;
	const unsigned char *in_end  = in  + in_len;
		  unsigned char *out = out_start;
		  unsigned char *out_end = out + *pout_len;
	int ok = TRUE;
	int ch;
	int n;
	ptrdiff_t out_len;

	while (in < in_end)
	{
									if (in  >=  in_end) { ok = FALSE; break;}
									if (out >= out_end) { ok = FALSE; break;}

		if (*in == RLE_ESC) {
			++in;					if (in >= in_end) { ok = FALSE;	break;}

			if (*in == RLE_ESC) {
				*out++ = *in++;
			} /*else if (*in == RLE_TER) {ok = TRUE;break;} */ else {

				/* rle */
				n  = *in++; 		if (in >= in_end) { ok = FALSE;	break;}
				ch = *in++;			
				while (n-->0) {		if (out >= out_end) { ok = FALSE; break;}
					*out++ = (unsigned char)ch;	
				}
			}
		} else {
			*out++ = *in++;	
		}
	}

	out_len = out - out_start;

	if (ok)
		*pout_len = (size_t)out_len;

	ok = ok && (out_max >= (size_t)out_len);

	return ok;
}