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