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).
23 /* Initializes the buffer structure. */
25 void buffer_init(Buffer *buffer)
28 buffer->buf = xmalloc(buffer->alloc);
33 /* Frees any memory used for the buffer. */
35 void buffer_free(Buffer *buffer)
37 memset(buffer->buf, 0, buffer->alloc);
41 /* Clears any data from the buffer, making it empty. This does not actually
44 void buffer_clear(Buffer *buffer)
50 /* Appends data to the buffer, expanding it if necessary. */
52 void buffer_append(Buffer *buffer, const char *data, unsigned int len)
55 buffer_append_space(buffer, &cp, len);
56 memcpy(cp, data, len);
59 /* Appends space to the buffer, expanding the buffer if necessary.
60 This does not actually copy the data into the buffer, but instead
61 returns a pointer to the allocated region. */
63 void buffer_append_space(Buffer *buffer, char **datap, unsigned int len)
65 /* If the buffer is empty, start using it from the beginning. */
66 if (buffer->offset == buffer->end)
73 /* If there is enough space to store all data, store it now. */
74 if (buffer->end + len < buffer->alloc)
76 *datap = buffer->buf + buffer->end;
81 /* If the buffer is quite empty, but all data is at the end, move the
82 data to the beginning and retry. */
83 if (buffer->offset > buffer->alloc / 2)
85 memmove(buffer->buf, buffer->buf + buffer->offset,
86 buffer->end - buffer->offset);
87 buffer->end -= buffer->offset;
92 /* Increase the size of the buffer and retry. */
93 buffer->alloc += len + 32768;
94 buffer->buf = xrealloc(buffer->buf, buffer->alloc);
98 /* Returns the number of bytes of data in the buffer. */
100 unsigned int buffer_len(Buffer *buffer)
102 return buffer->end - buffer->offset;
105 /* Gets data from the beginning of the buffer. */
107 void buffer_get(Buffer *buffer, char *buf, unsigned int len)
109 if (len > buffer->end - buffer->offset)
110 fatal("buffer_get trying to get more bytes than in buffer");
111 memcpy(buf, buffer->buf + buffer->offset, len);
112 buffer->offset += len;
115 /* Consumes the given number of bytes from the beginning of the buffer. */
117 void buffer_consume(Buffer *buffer, unsigned int bytes)
119 if (bytes > buffer->end - buffer->offset)
120 fatal("buffer_get trying to get more bytes than in buffer");
121 buffer->offset += bytes;
124 /* Consumes the given number of bytes from the end of the buffer. */
126 void buffer_consume_end(Buffer *buffer, unsigned int bytes)
128 if (bytes > buffer->end - buffer->offset)
129 fatal("buffer_get trying to get more bytes than in buffer");
130 buffer->end -= bytes;
133 /* Returns a pointer to the first used byte in the buffer. */
135 char *buffer_ptr(Buffer *buffer)
137 return buffer->buf + buffer->offset;
140 /* Dumps the contents of the buffer to stderr. */
142 void buffer_dump(Buffer *buffer)
145 unsigned char *ucp = (unsigned char *)buffer->buf;
147 for (i = buffer->offset; i < buffer->end; i++)
148 fprintf(stderr, " %02x", ucp[i]);
149 fprintf(stderr, "\n");