]> andersk Git - gssapi-openssh.git/blame - openssh/buffer.c
Import of OpenSSH 4.3p2
[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"
dec6d9fe 15RCSID("$OpenBSD: buffer.c,v 1.23 2005/03/14 11:46:56 markus 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{
b51b59a5 26 const u_int len = 4096;
27
28 buffer->alloc = 0;
29 buffer->buf = xmalloc(len);
30 buffer->alloc = len;
3c0ef626 31 buffer->offset = 0;
32 buffer->end = 0;
33}
34
35/* Frees any memory used for the buffer. */
36
37void
38buffer_free(Buffer *buffer)
39{
b51b59a5 40 if (buffer->alloc > 0) {
41 memset(buffer->buf, 0, buffer->alloc);
acc3d05e 42 buffer->alloc = 0;
b51b59a5 43 xfree(buffer->buf);
44 }
3c0ef626 45}
46
47/*
48 * Clears any data from the buffer, making it empty. This does not actually
49 * zero the memory.
50 */
51
52void
53buffer_clear(Buffer *buffer)
54{
55 buffer->offset = 0;
56 buffer->end = 0;
57}
58
59/* Appends data to the buffer, expanding it if necessary. */
60
61void
e9a17296 62buffer_append(Buffer *buffer, const void *data, u_int len)
3c0ef626 63{
e9a17296 64 void *p;
65 p = buffer_append_space(buffer, len);
66 memcpy(p, data, len);
3c0ef626 67}
68
69/*
70 * Appends space to the buffer, expanding the buffer if necessary. This does
71 * not actually copy the data into the buffer, but instead returns a pointer
72 * to the allocated region.
73 */
74
e9a17296 75void *
76buffer_append_space(Buffer *buffer, u_int len)
3c0ef626 77{
0fff78ff 78 u_int newlen;
e9a17296 79 void *p;
80
dec6d9fe 81 if (len > BUFFER_MAX_CHUNK)
680cee3b 82 fatal("buffer_append_space: len %u not supported", len);
83
3c0ef626 84 /* If the buffer is empty, start using it from the beginning. */
85 if (buffer->offset == buffer->end) {
86 buffer->offset = 0;
87 buffer->end = 0;
88 }
89restart:
90 /* If there is enough space to store all data, store it now. */
91 if (buffer->end + len < buffer->alloc) {
e9a17296 92 p = buffer->buf + buffer->end;
3c0ef626 93 buffer->end += len;
e9a17296 94 return p;
3c0ef626 95 }
96 /*
97 * If the buffer is quite empty, but all data is at the end, move the
98 * data to the beginning and retry.
99 */
dec6d9fe 100 if (buffer->offset > MIN(buffer->alloc, BUFFER_MAX_CHUNK)) {
3c0ef626 101 memmove(buffer->buf, buffer->buf + buffer->offset,
102 buffer->end - buffer->offset);
103 buffer->end -= buffer->offset;
104 buffer->offset = 0;
105 goto restart;
106 }
107 /* Increase the size of the buffer and retry. */
cdd66111 108
0fff78ff 109 newlen = buffer->alloc + len + 32768;
dec6d9fe 110 if (newlen > BUFFER_MAX_LEN)
680cee3b 111 fatal("buffer_append_space: alloc %u not supported",
0fff78ff 112 newlen);
113 buffer->buf = xrealloc(buffer->buf, newlen);
114 buffer->alloc = newlen;
3c0ef626 115 goto restart;
e9a17296 116 /* NOTREACHED */
3c0ef626 117}
118
119/* Returns the number of bytes of data in the buffer. */
120
121u_int
122buffer_len(Buffer *buffer)
123{
124 return buffer->end - buffer->offset;
125}
126
127/* Gets data from the beginning of the buffer. */
128
996d5e62 129int
130buffer_get_ret(Buffer *buffer, void *buf, u_int len)
3c0ef626 131{
996d5e62 132 if (len > buffer->end - buffer->offset) {
133 error("buffer_get_ret: trying to get more bytes %d than in buffer %d",
3c0ef626 134 len, buffer->end - buffer->offset);
996d5e62 135 return (-1);
136 }
3c0ef626 137 memcpy(buf, buffer->buf + buffer->offset, len);
138 buffer->offset += len;
996d5e62 139 return (0);
140}
141
142void
143buffer_get(Buffer *buffer, void *buf, u_int len)
144{
145 if (buffer_get_ret(buffer, buf, len) == -1)
146 fatal("buffer_get: buffer error");
3c0ef626 147}
148
149/* Consumes the given number of bytes from the beginning of the buffer. */
150
996d5e62 151int
152buffer_consume_ret(Buffer *buffer, u_int bytes)
153{
154 if (bytes > buffer->end - buffer->offset) {
155 error("buffer_consume_ret: trying to get more bytes than in buffer");
156 return (-1);
157 }
158 buffer->offset += bytes;
159 return (0);
160}
161
3c0ef626 162void
163buffer_consume(Buffer *buffer, u_int bytes)
164{
996d5e62 165 if (buffer_consume_ret(buffer, bytes) == -1)
166 fatal("buffer_consume: buffer error");
3c0ef626 167}
168
169/* Consumes the given number of bytes from the end of the buffer. */
170
996d5e62 171int
172buffer_consume_end_ret(Buffer *buffer, u_int bytes)
173{
174 if (bytes > buffer->end - buffer->offset)
175 return (-1);
176 buffer->end -= bytes;
177 return (0);
178}
179
3c0ef626 180void
181buffer_consume_end(Buffer *buffer, u_int bytes)
182{
996d5e62 183 if (buffer_consume_end_ret(buffer, bytes) == -1)
3c0ef626 184 fatal("buffer_consume_end: trying to get more bytes than in buffer");
3c0ef626 185}
186
187/* Returns a pointer to the first used byte in the buffer. */
188
e9a17296 189void *
3c0ef626 190buffer_ptr(Buffer *buffer)
191{
192 return buffer->buf + buffer->offset;
193}
194
195/* Dumps the contents of the buffer to stderr. */
196
197void
198buffer_dump(Buffer *buffer)
199{
cdd66111 200 u_int i;
e9a17296 201 u_char *ucp = buffer->buf;
3c0ef626 202
203 for (i = buffer->offset; i < buffer->end; i++) {
204 fprintf(stderr, "%02x", ucp[i]);
205 if ((i-buffer->offset)%16==15)
206 fprintf(stderr, "\r\n");
207 else if ((i-buffer->offset)%2==1)
208 fprintf(stderr, " ");
209 }
210 fprintf(stderr, "\r\n");
211}
This page took 0.163141 seconds and 5 git commands to generate.