1 /* $OpenBSD: buffer.c,v 1.28 2006/07/22 20:48:22 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".
23 #define BUFFER_MAX_CHUNK 0x100000
24 #define BUFFER_MAX_LEN 0xa00000
25 #define BUFFER_ALLOCSZ 0x008000
27 /* Initializes the buffer structure. */
30 buffer_init(Buffer *buffer)
32 const u_int len = 4096;
35 buffer->buf = xmalloc(len);
41 /* Frees any memory used for the buffer. */
44 buffer_free(Buffer *buffer)
46 if (buffer->alloc > 0) {
47 memset(buffer->buf, 0, buffer->alloc);
54 * Clears any data from the buffer, making it empty. This does not actually
59 buffer_clear(Buffer *buffer)
65 /* Appends data to the buffer, expanding it if necessary. */
68 buffer_append(Buffer *buffer, const void *data, u_int len)
71 p = buffer_append_space(buffer, len);
76 buffer_compact(Buffer *buffer)
79 * If the buffer is quite empty, but all data is at the end, move the
80 * data to the beginning.
82 if (buffer->offset > MIN(buffer->alloc, BUFFER_MAX_CHUNK)) {
83 memmove(buffer->buf, buffer->buf + buffer->offset,
84 buffer->end - buffer->offset);
85 buffer->end -= buffer->offset;
93 * Appends space to the buffer, expanding the buffer if necessary. This does
94 * not actually copy the data into the buffer, but instead returns a pointer
95 * to the allocated region.
99 buffer_append_space(Buffer *buffer, u_int len)
104 if (len > BUFFER_MAX_CHUNK)
105 fatal("buffer_append_space: len %u not supported", len);
107 /* If the buffer is empty, start using it from the beginning. */
108 if (buffer->offset == buffer->end) {
113 /* If there is enough space to store all data, store it now. */
114 if (buffer->end + len < buffer->alloc) {
115 p = buffer->buf + buffer->end;
120 /* Compact data back to the start of the buffer if necessary */
121 if (buffer_compact(buffer))
124 /* Increase the size of the buffer and retry. */
125 newlen = roundup(buffer->alloc + len, BUFFER_ALLOCSZ);
126 if (newlen > BUFFER_MAX_LEN)
127 fatal("buffer_append_space: alloc %u not supported",
129 buffer->buf = xrealloc(buffer->buf, 1, newlen);
130 buffer->alloc = newlen;
136 * Check whether an allocation of 'len' will fit in the buffer
137 * This must follow the same math as buffer_append_space
140 buffer_check_alloc(Buffer *buffer, u_int len)
142 if (buffer->offset == buffer->end) {
147 if (buffer->end + len < buffer->alloc)
149 if (buffer_compact(buffer))
151 if (roundup(buffer->alloc + len, BUFFER_ALLOCSZ) <= BUFFER_MAX_LEN)
156 /* Returns the number of bytes of data in the buffer. */
159 buffer_len(Buffer *buffer)
161 return buffer->end - buffer->offset;
164 /* Gets data from the beginning of the buffer. */
167 buffer_get_ret(Buffer *buffer, void *buf, u_int len)
169 if (len > buffer->end - buffer->offset) {
170 error("buffer_get_ret: trying to get more bytes %d than in buffer %d",
171 len, buffer->end - buffer->offset);
174 memcpy(buf, buffer->buf + buffer->offset, len);
175 buffer->offset += len;
180 buffer_get(Buffer *buffer, void *buf, u_int len)
182 if (buffer_get_ret(buffer, buf, len) == -1)
183 fatal("buffer_get: buffer error");
186 /* Consumes the given number of bytes from the beginning of the buffer. */
189 buffer_consume_ret(Buffer *buffer, u_int bytes)
191 if (bytes > buffer->end - buffer->offset) {
192 error("buffer_consume_ret: trying to get more bytes than in buffer");
195 buffer->offset += bytes;
200 buffer_consume(Buffer *buffer, u_int bytes)
202 if (buffer_consume_ret(buffer, bytes) == -1)
203 fatal("buffer_consume: buffer error");
206 /* Consumes the given number of bytes from the end of the buffer. */
209 buffer_consume_end_ret(Buffer *buffer, u_int bytes)
211 if (bytes > buffer->end - buffer->offset)
213 buffer->end -= bytes;
218 buffer_consume_end(Buffer *buffer, u_int bytes)
220 if (buffer_consume_end_ret(buffer, bytes) == -1)
221 fatal("buffer_consume_end: trying to get more bytes than in buffer");
224 /* Returns a pointer to the first used byte in the buffer. */
227 buffer_ptr(Buffer *buffer)
229 return buffer->buf + buffer->offset;
232 /* Dumps the contents of the buffer to stderr. */
235 buffer_dump(Buffer *buffer)
238 u_char *ucp = buffer->buf;
240 for (i = buffer->offset; i < buffer->end; i++) {
241 fprintf(stderr, "%02x", ucp[i]);
242 if ((i-buffer->offset)%16==15)
243 fprintf(stderr, "\r\n");
244 else if ((i-buffer->offset)%2==1)
245 fprintf(stderr, " ");
247 fprintf(stderr, "\r\n");