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