Home | History | Annotate | Download | only in openssh

Lines Matching refs:buffer

1 /* $OpenBSD: buffer.c,v 1.32 2010/02/09 03:56:28 djm Exp $ */
24 #include "buffer.h"
31 /* Initializes the buffer structure. */
34 buffer_init(Buffer *buffer)
38 buffer->alloc = 0;
39 buffer->buf = xmalloc(len);
40 buffer->alloc = len;
41 buffer->offset = 0;
42 buffer->end = 0;
45 /* Frees any memory used for the buffer. */
48 buffer_free(Buffer *buffer)
50 if (buffer->alloc > 0) {
51 memset(buffer->buf, 0, buffer->alloc);
52 buffer->alloc = 0;
53 xfree(buffer->buf);
58 * Clears any data from the buffer, making it empty. This does not actually
63 buffer_clear(Buffer *buffer)
65 buffer->offset = 0;
66 buffer->end = 0;
69 /* Appends data to the buffer, expanding it if necessary. */
72 buffer_append(Buffer *buffer, const void *data, u_int len)
75 p = buffer_append_space(buffer, len);
80 buffer_compact(Buffer *buffer)
83 * If the buffer is quite empty, but all data is at the end, move the
86 if (buffer->offset > MIN(buffer->alloc, BUFFER_MAX_CHUNK)) {
87 memmove(buffer->buf, buffer->buf + buffer->offset,
88 buffer->end - buffer->offset);
89 buffer->end -= buffer->offset;
90 buffer->offset = 0;
97 * Appends space to the buffer, expanding the buffer if necessary. This does
98 * not actually copy the data into the buffer, but instead returns a pointer
103 buffer_append_space(Buffer *buffer, u_int len)
111 /* If the buffer is empty, start using it from the beginning. */
112 if (buffer->offset == buffer->end) {
113 buffer->offset = 0;
114 buffer->end = 0;
118 if (buffer->end + len < buffer->alloc) {
119 p = buffer->buf + buffer->end;
120 buffer->end += len;
124 /* Compact data back to the start of the buffer if necessary */
125 if (buffer_compact(buffer))
128 /* Increase the size of the buffer and retry. */
129 newlen = roundup(buffer->alloc + len, BUFFER_ALLOCSZ);
133 buffer->buf = xrealloc(buffer->buf, 1, newlen);
134 buffer->alloc = newlen;
140 * Check whether an allocation of 'len' will fit in the buffer
144 buffer_check_alloc(Buffer *buffer, u_int len)
146 if (buffer->offset == buffer->end) {
147 buffer->offset = 0;
148 buffer->end = 0;
151 if (buffer->end + len < buffer->alloc)
153 if (buffer_compact(buffer))
155 if (roundup(buffer->alloc + len, BUFFER_ALLOCSZ) <= BUFFER_MAX_LEN)
160 /* Returns the number of bytes of data in the buffer. */
163 buffer_len(const Buffer *buffer)
165 return buffer->end - buffer->offset;
168 /* Gets data from the beginning of the buffer. */
171 buffer_get_ret(Buffer *buffer, void *buf, u_int len)
173 if (len > buffer->end - buffer->offset) {
174 error("buffer_get_ret: trying to get more bytes %d than in buffer %d",
175 len, buffer->end - buffer->offset);
178 memcpy(buf, buffer->buf + buffer->offset, len);
179 buffer->offset += len;
184 buffer_get(Buffer *buffer, void *buf, u_int len)
186 if (buffer_get_ret(buffer, buf, len) == -1)
187 fatal("buffer_get: buffer error");
190 /* Consumes the given number of bytes from the beginning of the buffer. */
193 buffer_consume_ret(Buffer *buffer, u_int bytes)
195 if (bytes > buffer->end - buffer->offset) {
196 error("buffer_consume_ret: trying to get more bytes than in buffer");
199 buffer->offset += bytes;
204 buffer_consume(Buffer *buffer, u_int bytes)
206 if (buffer_consume_ret(buffer, bytes) == -1)
207 fatal("buffer_consume: buffer error");
210 /* Consumes the given number of bytes from the end of the buffer. */
213 buffer_consume_end_ret(Buffer *buffer, u_int bytes)
215 if (bytes > buffer->end - buffer->offset)
217 buffer->end -= bytes;
222 buffer_consume_end(Buffer *buffer, u_int bytes)
224 if (buffer_consume_end_ret(buffer, bytes) == -1)
225 fatal("buffer_consume_end: trying to get more bytes than in buffer");
228 /* Returns a pointer to the first used byte in the buffer. */
231 buffer_ptr(const Buffer *buffer)
233 return buffer->buf + buffer->offset;
236 /* Dumps the contents of the buffer to stderr. */
239 buffer_dump(const Buffer *buffer)
242 u_char *ucp = buffer->buf;
244 for (i = buffer->offset; i < buffer->end; i++) {
246 if ((i-buffer->offset)%16==15)
248 else if ((i-buffer->offset)%2==1)