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

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