blob: c1e1afd7950f23f3d67be4a6f9b8c486c0921880 [file] [log] [blame]
William Lallemand82fe75c2012-10-23 10:25:10 +02001/*
2 * HTTP compression.
3 *
4 * Copyright 2012 Exceliance, David Du Colombier <dducolombier@exceliance.fr>
5 * William Lallemand <wlallemand@exceliance.fr>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <stdio.h>
Willy Tarreau34763642012-10-26 15:05:35 +020015
William Lallemand08289f12012-10-31 11:19:18 +010016#ifdef USE_ZLIB
Willy Tarreau34763642012-10-26 15:05:35 +020017/* Note: the crappy zlib and openssl libs both define the "free_func" type.
18 * That's a very clever idea to use such a generic name in general purpose
19 * libraries, really... The zlib one is easier to redefine than openssl's,
20 * so let's only fix this one.
21 */
22#define free_func zlib_free_func
William Lallemand82fe75c2012-10-23 10:25:10 +020023#include <zlib.h>
Willy Tarreau34763642012-10-26 15:05:35 +020024#undef free_func
William Lallemand08289f12012-10-31 11:19:18 +010025#endif /* USE_ZLIB */
William Lallemand82fe75c2012-10-23 10:25:10 +020026
27#include <common/compat.h>
William Lallemand2b502472012-10-30 14:30:39 +010028#include <common/memory.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020029
30#include <types/global.h>
31#include <types/compression.h>
32
33#include <proto/compression.h>
William Lallemandd85f9172012-11-09 17:05:39 +010034#include <proto/freq_ctr.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020035#include <proto/proto_http.h>
36
William Lallemand2b502472012-10-30 14:30:39 +010037
38#ifdef USE_ZLIB
39
William Lallemand8b52bb32012-11-16 18:06:41 +010040static void *alloc_zlib(void *opaque, unsigned int items, unsigned int size);
41static void free_zlib(void *opaque, void *ptr);
42
William Lallemand2b502472012-10-30 14:30:39 +010043/* zlib allocation */
44static struct pool_head *zlib_pool_deflate_state = NULL;
45static struct pool_head *zlib_pool_window = NULL;
46static struct pool_head *zlib_pool_prev = NULL;
47static struct pool_head *zlib_pool_head = NULL;
48static struct pool_head *zlib_pool_pending_buf = NULL;
49
William Lallemand9d5f5482012-11-07 16:12:57 +010050static long long zlib_memory_available = -1;
51
William Lallemand2b502472012-10-30 14:30:39 +010052#endif
53
William Lallemand8b52bb32012-11-16 18:06:41 +010054static struct pool_head *pool_comp_ctx = NULL;
55
William Lallemand2b502472012-10-30 14:30:39 +010056
Cyril Bonté6162c432012-11-10 19:27:47 +010057const struct comp_algo comp_algos[] =
William Lallemand82fe75c2012-10-23 10:25:10 +020058{
59 { "identity", 8, identity_init, identity_add_data, identity_flush, identity_reset, identity_end },
60#ifdef USE_ZLIB
61 { "deflate", 7, deflate_init, deflate_add_data, deflate_flush, deflate_reset, deflate_end },
62 { "gzip", 4, gzip_init, deflate_add_data, deflate_flush, deflate_reset, deflate_end },
63#endif /* USE_ZLIB */
64 { NULL, 0, NULL , NULL, NULL, NULL, NULL }
65};
66
67/*
68 * Add a content-type in the configuration
69 */
70int comp_append_type(struct comp *comp, const char *type)
71{
72 struct comp_type *comp_type;
73
74 comp_type = calloc(1, sizeof(struct comp_type));
75 comp_type->name_len = strlen(type);
76 comp_type->name = strdup(type);
77 comp_type->next = comp->types;
78 comp->types = comp_type;
79 return 0;
80}
81
82/*
83 * Add an algorithm in the configuration
84 */
85int comp_append_algo(struct comp *comp, const char *algo)
86{
87 struct comp_algo *comp_algo;
88 int i;
89
90 for (i = 0; comp_algos[i].name; i++) {
91 if (!strcmp(algo, comp_algos[i].name)) {
92 comp_algo = calloc(1, sizeof(struct comp_algo));
93 memmove(comp_algo, &comp_algos[i], sizeof(struct comp_algo));
94 comp_algo->next = comp->algos;
95 comp->algos = comp_algo;
96 return 0;
97 }
98 }
99 return -1;
100}
101
102/* emit the chunksize followed by a CRLF on the output and return the number of
103 * bytes written. Appends <add_crlf> additional CRLF after the first one. Chunk
104 * sizes are truncated to 6 hex digits (16 MB) and padded left. The caller is
105 * responsible for ensuring there is enough room left in the output buffer for
106 * the string (8 bytes * add_crlf*2).
107 */
108int http_emit_chunk_size(char *out, unsigned int chksz, int add_crlf)
109{
110 int shift;
111 int pos = 0;
112
113 for (shift = 20; shift >= 0; shift -= 4)
114 out[pos++] = hextab[(chksz >> shift) & 0xF];
115
116 do {
117 out[pos++] = '\r';
118 out[pos++] = '\n';
119 } while (--add_crlf >= 0);
120
121 return pos;
122}
123
124/*
125 * Init HTTP compression
126 */
127int http_compression_buffer_init(struct session *s, struct buffer *in, struct buffer *out)
128{
129 struct http_msg *msg = &s->txn.rsp;
130 int left;
131
132 /* not enough space */
133 if (in->size - buffer_len(in) < 40)
134 return -1;
135
136 /*
137 * Skip data, we don't need them in the new buffer. They are results
138 * of CHUNK_CRLF and CHUNK_SIZE parsing.
139 */
140 b_adv(in, msg->next);
141 msg->next = 0;
142 msg->sov = 0;
143 msg->sol = 0;
144
145 out->size = global.tune.bufsize;
146 out->i = 0;
147 out->o = 0;
148 out->p = out->data;
149 /* copy output data */
150 if (in->o > 0) {
151 left = in->o - bo_contig_data(in);
152 memcpy(out->data, bo_ptr(in), bo_contig_data(in));
153 out->p += bo_contig_data(in);
154 if (left > 0) { /* second part of the buffer */
155 memcpy(out->p, in->data, left);
156 out->p += left;
157 }
158 out->o = in->o;
159 }
160 out->i += http_emit_chunk_size(out->p, 0, 0);
161
162 return 0;
163}
164
165/*
166 * Add data to compress
167 */
168int http_compression_buffer_add_data(struct session *s, struct buffer *in, struct buffer *out)
169{
170 struct http_msg *msg = &s->txn.rsp;
William Lallemandbf3ae612012-11-19 12:35:37 +0100171 int consumed_data = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +0200172 int data_process_len;
173 int left;
174 int ret;
175
176 /*
177 * Skip data, we don't need them in the new buffer. They are results
178 * of CHUNK_CRLF and CHUNK_SIZE parsing.
179 */
180 b_adv(in, msg->next);
181 msg->next = 0;
182 msg->sov = 0;
183 msg->sol = 0;
184
185 /*
186 * select the smallest size between the announced chunk size, the input
187 * data, and the available output buffer size
188 */
189 data_process_len = MIN(in->i, msg->chunk_len);
190 data_process_len = MIN(out->size - buffer_len(out), data_process_len);
191
192 left = data_process_len - bi_contig_data(in);
193 if (left <= 0) {
William Lallemand8b52bb32012-11-16 18:06:41 +0100194 consumed_data += ret = s->comp_algo->add_data(s->comp_ctx, bi_ptr(in), data_process_len, out);
William Lallemand82fe75c2012-10-23 10:25:10 +0200195 if (ret < 0)
196 return -1;
William Lallemand82fe75c2012-10-23 10:25:10 +0200197
198 } else {
William Lallemand8b52bb32012-11-16 18:06:41 +0100199 consumed_data += ret = s->comp_algo->add_data(s->comp_ctx, bi_ptr(in), bi_contig_data(in), out);
William Lallemand82fe75c2012-10-23 10:25:10 +0200200 if (ret < 0)
201 return -1;
William Lallemand8b52bb32012-11-16 18:06:41 +0100202 consumed_data += ret = s->comp_algo->add_data(s->comp_ctx, in->data, left, out);
William Lallemand82fe75c2012-10-23 10:25:10 +0200203 if (ret < 0)
204 return -1;
William Lallemand82fe75c2012-10-23 10:25:10 +0200205 }
206
207 b_adv(in, data_process_len);
208 msg->chunk_len -= data_process_len;
209
William Lallemandbf3ae612012-11-19 12:35:37 +0100210 return consumed_data;
William Lallemand82fe75c2012-10-23 10:25:10 +0200211}
212
213/*
214 * Flush data in process, and write the header and footer of the chunk. Upon
215 * success, in and out buffers are swapped to avoid a copy.
216 */
217int http_compression_buffer_end(struct session *s, struct buffer **in, struct buffer **out, int end)
218{
219 int to_forward;
220 int left;
221 struct http_msg *msg = &s->txn.rsp;
222 struct buffer *ib = *in, *ob = *out;
William Lallemand08289f12012-10-31 11:19:18 +0100223
224#ifdef USE_ZLIB
William Lallemand82fe75c2012-10-23 10:25:10 +0200225 int ret;
226
227 /* flush data here */
228
229 if (end)
William Lallemand8b52bb32012-11-16 18:06:41 +0100230 ret = s->comp_algo->flush(s->comp_ctx, ob, Z_FINISH); /* end of data */
William Lallemand82fe75c2012-10-23 10:25:10 +0200231 else
William Lallemand8b52bb32012-11-16 18:06:41 +0100232 ret = s->comp_algo->flush(s->comp_ctx, ob, Z_SYNC_FLUSH); /* end of buffer */
William Lallemand82fe75c2012-10-23 10:25:10 +0200233
234 if (ret < 0)
235 return -1; /* flush failed */
236
William Lallemand08289f12012-10-31 11:19:18 +0100237#endif /* USE_ZLIB */
238
William Lallemand82fe75c2012-10-23 10:25:10 +0200239 if (ob->i > 8) {
240 /* more than a chunk size => some data were emitted */
241 char *tail = ob->p + ob->i;
242
243 /* write real size at the begining of the chunk, no need of wrapping */
244 http_emit_chunk_size(ob->p, ob->i - 8, 0);
245
246 /* chunked encoding requires CRLF after data */
247 *tail++ = '\r';
248 *tail++ = '\n';
249
250 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->chunk_len == 0) {
251 /* End of data, 0<CRLF><CRLF> is needed but we're not
252 * in chunked mode on input so we must add it ourselves.
253 */
254 memcpy(tail, "0\r\n\r\n", 5);
255 tail += 5;
256 }
257 ob->i = tail - ob->p;
258 } else {
259 /* no data were sent, cancel the chunk size */
260 ob->i = 0;
261 }
262
263 to_forward = ob->i;
William Lallemandd85f9172012-11-09 17:05:39 +0100264 /* update input rate */
William Lallemand8b52bb32012-11-16 18:06:41 +0100265 if (s->comp_ctx && s->comp_ctx->cur_lvl > 0)
William Lallemandd85f9172012-11-09 17:05:39 +0100266 update_freq_ctr(&global.comp_bps_in, ib->o - ob->o);
267
William Lallemand82fe75c2012-10-23 10:25:10 +0200268 /* copy the remaining data in the tmp buffer. */
269 if (ib->i > 0) {
270 left = ib->i - bi_contig_data(ib);
271 memcpy(bi_end(ob), bi_ptr(ib), bi_contig_data(ib));
272 ob->i += bi_contig_data(ib);
273 if (left > 0) {
274 memcpy(bi_end(ob), ib->data, left);
275 ob->i += left;
276 }
277 }
278
279 /* swap the buffers */
280 *in = ob;
281 *out = ib;
282
William Lallemand8b52bb32012-11-16 18:06:41 +0100283 if (s->comp_ctx && s->comp_ctx->cur_lvl > 0)
William Lallemandd85f9172012-11-09 17:05:39 +0100284 update_freq_ctr(&global.comp_bps_out, to_forward);
285
William Lallemand82fe75c2012-10-23 10:25:10 +0200286 /* forward the new chunk without remaining data */
287 b_adv(ob, to_forward);
288
289 /* if there are data between p and next, there are trailers, must forward them */
290 b_adv(ob, msg->next);
291 msg->next = 0;
292
293 return to_forward;
294}
295
William Lallemand8b52bb32012-11-16 18:06:41 +0100296/*
297 * Alloc the comp_ctx
298 */
299static inline int init_comp_ctx(struct comp_ctx **comp_ctx)
300{
301#ifdef USE_ZLIB
302 z_stream *strm;
303
304 if (global.maxzlibmem > 0 && zlib_memory_available < 0)
305 zlib_memory_available = global.maxzlibmem * 1024 * 1024; /* Megabytes to bytes */
306
307 if (global.maxzlibmem > 0 && zlib_memory_available < sizeof(struct comp_ctx))
308 return -1;
309#endif
310
311 if (unlikely(pool_comp_ctx == NULL))
312 pool_comp_ctx = create_pool("comp_ctx", sizeof(struct comp_ctx), MEM_F_SHARED);
313
314 *comp_ctx = pool_alloc2(pool_comp_ctx);
315 if (*comp_ctx == NULL)
316 return -1;
317#ifdef USE_ZLIB
318 zlib_memory_available -= sizeof(struct comp_ctx);
319
320 strm = &(*comp_ctx)->strm;
321 strm->zalloc = alloc_zlib;
322 strm->zfree = free_zlib;
323 strm->opaque = *comp_ctx;
324#endif
325 return 0;
326}
327
328/*
329 * Dealloc the comp_ctx
330 */
331static inline int deinit_comp_ctx(struct comp_ctx **comp_ctx)
332{
333 if (!*comp_ctx)
334 return 0;
335
336 pool_free2(pool_comp_ctx, *comp_ctx);
337 *comp_ctx = NULL;
338
339#ifdef USE_ZLIB
340 zlib_memory_available += sizeof(struct comp_ctx);
341#endif
342
343 return 0;
344}
345
William Lallemand82fe75c2012-10-23 10:25:10 +0200346
347/****************************
348 **** Identity algorithm ****
349 ****************************/
350
351/*
352 * Init the identity algorithm
353 */
William Lallemand8b52bb32012-11-16 18:06:41 +0100354int identity_init(struct comp_ctx **comp_ctx, int level)
William Lallemand82fe75c2012-10-23 10:25:10 +0200355{
356 return 0;
357}
358
359/*
360 * Process data
William Lallemandbf3ae612012-11-19 12:35:37 +0100361 * Return size of consumed data or -1 on error
William Lallemand82fe75c2012-10-23 10:25:10 +0200362 */
William Lallemandbf3ae612012-11-19 12:35:37 +0100363int identity_add_data(struct comp_ctx *comp_ctx, const char *in_data, int in_len, struct buffer *out)
William Lallemand82fe75c2012-10-23 10:25:10 +0200364{
William Lallemandbf3ae612012-11-19 12:35:37 +0100365 char *out_data = bi_end(out);
366 int out_len = out->size - buffer_len(out);
367
William Lallemand82fe75c2012-10-23 10:25:10 +0200368 if (out_len < in_len)
369 return -1;
370
371 memcpy(out_data, in_data, in_len);
372
William Lallemandbf3ae612012-11-19 12:35:37 +0100373 out->i += in_len;
374
William Lallemand82fe75c2012-10-23 10:25:10 +0200375 return in_len;
376}
377
William Lallemand1c2d6222012-10-30 15:52:53 +0100378int identity_flush(struct comp_ctx *comp_ctx, struct buffer *out, int flag)
William Lallemand82fe75c2012-10-23 10:25:10 +0200379{
380 return 0;
381}
382
383
William Lallemand1c2d6222012-10-30 15:52:53 +0100384int identity_reset(struct comp_ctx *comp_ctx)
William Lallemand82fe75c2012-10-23 10:25:10 +0200385{
386 return 0;
387}
388
389/*
390 * Deinit the algorithm
391 */
William Lallemand8b52bb32012-11-16 18:06:41 +0100392int identity_end(struct comp_ctx **comp_ctx)
William Lallemand82fe75c2012-10-23 10:25:10 +0200393{
394 return 0;
395}
396
397
398#ifdef USE_ZLIB
William Lallemand2b502472012-10-30 14:30:39 +0100399/*
400 * This is a tricky allocation function using the zlib.
401 * This is based on the allocation order in deflateInit2.
402 */
403static void *alloc_zlib(void *opaque, unsigned int items, unsigned int size)
404{
405 struct comp_ctx *ctx = opaque;
406 static char round = 0; /* order in deflateInit2 */
407 void *buf = NULL;
408
William Lallemand9d5f5482012-11-07 16:12:57 +0100409 if (global.maxzlibmem > 0 && zlib_memory_available < items * size){
410 buf = NULL;
411 goto end;
412 }
413
William Lallemand2b502472012-10-30 14:30:39 +0100414 switch (round) {
415 case 0:
416 if (zlib_pool_deflate_state == NULL)
417 zlib_pool_deflate_state = create_pool("zlib_state", size * items, MEM_F_SHARED);
418 ctx->zlib_deflate_state = buf = pool_alloc2(zlib_pool_deflate_state);
419 break;
420
421 case 1:
422 if (zlib_pool_window == NULL)
423 zlib_pool_window = create_pool("zlib_window", size * items, MEM_F_SHARED);
424 ctx->zlib_window = buf = pool_alloc2(zlib_pool_window);
425 break;
426
427 case 2:
428 if (zlib_pool_prev == NULL)
429 zlib_pool_prev = create_pool("zlib_prev", size * items, MEM_F_SHARED);
430 ctx->zlib_prev = buf = pool_alloc2(zlib_pool_prev);
431 break;
432
433 case 3:
434 if (zlib_pool_head == NULL)
435 zlib_pool_head = create_pool("zlib_head", size * items, MEM_F_SHARED);
436 ctx->zlib_head = buf = pool_alloc2(zlib_pool_head);
437 break;
438
439 case 4:
440 if (zlib_pool_pending_buf == NULL)
441 zlib_pool_pending_buf = create_pool("zlib_pending_buf", size * items, MEM_F_SHARED);
442 ctx->zlib_pending_buf = buf = pool_alloc2(zlib_pool_pending_buf);
443 break;
444 }
William Lallemand9d5f5482012-11-07 16:12:57 +0100445 if (buf != NULL && global.maxzlibmem > 0)
446 zlib_memory_available -= items * size;
447
448end:
William Lallemand2b502472012-10-30 14:30:39 +0100449
Willy Tarreau46909852012-11-15 14:57:56 +0100450 /* deflateInit2() first allocates and checks the deflate_state, then if
451 * it succeeds, it allocates all other 4 areas at ones and checks them
452 * at the end. So we want to correctly count the rounds depending on when
453 * zlib is supposed to abort.
454 */
455 if (buf || round)
456 round = (round + 1) % 5;
William Lallemand2b502472012-10-30 14:30:39 +0100457 return buf;
458}
459
460static void free_zlib(void *opaque, void *ptr)
461{
462 struct comp_ctx *ctx = opaque;
Willy Tarreaub1fbd052012-11-10 17:49:37 +0100463 struct pool_head *pool = NULL;
William Lallemand2b502472012-10-30 14:30:39 +0100464
465 if (ptr == ctx->zlib_window)
William Lallemand9d5f5482012-11-07 16:12:57 +0100466 pool = zlib_pool_window;
William Lallemand2b502472012-10-30 14:30:39 +0100467 else if (ptr == ctx->zlib_deflate_state)
William Lallemand9d5f5482012-11-07 16:12:57 +0100468 pool = zlib_pool_deflate_state;
William Lallemand2b502472012-10-30 14:30:39 +0100469 else if (ptr == ctx->zlib_prev)
William Lallemand9d5f5482012-11-07 16:12:57 +0100470 pool = zlib_pool_prev;
William Lallemand2b502472012-10-30 14:30:39 +0100471 else if (ptr == ctx->zlib_head)
William Lallemand9d5f5482012-11-07 16:12:57 +0100472 pool = zlib_pool_head;
William Lallemand2b502472012-10-30 14:30:39 +0100473 else if (ptr == ctx->zlib_pending_buf)
William Lallemand9d5f5482012-11-07 16:12:57 +0100474 pool = zlib_pool_pending_buf;
William Lallemand2b502472012-10-30 14:30:39 +0100475
William Lallemand9d5f5482012-11-07 16:12:57 +0100476 pool_free2(pool, ptr);
477 if (global.maxzlibmem > 0)
478 zlib_memory_available += pool->size;
William Lallemand2b502472012-10-30 14:30:39 +0100479}
480
William Lallemand82fe75c2012-10-23 10:25:10 +0200481/**************************
482**** gzip algorithm ****
483***************************/
William Lallemand8b52bb32012-11-16 18:06:41 +0100484int gzip_init(struct comp_ctx **comp_ctx, int level)
William Lallemand82fe75c2012-10-23 10:25:10 +0200485{
William Lallemand8b52bb32012-11-16 18:06:41 +0100486 z_stream *strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200487
William Lallemand8b52bb32012-11-16 18:06:41 +0100488 if (init_comp_ctx(comp_ctx) < 0)
489 return -1;
William Lallemand9d5f5482012-11-07 16:12:57 +0100490
William Lallemand8b52bb32012-11-16 18:06:41 +0100491 strm = &(*comp_ctx)->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200492
William Lallemand8b52bb32012-11-16 18:06:41 +0100493 if (deflateInit2(strm, level, Z_DEFLATED, global.tune.zlibwindowsize + 16, global.tune.zlibmemlevel, Z_DEFAULT_STRATEGY) != Z_OK) {
494 deinit_comp_ctx(comp_ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +0200495 return -1;
William Lallemand8b52bb32012-11-16 18:06:41 +0100496 }
497
498 (*comp_ctx)->cur_lvl = level;
William Lallemand82fe75c2012-10-23 10:25:10 +0200499
500 return 0;
501}
502/**************************
503**** Deflate algorithm ****
504***************************/
505
William Lallemand8b52bb32012-11-16 18:06:41 +0100506int deflate_init(struct comp_ctx **comp_ctx, int level)
William Lallemand82fe75c2012-10-23 10:25:10 +0200507{
William Lallemand8b52bb32012-11-16 18:06:41 +0100508 z_stream *strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200509
William Lallemand8b52bb32012-11-16 18:06:41 +0100510 if (init_comp_ctx(comp_ctx) < 0)
511 return -1;
512
513 strm = &(*comp_ctx)->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200514
William Lallemand8b52bb32012-11-16 18:06:41 +0100515 if (deflateInit(strm, level) != Z_OK) {
516 deinit_comp_ctx(comp_ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +0200517 return -1;
William Lallemand8b52bb32012-11-16 18:06:41 +0100518 }
519
520 (*comp_ctx)->cur_lvl = level;
William Lallemand82fe75c2012-10-23 10:25:10 +0200521
522 return 0;
523}
524
William Lallemandbf3ae612012-11-19 12:35:37 +0100525/* Return the size of consumed data or -1 */
526int deflate_add_data(struct comp_ctx *comp_ctx, const char *in_data, int in_len, struct buffer *out)
William Lallemand82fe75c2012-10-23 10:25:10 +0200527{
William Lallemand82fe75c2012-10-23 10:25:10 +0200528 int ret;
William Lallemandbf3ae612012-11-19 12:35:37 +0100529 z_stream *strm = &comp_ctx->strm;
530 char *out_data = bi_end(out);
531 int out_len = out->size - buffer_len(out);
William Lallemand82fe75c2012-10-23 10:25:10 +0200532
533 if (in_len <= 0)
534 return 0;
535
536
537 if (out_len <= 0)
538 return -1;
539
William Lallemand82fe75c2012-10-23 10:25:10 +0200540 strm->next_in = (unsigned char *)in_data;
541 strm->avail_in = in_len;
542 strm->next_out = (unsigned char *)out_data;
543 strm->avail_out = out_len;
544
545 ret = deflate(strm, Z_NO_FLUSH);
546 if (ret != Z_OK)
547 return -1;
548
549 /* deflate update the available data out */
William Lallemandbf3ae612012-11-19 12:35:37 +0100550 out->i += out_len - strm->avail_out;
William Lallemand82fe75c2012-10-23 10:25:10 +0200551
William Lallemandbf3ae612012-11-19 12:35:37 +0100552 return in_len - strm->avail_in;
William Lallemand82fe75c2012-10-23 10:25:10 +0200553}
554
William Lallemand1c2d6222012-10-30 15:52:53 +0100555int deflate_flush(struct comp_ctx *comp_ctx, struct buffer *out, int flag)
William Lallemand82fe75c2012-10-23 10:25:10 +0200556{
557 int ret;
William Lallemand82fe75c2012-10-23 10:25:10 +0200558 int out_len = 0;
William Lallemand1c2d6222012-10-30 15:52:53 +0100559 z_stream *strm = &comp_ctx->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200560
William Lallemand82fe75c2012-10-23 10:25:10 +0200561 strm->next_out = (unsigned char *)bi_end(out);
562 strm->avail_out = out->size - buffer_len(out);
563
564 ret = deflate(strm, flag);
565 if (ret != Z_OK && ret != Z_STREAM_END)
566 return -1;
567
568 out_len = (out->size - buffer_len(out)) - strm->avail_out;
569 out->i += out_len;
570
William Lallemandd85f9172012-11-09 17:05:39 +0100571 /* compression rate limit */
572 if (global.comp_rate_lim > 0) {
573
574 if (read_freq_ctr(&global.comp_bps_out) > global.comp_rate_lim) {
575 /* decrease level */
576 if (comp_ctx->cur_lvl > 0) {
577 comp_ctx->cur_lvl--;
578 deflateParams(&comp_ctx->strm, comp_ctx->cur_lvl, Z_DEFAULT_STRATEGY);
579 }
580
581 } else if (comp_ctx->cur_lvl < global.comp_rate_lim) {
582 /* increase level */
583 comp_ctx->cur_lvl++ ;
584 deflateParams(&comp_ctx->strm, comp_ctx->cur_lvl, Z_DEFAULT_STRATEGY);
585 }
586 }
587
William Lallemand82fe75c2012-10-23 10:25:10 +0200588 return out_len;
589}
590
William Lallemand1c2d6222012-10-30 15:52:53 +0100591int deflate_reset(struct comp_ctx *comp_ctx)
William Lallemand82fe75c2012-10-23 10:25:10 +0200592{
William Lallemand1c2d6222012-10-30 15:52:53 +0100593 z_stream *strm = &comp_ctx->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200594
William Lallemand82fe75c2012-10-23 10:25:10 +0200595 if (deflateReset(strm) == Z_OK)
596 return 0;
597 return -1;
598}
599
William Lallemand8b52bb32012-11-16 18:06:41 +0100600int deflate_end(struct comp_ctx **comp_ctx)
William Lallemand82fe75c2012-10-23 10:25:10 +0200601{
William Lallemand8b52bb32012-11-16 18:06:41 +0100602 z_stream *strm = &(*comp_ctx)->strm;
603 int ret;
William Lallemand82fe75c2012-10-23 10:25:10 +0200604
William Lallemand8b52bb32012-11-16 18:06:41 +0100605 ret = deflateEnd(strm);
William Lallemand82fe75c2012-10-23 10:25:10 +0200606
William Lallemand8b52bb32012-11-16 18:06:41 +0100607 deinit_comp_ctx(comp_ctx);
608
609 return ret;
William Lallemand82fe75c2012-10-23 10:25:10 +0200610}
611
612#endif /* USE_ZLIB */
613