]>
Commit | Line | Data |
---|---|---|
78372b6e | 1 | /* $OpenBSD: compress.c,v 1.25 2006/08/06 01:13:32 stevesk Exp $ */ |
8efc0c15 | 2 | /* |
5260325f | 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
5260325f | 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
5 | * All rights reserved | |
5260325f | 6 | * Interface to packet compression for ssh. |
6ae2364d | 7 | * |
bcbf86ec | 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". | |
5260325f | 13 | */ |
8efc0c15 | 14 | |
15 | #include "includes.h" | |
8efc0c15 | 16 | |
31652869 | 17 | #include <sys/types.h> |
18 | ||
19 | #include <stdarg.h> | |
78372b6e | 20 | #include <zlib.h> |
31652869 | 21 | |
42f11eb2 | 22 | #include "log.h" |
8efc0c15 | 23 | #include "buffer.h" |
5ca51e19 | 24 | #include "compress.h" |
8efc0c15 | 25 | |
3d6fc2f8 | 26 | z_stream incoming_stream; |
27 | z_stream outgoing_stream; | |
6ba22c93 | 28 | static int compress_init_send_called = 0; |
29 | static int compress_init_recv_called = 0; | |
bcb68a8f | 30 | static int inflate_failed = 0; |
31 | static int deflate_failed = 0; | |
8efc0c15 | 32 | |
aa3378df | 33 | /* |
34 | * Initializes compression; level is compression level from 1 to 9 | |
35 | * (as in gzip). | |
36 | */ | |
8efc0c15 | 37 | |
6ae2364d | 38 | void |
6ba22c93 | 39 | buffer_compress_init_send(int level) |
8efc0c15 | 40 | { |
6ba22c93 | 41 | if (compress_init_send_called == 1) |
c71a16ee | 42 | deflateEnd(&outgoing_stream); |
6ba22c93 | 43 | compress_init_send_called = 1; |
5260325f | 44 | debug("Enabling compression at level %d.", level); |
45 | if (level < 1 || level > 9) | |
46 | fatal("Bad compression level %d.", level); | |
5260325f | 47 | deflateInit(&outgoing_stream, level); |
8efc0c15 | 48 | } |
6ba22c93 | 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 | } | |
8efc0c15 | 57 | |
58 | /* Frees any data structures allocated for compression. */ | |
59 | ||
6ae2364d | 60 | void |
c52c7082 | 61 | buffer_compress_uninit(void) |
8efc0c15 | 62 | { |
39dfceeb | 63 | debug("compress outgoing: raw data %llu, compressed %llu, factor %.2f", |
33623c65 | 64 | (unsigned long long)outgoing_stream.total_in, |
65 | (unsigned long long)outgoing_stream.total_out, | |
184eed6a | 66 | outgoing_stream.total_in == 0 ? 0.0 : |
67 | (double) outgoing_stream.total_out / outgoing_stream.total_in); | |
39dfceeb | 68 | debug("compress incoming: raw data %llu, compressed %llu, factor %.2f", |
33623c65 | 69 | (unsigned long long)incoming_stream.total_out, |
70 | (unsigned long long)incoming_stream.total_in, | |
184eed6a | 71 | incoming_stream.total_out == 0 ? 0.0 : |
72 | (double) incoming_stream.total_in / incoming_stream.total_out); | |
bcb68a8f | 73 | if (compress_init_recv_called == 1 && inflate_failed == 0) |
6ba22c93 | 74 | inflateEnd(&incoming_stream); |
bcb68a8f | 75 | if (compress_init_send_called == 1 && deflate_failed == 0) |
6ba22c93 | 76 | deflateEnd(&outgoing_stream); |
8efc0c15 | 77 | } |
78 | ||
aa3378df | 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 | */ | |
8efc0c15 | 87 | |
6ae2364d | 88 | void |
5260325f | 89 | buffer_compress(Buffer * input_buffer, Buffer * output_buffer) |
8efc0c15 | 90 | { |
20905a8e | 91 | u_char buf[4096]; |
5260325f | 92 | int status; |
93 | ||
94 | /* This case is not handled below. */ | |
95 | if (buffer_len(input_buffer) == 0) | |
96 | return; | |
97 | ||
98 | /* Input is the contents of the input buffer. */ | |
20905a8e | 99 | outgoing_stream.next_in = buffer_ptr(input_buffer); |
5260325f | 100 | outgoing_stream.avail_in = buffer_len(input_buffer); |
101 | ||
102 | /* Loop compressing until deflate() returns with avail_out != 0. */ | |
103 | do { | |
104 | /* Set up fixed-size output buffer. */ | |
20905a8e | 105 | outgoing_stream.next_out = buf; |
5260325f | 106 | outgoing_stream.avail_out = sizeof(buf); |
107 | ||
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, | |
7368a6c8 | 114 | sizeof(buf) - outgoing_stream.avail_out); |
5260325f | 115 | break; |
5260325f | 116 | default: |
bcb68a8f | 117 | deflate_failed = 1; |
5260325f | 118 | fatal("buffer_compress: deflate returned %d", status); |
119 | /* NOTREACHED */ | |
120 | } | |
7368a6c8 | 121 | } while (outgoing_stream.avail_out == 0); |
8efc0c15 | 122 | } |
123 | ||
aa3378df | 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 | */ | |
8efc0c15 | 132 | |
6ae2364d | 133 | void |
5260325f | 134 | buffer_uncompress(Buffer * input_buffer, Buffer * output_buffer) |
8efc0c15 | 135 | { |
20905a8e | 136 | u_char buf[4096]; |
5260325f | 137 | int status; |
138 | ||
20905a8e | 139 | incoming_stream.next_in = buffer_ptr(input_buffer); |
5260325f | 140 | incoming_stream.avail_in = buffer_len(input_buffer); |
141 | ||
5260325f | 142 | for (;;) { |
7368a6c8 | 143 | /* Set up fixed-size output buffer. */ |
20905a8e | 144 | incoming_stream.next_out = buf; |
7368a6c8 | 145 | incoming_stream.avail_out = sizeof(buf); |
146 | ||
5260325f | 147 | status = inflate(&incoming_stream, Z_PARTIAL_FLUSH); |
148 | switch (status) { | |
149 | case Z_OK: | |
150 | buffer_append(output_buffer, buf, | |
7368a6c8 | 151 | sizeof(buf) - incoming_stream.avail_out); |
5260325f | 152 | break; |
5260325f | 153 | case Z_BUF_ERROR: |
aa3378df | 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 | */ | |
5260325f | 159 | return; |
5260325f | 160 | default: |
bcb68a8f | 161 | inflate_failed = 1; |
5260325f | 162 | fatal("buffer_uncompress: inflate returned %d", status); |
7368a6c8 | 163 | /* NOTREACHED */ |
5260325f | 164 | } |
8efc0c15 | 165 | } |
8efc0c15 | 166 | } |