blob: 6f8d6b77550ae3fd1a47ae7678d84fd459a4349f [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"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100015
Ben Lindstrom226cfa02001-01-22 05:34:40 +000016#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100017#include "buffer.h"
18#include "zlib.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000019#include "compress.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100020
Ben Lindstrom0f345f52002-03-22 01:51:24 +000021z_stream incoming_stream;
22z_stream outgoing_stream;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +000023static int compress_init_send_called = 0;
24static int compress_init_recv_called = 0;
Ben Lindstromce398b22002-03-22 01:17:52 +000025static int inflate_failed = 0;
26static int deflate_failed = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027
Damien Miller5428f641999-11-25 11:54:57 +110028/*
29 * Initializes compression; level is compression level from 1 to 9
30 * (as in gzip).
31 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100032
Damien Miller4af51302000-04-16 11:18:38 +100033void
Ben Lindstromfb50cdf2001-04-05 23:20:46 +000034buffer_compress_init_send(int level)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100035{
Ben Lindstromfb50cdf2001-04-05 23:20:46 +000036 if (compress_init_send_called == 1)
Ben Lindstrom96f8d142001-10-03 17:07:47 +000037 deflateEnd(&outgoing_stream);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +000038 compress_init_send_called = 1;
Damien Miller95def091999-11-25 00:26:21 +110039 debug("Enabling compression at level %d.", level);
40 if (level < 1 || level > 9)
41 fatal("Bad compression level %d.", level);
Damien Miller95def091999-11-25 00:26:21 +110042 deflateInit(&outgoing_stream, level);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100043}
Ben Lindstromfb50cdf2001-04-05 23:20:46 +000044void
45buffer_compress_init_recv(void)
46{
47 if (compress_init_recv_called == 1)
48 inflateEnd(&incoming_stream);
49 compress_init_recv_called = 1;
50 inflateInit(&incoming_stream);
51}
Damien Millerd4a8b7e1999-10-27 13:42:43 +100052
53/* Frees any data structures allocated for compression. */
54
Damien Miller4af51302000-04-16 11:18:38 +100055void
Kevin Stevese7652402000-12-28 22:16:00 +000056buffer_compress_uninit(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100057{
Damien Miller8f341f82004-01-21 11:00:46 +110058 debug("compress outgoing: raw data %llu, compressed %llu, factor %.2f",
Damien Millerf84fed62004-01-21 11:01:23 +110059 (unsigned long long)outgoing_stream.total_in,
60 (unsigned long long)outgoing_stream.total_out,
Damien Miller9f0f5c62001-12-21 14:45:46 +110061 outgoing_stream.total_in == 0 ? 0.0 :
62 (double) outgoing_stream.total_out / outgoing_stream.total_in);
Damien Miller8f341f82004-01-21 11:00:46 +110063 debug("compress incoming: raw data %llu, compressed %llu, factor %.2f",
Damien Millerf84fed62004-01-21 11:01:23 +110064 (unsigned long long)incoming_stream.total_out,
65 (unsigned long long)incoming_stream.total_in,
Damien Miller9f0f5c62001-12-21 14:45:46 +110066 incoming_stream.total_out == 0 ? 0.0 :
67 (double) incoming_stream.total_in / incoming_stream.total_out);
Ben Lindstromce398b22002-03-22 01:17:52 +000068 if (compress_init_recv_called == 1 && inflate_failed == 0)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +000069 inflateEnd(&incoming_stream);
Ben Lindstromce398b22002-03-22 01:17:52 +000070 if (compress_init_send_called == 1 && deflate_failed == 0)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +000071 deflateEnd(&outgoing_stream);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072}
73
Damien Miller5428f641999-11-25 11:54:57 +110074/*
75 * Compresses the contents of input_buffer into output_buffer. All packets
76 * compressed using this function will form a single compressed data stream;
77 * however, data will be flushed at the end of every call so that each
78 * output_buffer can be decompressed independently (but in the appropriate
79 * order since they together form a single compression stream) by the
80 * receiver. This appends the compressed data to the output buffer.
81 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082
Damien Miller4af51302000-04-16 11:18:38 +100083void
Damien Miller95def091999-11-25 00:26:21 +110084buffer_compress(Buffer * input_buffer, Buffer * output_buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085{
Damien Miller708d21c2002-01-22 23:18:15 +110086 u_char buf[4096];
Damien Miller95def091999-11-25 00:26:21 +110087 int status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088
Damien Miller95def091999-11-25 00:26:21 +110089 /* This case is not handled below. */
90 if (buffer_len(input_buffer) == 0)
91 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092
Damien Miller95def091999-11-25 00:26:21 +110093 /* Input is the contents of the input buffer. */
Damien Miller708d21c2002-01-22 23:18:15 +110094 outgoing_stream.next_in = buffer_ptr(input_buffer);
Damien Miller95def091999-11-25 00:26:21 +110095 outgoing_stream.avail_in = buffer_len(input_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096
Damien Miller95def091999-11-25 00:26:21 +110097 /* Loop compressing until deflate() returns with avail_out != 0. */
98 do {
99 /* Set up fixed-size output buffer. */
Damien Miller708d21c2002-01-22 23:18:15 +1100100 outgoing_stream.next_out = buf;
Damien Miller95def091999-11-25 00:26:21 +1100101 outgoing_stream.avail_out = sizeof(buf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000102
Damien Miller95def091999-11-25 00:26:21 +1100103 /* Compress as much data into the buffer as possible. */
104 status = deflate(&outgoing_stream, Z_PARTIAL_FLUSH);
105 switch (status) {
106 case Z_OK:
107 /* Append compressed data to output_buffer. */
108 buffer_append(output_buffer, buf,
Damien Millerb38eff82000-04-01 11:09:21 +1000109 sizeof(buf) - outgoing_stream.avail_out);
Damien Miller95def091999-11-25 00:26:21 +1100110 break;
Damien Miller95def091999-11-25 00:26:21 +1100111 default:
Ben Lindstromce398b22002-03-22 01:17:52 +0000112 deflate_failed = 1;
Damien Miller95def091999-11-25 00:26:21 +1100113 fatal("buffer_compress: deflate returned %d", status);
114 /* NOTREACHED */
115 }
Damien Millerb38eff82000-04-01 11:09:21 +1000116 } while (outgoing_stream.avail_out == 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117}
118
Damien Miller5428f641999-11-25 11:54:57 +1100119/*
120 * Uncompresses the contents of input_buffer into output_buffer. All packets
121 * uncompressed using this function will form a single compressed data
122 * stream; however, data will be flushed at the end of every call so that
123 * each output_buffer. This must be called for the same size units that the
124 * buffer_compress was called, and in the same order that buffers compressed
125 * with that. This appends the uncompressed data to the output buffer.
126 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000127
Damien Miller4af51302000-04-16 11:18:38 +1000128void
Damien Miller95def091999-11-25 00:26:21 +1100129buffer_uncompress(Buffer * input_buffer, Buffer * output_buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000130{
Damien Miller708d21c2002-01-22 23:18:15 +1100131 u_char buf[4096];
Damien Miller95def091999-11-25 00:26:21 +1100132 int status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000133
Damien Miller708d21c2002-01-22 23:18:15 +1100134 incoming_stream.next_in = buffer_ptr(input_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100135 incoming_stream.avail_in = buffer_len(input_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000136
Damien Miller95def091999-11-25 00:26:21 +1100137 for (;;) {
Damien Millerb38eff82000-04-01 11:09:21 +1000138 /* Set up fixed-size output buffer. */
Damien Miller708d21c2002-01-22 23:18:15 +1100139 incoming_stream.next_out = buf;
Damien Millerb38eff82000-04-01 11:09:21 +1000140 incoming_stream.avail_out = sizeof(buf);
141
Damien Miller95def091999-11-25 00:26:21 +1100142 status = inflate(&incoming_stream, Z_PARTIAL_FLUSH);
143 switch (status) {
144 case Z_OK:
145 buffer_append(output_buffer, buf,
Damien Millerb38eff82000-04-01 11:09:21 +1000146 sizeof(buf) - incoming_stream.avail_out);
Damien Miller95def091999-11-25 00:26:21 +1100147 break;
Damien Miller95def091999-11-25 00:26:21 +1100148 case Z_BUF_ERROR:
Damien Miller5428f641999-11-25 11:54:57 +1100149 /*
150 * Comments in zlib.h say that we should keep calling
151 * inflate() until we get an error. This appears to
152 * be the error that we get.
153 */
Damien Miller95def091999-11-25 00:26:21 +1100154 return;
Damien Miller95def091999-11-25 00:26:21 +1100155 default:
Ben Lindstromce398b22002-03-22 01:17:52 +0000156 inflate_failed = 1;
Damien Miller95def091999-11-25 00:26:21 +1100157 fatal("buffer_uncompress: inflate returned %d", status);
Damien Millerb38eff82000-04-01 11:09:21 +1000158 /* NOTREACHED */
Damien Miller95def091999-11-25 00:26:21 +1100159 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000160 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000161}