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".
13 * $DragonFly: src/crypto/openssh/Attic/buffer.c,v 1.4 2003/09/17 02:01:05 dillon Exp $
17 RCSID("$OpenBSD: buffer.c,v 1.16 2002/06/26 08:54:18 markus Exp $");
23 /* Initializes the buffer structure. */
26 buffer_init(Buffer *buffer)
28 const u_int len = 4096;
30 bzero(buffer, sizeof(Buffer));
31 buffer->buf = xmalloc(len);
35 /* Frees any memory used for the buffer. */
38 buffer_free(Buffer *buffer)
40 if (buffer->alloc > 0) {
41 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)
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 > buffer->alloc / 2) {
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 > 0xa00000)
110 fatal("buffer_append_space: alloc %u not supported",
112 buffer->buf = xrealloc(buffer->buf, 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(Buffer *buffer, void *buf, u_int len)
131 if (len > buffer->end - buffer->offset)
132 fatal("buffer_get: trying to get more bytes %d than in buffer %d",
133 len, buffer->end - buffer->offset);
134 memcpy(buf, buffer->buf + buffer->offset, len);
135 buffer->offset += len;
138 /* Consumes the given number of bytes from the beginning of the buffer. */
141 buffer_consume(Buffer *buffer, u_int bytes)
143 if (bytes > buffer->end - buffer->offset)
144 fatal("buffer_consume: trying to get more bytes than in buffer");
145 buffer->offset += bytes;
148 /* Consumes the given number of bytes from the end of the buffer. */
151 buffer_consume_end(Buffer *buffer, u_int bytes)
153 if (bytes > buffer->end - buffer->offset)
154 fatal("buffer_consume_end: trying to get more bytes than in buffer");
155 buffer->end -= bytes;
158 /* Returns a pointer to the first used byte in the buffer. */
161 buffer_ptr(Buffer *buffer)
163 return buffer->buf + buffer->offset;
166 /* Dumps the contents of the buffer to stderr. */
169 buffer_dump(Buffer *buffer)
172 u_char *ucp = buffer->buf;
174 for (i = buffer->offset; i < buffer->end; i++) {
175 fprintf(stderr, "%02x", ucp[i]);
176 if ((i-buffer->offset)%16==15)
177 fprintf(stderr, "\r\n");
178 else if ((i-buffer->offset)%2==1)
179 fprintf(stderr, " ");
181 fprintf(stderr, "\r\n");