File: encode.c

package info (click to toggle)
c-icap 1:0.5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,600 kB
  • sloc: ansic: 27,220; sh: 4,415; makefile: 242; perl: 95; awk: 10
file content (508 lines) | stat: -rw-r--r-- 13,392 bytes parent folder | download | duplicates (2)
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/*
 *  Copyright (C) 2017 Jeffrey Merkey
 *  Copyright (C) 2017 Trever L. Adams
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *  MA  02110-1301  USA.
 */


#include "common.h"
#include "c-icap.h"
#include "body.h"
#include "simple_api.h"
#include "debug.h"

#include <assert.h>
#ifdef HAVE_ZLIB
#include <zlib.h>
#endif
#ifdef HAVE_BZLIB
#include <bzlib.h>
#endif
#ifdef HAVE_BROTLI
#include "brotli/decode.h"
#include "brotli/encode.h"
#include "brotli/types.h"
#include "brotli/port.h"
#endif

/*return CI_DEFLATE_ERRORS
*/
int ci_compress_to_membuf(int encoding_format, const char *inbuf,
				  size_t inlen, ci_membuf_t *outbuf,
				  ci_off_t max_size)
{
	switch (encoding_format) {
	case CI_ENCODE_NONE:
		return CI_COMP_OK;
		break;
#ifdef HAVE_ZLIB
	case CI_ENCODE_GZIP:
		return ci_gzip_to_membuf(inbuf, inlen, outbuf, max_size);
		break;
	case CI_ENCODE_DEFLATE:
		return ci_deflate_to_membuf(inbuf, inlen, outbuf, max_size);
		break;
#endif
#ifdef HAVE_BZLIB
	case CI_ENCODE_BZIP2:
		return ci_bzzip_to_membuf(inbuf, inlen, outbuf, max_size);
		break;
#endif
#ifdef HAVE_BROTLI
	case CI_ENCODE_BROTLI:
		return ci_brdeflate_to_membuf(inbuf, inlen, outbuf, max_size);
		break;
#endif
	case CI_ENCODE_UNKNOWN:
	default:
		return CI_COMP_ERR_ERROR;
		break;
	}
}

/*return CI_DEFLATE_ERRORS
*/
int ci_compress_to_simple_file(int encoding_format, const char *inbuf,
				       size_t inlen,
				       struct ci_simple_file *outbuf,
				       ci_off_t max_size)
{
	switch (encoding_format) {
	case CI_ENCODE_NONE:
		return CI_COMP_OK;
		break;
#ifdef HAVE_ZLIB
	case CI_ENCODE_GZIP:
		return ci_gzip_to_simple_file(inbuf, inlen, outbuf,
						 max_size);
		break;
	case CI_ENCODE_DEFLATE:
		return ci_deflate_to_simple_file(inbuf, inlen, outbuf,
						 max_size);
		break;
#endif
#ifdef HAVE_BZLIB
	case CI_ENCODE_BZIP2:
		return ci_bzzip_to_simple_file(inbuf, inlen, outbuf,
					       max_size);
		break;
#endif
#ifdef HAVE_BROTLI
	case CI_ENCODE_BROTLI:
		return ci_brdeflate_to_simple_file(inbuf, inlen, outbuf,
						   max_size);
		break;
#endif
	case CI_ENCODE_UNKNOWN:
	default:
		return CI_COMP_ERR_ERROR;
		break;
	}
}

#define CHUNK 8192

static int write_membuf_func(void *obj, const char *buf, size_t len)
{
	return ci_membuf_write((ci_membuf_t *)obj, buf, len, 0);
}

static int write_simple_file_func(void *obj, const char *buf, size_t len)
{
	return ci_simple_file_write((ci_simple_file_t *)obj, buf, len, 0);
}

#ifdef HAVE_BROTLI
#define DEFAULT_LGWIN	 22
#define DEFAULT_QUALITY	 11
#define kFileBufferSize 16384

BROTLI_BOOL brotli_compress(BrotliEncoderState* s, const char *buf, int inlen,
			    void *outbuf,
			    char *(*get_outbuf)(void *obj, unsigned int *len),
			    int (*writefunc)(void *obj,
					     const char *buf, size_t len),
			    ci_off_t max_size)
{
	size_t available_in;
	const uint8_t* next_in;
	size_t available_out;
	uint8_t* next_out;
	unsigned have, written;
	long long outsize;
	int result;
	size_t total_out = 0;
	uint8_t out[kFileBufferSize];

	ci_debug_printf(4, "data-compression:  brotli compress called size: %d\n",
			inlen);

	next_in = (uint8_t *)buf;
	available_in = inlen;
	outsize = 0;
	for (;;) {
		available_out = kFileBufferSize;
		next_out = out;
		result = BrotliEncoderCompressStream(s,
						     available_in
						     ? BROTLI_OPERATION_PROCESS
						     : BROTLI_OPERATION_FINISH,
						     &available_in, &next_in,
						     &available_out, &next_out,
						     &total_out);
		if (!result) {
			/* Should detect OOM? */
			ci_debug_printf(4, "data-compression:  brotli failed to compress data\n");
			return BROTLI_FALSE;
		}
		if (available_out != kFileBufferSize) {
			have = kFileBufferSize - available_out;
			if (!have || (written =
				      writefunc(outbuf, (char *)out, have)) !=
			    have) {
				ci_debug_printf(4, "data-compression:  brotli data corrupt\n");
				return BROTLI_FALSE;
			}
			outsize += written;
		}
		if (BrotliEncoderIsFinished(s)) {
			ci_debug_printf(4, "data-compression:  brotli total compressed size %lld (%lld) ...\n",
					outsize, (long long) total_out);
			return BROTLI_TRUE;
		}
	}
}

int ci_mem_brdeflate(const char *inbuf, int inlen, void *outbuf,
		     char *(*get_outbuf)(void *obj, unsigned int *len),
		     int (*writefunc)(void *obj, const char *buf, size_t len),
		     ci_off_t max_size)
{
	BROTLI_BOOL ccode = BROTLI_TRUE;
	BrotliEncoderState *s;

	s = BrotliEncoderCreateInstance(NULL, NULL, NULL);
	if (!s) {
		ci_debug_printf(4, "data-compression:  brotli out of memory\n");
		return -1;
	}
	BrotliEncoderSetParameter(s, BROTLI_PARAM_MODE, BROTLI_MODE_TEXT);
	BrotliEncoderSetParameter(s, BROTLI_PARAM_QUALITY, DEFAULT_QUALITY);
	BrotliEncoderSetParameter(s, BROTLI_PARAM_LGWIN, DEFAULT_LGWIN);
	ccode = brotli_compress(s, inbuf, inlen, outbuf, get_outbuf, writefunc,
				max_size);
	BrotliEncoderDestroyInstance(s);

	if (!ccode)
		return -1;
	return 1;
}

int ci_brdeflate_to_membuf(const char *inbuf, size_t inlen, ci_membuf_t *outbuf,
			   ci_off_t max_size)
{
	int ret = ci_mem_brdeflate(inbuf, inlen, outbuf, NULL,
				   write_membuf_func, max_size);
	ci_membuf_write(outbuf, "", 0, 1);
	return ret;
}

int ci_brdeflate_to_simple_file(const char *inbuf, size_t inlen,
				struct ci_simple_file *outbuf,
				ci_off_t max_size)
{
	int ret = ci_mem_brdeflate(inbuf, inlen, outbuf, NULL,
				   write_simple_file_func, max_size);
	ci_simple_file_write(outbuf, "", 0, 1);
	return ret;
}

#else
int ci_brdeflate_to_membuf(const char *inbuf, size_t inlen, ci_membuf_t *outbuf,
			   ci_off_t max_size)
{
	ci_debug_printf(1, "brotliencode is not supported.\n");
	return CI_COMP_ERR_NONE;
}

int ci_brdeflate_to_simple_file(const char *inbuf, size_t inlen,
				struct ci_simple_file *outbuf,
				ci_off_t max_size)
{
	ci_debug_printf(1, "brotliencode is not supported.\n");
	return CI_COMP_ERR_NONE;
}
#endif

#ifdef HAVE_ZLIB
#define ZIP_HEAD_CRC     0x02   /* bit 1 set: header CRC present */
#define ZIP_EXTRA_FIELD  0x04   /* bit 2 set: extra field present */
#define ZIP_ORIG_NAME    0x08   /* bit 3 set: original file name present */
#define ZIP_COMMENT      0x10   /* bit 4 set: file comment present */

#define windowBits 15
#define GZIP_ENCODING 16

static void *alloc_a_buffer(void *op, unsigned int items, unsigned int size)
{
	return ci_buffer_alloc(items*size);
}

static void free_a_buffer(void *op, void *ptr)
{
	ci_buffer_free(ptr);
}

/*return CI_DEFLATE_ERRORS
*/
static int strm_init(z_stream * strm, int which, int inlen)
{
	int ret;

	strm->zalloc = alloc_a_buffer;
	strm->zfree  = free_a_buffer;
	strm->opaque = Z_NULL;

	switch (which) {
	case CI_ENCODE_DEFLATE:
		ci_debug_printf(4, "data-compression:  deflate called size: %d\n",
				inlen);
		ret = deflateInit(strm, Z_DEFAULT_COMPRESSION);
		break;
	case CI_ENCODE_GZIP:
	default:
		ci_debug_printf(4, "data-compression:  gzip called size: %d\n", inlen);
		ret = deflateInit2(strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
				   windowBits | GZIP_ENCODING, 8,
				   Z_DEFAULT_STRATEGY);
		break;
	}
	return ret;
}

int ci_mem_deflate(const char *inbuf, size_t inlen, void *out_obj,
		   char *(*get_outbuf)(void *obj, unsigned int *len),
		   int (*writefunc)(void *obj, const char *buf, size_t len),
		   ci_off_t max_size, int which) {

	int ret = Z_STREAM_END, written, outsize = 0;
	unsigned char out[CHUNK];
	z_stream strm;

	strm_init(&strm, which, inlen);
	strm.next_in = (unsigned char *) inbuf;
	strm.avail_in = inlen;
	do {
		int have;
		strm.avail_out = CHUNK;
		strm.next_out = out;
		ret = deflate(&strm, Z_FINISH);
		have = CHUNK - strm.avail_out;
		if ((written = writefunc(out_obj, (char *)out, have)) != have) {
			deflateEnd(&strm);
			return CI_COMP_ERR_CORRUPT;
		}
		outsize += written;
	}
	while (strm.avail_out == 0);

	deflateEnd (&strm);

	switch(which) {
	case CI_ENCODE_GZIP:
		ci_debug_printf(4, "data-compression:  gzip total compressed size %d ...\n", outsize);
		break;
	case CI_ENCODE_DEFLATE:
	default:
		ci_debug_printf(4, "data-compression:  deflate total compressed size %d ...\n", outsize);
		break;
	}
	return ret == Z_STREAM_END ? CI_COMP_OK : CI_COMP_ERR_CORRUPT;
}

int ci_deflate_to_membuf(const char *inbuf, size_t inlen, ci_membuf_t *outbuf,
			 ci_off_t max_size)
{
	int ret = ci_mem_deflate(inbuf, inlen, outbuf, NULL, write_membuf_func,
				 max_size, CI_ENCODE_DEFLATE);
	ci_membuf_write(outbuf, "", 0, 1);
	return ret;
}

int ci_deflate_to_simple_file(const char *inbuf, size_t inlen,
			      struct ci_simple_file *outbuf, ci_off_t max_size)
{
	int ret = ci_mem_deflate(inbuf, inlen, outbuf, NULL,
				 write_simple_file_func, max_size,
				 CI_ENCODE_DEFLATE);
	ci_simple_file_write(outbuf, "", 0, 1);
	return ret;
}

int ci_gzip_to_membuf(const char *inbuf, size_t inlen, ci_membuf_t *outbuf,
		      ci_off_t max_size)
{
	int ret = ci_mem_deflate(inbuf, inlen, outbuf, NULL, write_membuf_func,
				 max_size, CI_ENCODE_GZIP);
	ci_membuf_write(outbuf, "", 0, 1);
	return ret;
}

int ci_gzip_to_simple_file(const char *inbuf, size_t inlen,
			   struct ci_simple_file *outbuf, ci_off_t max_size)
{
	int ret = ci_mem_deflate(inbuf, inlen, outbuf, NULL,
				 write_simple_file_func,
				 max_size, CI_ENCODE_GZIP);
	ci_simple_file_write(outbuf, "", 0, 1);
	return ret;
}

#else
int ci_deflate_to_membuf(const char *inbuf, size_t inlen, ci_membuf_t *outbuf,
			 ci_off_t max_size)
{
	ci_debug_printf(1, "zlib/inflate is not supported.\n");
	return CI_COMP_ERR_NONE;
}

int ci_deflate_to_simple_file(const char *inbuf, size_t inlen,
			      struct ci_simple_file *outbuf, ci_off_t max_size)
{
	ci_debug_printf(1, "zlib/inflate is not supported.\n");
	return CI_COMP_ERR_NONE;
}
int ci_gzip_to_membuf(const char *inbuf, size_t inlen, ci_membuf_t *outbuf,
			 ci_off_t max_size)
{
	ci_debug_printf(1, "gzip is not supported.\n");
	return CI_COMP_ERR_NONE;
}

int ci_gzip_to_simple_file(const char *inbuf, size_t inlen,
			      struct ci_simple_file *outbuf, ci_off_t max_size)
{
	ci_debug_printf(1, "gzip is not supported.\n");
	return CI_COMP_ERR_NONE;
}
#endif

#ifdef HAVE_BZLIB
static void *bzalloc_a_buffer(void *op, int items, int size)
{
	return ci_buffer_alloc(items*size);
}

static void bzfree_a_buffer(void *op, void *ptr)
{
	ci_buffer_free(ptr);
}

int ci_mem_bzzip(const char *buf, int inlen,  void *out_obj,
		 char *(*get_outbuf)(void *obj, unsigned int *len),
		 int (*writefunc)(void *obj, const char *buf, size_t len),
		 ci_off_t max_size)
{
	int ret;
	unsigned have, written;
	long long outsize;
	bz_stream strm;
	char out[CHUNK];

	ci_debug_printf(4, "data-compression:  bzip called size: %d\n", inlen);

	strm.bzalloc = bzalloc_a_buffer;
	strm.bzfree = bzfree_a_buffer;
	strm.opaque = NULL;
	strm.avail_in = 0;
	strm.next_in = NULL;
	ret = BZ2_bzCompressInit(&strm,
				 9, // number of 100k blocks 9 is best compression (1-9)
				 0, // verbosity (0-4) 0-none 4 max
				 30); // work factor - 30 is default (0-250)
	if (ret != BZ_OK) {
		ci_debug_printf(1,
				"data-compression:  error initializing bzlib (BZ2_bzCompressInit return:%d)\n",
				ret);
		return CI_ERROR;
	}
	strm.next_in = (char *)buf;
	strm.avail_in = inlen;
	outsize = 0;

	do {
		strm.avail_out = CHUNK;
		strm.next_out = out;

		ret = BZ2_bzCompress(&strm, BZ_FINISH);
		switch (ret) {
		case BZ_PARAM_ERROR:
		case BZ_DATA_ERROR:
		case BZ_DATA_ERROR_MAGIC:
		case BZ_MEM_ERROR:
			BZ2_bzCompressEnd(&strm);
			return CI_ERROR;
		}

		have = CHUNK - strm.avail_out;
		if (!have ||
		    (written = writefunc(out_obj, (char *)out, have)) != have) {
			BZ2_bzCompressEnd(&strm);
			return CI_COMP_ERR_OUTPUT;
		}
		outsize += written;
	} while (strm.avail_out == 0);

	BZ2_bzCompressEnd(&strm);
	ci_debug_printf(4, "data-compression:  bzip total compressed size %lld ...\n",
			outsize);
	return CI_COMP_OK;
}

int ci_bzzip_to_membuf(const char *inbuf, size_t inlen, ci_membuf_t *outbuf,
		       ci_off_t max_size)
{
	int ret = ci_mem_bzzip(inbuf, inlen, outbuf, NULL, write_membuf_func,
			       max_size);
	ci_membuf_write(outbuf, "", 0, 1);
	return ret;
}

int ci_bzzip_to_simple_file(const char *inbuf, size_t inlen,
			    struct ci_simple_file *outbuf, ci_off_t max_size)
{
	int ret = ci_mem_bzzip(inbuf, inlen, outbuf, NULL,
			       write_simple_file_func, max_size);
	ci_simple_file_write(outbuf, "", 0, 1);
	return ret;
}

#else
int ci_bzzip_to_membuf(const char *inbuf, size_t inlen, ci_membuf_t *outbuf,
		       ci_off_t max_size)
{
	ci_debug_printf(1, "bzlib/bzzip is not supported.\n");
	return CI_COMP_ERR_NONE;
}

int ci_bzzip_to_simple_file(const char *inbuf, size_t inlen,
			    struct ci_simple_file *outbuf, ci_off_t max_size)
{
	ci_debug_printf(1, "bzlib/bzzip is not supported.\n");
	return CI_COMP_ERR_NONE;
}
#endif