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