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