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".
20 /* Initializes the buffer structure. */
23 buffer_init(Buffer *buffer)
25 const u_int len = 4096;
28 buffer->buf = xmalloc(len);
34 /* Frees any memory used for the buffer. */
37 buffer_free(Buffer *buffer)
39 if (buffer->alloc > 0) {
40 memset(buffer->buf, 0, buffer->alloc);
47 * Clears any data from the buffer, making it empty. This does not actually
52 buffer_clear(Buffer *buffer)
58 /* Appends data to the buffer, expanding it if necessary. */
61 buffer_append(Buffer *buffer, const void *data, u_int len)
64 p = buffer_append_space(buffer, len);
69 * Appends space to the buffer, expanding the buffer if necessary. This does
70 * not actually copy the data into the buffer, but instead returns a pointer
71 * to the allocated region.
75 buffer_append_space(Buffer *buffer, u_int len)
80 if (len > BUFFER_MAX_CHUNK)
81 fatal("buffer_append_space: len %u not supported", len);
83 /* If the buffer is empty, start using it from the beginning. */
84 if (buffer->offset == buffer->end) {
89 /* If there is enough space to store all data, store it now. */
90 if (buffer->end + len < buffer->alloc) {
91 p = buffer->buf + buffer->end;
96 * If the buffer is quite empty, but all data is at the end, move the
97 * data to the beginning and retry.
99 if (buffer->offset > MIN(buffer->alloc, BUFFER_MAX_CHUNK)) {
100 memmove(buffer->buf, buffer->buf + buffer->offset,
101 buffer->end - buffer->offset);
102 buffer->end -= buffer->offset;
106 /* Increase the size of the buffer and retry. */
108 newlen = buffer->alloc + len + 32768;
109 if (newlen > BUFFER_MAX_LEN)
110 fatal("buffer_append_space: alloc %u not supported",
112 buffer->buf = xrealloc(buffer->buf, 1, newlen);
113 buffer->alloc = newlen;
118 /* Returns the number of bytes of data in the buffer. */
121 buffer_len(Buffer *buffer)
123 return buffer->end - buffer->offset;
126 /* Gets data from the beginning of the buffer. */
129 buffer_get_ret(Buffer *buffer, void *buf, u_int len)
131 if (len > buffer->end - buffer->offset) {
132 error("buffer_get_ret: trying to get more bytes %d than in buffer %d",
133 len, buffer->end - buffer->offset);
136 memcpy(buf, buffer->buf + buffer->offset, len);
137 buffer->offset += len;
142 buffer_get(Buffer *buffer, void *buf, u_int len)
144 if (buffer_get_ret(buffer, buf, len) == -1)
145 fatal("buffer_get: buffer error");
148 /* Consumes the given number of bytes from the beginning of the buffer. */
151 buffer_consume_ret(Buffer *buffer, u_int bytes)
153 if (bytes > buffer->end - buffer->offset) {
154 error("buffer_consume_ret: trying to get more bytes than in buffer");
157 buffer->offset += bytes;
162 buffer_consume(Buffer *buffer, u_int bytes)
164 if (buffer_consume_ret(buffer, bytes) == -1)
165 fatal("buffer_consume: buffer error");
168 /* Consumes the given number of bytes from the end of the buffer. */
171 buffer_consume_end_ret(Buffer *buffer, u_int bytes)
173 if (bytes > buffer->end - buffer->offset)
175 buffer->end -= bytes;
180 buffer_consume_end(Buffer *buffer, u_int bytes)
182 if (buffer_consume_end_ret(buffer, bytes) == -1)
183 fatal("buffer_consume_end: trying to get more bytes than in buffer");
186 /* Returns a pointer to the first used byte in the buffer. */
189 buffer_ptr(Buffer *buffer)
191 return buffer->buf + buffer->offset;
194 /* Dumps the contents of the buffer to stderr. */
197 buffer_dump(Buffer *buffer)
200 u_char *ucp = buffer->buf;
202 for (i = buffer->offset; i < buffer->end; i++) {
203 fprintf(stderr, "%02x", ucp[i]);
204 if ((i-buffer->offset)%16==15)
205 fprintf(stderr, "\r\n");
206 else if ((i-buffer->offset)%2==1)
207 fprintf(stderr, " ");
209 fprintf(stderr, "\r\n");