1 /* $OpenBSD: buffer.c,v 1.31 2006/08/03 03:34:41 deraadt 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).
7  *
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".
13  */
14 
15 #include <sys/param.h>
16 
17 #include <stdio.h>
18 #include <string.h>
19 #include <stdarg.h>
20 
21 #include "xmalloc.h"
22 #include "buffer.h"
23 #include "log.h"
24 
25 #define	BUFFER_MAX_CHUNK	0x100000
26 #define	BUFFER_MAX_LEN		0xa00000
27 #define	BUFFER_ALLOCSZ		0x008000
28 
29 /* Initializes the buffer structure. */
30 
31 void
buffer_init(Buffer * buffer)32 buffer_init(Buffer *buffer)
33 {
34 	const u_int len = 4096;
35 
36 	buffer->alloc = 0;
37 	buffer->buf = xmalloc(len);
38 	buffer->alloc = len;
39 	buffer->offset = 0;
40 	buffer->end = 0;
41 }
42 
43 /* Frees any memory used for the buffer. */
44 
45 void
buffer_free(Buffer * buffer)46 buffer_free(Buffer *buffer)
47 {
48 	if (buffer->alloc > 0) {
49 		memset(buffer->buf, 0, buffer->alloc);
50 		buffer->alloc = 0;
51 		xfree(buffer->buf);
52 	}
53 }
54 
55 /*
56  * Clears any data from the buffer, making it empty.  This does not actually
57  * zero the memory.
58  */
59 
60 void
buffer_clear(Buffer * buffer)61 buffer_clear(Buffer *buffer)
62 {
63 	buffer->offset = 0;
64 	buffer->end = 0;
65 }
66 
67 /* Appends data to the buffer, expanding it if necessary. */
68 
69 void
buffer_append(Buffer * buffer,const void * data,u_int len)70 buffer_append(Buffer *buffer, const void *data, u_int len)
71 {
72 	void *p;
73 	p = buffer_append_space(buffer, len);
74 	memcpy(p, data, len);
75 }
76 
77 static int
buffer_compact(Buffer * buffer)78 buffer_compact(Buffer *buffer)
79 {
80 	/*
81 	 * If the buffer is quite empty, but all data is at the end, move the
82 	 * data to the beginning.
83 	 */
84 	if (buffer->offset > MIN(buffer->alloc, BUFFER_MAX_CHUNK)) {
85 		memmove(buffer->buf, buffer->buf + buffer->offset,
86 			buffer->end - buffer->offset);
87 		buffer->end -= buffer->offset;
88 		buffer->offset = 0;
89 		return (1);
90 	}
91 	return (0);
92 }
93 
94 /*
95  * Appends space to the buffer, expanding the buffer if necessary. This does
96  * not actually copy the data into the buffer, but instead returns a pointer
97  * to the allocated region.
98  */
99 
100 void *
buffer_append_space(Buffer * buffer,u_int len)101 buffer_append_space(Buffer *buffer, u_int len)
102 {
103 	u_int newlen;
104 	void *p;
105 
106 	if (len > BUFFER_MAX_CHUNK)
107 		fatal("buffer_append_space: len %u not supported", len);
108 
109 	/* If the buffer is empty, start using it from the beginning. */
110 	if (buffer->offset == buffer->end) {
111 		buffer->offset = 0;
112 		buffer->end = 0;
113 	}
114 restart:
115 	/* If there is enough space to store all data, store it now. */
116 	if (buffer->end + len < buffer->alloc) {
117 		p = buffer->buf + buffer->end;
118 		buffer->end += len;
119 		return p;
120 	}
121 
122 	/* Compact data back to the start of the buffer if necessary */
123 	if (buffer_compact(buffer))
124 		goto restart;
125 
126 	/* Increase the size of the buffer and retry. */
127 	newlen = roundup(buffer->alloc + len, BUFFER_ALLOCSZ);
128 	if (newlen > BUFFER_MAX_LEN)
129 		fatal("buffer_append_space: alloc %u not supported",
130 		    newlen);
131 	buffer->buf = xrealloc(buffer->buf, 1, newlen);
132 	buffer->alloc = newlen;
133 	goto restart;
134 	/* NOTREACHED */
135 }
136 
137 /*
138  * Check whether an allocation of 'len' will fit in the buffer
139  * This must follow the same math as buffer_append_space
140  */
141 int
buffer_check_alloc(Buffer * buffer,u_int len)142 buffer_check_alloc(Buffer *buffer, u_int len)
143 {
144 	if (buffer->offset == buffer->end) {
145 		buffer->offset = 0;
146 		buffer->end = 0;
147 	}
148  restart:
149 	if (buffer->end + len < buffer->alloc)
150 		return (1);
151 	if (buffer_compact(buffer))
152 		goto restart;
153 	if (roundup(buffer->alloc + len, BUFFER_ALLOCSZ) <= BUFFER_MAX_LEN)
154 		return (1);
155 	return (0);
156 }
157 
158 /* Returns the number of bytes of data in the buffer. */
159 
160 u_int
buffer_len(Buffer * buffer)161 buffer_len(Buffer *buffer)
162 {
163 	return buffer->end - buffer->offset;
164 }
165 
166 /* Gets data from the beginning of the buffer. */
167 
168 int
buffer_get_ret(Buffer * buffer,void * buf,u_int len)169 buffer_get_ret(Buffer *buffer, void *buf, u_int len)
170 {
171 	if (len > buffer->end - buffer->offset) {
172 		error("buffer_get_ret: trying to get more bytes %d than in buffer %d",
173 		    len, buffer->end - buffer->offset);
174 		return (-1);
175 	}
176 	memcpy(buf, buffer->buf + buffer->offset, len);
177 	buffer->offset += len;
178 	return (0);
179 }
180 
181 void
buffer_get(Buffer * buffer,void * buf,u_int len)182 buffer_get(Buffer *buffer, void *buf, u_int len)
183 {
184 	if (buffer_get_ret(buffer, buf, len) == -1)
185 		fatal("buffer_get: buffer error");
186 }
187 
188 /* Consumes the given number of bytes from the beginning of the buffer. */
189 
190 int
buffer_consume_ret(Buffer * buffer,u_int bytes)191 buffer_consume_ret(Buffer *buffer, u_int bytes)
192 {
193 	if (bytes > buffer->end - buffer->offset) {
194 		error("buffer_consume_ret: trying to get more bytes than in buffer");
195 		return (-1);
196 	}
197 	buffer->offset += bytes;
198 	return (0);
199 }
200 
201 void
buffer_consume(Buffer * buffer,u_int bytes)202 buffer_consume(Buffer *buffer, u_int bytes)
203 {
204 	if (buffer_consume_ret(buffer, bytes) == -1)
205 		fatal("buffer_consume: buffer error");
206 }
207 
208 /* Consumes the given number of bytes from the end of the buffer. */
209 
210 int
buffer_consume_end_ret(Buffer * buffer,u_int bytes)211 buffer_consume_end_ret(Buffer *buffer, u_int bytes)
212 {
213 	if (bytes > buffer->end - buffer->offset)
214 		return (-1);
215 	buffer->end -= bytes;
216 	return (0);
217 }
218 
219 void
buffer_consume_end(Buffer * buffer,u_int bytes)220 buffer_consume_end(Buffer *buffer, u_int bytes)
221 {
222 	if (buffer_consume_end_ret(buffer, bytes) == -1)
223 		fatal("buffer_consume_end: trying to get more bytes than in buffer");
224 }
225 
226 /* Returns a pointer to the first used byte in the buffer. */
227 
228 void *
buffer_ptr(Buffer * buffer)229 buffer_ptr(Buffer *buffer)
230 {
231 	return buffer->buf + buffer->offset;
232 }
233 
234 /* Dumps the contents of the buffer to stderr. */
235 
236 void
buffer_dump(Buffer * buffer)237 buffer_dump(Buffer *buffer)
238 {
239 	u_int i;
240 	u_char *ucp = buffer->buf;
241 
242 	for (i = buffer->offset; i < buffer->end; i++) {
243 		fprintf(stderr, "%02x", ucp[i]);
244 		if ((i-buffer->offset)%16==15)
245 			fprintf(stderr, "\r\n");
246 		else if ((i-buffer->offset)%2==1)
247 			fprintf(stderr, " ");
248 	}
249 	fprintf(stderr, "\r\n");
250 }
251