blob: ad04b267e0d8c2493860a7c5fd1e067a954c767a [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 Miller468cd712002-06-26 19:14:25 +100015RCSID("$OpenBSD: buffer.c,v 1.16 2002/06/26 08:54:18 markus 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 Miller95def091999-11-25 00:26:21 +110026 buffer->alloc = 4096;
27 buffer->buf = xmalloc(buffer->alloc);
28 buffer->offset = 0;
29 buffer->end = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100030}
31
32/* Frees any memory used for the buffer. */
33
Damien Miller4af51302000-04-16 11:18:38 +100034void
Damien Miller95def091999-11-25 00:26:21 +110035buffer_free(Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100036{
Damien Miller95def091999-11-25 00:26:21 +110037 memset(buffer->buf, 0, buffer->alloc);
38 xfree(buffer->buf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039}
40
Damien Miller5428f641999-11-25 11:54:57 +110041/*
42 * Clears any data from the buffer, making it empty. This does not actually
43 * zero the memory.
44 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045
Damien Miller4af51302000-04-16 11:18:38 +100046void
Damien Miller95def091999-11-25 00:26:21 +110047buffer_clear(Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100048{
Damien Miller95def091999-11-25 00:26:21 +110049 buffer->offset = 0;
50 buffer->end = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051}
52
53/* Appends data to the buffer, expanding it if necessary. */
54
Damien Miller4af51302000-04-16 11:18:38 +100055void
Damien Miller5a6b4fe2001-12-21 14:56:54 +110056buffer_append(Buffer *buffer, const void *data, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100057{
Damien Miller5a6b4fe2001-12-21 14:56:54 +110058 void *p;
59 p = buffer_append_space(buffer, len);
60 memcpy(p, data, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061}
62
Damien Miller5428f641999-11-25 11:54:57 +110063/*
64 * Appends space to the buffer, expanding the buffer if necessary. This does
65 * not actually copy the data into the buffer, but instead returns a pointer
66 * to the allocated region.
67 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068
Damien Miller5a6b4fe2001-12-21 14:56:54 +110069void *
70buffer_append_space(Buffer *buffer, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071{
Damien Miller5a6b4fe2001-12-21 14:56:54 +110072 void *p;
73
Damien Miller468cd712002-06-26 19:14:25 +100074 if (len > 0x100000)
75 fatal("buffer_append_space: len %u not supported", len);
76
Damien Miller95def091999-11-25 00:26:21 +110077 /* If the buffer is empty, start using it from the beginning. */
78 if (buffer->offset == buffer->end) {
79 buffer->offset = 0;
80 buffer->end = 0;
81 }
82restart:
83 /* If there is enough space to store all data, store it now. */
84 if (buffer->end + len < buffer->alloc) {
Damien Miller5a6b4fe2001-12-21 14:56:54 +110085 p = buffer->buf + buffer->end;
Damien Miller95def091999-11-25 00:26:21 +110086 buffer->end += len;
Damien Miller5a6b4fe2001-12-21 14:56:54 +110087 return p;
Damien Miller95def091999-11-25 00:26:21 +110088 }
Damien Miller5428f641999-11-25 11:54:57 +110089 /*
90 * If the buffer is quite empty, but all data is at the end, move the
91 * data to the beginning and retry.
92 */
Damien Miller95def091999-11-25 00:26:21 +110093 if (buffer->offset > buffer->alloc / 2) {
94 memmove(buffer->buf, buffer->buf + buffer->offset,
95 buffer->end - buffer->offset);
96 buffer->end -= buffer->offset;
97 buffer->offset = 0;
98 goto restart;
99 }
100 /* Increase the size of the buffer and retry. */
101 buffer->alloc += len + 32768;
Damien Miller468cd712002-06-26 19:14:25 +1000102 if (buffer->alloc > 0xa00000)
103 fatal("buffer_append_space: alloc %u not supported",
104 buffer->alloc);
Damien Miller95def091999-11-25 00:26:21 +1100105 buffer->buf = xrealloc(buffer->buf, buffer->alloc);
106 goto restart;
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100107 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108}
109
110/* Returns the number of bytes of data in the buffer. */
111
Ben Lindstrom46c16222000-12-22 01:43:59 +0000112u_int
Damien Miller95def091999-11-25 00:26:21 +1100113buffer_len(Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114{
Damien Miller95def091999-11-25 00:26:21 +1100115 return buffer->end - buffer->offset;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116}
117
118/* Gets data from the beginning of the buffer. */
119
Damien Miller4af51302000-04-16 11:18:38 +1000120void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100121buffer_get(Buffer *buffer, void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122{
Damien Miller95def091999-11-25 00:26:21 +1100123 if (len > buffer->end - buffer->offset)
Ben Lindstromd7dd23f2001-04-05 23:36:01 +0000124 fatal("buffer_get: trying to get more bytes %d than in buffer %d",
125 len, buffer->end - buffer->offset);
Damien Miller95def091999-11-25 00:26:21 +1100126 memcpy(buf, buffer->buf + buffer->offset, len);
127 buffer->offset += len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128}
129
130/* Consumes the given number of bytes from the beginning of the buffer. */
131
Damien Miller4af51302000-04-16 11:18:38 +1000132void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000133buffer_consume(Buffer *buffer, u_int bytes)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000134{
Damien Miller95def091999-11-25 00:26:21 +1100135 if (bytes > buffer->end - buffer->offset)
Damien Miller22c77262000-04-13 12:26:34 +1000136 fatal("buffer_consume: trying to get more bytes than in buffer");
Damien Miller95def091999-11-25 00:26:21 +1100137 buffer->offset += bytes;
138}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139
140/* Consumes the given number of bytes from the end of the buffer. */
141
Damien Miller4af51302000-04-16 11:18:38 +1000142void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000143buffer_consume_end(Buffer *buffer, u_int bytes)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144{
Damien Miller95def091999-11-25 00:26:21 +1100145 if (bytes > buffer->end - buffer->offset)
Damien Miller22c77262000-04-13 12:26:34 +1000146 fatal("buffer_consume_end: trying to get more bytes than in buffer");
Damien Miller95def091999-11-25 00:26:21 +1100147 buffer->end -= bytes;
148}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000149
150/* Returns a pointer to the first used byte in the buffer. */
151
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100152void *
Damien Miller95def091999-11-25 00:26:21 +1100153buffer_ptr(Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000154{
Damien Miller95def091999-11-25 00:26:21 +1100155 return buffer->buf + buffer->offset;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000156}
157
158/* Dumps the contents of the buffer to stderr. */
159
Damien Miller4af51302000-04-16 11:18:38 +1000160void
Damien Miller95def091999-11-25 00:26:21 +1100161buffer_dump(Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000162{
Damien Miller95def091999-11-25 00:26:21 +1100163 int i;
Damien Miller4a8ed542002-01-22 23:33:31 +1100164 u_char *ucp = buffer->buf;
Damien Miller95def091999-11-25 00:26:21 +1100165
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000166 for (i = buffer->offset; i < buffer->end; i++) {
167 fprintf(stderr, "%02x", ucp[i]);
168 if ((i-buffer->offset)%16==15)
169 fprintf(stderr, "\r\n");
170 else if ((i-buffer->offset)%2==1)
171 fprintf(stderr, " ");
172 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000173 fprintf(stderr, "\r\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000174}