buffer.c revision 92555
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.15 2002/01/18 18:14:17 stevesk 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	buffer->alloc = 4096;
27	buffer->buf = xmalloc(buffer->alloc);
28	buffer->offset = 0;
29	buffer->end = 0;
30}
31
32/* Frees any memory used for the buffer. */
33
34void
35buffer_free(Buffer *buffer)
36{
37	memset(buffer->buf, 0, buffer->alloc);
38	xfree(buffer->buf);
39}
40
41/*
42 * Clears any data from the buffer, making it empty.  This does not actually
43 * zero the memory.
44 */
45
46void
47buffer_clear(Buffer *buffer)
48{
49	buffer->offset = 0;
50	buffer->end = 0;
51}
52
53/* Appends data to the buffer, expanding it if necessary. */
54
55void
56buffer_append(Buffer *buffer, const void *data, u_int len)
57{
58	void *p;
59	p = buffer_append_space(buffer, len);
60	memcpy(p, data, len);
61}
62
63/*
64 * Appends space to the buffer, expanding the buffer if necessary. This does
65 * not actually copy the data into the buffer, but instead returns a pointer
66 * to the allocated region.
67 */
68
69void *
70buffer_append_space(Buffer *buffer, u_int len)
71{
72	void *p;
73
74	/* If the buffer is empty, start using it from the beginning. */
75	if (buffer->offset == buffer->end) {
76		buffer->offset = 0;
77		buffer->end = 0;
78	}
79restart:
80	/* If there is enough space to store all data, store it now. */
81	if (buffer->end + len < buffer->alloc) {
82		p = buffer->buf + buffer->end;
83		buffer->end += len;
84		return p;
85	}
86	/*
87	 * If the buffer is quite empty, but all data is at the end, move the
88	 * data to the beginning and retry.
89	 */
90	if (buffer->offset > buffer->alloc / 2) {
91		memmove(buffer->buf, buffer->buf + buffer->offset,
92			buffer->end - buffer->offset);
93		buffer->end -= buffer->offset;
94		buffer->offset = 0;
95		goto restart;
96	}
97	/* Increase the size of the buffer and retry. */
98	buffer->alloc += len + 32768;
99	buffer->buf = xrealloc(buffer->buf, buffer->alloc);
100	goto restart;
101	/* NOTREACHED */
102}
103
104/* Returns the number of bytes of data in the buffer. */
105
106u_int
107buffer_len(Buffer *buffer)
108{
109	return buffer->end - buffer->offset;
110}
111
112/* Gets data from the beginning of the buffer. */
113
114void
115buffer_get(Buffer *buffer, void *buf, u_int len)
116{
117	if (len > buffer->end - buffer->offset)
118		fatal("buffer_get: trying to get more bytes %d than in buffer %d",
119		    len, buffer->end - buffer->offset);
120	memcpy(buf, buffer->buf + buffer->offset, len);
121	buffer->offset += len;
122}
123
124/* Consumes the given number of bytes from the beginning of the buffer. */
125
126void
127buffer_consume(Buffer *buffer, u_int bytes)
128{
129	if (bytes > buffer->end - buffer->offset)
130		fatal("buffer_consume: trying to get more bytes than in buffer");
131	buffer->offset += bytes;
132}
133
134/* Consumes the given number of bytes from the end of the buffer. */
135
136void
137buffer_consume_end(Buffer *buffer, u_int bytes)
138{
139	if (bytes > buffer->end - buffer->offset)
140		fatal("buffer_consume_end: trying to get more bytes than in buffer");
141	buffer->end -= bytes;
142}
143
144/* Returns a pointer to the first used byte in the buffer. */
145
146void *
147buffer_ptr(Buffer *buffer)
148{
149	return buffer->buf + buffer->offset;
150}
151
152/* Dumps the contents of the buffer to stderr. */
153
154void
155buffer_dump(Buffer *buffer)
156{
157	int i;
158	u_char *ucp = buffer->buf;
159
160	for (i = buffer->offset; i < buffer->end; i++) {
161		fprintf(stderr, "%02x", ucp[i]);
162		if ((i-buffer->offset)%16==15)
163			fprintf(stderr, "\r\n");
164		else if ((i-buffer->offset)%2==1)
165			fprintf(stderr, " ");
166	}
167	fprintf(stderr, "\r\n");
168}
169