blob: 93266ed9f35b1c73771a5b1806b56b19300ab336 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11003 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * Interface to packet compression for ssh.
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Millere4340be2000-09-16 13:29:08 +11007 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110012 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100013
14#include "includes.h"
Damien Millere4340be2000-09-16 13:29:08 +110015RCSID("$OpenBSD: compress.c,v 1.9 2000/09/07 20:27:50 deraadt Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100016
17#include "ssh.h"
18#include "buffer.h"
19#include "zlib.h"
20
21static z_stream incoming_stream;
22static z_stream outgoing_stream;
23
Damien Miller5428f641999-11-25 11:54:57 +110024/*
25 * Initializes compression; level is compression level from 1 to 9
26 * (as in gzip).
27 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100028
Damien Miller4af51302000-04-16 11:18:38 +100029void
Damien Miller95def091999-11-25 00:26:21 +110030buffer_compress_init(int level)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100031{
Damien Miller95def091999-11-25 00:26:21 +110032 debug("Enabling compression at level %d.", level);
33 if (level < 1 || level > 9)
34 fatal("Bad compression level %d.", level);
35 inflateInit(&incoming_stream);
36 deflateInit(&outgoing_stream, level);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100037}
38
39/* Frees any data structures allocated for compression. */
40
Damien Miller4af51302000-04-16 11:18:38 +100041void
Damien Miller95def091999-11-25 00:26:21 +110042buffer_compress_uninit()
Damien Millerd4a8b7e1999-10-27 13:42:43 +100043{
Damien Miller95def091999-11-25 00:26:21 +110044 debug("compress outgoing: raw data %lu, compressed %lu, factor %.2f",
45 outgoing_stream.total_in, outgoing_stream.total_out,
46 outgoing_stream.total_in == 0 ? 0.0 :
47 (double) outgoing_stream.total_out / outgoing_stream.total_in);
48 debug("compress incoming: raw data %lu, compressed %lu, factor %.2f",
49 incoming_stream.total_out, incoming_stream.total_in,
50 incoming_stream.total_out == 0 ? 0.0 :
51 (double) incoming_stream.total_in / incoming_stream.total_out);
52 inflateEnd(&incoming_stream);
53 deflateEnd(&outgoing_stream);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054}
55
Damien Miller5428f641999-11-25 11:54:57 +110056/*
57 * Compresses the contents of input_buffer into output_buffer. All packets
58 * compressed using this function will form a single compressed data stream;
59 * however, data will be flushed at the end of every call so that each
60 * output_buffer can be decompressed independently (but in the appropriate
61 * order since they together form a single compression stream) by the
62 * receiver. This appends the compressed data to the output buffer.
63 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064
Damien Miller4af51302000-04-16 11:18:38 +100065void
Damien Miller95def091999-11-25 00:26:21 +110066buffer_compress(Buffer * input_buffer, Buffer * output_buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067{
Damien Miller95def091999-11-25 00:26:21 +110068 char buf[4096];
69 int status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070
Damien Miller95def091999-11-25 00:26:21 +110071 /* This case is not handled below. */
72 if (buffer_len(input_buffer) == 0)
73 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074
Damien Miller95def091999-11-25 00:26:21 +110075 /* Input is the contents of the input buffer. */
Damien Miller7684ee12000-03-17 23:40:15 +110076 outgoing_stream.next_in = (unsigned char *) buffer_ptr(input_buffer);
Damien Miller95def091999-11-25 00:26:21 +110077 outgoing_stream.avail_in = buffer_len(input_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078
Damien Miller95def091999-11-25 00:26:21 +110079 /* Loop compressing until deflate() returns with avail_out != 0. */
80 do {
81 /* Set up fixed-size output buffer. */
Damien Miller7684ee12000-03-17 23:40:15 +110082 outgoing_stream.next_out = (unsigned char *)buf;
Damien Miller95def091999-11-25 00:26:21 +110083 outgoing_stream.avail_out = sizeof(buf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084
Damien Miller95def091999-11-25 00:26:21 +110085 /* Compress as much data into the buffer as possible. */
86 status = deflate(&outgoing_stream, Z_PARTIAL_FLUSH);
87 switch (status) {
88 case Z_OK:
89 /* Append compressed data to output_buffer. */
90 buffer_append(output_buffer, buf,
Damien Millerb38eff82000-04-01 11:09:21 +100091 sizeof(buf) - outgoing_stream.avail_out);
Damien Miller95def091999-11-25 00:26:21 +110092 break;
Damien Miller95def091999-11-25 00:26:21 +110093 default:
94 fatal("buffer_compress: deflate returned %d", status);
95 /* NOTREACHED */
96 }
Damien Millerb38eff82000-04-01 11:09:21 +100097 } while (outgoing_stream.avail_out == 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098}
99
Damien Miller5428f641999-11-25 11:54:57 +1100100/*
101 * Uncompresses the contents of input_buffer into output_buffer. All packets
102 * uncompressed using this function will form a single compressed data
103 * stream; however, data will be flushed at the end of every call so that
104 * each output_buffer. This must be called for the same size units that the
105 * buffer_compress was called, and in the same order that buffers compressed
106 * with that. This appends the uncompressed data to the output buffer.
107 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108
Damien Miller4af51302000-04-16 11:18:38 +1000109void
Damien Miller95def091999-11-25 00:26:21 +1100110buffer_uncompress(Buffer * input_buffer, Buffer * output_buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111{
Damien Miller95def091999-11-25 00:26:21 +1100112 char buf[4096];
113 int status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114
Damien Miller7684ee12000-03-17 23:40:15 +1100115 incoming_stream.next_in = (unsigned char *) buffer_ptr(input_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100116 incoming_stream.avail_in = buffer_len(input_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117
Damien Miller95def091999-11-25 00:26:21 +1100118 for (;;) {
Damien Millerb38eff82000-04-01 11:09:21 +1000119 /* Set up fixed-size output buffer. */
120 incoming_stream.next_out = (unsigned char *) buf;
121 incoming_stream.avail_out = sizeof(buf);
122
Damien Miller95def091999-11-25 00:26:21 +1100123 status = inflate(&incoming_stream, Z_PARTIAL_FLUSH);
124 switch (status) {
125 case Z_OK:
126 buffer_append(output_buffer, buf,
Damien Millerb38eff82000-04-01 11:09:21 +1000127 sizeof(buf) - incoming_stream.avail_out);
Damien Miller95def091999-11-25 00:26:21 +1100128 break;
Damien Miller95def091999-11-25 00:26:21 +1100129 case Z_BUF_ERROR:
Damien Miller5428f641999-11-25 11:54:57 +1100130 /*
131 * Comments in zlib.h say that we should keep calling
132 * inflate() until we get an error. This appears to
133 * be the error that we get.
134 */
Damien Miller95def091999-11-25 00:26:21 +1100135 return;
Damien Miller95def091999-11-25 00:26:21 +1100136 default:
137 fatal("buffer_uncompress: inflate returned %d", status);
Damien Millerb38eff82000-04-01 11:09:21 +1000138 /* NOTREACHED */
Damien Miller95def091999-11-25 00:26:21 +1100139 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000140 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141}