buffer.c revision 120489
1/*
2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 *                    All rights reserved
5 * Functions for manipulating fifo buffers (that can grow if needed).
6 *
7 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose.  Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
12 */
13
14#include "includes.h"
15RCSID("$OpenBSD: buffer.c,v 1.17 2003/09/16 03:03:47 deraadt Exp $");
16
17#include "xmalloc.h"
18#include "buffer.h"
19#include "log.h"
20
21/* Initializes the buffer structure. */
22
23void
24buffer_init(Buffer *buffer)
25{
26	const u_int len = 4096;
27
28	buffer->alloc = 0;
29	buffer->buf = xmalloc(len);
30	buffer->alloc = len;
31	buffer->offset = 0;
32	buffer->end = 0;
33}
34
35/* Frees any memory used for the buffer. */
36
37void
38buffer_free(Buffer *buffer)
39{
40	if (buffer->alloc > 0) {
41		memset(buffer->buf, 0, buffer->alloc);
42		xfree(buffer->buf);
43	}
44}
45
46/*
47 * Clears any data from the buffer, making it empty.  This does not actually
48 * zero the memory.
49 */
50
51void
52buffer_clear(Buffer *buffer)
53{
54	buffer->offset = 0;
55	buffer->end = 0;
56}
57
58/* Appends data to the buffer, expanding it if necessary. */
59
60void
61buffer_append(Buffer *buffer, const void *data, u_int len)
62{
63	void *p;
64	p = buffer_append_space(buffer, len);
65	memcpy(p, data, len);
66}
67
68/*
69 * Appends space to the buffer, expanding the buffer if necessary. This does
70 * not actually copy the data into the buffer, but instead returns a pointer
71 * to the allocated region.
72 */
73
74void *
75buffer_append_space(Buffer *buffer, u_int len)
76{
77	u_int newlen;
78	void *p;
79
80	if (len > 0x100000)
81		fatal("buffer_append_space: len %u not supported", len);
82
83	/* If the buffer is empty, start using it from the beginning. */
84	if (buffer->offset == buffer->end) {
85		buffer->offset = 0;
86		buffer->end = 0;
87	}
88restart:
89	/* If there is enough space to store all data, store it now. */
90	if (buffer->end + len < buffer->alloc) {
91		p = buffer->buf + buffer->end;
92		buffer->end += len;
93		return p;
94	}
95	/*
96	 * If the buffer is quite empty, but all data is at the end, move the
97	 * data to the beginning and retry.
98	 */
99	if (buffer->offset > buffer->alloc / 2) {
100		memmove(buffer->buf, buffer->buf + buffer->offset,
101			buffer->end - buffer->offset);
102		buffer->end -= buffer->offset;
103		buffer->offset = 0;
104		goto restart;
105	}
106	/* Increase the size of the buffer and retry. */
107
108	newlen = buffer->alloc + len + 32768;
109	if (newlen > 0xa00000)
110		fatal("buffer_append_space: alloc %u not supported",
111		    newlen);
112	buffer->buf = xrealloc(buffer->buf, newlen);
113	buffer->alloc = newlen;
114	goto restart;
115	/* NOTREACHED */
116}
117
118/* Returns the number of bytes of data in the buffer. */
119
120u_int
121buffer_len(Buffer *buffer)
122{
123	return buffer->end - buffer->offset;
124}
125
126/* Gets data from the beginning of the buffer. */
127
128void
129buffer_get(Buffer *buffer, void *buf, u_int len)
130{
131	if (len > buffer->end - buffer->offset)
132		fatal("buffer_get: trying to get more bytes %d than in buffer %d",
133		    len, buffer->end - buffer->offset);
134	memcpy(buf, buffer->buf + buffer->offset, len);
135	buffer->offset += len;
136}
137
138/* Consumes the given number of bytes from the beginning of the buffer. */
139
140void
141buffer_consume(Buffer *buffer, u_int bytes)
142{
143	if (bytes > buffer->end - buffer->offset)
144		fatal("buffer_consume: trying to get more bytes than in buffer");
145	buffer->offset += bytes;
146}
147
148/* Consumes the given number of bytes from the end of the buffer. */
149
150void
151buffer_consume_end(Buffer *buffer, u_int bytes)
152{
153	if (bytes > buffer->end - buffer->offset)
154		fatal("buffer_consume_end: trying to get more bytes than in buffer");
155	buffer->end -= bytes;
156}
157
158/* Returns a pointer to the first used byte in the buffer. */
159
160void *
161buffer_ptr(Buffer *buffer)
162{
163	return buffer->buf + buffer->offset;
164}
165
166/* Dumps the contents of the buffer to stderr. */
167
168void
169buffer_dump(Buffer *buffer)
170{
171	int i;
172	u_char *ucp = buffer->buf;
173
174	for (i = buffer->offset; i < buffer->end; i++) {
175		fprintf(stderr, "%02x", ucp[i]);
176		if ((i-buffer->offset)%16==15)
177			fprintf(stderr, "\r\n");
178		else if ((i-buffer->offset)%2==1)
179			fprintf(stderr, " ");
180	}
181	fprintf(stderr, "\r\n");
182}
183