1 /* $OpenBSD: buffer.c,v 1.29 2006/07/26 02:35:17 stevesk 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>
25 #define BUFFER_MAX_CHUNK 0x100000
26 #define BUFFER_MAX_LEN 0xa00000
27 #define BUFFER_ALLOCSZ 0x008000
29 /* Initializes the buffer structure. */
32 buffer_init(Buffer *buffer)
34 const u_int len = 4096;
37 buffer->buf = xmalloc(len);
43 /* Frees any memory used for the buffer. */
46 buffer_free(Buffer *buffer)
48 if (buffer->alloc > 0) {
49 memset(buffer->buf, 0, buffer->alloc);
56 * Clears any data from the buffer, making it empty. This does not actually
61 buffer_clear(Buffer *buffer)
67 /* Appends data to the buffer, expanding it if necessary. */
70 buffer_append(Buffer *buffer, const void *data, u_int len)
73 p = buffer_append_space(buffer, len);
78 buffer_compact(Buffer *buffer)
81 * If the buffer is quite empty, but all data is at the end, move the
82 * data to the beginning.
84 if (buffer->offset > MIN(buffer->alloc, BUFFER_MAX_CHUNK)) {
85 memmove(buffer->buf, buffer->buf + buffer->offset,
86 buffer->end - buffer->offset);
87 buffer->end -= buffer->offset;
95 * Appends space to the buffer, expanding the buffer if necessary. This does
96 * not actually copy the data into the buffer, but instead returns a pointer
97 * to the allocated region.
101 buffer_append_space(Buffer *buffer, u_int len)
106 if (len > BUFFER_MAX_CHUNK)
107 fatal("buffer_append_space: len %u not supported", len);
109 /* If the buffer is empty, start using it from the beginning. */
110 if (buffer->offset == buffer->end) {
115 /* If there is enough space to store all data, store it now. */
116 if (buffer->end + len < buffer->alloc) {
117 p = buffer->buf + buffer->end;
122 /* Compact data back to the start of the buffer if necessary */
123 if (buffer_compact(buffer))
126 /* Increase the size of the buffer and retry. */
127 newlen = roundup(buffer->alloc + len, BUFFER_ALLOCSZ);
128 if (newlen > BUFFER_MAX_LEN)
129 fatal("buffer_append_space: alloc %u not supported",
131 buffer->buf = xrealloc(buffer->buf, 1, newlen);
132 buffer->alloc = newlen;
138 * Check whether an allocation of 'len' will fit in the buffer
139 * This must follow the same math as buffer_append_space
142 buffer_check_alloc(Buffer *buffer, u_int len)
144 if (buffer->offset == buffer->end) {
149 if (buffer->end + len < buffer->alloc)
151 if (buffer_compact(buffer))
153 if (roundup(buffer->alloc + len, BUFFER_ALLOCSZ) <= BUFFER_MAX_LEN)
158 /* Returns the number of bytes of data in the buffer. */
161 buffer_len(Buffer *buffer)
163 return buffer->end - buffer->offset;
166 /* Gets data from the beginning of the buffer. */
169 buffer_get_ret(Buffer *buffer, void *buf, u_int len)
171 if (len > buffer->end - buffer->offset) {
172 error("buffer_get_ret: trying to get more bytes %d than in buffer %d",
173 len, buffer->end - buffer->offset);
176 memcpy(buf, buffer->buf + buffer->offset, len);
177 buffer->offset += len;
182 buffer_get(Buffer *buffer, void *buf, u_int len)
184 if (buffer_get_ret(buffer, buf, len) == -1)
185 fatal("buffer_get: buffer error");
188 /* Consumes the given number of bytes from the beginning of the buffer. */
191 buffer_consume_ret(Buffer *buffer, u_int bytes)
193 if (bytes > buffer->end - buffer->offset) {
194 error("buffer_consume_ret: trying to get more bytes than in buffer");
197 buffer->offset += bytes;
202 buffer_consume(Buffer *buffer, u_int bytes)
204 if (buffer_consume_ret(buffer, bytes) == -1)
205 fatal("buffer_consume: buffer error");
208 /* Consumes the given number of bytes from the end of the buffer. */
211 buffer_consume_end_ret(Buffer *buffer, u_int bytes)
213 if (bytes > buffer->end - buffer->offset)
215 buffer->end -= bytes;
220 buffer_consume_end(Buffer *buffer, u_int bytes)
222 if (buffer_consume_end_ret(buffer, bytes) == -1)
223 fatal("buffer_consume_end: trying to get more bytes than in buffer");
226 /* Returns a pointer to the first used byte in the buffer. */
229 buffer_ptr(Buffer *buffer)
231 return buffer->buf + buffer->offset;
234 /* Dumps the contents of the buffer to stderr. */
237 buffer_dump(Buffer *buffer)
240 u_char *ucp = buffer->buf;
242 for (i = buffer->offset; i < buffer->end; i++) {
243 fprintf(stderr, "%02x", ucp[i]);
244 if ((i-buffer->offset)%16==15)
245 fprintf(stderr, "\r\n");
246 else if ((i-buffer->offset)%2==1)
247 fprintf(stderr, " ");
249 fprintf(stderr, "\r\n");