blob: e8539baf0064406d05b122d90a838f7a890167c4 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11003 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * Interface to packet compression for ssh.
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Millere4340be2000-09-16 13:29:08 +11007 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110012 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100013
14#include "includes.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000015RCSID("$OpenBSD: compress.c,v 1.13 2001/02/08 19:30:51 itojun Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100016
Ben Lindstrom226cfa02001-01-22 05:34:40 +000017#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100018#include "buffer.h"
19#include "zlib.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000020#include "compress.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100021
22static z_stream incoming_stream;
23static z_stream outgoing_stream;
24
Damien Miller5428f641999-11-25 11:54:57 +110025/*
26 * Initializes compression; level is compression level from 1 to 9
27 * (as in gzip).
28 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100029
Damien Miller4af51302000-04-16 11:18:38 +100030void
Damien Miller95def091999-11-25 00:26:21 +110031buffer_compress_init(int level)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100032{
Damien Miller95def091999-11-25 00:26:21 +110033 debug("Enabling compression at level %d.", level);
34 if (level < 1 || level > 9)
35 fatal("Bad compression level %d.", level);
36 inflateInit(&incoming_stream);
37 deflateInit(&outgoing_stream, level);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100038}
39
40/* Frees any data structures allocated for compression. */
41
Damien Miller4af51302000-04-16 11:18:38 +100042void
Kevin Stevese7652402000-12-28 22:16:00 +000043buffer_compress_uninit(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044{
Damien Miller95def091999-11-25 00:26:21 +110045 debug("compress outgoing: raw data %lu, compressed %lu, factor %.2f",
46 outgoing_stream.total_in, outgoing_stream.total_out,
47 outgoing_stream.total_in == 0 ? 0.0 :
48 (double) outgoing_stream.total_out / outgoing_stream.total_in);
49 debug("compress incoming: raw data %lu, compressed %lu, factor %.2f",
50 incoming_stream.total_out, incoming_stream.total_in,
51 incoming_stream.total_out == 0 ? 0.0 :
52 (double) incoming_stream.total_in / incoming_stream.total_out);
53 inflateEnd(&incoming_stream);
54 deflateEnd(&outgoing_stream);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100055}
56
Damien Miller5428f641999-11-25 11:54:57 +110057/*
58 * Compresses the contents of input_buffer into output_buffer. All packets
59 * compressed using this function will form a single compressed data stream;
60 * however, data will be flushed at the end of every call so that each
61 * output_buffer can be decompressed independently (but in the appropriate
62 * order since they together form a single compression stream) by the
63 * receiver. This appends the compressed data to the output buffer.
64 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100065
Damien Miller4af51302000-04-16 11:18:38 +100066void
Damien Miller95def091999-11-25 00:26:21 +110067buffer_compress(Buffer * input_buffer, Buffer * output_buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068{
Damien Miller95def091999-11-25 00:26:21 +110069 char buf[4096];
70 int status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071
Damien Miller95def091999-11-25 00:26:21 +110072 /* This case is not handled below. */
73 if (buffer_len(input_buffer) == 0)
74 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075
Damien Miller95def091999-11-25 00:26:21 +110076 /* Input is the contents of the input buffer. */
Ben Lindstrom46c16222000-12-22 01:43:59 +000077 outgoing_stream.next_in = (u_char *) buffer_ptr(input_buffer);
Damien Miller95def091999-11-25 00:26:21 +110078 outgoing_stream.avail_in = buffer_len(input_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079
Damien Miller95def091999-11-25 00:26:21 +110080 /* Loop compressing until deflate() returns with avail_out != 0. */
81 do {
82 /* Set up fixed-size output buffer. */
Ben Lindstrom46c16222000-12-22 01:43:59 +000083 outgoing_stream.next_out = (u_char *)buf;
Damien Miller95def091999-11-25 00:26:21 +110084 outgoing_stream.avail_out = sizeof(buf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085
Damien Miller95def091999-11-25 00:26:21 +110086 /* Compress as much data into the buffer as possible. */
87 status = deflate(&outgoing_stream, Z_PARTIAL_FLUSH);
88 switch (status) {
89 case Z_OK:
90 /* Append compressed data to output_buffer. */
91 buffer_append(output_buffer, buf,
Damien Millerb38eff82000-04-01 11:09:21 +100092 sizeof(buf) - outgoing_stream.avail_out);
Damien Miller95def091999-11-25 00:26:21 +110093 break;
Damien Miller95def091999-11-25 00:26:21 +110094 default:
95 fatal("buffer_compress: deflate returned %d", status);
96 /* NOTREACHED */
97 }
Damien Millerb38eff82000-04-01 11:09:21 +100098 } while (outgoing_stream.avail_out == 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100099}
100
Damien Miller5428f641999-11-25 11:54:57 +1100101/*
102 * Uncompresses the contents of input_buffer into output_buffer. All packets
103 * uncompressed using this function will form a single compressed data
104 * stream; however, data will be flushed at the end of every call so that
105 * each output_buffer. This must be called for the same size units that the
106 * buffer_compress was called, and in the same order that buffers compressed
107 * with that. This appends the uncompressed data to the output buffer.
108 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000109
Damien Miller4af51302000-04-16 11:18:38 +1000110void
Damien Miller95def091999-11-25 00:26:21 +1100111buffer_uncompress(Buffer * input_buffer, Buffer * output_buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112{
Damien Miller95def091999-11-25 00:26:21 +1100113 char buf[4096];
114 int status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000115
Ben Lindstrom46c16222000-12-22 01:43:59 +0000116 incoming_stream.next_in = (u_char *) buffer_ptr(input_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100117 incoming_stream.avail_in = buffer_len(input_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118
Damien Miller95def091999-11-25 00:26:21 +1100119 for (;;) {
Damien Millerb38eff82000-04-01 11:09:21 +1000120 /* Set up fixed-size output buffer. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000121 incoming_stream.next_out = (u_char *) buf;
Damien Millerb38eff82000-04-01 11:09:21 +1000122 incoming_stream.avail_out = sizeof(buf);
123
Damien Miller95def091999-11-25 00:26:21 +1100124 status = inflate(&incoming_stream, Z_PARTIAL_FLUSH);
125 switch (status) {
126 case Z_OK:
127 buffer_append(output_buffer, buf,
Damien Millerb38eff82000-04-01 11:09:21 +1000128 sizeof(buf) - incoming_stream.avail_out);
Damien Miller95def091999-11-25 00:26:21 +1100129 break;
Damien Miller95def091999-11-25 00:26:21 +1100130 case Z_BUF_ERROR:
Damien Miller5428f641999-11-25 11:54:57 +1100131 /*
132 * Comments in zlib.h say that we should keep calling
133 * inflate() until we get an error. This appears to
134 * be the error that we get.
135 */
Damien Miller95def091999-11-25 00:26:21 +1100136 return;
Damien Miller95def091999-11-25 00:26:21 +1100137 default:
138 fatal("buffer_uncompress: inflate returned %d", status);
Damien Millerb38eff82000-04-01 11:09:21 +1000139 /* NOTREACHED */
Damien Miller95def091999-11-25 00:26:21 +1100140 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000142}