]>
Commit | Line | Data |
---|---|---|
8efc0c15 | 1 | /* |
5260325f | 2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
5260325f | 3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
4 | * All rights reserved | |
5260325f | 5 | * Interface to packet compression for ssh. |
6ae2364d | 6 | * |
bcbf86ec | 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". | |
5260325f | 12 | */ |
8efc0c15 | 13 | |
14 | #include "includes.h" | |
8efc0c15 | 15 | |
42f11eb2 | 16 | #include "log.h" |
8efc0c15 | 17 | #include "buffer.h" |
18 | #include "zlib.h" | |
5ca51e19 | 19 | #include "compress.h" |
8efc0c15 | 20 | |
3d6fc2f8 | 21 | z_stream incoming_stream; |
22 | z_stream outgoing_stream; | |
6ba22c93 | 23 | static int compress_init_send_called = 0; |
24 | static int compress_init_recv_called = 0; | |
bcb68a8f | 25 | static int inflate_failed = 0; |
26 | static int deflate_failed = 0; | |
8efc0c15 | 27 | |
aa3378df | 28 | /* |
29 | * Initializes compression; level is compression level from 1 to 9 | |
30 | * (as in gzip). | |
31 | */ | |
8efc0c15 | 32 | |
6ae2364d | 33 | void |
6ba22c93 | 34 | buffer_compress_init_send(int level) |
8efc0c15 | 35 | { |
6ba22c93 | 36 | if (compress_init_send_called == 1) |
c71a16ee | 37 | deflateEnd(&outgoing_stream); |
6ba22c93 | 38 | compress_init_send_called = 1; |
5260325f | 39 | debug("Enabling compression at level %d.", level); |
40 | if (level < 1 || level > 9) | |
41 | fatal("Bad compression level %d.", level); | |
5260325f | 42 | deflateInit(&outgoing_stream, level); |
8efc0c15 | 43 | } |
6ba22c93 | 44 | void |
45 | buffer_compress_init_recv(void) | |
46 | { | |
47 | if (compress_init_recv_called == 1) | |
48 | inflateEnd(&incoming_stream); | |
49 | compress_init_recv_called = 1; | |
50 | inflateInit(&incoming_stream); | |
51 | } | |
8efc0c15 | 52 | |
53 | /* Frees any data structures allocated for compression. */ | |
54 | ||
6ae2364d | 55 | void |
c52c7082 | 56 | buffer_compress_uninit(void) |
8efc0c15 | 57 | { |
39dfceeb | 58 | debug("compress outgoing: raw data %llu, compressed %llu, factor %.2f", |
33623c65 | 59 | (unsigned long long)outgoing_stream.total_in, |
60 | (unsigned long long)outgoing_stream.total_out, | |
184eed6a | 61 | outgoing_stream.total_in == 0 ? 0.0 : |
62 | (double) outgoing_stream.total_out / outgoing_stream.total_in); | |
39dfceeb | 63 | debug("compress incoming: raw data %llu, compressed %llu, factor %.2f", |
33623c65 | 64 | (unsigned long long)incoming_stream.total_out, |
65 | (unsigned long long)incoming_stream.total_in, | |
184eed6a | 66 | incoming_stream.total_out == 0 ? 0.0 : |
67 | (double) incoming_stream.total_in / incoming_stream.total_out); | |
bcb68a8f | 68 | if (compress_init_recv_called == 1 && inflate_failed == 0) |
6ba22c93 | 69 | inflateEnd(&incoming_stream); |
bcb68a8f | 70 | if (compress_init_send_called == 1 && deflate_failed == 0) |
6ba22c93 | 71 | deflateEnd(&outgoing_stream); |
8efc0c15 | 72 | } |
73 | ||
aa3378df | 74 | /* |
75 | * Compresses the contents of input_buffer into output_buffer. All packets | |
76 | * compressed using this function will form a single compressed data stream; | |
77 | * however, data will be flushed at the end of every call so that each | |
78 | * output_buffer can be decompressed independently (but in the appropriate | |
79 | * order since they together form a single compression stream) by the | |
80 | * receiver. This appends the compressed data to the output buffer. | |
81 | */ | |
8efc0c15 | 82 | |
6ae2364d | 83 | void |
5260325f | 84 | buffer_compress(Buffer * input_buffer, Buffer * output_buffer) |
8efc0c15 | 85 | { |
20905a8e | 86 | u_char buf[4096]; |
5260325f | 87 | int status; |
88 | ||
89 | /* This case is not handled below. */ | |
90 | if (buffer_len(input_buffer) == 0) | |
91 | return; | |
92 | ||
93 | /* Input is the contents of the input buffer. */ | |
20905a8e | 94 | outgoing_stream.next_in = buffer_ptr(input_buffer); |
5260325f | 95 | outgoing_stream.avail_in = buffer_len(input_buffer); |
96 | ||
97 | /* Loop compressing until deflate() returns with avail_out != 0. */ | |
98 | do { | |
99 | /* Set up fixed-size output buffer. */ | |
20905a8e | 100 | outgoing_stream.next_out = buf; |
5260325f | 101 | outgoing_stream.avail_out = sizeof(buf); |
102 | ||
103 | /* Compress as much data into the buffer as possible. */ | |
104 | status = deflate(&outgoing_stream, Z_PARTIAL_FLUSH); | |
105 | switch (status) { | |
106 | case Z_OK: | |
107 | /* Append compressed data to output_buffer. */ | |
108 | buffer_append(output_buffer, buf, | |
7368a6c8 | 109 | sizeof(buf) - outgoing_stream.avail_out); |
5260325f | 110 | break; |
5260325f | 111 | default: |
bcb68a8f | 112 | deflate_failed = 1; |
5260325f | 113 | fatal("buffer_compress: deflate returned %d", status); |
114 | /* NOTREACHED */ | |
115 | } | |
7368a6c8 | 116 | } while (outgoing_stream.avail_out == 0); |
8efc0c15 | 117 | } |
118 | ||
aa3378df | 119 | /* |
120 | * Uncompresses the contents of input_buffer into output_buffer. All packets | |
121 | * uncompressed using this function will form a single compressed data | |
122 | * stream; however, data will be flushed at the end of every call so that | |
123 | * each output_buffer. This must be called for the same size units that the | |
124 | * buffer_compress was called, and in the same order that buffers compressed | |
125 | * with that. This appends the uncompressed data to the output buffer. | |
126 | */ | |
8efc0c15 | 127 | |
6ae2364d | 128 | void |
5260325f | 129 | buffer_uncompress(Buffer * input_buffer, Buffer * output_buffer) |
8efc0c15 | 130 | { |
20905a8e | 131 | u_char buf[4096]; |
5260325f | 132 | int status; |
133 | ||
20905a8e | 134 | incoming_stream.next_in = buffer_ptr(input_buffer); |
5260325f | 135 | incoming_stream.avail_in = buffer_len(input_buffer); |
136 | ||
5260325f | 137 | for (;;) { |
7368a6c8 | 138 | /* Set up fixed-size output buffer. */ |
20905a8e | 139 | incoming_stream.next_out = buf; |
7368a6c8 | 140 | incoming_stream.avail_out = sizeof(buf); |
141 | ||
5260325f | 142 | status = inflate(&incoming_stream, Z_PARTIAL_FLUSH); |
143 | switch (status) { | |
144 | case Z_OK: | |
145 | buffer_append(output_buffer, buf, | |
7368a6c8 | 146 | sizeof(buf) - incoming_stream.avail_out); |
5260325f | 147 | break; |
5260325f | 148 | case Z_BUF_ERROR: |
aa3378df | 149 | /* |
150 | * Comments in zlib.h say that we should keep calling | |
151 | * inflate() until we get an error. This appears to | |
152 | * be the error that we get. | |
153 | */ | |
5260325f | 154 | return; |
5260325f | 155 | default: |
bcb68a8f | 156 | inflate_failed = 1; |
5260325f | 157 | fatal("buffer_uncompress: inflate returned %d", status); |
7368a6c8 | 158 | /* NOTREACHED */ |
5260325f | 159 | } |
8efc0c15 | 160 | } |
8efc0c15 | 161 | } |