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