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

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