File: zlib.c

package info (click to toggle)
lsh-utils 2.1-12
  • links: PTS
  • area: main
  • in suites: buster
  • size: 12,884 kB
  • sloc: ansic: 51,017; sh: 5,683; lisp: 657; makefile: 381; perl: 63
file content (371 lines) | stat: -rw-r--r-- 9,456 bytes parent folder | download | duplicates (9)
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
/* zlib.c
 *
 * zlib compression algorithm
 * 
 */

/* lsh, an implementation of the ssh protocol
 *
 * Copyright (C) 1998 Balzs Scheidler, Niels Mller
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#if HAVE_CONFIG_H
#include "config.h"
#endif

#include "compress.h"
#include "format.h"
#include "lsh_string.h"
#include "ssh.h"
#include "string_buffer.h"
#include "werror.h"
#include "xalloc.h"

#if WITH_ZLIB

#if HAVE_ZLIB_H
#include <zlib.h>
#endif

#include <assert.h>

static void do_free_zstream(z_stream *z);
  
#include "zlib.c.x"

/* GABA:
   (class
     (name zlib_instance)
     (super compress_instance)
     (vars
       ; Fail before producing larger packets than this
       (max . uint32_t)
       (rate . uint32_t)
       (f pointer (function int "z_stream *" int))
       (z indirect-special z_stream
          #f do_free_zstream)))
*/

/* GABA:
   (class
     (name zlib_algorithm)
     (super compress_algorithm)
     (vars
       (level . int)))
*/

/* Stored in the opaque pointer. */
struct zlib_type
{
  int (*free_func)(z_stream *z);
  const char *operation;
};

#define ZLIB_TYPE(z) ((struct zlib_type *)((z)->opaque))

static const struct zlib_type
zlib_inflate = {  inflateEnd, "inflate" };

static const struct zlib_type
zlib_deflate = {  deflateEnd, "deflate" };

/* zlib memory functions */
static void *
zlib_alloc(void *opaque UNUSED, unsigned int items, unsigned int size)
{
  return lsh_space_alloc(items * size);
}

static void
zlib_free(void *opaque UNUSED, void *address)
{
  lsh_space_free(address);
}

static void
do_free_zstream(z_stream *z)
{
  /* We use the opaque pointer, as there's nothing else to help us
   * figure if we should be calling inflateEnd or deflateEnd. */

  const struct zlib_type *type = ZLIB_TYPE(z);
  
  int res = type->free_func(z);

  if (res != Z_OK)
    debug("do_free_zstream (%z): Freeing failed: %z\n",
	  type->operation, z->msg ? z->msg : "No error");
}

/* Estimates of the resulting packet sizes. We use fixnum arithmetic,
 * with one represented as 1<<10=1024. Only rates between 1/16 and 16
 * are used. This may be a little too conservative; I have observed
 * compression ratios of about 50. */
 
#define RATE_UNIT 1024
#define RATE_MAX (RATE_UNIT * 16)
#define RATE_MIN (RATE_UNIT / 16)
#define MARGIN 200
#define INSIGNIFICANT 100

static uint32_t estimate_size(uint32_t rate, uint32_t input, uint32_t max)
{
  uint32_t guess = rate * input / RATE_UNIT + MARGIN;
  return MIN(max, guess);
}

/* Assumes that input is nonzero */
static uint32_t estimate_update(uint32_t rate, uint32_t input, uint32_t output)
{
  /* Decay old estimate */
  rate = rate * 15 / 16;

  /* NOTE: Following the envelope is suboptimal for small inputs. We
   * do it only for input packets of reasonable size. This method
   * could be improved.
   *
   * Perhaps a linear combination k * rate + (1-k) estimate, where k
   * depends on the size of the sample (i.e. input) would make sense?
   * Or use different rate estimates for different lengths? */
  
  if (input > INSIGNIFICANT)
    {
      uint32_t estimate = output * RATE_UNIT / input;

      if (estimate > RATE_MAX)
	return RATE_MAX;

      /* Follow the "envelope" */
      rate = MAX(estimate, rate);
    }
  
  return MAX(rate, RATE_MIN);
}

/* Compress incoming data */
static struct lsh_string *
do_zlib(struct compress_instance *c,
	struct lsh_string *packet,
	int free)
{
  CAST(zlib_instance, self, c);
  struct string_buffer buffer;
  
  /* LIMIT keeps track of the amount of storage we may still need to
   * allocate. To detect that a packet grows unexpectedly large, we
   * need a little extra buffer space beyond the maximum size. */
  uint32_t limit = self->max + 1;
  uint32_t input = lsh_string_length(packet);
  uint32_t estimate;
  
  debug("do_zlib (%z): length in: %i\n",
	ZLIB_TYPE(&self->z)->operation, input);
  
  if (!input)
    {
      werror("do_zlib (%z): Compressing empty packet.\n",
	     ZLIB_TYPE(&self->z)->operation);
      return free ? packet : lsh_string_dup(packet);
    }

  estimate = estimate_size(self->rate, input, limit);
  debug("do_zlib (%z): estimate:  %i\n",
	ZLIB_TYPE(&self->z)->operation,
	estimate);

  string_buffer_init(&buffer, estimate);

  limit -= lsh_string_length(buffer.partial);

  /* The cast is needed because zlib, at least version 1.1.4, doesn't
     use const */
  self->z.next_in = (uint8_t *) lsh_string_data(packet);
  self->z.avail_in = input;

  for (;;)
    {
      int rc;
      
      assert(buffer.left);
      
      rc = lsh_string_zlib(buffer.partial, buffer.pos,
			   self->f, &self->z, Z_SYNC_FLUSH,
			   buffer.left);

      switch (rc)
	{
	case Z_BUF_ERROR:
	  /* If avail_in is zero, this just means that all data have
	   * been flushed. */
	  if (self->z.avail_in)
	    werror("do_zlib (%z): Z_BUF_ERROR (probably harmless),\n"
		   "  avail_in = %i, avail_out = %i\n",
		   ZLIB_TYPE(&self->z)->operation,
		   self->z.avail_in, self->z.avail_out);
	  /* Fall through */
	case Z_OK:
	  break;
	default:
	  werror("do_zlib: %z failed: %z\n",
		 ZLIB_TYPE(&self->z)->operation,
		 self->z.msg ? self->z.msg : "No error(?)");
	  if (free)
	    lsh_string_free(packet);
	  
	  return NULL;
	}
      
      /* NOTE: It's not enough to check that avail_in is zero to
       * determine that all data have been flushed. avail_in == 0 and
       * avail_out > 0 implies that all data has been flushed, but if
       * avail_in == avail_out == 0, we have to allocate more output
       * space. */
	 
      if (!self->z.avail_in && !self->z.avail_out)
	debug("do_zlib (%z): Both avail_in and avail_out are zero.\n",
	      ZLIB_TYPE(&self->z)->operation);
      
      if (!self->z.avail_out)
	{ /* All output space consumed */
	  uint32_t plength = lsh_string_length(buffer.partial);
	  
	  if (!limit)
	    {
	      werror("do_zlib (%z): Packet grew too large!\n",
		     ZLIB_TYPE(&self->z)->operation);
	      
	      if (free)
		lsh_string_free(packet);

	      string_buffer_clear(&buffer);
	      return NULL;
	    }

	  /* Grow to about double size. */
	  string_buffer_grow(&buffer,
			     MIN(limit, plength + buffer.total + 100));
	  limit -= plength;
	}
      else if (!self->z.avail_in)
	{ /* Compressed entire packet */
	  uint32_t output;
	  
	  if (free)
	    lsh_string_free(packet);
	  
	  packet =
	    string_buffer_final(&buffer, self->z.avail_out);

	  output = lsh_string_length(packet);
	  assert(output <= self->max);

	  debug("do_zlib (%z): length out: %i\n",
		ZLIB_TYPE(&self->z)->operation,
		output);

	  if (output > estimate)
	    debug("do_zlib (%z): Estimated size exceeded: input = %i, estimate = %i, output = %i\n",
		  ZLIB_TYPE(&self->z)->operation,
		  input, estimate, output);
	  
	  self->rate = estimate_update(self->rate, input, output);

	  return packet;
	}
    }
}

static struct compress_instance *
make_zlib_instance(struct compress_algorithm *c, int mode)
{
  CAST(zlib_algorithm, closure, c);
  NEW(zlib_instance, res);

  res->z.zalloc = zlib_alloc;
  res->z.zfree = zlib_free;

  switch (mode)
    {
      case COMPRESS_DEFLATE:
	res->z.opaque = (void *) &zlib_deflate;
	res->f = deflate;
        res->super.codec = do_zlib;

	/* Maximum expansion is 0.1% + 12 bytes. We use 1% + 12, to be
	 * conservative.
	 *
	 * FIXME: These figures are documented for the entire stream,
	 * does they really apply to all segments, separated by
	 * Z_SYNC_FLUSH ? */

	res->max = SSH_MAX_PACKET + SSH_MAX_PACKET / 100 + 12;
	res->rate = RATE_UNIT;
	
        if (deflateInit(&res->z, closure->level) != Z_OK)
          {
            werror("deflateInit failed: %z\n",
                   res->z.msg ? res->z.msg : "No error(?)");
            KILL(res);
            return NULL;
          }
        break;

    case COMPRESS_INFLATE:
	res->z.opaque = (void *) &zlib_inflate;
	res->f = inflate;
        res->super.codec = do_zlib;

	/* FIXME: Perhaps we ought to use the connection's
	 * rec_max_packet size? */
	res->max = SSH_MAX_PACKET;
	res->rate = 2 * RATE_UNIT;
	
        if (inflateInit(&res->z) != Z_OK)
          {
            werror("inflateInit failed: %z\n",
                   res->z.msg ? res->z.msg : "No error(?)");
            KILL(res);
            return NULL;
          }
        break;
    }
  return &res->super;
}

struct compress_algorithm *make_zlib_algorithm(int level)
{
  if ( (level != Z_DEFAULT_COMPRESSION)
       && ( (level < Z_NO_COMPRESSION)
	    || (level > Z_BEST_COMPRESSION) ))
    return NULL;
  else
    {
      NEW(zlib_algorithm, closure);
      
      closure->super.make_compress = make_zlib_instance;
      closure->level = level;

      return &closure->super;
    }
}

struct compress_algorithm *make_zlib(void)
{
  return make_zlib_algorithm(Z_DEFAULT_COMPRESSION);
}

#endif /* WITH_ZLIB */