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