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