blob: 9e7c40a5a87422597aa5148427a199e3b3911f45 [file] [log] [blame]
Damien Miller0600c702013-11-21 13:55:43 +11001/* $OpenBSD: buffer.c,v 1.34 2013/11/08 11:15:19 dtucker Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Functions for manipulating fifo buffers (that can grow if needed).
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * 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".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100016
Damien Miller8dbffe72006-08-05 11:02:17 +100017#include <sys/param.h>
18
Damien Millera7a73ee2006-08-05 11:37:59 +100019#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100020#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100021#include <stdarg.h>
Damien Miller0600c702013-11-21 13:55:43 +110022#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100023
Damien Millerd4a8b7e1999-10-27 13:42:43 +100024#include "xmalloc.h"
25#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000026#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027
Damien Miller499a0d52006-04-23 12:06:03 +100028#define BUFFER_MAX_CHUNK 0x100000
29#define BUFFER_MAX_LEN 0xa00000
30#define BUFFER_ALLOCSZ 0x008000
31
Damien Millerd4a8b7e1999-10-27 13:42:43 +100032/* Initializes the buffer structure. */
33
Damien Miller4af51302000-04-16 11:18:38 +100034void
Damien Miller95def091999-11-25 00:26:21 +110035buffer_init(Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100036{
Damien Miller5efcecc2003-09-17 07:31:14 +100037 const u_int len = 4096;
38
39 buffer->alloc = 0;
40 buffer->buf = xmalloc(len);
41 buffer->alloc = len;
Damien Miller95def091999-11-25 00:26:21 +110042 buffer->offset = 0;
43 buffer->end = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044}
45
46/* Frees any memory used for the buffer. */
47
Damien Miller4af51302000-04-16 11:18:38 +100048void
Damien Miller95def091999-11-25 00:26:21 +110049buffer_free(Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050{
Damien Miller5efcecc2003-09-17 07:31:14 +100051 if (buffer->alloc > 0) {
52 memset(buffer->buf, 0, buffer->alloc);
Darren Tuckerd2730d32003-09-22 21:00:19 +100053 buffer->alloc = 0;
Darren Tuckera627d422013-06-02 07:31:17 +100054 free(buffer->buf);
Damien Miller5efcecc2003-09-17 07:31:14 +100055 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +100056}
57
Damien Miller5428f641999-11-25 11:54:57 +110058/*
59 * Clears any data from the buffer, making it empty. This does not actually
60 * zero the memory.
61 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062
Damien Miller4af51302000-04-16 11:18:38 +100063void
Damien Miller95def091999-11-25 00:26:21 +110064buffer_clear(Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100065{
Damien Miller95def091999-11-25 00:26:21 +110066 buffer->offset = 0;
67 buffer->end = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068}
69
70/* Appends data to the buffer, expanding it if necessary. */
71
Damien Miller4af51302000-04-16 11:18:38 +100072void
Damien Miller5a6b4fe2001-12-21 14:56:54 +110073buffer_append(Buffer *buffer, const void *data, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074{
Damien Miller5a6b4fe2001-12-21 14:56:54 +110075 void *p;
76 p = buffer_append_space(buffer, len);
77 memcpy(p, data, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078}
79
Damien Miller499a0d52006-04-23 12:06:03 +100080static int
81buffer_compact(Buffer *buffer)
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}
96
Damien Miller5428f641999-11-25 11:54:57 +110097/*
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 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000102
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100103void *
104buffer_append_space(Buffer *buffer, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105{
Ben Lindstromf2b4e4e2003-09-16 03:31:03 +0000106 u_int newlen;
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100107 void *p;
108
Darren Tucker11327cc2005-03-14 23:22:25 +1100109 if (len > BUFFER_MAX_CHUNK)
Damien Miller468cd712002-06-26 19:14:25 +1000110 fatal("buffer_append_space: len %u not supported", len);
111
Damien Miller95def091999-11-25 00:26:21 +1100112 /* 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) {
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100120 p = buffer->buf + buffer->end;
Damien Miller95def091999-11-25 00:26:21 +1100121 buffer->end += len;
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100122 return p;
Damien Miller95def091999-11-25 00:26:21 +1100123 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100124
Damien Miller499a0d52006-04-23 12:06:03 +1000125 /* 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);
Darren Tucker11327cc2005-03-14 23:22:25 +1100131 if (newlen > BUFFER_MAX_LEN)
Damien Miller468cd712002-06-26 19:14:25 +1000132 fatal("buffer_append_space: alloc %u not supported",
Ben Lindstromf2b4e4e2003-09-16 03:31:03 +0000133 newlen);
Damien Miller36812092006-03-26 14:22:47 +1100134 buffer->buf = xrealloc(buffer->buf, 1, newlen);
Ben Lindstromf2b4e4e2003-09-16 03:31:03 +0000135 buffer->alloc = newlen;
Damien Miller95def091999-11-25 00:26:21 +1100136 goto restart;
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100137 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000138}
139
Damien Miller499a0d52006-04-23 12:06:03 +1000140/*
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
145buffer_check_alloc(Buffer *buffer, u_int len)
146{
147 if (buffer->offset == buffer->end) {
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
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000161/* Returns the number of bytes of data in the buffer. */
162
Ben Lindstrom46c16222000-12-22 01:43:59 +0000163u_int
Damien Miller89221062010-02-12 09:23:40 +1100164buffer_len(const Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000165{
Damien Miller95def091999-11-25 00:26:21 +1100166 return buffer->end - buffer->offset;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000167}
168
169/* Gets data from the beginning of the buffer. */
170
Darren Tucker50dbe832004-11-05 20:41:24 +1100171int
172buffer_get_ret(Buffer *buffer, void *buf, u_int len)
173{
174 if (len > buffer->end - buffer->offset) {
175 error("buffer_get_ret: trying to get more bytes %d than in buffer %d",
176 len, buffer->end - buffer->offset);
177 return (-1);
178 }
179 memcpy(buf, buffer->buf + buffer->offset, len);
180 buffer->offset += len;
181 return (0);
182}
183
Damien Miller4af51302000-04-16 11:18:38 +1000184void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100185buffer_get(Buffer *buffer, void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000186{
Darren Tucker50dbe832004-11-05 20:41:24 +1100187 if (buffer_get_ret(buffer, buf, len) == -1)
188 fatal("buffer_get: buffer error");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000189}
190
191/* Consumes the given number of bytes from the beginning of the buffer. */
192
Darren Tucker50dbe832004-11-05 20:41:24 +1100193int
194buffer_consume_ret(Buffer *buffer, u_int bytes)
195{
196 if (bytes > buffer->end - buffer->offset) {
197 error("buffer_consume_ret: trying to get more bytes than in buffer");
198 return (-1);
199 }
200 buffer->offset += bytes;
201 return (0);
202}
203
Damien Miller4af51302000-04-16 11:18:38 +1000204void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000205buffer_consume(Buffer *buffer, u_int bytes)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000206{
Darren Tucker50dbe832004-11-05 20:41:24 +1100207 if (buffer_consume_ret(buffer, bytes) == -1)
208 fatal("buffer_consume: buffer error");
Damien Miller95def091999-11-25 00:26:21 +1100209}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000210
211/* Consumes the given number of bytes from the end of the buffer. */
212
Darren Tucker50dbe832004-11-05 20:41:24 +1100213int
214buffer_consume_end_ret(Buffer *buffer, u_int bytes)
215{
216 if (bytes > buffer->end - buffer->offset)
217 return (-1);
218 buffer->end -= bytes;
219 return (0);
220}
221
Damien Miller4af51302000-04-16 11:18:38 +1000222void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000223buffer_consume_end(Buffer *buffer, u_int bytes)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000224{
Darren Tucker50dbe832004-11-05 20:41:24 +1100225 if (buffer_consume_end_ret(buffer, bytes) == -1)
Damien Miller22c77262000-04-13 12:26:34 +1000226 fatal("buffer_consume_end: trying to get more bytes than in buffer");
Damien Miller95def091999-11-25 00:26:21 +1100227}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000228
229/* Returns a pointer to the first used byte in the buffer. */
230
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100231void *
Damien Miller89221062010-02-12 09:23:40 +1100232buffer_ptr(const Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000233{
Damien Miller95def091999-11-25 00:26:21 +1100234 return buffer->buf + buffer->offset;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000235}
236
237/* Dumps the contents of the buffer to stderr. */
238
Damien Miller4af51302000-04-16 11:18:38 +1000239void
Damien Miller89221062010-02-12 09:23:40 +1100240buffer_dump(const Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000241{
Darren Tucker631a4a92003-09-22 21:08:21 +1000242 u_int i;
Damien Miller4a8ed542002-01-22 23:33:31 +1100243 u_char *ucp = buffer->buf;
Damien Miller95def091999-11-25 00:26:21 +1100244
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000245 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 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000252 fprintf(stderr, "\r\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000253}