blob: 24778e524b4a0199382e574eb2f56ed644e7103d [file] [log] [blame]
Damien Miller3796ab42010-09-10 11:20:59 +10001/* $OpenBSD: compress.c,v 1.26 2010/09/08 04:13:31 deraadt Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Interface to packet compression for ssh.
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100016
Damien Millerd7834352006-08-05 12:39:39 +100017#include <sys/types.h>
18
19#include <stdarg.h>
20
Ben Lindstrom226cfa02001-01-22 05:34:40 +000021#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100022#include "buffer.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000023#include "compress.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100024
Damien Miller3796ab42010-09-10 11:20:59 +100025#include <zlib.h>
26
Ben Lindstrom0f345f52002-03-22 01:51:24 +000027z_stream incoming_stream;
28z_stream outgoing_stream;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +000029static int compress_init_send_called = 0;
30static int compress_init_recv_called = 0;
Ben Lindstromce398b22002-03-22 01:17:52 +000031static int inflate_failed = 0;
32static int deflate_failed = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100033
Damien Miller5428f641999-11-25 11:54:57 +110034/*
35 * Initializes compression; level is compression level from 1 to 9
36 * (as in gzip).
37 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100038
Damien Miller4af51302000-04-16 11:18:38 +100039void
Ben Lindstromfb50cdf2001-04-05 23:20:46 +000040buffer_compress_init_send(int level)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041{
Ben Lindstromfb50cdf2001-04-05 23:20:46 +000042 if (compress_init_send_called == 1)
Ben Lindstrom96f8d142001-10-03 17:07:47 +000043 deflateEnd(&outgoing_stream);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +000044 compress_init_send_called = 1;
Damien Miller95def091999-11-25 00:26:21 +110045 debug("Enabling compression at level %d.", level);
46 if (level < 1 || level > 9)
47 fatal("Bad compression level %d.", level);
Damien Miller95def091999-11-25 00:26:21 +110048 deflateInit(&outgoing_stream, level);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100049}
Ben Lindstromfb50cdf2001-04-05 23:20:46 +000050void
51buffer_compress_init_recv(void)
52{
53 if (compress_init_recv_called == 1)
54 inflateEnd(&incoming_stream);
55 compress_init_recv_called = 1;
56 inflateInit(&incoming_stream);
57}
Damien Millerd4a8b7e1999-10-27 13:42:43 +100058
59/* Frees any data structures allocated for compression. */
60
Damien Miller4af51302000-04-16 11:18:38 +100061void
Kevin Stevese7652402000-12-28 22:16:00 +000062buffer_compress_uninit(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063{
Damien Miller8f341f82004-01-21 11:00:46 +110064 debug("compress outgoing: raw data %llu, compressed %llu, factor %.2f",
Damien Millerf84fed62004-01-21 11:01:23 +110065 (unsigned long long)outgoing_stream.total_in,
66 (unsigned long long)outgoing_stream.total_out,
Damien Miller9f0f5c62001-12-21 14:45:46 +110067 outgoing_stream.total_in == 0 ? 0.0 :
68 (double) outgoing_stream.total_out / outgoing_stream.total_in);
Damien Miller8f341f82004-01-21 11:00:46 +110069 debug("compress incoming: raw data %llu, compressed %llu, factor %.2f",
Damien Millerf84fed62004-01-21 11:01:23 +110070 (unsigned long long)incoming_stream.total_out,
71 (unsigned long long)incoming_stream.total_in,
Damien Miller9f0f5c62001-12-21 14:45:46 +110072 incoming_stream.total_out == 0 ? 0.0 :
73 (double) incoming_stream.total_in / incoming_stream.total_out);
Ben Lindstromce398b22002-03-22 01:17:52 +000074 if (compress_init_recv_called == 1 && inflate_failed == 0)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +000075 inflateEnd(&incoming_stream);
Ben Lindstromce398b22002-03-22 01:17:52 +000076 if (compress_init_send_called == 1 && deflate_failed == 0)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +000077 deflateEnd(&outgoing_stream);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078}
79
Damien Miller5428f641999-11-25 11:54:57 +110080/*
81 * Compresses the contents of input_buffer into output_buffer. All packets
82 * compressed using this function will form a single compressed data stream;
83 * however, data will be flushed at the end of every call so that each
84 * output_buffer can be decompressed independently (but in the appropriate
85 * order since they together form a single compression stream) by the
86 * receiver. This appends the compressed data to the output buffer.
87 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088
Damien Miller4af51302000-04-16 11:18:38 +100089void
Damien Miller95def091999-11-25 00:26:21 +110090buffer_compress(Buffer * input_buffer, Buffer * output_buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091{
Damien Miller708d21c2002-01-22 23:18:15 +110092 u_char buf[4096];
Damien Miller95def091999-11-25 00:26:21 +110093 int status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094
Damien Miller95def091999-11-25 00:26:21 +110095 /* This case is not handled below. */
96 if (buffer_len(input_buffer) == 0)
97 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098
Damien Miller95def091999-11-25 00:26:21 +110099 /* Input is the contents of the input buffer. */
Damien Miller708d21c2002-01-22 23:18:15 +1100100 outgoing_stream.next_in = buffer_ptr(input_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100101 outgoing_stream.avail_in = buffer_len(input_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000102
Damien Miller95def091999-11-25 00:26:21 +1100103 /* Loop compressing until deflate() returns with avail_out != 0. */
104 do {
105 /* Set up fixed-size output buffer. */
Damien Miller708d21c2002-01-22 23:18:15 +1100106 outgoing_stream.next_out = buf;
Damien Miller95def091999-11-25 00:26:21 +1100107 outgoing_stream.avail_out = sizeof(buf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108
Damien Miller95def091999-11-25 00:26:21 +1100109 /* Compress as much data into the buffer as possible. */
110 status = deflate(&outgoing_stream, Z_PARTIAL_FLUSH);
111 switch (status) {
112 case Z_OK:
113 /* Append compressed data to output_buffer. */
114 buffer_append(output_buffer, buf,
Damien Millerb38eff82000-04-01 11:09:21 +1000115 sizeof(buf) - outgoing_stream.avail_out);
Damien Miller95def091999-11-25 00:26:21 +1100116 break;
Damien Miller95def091999-11-25 00:26:21 +1100117 default:
Ben Lindstromce398b22002-03-22 01:17:52 +0000118 deflate_failed = 1;
Damien Miller95def091999-11-25 00:26:21 +1100119 fatal("buffer_compress: deflate returned %d", status);
120 /* NOTREACHED */
121 }
Damien Millerb38eff82000-04-01 11:09:21 +1000122 } while (outgoing_stream.avail_out == 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123}
124
Damien Miller5428f641999-11-25 11:54:57 +1100125/*
126 * Uncompresses the contents of input_buffer into output_buffer. All packets
127 * uncompressed using this function will form a single compressed data
128 * stream; however, data will be flushed at the end of every call so that
129 * each output_buffer. This must be called for the same size units that the
130 * buffer_compress was called, and in the same order that buffers compressed
131 * with that. This appends the uncompressed data to the output buffer.
132 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000133
Damien Miller4af51302000-04-16 11:18:38 +1000134void
Damien Miller95def091999-11-25 00:26:21 +1100135buffer_uncompress(Buffer * input_buffer, Buffer * output_buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000136{
Damien Miller708d21c2002-01-22 23:18:15 +1100137 u_char buf[4096];
Damien Miller95def091999-11-25 00:26:21 +1100138 int status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139
Damien Miller708d21c2002-01-22 23:18:15 +1100140 incoming_stream.next_in = buffer_ptr(input_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100141 incoming_stream.avail_in = buffer_len(input_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000142
Damien Miller95def091999-11-25 00:26:21 +1100143 for (;;) {
Damien Millerb38eff82000-04-01 11:09:21 +1000144 /* Set up fixed-size output buffer. */
Damien Miller708d21c2002-01-22 23:18:15 +1100145 incoming_stream.next_out = buf;
Damien Millerb38eff82000-04-01 11:09:21 +1000146 incoming_stream.avail_out = sizeof(buf);
147
Damien Miller95def091999-11-25 00:26:21 +1100148 status = inflate(&incoming_stream, Z_PARTIAL_FLUSH);
149 switch (status) {
150 case Z_OK:
151 buffer_append(output_buffer, buf,
Damien Millerb38eff82000-04-01 11:09:21 +1000152 sizeof(buf) - incoming_stream.avail_out);
Damien Miller95def091999-11-25 00:26:21 +1100153 break;
Damien Miller95def091999-11-25 00:26:21 +1100154 case Z_BUF_ERROR:
Damien Miller5428f641999-11-25 11:54:57 +1100155 /*
156 * Comments in zlib.h say that we should keep calling
157 * inflate() until we get an error. This appears to
158 * be the error that we get.
159 */
Damien Miller95def091999-11-25 00:26:21 +1100160 return;
Damien Miller95def091999-11-25 00:26:21 +1100161 default:
Ben Lindstromce398b22002-03-22 01:17:52 +0000162 inflate_failed = 1;
Damien Miller95def091999-11-25 00:26:21 +1100163 fatal("buffer_uncompress: inflate returned %d", status);
Damien Millerb38eff82000-04-01 11:09:21 +1000164 /* NOTREACHED */
Damien Miller95def091999-11-25 00:26:21 +1100165 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000166 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000167}