]> andersk Git - openssh.git/blob - buffer.c
- stevesk@cvs.openbsd.org 2006/07/22 20:48:23
[openssh.git] / buffer.c
1 /* $OpenBSD: buffer.c,v 1.28 2006/07/22 20:48:22 stevesk Exp $ */
2 /*
3  * Author: Tatu Ylonen <ylo@cs.hut.fi>
4  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5  *                    All rights reserved
6  * Functions for manipulating fifo buffers (that can grow if needed).
7  *
8  * As far as I am concerned, the code I have written for this software
9  * can be used freely for any purpose.  Any derived versions of this
10  * software must be clearly marked as such, and if the derived work is
11  * incompatible with the protocol description in the RFC file, it must be
12  * called by a name other than "ssh" or "Secure Shell".
13  */
14
15 #include "includes.h"
16
17 #include <string.h>
18
19 #include "xmalloc.h"
20 #include "buffer.h"
21 #include "log.h"
22
23 #define BUFFER_MAX_CHUNK        0x100000
24 #define BUFFER_MAX_LEN          0xa00000
25 #define BUFFER_ALLOCSZ          0x008000
26
27 /* Initializes the buffer structure. */
28
29 void
30 buffer_init(Buffer *buffer)
31 {
32         const u_int len = 4096;
33
34         buffer->alloc = 0;
35         buffer->buf = xmalloc(len);
36         buffer->alloc = len;
37         buffer->offset = 0;
38         buffer->end = 0;
39 }
40
41 /* Frees any memory used for the buffer. */
42
43 void
44 buffer_free(Buffer *buffer)
45 {
46         if (buffer->alloc > 0) {
47                 memset(buffer->buf, 0, buffer->alloc);
48                 buffer->alloc = 0;
49                 xfree(buffer->buf);
50         }
51 }
52
53 /*
54  * Clears any data from the buffer, making it empty.  This does not actually
55  * zero the memory.
56  */
57
58 void
59 buffer_clear(Buffer *buffer)
60 {
61         buffer->offset = 0;
62         buffer->end = 0;
63 }
64
65 /* Appends data to the buffer, expanding it if necessary. */
66
67 void
68 buffer_append(Buffer *buffer, const void *data, u_int len)
69 {
70         void *p;
71         p = buffer_append_space(buffer, len);
72         memcpy(p, data, len);
73 }
74
75 static int
76 buffer_compact(Buffer *buffer)
77 {
78         /*
79          * If the buffer is quite empty, but all data is at the end, move the
80          * data to the beginning.
81          */
82         if (buffer->offset > MIN(buffer->alloc, BUFFER_MAX_CHUNK)) {
83                 memmove(buffer->buf, buffer->buf + buffer->offset,
84                         buffer->end - buffer->offset);
85                 buffer->end -= buffer->offset;
86                 buffer->offset = 0;
87                 return (1);
88         }
89         return (0);
90 }
91
92 /*
93  * Appends space to the buffer, expanding the buffer if necessary. This does
94  * not actually copy the data into the buffer, but instead returns a pointer
95  * to the allocated region.
96  */
97
98 void *
99 buffer_append_space(Buffer *buffer, u_int len)
100 {
101         u_int newlen;
102         void *p;
103
104         if (len > BUFFER_MAX_CHUNK)
105                 fatal("buffer_append_space: len %u not supported", len);
106
107         /* If the buffer is empty, start using it from the beginning. */
108         if (buffer->offset == buffer->end) {
109                 buffer->offset = 0;
110                 buffer->end = 0;
111         }
112 restart:
113         /* If there is enough space to store all data, store it now. */
114         if (buffer->end + len < buffer->alloc) {
115                 p = buffer->buf + buffer->end;
116                 buffer->end += len;
117                 return p;
118         }
119
120         /* Compact data back to the start of the buffer if necessary */
121         if (buffer_compact(buffer))
122                 goto restart;
123
124         /* Increase the size of the buffer and retry. */
125         newlen = roundup(buffer->alloc + len, BUFFER_ALLOCSZ);
126         if (newlen > BUFFER_MAX_LEN)
127                 fatal("buffer_append_space: alloc %u not supported",
128                     newlen);
129         buffer->buf = xrealloc(buffer->buf, 1, newlen);
130         buffer->alloc = newlen;
131         goto restart;
132         /* NOTREACHED */
133 }
134
135 /*
136  * Check whether an allocation of 'len' will fit in the buffer
137  * This must follow the same math as buffer_append_space
138  */
139 int
140 buffer_check_alloc(Buffer *buffer, u_int len)
141 {
142         if (buffer->offset == buffer->end) {
143                 buffer->offset = 0;
144                 buffer->end = 0;
145         }
146  restart:
147         if (buffer->end + len < buffer->alloc)
148                 return (1);
149         if (buffer_compact(buffer))
150                 goto restart;
151         if (roundup(buffer->alloc + len, BUFFER_ALLOCSZ) <= BUFFER_MAX_LEN)
152                 return (1);
153         return (0);
154 }
155
156 /* Returns the number of bytes of data in the buffer. */
157
158 u_int
159 buffer_len(Buffer *buffer)
160 {
161         return buffer->end - buffer->offset;
162 }
163
164 /* Gets data from the beginning of the buffer. */
165
166 int
167 buffer_get_ret(Buffer *buffer, void *buf, u_int len)
168 {
169         if (len > buffer->end - buffer->offset) {
170                 error("buffer_get_ret: trying to get more bytes %d than in buffer %d",
171                     len, buffer->end - buffer->offset);
172                 return (-1);
173         }
174         memcpy(buf, buffer->buf + buffer->offset, len);
175         buffer->offset += len;
176         return (0);
177 }
178
179 void
180 buffer_get(Buffer *buffer, void *buf, u_int len)
181 {
182         if (buffer_get_ret(buffer, buf, len) == -1)
183                 fatal("buffer_get: buffer error");
184 }
185
186 /* Consumes the given number of bytes from the beginning of the buffer. */
187
188 int
189 buffer_consume_ret(Buffer *buffer, u_int bytes)
190 {
191         if (bytes > buffer->end - buffer->offset) {
192                 error("buffer_consume_ret: trying to get more bytes than in buffer");
193                 return (-1);
194         }
195         buffer->offset += bytes;
196         return (0);
197 }
198
199 void
200 buffer_consume(Buffer *buffer, u_int bytes)
201 {
202         if (buffer_consume_ret(buffer, bytes) == -1)
203                 fatal("buffer_consume: buffer error");
204 }
205
206 /* Consumes the given number of bytes from the end of the buffer. */
207
208 int
209 buffer_consume_end_ret(Buffer *buffer, u_int bytes)
210 {
211         if (bytes > buffer->end - buffer->offset)
212                 return (-1);
213         buffer->end -= bytes;
214         return (0);
215 }
216
217 void
218 buffer_consume_end(Buffer *buffer, u_int bytes)
219 {
220         if (buffer_consume_end_ret(buffer, bytes) == -1)
221                 fatal("buffer_consume_end: trying to get more bytes than in buffer");
222 }
223
224 /* Returns a pointer to the first used byte in the buffer. */
225
226 void *
227 buffer_ptr(Buffer *buffer)
228 {
229         return buffer->buf + buffer->offset;
230 }
231
232 /* Dumps the contents of the buffer to stderr. */
233
234 void
235 buffer_dump(Buffer *buffer)
236 {
237         u_int i;
238         u_char *ucp = buffer->buf;
239
240         for (i = buffer->offset; i < buffer->end; i++) {
241                 fprintf(stderr, "%02x", ucp[i]);
242                 if ((i-buffer->offset)%16==15)
243                         fprintf(stderr, "\r\n");
244                 else if ((i-buffer->offset)%2==1)
245                         fprintf(stderr, " ");
246         }
247         fprintf(stderr, "\r\n");
248 }
This page took 0.052467 seconds and 5 git commands to generate.