[BACK]Return to compress.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/compress.c, Revision 1.20

1.1       deraadt     1: /*
1.3       deraadt     2:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
                      3:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      4:  *                    All rights reserved
                      5:  * Interface to packet compression for ssh.
1.7       markus      6:  *
1.9       deraadt     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".
1.3       deraadt    12:  */
1.1       deraadt    13:
                     14: #include "includes.h"
1.20    ! markus     15: RCSID("$OpenBSD: compress.c,v 1.19 2002/03/18 17:31:54 provos Exp $");
1.1       deraadt    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.16      deraadt    60:            outgoing_stream.total_in, outgoing_stream.total_out,
                     61:            outgoing_stream.total_in == 0 ? 0.0 :
                     62:            (double) outgoing_stream.total_out / outgoing_stream.total_in);
1.20    ! markus     63:        debug("compress incoming: raw data %llu, compressed %llu, factor %.2f",
1.16      deraadt    64:            incoming_stream.total_out, incoming_stream.total_in,
                     65:            incoming_stream.total_out == 0 ? 0.0 :
                     66:            (double) incoming_stream.total_in / incoming_stream.total_out);
1.18      markus     67:        if (compress_init_recv_called == 1 && inflate_failed == 0)
1.14      markus     68:                inflateEnd(&incoming_stream);
1.18      markus     69:        if (compress_init_send_called == 1 && deflate_failed == 0)
1.14      markus     70:                deflateEnd(&outgoing_stream);
1.1       deraadt    71: }
                     72:
1.4       markus     73: /*
                     74:  * Compresses the contents of input_buffer into output_buffer.  All packets
                     75:  * compressed using this function will form a single compressed data stream;
                     76:  * however, data will be flushed at the end of every call so that each
                     77:  * output_buffer can be decompressed independently (but in the appropriate
                     78:  * order since they together form a single compression stream) by the
                     79:  * receiver.  This appends the compressed data to the output buffer.
                     80:  */
1.1       deraadt    81:
1.7       markus     82: void
1.2       markus     83: buffer_compress(Buffer * input_buffer, Buffer * output_buffer)
1.1       deraadt    84: {
1.17      stevesk    85:        u_char buf[4096];
1.2       markus     86:        int status;
1.1       deraadt    87:
1.2       markus     88:        /* This case is not handled below. */
                     89:        if (buffer_len(input_buffer) == 0)
                     90:                return;
                     91:
                     92:        /* Input is the contents of the input buffer. */
1.17      stevesk    93:        outgoing_stream.next_in = buffer_ptr(input_buffer);
1.2       markus     94:        outgoing_stream.avail_in = buffer_len(input_buffer);
                     95:
                     96:        /* Loop compressing until deflate() returns with avail_out != 0. */
                     97:        do {
                     98:                /* Set up fixed-size output buffer. */
1.17      stevesk    99:                outgoing_stream.next_out = buf;
1.2       markus    100:                outgoing_stream.avail_out = sizeof(buf);
                    101:
                    102:                /* Compress as much data into the buffer as possible. */
                    103:                status = deflate(&outgoing_stream, Z_PARTIAL_FLUSH);
                    104:                switch (status) {
                    105:                case Z_OK:
                    106:                        /* Append compressed data to output_buffer. */
                    107:                        buffer_append(output_buffer, buf,
1.6       markus    108:                            sizeof(buf) - outgoing_stream.avail_out);
1.2       markus    109:                        break;
                    110:                default:
1.18      markus    111:                        deflate_failed = 1;
1.2       markus    112:                        fatal("buffer_compress: deflate returned %d", status);
                    113:                        /* NOTREACHED */
                    114:                }
1.6       markus    115:        } while (outgoing_stream.avail_out == 0);
1.1       deraadt   116: }
                    117:
1.4       markus    118: /*
                    119:  * Uncompresses the contents of input_buffer into output_buffer.  All packets
                    120:  * uncompressed using this function will form a single compressed data
                    121:  * stream; however, data will be flushed at the end of every call so that
                    122:  * each output_buffer.  This must be called for the same size units that the
                    123:  * buffer_compress was called, and in the same order that buffers compressed
                    124:  * with that.  This appends the uncompressed data to the output buffer.
                    125:  */
1.1       deraadt   126:
1.7       markus    127: void
1.2       markus    128: buffer_uncompress(Buffer * input_buffer, Buffer * output_buffer)
1.1       deraadt   129: {
1.17      stevesk   130:        u_char buf[4096];
1.2       markus    131:        int status;
1.1       deraadt   132:
1.17      stevesk   133:        incoming_stream.next_in = buffer_ptr(input_buffer);
1.2       markus    134:        incoming_stream.avail_in = buffer_len(input_buffer);
1.1       deraadt   135:
1.6       markus    136:        for (;;) {
                    137:                /* Set up fixed-size output buffer. */
1.17      stevesk   138:                incoming_stream.next_out = buf;
1.6       markus    139:                incoming_stream.avail_out = sizeof(buf);
1.1       deraadt   140:
1.2       markus    141:                status = inflate(&incoming_stream, Z_PARTIAL_FLUSH);
                    142:                switch (status) {
                    143:                case Z_OK:
                    144:                        buffer_append(output_buffer, buf,
1.6       markus    145:                            sizeof(buf) - incoming_stream.avail_out);
1.2       markus    146:                        break;
                    147:                case Z_BUF_ERROR:
1.4       markus    148:                        /*
                    149:                         * Comments in zlib.h say that we should keep calling
                    150:                         * inflate() until we get an error.  This appears to
                    151:                         * be the error that we get.
                    152:                         */
1.2       markus    153:                        return;
                    154:                default:
1.18      markus    155:                        inflate_failed = 1;
1.2       markus    156:                        fatal("buffer_uncompress: inflate returned %d", status);
1.6       markus    157:                        /* NOTREACHED */
1.2       markus    158:                }
1.1       deraadt   159:        }
                    160: }