blob: db5ae0a2301109945c285fc51d59cda9216e6e60 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller4af51302000-04-16 11:18:38 +10002 *
Damien Miller95def091999-11-25 00:26:21 +11003 * buffer.c
Damien Miller4af51302000-04-16 11:18:38 +10004 *
Damien Miller95def091999-11-25 00:26:21 +11005 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Miller95def091999-11-25 00:26:21 +11007 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 * All rights reserved
Damien Miller4af51302000-04-16 11:18:38 +10009 *
Damien Miller95def091999-11-25 00:26:21 +110010 * Created: Sat Mar 18 04:15:33 1995 ylo
Damien Miller4af51302000-04-16 11:18:38 +100011 *
Damien Miller95def091999-11-25 00:26:21 +110012 * Functions for manipulating fifo buffers (that can grow if needed).
Damien Miller4af51302000-04-16 11:18:38 +100013 *
Damien Miller95def091999-11-25 00:26:21 +110014 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100015
16#include "includes.h"
Damien Miller6536c7d2000-06-22 21:32:31 +100017RCSID("$OpenBSD: buffer.c,v 1.7 2000/06/20 01:39:39 markus Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100018
19#include "xmalloc.h"
20#include "buffer.h"
21#include "ssh.h"
22
23/* Initializes the buffer structure. */
24
Damien Miller4af51302000-04-16 11:18:38 +100025void
Damien Miller95def091999-11-25 00:26:21 +110026buffer_init(Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027{
Damien Miller95def091999-11-25 00:26:21 +110028 buffer->alloc = 4096;
29 buffer->buf = xmalloc(buffer->alloc);
30 buffer->offset = 0;
31 buffer->end = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100032}
33
34/* Frees any memory used for the buffer. */
35
Damien Miller4af51302000-04-16 11:18:38 +100036void
Damien Miller95def091999-11-25 00:26:21 +110037buffer_free(Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100038{
Damien Miller95def091999-11-25 00:26:21 +110039 memset(buffer->buf, 0, buffer->alloc);
40 xfree(buffer->buf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041}
42
Damien Miller5428f641999-11-25 11:54:57 +110043/*
44 * Clears any data from the buffer, making it empty. This does not actually
45 * zero the memory.
46 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100047
Damien Miller4af51302000-04-16 11:18:38 +100048void
Damien Miller95def091999-11-25 00:26:21 +110049buffer_clear(Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050{
Damien Miller95def091999-11-25 00:26:21 +110051 buffer->offset = 0;
52 buffer->end = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053}
54
55/* Appends data to the buffer, expanding it if necessary. */
56
Damien Miller4af51302000-04-16 11:18:38 +100057void
Damien Miller95def091999-11-25 00:26:21 +110058buffer_append(Buffer *buffer, const char *data, unsigned int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059{
Damien Miller95def091999-11-25 00:26:21 +110060 char *cp;
61 buffer_append_space(buffer, &cp, len);
62 memcpy(cp, data, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063}
64
Damien Miller5428f641999-11-25 11:54:57 +110065/*
66 * Appends space to the buffer, expanding the buffer if necessary. This does
67 * not actually copy the data into the buffer, but instead returns a pointer
68 * to the allocated region.
69 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070
Damien Miller4af51302000-04-16 11:18:38 +100071void
Damien Miller95def091999-11-25 00:26:21 +110072buffer_append_space(Buffer *buffer, char **datap, unsigned int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100073{
Damien Miller95def091999-11-25 00:26:21 +110074 /* If the buffer is empty, start using it from the beginning. */
75 if (buffer->offset == buffer->end) {
76 buffer->offset = 0;
77 buffer->end = 0;
78 }
79restart:
80 /* If there is enough space to store all data, store it now. */
81 if (buffer->end + len < buffer->alloc) {
82 *datap = buffer->buf + buffer->end;
83 buffer->end += len;
84 return;
85 }
Damien Miller5428f641999-11-25 11:54:57 +110086 /*
87 * If the buffer is quite empty, but all data is at the end, move the
88 * data to the beginning and retry.
89 */
Damien Miller95def091999-11-25 00:26:21 +110090 if (buffer->offset > buffer->alloc / 2) {
91 memmove(buffer->buf, buffer->buf + buffer->offset,
92 buffer->end - buffer->offset);
93 buffer->end -= buffer->offset;
94 buffer->offset = 0;
95 goto restart;
96 }
97 /* Increase the size of the buffer and retry. */
98 buffer->alloc += len + 32768;
99 buffer->buf = xrealloc(buffer->buf, buffer->alloc);
100 goto restart;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000101}
102
103/* Returns the number of bytes of data in the buffer. */
104
Damien Miller4af51302000-04-16 11:18:38 +1000105unsigned int
Damien Miller95def091999-11-25 00:26:21 +1100106buffer_len(Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107{
Damien Miller95def091999-11-25 00:26:21 +1100108 return buffer->end - buffer->offset;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000109}
110
111/* Gets data from the beginning of the buffer. */
112
Damien Miller4af51302000-04-16 11:18:38 +1000113void
Damien Miller95def091999-11-25 00:26:21 +1100114buffer_get(Buffer *buffer, char *buf, unsigned int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000115{
Damien Miller95def091999-11-25 00:26:21 +1100116 if (len > buffer->end - buffer->offset)
Damien Miller22c77262000-04-13 12:26:34 +1000117 fatal("buffer_get: trying to get more bytes than in buffer");
Damien Miller95def091999-11-25 00:26:21 +1100118 memcpy(buf, buffer->buf + buffer->offset, len);
119 buffer->offset += len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120}
121
122/* Consumes the given number of bytes from the beginning of the buffer. */
123
Damien Miller4af51302000-04-16 11:18:38 +1000124void
Damien Miller95def091999-11-25 00:26:21 +1100125buffer_consume(Buffer *buffer, unsigned int bytes)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126{
Damien Miller95def091999-11-25 00:26:21 +1100127 if (bytes > buffer->end - buffer->offset)
Damien Miller22c77262000-04-13 12:26:34 +1000128 fatal("buffer_consume: trying to get more bytes than in buffer");
Damien Miller95def091999-11-25 00:26:21 +1100129 buffer->offset += bytes;
130}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000131
132/* Consumes the given number of bytes from the end of the buffer. */
133
Damien Miller4af51302000-04-16 11:18:38 +1000134void
Damien Miller95def091999-11-25 00:26:21 +1100135buffer_consume_end(Buffer *buffer, unsigned int bytes)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000136{
Damien Miller95def091999-11-25 00:26:21 +1100137 if (bytes > buffer->end - buffer->offset)
Damien Miller22c77262000-04-13 12:26:34 +1000138 fatal("buffer_consume_end: trying to get more bytes than in buffer");
Damien Miller95def091999-11-25 00:26:21 +1100139 buffer->end -= bytes;
140}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141
142/* Returns a pointer to the first used byte in the buffer. */
143
Damien Miller95def091999-11-25 00:26:21 +1100144char *
145buffer_ptr(Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000146{
Damien Miller95def091999-11-25 00:26:21 +1100147 return buffer->buf + buffer->offset;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000148}
149
150/* Dumps the contents of the buffer to stderr. */
151
Damien Miller4af51302000-04-16 11:18:38 +1000152void
Damien Miller95def091999-11-25 00:26:21 +1100153buffer_dump(Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000154{
Damien Miller95def091999-11-25 00:26:21 +1100155 int i;
156 unsigned char *ucp = (unsigned char *) buffer->buf;
157
158 for (i = buffer->offset; i < buffer->end; i++)
159 fprintf(stderr, " %02x", ucp[i]);
160 fprintf(stderr, "\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000161}