2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * Functions for manipulating fifo buffers (that can grow if needed).
7 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
15 RCSID("$OpenBSD: buffer.c,v 1.23 2005/03/14 11:46:56 markus Exp $");
21 /* Initializes the buffer structure. */
24 buffer_init(Buffer
*buffer
)
26 const u_int len
= 4096;
29 buffer
->buf
= xmalloc(len
);
35 /* Frees any memory used for the buffer. */
38 buffer_free(Buffer
*buffer
)
40 if (buffer
->alloc
> 0) {
41 memset(buffer
->buf
, 0, buffer
->alloc
);
48 * Clears any data from the buffer, making it empty. This does not actually
53 buffer_clear(Buffer
*buffer
)
59 /* Appends data to the buffer, expanding it if necessary. */
62 buffer_append(Buffer
*buffer
, const void *data
, u_int len
)
65 p
= buffer_append_space(buffer
, len
);
70 * Appends space to the buffer, expanding the buffer if necessary. This does
71 * not actually copy the data into the buffer, but instead returns a pointer
72 * to the allocated region.
76 buffer_append_space(Buffer
*buffer
, u_int len
)
81 if (len
> BUFFER_MAX_CHUNK
)
82 fatal("buffer_append_space: len %u not supported", len
);
84 /* If the buffer is empty, start using it from the beginning. */
85 if (buffer
->offset
== buffer
->end
) {
90 /* If there is enough space to store all data, store it now. */
91 if (buffer
->end
+ len
< buffer
->alloc
) {
92 p
= buffer
->buf
+ buffer
->end
;
97 * If the buffer is quite empty, but all data is at the end, move the
98 * data to the beginning and retry.
100 if (buffer
->offset
> MIN(buffer
->alloc
, BUFFER_MAX_CHUNK
)) {
101 memmove(buffer
->buf
, buffer
->buf
+ buffer
->offset
,
102 buffer
->end
- buffer
->offset
);
103 buffer
->end
-= buffer
->offset
;
107 /* Increase the size of the buffer and retry. */
109 newlen
= buffer
->alloc
+ len
+ 32768;
110 if (newlen
> BUFFER_MAX_LEN
)
111 fatal("buffer_append_space: alloc %u not supported",
113 buffer
->buf
= xrealloc(buffer
->buf
, newlen
);
114 buffer
->alloc
= newlen
;
119 /* Returns the number of bytes of data in the buffer. */
122 buffer_len(Buffer
*buffer
)
124 return buffer
->end
- buffer
->offset
;
127 /* Gets data from the beginning of the buffer. */
130 buffer_get_ret(Buffer
*buffer
, void *buf
, u_int len
)
132 if (len
> buffer
->end
- buffer
->offset
) {
133 error("buffer_get_ret: trying to get more bytes %d than in buffer %d",
134 len
, buffer
->end
- buffer
->offset
);
137 memcpy(buf
, buffer
->buf
+ buffer
->offset
, len
);
138 buffer
->offset
+= len
;
143 buffer_get(Buffer
*buffer
, void *buf
, u_int len
)
145 if (buffer_get_ret(buffer
, buf
, len
) == -1)
146 fatal("buffer_get: buffer error");
149 /* Consumes the given number of bytes from the beginning of the buffer. */
152 buffer_consume_ret(Buffer
*buffer
, u_int bytes
)
154 if (bytes
> buffer
->end
- buffer
->offset
) {
155 error("buffer_consume_ret: trying to get more bytes than in buffer");
158 buffer
->offset
+= bytes
;
163 buffer_consume(Buffer
*buffer
, u_int bytes
)
165 if (buffer_consume_ret(buffer
, bytes
) == -1)
166 fatal("buffer_consume: buffer error");
169 /* Consumes the given number of bytes from the end of the buffer. */
172 buffer_consume_end_ret(Buffer
*buffer
, u_int bytes
)
174 if (bytes
> buffer
->end
- buffer
->offset
)
176 buffer
->end
-= bytes
;
181 buffer_consume_end(Buffer
*buffer
, u_int bytes
)
183 if (buffer_consume_end_ret(buffer
, bytes
) == -1)
184 fatal("buffer_consume_end: trying to get more bytes than in buffer");
187 /* Returns a pointer to the first used byte in the buffer. */
190 buffer_ptr(Buffer
*buffer
)
192 return buffer
->buf
+ buffer
->offset
;
195 /* Dumps the contents of the buffer to stderr. */
198 buffer_dump(Buffer
*buffer
)
201 u_char
*ucp
= buffer
->buf
;
203 for (i
= buffer
->offset
; i
< buffer
->end
; i
++) {
204 fprintf(stderr
, "%02x", ucp
[i
]);
205 if ((i
-buffer
->offset
)%16==15)
206 fprintf(stderr
, "\r\n");
207 else if ((i
-buffer
->offset
)%2==1)
208 fprintf(stderr
, " ");
210 fprintf(stderr
, "\r\n");