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