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