blob: 9217cb2695cf9ad68be4545b4d76ab9fced181d4 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11003 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * Functions for manipulating fifo buffers (that can grow if needed).
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Millere4340be2000-09-16 13:29:08 +11007 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110012 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100013
14#include "includes.h"
Damien Millera8e06ce2003-11-21 23:48:55 +110015RCSID("$OpenBSD: buffer.c,v 1.21 2003/11/21 11:57:03 djm Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100016
17#include "xmalloc.h"
18#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000019#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100020
21/* Initializes the buffer structure. */
22
Damien Miller4af51302000-04-16 11:18:38 +100023void
Damien Miller95def091999-11-25 00:26:21 +110024buffer_init(Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100025{
Damien Miller5efcecc2003-09-17 07:31:14 +100026 const u_int len = 4096;
27
28 buffer->alloc = 0;
29 buffer->buf = xmalloc(len);
30 buffer->alloc = len;
Damien Miller95def091999-11-25 00:26:21 +110031 buffer->offset = 0;
32 buffer->end = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100033}
34
35/* Frees any memory used for the buffer. */
36
Damien Miller4af51302000-04-16 11:18:38 +100037void
Damien Miller95def091999-11-25 00:26:21 +110038buffer_free(Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039{
Damien Miller5efcecc2003-09-17 07:31:14 +100040 if (buffer->alloc > 0) {
41 memset(buffer->buf, 0, buffer->alloc);
Darren Tuckerd2730d32003-09-22 21:00:19 +100042 buffer->alloc = 0;
Damien Miller5efcecc2003-09-17 07:31:14 +100043 xfree(buffer->buf);
44 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045}
46
Damien Miller5428f641999-11-25 11:54:57 +110047/*
48 * Clears any data from the buffer, making it empty. This does not actually
49 * zero the memory.
50 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051
Damien Miller4af51302000-04-16 11:18:38 +100052void
Damien Miller95def091999-11-25 00:26:21 +110053buffer_clear(Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054{
Damien Miller95def091999-11-25 00:26:21 +110055 buffer->offset = 0;
56 buffer->end = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100057}
58
59/* Appends data to the buffer, expanding it if necessary. */
60
Damien Miller4af51302000-04-16 11:18:38 +100061void
Damien Miller5a6b4fe2001-12-21 14:56:54 +110062buffer_append(Buffer *buffer, const void *data, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063{
Damien Miller5a6b4fe2001-12-21 14:56:54 +110064 void *p;
65 p = buffer_append_space(buffer, len);
66 memcpy(p, data, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067}
68
Damien Miller5428f641999-11-25 11:54:57 +110069/*
70 * Appends space to the buffer, expanding the buffer if necessary. This does
71 * not actually copy the data into the buffer, but instead returns a pointer
72 * to the allocated region.
73 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074
Damien Miller5a6b4fe2001-12-21 14:56:54 +110075void *
76buffer_append_space(Buffer *buffer, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100077{
Ben Lindstromf2b4e4e2003-09-16 03:31:03 +000078 u_int newlen;
Damien Miller5a6b4fe2001-12-21 14:56:54 +110079 void *p;
80
Damien Miller468cd712002-06-26 19:14:25 +100081 if (len > 0x100000)
82 fatal("buffer_append_space: len %u not supported", len);
83
Damien Miller95def091999-11-25 00:26:21 +110084 /* If the buffer is empty, start using it from the beginning. */
85 if (buffer->offset == buffer->end) {
86 buffer->offset = 0;
87 buffer->end = 0;
88 }
89restart:
90 /* If there is enough space to store all data, store it now. */
91 if (buffer->end + len < buffer->alloc) {
Damien Miller5a6b4fe2001-12-21 14:56:54 +110092 p = buffer->buf + buffer->end;
Damien Miller95def091999-11-25 00:26:21 +110093 buffer->end += len;
Damien Miller5a6b4fe2001-12-21 14:56:54 +110094 return p;
Damien Miller95def091999-11-25 00:26:21 +110095 }
Damien Miller5428f641999-11-25 11:54:57 +110096 /*
97 * If the buffer is quite empty, but all data is at the end, move the
98 * data to the beginning and retry.
99 */
Damien Miller95def091999-11-25 00:26:21 +1100100 if (buffer->offset > buffer->alloc / 2) {
101 memmove(buffer->buf, buffer->buf + buffer->offset,
102 buffer->end - buffer->offset);
103 buffer->end -= buffer->offset;
104 buffer->offset = 0;
105 goto restart;
106 }
107 /* Increase the size of the buffer and retry. */
Damien Miller787b2ec2003-11-21 23:56:47 +1100108
Ben Lindstromf2b4e4e2003-09-16 03:31:03 +0000109 newlen = buffer->alloc + len + 32768;
110 if (newlen > 0xa00000)
Damien Miller468cd712002-06-26 19:14:25 +1000111 fatal("buffer_append_space: alloc %u not supported",
Ben Lindstromf2b4e4e2003-09-16 03:31:03 +0000112 newlen);
113 buffer->buf = xrealloc(buffer->buf, newlen);
114 buffer->alloc = newlen;
Damien Miller95def091999-11-25 00:26:21 +1100115 goto restart;
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100116 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117}
118
119/* Returns the number of bytes of data in the buffer. */
120
Ben Lindstrom46c16222000-12-22 01:43:59 +0000121u_int
Damien Miller95def091999-11-25 00:26:21 +1100122buffer_len(Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123{
Damien Miller95def091999-11-25 00:26:21 +1100124 return buffer->end - buffer->offset;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125}
126
127/* Gets data from the beginning of the buffer. */
128
Damien Miller4af51302000-04-16 11:18:38 +1000129void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100130buffer_get(Buffer *buffer, void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000131{
Damien Miller95def091999-11-25 00:26:21 +1100132 if (len > buffer->end - buffer->offset)
Ben Lindstromd7dd23f2001-04-05 23:36:01 +0000133 fatal("buffer_get: trying to get more bytes %d than in buffer %d",
134 len, buffer->end - buffer->offset);
Damien Miller95def091999-11-25 00:26:21 +1100135 memcpy(buf, buffer->buf + buffer->offset, len);
136 buffer->offset += len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000137}
138
139/* Consumes the given number of bytes from the beginning of the buffer. */
140
Damien Miller4af51302000-04-16 11:18:38 +1000141void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000142buffer_consume(Buffer *buffer, u_int bytes)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143{
Damien Miller95def091999-11-25 00:26:21 +1100144 if (bytes > buffer->end - buffer->offset)
Damien Miller22c77262000-04-13 12:26:34 +1000145 fatal("buffer_consume: trying to get more bytes than in buffer");
Damien Miller95def091999-11-25 00:26:21 +1100146 buffer->offset += bytes;
147}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000148
149/* Consumes the given number of bytes from the end of the buffer. */
150
Damien Miller4af51302000-04-16 11:18:38 +1000151void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000152buffer_consume_end(Buffer *buffer, u_int bytes)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000153{
Damien Miller95def091999-11-25 00:26:21 +1100154 if (bytes > buffer->end - buffer->offset)
Damien Miller22c77262000-04-13 12:26:34 +1000155 fatal("buffer_consume_end: trying to get more bytes than in buffer");
Damien Miller95def091999-11-25 00:26:21 +1100156 buffer->end -= bytes;
157}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000158
159/* Returns a pointer to the first used byte in the buffer. */
160
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100161void *
Damien Miller95def091999-11-25 00:26:21 +1100162buffer_ptr(Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000163{
Damien Miller95def091999-11-25 00:26:21 +1100164 return buffer->buf + buffer->offset;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000165}
166
167/* Dumps the contents of the buffer to stderr. */
168
Damien Miller4af51302000-04-16 11:18:38 +1000169void
Damien Miller95def091999-11-25 00:26:21 +1100170buffer_dump(Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000171{
Darren Tucker631a4a92003-09-22 21:08:21 +1000172 u_int i;
Damien Miller4a8ed542002-01-22 23:33:31 +1100173 u_char *ucp = buffer->buf;
Damien Miller95def091999-11-25 00:26:21 +1100174
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000175 for (i = buffer->offset; i < buffer->end; i++) {
176 fprintf(stderr, "%02x", ucp[i]);
177 if ((i-buffer->offset)%16==15)
178 fprintf(stderr, "\r\n");
179 else if ((i-buffer->offset)%2==1)
180 fprintf(stderr, " ");
181 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000182 fprintf(stderr, "\r\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000183}