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