View | Details | Raw Unified | Return to bug 1131 | Differences between
and this patch

Collapse All | Expand All

(-)buffer.c (-12 / +47 lines)
Lines 18-23 Link Here
18
#include "buffer.h"
18
#include "buffer.h"
19
#include "log.h"
19
#include "log.h"
20
20
21
#define	BUFFER_MAX_CHUNK	0x100000
22
#define	BUFFER_MAX_LEN		0xa00000
23
#define	BUFFER_ALLOCSZ		0x008000
24
21
/* Initializes the buffer structure. */
25
/* Initializes the buffer structure. */
22
26
23
void
27
void
Lines 66-71 buffer_append(Buffer *buffer, const void Link Here
66
	memcpy(p, data, len);
70
	memcpy(p, data, len);
67
}
71
}
68
72
73
static int
74
buffer_compact(Buffer *buffer)
75
{
76
	/*
77
	 * If the buffer is quite empty, but all data is at the end, move the
78
	 * data to the beginning.
79
	 */
80
	if (buffer->offset > MIN(buffer->alloc, BUFFER_MAX_CHUNK)) {
81
		memmove(buffer->buf, buffer->buf + buffer->offset,
82
			buffer->end - buffer->offset);
83
		buffer->end -= buffer->offset;
84
		buffer->offset = 0;
85
		return (1);
86
	}
87
	return (0);
88
}
89
69
/*
90
/*
70
 * Appends space to the buffer, expanding the buffer if necessary. This does
91
 * Appends space to the buffer, expanding the buffer if necessary. This does
71
 * not actually copy the data into the buffer, but instead returns a pointer
92
 * not actually copy the data into the buffer, but instead returns a pointer
Lines 93-112 restart: Link Here
93
		buffer->end += len;
114
		buffer->end += len;
94
		return p;
115
		return p;
95
	}
116
	}
96
	/*
117
97
	 * If the buffer is quite empty, but all data is at the end, move the
118
	/* Compact data back to the start of the buffer if necessary */
98
	 * data to the beginning and retry.
119
	if (buffer_compact(buffer))
99
	 */
100
	if (buffer->offset > MIN(buffer->alloc, BUFFER_MAX_CHUNK)) {
101
		memmove(buffer->buf, buffer->buf + buffer->offset,
102
			buffer->end - buffer->offset);
103
		buffer->end -= buffer->offset;
104
		buffer->offset = 0;
105
		goto restart;
120
		goto restart;
106
	}
107
	/* Increase the size of the buffer and retry. */
108
121
109
	newlen = buffer->alloc + len + 32768;
122
	/* Increase the size of the buffer and retry. */
123
	newlen = roundup(buffer->alloc + len, BUFFER_ALLOCSZ);
110
	if (newlen > BUFFER_MAX_LEN)
124
	if (newlen > BUFFER_MAX_LEN)
111
		fatal("buffer_append_space: alloc %u not supported",
125
		fatal("buffer_append_space: alloc %u not supported",
112
		    newlen);
126
		    newlen);
Lines 114-119 restart: Link Here
114
	buffer->alloc = newlen;
128
	buffer->alloc = newlen;
115
	goto restart;
129
	goto restart;
116
	/* NOTREACHED */
130
	/* NOTREACHED */
131
}
132
133
/*
134
 * Check whether an allocation of 'len' will fit in the buffer
135
 * This must follow the same math as buffer_append_space
136
 */
137
int
138
buffer_check_alloc(Buffer *buffer, u_int len)
139
{
140
	if (buffer->offset == buffer->end) {
141
		buffer->offset = 0;
142
		buffer->end = 0;
143
	}
144
 restart:
145
	if (buffer->end + len < buffer->alloc)
146
		return (1);
147
	if (buffer_compact(buffer))
148
		goto restart;
149
	if (roundup(buffer->alloc + len, BUFFER_ALLOCSZ) <= BUFFER_MAX_LEN)
150
		return (1);
151
	return (0);
117
}
152
}
118
153
119
/* Returns the number of bytes of data in the buffer. */
154
/* Returns the number of bytes of data in the buffer. */
(-)buffer.h (-3 / +2 lines)
Lines 23-31 typedef struct { Link Here
23
	u_int	 end;		/* Offset of last byte containing data. */
23
	u_int	 end;		/* Offset of last byte containing data. */
24
}       Buffer;
24
}       Buffer;
25
25
26
#define	BUFFER_MAX_CHUNK	0x100000
27
#define	BUFFER_MAX_LEN		0xa00000
28
29
void	 buffer_init(Buffer *);
26
void	 buffer_init(Buffer *);
30
void	 buffer_clear(Buffer *);
27
void	 buffer_clear(Buffer *);
31
void	 buffer_free(Buffer *);
28
void	 buffer_free(Buffer *);
Lines 35-40 void *buffer_ptr(Buffer *); Link Here
35
32
36
void	 buffer_append(Buffer *, const void *, u_int);
33
void	 buffer_append(Buffer *, const void *, u_int);
37
void	*buffer_append_space(Buffer *, u_int);
34
void	*buffer_append_space(Buffer *, u_int);
35
36
int	 buffer_check_alloc(Buffer *, u_int);
38
37
39
void	 buffer_get(Buffer *, void *, u_int);
38
void	 buffer_get(Buffer *, void *, u_int);
40
39
(-)channels.c (-4 / +2 lines)
Lines 747-758 channel_pre_open(Channel *c, fd_set *rea Link Here
747
{
747
{
748
	u_int limit = compat20 ? c->remote_window : packet_get_maxsize();
748
	u_int limit = compat20 ? c->remote_window : packet_get_maxsize();
749
749
750
	/* check buffer limits */
751
	limit = MIN(limit, (BUFFER_MAX_LEN - BUFFER_MAX_CHUNK - CHAN_RBUF));
752
753
	if (c->istate == CHAN_INPUT_OPEN &&
750
	if (c->istate == CHAN_INPUT_OPEN &&
754
	    limit > 0 &&
751
	    limit > 0 &&
755
	    buffer_len(&c->input) < limit)
752
	    buffer_len(&c->input) < limit &&
753
	    buffer_check_alloc(&c->input, CHAN_RBUF))
756
		FD_SET(c->rfd, readset);
754
		FD_SET(c->rfd, readset);
757
	if (c->ostate == CHAN_OUTPUT_OPEN ||
755
	if (c->ostate == CHAN_OUTPUT_OPEN ||
758
	    c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
756
	    c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {

Return to bug 1131