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. */
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);
43 /* Clears any data from the buffer, making it empty. This does not actually
47 buffer_clear(Buffer *buffer)
53 /* Appends data to the buffer, expanding it if necessary. */
56 buffer_append(Buffer *buffer, const char *data, unsigned int len)
59 buffer_append_space(buffer, &cp, len);
60 memcpy(cp, data, len);
63 /* Appends space to the buffer, expanding the buffer if necessary.
64 This does not actually copy the data into the buffer, but instead
65 returns a pointer to the allocated region. */
68 buffer_append_space(Buffer *buffer, char **datap, unsigned int len)
70 /* If the buffer is empty, start using it from the beginning. */
71 if (buffer->offset == buffer->end) {
76 /* If there is enough space to store all data, store it now. */
77 if (buffer->end + len < buffer->alloc) {
78 *datap = buffer->buf + buffer->end;
82 /* If the buffer is quite empty, but all data is at the end, move
83 the data to the beginning and retry. */
84 if (buffer->offset > buffer->alloc / 2) {
85 memmove(buffer->buf, buffer->buf + buffer->offset,
86 buffer->end - buffer->offset);
87 buffer->end -= buffer->offset;
91 /* Increase the size of the buffer and retry. */
92 buffer->alloc += len + 32768;
93 buffer->buf = xrealloc(buffer->buf, buffer->alloc);
97 /* Returns the number of bytes of data in the buffer. */
100 buffer_len(Buffer *buffer)
102 return buffer->end - buffer->offset;
105 /* Gets data from the beginning of the buffer. */
108 buffer_get(Buffer *buffer, char *buf, unsigned int len)
110 if (len > buffer->end - buffer->offset)
111 fatal("buffer_get trying to get more bytes than in buffer");
112 memcpy(buf, buffer->buf + buffer->offset, len);
113 buffer->offset += len;
116 /* Consumes the given number of bytes from the beginning of the buffer. */
119 buffer_consume(Buffer *buffer, unsigned int bytes)
121 if (bytes > buffer->end - buffer->offset)
122 fatal("buffer_get trying to get more bytes than in buffer");
123 buffer->offset += bytes;
126 /* Consumes the given number of bytes from the end of the buffer. */
129 buffer_consume_end(Buffer *buffer, unsigned int bytes)
131 if (bytes > buffer->end - buffer->offset)
132 fatal("buffer_get trying to get more bytes than in buffer");
133 buffer->end -= bytes;
136 /* Returns a pointer to the first used byte in the buffer. */
139 buffer_ptr(Buffer *buffer)
141 return buffer->buf + buffer->offset;
144 /* Dumps the contents of the buffer to stderr. */
147 buffer_dump(Buffer *buffer)
150 unsigned char *ucp = (unsigned char *) buffer->buf;
152 for (i = buffer->offset; i < buffer->end; i++)
153 fprintf(stderr, " %02x", ucp[i]);
154 fprintf(stderr, "\n");