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