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