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