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" |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 15 | RCSID("$OpenBSD: compress.c,v 1.13 2001/02/08 19:30:51 itojun 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 | |
| 22 | static z_stream incoming_stream; |
| 23 | static z_stream outgoing_stream; |
| 24 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 25 | /* |
| 26 | * Initializes compression; level is compression level from 1 to 9 |
| 27 | * (as in gzip). |
| 28 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 29 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 30 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 31 | buffer_compress_init(int level) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 32 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 33 | debug("Enabling compression at level %d.", level); |
| 34 | if (level < 1 || level > 9) |
| 35 | fatal("Bad compression level %d.", level); |
| 36 | inflateInit(&incoming_stream); |
| 37 | deflateInit(&outgoing_stream, level); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | /* Frees any data structures allocated for compression. */ |
| 41 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 42 | void |
Kevin Steves | e765240 | 2000-12-28 22:16:00 +0000 | [diff] [blame] | 43 | buffer_compress_uninit(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 44 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 45 | debug("compress outgoing: raw data %lu, compressed %lu, factor %.2f", |
| 46 | outgoing_stream.total_in, outgoing_stream.total_out, |
| 47 | outgoing_stream.total_in == 0 ? 0.0 : |
| 48 | (double) outgoing_stream.total_out / outgoing_stream.total_in); |
| 49 | debug("compress incoming: raw data %lu, compressed %lu, factor %.2f", |
| 50 | incoming_stream.total_out, incoming_stream.total_in, |
| 51 | incoming_stream.total_out == 0 ? 0.0 : |
| 52 | (double) incoming_stream.total_in / incoming_stream.total_out); |
| 53 | inflateEnd(&incoming_stream); |
| 54 | deflateEnd(&outgoing_stream); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 55 | } |
| 56 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 57 | /* |
| 58 | * Compresses the contents of input_buffer into output_buffer. All packets |
| 59 | * compressed using this function will form a single compressed data stream; |
| 60 | * however, data will be flushed at the end of every call so that each |
| 61 | * output_buffer can be decompressed independently (but in the appropriate |
| 62 | * order since they together form a single compression stream) by the |
| 63 | * receiver. This appends the compressed data to the output buffer. |
| 64 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 65 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 66 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 67 | buffer_compress(Buffer * input_buffer, Buffer * output_buffer) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 68 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 69 | char buf[4096]; |
| 70 | int status; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 71 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 72 | /* This case is not handled below. */ |
| 73 | if (buffer_len(input_buffer) == 0) |
| 74 | return; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 75 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 76 | /* Input is the contents of the input buffer. */ |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 77 | outgoing_stream.next_in = (u_char *) buffer_ptr(input_buffer); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 78 | outgoing_stream.avail_in = buffer_len(input_buffer); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 79 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 80 | /* Loop compressing until deflate() returns with avail_out != 0. */ |
| 81 | do { |
| 82 | /* Set up fixed-size output buffer. */ |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 83 | outgoing_stream.next_out = (u_char *)buf; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 84 | outgoing_stream.avail_out = sizeof(buf); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 85 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 86 | /* Compress as much data into the buffer as possible. */ |
| 87 | status = deflate(&outgoing_stream, Z_PARTIAL_FLUSH); |
| 88 | switch (status) { |
| 89 | case Z_OK: |
| 90 | /* Append compressed data to output_buffer. */ |
| 91 | buffer_append(output_buffer, buf, |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 92 | sizeof(buf) - outgoing_stream.avail_out); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 93 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 94 | default: |
| 95 | fatal("buffer_compress: deflate returned %d", status); |
| 96 | /* NOTREACHED */ |
| 97 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 98 | } while (outgoing_stream.avail_out == 0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 99 | } |
| 100 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 101 | /* |
| 102 | * Uncompresses the contents of input_buffer into output_buffer. All packets |
| 103 | * uncompressed using this function will form a single compressed data |
| 104 | * stream; however, data will be flushed at the end of every call so that |
| 105 | * each output_buffer. This must be called for the same size units that the |
| 106 | * buffer_compress was called, and in the same order that buffers compressed |
| 107 | * with that. This appends the uncompressed data to the output buffer. |
| 108 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 109 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 110 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 111 | buffer_uncompress(Buffer * input_buffer, Buffer * output_buffer) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 112 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 113 | char buf[4096]; |
| 114 | int status; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 115 | |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 116 | incoming_stream.next_in = (u_char *) buffer_ptr(input_buffer); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 117 | incoming_stream.avail_in = buffer_len(input_buffer); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 118 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 119 | for (;;) { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 120 | /* Set up fixed-size output buffer. */ |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 121 | incoming_stream.next_out = (u_char *) buf; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 122 | incoming_stream.avail_out = sizeof(buf); |
| 123 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 124 | status = inflate(&incoming_stream, Z_PARTIAL_FLUSH); |
| 125 | switch (status) { |
| 126 | case Z_OK: |
| 127 | buffer_append(output_buffer, buf, |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 128 | sizeof(buf) - incoming_stream.avail_out); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 129 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 130 | case Z_BUF_ERROR: |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 131 | /* |
| 132 | * Comments in zlib.h say that we should keep calling |
| 133 | * inflate() until we get an error. This appears to |
| 134 | * be the error that we get. |
| 135 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 136 | return; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 137 | default: |
| 138 | fatal("buffer_uncompress: inflate returned %d", status); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 139 | /* NOTREACHED */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 140 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 141 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 142 | } |