Deleted Added
full compact
compress.c (65668) compress.c (76259)
1/*
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.
6 *
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".
12 */
13
14#include "includes.h"
1/*
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.
6 *
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".
12 */
13
14#include "includes.h"
15RCSID("$OpenBSD: compress.c,v 1.9 2000/09/07 20:27:50 deraadt Exp $");
15RCSID("$OpenBSD: compress.c,v 1.14 2001/04/05 10:39:01 markus Exp $");
16
16
17#include "ssh.h"
17#include "log.h"
18#include "buffer.h"
19#include "zlib.h"
18#include "buffer.h"
19#include "zlib.h"
20#include "compress.h"
20
21static z_stream incoming_stream;
22static z_stream outgoing_stream;
21
22static z_stream incoming_stream;
23static z_stream outgoing_stream;
24static int compress_init_send_called = 0;
25static int compress_init_recv_called = 0;
23
24/*
25 * Initializes compression; level is compression level from 1 to 9
26 * (as in gzip).
27 */
28
29void
26
27/*
28 * Initializes compression; level is compression level from 1 to 9
29 * (as in gzip).
30 */
31
32void
30buffer_compress_init(int level)
33buffer_compress_init_send(int level)
31{
34{
35 if (compress_init_send_called == 1)
36 deflateEnd(&incoming_stream);
37 compress_init_send_called = 1;
32 debug("Enabling compression at level %d.", level);
33 if (level < 1 || level > 9)
34 fatal("Bad compression level %d.", level);
38 debug("Enabling compression at level %d.", level);
39 if (level < 1 || level > 9)
40 fatal("Bad compression level %d.", level);
35 inflateInit(&incoming_stream);
36 deflateInit(&outgoing_stream, level);
37}
41 deflateInit(&outgoing_stream, level);
42}
43void
44buffer_compress_init_recv(void)
45{
46 if (compress_init_recv_called == 1)
47 inflateEnd(&incoming_stream);
48 compress_init_recv_called = 1;
49 inflateInit(&incoming_stream);
50}
38
39/* Frees any data structures allocated for compression. */
40
41void
51
52/* Frees any data structures allocated for compression. */
53
54void
42buffer_compress_uninit()
55buffer_compress_uninit(void)
43{
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);
56{
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);
52 inflateEnd(&incoming_stream);
53 deflateEnd(&outgoing_stream);
65 if (compress_init_recv_called == 1)
66 inflateEnd(&incoming_stream);
67 if (compress_init_send_called == 1)
68 deflateEnd(&outgoing_stream);
54}
55
56/*
57 * Compresses the contents of input_buffer into output_buffer. All packets
58 * compressed using this function will form a single compressed data stream;
59 * however, data will be flushed at the end of every call so that each
60 * output_buffer can be decompressed independently (but in the appropriate
61 * order since they together form a single compression stream) by the

--- 6 unchanged lines hidden (view full) ---

68 char buf[4096];
69 int status;
70
71 /* This case is not handled below. */
72 if (buffer_len(input_buffer) == 0)
73 return;
74
75 /* Input is the contents of the input buffer. */
69}
70
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

--- 6 unchanged lines hidden (view full) ---

83 char buf[4096];
84 int status;
85
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. */
76 outgoing_stream.next_in = (unsigned char *) buffer_ptr(input_buffer);
91 outgoing_stream.next_in = (u_char *) buffer_ptr(input_buffer);
77 outgoing_stream.avail_in = buffer_len(input_buffer);
78
79 /* Loop compressing until deflate() returns with avail_out != 0. */
80 do {
81 /* Set up fixed-size output buffer. */
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. */
82 outgoing_stream.next_out = (unsigned char *)buf;
97 outgoing_stream.next_out = (u_char *)buf;
83 outgoing_stream.avail_out = sizeof(buf);
84
85 /* Compress as much data into the buffer as possible. */
86 status = deflate(&outgoing_stream, Z_PARTIAL_FLUSH);
87 switch (status) {
88 case Z_OK:
89 /* Append compressed data to output_buffer. */
90 buffer_append(output_buffer, buf,

--- 16 unchanged lines hidden (view full) ---

107 */
108
109void
110buffer_uncompress(Buffer * input_buffer, Buffer * output_buffer)
111{
112 char buf[4096];
113 int status;
114
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,

--- 16 unchanged lines hidden (view full) ---

122 */
123
124void
125buffer_uncompress(Buffer * input_buffer, Buffer * output_buffer)
126{
127 char buf[4096];
128 int status;
129
115 incoming_stream.next_in = (unsigned char *) buffer_ptr(input_buffer);
130 incoming_stream.next_in = (u_char *) buffer_ptr(input_buffer);
116 incoming_stream.avail_in = buffer_len(input_buffer);
117
118 for (;;) {
119 /* Set up fixed-size output buffer. */
131 incoming_stream.avail_in = buffer_len(input_buffer);
132
133 for (;;) {
134 /* Set up fixed-size output buffer. */
120 incoming_stream.next_out = (unsigned char *) buf;
135 incoming_stream.next_out = (u_char *) buf;
121 incoming_stream.avail_out = sizeof(buf);
122
123 status = inflate(&incoming_stream, Z_PARTIAL_FLUSH);
124 switch (status) {
125 case Z_OK:
126 buffer_append(output_buffer, buf,
127 sizeof(buf) - incoming_stream.avail_out);
128 break;

--- 13 unchanged lines hidden ---
136 incoming_stream.avail_out = sizeof(buf);
137
138 status = inflate(&incoming_stream, Z_PARTIAL_FLUSH);
139 switch (status) {
140 case Z_OK:
141 buffer_append(output_buffer, buf,
142 sizeof(buf) - incoming_stream.avail_out);
143 break;

--- 13 unchanged lines hidden ---