[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.22

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