Annotation of src/usr.bin/ssh/compress.c, Revision 1.23
1.23 ! djm 1: /* $OpenBSD$ */
1.1 deraadt 2: /*
1.3 deraadt 3: * Author: Tatu Ylonen <ylo@cs.hut.fi>
4: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5: * All rights reserved
6: * Interface to packet compression for ssh.
1.7 markus 7: *
1.9 deraadt 8: * As far as I am concerned, the code I have written for this software
9: * can be used freely for any purpose. Any derived versions of this
10: * software must be clearly marked as such, and if the derived work is
11: * incompatible with the protocol description in the RFC file, it must be
12: * called by a name other than "ssh" or "Secure Shell".
1.3 deraadt 13: */
1.1 deraadt 14:
15: #include "includes.h"
16:
1.12 markus 17: #include "log.h"
1.1 deraadt 18: #include "buffer.h"
19: #include "zlib.h"
1.13 itojun 20: #include "compress.h"
1.1 deraadt 21:
1.19 provos 22: z_stream incoming_stream;
23: z_stream outgoing_stream;
1.14 markus 24: static int compress_init_send_called = 0;
25: static int compress_init_recv_called = 0;
1.18 markus 26: static int inflate_failed = 0;
27: static int deflate_failed = 0;
1.1 deraadt 28:
1.4 markus 29: /*
30: * Initializes compression; level is compression level from 1 to 9
31: * (as in gzip).
32: */
1.1 deraadt 33:
1.7 markus 34: void
1.14 markus 35: buffer_compress_init_send(int level)
1.1 deraadt 36: {
1.14 markus 37: if (compress_init_send_called == 1)
1.15 markus 38: deflateEnd(&outgoing_stream);
1.14 markus 39: compress_init_send_called = 1;
1.2 markus 40: debug("Enabling compression at level %d.", level);
41: if (level < 1 || level > 9)
42: fatal("Bad compression level %d.", level);
1.14 markus 43: deflateInit(&outgoing_stream, level);
44: }
45: void
46: buffer_compress_init_recv(void)
47: {
48: if (compress_init_recv_called == 1)
49: inflateEnd(&incoming_stream);
50: compress_init_recv_called = 1;
1.2 markus 51: inflateInit(&incoming_stream);
1.1 deraadt 52: }
53:
54: /* Frees any data structures allocated for compression. */
55:
1.7 markus 56: void
1.11 markus 57: buffer_compress_uninit(void)
1.1 deraadt 58: {
1.20 markus 59: debug("compress outgoing: raw data %llu, compressed %llu, factor %.2f",
1.21 markus 60: (unsigned long long)outgoing_stream.total_in,
61: (unsigned long long)outgoing_stream.total_out,
1.16 deraadt 62: outgoing_stream.total_in == 0 ? 0.0 :
63: (double) outgoing_stream.total_out / outgoing_stream.total_in);
1.20 markus 64: debug("compress incoming: raw data %llu, compressed %llu, factor %.2f",
1.21 markus 65: (unsigned long long)incoming_stream.total_out,
66: (unsigned long long)incoming_stream.total_in,
1.16 deraadt 67: incoming_stream.total_out == 0 ? 0.0 :
68: (double) incoming_stream.total_in / incoming_stream.total_out);
1.18 markus 69: if (compress_init_recv_called == 1 && inflate_failed == 0)
1.14 markus 70: inflateEnd(&incoming_stream);
1.18 markus 71: if (compress_init_send_called == 1 && deflate_failed == 0)
1.14 markus 72: deflateEnd(&outgoing_stream);
1.1 deraadt 73: }
74:
1.4 markus 75: /*
76: * Compresses the contents of input_buffer into output_buffer. All packets
77: * compressed using this function will form a single compressed data stream;
78: * however, data will be flushed at the end of every call so that each
79: * output_buffer can be decompressed independently (but in the appropriate
80: * order since they together form a single compression stream) by the
81: * receiver. This appends the compressed data to the output buffer.
82: */
1.1 deraadt 83:
1.7 markus 84: void
1.2 markus 85: buffer_compress(Buffer * input_buffer, Buffer * output_buffer)
1.1 deraadt 86: {
1.17 stevesk 87: u_char buf[4096];
1.2 markus 88: int status;
1.1 deraadt 89:
1.2 markus 90: /* This case is not handled below. */
91: if (buffer_len(input_buffer) == 0)
92: return;
93:
94: /* Input is the contents of the input buffer. */
1.17 stevesk 95: outgoing_stream.next_in = buffer_ptr(input_buffer);
1.2 markus 96: outgoing_stream.avail_in = buffer_len(input_buffer);
97:
98: /* Loop compressing until deflate() returns with avail_out != 0. */
99: do {
100: /* Set up fixed-size output buffer. */
1.17 stevesk 101: outgoing_stream.next_out = buf;
1.2 markus 102: outgoing_stream.avail_out = sizeof(buf);
103:
104: /* Compress as much data into the buffer as possible. */
105: status = deflate(&outgoing_stream, Z_PARTIAL_FLUSH);
106: switch (status) {
107: case Z_OK:
108: /* Append compressed data to output_buffer. */
109: buffer_append(output_buffer, buf,
1.6 markus 110: sizeof(buf) - outgoing_stream.avail_out);
1.2 markus 111: break;
112: default:
1.18 markus 113: deflate_failed = 1;
1.2 markus 114: fatal("buffer_compress: deflate returned %d", status);
115: /* NOTREACHED */
116: }
1.6 markus 117: } while (outgoing_stream.avail_out == 0);
1.1 deraadt 118: }
119:
1.4 markus 120: /*
121: * Uncompresses the contents of input_buffer into output_buffer. All packets
122: * uncompressed using this function will form a single compressed data
123: * stream; however, data will be flushed at the end of every call so that
124: * each output_buffer. This must be called for the same size units that the
125: * buffer_compress was called, and in the same order that buffers compressed
126: * with that. This appends the uncompressed data to the output buffer.
127: */
1.1 deraadt 128:
1.7 markus 129: void
1.2 markus 130: buffer_uncompress(Buffer * input_buffer, Buffer * output_buffer)
1.1 deraadt 131: {
1.17 stevesk 132: u_char buf[4096];
1.2 markus 133: int status;
1.1 deraadt 134:
1.17 stevesk 135: incoming_stream.next_in = buffer_ptr(input_buffer);
1.2 markus 136: incoming_stream.avail_in = buffer_len(input_buffer);
1.1 deraadt 137:
1.6 markus 138: for (;;) {
139: /* Set up fixed-size output buffer. */
1.17 stevesk 140: incoming_stream.next_out = buf;
1.6 markus 141: incoming_stream.avail_out = sizeof(buf);
1.1 deraadt 142:
1.2 markus 143: status = inflate(&incoming_stream, Z_PARTIAL_FLUSH);
144: switch (status) {
145: case Z_OK:
146: buffer_append(output_buffer, buf,
1.6 markus 147: sizeof(buf) - incoming_stream.avail_out);
1.2 markus 148: break;
149: case Z_BUF_ERROR:
1.4 markus 150: /*
151: * Comments in zlib.h say that we should keep calling
152: * inflate() until we get an error. This appears to
153: * be the error that we get.
154: */
1.2 markus 155: return;
156: default:
1.18 markus 157: inflate_failed = 1;
1.2 markus 158: fatal("buffer_uncompress: inflate returned %d", status);
1.6 markus 159: /* NOTREACHED */
1.2 markus 160: }
1.1 deraadt 161: }
162: }