5 * Author: Tatu Ylonen <ylo@cs.hut.fi>
7 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
10 * Created: Sat Mar 18 04:15:33 1995 ylo
12 * Functions for manipulating fifo buffers (that can grow if needed).
17 RCSID("$OpenBSD: buffer.c,v 1.7 2000/06/20 01:39:39 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 char *data, unsigned int len)
61 buffer_append_space(buffer, &cp, len);
62 memcpy(cp, data, 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, char **datap, unsigned int len)
74 /* If the buffer is empty, start using it from the beginning. */
75 if (buffer->offset == buffer->end) {
80 /* If there is enough space to store all data, store it now. */
81 if (buffer->end + len < buffer->alloc) {
82 *datap = buffer->buf + buffer->end;
87 * If the buffer is quite empty, but all data is at the end, move the
88 * data to the beginning and retry.
90 if (buffer->offset > buffer->alloc / 2) {
91 memmove(buffer->buf, buffer->buf + buffer->offset,
92 buffer->end - buffer->offset);
93 buffer->end -= buffer->offset;
97 /* Increase the size of the buffer and retry. */
98 buffer->alloc += len + 32768;
99 buffer->buf = xrealloc(buffer->buf, buffer->alloc);
103 /* Returns the number of bytes of data in the buffer. */
106 buffer_len(Buffer *buffer)
108 return buffer->end - buffer->offset;
111 /* Gets data from the beginning of the buffer. */
114 buffer_get(Buffer *buffer, char *buf, unsigned int len)
116 if (len > buffer->end - buffer->offset)
117 fatal("buffer_get: trying to get more bytes than in buffer");
118 memcpy(buf, buffer->buf + buffer->offset, len);
119 buffer->offset += len;
122 /* Consumes the given number of bytes from the beginning of the buffer. */
125 buffer_consume(Buffer *buffer, unsigned int bytes)
127 if (bytes > buffer->end - buffer->offset)
128 fatal("buffer_consume: trying to get more bytes than in buffer");
129 buffer->offset += bytes;
132 /* Consumes the given number of bytes from the end of the buffer. */
135 buffer_consume_end(Buffer *buffer, unsigned int bytes)
137 if (bytes > buffer->end - buffer->offset)
138 fatal("buffer_consume_end: trying to get more bytes than in buffer");
139 buffer->end -= bytes;
142 /* Returns a pointer to the first used byte in the buffer. */
145 buffer_ptr(Buffer *buffer)
147 return buffer->buf + buffer->offset;
150 /* Dumps the contents of the buffer to stderr. */
153 buffer_dump(Buffer *buffer)
156 unsigned char *ucp = (unsigned char *) buffer->buf;
158 for (i = buffer->offset; i < buffer->end; i++)
159 fprintf(stderr, " %02x", ucp[i]);
160 fprintf(stderr, "\n");