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