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