blob: 0d1c7e55e8ed49180229982c9208c07833d38f37 [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 Millerf84fed62004-01-21 11:01:23 +110015RCSID("$OpenBSD: compress.c,v 1.21 2004/01/13 19:45:15 markus 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
Ben Lindstrom0f345f52002-03-22 01:51:24 +000022z_stream incoming_stream;
23z_stream outgoing_stream;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +000024static int compress_init_send_called = 0;
25static int compress_init_recv_called = 0;
Ben Lindstromce398b22002-03-22 01:17:52 +000026static int inflate_failed = 0;
27static int deflate_failed = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100028
Damien Miller5428f641999-11-25 11:54:57 +110029/*
30 * Initializes compression; level is compression level from 1 to 9
31 * (as in gzip).
32 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100033
Damien Miller4af51302000-04-16 11:18:38 +100034void
Ben Lindstromfb50cdf2001-04-05 23:20:46 +000035buffer_compress_init_send(int level)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100036{
Ben Lindstromfb50cdf2001-04-05 23:20:46 +000037 if (compress_init_send_called == 1)
Ben Lindstrom96f8d142001-10-03 17:07:47 +000038 deflateEnd(&outgoing_stream);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +000039 compress_init_send_called = 1;
Damien Miller95def091999-11-25 00:26:21 +110040 debug("Enabling compression at level %d.", level);
41 if (level < 1 || level > 9)
42 fatal("Bad compression level %d.", level);
Damien Miller95def091999-11-25 00:26:21 +110043 deflateInit(&outgoing_stream, level);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044}
Ben Lindstromfb50cdf2001-04-05 23:20:46 +000045void
46buffer_compress_init_recv(void)
47{
48 if (compress_init_recv_called == 1)
49 inflateEnd(&incoming_stream);
50 compress_init_recv_called = 1;
51 inflateInit(&incoming_stream);
52}
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053
54/* Frees any data structures allocated for compression. */
55
Damien Miller4af51302000-04-16 11:18:38 +100056void
Kevin Stevese7652402000-12-28 22:16:00 +000057buffer_compress_uninit(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100058{
Damien Miller8f341f82004-01-21 11:00:46 +110059 debug("compress outgoing: raw data %llu, compressed %llu, factor %.2f",
Damien Millerf84fed62004-01-21 11:01:23 +110060 (unsigned long long)outgoing_stream.total_in,
61 (unsigned long long)outgoing_stream.total_out,
Damien Miller9f0f5c62001-12-21 14:45:46 +110062 outgoing_stream.total_in == 0 ? 0.0 :
63 (double) outgoing_stream.total_out / outgoing_stream.total_in);
Damien Miller8f341f82004-01-21 11:00:46 +110064 debug("compress incoming: raw data %llu, compressed %llu, factor %.2f",
Damien Millerf84fed62004-01-21 11:01:23 +110065 (unsigned long long)incoming_stream.total_out,
66 (unsigned long long)incoming_stream.total_in,
Damien Miller9f0f5c62001-12-21 14:45:46 +110067 incoming_stream.total_out == 0 ? 0.0 :
68 (double) incoming_stream.total_in / incoming_stream.total_out);
Ben Lindstromce398b22002-03-22 01:17:52 +000069 if (compress_init_recv_called == 1 && inflate_failed == 0)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +000070 inflateEnd(&incoming_stream);
Ben Lindstromce398b22002-03-22 01:17:52 +000071 if (compress_init_send_called == 1 && deflate_failed == 0)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +000072 deflateEnd(&outgoing_stream);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100073}
74
Damien Miller5428f641999-11-25 11:54:57 +110075/*
76 * Compresses the contents of input_buffer into output_buffer. All packets
77 * compressed using this function will form a single compressed data stream;
78 * however, data will be flushed at the end of every call so that each
79 * output_buffer can be decompressed independently (but in the appropriate
80 * order since they together form a single compression stream) by the
81 * receiver. This appends the compressed data to the output buffer.
82 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100083
Damien Miller4af51302000-04-16 11:18:38 +100084void
Damien Miller95def091999-11-25 00:26:21 +110085buffer_compress(Buffer * input_buffer, Buffer * output_buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086{
Damien Miller708d21c2002-01-22 23:18:15 +110087 u_char buf[4096];
Damien Miller95def091999-11-25 00:26:21 +110088 int status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089
Damien Miller95def091999-11-25 00:26:21 +110090 /* This case is not handled below. */
91 if (buffer_len(input_buffer) == 0)
92 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093
Damien Miller95def091999-11-25 00:26:21 +110094 /* Input is the contents of the input buffer. */
Damien Miller708d21c2002-01-22 23:18:15 +110095 outgoing_stream.next_in = buffer_ptr(input_buffer);
Damien Miller95def091999-11-25 00:26:21 +110096 outgoing_stream.avail_in = buffer_len(input_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100097
Damien Miller95def091999-11-25 00:26:21 +110098 /* Loop compressing until deflate() returns with avail_out != 0. */
99 do {
100 /* Set up fixed-size output buffer. */
Damien Miller708d21c2002-01-22 23:18:15 +1100101 outgoing_stream.next_out = buf;
Damien Miller95def091999-11-25 00:26:21 +1100102 outgoing_stream.avail_out = sizeof(buf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103
Damien Miller95def091999-11-25 00:26:21 +1100104 /* Compress as much data into the buffer as possible. */
105 status = deflate(&outgoing_stream, Z_PARTIAL_FLUSH);
106 switch (status) {
107 case Z_OK:
108 /* Append compressed data to output_buffer. */
109 buffer_append(output_buffer, buf,
Damien Millerb38eff82000-04-01 11:09:21 +1000110 sizeof(buf) - outgoing_stream.avail_out);
Damien Miller95def091999-11-25 00:26:21 +1100111 break;
Damien Miller95def091999-11-25 00:26:21 +1100112 default:
Ben Lindstromce398b22002-03-22 01:17:52 +0000113 deflate_failed = 1;
Damien Miller95def091999-11-25 00:26:21 +1100114 fatal("buffer_compress: deflate returned %d", status);
115 /* NOTREACHED */
116 }
Damien Millerb38eff82000-04-01 11:09:21 +1000117 } while (outgoing_stream.avail_out == 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118}
119
Damien Miller5428f641999-11-25 11:54:57 +1100120/*
121 * Uncompresses the contents of input_buffer into output_buffer. All packets
122 * uncompressed using this function will form a single compressed data
123 * stream; however, data will be flushed at the end of every call so that
124 * each output_buffer. This must be called for the same size units that the
125 * buffer_compress was called, and in the same order that buffers compressed
126 * with that. This appends the uncompressed data to the output buffer.
127 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128
Damien Miller4af51302000-04-16 11:18:38 +1000129void
Damien Miller95def091999-11-25 00:26:21 +1100130buffer_uncompress(Buffer * input_buffer, Buffer * output_buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000131{
Damien Miller708d21c2002-01-22 23:18:15 +1100132 u_char buf[4096];
Damien Miller95def091999-11-25 00:26:21 +1100133 int status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000134
Damien Miller708d21c2002-01-22 23:18:15 +1100135 incoming_stream.next_in = buffer_ptr(input_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100136 incoming_stream.avail_in = buffer_len(input_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000137
Damien Miller95def091999-11-25 00:26:21 +1100138 for (;;) {
Damien Millerb38eff82000-04-01 11:09:21 +1000139 /* Set up fixed-size output buffer. */
Damien Miller708d21c2002-01-22 23:18:15 +1100140 incoming_stream.next_out = buf;
Damien Millerb38eff82000-04-01 11:09:21 +1000141 incoming_stream.avail_out = sizeof(buf);
142
Damien Miller95def091999-11-25 00:26:21 +1100143 status = inflate(&incoming_stream, Z_PARTIAL_FLUSH);
144 switch (status) {
145 case Z_OK:
146 buffer_append(output_buffer, buf,
Damien Millerb38eff82000-04-01 11:09:21 +1000147 sizeof(buf) - incoming_stream.avail_out);
Damien Miller95def091999-11-25 00:26:21 +1100148 break;
Damien Miller95def091999-11-25 00:26:21 +1100149 case Z_BUF_ERROR:
Damien Miller5428f641999-11-25 11:54:57 +1100150 /*
151 * Comments in zlib.h say that we should keep calling
152 * inflate() until we get an error. This appears to
153 * be the error that we get.
154 */
Damien Miller95def091999-11-25 00:26:21 +1100155 return;
Damien Miller95def091999-11-25 00:26:21 +1100156 default:
Ben Lindstromce398b22002-03-22 01:17:52 +0000157 inflate_failed = 1;
Damien Miller95def091999-11-25 00:26:21 +1100158 fatal("buffer_uncompress: inflate returned %d", status);
Damien Millerb38eff82000-04-01 11:09:21 +1000159 /* NOTREACHED */
Damien Miller95def091999-11-25 00:26:21 +1100160 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000161 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000162}