blob: c058d222412fba86df23cf7e679bf15533988cf7 [file] [log] [blame]
Damien Miller63b94122006-08-19 00:21:46 +10001/* $OpenBSD: compress.c,v 1.25 2006/08/06 01:13:32 stevesk 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>
Damien Miller63b94122006-08-19 00:21:46 +100020#include <zlib.h>
Damien Millerd7834352006-08-05 12:39:39 +100021
Ben Lindstrom226cfa02001-01-22 05:34:40 +000022#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100023#include "buffer.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000024#include "compress.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100025
Ben Lindstrom0f345f52002-03-22 01:51:24 +000026z_stream incoming_stream;
27z_stream outgoing_stream;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +000028static int compress_init_send_called = 0;
29static int compress_init_recv_called = 0;
Ben Lindstromce398b22002-03-22 01:17:52 +000030static int inflate_failed = 0;
31static int deflate_failed = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100032
Damien Miller5428f641999-11-25 11:54:57 +110033/*
34 * Initializes compression; level is compression level from 1 to 9
35 * (as in gzip).
36 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100037
Damien Miller4af51302000-04-16 11:18:38 +100038void
Ben Lindstromfb50cdf2001-04-05 23:20:46 +000039buffer_compress_init_send(int level)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100040{
Ben Lindstromfb50cdf2001-04-05 23:20:46 +000041 if (compress_init_send_called == 1)
Ben Lindstrom96f8d142001-10-03 17:07:47 +000042 deflateEnd(&outgoing_stream);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +000043 compress_init_send_called = 1;
Damien Miller95def091999-11-25 00:26:21 +110044 debug("Enabling compression at level %d.", level);
45 if (level < 1 || level > 9)
46 fatal("Bad compression level %d.", level);
Damien Miller95def091999-11-25 00:26:21 +110047 deflateInit(&outgoing_stream, level);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100048}
Ben Lindstromfb50cdf2001-04-05 23:20:46 +000049void
50buffer_compress_init_recv(void)
51{
52 if (compress_init_recv_called == 1)
53 inflateEnd(&incoming_stream);
54 compress_init_recv_called = 1;
55 inflateInit(&incoming_stream);
56}
Damien Millerd4a8b7e1999-10-27 13:42:43 +100057
58/* Frees any data structures allocated for compression. */
59
Damien Miller4af51302000-04-16 11:18:38 +100060void
Kevin Stevese7652402000-12-28 22:16:00 +000061buffer_compress_uninit(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062{
Damien Miller8f341f82004-01-21 11:00:46 +110063 debug("compress outgoing: raw data %llu, compressed %llu, factor %.2f",
Damien Millerf84fed62004-01-21 11:01:23 +110064 (unsigned long long)outgoing_stream.total_in,
65 (unsigned long long)outgoing_stream.total_out,
Damien Miller9f0f5c62001-12-21 14:45:46 +110066 outgoing_stream.total_in == 0 ? 0.0 :
67 (double) outgoing_stream.total_out / outgoing_stream.total_in);
Damien Miller8f341f82004-01-21 11:00:46 +110068 debug("compress incoming: raw data %llu, compressed %llu, factor %.2f",
Damien Millerf84fed62004-01-21 11:01:23 +110069 (unsigned long long)incoming_stream.total_out,
70 (unsigned long long)incoming_stream.total_in,
Damien Miller9f0f5c62001-12-21 14:45:46 +110071 incoming_stream.total_out == 0 ? 0.0 :
72 (double) incoming_stream.total_in / incoming_stream.total_out);
Ben Lindstromce398b22002-03-22 01:17:52 +000073 if (compress_init_recv_called == 1 && inflate_failed == 0)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +000074 inflateEnd(&incoming_stream);
Ben Lindstromce398b22002-03-22 01:17:52 +000075 if (compress_init_send_called == 1 && deflate_failed == 0)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +000076 deflateEnd(&outgoing_stream);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100077}
78
Damien Miller5428f641999-11-25 11:54:57 +110079/*
80 * Compresses the contents of input_buffer into output_buffer. All packets
81 * compressed using this function will form a single compressed data stream;
82 * however, data will be flushed at the end of every call so that each
83 * output_buffer can be decompressed independently (but in the appropriate
84 * order since they together form a single compression stream) by the
85 * receiver. This appends the compressed data to the output buffer.
86 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087
Damien Miller4af51302000-04-16 11:18:38 +100088void
Damien Miller95def091999-11-25 00:26:21 +110089buffer_compress(Buffer * input_buffer, Buffer * output_buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090{
Damien Miller708d21c2002-01-22 23:18:15 +110091 u_char buf[4096];
Damien Miller95def091999-11-25 00:26:21 +110092 int status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093
Damien Miller95def091999-11-25 00:26:21 +110094 /* This case is not handled below. */
95 if (buffer_len(input_buffer) == 0)
96 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100097
Damien Miller95def091999-11-25 00:26:21 +110098 /* Input is the contents of the input buffer. */
Damien Miller708d21c2002-01-22 23:18:15 +110099 outgoing_stream.next_in = buffer_ptr(input_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100100 outgoing_stream.avail_in = buffer_len(input_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000101
Damien Miller95def091999-11-25 00:26:21 +1100102 /* Loop compressing until deflate() returns with avail_out != 0. */
103 do {
104 /* Set up fixed-size output buffer. */
Damien Miller708d21c2002-01-22 23:18:15 +1100105 outgoing_stream.next_out = buf;
Damien Miller95def091999-11-25 00:26:21 +1100106 outgoing_stream.avail_out = sizeof(buf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107
Damien Miller95def091999-11-25 00:26:21 +1100108 /* Compress as much data into the buffer as possible. */
109 status = deflate(&outgoing_stream, Z_PARTIAL_FLUSH);
110 switch (status) {
111 case Z_OK:
112 /* Append compressed data to output_buffer. */
113 buffer_append(output_buffer, buf,
Damien Millerb38eff82000-04-01 11:09:21 +1000114 sizeof(buf) - outgoing_stream.avail_out);
Damien Miller95def091999-11-25 00:26:21 +1100115 break;
Damien Miller95def091999-11-25 00:26:21 +1100116 default:
Ben Lindstromce398b22002-03-22 01:17:52 +0000117 deflate_failed = 1;
Damien Miller95def091999-11-25 00:26:21 +1100118 fatal("buffer_compress: deflate returned %d", status);
119 /* NOTREACHED */
120 }
Damien Millerb38eff82000-04-01 11:09:21 +1000121 } while (outgoing_stream.avail_out == 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122}
123
Damien Miller5428f641999-11-25 11:54:57 +1100124/*
125 * Uncompresses the contents of input_buffer into output_buffer. All packets
126 * uncompressed using this function will form a single compressed data
127 * stream; however, data will be flushed at the end of every call so that
128 * each output_buffer. This must be called for the same size units that the
129 * buffer_compress was called, and in the same order that buffers compressed
130 * with that. This appends the uncompressed data to the output buffer.
131 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000132
Damien Miller4af51302000-04-16 11:18:38 +1000133void
Damien Miller95def091999-11-25 00:26:21 +1100134buffer_uncompress(Buffer * input_buffer, Buffer * output_buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000135{
Damien Miller708d21c2002-01-22 23:18:15 +1100136 u_char buf[4096];
Damien Miller95def091999-11-25 00:26:21 +1100137 int status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000138
Damien Miller708d21c2002-01-22 23:18:15 +1100139 incoming_stream.next_in = buffer_ptr(input_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100140 incoming_stream.avail_in = buffer_len(input_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141
Damien Miller95def091999-11-25 00:26:21 +1100142 for (;;) {
Damien Millerb38eff82000-04-01 11:09:21 +1000143 /* Set up fixed-size output buffer. */
Damien Miller708d21c2002-01-22 23:18:15 +1100144 incoming_stream.next_out = buf;
Damien Millerb38eff82000-04-01 11:09:21 +1000145 incoming_stream.avail_out = sizeof(buf);
146
Damien Miller95def091999-11-25 00:26:21 +1100147 status = inflate(&incoming_stream, Z_PARTIAL_FLUSH);
148 switch (status) {
149 case Z_OK:
150 buffer_append(output_buffer, buf,
Damien Millerb38eff82000-04-01 11:09:21 +1000151 sizeof(buf) - incoming_stream.avail_out);
Damien Miller95def091999-11-25 00:26:21 +1100152 break;
Damien Miller95def091999-11-25 00:26:21 +1100153 case Z_BUF_ERROR:
Damien Miller5428f641999-11-25 11:54:57 +1100154 /*
155 * Comments in zlib.h say that we should keep calling
156 * inflate() until we get an error. This appears to
157 * be the error that we get.
158 */
Damien Miller95def091999-11-25 00:26:21 +1100159 return;
Damien Miller95def091999-11-25 00:26:21 +1100160 default:
Ben Lindstromce398b22002-03-22 01:17:52 +0000161 inflate_failed = 1;
Damien Miller95def091999-11-25 00:26:21 +1100162 fatal("buffer_uncompress: inflate returned %d", status);
Damien Millerb38eff82000-04-01 11:09:21 +1000163 /* NOTREACHED */
Damien Miller95def091999-11-25 00:26:21 +1100164 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000165 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000166}