]> andersk Git - openssh.git/blame - buffer.c
- (stevesk) OpenBSD CVS updates:
[openssh.git] / buffer.c
CommitLineData
8efc0c15 1/*
5260325f 2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
5260325f 3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
5260325f 5 * Functions for manipulating fifo buffers (that can grow if needed).
6ae2364d 6 *
bcbf86ec 7 * 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".
5260325f 12 */
8efc0c15 13
14#include "includes.h"
bcbf86ec 15RCSID("$OpenBSD: buffer.c,v 1.8 2000/09/07 20:27:50 deraadt Exp $");
8efc0c15 16
17#include "xmalloc.h"
18#include "buffer.h"
19#include "ssh.h"
20
21/* Initializes the buffer structure. */
22
6ae2364d 23void
5260325f 24buffer_init(Buffer *buffer)
8efc0c15 25{
5260325f 26 buffer->alloc = 4096;
27 buffer->buf = xmalloc(buffer->alloc);
28 buffer->offset = 0;
29 buffer->end = 0;
8efc0c15 30}
31
32/* Frees any memory used for the buffer. */
33
6ae2364d 34void
5260325f 35buffer_free(Buffer *buffer)
8efc0c15 36{
5260325f 37 memset(buffer->buf, 0, buffer->alloc);
38 xfree(buffer->buf);
8efc0c15 39}
40
aa3378df 41/*
42 * Clears any data from the buffer, making it empty. This does not actually
43 * zero the memory.
44 */
8efc0c15 45
6ae2364d 46void
5260325f 47buffer_clear(Buffer *buffer)
8efc0c15 48{
5260325f 49 buffer->offset = 0;
50 buffer->end = 0;
8efc0c15 51}
52
53/* Appends data to the buffer, expanding it if necessary. */
54
6ae2364d 55void
5260325f 56buffer_append(Buffer *buffer, const char *data, unsigned int len)
8efc0c15 57{
5260325f 58 char *cp;
59 buffer_append_space(buffer, &cp, len);
60 memcpy(cp, data, len);
8efc0c15 61}
62
aa3378df 63/*
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 */
8efc0c15 68
6ae2364d 69void
5260325f 70buffer_append_space(Buffer *buffer, char **datap, unsigned int len)
8efc0c15 71{
5260325f 72 /* If the buffer is empty, start using it from the beginning. */
73 if (buffer->offset == buffer->end) {
74 buffer->offset = 0;
75 buffer->end = 0;
76 }
77restart:
78 /* If there is enough space to store all data, store it now. */
79 if (buffer->end + len < buffer->alloc) {
80 *datap = buffer->buf + buffer->end;
81 buffer->end += len;
82 return;
83 }
aa3378df 84 /*
85 * If the buffer is quite empty, but all data is at the end, move the
86 * data to the beginning and retry.
87 */
5260325f 88 if (buffer->offset > buffer->alloc / 2) {
89 memmove(buffer->buf, buffer->buf + buffer->offset,
90 buffer->end - buffer->offset);
91 buffer->end -= buffer->offset;
92 buffer->offset = 0;
93 goto restart;
94 }
95 /* Increase the size of the buffer and retry. */
96 buffer->alloc += len + 32768;
97 buffer->buf = xrealloc(buffer->buf, buffer->alloc);
98 goto restart;
8efc0c15 99}
100
101/* Returns the number of bytes of data in the buffer. */
102
6ae2364d 103unsigned int
5260325f 104buffer_len(Buffer *buffer)
8efc0c15 105{
5260325f 106 return buffer->end - buffer->offset;
8efc0c15 107}
108
109/* Gets data from the beginning of the buffer. */
110
6ae2364d 111void
5260325f 112buffer_get(Buffer *buffer, char *buf, unsigned int len)
8efc0c15 113{
5260325f 114 if (len > buffer->end - buffer->offset)
f54651ce 115 fatal("buffer_get: trying to get more bytes than in buffer");
5260325f 116 memcpy(buf, buffer->buf + buffer->offset, len);
117 buffer->offset += len;
8efc0c15 118}
119
120/* Consumes the given number of bytes from the beginning of the buffer. */
121
6ae2364d 122void
5260325f 123buffer_consume(Buffer *buffer, unsigned int bytes)
8efc0c15 124{
5260325f 125 if (bytes > buffer->end - buffer->offset)
f54651ce 126 fatal("buffer_consume: trying to get more bytes than in buffer");
5260325f 127 buffer->offset += bytes;
128}
8efc0c15 129
130/* Consumes the given number of bytes from the end of the buffer. */
131
6ae2364d 132void
5260325f 133buffer_consume_end(Buffer *buffer, unsigned int bytes)
8efc0c15 134{
5260325f 135 if (bytes > buffer->end - buffer->offset)
f54651ce 136 fatal("buffer_consume_end: trying to get more bytes than in buffer");
5260325f 137 buffer->end -= bytes;
138}
8efc0c15 139
140/* Returns a pointer to the first used byte in the buffer. */
141
5260325f 142char *
143buffer_ptr(Buffer *buffer)
8efc0c15 144{
5260325f 145 return buffer->buf + buffer->offset;
8efc0c15 146}
147
148/* Dumps the contents of the buffer to stderr. */
149
6ae2364d 150void
5260325f 151buffer_dump(Buffer *buffer)
8efc0c15 152{
5260325f 153 int i;
154 unsigned char *ucp = (unsigned char *) buffer->buf;
155
156 for (i = buffer->offset; i < buffer->end; i++)
157 fprintf(stderr, " %02x", ucp[i]);
158 fprintf(stderr, "\n");
8efc0c15 159}
This page took 0.107586 seconds and 5 git commands to generate.