1264377Sdes/* $OpenBSD: buffer.c,v 1.35 2014/02/02 03:44:31 djm Exp $ */
2224638Sbrooks/* $FreeBSD$ */
357429Smarkm/*
457429Smarkm * Author: Tatu Ylonen <ylo@cs.hut.fi>
557429Smarkm * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
657429Smarkm *                    All rights reserved
757429Smarkm * Functions for manipulating fifo buffers (that can grow if needed).
860573Skris *
965668Skris * As far as I am concerned, the code I have written for this software
1065668Skris * can be used freely for any purpose.  Any derived versions of this
1165668Skris * software must be clearly marked as such, and if the derived work is
1265668Skris * incompatible with the protocol description in the RFC file, it must be
1365668Skris * called by a name other than "ssh" or "Secure Shell".
1457429Smarkm */
1557429Smarkm
1657429Smarkm#include "includes.h"
1757429Smarkm
18162852Sdes#include <sys/param.h>
19162852Sdes
20162852Sdes#include <stdio.h>
21162852Sdes#include <string.h>
22162852Sdes#include <stdarg.h>
23262566Sdes#include <stdlib.h>
24162852Sdes
2557429Smarkm#include "xmalloc.h"
2657429Smarkm#include "buffer.h"
2776259Sgreen#include "log.h"
2857429Smarkm
29162852Sdes#define	BUFFER_MAX_CHUNK	0x100000
30224638Sbrooks#define	BUFFER_MAX_LEN		0x4000000	/* 64MB */
31162852Sdes#define	BUFFER_ALLOCSZ		0x008000
32162852Sdes
3357429Smarkm/* Initializes the buffer structure. */
3457429Smarkm
3560573Skrisvoid
3657429Smarkmbuffer_init(Buffer *buffer)
3757429Smarkm{
38120489Sjoe	const u_int len = 4096;
39120489Sjoe
40120489Sjoe	buffer->alloc = 0;
41120489Sjoe	buffer->buf = xmalloc(len);
42120489Sjoe	buffer->alloc = len;
4357429Smarkm	buffer->offset = 0;
4457429Smarkm	buffer->end = 0;
4557429Smarkm}
4657429Smarkm
4757429Smarkm/* Frees any memory used for the buffer. */
4857429Smarkm
4960573Skrisvoid
5057429Smarkmbuffer_free(Buffer *buffer)
5157429Smarkm{
52120489Sjoe	if (buffer->alloc > 0) {
53264377Sdes		explicit_bzero(buffer->buf, buffer->alloc);
54124208Sdes		buffer->alloc = 0;
55255767Sdes		free(buffer->buf);
56120489Sjoe	}
5757429Smarkm}
5857429Smarkm
5957429Smarkm/*
6057429Smarkm * Clears any data from the buffer, making it empty.  This does not actually
6157429Smarkm * zero the memory.
6257429Smarkm */
6357429Smarkm
6460573Skrisvoid
6557429Smarkmbuffer_clear(Buffer *buffer)
6657429Smarkm{
6757429Smarkm	buffer->offset = 0;
6857429Smarkm	buffer->end = 0;
6957429Smarkm}
7057429Smarkm
7157429Smarkm/* Appends data to the buffer, expanding it if necessary. */
7257429Smarkm
7360573Skrisvoid
7492555Sdesbuffer_append(Buffer *buffer, const void *data, u_int len)
7557429Smarkm{
7692555Sdes	void *p;
7792555Sdes	p = buffer_append_space(buffer, len);
7892555Sdes	memcpy(p, data, len);
7957429Smarkm}
8057429Smarkm
81162852Sdesstatic int
82162852Sdesbuffer_compact(Buffer *buffer)
83162852Sdes{
84162852Sdes	/*
85162852Sdes	 * If the buffer is quite empty, but all data is at the end, move the
86162852Sdes	 * data to the beginning.
87162852Sdes	 */
88162852Sdes	if (buffer->offset > MIN(buffer->alloc, BUFFER_MAX_CHUNK)) {
89162852Sdes		memmove(buffer->buf, buffer->buf + buffer->offset,
90162852Sdes			buffer->end - buffer->offset);
91162852Sdes		buffer->end -= buffer->offset;
92162852Sdes		buffer->offset = 0;
93162852Sdes		return (1);
94162852Sdes	}
95162852Sdes	return (0);
96162852Sdes}
97162852Sdes
9857429Smarkm/*
9957429Smarkm * Appends space to the buffer, expanding the buffer if necessary. This does
10057429Smarkm * not actually copy the data into the buffer, but instead returns a pointer
10157429Smarkm * to the allocated region.
10257429Smarkm */
10357429Smarkm
10492555Sdesvoid *
10592555Sdesbuffer_append_space(Buffer *buffer, u_int len)
10657429Smarkm{
107120113Snectar	u_int newlen;
10892555Sdes	void *p;
10992555Sdes
110147001Sdes	if (len > BUFFER_MAX_CHUNK)
11199060Sdes		fatal("buffer_append_space: len %u not supported", len);
11299060Sdes
11357429Smarkm	/* If the buffer is empty, start using it from the beginning. */
11457429Smarkm	if (buffer->offset == buffer->end) {
11557429Smarkm		buffer->offset = 0;
11657429Smarkm		buffer->end = 0;
11757429Smarkm	}
11857429Smarkmrestart:
11957429Smarkm	/* If there is enough space to store all data, store it now. */
12057429Smarkm	if (buffer->end + len < buffer->alloc) {
12192555Sdes		p = buffer->buf + buffer->end;
12257429Smarkm		buffer->end += len;
12392555Sdes		return p;
12457429Smarkm	}
125162852Sdes
126162852Sdes	/* Compact data back to the start of the buffer if necessary */
127162852Sdes	if (buffer_compact(buffer))
12857429Smarkm		goto restart;
129162852Sdes
13057429Smarkm	/* Increase the size of the buffer and retry. */
131162852Sdes	newlen = roundup(buffer->alloc + len, BUFFER_ALLOCSZ);
132147001Sdes	if (newlen > BUFFER_MAX_LEN)
13399060Sdes		fatal("buffer_append_space: alloc %u not supported",
134120113Snectar		    newlen);
135162852Sdes	buffer->buf = xrealloc(buffer->buf, 1, newlen);
136120113Snectar	buffer->alloc = newlen;
13757429Smarkm	goto restart;
13892555Sdes	/* NOTREACHED */
13957429Smarkm}
14057429Smarkm
141162852Sdes/*
142162852Sdes * Check whether an allocation of 'len' will fit in the buffer
143162852Sdes * This must follow the same math as buffer_append_space
144162852Sdes */
145162852Sdesint
146162852Sdesbuffer_check_alloc(Buffer *buffer, u_int len)
147162852Sdes{
148162852Sdes	if (buffer->offset == buffer->end) {
149162852Sdes		buffer->offset = 0;
150162852Sdes		buffer->end = 0;
151162852Sdes	}
152162852Sdes restart:
153162852Sdes	if (buffer->end + len < buffer->alloc)
154162852Sdes		return (1);
155162852Sdes	if (buffer_compact(buffer))
156162852Sdes		goto restart;
157162852Sdes	if (roundup(buffer->alloc + len, BUFFER_ALLOCSZ) <= BUFFER_MAX_LEN)
158162852Sdes		return (1);
159162852Sdes	return (0);
160162852Sdes}
161162852Sdes
16257429Smarkm/* Returns the number of bytes of data in the buffer. */
16357429Smarkm
16476259Sgreenu_int
165204917Sdesbuffer_len(const Buffer *buffer)
16657429Smarkm{
16757429Smarkm	return buffer->end - buffer->offset;
16857429Smarkm}
16957429Smarkm
170224638Sbrooks/* Returns the maximum number of bytes of data that may be in the buffer. */
171224638Sbrooksu_int
172224638Sbrooksbuffer_get_max_len(void)
173224638Sbrooks{
174224638Sbrooks	return (BUFFER_MAX_LEN);
175224638Sbrooks}
176224638Sbrooks
17757429Smarkm/* Gets data from the beginning of the buffer. */
17857429Smarkm
179146998Sdesint
180146998Sdesbuffer_get_ret(Buffer *buffer, void *buf, u_int len)
18157429Smarkm{
182146998Sdes	if (len > buffer->end - buffer->offset) {
183146998Sdes		error("buffer_get_ret: trying to get more bytes %d than in buffer %d",
18476259Sgreen		    len, buffer->end - buffer->offset);
185146998Sdes		return (-1);
186146998Sdes	}
18757429Smarkm	memcpy(buf, buffer->buf + buffer->offset, len);
18857429Smarkm	buffer->offset += len;
189146998Sdes	return (0);
19057429Smarkm}
19157429Smarkm
192146998Sdesvoid
193146998Sdesbuffer_get(Buffer *buffer, void *buf, u_int len)
194146998Sdes{
195146998Sdes	if (buffer_get_ret(buffer, buf, len) == -1)
196146998Sdes		fatal("buffer_get: buffer error");
197146998Sdes}
198146998Sdes
19957429Smarkm/* Consumes the given number of bytes from the beginning of the buffer. */
20057429Smarkm
201146998Sdesint
202146998Sdesbuffer_consume_ret(Buffer *buffer, u_int bytes)
203146998Sdes{
204146998Sdes	if (bytes > buffer->end - buffer->offset) {
205146998Sdes		error("buffer_consume_ret: trying to get more bytes than in buffer");
206146998Sdes		return (-1);
207146998Sdes	}
208146998Sdes	buffer->offset += bytes;
209146998Sdes	return (0);
210146998Sdes}
211146998Sdes
21260573Skrisvoid
21376259Sgreenbuffer_consume(Buffer *buffer, u_int bytes)
21457429Smarkm{
215146998Sdes	if (buffer_consume_ret(buffer, bytes) == -1)
216146998Sdes		fatal("buffer_consume: buffer error");
21757429Smarkm}
21857429Smarkm
21957429Smarkm/* Consumes the given number of bytes from the end of the buffer. */
22057429Smarkm
221146998Sdesint
222146998Sdesbuffer_consume_end_ret(Buffer *buffer, u_int bytes)
223146998Sdes{
224146998Sdes	if (bytes > buffer->end - buffer->offset)
225146998Sdes		return (-1);
226146998Sdes	buffer->end -= bytes;
227146998Sdes	return (0);
228146998Sdes}
229146998Sdes
23060573Skrisvoid
23176259Sgreenbuffer_consume_end(Buffer *buffer, u_int bytes)
23257429Smarkm{
233146998Sdes	if (buffer_consume_end_ret(buffer, bytes) == -1)
23460573Skris		fatal("buffer_consume_end: trying to get more bytes than in buffer");
23557429Smarkm}
23657429Smarkm
23757429Smarkm/* Returns a pointer to the first used byte in the buffer. */
23857429Smarkm
23992555Sdesvoid *
240204917Sdesbuffer_ptr(const Buffer *buffer)
24157429Smarkm{
24257429Smarkm	return buffer->buf + buffer->offset;
24357429Smarkm}
24457429Smarkm
24557429Smarkm/* Dumps the contents of the buffer to stderr. */
24657429Smarkm
24760573Skrisvoid
248204917Sdesbuffer_dump(const Buffer *buffer)
24957429Smarkm{
250126274Sdes	u_int i;
25192555Sdes	u_char *ucp = buffer->buf;
25257429Smarkm
25376259Sgreen	for (i = buffer->offset; i < buffer->end; i++) {
25476259Sgreen		fprintf(stderr, "%02x", ucp[i]);
25576259Sgreen		if ((i-buffer->offset)%16==15)
25676259Sgreen			fprintf(stderr, "\r\n");
25776259Sgreen		else if ((i-buffer->offset)%2==1)
25876259Sgreen			fprintf(stderr, " ");
25976259Sgreen	}
26076259Sgreen	fprintf(stderr, "\r\n");
26157429Smarkm}
262