blob: 007e7f94e31e8a2e9234c300c34a95fa6f0346e1 [file] [log] [blame]
Darren Tuckera627d422013-06-02 07:31:17 +10001/* $OpenBSD: buffer.c,v 1.33 2013/05/17 00:13:13 djm 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 Millere3476ed2006-07-24 14:13:33 +100022
Damien Millerd4a8b7e1999-10-27 13:42:43 +100023#include "xmalloc.h"
24#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000025#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100026
Damien Miller499a0d52006-04-23 12:06:03 +100027#define BUFFER_MAX_CHUNK 0x100000
28#define BUFFER_MAX_LEN 0xa00000
29#define BUFFER_ALLOCSZ 0x008000
30
Damien Millerd4a8b7e1999-10-27 13:42:43 +100031/* Initializes the buffer structure. */
32
Damien Miller4af51302000-04-16 11:18:38 +100033void
Damien Miller95def091999-11-25 00:26:21 +110034buffer_init(Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100035{
Damien Miller5efcecc2003-09-17 07:31:14 +100036 const u_int len = 4096;
37
38 buffer->alloc = 0;
39 buffer->buf = xmalloc(len);
40 buffer->alloc = len;
Damien Miller95def091999-11-25 00:26:21 +110041 buffer->offset = 0;
42 buffer->end = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100043}
44
45/* Frees any memory used for the buffer. */
46
Damien Miller4af51302000-04-16 11:18:38 +100047void
Damien Miller95def091999-11-25 00:26:21 +110048buffer_free(Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100049{
Damien Miller5efcecc2003-09-17 07:31:14 +100050 if (buffer->alloc > 0) {
51 memset(buffer->buf, 0, buffer->alloc);
Darren Tuckerd2730d32003-09-22 21:00:19 +100052 buffer->alloc = 0;
Darren Tuckera627d422013-06-02 07:31:17 +100053 free(buffer->buf);
Damien Miller5efcecc2003-09-17 07:31:14 +100054 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +100055}
56
Damien Miller5428f641999-11-25 11:54:57 +110057/*
58 * Clears any data from the buffer, making it empty. This does not actually
59 * zero the memory.
60 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061
Damien Miller4af51302000-04-16 11:18:38 +100062void
Damien Miller95def091999-11-25 00:26:21 +110063buffer_clear(Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064{
Damien Miller95def091999-11-25 00:26:21 +110065 buffer->offset = 0;
66 buffer->end = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067}
68
69/* Appends data to the buffer, expanding it if necessary. */
70
Damien Miller4af51302000-04-16 11:18:38 +100071void
Damien Miller5a6b4fe2001-12-21 14:56:54 +110072buffer_append(Buffer *buffer, const void *data, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100073{
Damien Miller5a6b4fe2001-12-21 14:56:54 +110074 void *p;
75 p = buffer_append_space(buffer, len);
76 memcpy(p, data, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100077}
78
Damien Miller499a0d52006-04-23 12:06:03 +100079static 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
Damien Miller5428f641999-11-25 11:54:57 +110096/*
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 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000101
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100102void *
103buffer_append_space(Buffer *buffer, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104{
Ben Lindstromf2b4e4e2003-09-16 03:31:03 +0000105 u_int newlen;
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100106 void *p;
107
Darren Tucker11327cc2005-03-14 23:22:25 +1100108 if (len > BUFFER_MAX_CHUNK)
Damien Miller468cd712002-06-26 19:14:25 +1000109 fatal("buffer_append_space: len %u not supported", len);
110
Damien Miller95def091999-11-25 00:26:21 +1100111 /* 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) {
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100119 p = buffer->buf + buffer->end;
Damien Miller95def091999-11-25 00:26:21 +1100120 buffer->end += len;
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100121 return p;
Damien Miller95def091999-11-25 00:26:21 +1100122 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100123
Damien Miller499a0d52006-04-23 12:06:03 +1000124 /* Compact data back to the start of the buffer if necessary */
125 if (buffer_compact(buffer))
126 goto restart;
127
128 /* Increase the size of the buffer and retry. */
129 newlen = roundup(buffer->alloc + len, BUFFER_ALLOCSZ);
Darren Tucker11327cc2005-03-14 23:22:25 +1100130 if (newlen > BUFFER_MAX_LEN)
Damien Miller468cd712002-06-26 19:14:25 +1000131 fatal("buffer_append_space: alloc %u not supported",
Ben Lindstromf2b4e4e2003-09-16 03:31:03 +0000132 newlen);
Damien Miller36812092006-03-26 14:22:47 +1100133 buffer->buf = xrealloc(buffer->buf, 1, newlen);
Ben Lindstromf2b4e4e2003-09-16 03:31:03 +0000134 buffer->alloc = newlen;
Damien Miller95def091999-11-25 00:26:21 +1100135 goto restart;
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100136 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000137}
138
Damien Miller499a0d52006-04-23 12:06:03 +1000139/*
140 * Check whether an allocation of 'len' will fit in the buffer
141 * This must follow the same math as buffer_append_space
142 */
143int
144buffer_check_alloc(Buffer *buffer, u_int len)
145{
146 if (buffer->offset == buffer->end) {
147 buffer->offset = 0;
148 buffer->end = 0;
149 }
150 restart:
151 if (buffer->end + len < buffer->alloc)
152 return (1);
153 if (buffer_compact(buffer))
154 goto restart;
155 if (roundup(buffer->alloc + len, BUFFER_ALLOCSZ) <= BUFFER_MAX_LEN)
156 return (1);
157 return (0);
158}
159
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000160/* Returns the number of bytes of data in the buffer. */
161
Ben Lindstrom46c16222000-12-22 01:43:59 +0000162u_int
Damien Miller89221062010-02-12 09:23:40 +1100163buffer_len(const Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000164{
Damien Miller95def091999-11-25 00:26:21 +1100165 return buffer->end - buffer->offset;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000166}
167
168/* Gets data from the beginning of the buffer. */
169
Darren Tucker50dbe832004-11-05 20:41:24 +1100170int
171buffer_get_ret(Buffer *buffer, void *buf, u_int len)
172{
173 if (len > buffer->end - buffer->offset) {
174 error("buffer_get_ret: trying to get more bytes %d than in buffer %d",
175 len, buffer->end - buffer->offset);
176 return (-1);
177 }
178 memcpy(buf, buffer->buf + buffer->offset, len);
179 buffer->offset += len;
180 return (0);
181}
182
Damien Miller4af51302000-04-16 11:18:38 +1000183void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100184buffer_get(Buffer *buffer, void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000185{
Darren Tucker50dbe832004-11-05 20:41:24 +1100186 if (buffer_get_ret(buffer, buf, len) == -1)
187 fatal("buffer_get: buffer error");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000188}
189
190/* Consumes the given number of bytes from the beginning of the buffer. */
191
Darren Tucker50dbe832004-11-05 20:41:24 +1100192int
193buffer_consume_ret(Buffer *buffer, u_int bytes)
194{
195 if (bytes > buffer->end - buffer->offset) {
196 error("buffer_consume_ret: trying to get more bytes than in buffer");
197 return (-1);
198 }
199 buffer->offset += bytes;
200 return (0);
201}
202
Damien Miller4af51302000-04-16 11:18:38 +1000203void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000204buffer_consume(Buffer *buffer, u_int bytes)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000205{
Darren Tucker50dbe832004-11-05 20:41:24 +1100206 if (buffer_consume_ret(buffer, bytes) == -1)
207 fatal("buffer_consume: buffer error");
Damien Miller95def091999-11-25 00:26:21 +1100208}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000209
210/* Consumes the given number of bytes from the end of the buffer. */
211
Darren Tucker50dbe832004-11-05 20:41:24 +1100212int
213buffer_consume_end_ret(Buffer *buffer, u_int bytes)
214{
215 if (bytes > buffer->end - buffer->offset)
216 return (-1);
217 buffer->end -= bytes;
218 return (0);
219}
220
Damien Miller4af51302000-04-16 11:18:38 +1000221void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000222buffer_consume_end(Buffer *buffer, u_int bytes)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000223{
Darren Tucker50dbe832004-11-05 20:41:24 +1100224 if (buffer_consume_end_ret(buffer, bytes) == -1)
Damien Miller22c77262000-04-13 12:26:34 +1000225 fatal("buffer_consume_end: trying to get more bytes than in buffer");
Damien Miller95def091999-11-25 00:26:21 +1100226}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000227
228/* Returns a pointer to the first used byte in the buffer. */
229
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100230void *
Damien Miller89221062010-02-12 09:23:40 +1100231buffer_ptr(const Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000232{
Damien Miller95def091999-11-25 00:26:21 +1100233 return buffer->buf + buffer->offset;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000234}
235
236/* Dumps the contents of the buffer to stderr. */
237
Damien Miller4af51302000-04-16 11:18:38 +1000238void
Damien Miller89221062010-02-12 09:23:40 +1100239buffer_dump(const Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000240{
Darren Tucker631a4a92003-09-22 21:08:21 +1000241 u_int i;
Damien Miller4a8ed542002-01-22 23:33:31 +1100242 u_char *ucp = buffer->buf;
Damien Miller95def091999-11-25 00:26:21 +1100243
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000244 for (i = buffer->offset; i < buffer->end; i++) {
245 fprintf(stderr, "%02x", ucp[i]);
246 if ((i-buffer->offset)%16==15)
247 fprintf(stderr, "\r\n");
248 else if ((i-buffer->offset)%2==1)
249 fprintf(stderr, " ");
250 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000251 fprintf(stderr, "\r\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000252}