- djm@cvs.openbsd.org 2006/07/10 12:03:20
[openssh-git.git] / buffer.c
blobba718daf2214e024f29716bc0d2f6c9112a5a53c
1 /* $OpenBSD: buffer.c,v 1.27 2006/04/16 00:48:52 djm Exp $ */
2 /*
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
6 * Functions for manipulating fifo buffers (that can grow if needed).
8 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
15 #include "includes.h"
17 #include "xmalloc.h"
18 #include "buffer.h"
19 #include "log.h"
21 #define BUFFER_MAX_CHUNK 0x100000
22 #define BUFFER_MAX_LEN 0xa00000
23 #define BUFFER_ALLOCSZ 0x008000
25 /* Initializes the buffer structure. */
27 void
28 buffer_init(Buffer *buffer)
30 const u_int len = 4096;
32 buffer->alloc = 0;
33 buffer->buf = xmalloc(len);
34 buffer->alloc = len;
35 buffer->offset = 0;
36 buffer->end = 0;
39 /* Frees any memory used for the buffer. */
41 void
42 buffer_free(Buffer *buffer)
44 if (buffer->alloc > 0) {
45 memset(buffer->buf, 0, buffer->alloc);
46 buffer->alloc = 0;
47 xfree(buffer->buf);
52 * Clears any data from the buffer, making it empty. This does not actually
53 * zero the memory.
56 void
57 buffer_clear(Buffer *buffer)
59 buffer->offset = 0;
60 buffer->end = 0;
63 /* Appends data to the buffer, expanding it if necessary. */
65 void
66 buffer_append(Buffer *buffer, const void *data, u_int len)
68 void *p;
69 p = buffer_append_space(buffer, len);
70 memcpy(p, data, len);
73 static int
74 buffer_compact(Buffer *buffer)
77 * If the buffer is quite empty, but all data is at the end, move the
78 * data to the beginning.
80 if (buffer->offset > MIN(buffer->alloc, BUFFER_MAX_CHUNK)) {
81 memmove(buffer->buf, buffer->buf + buffer->offset,
82 buffer->end - buffer->offset);
83 buffer->end -= buffer->offset;
84 buffer->offset = 0;
85 return (1);
87 return (0);
91 * Appends space to the buffer, expanding the buffer if necessary. This does
92 * not actually copy the data into the buffer, but instead returns a pointer
93 * to the allocated region.
96 void *
97 buffer_append_space(Buffer *buffer, u_int len)
99 u_int newlen;
100 void *p;
102 if (len > BUFFER_MAX_CHUNK)
103 fatal("buffer_append_space: len %u not supported", len);
105 /* If the buffer is empty, start using it from the beginning. */
106 if (buffer->offset == buffer->end) {
107 buffer->offset = 0;
108 buffer->end = 0;
110 restart:
111 /* If there is enough space to store all data, store it now. */
112 if (buffer->end + len < buffer->alloc) {
113 p = buffer->buf + buffer->end;
114 buffer->end += len;
115 return p;
118 /* Compact data back to the start of the buffer if necessary */
119 if (buffer_compact(buffer))
120 goto restart;
122 /* Increase the size of the buffer and retry. */
123 newlen = roundup(buffer->alloc + len, BUFFER_ALLOCSZ);
124 if (newlen > BUFFER_MAX_LEN)
125 fatal("buffer_append_space: alloc %u not supported",
126 newlen);
127 buffer->buf = xrealloc(buffer->buf, 1, newlen);
128 buffer->alloc = newlen;
129 goto restart;
130 /* NOTREACHED */
134 * Check whether an allocation of 'len' will fit in the buffer
135 * This must follow the same math as buffer_append_space
138 buffer_check_alloc(Buffer *buffer, u_int len)
140 if (buffer->offset == buffer->end) {
141 buffer->offset = 0;
142 buffer->end = 0;
144 restart:
145 if (buffer->end + len < buffer->alloc)
146 return (1);
147 if (buffer_compact(buffer))
148 goto restart;
149 if (roundup(buffer->alloc + len, BUFFER_ALLOCSZ) <= BUFFER_MAX_LEN)
150 return (1);
151 return (0);
154 /* Returns the number of bytes of data in the buffer. */
156 u_int
157 buffer_len(Buffer *buffer)
159 return buffer->end - buffer->offset;
162 /* Gets data from the beginning of the buffer. */
165 buffer_get_ret(Buffer *buffer, void *buf, u_int len)
167 if (len > buffer->end - buffer->offset) {
168 error("buffer_get_ret: trying to get more bytes %d than in buffer %d",
169 len, buffer->end - buffer->offset);
170 return (-1);
172 memcpy(buf, buffer->buf + buffer->offset, len);
173 buffer->offset += len;
174 return (0);
177 void
178 buffer_get(Buffer *buffer, void *buf, u_int len)
180 if (buffer_get_ret(buffer, buf, len) == -1)
181 fatal("buffer_get: buffer error");
184 /* Consumes the given number of bytes from the beginning of the buffer. */
187 buffer_consume_ret(Buffer *buffer, u_int bytes)
189 if (bytes > buffer->end - buffer->offset) {
190 error("buffer_consume_ret: trying to get more bytes than in buffer");
191 return (-1);
193 buffer->offset += bytes;
194 return (0);
197 void
198 buffer_consume(Buffer *buffer, u_int bytes)
200 if (buffer_consume_ret(buffer, bytes) == -1)
201 fatal("buffer_consume: buffer error");
204 /* Consumes the given number of bytes from the end of the buffer. */
207 buffer_consume_end_ret(Buffer *buffer, u_int bytes)
209 if (bytes > buffer->end - buffer->offset)
210 return (-1);
211 buffer->end -= bytes;
212 return (0);
215 void
216 buffer_consume_end(Buffer *buffer, u_int bytes)
218 if (buffer_consume_end_ret(buffer, bytes) == -1)
219 fatal("buffer_consume_end: trying to get more bytes than in buffer");
222 /* Returns a pointer to the first used byte in the buffer. */
224 void *
225 buffer_ptr(Buffer *buffer)
227 return buffer->buf + buffer->offset;
230 /* Dumps the contents of the buffer to stderr. */
232 void
233 buffer_dump(Buffer *buffer)
235 u_int i;
236 u_char *ucp = buffer->buf;
238 for (i = buffer->offset; i < buffer->end; i++) {
239 fprintf(stderr, "%02x", ucp[i]);
240 if ((i-buffer->offset)%16==15)
241 fprintf(stderr, "\r\n");
242 else if ((i-buffer->offset)%2==1)
243 fprintf(stderr, " ");
245 fprintf(stderr, "\r\n");