2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * Functions for manipulating fifo buffers (that can grow if needed).
7 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
15 RCSID("$OpenBSD: buffer.c,v 1.16 2002/06/26 08:54:18 markus Exp $");
21 /* Initializes the buffer structure. */
24 buffer_init(Buffer *buffer)
27 buffer->buf = xmalloc(buffer->alloc);
32 /* Frees any memory used for the buffer. */
35 buffer_free(Buffer *buffer)
37 memset(buffer->buf, 0, buffer->alloc);
42 * Clears any data from the buffer, making it empty. This does not actually
47 buffer_clear(Buffer *buffer)
53 /* Appends data to the buffer, expanding it if necessary. */
56 buffer_append(Buffer *buffer, const void *data, u_int len)
59 p = buffer_append_space(buffer, len);
64 * Appends space to the buffer, expanding the buffer if necessary. This does
65 * not actually copy the data into the buffer, but instead returns a pointer
66 * to the allocated region.
70 buffer_append_space(Buffer *buffer, u_int len)
75 fatal("buffer_append_space: len %u not supported", len);
77 /* If the buffer is empty, start using it from the beginning. */
78 if (buffer->offset == buffer->end) {
83 /* If there is enough space to store all data, store it now. */
84 if (buffer->end + len < buffer->alloc) {
85 p = buffer->buf + buffer->end;
90 * If the buffer is quite empty, but all data is at the end, move the
91 * data to the beginning and retry.
93 if (buffer->offset > buffer->alloc / 2) {
94 memmove(buffer->buf, buffer->buf + buffer->offset,
95 buffer->end - buffer->offset);
96 buffer->end -= buffer->offset;
100 /* Increase the size of the buffer and retry. */
101 buffer->alloc += len + 32768;
102 if (buffer->alloc > 0xa00000)
103 fatal("buffer_append_space: alloc %u not supported",
105 buffer->buf = xrealloc(buffer->buf, buffer->alloc);
110 /* Returns the number of bytes of data in the buffer. */
113 buffer_len(Buffer *buffer)
115 return buffer->end - buffer->offset;
118 /* Gets data from the beginning of the buffer. */
121 buffer_get(Buffer *buffer, void *buf, u_int len)
123 if (len > buffer->end - buffer->offset)
124 fatal("buffer_get: trying to get more bytes %d than in buffer %d",
125 len, buffer->end - buffer->offset);
126 memcpy(buf, buffer->buf + buffer->offset, len);
127 buffer->offset += len;
130 /* Consumes the given number of bytes from the beginning of the buffer. */
133 buffer_consume(Buffer *buffer, u_int bytes)
135 if (bytes > buffer->end - buffer->offset)
136 fatal("buffer_consume: trying to get more bytes than in buffer");
137 buffer->offset += bytes;
140 /* Consumes the given number of bytes from the end of the buffer. */
143 buffer_consume_end(Buffer *buffer, u_int bytes)
145 if (bytes > buffer->end - buffer->offset)
146 fatal("buffer_consume_end: trying to get more bytes than in buffer");
147 buffer->end -= bytes;
150 /* Returns a pointer to the first used byte in the buffer. */
153 buffer_ptr(Buffer *buffer)
155 return buffer->buf + buffer->offset;
158 /* Dumps the contents of the buffer to stderr. */
161 buffer_dump(Buffer *buffer)
164 u_char *ucp = buffer->buf;
166 for (i = buffer->offset; i < buffer->end; i++) {
167 fprintf(stderr, "%02x", ucp[i]);
168 if ((i-buffer->offset)%16==15)
169 fprintf(stderr, "\r\n");
170 else if ((i-buffer->offset)%2==1)
171 fprintf(stderr, " ");
173 fprintf(stderr, "\r\n");