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.2 2003/09/16 16:12:00 drhodus 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)
29 buffer->buf = xmalloc(buffer->alloc);
34 /* Frees any memory used for the buffer. */
37 buffer_free(Buffer *buffer)
39 memset(buffer->buf, 0, buffer->alloc);
44 * Clears any data from the buffer, making it empty. This does not actually
49 buffer_clear(Buffer *buffer)
55 /* Appends data to the buffer, expanding it if necessary. */
58 buffer_append(Buffer *buffer, const void *data, u_int len)
61 p = buffer_append_space(buffer, len);
66 * Appends space to the buffer, expanding the buffer if necessary. This does
67 * not actually copy the data into the buffer, but instead returns a pointer
68 * to the allocated region.
72 buffer_append_space(Buffer *buffer, u_int len)
78 fatal("buffer_append_space: len %u not supported", len);
80 /* If the buffer is empty, start using it from the beginning. */
81 if (buffer->offset == buffer->end) {
86 /* If there is enough space to store all data, store it now. */
87 if (buffer->end + len < buffer->alloc) {
88 p = buffer->buf + buffer->end;
93 * If the buffer is quite empty, but all data is at the end, move the
94 * data to the beginning and retry.
96 if (buffer->offset > buffer->alloc / 2) {
97 memmove(buffer->buf, buffer->buf + buffer->offset,
98 buffer->end - buffer->offset);
99 buffer->end -= buffer->offset;
103 /* Increase the size of the buffer and retry. */
105 newlen = buffer->alloc + len + 32768;
106 if (newlen > 0xa00000)
107 fatal("buffer_append_space: alloc %u not supported",
109 buffer->buf = xrealloc(buffer->buf, newlen);
110 buffer->alloc = newlen;
115 /* Returns the number of bytes of data in the buffer. */
118 buffer_len(Buffer *buffer)
120 return buffer->end - buffer->offset;
123 /* Gets data from the beginning of the buffer. */
126 buffer_get(Buffer *buffer, void *buf, u_int len)
128 if (len > buffer->end - buffer->offset)
129 fatal("buffer_get: trying to get more bytes %d than in buffer %d",
130 len, buffer->end - buffer->offset);
131 memcpy(buf, buffer->buf + buffer->offset, len);
132 buffer->offset += len;
135 /* Consumes the given number of bytes from the beginning of the buffer. */
138 buffer_consume(Buffer *buffer, u_int bytes)
140 if (bytes > buffer->end - buffer->offset)
141 fatal("buffer_consume: trying to get more bytes than in buffer");
142 buffer->offset += bytes;
145 /* Consumes the given number of bytes from the end of the buffer. */
148 buffer_consume_end(Buffer *buffer, u_int bytes)
150 if (bytes > buffer->end - buffer->offset)
151 fatal("buffer_consume_end: trying to get more bytes than in buffer");
152 buffer->end -= bytes;
155 /* Returns a pointer to the first used byte in the buffer. */
158 buffer_ptr(Buffer *buffer)
160 return buffer->buf + buffer->offset;
163 /* Dumps the contents of the buffer to stderr. */
166 buffer_dump(Buffer *buffer)
169 u_char *ucp = buffer->buf;
171 for (i = buffer->offset; i < buffer->end; i++) {
172 fprintf(stderr, "%02x", ucp[i]);
173 if ((i-buffer->offset)%16==15)
174 fprintf(stderr, "\r\n");
175 else if ((i-buffer->offset)%2==1)
176 fprintf(stderr, " ");
178 fprintf(stderr, "\r\n");