summaryrefslogtreecommitdiff
path: root/buffer.c
diff options
context:
space:
mode:
authorDamien Miller <djm@mindrot.org>2014-05-15 14:33:43 +1000
committerDamien Miller <djm@mindrot.org>2014-05-15 14:33:43 +1000
commit05e82c3b963c33048128baf72a6f6b3a1c10b4c1 (patch)
treecb238452459af2f8311d54ca509722497e799517 /buffer.c
parent380948180f847a26f2d0c85b4dad3dca2ed2fd8b (diff)
- djm@cvs.openbsd.org 2014/04/30 05:29:56
[bufaux.c bufbn.c bufec.c buffer.c buffer.h sshbuf-getput-basic.c] [sshbuf-getput-crypto.c sshbuf-misc.c sshbuf.c sshbuf.h ssherr.c] [ssherr.h] New buffer API; the first installment of the conversion/replacement of OpenSSH's internals to make them usable as a standalone library. This includes a set of wrappers to make it compatible with the existing buffer API so replacement can occur incrementally. With and ok markus@ Thanks also to Ben Hawkes, David Tomaschik, Ivan Fratric, Matthew Dempsky and Ron Bowes for a detailed review.
Diffstat (limited to 'buffer.c')
-rw-r--r--buffer.c245
1 files changed, 54 insertions, 191 deletions
diff --git a/buffer.c b/buffer.c
index d240f6753..07bc186d0 100644
--- a/buffer.c
+++ b/buffer.c
@@ -1,253 +1,116 @@
1/* $OpenBSD: buffer.c,v 1.35 2014/02/02 03:44:31 djm Exp $ */ 1/* $OpenBSD: buffer.c,v 1.36 2014/04/30 05:29:56 djm Exp $ */
2
2/* 3/*
3 * Author: Tatu Ylonen <ylo@cs.hut.fi> 4 * Copyright (c) 2012 Damien Miller <djm@mindrot.org>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 5 *
5 * All rights reserved 6 * Permission to use, copy, modify, and distribute this software for any
6 * Functions for manipulating fifo buffers (that can grow if needed). 7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
7 * 9 *
8 * As far as I am concerned, the code I have written for this software 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * can be used freely for any purpose. Any derived versions of this 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * software must be clearly marked as such, and if the derived work is 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * incompatible with the protocol description in the RFC file, it must be 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * called by a name other than "ssh" or "Secure Shell". 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
13 */ 17 */
14 18
15#include "includes.h" 19/* Emulation wrappers for legacy OpenSSH buffer API atop sshbuf */
16 20
17#include <sys/param.h> 21#include <sys/types.h>
18 22
19#include <stdio.h>
20#include <string.h>
21#include <stdarg.h>
22#include <stdlib.h>
23
24#include "xmalloc.h"
25#include "buffer.h" 23#include "buffer.h"
26#include "log.h" 24#include "log.h"
27 25#include "ssherr.h"
28#define BUFFER_MAX_CHUNK 0x100000
29#define BUFFER_MAX_LEN 0xa00000
30#define BUFFER_ALLOCSZ 0x008000
31
32/* Initializes the buffer structure. */
33
34void
35buffer_init(Buffer *buffer)
36{
37 const u_int len = 4096;
38
39 buffer->alloc = 0;
40 buffer->buf = xmalloc(len);
41 buffer->alloc = len;
42 buffer->offset = 0;
43 buffer->end = 0;
44}
45
46/* Frees any memory used for the buffer. */
47
48void
49buffer_free(Buffer *buffer)
50{
51 if (buffer->alloc > 0) {
52 explicit_bzero(buffer->buf, buffer->alloc);
53 buffer->alloc = 0;
54 free(buffer->buf);
55 }
56}
57
58/*
59 * Clears any data from the buffer, making it empty. This does not actually
60 * zero the memory.
61 */
62
63void
64buffer_clear(Buffer *buffer)
65{
66 buffer->offset = 0;
67 buffer->end = 0;
68}
69
70/* Appends data to the buffer, expanding it if necessary. */
71 26
72void 27void
73buffer_append(Buffer *buffer, const void *data, u_int len) 28buffer_append(Buffer *buffer, const void *data, u_int len)
74{ 29{
75 void *p; 30 int ret;
76 p = buffer_append_space(buffer, len);
77 memcpy(p, data, len);
78}
79 31
80static int 32 if ((ret = sshbuf_put(buffer, data, len)) != 0)
81buffer_compact(Buffer *buffer) 33 fatal("%s: %s", __func__, ssh_err(ret));
82{
83 /*
84 * If the buffer is quite empty, but all data is at the end, move the
85 * data to the beginning.
86 */
87 if (buffer->offset > MIN(buffer->alloc, BUFFER_MAX_CHUNK)) {
88 memmove(buffer->buf, buffer->buf + buffer->offset,
89 buffer->end - buffer->offset);
90 buffer->end -= buffer->offset;
91 buffer->offset = 0;
92 return (1);
93 }
94 return (0);
95} 34}
96 35
97/*
98 * Appends space to the buffer, expanding the buffer if necessary. This does
99 * not actually copy the data into the buffer, but instead returns a pointer
100 * to the allocated region.
101 */
102
103void * 36void *
104buffer_append_space(Buffer *buffer, u_int len) 37buffer_append_space(Buffer *buffer, u_int len)
105{ 38{
106 u_int newlen; 39 int ret;
107 void *p; 40 u_char *p;
108 41
109 if (len > BUFFER_MAX_CHUNK) 42 if ((ret = sshbuf_reserve(buffer, len, &p)) != 0)
110 fatal("buffer_append_space: len %u not supported", len); 43 fatal("%s: %s", __func__, ssh_err(ret));
111 44 return p;
112 /* If the buffer is empty, start using it from the beginning. */
113 if (buffer->offset == buffer->end) {
114 buffer->offset = 0;
115 buffer->end = 0;
116 }
117restart:
118 /* If there is enough space to store all data, store it now. */
119 if (buffer->end + len < buffer->alloc) {
120 p = buffer->buf + buffer->end;
121 buffer->end += len;
122 return p;
123 }
124
125 /* Compact data back to the start of the buffer if necessary */
126 if (buffer_compact(buffer))
127 goto restart;
128
129 /* Increase the size of the buffer and retry. */
130 newlen = roundup(buffer->alloc + len, BUFFER_ALLOCSZ);
131 if (newlen > BUFFER_MAX_LEN)
132 fatal("buffer_append_space: alloc %u not supported",
133 newlen);
134 buffer->buf = xrealloc(buffer->buf, 1, newlen);
135 buffer->alloc = newlen;
136 goto restart;
137 /* NOTREACHED */
138} 45}
139 46
140/*
141 * Check whether an allocation of 'len' will fit in the buffer
142 * This must follow the same math as buffer_append_space
143 */
144int 47int
145buffer_check_alloc(Buffer *buffer, u_int len) 48buffer_check_alloc(Buffer *buffer, u_int len)
146{ 49{
147 if (buffer->offset == buffer->end) { 50 int ret = sshbuf_check_reserve(buffer, len);
148 buffer->offset = 0;
149 buffer->end = 0;
150 }
151 restart:
152 if (buffer->end + len < buffer->alloc)
153 return (1);
154 if (buffer_compact(buffer))
155 goto restart;
156 if (roundup(buffer->alloc + len, BUFFER_ALLOCSZ) <= BUFFER_MAX_LEN)
157 return (1);
158 return (0);
159}
160
161/* Returns the number of bytes of data in the buffer. */
162 51
163u_int 52 if (ret == 0)
164buffer_len(const Buffer *buffer) 53 return 1;
165{ 54 if (ret == SSH_ERR_NO_BUFFER_SPACE)
166 return buffer->end - buffer->offset; 55 return 0;
56 fatal("%s: %s", __func__, ssh_err(ret));
167} 57}
168 58
169/* Gets data from the beginning of the buffer. */
170
171int 59int
172buffer_get_ret(Buffer *buffer, void *buf, u_int len) 60buffer_get_ret(Buffer *buffer, void *buf, u_int len)
173{ 61{
174 if (len > buffer->end - buffer->offset) { 62 int ret;
175 error("buffer_get_ret: trying to get more bytes %d than in buffer %d", 63
176 len, buffer->end - buffer->offset); 64 if ((ret = sshbuf_get(buffer, buf, len)) != 0) {
177 return (-1); 65 error("%s: %s", __func__, ssh_err(ret));
66 return -1;
178 } 67 }
179 memcpy(buf, buffer->buf + buffer->offset, len); 68 return 0;
180 buffer->offset += len;
181 return (0);
182} 69}
183 70
184void 71void
185buffer_get(Buffer *buffer, void *buf, u_int len) 72buffer_get(Buffer *buffer, void *buf, u_int len)
186{ 73{
187 if (buffer_get_ret(buffer, buf, len) == -1) 74 if (buffer_get_ret(buffer, buf, len) == -1)
188 fatal("buffer_get: buffer error"); 75 fatal("%s: buffer error", __func__);
189} 76}
190 77
191/* Consumes the given number of bytes from the beginning of the buffer. */
192
193int 78int
194buffer_consume_ret(Buffer *buffer, u_int bytes) 79buffer_consume_ret(Buffer *buffer, u_int bytes)
195{ 80{
196 if (bytes > buffer->end - buffer->offset) { 81 int ret = sshbuf_consume(buffer, bytes);
197 error("buffer_consume_ret: trying to get more bytes than in buffer"); 82
198 return (-1); 83 if (ret == 0)
199 } 84 return 0;
200 buffer->offset += bytes; 85 if (ret == SSH_ERR_MESSAGE_INCOMPLETE)
201 return (0); 86 return -1;
87 fatal("%s: %s", __func__, ssh_err(ret));
202} 88}
203 89
204void 90void
205buffer_consume(Buffer *buffer, u_int bytes) 91buffer_consume(Buffer *buffer, u_int bytes)
206{ 92{
207 if (buffer_consume_ret(buffer, bytes) == -1) 93 if (buffer_consume_ret(buffer, bytes) == -1)
208 fatal("buffer_consume: buffer error"); 94 fatal("%s: buffer error", __func__);
209} 95}
210 96
211/* Consumes the given number of bytes from the end of the buffer. */
212
213int 97int
214buffer_consume_end_ret(Buffer *buffer, u_int bytes) 98buffer_consume_end_ret(Buffer *buffer, u_int bytes)
215{ 99{
216 if (bytes > buffer->end - buffer->offset) 100 int ret = sshbuf_consume_end(buffer, bytes);
217 return (-1); 101
218 buffer->end -= bytes; 102 if (ret == 0)
219 return (0); 103 return 0;
104 if (ret == SSH_ERR_MESSAGE_INCOMPLETE)
105 return -1;
106 fatal("%s: %s", __func__, ssh_err(ret));
220} 107}
221 108
222void 109void
223buffer_consume_end(Buffer *buffer, u_int bytes) 110buffer_consume_end(Buffer *buffer, u_int bytes)
224{ 111{
225 if (buffer_consume_end_ret(buffer, bytes) == -1) 112 if (buffer_consume_end_ret(buffer, bytes) == -1)
226 fatal("buffer_consume_end: trying to get more bytes than in buffer"); 113 fatal("%s: buffer error", __func__);
227}
228
229/* Returns a pointer to the first used byte in the buffer. */
230
231void *
232buffer_ptr(const Buffer *buffer)
233{
234 return buffer->buf + buffer->offset;
235} 114}
236 115
237/* Dumps the contents of the buffer to stderr. */
238 116
239void
240buffer_dump(const Buffer *buffer)
241{
242 u_int i;
243 u_char *ucp = buffer->buf;
244
245 for (i = buffer->offset; i < buffer->end; i++) {
246 fprintf(stderr, "%02x", ucp[i]);
247 if ((i-buffer->offset)%16==15)
248 fprintf(stderr, "\r\n");
249 else if ((i-buffer->offset)%2==1)
250 fprintf(stderr, " ");
251 }
252 fprintf(stderr, "\r\n");
253}