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