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