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);
48 /* Frees any memory used for the buffer. */
51 buffer_free(Buffer *buffer)
53 if (buffer->alloc > 0) {
54 memset(buffer->buf, 0, buffer->alloc);
61 * Clears any data from the buffer, making it empty. This does not actually
66 buffer_clear(Buffer *buffer)
72 /* Appends data to the buffer, expanding it if necessary. */
75 buffer_append(Buffer *buffer, const void *data, u_int len)
78 p = buffer_append_space(buffer, len);
83 buffer_compact(Buffer *buffer)
86 * If the buffer is quite empty, but all data is at the end, move the
87 * data to the beginning.
89 if (buffer->offset > MIN(buffer->alloc, BUFFER_MAX_CHUNK)) {
90 memmove(buffer->buf, buffer->buf + buffer->offset,
91 buffer->end - buffer->offset);
92 buffer->end -= buffer->offset;
100 * Appends space to the buffer, expanding the buffer if necessary. This does
101 * not actually copy the data into the buffer, but instead returns a pointer
102 * to the allocated region.
106 buffer_append_space(Buffer *buffer, u_int len)
113 if (len > BUFFER_MAX_CHUNK)
114 fatal("buffer_append_space: len %u not supported", len);
116 /* If the buffer is empty, start using it from the beginning. */
117 if (buffer->offset == buffer->end) {
122 /* If there is enough space to store all data, store it now. */
123 if (buffer->end + len < buffer->alloc) {
124 p = buffer->buf + buffer->end;
129 /* Compact data back to the start of the buffer if necessary */
130 if (buffer_compact(buffer))
133 // if hpn is disabled use the smaller buffer size
134 buf_max = BUFFER_MAX_LEN_HPN;
135 buf_alloc_sz = BUFFER_ALLOCSZ_HPN;
137 /* Increase the size of the buffer and retry. */
138 newlen = roundup(buffer->alloc + len, buf_alloc_sz);
141 if (newlen > buf_max)
142 fatal("buffer_append_space: alloc %u not supported",
144 buffer->buf = xrealloc(buffer->buf, 1, newlen);
145 buffer->alloc = newlen;
151 * Check whether an allocation of 'len' will fit in the buffer
152 * This must follow the same math as buffer_append_space
155 buffer_check_alloc(Buffer *buffer, u_int len)
160 if (buffer->offset == buffer->end) {
165 if (buffer->end + len < buffer->alloc)
167 if (buffer_compact(buffer))
170 // if hpn is disabled use the smaller buffer size
171 buf_max = BUFFER_MAX_LEN_HPN;
172 buf_alloc_sz = BUFFER_ALLOCSZ_HPN;
174 if (roundup(buffer->alloc + len, buf_alloc_sz) <= buf_max)
179 /* Returns the number of bytes of data in the buffer. */
182 buffer_len(Buffer *buffer)
184 return buffer->end - buffer->offset;
187 /* Gets data from the beginning of the buffer. */
190 buffer_get_ret(Buffer *buffer, void *buf, u_int len)
192 if (len > buffer->end - buffer->offset) {
193 error("buffer_get_ret: trying to get more bytes %d than in buffer %d",
194 len, buffer->end - buffer->offset);
197 memcpy(buf, buffer->buf + buffer->offset, len);
198 buffer->offset += len;
203 buffer_get(Buffer *buffer, void *buf, u_int len)
205 if (buffer_get_ret(buffer, buf, len) == -1)
206 fatal("buffer_get: buffer error");
209 /* Consumes the given number of bytes from the beginning of the buffer. */
212 buffer_consume_ret(Buffer *buffer, u_int bytes)
214 if (bytes > buffer->end - buffer->offset) {
215 error("buffer_consume_ret: trying to get more bytes than in buffer");
218 buffer->offset += bytes;
223 buffer_consume(Buffer *buffer, u_int bytes)
225 if (buffer_consume_ret(buffer, bytes) == -1)
226 fatal("buffer_consume: buffer error");
229 /* Consumes the given number of bytes from the end of the buffer. */
232 buffer_consume_end_ret(Buffer *buffer, u_int bytes)
234 if (bytes > buffer->end - buffer->offset)
236 buffer->end -= bytes;
241 buffer_consume_end(Buffer *buffer, u_int bytes)
243 if (buffer_consume_end_ret(buffer, bytes) == -1)
244 fatal("buffer_consume_end: trying to get more bytes than in buffer");
247 /* Returns a pointer to the first used byte in the buffer. */
250 buffer_ptr(Buffer *buffer)
252 return buffer->buf + buffer->offset;
255 /* Dumps the contents of the buffer to stderr. */
258 buffer_dump(Buffer *buffer)
261 u_char *ucp = buffer->buf;
263 for (i = buffer->offset; i < buffer->end; i++) {
264 fprintf(stderr, "%02x", ucp[i]);
265 if ((i-buffer->offset)%16==15)
266 fprintf(stderr, "\r\n");
267 else if ((i-buffer->offset)%2==1)
268 fprintf(stderr, " ");
270 fprintf(stderr, "\r\n");