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