blob: 85a361d3a58ec282a84ad40da8f43ed4e643b4b5 [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 Lindstrom0f345f52002-03-22 01:51:24 +000015RCSID("$OpenBSD: compress.c,v 1.19 2002/03/18 17:31:54 provos 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 Miller95def091999-11-25 00:26:21 +110059 debug("compress outgoing: raw data %lu, compressed %lu, factor %.2f",
Damien Miller9f0f5c62001-12-21 14:45:46 +110060 outgoing_stream.total_in, outgoing_stream.total_out,
61 outgoing_stream.total_in == 0 ? 0.0 :
62 (double) outgoing_stream.total_out / outgoing_stream.total_in);
Damien Miller95def091999-11-25 00:26:21 +110063 debug("compress incoming: raw data %lu, compressed %lu, factor %.2f",
Damien Miller9f0f5c62001-12-21 14:45:46 +110064 incoming_stream.total_out, incoming_stream.total_in,
65 incoming_stream.total_out == 0 ? 0.0 :
66 (double) incoming_stream.total_in / incoming_stream.total_out);
Ben Lindstromce398b22002-03-22 01:17:52 +000067 if (compress_init_recv_called == 1 && inflate_failed == 0)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +000068 inflateEnd(&incoming_stream);
Ben Lindstromce398b22002-03-22 01:17:52 +000069 if (compress_init_send_called == 1 && deflate_failed == 0)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +000070 deflateEnd(&outgoing_stream);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071}
72
Damien Miller5428f641999-11-25 11:54:57 +110073/*
74 * Compresses the contents of input_buffer into output_buffer. All packets
75 * compressed using this function will form a single compressed data stream;
76 * however, data will be flushed at the end of every call so that each
77 * output_buffer can be decompressed independently (but in the appropriate
78 * order since they together form a single compression stream) by the
79 * receiver. This appends the compressed data to the output buffer.
80 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081
Damien Miller4af51302000-04-16 11:18:38 +100082void
Damien Miller95def091999-11-25 00:26:21 +110083buffer_compress(Buffer * input_buffer, Buffer * output_buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084{
Damien Miller708d21c2002-01-22 23:18:15 +110085 u_char buf[4096];
Damien Miller95def091999-11-25 00:26:21 +110086 int status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087
Damien Miller95def091999-11-25 00:26:21 +110088 /* This case is not handled below. */
89 if (buffer_len(input_buffer) == 0)
90 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091
Damien Miller95def091999-11-25 00:26:21 +110092 /* Input is the contents of the input buffer. */
Damien Miller708d21c2002-01-22 23:18:15 +110093 outgoing_stream.next_in = buffer_ptr(input_buffer);
Damien Miller95def091999-11-25 00:26:21 +110094 outgoing_stream.avail_in = buffer_len(input_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100095
Damien Miller95def091999-11-25 00:26:21 +110096 /* Loop compressing until deflate() returns with avail_out != 0. */
97 do {
98 /* Set up fixed-size output buffer. */
Damien Miller708d21c2002-01-22 23:18:15 +110099 outgoing_stream.next_out = buf;
Damien Miller95def091999-11-25 00:26:21 +1100100 outgoing_stream.avail_out = sizeof(buf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000101
Damien Miller95def091999-11-25 00:26:21 +1100102 /* Compress as much data into the buffer as possible. */
103 status = deflate(&outgoing_stream, Z_PARTIAL_FLUSH);
104 switch (status) {
105 case Z_OK:
106 /* Append compressed data to output_buffer. */
107 buffer_append(output_buffer, buf,
Damien Millerb38eff82000-04-01 11:09:21 +1000108 sizeof(buf) - outgoing_stream.avail_out);
Damien Miller95def091999-11-25 00:26:21 +1100109 break;
Damien Miller95def091999-11-25 00:26:21 +1100110 default:
Ben Lindstromce398b22002-03-22 01:17:52 +0000111 deflate_failed = 1;
Damien Miller95def091999-11-25 00:26:21 +1100112 fatal("buffer_compress: deflate returned %d", status);
113 /* NOTREACHED */
114 }
Damien Millerb38eff82000-04-01 11:09:21 +1000115 } while (outgoing_stream.avail_out == 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116}
117
Damien Miller5428f641999-11-25 11:54:57 +1100118/*
119 * Uncompresses the contents of input_buffer into output_buffer. All packets
120 * uncompressed using this function will form a single compressed data
121 * stream; however, data will be flushed at the end of every call so that
122 * each output_buffer. This must be called for the same size units that the
123 * buffer_compress was called, and in the same order that buffers compressed
124 * with that. This appends the uncompressed data to the output buffer.
125 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126
Damien Miller4af51302000-04-16 11:18:38 +1000127void
Damien Miller95def091999-11-25 00:26:21 +1100128buffer_uncompress(Buffer * input_buffer, Buffer * output_buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000129{
Damien Miller708d21c2002-01-22 23:18:15 +1100130 u_char buf[4096];
Damien Miller95def091999-11-25 00:26:21 +1100131 int status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000132
Damien Miller708d21c2002-01-22 23:18:15 +1100133 incoming_stream.next_in = buffer_ptr(input_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100134 incoming_stream.avail_in = buffer_len(input_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000135
Damien Miller95def091999-11-25 00:26:21 +1100136 for (;;) {
Damien Millerb38eff82000-04-01 11:09:21 +1000137 /* Set up fixed-size output buffer. */
Damien Miller708d21c2002-01-22 23:18:15 +1100138 incoming_stream.next_out = buf;
Damien Millerb38eff82000-04-01 11:09:21 +1000139 incoming_stream.avail_out = sizeof(buf);
140
Damien Miller95def091999-11-25 00:26:21 +1100141 status = inflate(&incoming_stream, Z_PARTIAL_FLUSH);
142 switch (status) {
143 case Z_OK:
144 buffer_append(output_buffer, buf,
Damien Millerb38eff82000-04-01 11:09:21 +1000145 sizeof(buf) - incoming_stream.avail_out);
Damien Miller95def091999-11-25 00:26:21 +1100146 break;
Damien Miller95def091999-11-25 00:26:21 +1100147 case Z_BUF_ERROR:
Damien Miller5428f641999-11-25 11:54:57 +1100148 /*
149 * Comments in zlib.h say that we should keep calling
150 * inflate() until we get an error. This appears to
151 * be the error that we get.
152 */
Damien Miller95def091999-11-25 00:26:21 +1100153 return;
Damien Miller95def091999-11-25 00:26:21 +1100154 default:
Ben Lindstromce398b22002-03-22 01:17:52 +0000155 inflate_failed = 1;
Damien Miller95def091999-11-25 00:26:21 +1100156 fatal("buffer_uncompress: inflate returned %d", status);
Damien Millerb38eff82000-04-01 11:09:21 +1000157 /* NOTREACHED */
Damien Miller95def091999-11-25 00:26:21 +1100158 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000159 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000160}