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