1 /* $OpenBSD: buffer.c,v 1.31 2006/08/03 03:34:41 deraadt Exp $ */
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * Functions for manipulating fifo buffers (that can grow if needed).
8 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
17 #include <sys/param.h>
27 #define BUFFER_MAX_CHUNK 0x100000
28 #define BUFFER_MAX_LEN 0xa00000
29 /* try increasing to 256k in hpnxfers */
30 #define BUFFER_ALLOCSZ 0x008000 /* 32k */
31 #define BUFFER_ALLOCSZ_HPN 0x040000 /* 256k */
33 /* Initializes the buffer structure. */
36 buffer_init(Buffer *buffer)
38 const u_int len = 4096;
41 buffer->buf = xmalloc(len);
47 /* Frees any memory used for the buffer. */
50 buffer_free(Buffer *buffer)
52 if (buffer->alloc > 0) {
53 memset(buffer->buf, 0, buffer->alloc);
60 * Clears any data from the buffer, making it empty. This does not actually
65 buffer_clear(Buffer *buffer)
71 /* Appends data to the buffer, expanding it if necessary. */
74 buffer_append(Buffer *buffer, const void *data, u_int len)
77 p = buffer_append_space(buffer, len);
82 buffer_compact(Buffer *buffer)
85 * If the buffer is quite empty, but all data is at the end, move the
86 * data to the beginning.
88 if (buffer->offset > MIN(buffer->alloc, BUFFER_MAX_CHUNK)) {
89 memmove(buffer->buf, buffer->buf + buffer->offset,
90 buffer->end - buffer->offset);
91 buffer->end -= buffer->offset;
99 * Appends space to the buffer, expanding the buffer if necessary. This does
100 * not actually copy the data into the buffer, but instead returns a pointer
101 * to the allocated region.
105 buffer_append_space(Buffer *buffer, u_int len)
112 if (len > BUFFER_MAX_CHUNK)
113 fatal("buffer_append_space: len %u not supported", len);
115 /* If the buffer is empty, start using it from the beginning. */
116 if (buffer->offset == buffer->end) {
121 /* If there is enough space to store all data, store it now. */
122 if (buffer->end + len < buffer->alloc) {
123 p = buffer->buf + buffer->end;
128 /* Compact data back to the start of the buffer if necessary */
129 if (buffer_compact(buffer))
132 /* if hpn is disabled use the smaller buffer size */
133 buf_max = BUFFER_MAX_LEN_HPN;
134 buf_alloc_sz = BUFFER_ALLOCSZ_HPN;
136 /* Increase the size of the buffer and retry. */
137 newlen = roundup(buffer->alloc + len, buf_alloc_sz);
140 if (newlen > buf_max)
141 fatal("buffer_append_space: alloc %u not supported",
143 buffer->buf = xrealloc(buffer->buf, 1, newlen);
144 buffer->alloc = newlen;
150 * Check whether an allocation of 'len' will fit in the buffer
151 * This must follow the same math as buffer_append_space
154 buffer_check_alloc(Buffer *buffer, u_int len)
159 if (buffer->offset == buffer->end) {
164 if (buffer->end + len < buffer->alloc)
166 if (buffer_compact(buffer))
169 /* if hpn is disabled use the smaller buffer size */
170 buf_max = BUFFER_MAX_LEN_HPN;
171 buf_alloc_sz = BUFFER_ALLOCSZ_HPN;
173 if (roundup(buffer->alloc + len, buf_alloc_sz) <= buf_max)
178 /* Returns the number of bytes of data in the buffer. */
181 buffer_len(Buffer *buffer)
183 return buffer->end - buffer->offset;
186 /* Gets data from the beginning of the buffer. */
189 buffer_get_ret(Buffer *buffer, void *buf, u_int len)
191 if (len > buffer->end - buffer->offset) {
192 error("buffer_get_ret: trying to get more bytes %d than in buffer %d",
193 len, buffer->end - buffer->offset);
196 memcpy(buf, buffer->buf + buffer->offset, len);
197 buffer->offset += len;
202 buffer_get(Buffer *buffer, void *buf, u_int len)
204 if (buffer_get_ret(buffer, buf, len) == -1)
205 fatal("buffer_get: buffer error");
208 /* Consumes the given number of bytes from the beginning of the buffer. */
211 buffer_consume_ret(Buffer *buffer, u_int bytes)
213 if (bytes > buffer->end - buffer->offset) {
214 error("buffer_consume_ret: trying to get more bytes than in buffer");
217 buffer->offset += bytes;
222 buffer_consume(Buffer *buffer, u_int bytes)
224 if (buffer_consume_ret(buffer, bytes) == -1)
225 fatal("buffer_consume: buffer error");
228 /* Consumes the given number of bytes from the end of the buffer. */
231 buffer_consume_end_ret(Buffer *buffer, u_int bytes)
233 if (bytes > buffer->end - buffer->offset)
235 buffer->end -= bytes;
240 buffer_consume_end(Buffer *buffer, u_int bytes)
242 if (buffer_consume_end_ret(buffer, bytes) == -1)
243 fatal("buffer_consume_end: trying to get more bytes than in buffer");
246 /* Returns a pointer to the first used byte in the buffer. */
249 buffer_ptr(Buffer *buffer)
251 return buffer->buf + buffer->offset;
254 /* Dumps the contents of the buffer to stderr. */
257 buffer_dump(Buffer *buffer)
260 u_char *ucp = buffer->buf;
262 for (i = buffer->offset; i < buffer->end; i++) {
263 fprintf(stderr, "%02x", ucp[i]);
264 if ((i-buffer->offset)%16==15)
265 fprintf(stderr, "\r\n");
266 else if ((i-buffer->offset)%2==1)
267 fprintf(stderr, " ");
269 fprintf(stderr, "\r\n");