blob: 3badbf452da28c21c6f8f33a29c0b1f6d63b8bd8 [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 Miller708d21c2002-01-22 23:18:15 +110015RCSID("$OpenBSD: compress.c,v 1.17 2001/12/29 21:56:01 stevesk 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
22static z_stream incoming_stream;
23static z_stream outgoing_stream;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +000024static int compress_init_send_called = 0;
25static int compress_init_recv_called = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100026
Damien Miller5428f641999-11-25 11:54:57 +110027/*
28 * Initializes compression; level is compression level from 1 to 9
29 * (as in gzip).
30 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100031
Damien Miller4af51302000-04-16 11:18:38 +100032void
Ben Lindstromfb50cdf2001-04-05 23:20:46 +000033buffer_compress_init_send(int level)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100034{
Ben Lindstromfb50cdf2001-04-05 23:20:46 +000035 if (compress_init_send_called == 1)
Ben Lindstrom96f8d142001-10-03 17:07:47 +000036 deflateEnd(&outgoing_stream);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +000037 compress_init_send_called = 1;
Damien Miller95def091999-11-25 00:26:21 +110038 debug("Enabling compression at level %d.", level);
39 if (level < 1 || level > 9)
40 fatal("Bad compression level %d.", level);
Damien Miller95def091999-11-25 00:26:21 +110041 deflateInit(&outgoing_stream, level);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100042}
Ben Lindstromfb50cdf2001-04-05 23:20:46 +000043void
44buffer_compress_init_recv(void)
45{
46 if (compress_init_recv_called == 1)
47 inflateEnd(&incoming_stream);
48 compress_init_recv_called = 1;
49 inflateInit(&incoming_stream);
50}
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051
52/* Frees any data structures allocated for compression. */
53
Damien Miller4af51302000-04-16 11:18:38 +100054void
Kevin Stevese7652402000-12-28 22:16:00 +000055buffer_compress_uninit(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100056{
Damien Miller95def091999-11-25 00:26:21 +110057 debug("compress outgoing: raw data %lu, compressed %lu, factor %.2f",
Damien Miller9f0f5c62001-12-21 14:45:46 +110058 outgoing_stream.total_in, outgoing_stream.total_out,
59 outgoing_stream.total_in == 0 ? 0.0 :
60 (double) outgoing_stream.total_out / outgoing_stream.total_in);
Damien Miller95def091999-11-25 00:26:21 +110061 debug("compress incoming: raw data %lu, compressed %lu, factor %.2f",
Damien Miller9f0f5c62001-12-21 14:45:46 +110062 incoming_stream.total_out, incoming_stream.total_in,
63 incoming_stream.total_out == 0 ? 0.0 :
64 (double) incoming_stream.total_in / incoming_stream.total_out);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +000065 if (compress_init_recv_called == 1)
66 inflateEnd(&incoming_stream);
67 if (compress_init_send_called == 1)
68 deflateEnd(&outgoing_stream);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069}
70
Damien Miller5428f641999-11-25 11:54:57 +110071/*
72 * Compresses the contents of input_buffer into output_buffer. All packets
73 * compressed using this function will form a single compressed data stream;
74 * however, data will be flushed at the end of every call so that each
75 * output_buffer can be decompressed independently (but in the appropriate
76 * order since they together form a single compression stream) by the
77 * receiver. This appends the compressed data to the output buffer.
78 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079
Damien Miller4af51302000-04-16 11:18:38 +100080void
Damien Miller95def091999-11-25 00:26:21 +110081buffer_compress(Buffer * input_buffer, Buffer * output_buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082{
Damien Miller708d21c2002-01-22 23:18:15 +110083 u_char buf[4096];
Damien Miller95def091999-11-25 00:26:21 +110084 int status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085
Damien Miller95def091999-11-25 00:26:21 +110086 /* This case is not handled below. */
87 if (buffer_len(input_buffer) == 0)
88 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089
Damien Miller95def091999-11-25 00:26:21 +110090 /* Input is the contents of the input buffer. */
Damien Miller708d21c2002-01-22 23:18:15 +110091 outgoing_stream.next_in = buffer_ptr(input_buffer);
Damien Miller95def091999-11-25 00:26:21 +110092 outgoing_stream.avail_in = buffer_len(input_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093
Damien Miller95def091999-11-25 00:26:21 +110094 /* Loop compressing until deflate() returns with avail_out != 0. */
95 do {
96 /* Set up fixed-size output buffer. */
Damien Miller708d21c2002-01-22 23:18:15 +110097 outgoing_stream.next_out = buf;
Damien Miller95def091999-11-25 00:26:21 +110098 outgoing_stream.avail_out = sizeof(buf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100099
Damien Miller95def091999-11-25 00:26:21 +1100100 /* Compress as much data into the buffer as possible. */
101 status = deflate(&outgoing_stream, Z_PARTIAL_FLUSH);
102 switch (status) {
103 case Z_OK:
104 /* Append compressed data to output_buffer. */
105 buffer_append(output_buffer, buf,
Damien Millerb38eff82000-04-01 11:09:21 +1000106 sizeof(buf) - outgoing_stream.avail_out);
Damien Miller95def091999-11-25 00:26:21 +1100107 break;
Damien Miller95def091999-11-25 00:26:21 +1100108 default:
109 fatal("buffer_compress: deflate returned %d", status);
110 /* NOTREACHED */
111 }
Damien Millerb38eff82000-04-01 11:09:21 +1000112 } while (outgoing_stream.avail_out == 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113}
114
Damien Miller5428f641999-11-25 11:54:57 +1100115/*
116 * Uncompresses the contents of input_buffer into output_buffer. All packets
117 * uncompressed using this function will form a single compressed data
118 * stream; however, data will be flushed at the end of every call so that
119 * each output_buffer. This must be called for the same size units that the
120 * buffer_compress was called, and in the same order that buffers compressed
121 * with that. This appends the uncompressed data to the output buffer.
122 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123
Damien Miller4af51302000-04-16 11:18:38 +1000124void
Damien Miller95def091999-11-25 00:26:21 +1100125buffer_uncompress(Buffer * input_buffer, Buffer * output_buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126{
Damien Miller708d21c2002-01-22 23:18:15 +1100127 u_char buf[4096];
Damien Miller95def091999-11-25 00:26:21 +1100128 int status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000129
Damien Miller708d21c2002-01-22 23:18:15 +1100130 incoming_stream.next_in = buffer_ptr(input_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100131 incoming_stream.avail_in = buffer_len(input_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000132
Damien Miller95def091999-11-25 00:26:21 +1100133 for (;;) {
Damien Millerb38eff82000-04-01 11:09:21 +1000134 /* Set up fixed-size output buffer. */
Damien Miller708d21c2002-01-22 23:18:15 +1100135 incoming_stream.next_out = buf;
Damien Millerb38eff82000-04-01 11:09:21 +1000136 incoming_stream.avail_out = sizeof(buf);
137
Damien Miller95def091999-11-25 00:26:21 +1100138 status = inflate(&incoming_stream, Z_PARTIAL_FLUSH);
139 switch (status) {
140 case Z_OK:
141 buffer_append(output_buffer, buf,
Damien Millerb38eff82000-04-01 11:09:21 +1000142 sizeof(buf) - incoming_stream.avail_out);
Damien Miller95def091999-11-25 00:26:21 +1100143 break;
Damien Miller95def091999-11-25 00:26:21 +1100144 case Z_BUF_ERROR:
Damien Miller5428f641999-11-25 11:54:57 +1100145 /*
146 * Comments in zlib.h say that we should keep calling
147 * inflate() until we get an error. This appears to
148 * be the error that we get.
149 */
Damien Miller95def091999-11-25 00:26:21 +1100150 return;
Damien Miller95def091999-11-25 00:26:21 +1100151 default:
152 fatal("buffer_uncompress: inflate returned %d", status);
Damien Millerb38eff82000-04-01 11:09:21 +1000153 /* NOTREACHED */
Damien Miller95def091999-11-25 00:26:21 +1100154 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000155 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000156}