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