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