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