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