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