Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 4 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 5 | * Interface to packet compression for ssh. |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 6 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 7 | * 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 Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 12 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 13 | |
| 14 | #include "includes.h" |
Damien Miller | f84fed6 | 2004-01-21 11:01:23 +1100 | [diff] [blame] | 15 | RCSID("$OpenBSD: compress.c,v 1.21 2004/01/13 19:45:15 markus Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 16 | |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 17 | #include "log.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 18 | #include "buffer.h" |
| 19 | #include "zlib.h" |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 20 | #include "compress.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 21 | |
Ben Lindstrom | 0f345f5 | 2002-03-22 01:51:24 +0000 | [diff] [blame] | 22 | z_stream incoming_stream; |
| 23 | z_stream outgoing_stream; |
Ben Lindstrom | fb50cdf | 2001-04-05 23:20:46 +0000 | [diff] [blame] | 24 | static int compress_init_send_called = 0; |
| 25 | static int compress_init_recv_called = 0; |
Ben Lindstrom | ce398b2 | 2002-03-22 01:17:52 +0000 | [diff] [blame] | 26 | static int inflate_failed = 0; |
| 27 | static int deflate_failed = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 28 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 29 | /* |
| 30 | * Initializes compression; level is compression level from 1 to 9 |
| 31 | * (as in gzip). |
| 32 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 33 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 34 | void |
Ben Lindstrom | fb50cdf | 2001-04-05 23:20:46 +0000 | [diff] [blame] | 35 | buffer_compress_init_send(int level) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 36 | { |
Ben Lindstrom | fb50cdf | 2001-04-05 23:20:46 +0000 | [diff] [blame] | 37 | if (compress_init_send_called == 1) |
Ben Lindstrom | 96f8d14 | 2001-10-03 17:07:47 +0000 | [diff] [blame] | 38 | deflateEnd(&outgoing_stream); |
Ben Lindstrom | fb50cdf | 2001-04-05 23:20:46 +0000 | [diff] [blame] | 39 | compress_init_send_called = 1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 40 | debug("Enabling compression at level %d.", level); |
| 41 | if (level < 1 || level > 9) |
| 42 | fatal("Bad compression level %d.", level); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 43 | deflateInit(&outgoing_stream, level); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 44 | } |
Ben Lindstrom | fb50cdf | 2001-04-05 23:20:46 +0000 | [diff] [blame] | 45 | void |
| 46 | buffer_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 Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 53 | |
| 54 | /* Frees any data structures allocated for compression. */ |
| 55 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 56 | void |
Kevin Steves | e765240 | 2000-12-28 22:16:00 +0000 | [diff] [blame] | 57 | buffer_compress_uninit(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 58 | { |
Damien Miller | 8f341f8 | 2004-01-21 11:00:46 +1100 | [diff] [blame] | 59 | debug("compress outgoing: raw data %llu, compressed %llu, factor %.2f", |
Damien Miller | f84fed6 | 2004-01-21 11:01:23 +1100 | [diff] [blame] | 60 | (unsigned long long)outgoing_stream.total_in, |
| 61 | (unsigned long long)outgoing_stream.total_out, |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 62 | outgoing_stream.total_in == 0 ? 0.0 : |
| 63 | (double) outgoing_stream.total_out / outgoing_stream.total_in); |
Damien Miller | 8f341f8 | 2004-01-21 11:00:46 +1100 | [diff] [blame] | 64 | debug("compress incoming: raw data %llu, compressed %llu, factor %.2f", |
Damien Miller | f84fed6 | 2004-01-21 11:01:23 +1100 | [diff] [blame] | 65 | (unsigned long long)incoming_stream.total_out, |
| 66 | (unsigned long long)incoming_stream.total_in, |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 67 | incoming_stream.total_out == 0 ? 0.0 : |
| 68 | (double) incoming_stream.total_in / incoming_stream.total_out); |
Ben Lindstrom | ce398b2 | 2002-03-22 01:17:52 +0000 | [diff] [blame] | 69 | if (compress_init_recv_called == 1 && inflate_failed == 0) |
Ben Lindstrom | fb50cdf | 2001-04-05 23:20:46 +0000 | [diff] [blame] | 70 | inflateEnd(&incoming_stream); |
Ben Lindstrom | ce398b2 | 2002-03-22 01:17:52 +0000 | [diff] [blame] | 71 | if (compress_init_send_called == 1 && deflate_failed == 0) |
Ben Lindstrom | fb50cdf | 2001-04-05 23:20:46 +0000 | [diff] [blame] | 72 | deflateEnd(&outgoing_stream); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 73 | } |
| 74 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 75 | /* |
| 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 Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 83 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 84 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 85 | buffer_compress(Buffer * input_buffer, Buffer * output_buffer) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 86 | { |
Damien Miller | 708d21c | 2002-01-22 23:18:15 +1100 | [diff] [blame] | 87 | u_char buf[4096]; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 88 | int status; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 89 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 90 | /* This case is not handled below. */ |
| 91 | if (buffer_len(input_buffer) == 0) |
| 92 | return; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 93 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 94 | /* Input is the contents of the input buffer. */ |
Damien Miller | 708d21c | 2002-01-22 23:18:15 +1100 | [diff] [blame] | 95 | outgoing_stream.next_in = buffer_ptr(input_buffer); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 96 | outgoing_stream.avail_in = buffer_len(input_buffer); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 97 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 98 | /* Loop compressing until deflate() returns with avail_out != 0. */ |
| 99 | do { |
| 100 | /* Set up fixed-size output buffer. */ |
Damien Miller | 708d21c | 2002-01-22 23:18:15 +1100 | [diff] [blame] | 101 | outgoing_stream.next_out = buf; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 102 | outgoing_stream.avail_out = sizeof(buf); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 103 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 104 | /* 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 Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 110 | sizeof(buf) - outgoing_stream.avail_out); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 111 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 112 | default: |
Ben Lindstrom | ce398b2 | 2002-03-22 01:17:52 +0000 | [diff] [blame] | 113 | deflate_failed = 1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 114 | fatal("buffer_compress: deflate returned %d", status); |
| 115 | /* NOTREACHED */ |
| 116 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 117 | } while (outgoing_stream.avail_out == 0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 118 | } |
| 119 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 120 | /* |
| 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 Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 128 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 129 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 130 | buffer_uncompress(Buffer * input_buffer, Buffer * output_buffer) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 131 | { |
Damien Miller | 708d21c | 2002-01-22 23:18:15 +1100 | [diff] [blame] | 132 | u_char buf[4096]; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 133 | int status; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 134 | |
Damien Miller | 708d21c | 2002-01-22 23:18:15 +1100 | [diff] [blame] | 135 | incoming_stream.next_in = buffer_ptr(input_buffer); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 136 | incoming_stream.avail_in = buffer_len(input_buffer); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 137 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 138 | for (;;) { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 139 | /* Set up fixed-size output buffer. */ |
Damien Miller | 708d21c | 2002-01-22 23:18:15 +1100 | [diff] [blame] | 140 | incoming_stream.next_out = buf; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 141 | incoming_stream.avail_out = sizeof(buf); |
| 142 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 143 | status = inflate(&incoming_stream, Z_PARTIAL_FLUSH); |
| 144 | switch (status) { |
| 145 | case Z_OK: |
| 146 | buffer_append(output_buffer, buf, |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 147 | sizeof(buf) - incoming_stream.avail_out); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 148 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 149 | case Z_BUF_ERROR: |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 150 | /* |
| 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 Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 155 | return; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 156 | default: |
Ben Lindstrom | ce398b2 | 2002-03-22 01:17:52 +0000 | [diff] [blame] | 157 | inflate_failed = 1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 158 | fatal("buffer_uncompress: inflate returned %d", status); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 159 | /* NOTREACHED */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 160 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 161 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 162 | } |