blob: 4ec20104eb745cf77f8d953388073a9a36356d3d [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller4af51302000-04-16 11:18:38 +10002 *
Damien Miller95def091999-11-25 00:26:21 +11003 * compress.c
Damien Miller4af51302000-04-16 11:18:38 +10004 *
Damien Miller95def091999-11-25 00:26:21 +11005 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Miller95def091999-11-25 00:26:21 +11007 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 * All rights reserved
Damien Miller4af51302000-04-16 11:18:38 +10009 *
Damien Miller95def091999-11-25 00:26:21 +110010 * Created: Wed Oct 25 22:12:46 1995 ylo
Damien Miller4af51302000-04-16 11:18:38 +100011 *
Damien Miller95def091999-11-25 00:26:21 +110012 * Interface to packet compression for ssh.
Damien Miller4af51302000-04-16 11:18:38 +100013 *
Damien Miller95def091999-11-25 00:26:21 +110014 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100015
16#include "includes.h"
Damien Miller6536c7d2000-06-22 21:32:31 +100017RCSID("$OpenBSD: compress.c,v 1.8 2000/06/20 01:39:40 markus Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100018
19#include "ssh.h"
20#include "buffer.h"
21#include "zlib.h"
22
23static z_stream incoming_stream;
24static z_stream outgoing_stream;
25
Damien Miller5428f641999-11-25 11:54:57 +110026/*
27 * Initializes compression; level is compression level from 1 to 9
28 * (as in gzip).
29 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100030
Damien Miller4af51302000-04-16 11:18:38 +100031void
Damien Miller95def091999-11-25 00:26:21 +110032buffer_compress_init(int level)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100033{
Damien Miller95def091999-11-25 00:26:21 +110034 debug("Enabling compression at level %d.", level);
35 if (level < 1 || level > 9)
36 fatal("Bad compression level %d.", level);
37 inflateInit(&incoming_stream);
38 deflateInit(&outgoing_stream, level);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039}
40
41/* Frees any data structures allocated for compression. */
42
Damien Miller4af51302000-04-16 11:18:38 +100043void
Damien Miller95def091999-11-25 00:26:21 +110044buffer_compress_uninit()
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045{
Damien Miller95def091999-11-25 00:26:21 +110046 debug("compress outgoing: raw data %lu, compressed %lu, factor %.2f",
47 outgoing_stream.total_in, outgoing_stream.total_out,
48 outgoing_stream.total_in == 0 ? 0.0 :
49 (double) outgoing_stream.total_out / outgoing_stream.total_in);
50 debug("compress incoming: raw data %lu, compressed %lu, factor %.2f",
51 incoming_stream.total_out, incoming_stream.total_in,
52 incoming_stream.total_out == 0 ? 0.0 :
53 (double) incoming_stream.total_in / incoming_stream.total_out);
54 inflateEnd(&incoming_stream);
55 deflateEnd(&outgoing_stream);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100056}
57
Damien Miller5428f641999-11-25 11:54:57 +110058/*
59 * Compresses the contents of input_buffer into output_buffer. All packets
60 * compressed using this function will form a single compressed data stream;
61 * however, data will be flushed at the end of every call so that each
62 * output_buffer can be decompressed independently (but in the appropriate
63 * order since they together form a single compression stream) by the
64 * receiver. This appends the compressed data to the output buffer.
65 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066
Damien Miller4af51302000-04-16 11:18:38 +100067void
Damien Miller95def091999-11-25 00:26:21 +110068buffer_compress(Buffer * input_buffer, Buffer * output_buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069{
Damien Miller95def091999-11-25 00:26:21 +110070 char buf[4096];
71 int status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072
Damien Miller95def091999-11-25 00:26:21 +110073 /* This case is not handled below. */
74 if (buffer_len(input_buffer) == 0)
75 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100076
Damien Miller95def091999-11-25 00:26:21 +110077 /* Input is the contents of the input buffer. */
Damien Miller7684ee12000-03-17 23:40:15 +110078 outgoing_stream.next_in = (unsigned char *) buffer_ptr(input_buffer);
Damien Miller95def091999-11-25 00:26:21 +110079 outgoing_stream.avail_in = buffer_len(input_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080
Damien Miller95def091999-11-25 00:26:21 +110081 /* Loop compressing until deflate() returns with avail_out != 0. */
82 do {
83 /* Set up fixed-size output buffer. */
Damien Miller7684ee12000-03-17 23:40:15 +110084 outgoing_stream.next_out = (unsigned char *)buf;
Damien Miller95def091999-11-25 00:26:21 +110085 outgoing_stream.avail_out = sizeof(buf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086
Damien Miller95def091999-11-25 00:26:21 +110087 /* Compress as much data into the buffer as possible. */
88 status = deflate(&outgoing_stream, Z_PARTIAL_FLUSH);
89 switch (status) {
90 case Z_OK:
91 /* Append compressed data to output_buffer. */
92 buffer_append(output_buffer, buf,
Damien Millerb38eff82000-04-01 11:09:21 +100093 sizeof(buf) - outgoing_stream.avail_out);
Damien Miller95def091999-11-25 00:26:21 +110094 break;
Damien Miller95def091999-11-25 00:26:21 +110095 default:
96 fatal("buffer_compress: deflate returned %d", status);
97 /* NOTREACHED */
98 }
Damien Millerb38eff82000-04-01 11:09:21 +100099 } while (outgoing_stream.avail_out == 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100}
101
Damien Miller5428f641999-11-25 11:54:57 +1100102/*
103 * Uncompresses the contents of input_buffer into output_buffer. All packets
104 * uncompressed using this function will form a single compressed data
105 * stream; however, data will be flushed at the end of every call so that
106 * each output_buffer. This must be called for the same size units that the
107 * buffer_compress was called, and in the same order that buffers compressed
108 * with that. This appends the uncompressed data to the output buffer.
109 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110
Damien Miller4af51302000-04-16 11:18:38 +1000111void
Damien Miller95def091999-11-25 00:26:21 +1100112buffer_uncompress(Buffer * input_buffer, Buffer * output_buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113{
Damien Miller95def091999-11-25 00:26:21 +1100114 char buf[4096];
115 int status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116
Damien Miller7684ee12000-03-17 23:40:15 +1100117 incoming_stream.next_in = (unsigned char *) buffer_ptr(input_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100118 incoming_stream.avail_in = buffer_len(input_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119
Damien Miller95def091999-11-25 00:26:21 +1100120 for (;;) {
Damien Millerb38eff82000-04-01 11:09:21 +1000121 /* Set up fixed-size output buffer. */
122 incoming_stream.next_out = (unsigned char *) buf;
123 incoming_stream.avail_out = sizeof(buf);
124
Damien Miller95def091999-11-25 00:26:21 +1100125 status = inflate(&incoming_stream, Z_PARTIAL_FLUSH);
126 switch (status) {
127 case Z_OK:
128 buffer_append(output_buffer, buf,
Damien Millerb38eff82000-04-01 11:09:21 +1000129 sizeof(buf) - incoming_stream.avail_out);
Damien Miller95def091999-11-25 00:26:21 +1100130 break;
Damien Miller95def091999-11-25 00:26:21 +1100131 case Z_BUF_ERROR:
Damien Miller5428f641999-11-25 11:54:57 +1100132 /*
133 * Comments in zlib.h say that we should keep calling
134 * inflate() until we get an error. This appears to
135 * be the error that we get.
136 */
Damien Miller95def091999-11-25 00:26:21 +1100137 return;
Damien Miller95def091999-11-25 00:26:21 +1100138 default:
139 fatal("buffer_uncompress: inflate returned %d", status);
Damien Millerb38eff82000-04-01 11:09:21 +1000140 /* NOTREACHED */
Damien Miller95def091999-11-25 00:26:21 +1100141 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000142 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143}