]> andersk Git - gssapi-openssh.git/blame - openssh/buffer.c
new version: NCSA_GSSAPI_20080403
[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
6df46d40 29#define BUFFER_ALLOCSZ 0x008000
30460aeb 30
3c0ef626 31/* Initializes the buffer structure. */
32
33void
34buffer_init(Buffer *buffer)
35{
473db5ab 36 const u_int len = 4096;
37
38 buffer->alloc = 0;
39 buffer->buf = xmalloc(len);
40 buffer->alloc = len;
3c0ef626 41 buffer->offset = 0;
42 buffer->end = 0;
43}
44
45/* Frees any memory used for the buffer. */
46
47void
48buffer_free(Buffer *buffer)
49{
473db5ab 50 if (buffer->alloc > 0) {
51 memset(buffer->buf, 0, buffer->alloc);
52 buffer->alloc = 0;
53 xfree(buffer->buf);
54 }
3c0ef626 55}
56
57/*
58 * Clears any data from the buffer, making it empty. This does not actually
59 * zero the memory.
60 */
61
62void
63buffer_clear(Buffer *buffer)
64{
65 buffer->offset = 0;
66 buffer->end = 0;
67}
68
69/* Appends data to the buffer, expanding it if necessary. */
70
71void
473db5ab 72buffer_append(Buffer *buffer, const void *data, u_int len)
3c0ef626 73{
473db5ab 74 void *p;
75 p = buffer_append_space(buffer, len);
76 memcpy(p, data, len);
3c0ef626 77}
78
30460aeb 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
3c0ef626 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 */
101
473db5ab 102void *
103buffer_append_space(Buffer *buffer, u_int len)
3c0ef626 104{
473db5ab 105 u_int newlen;
106 void *p;
107
108 if (len > BUFFER_MAX_CHUNK)
109 fatal("buffer_append_space: len %u not supported", len);
110
3c0ef626 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) {
473db5ab 119 p = buffer->buf + buffer->end;
3c0ef626 120 buffer->end += len;
473db5ab 121 return p;
3c0ef626 122 }
30460aeb 123
124 /* Compact data back to the start of the buffer if necessary */
125 if (buffer_compact(buffer))
3c0ef626 126 goto restart;
473db5ab 127
30460aeb 128 /* Increase the size of the buffer and retry. */
6df46d40 129 newlen = roundup(buffer->alloc + len, BUFFER_ALLOCSZ);
fa7499cc 130 /* need it to be slightly larger than the MAX LEN for this */
131 /* still investigating *why* but this works for now -cjr */
132 if (newlen > (BUFFER_MAX_LEN_HPN + BUFFER_MAX_LEN))
473db5ab 133 fatal("buffer_append_space: alloc %u not supported",
134 newlen);
30460aeb 135 buffer->buf = xrealloc(buffer->buf, 1, newlen);
473db5ab 136 buffer->alloc = newlen;
3c0ef626 137 goto restart;
473db5ab 138 /* NOTREACHED */
3c0ef626 139}
140
30460aeb 141/*
142 * Check whether an allocation of 'len' will fit in the buffer
143 * This must follow the same math as buffer_append_space
144 */
145int
146buffer_check_alloc(Buffer *buffer, u_int len)
147{
148 if (buffer->offset == buffer->end) {
149 buffer->offset = 0;
150 buffer->end = 0;
151 }
152 restart:
153 if (buffer->end + len < buffer->alloc)
154 return (1);
155 if (buffer_compact(buffer))
156 goto restart;
6df46d40 157 if (roundup(buffer->alloc + len, BUFFER_ALLOCSZ) <= BUFFER_MAX_LEN)
30460aeb 158 return (1);
159 return (0);
160}
161
3c0ef626 162/* Returns the number of bytes of data in the buffer. */
163
164u_int
165buffer_len(Buffer *buffer)
166{
167 return buffer->end - buffer->offset;
168}
169
170/* Gets data from the beginning of the buffer. */
171
473db5ab 172int
173buffer_get_ret(Buffer *buffer, void *buf, u_int len)
3c0ef626 174{
473db5ab 175 if (len > buffer->end - buffer->offset) {
176 error("buffer_get_ret: trying to get more bytes %d than in buffer %d",
3c0ef626 177 len, buffer->end - buffer->offset);
473db5ab 178 return (-1);
179 }
3c0ef626 180 memcpy(buf, buffer->buf + buffer->offset, len);
181 buffer->offset += len;
473db5ab 182 return (0);
183}
184
185void
186buffer_get(Buffer *buffer, void *buf, u_int len)
187{
188 if (buffer_get_ret(buffer, buf, len) == -1)
189 fatal("buffer_get: buffer error");
3c0ef626 190}
191
192/* Consumes the given number of bytes from the beginning of the buffer. */
193
473db5ab 194int
195buffer_consume_ret(Buffer *buffer, u_int bytes)
196{
197 if (bytes > buffer->end - buffer->offset) {
198 error("buffer_consume_ret: trying to get more bytes than in buffer");
199 return (-1);
200 }
201 buffer->offset += bytes;
202 return (0);
203}
204
3c0ef626 205void
206buffer_consume(Buffer *buffer, u_int bytes)
207{
473db5ab 208 if (buffer_consume_ret(buffer, bytes) == -1)
209 fatal("buffer_consume: buffer error");
3c0ef626 210}
211
212/* Consumes the given number of bytes from the end of the buffer. */
213
473db5ab 214int
215buffer_consume_end_ret(Buffer *buffer, u_int bytes)
216{
217 if (bytes > buffer->end - buffer->offset)
218 return (-1);
219 buffer->end -= bytes;
220 return (0);
221}
222
3c0ef626 223void
224buffer_consume_end(Buffer *buffer, u_int bytes)
225{
473db5ab 226 if (buffer_consume_end_ret(buffer, bytes) == -1)
3c0ef626 227 fatal("buffer_consume_end: trying to get more bytes than in buffer");
3c0ef626 228}
229
230/* Returns a pointer to the first used byte in the buffer. */
231
473db5ab 232void *
3c0ef626 233buffer_ptr(Buffer *buffer)
234{
235 return buffer->buf + buffer->offset;
236}
237
238/* Dumps the contents of the buffer to stderr. */
239
240void
241buffer_dump(Buffer *buffer)
242{
473db5ab 243 u_int i;
244 u_char *ucp = buffer->buf;
3c0ef626 245
246 for (i = buffer->offset; i < buffer->end; i++) {
247 fprintf(stderr, "%02x", ucp[i]);
248 if ((i-buffer->offset)%16==15)
249 fprintf(stderr, "\r\n");
250 else if ((i-buffer->offset)%2==1)
251 fprintf(stderr, " ");
252 }
253 fprintf(stderr, "\r\n");
254}
This page took 1.766638 seconds and 5 git commands to generate.