Annotation of src/usr.bin/ssh/buffer.c, Revision 1.16
1.1 deraadt 1: /*
1.3 deraadt 2: * Author: Tatu Ylonen <ylo@cs.hut.fi>
3: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4: * All rights reserved
5: * Functions for manipulating fifo buffers (that can grow if needed).
1.6 markus 6: *
1.8 deraadt 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".
1.3 deraadt 12: */
1.1 deraadt 13:
14: #include "includes.h"
1.16 ! markus 15: RCSID("$OpenBSD: buffer.c,v 1.15 2002/01/18 18:14:17 stevesk Exp $");
1.1 deraadt 16:
17: #include "xmalloc.h"
18: #include "buffer.h"
1.10 markus 19: #include "log.h"
1.1 deraadt 20:
21: /* Initializes the buffer structure. */
22:
1.6 markus 23: void
1.2 markus 24: buffer_init(Buffer *buffer)
1.1 deraadt 25: {
1.2 markus 26: buffer->alloc = 4096;
27: buffer->buf = xmalloc(buffer->alloc);
28: buffer->offset = 0;
29: buffer->end = 0;
1.1 deraadt 30: }
31:
32: /* Frees any memory used for the buffer. */
33:
1.6 markus 34: void
1.2 markus 35: buffer_free(Buffer *buffer)
1.1 deraadt 36: {
1.2 markus 37: memset(buffer->buf, 0, buffer->alloc);
38: xfree(buffer->buf);
1.1 deraadt 39: }
40:
1.4 markus 41: /*
42: * Clears any data from the buffer, making it empty. This does not actually
43: * zero the memory.
44: */
1.1 deraadt 45:
1.6 markus 46: void
1.2 markus 47: buffer_clear(Buffer *buffer)
1.1 deraadt 48: {
1.2 markus 49: buffer->offset = 0;
50: buffer->end = 0;
1.1 deraadt 51: }
52:
53: /* Appends data to the buffer, expanding it if necessary. */
54:
1.6 markus 55: void
1.14 stevesk 56: buffer_append(Buffer *buffer, const void *data, u_int len)
1.1 deraadt 57: {
1.14 stevesk 58: void *p;
59: p = buffer_append_space(buffer, len);
60: memcpy(p, data, len);
1.1 deraadt 61: }
62:
1.4 markus 63: /*
64: * Appends space to the buffer, expanding the buffer if necessary. This does
65: * not actually copy the data into the buffer, but instead returns a pointer
66: * to the allocated region.
67: */
1.1 deraadt 68:
1.14 stevesk 69: void *
70: buffer_append_space(Buffer *buffer, u_int len)
1.1 deraadt 71: {
1.14 stevesk 72: void *p;
73:
1.16 ! markus 74: if (len > 0x100000)
! 75: fatal("buffer_append_space: len %u not supported", len);
! 76:
1.2 markus 77: /* If the buffer is empty, start using it from the beginning. */
78: if (buffer->offset == buffer->end) {
79: buffer->offset = 0;
80: buffer->end = 0;
81: }
82: restart:
83: /* If there is enough space to store all data, store it now. */
84: if (buffer->end + len < buffer->alloc) {
1.14 stevesk 85: p = buffer->buf + buffer->end;
1.2 markus 86: buffer->end += len;
1.14 stevesk 87: return p;
1.2 markus 88: }
1.4 markus 89: /*
90: * If the buffer is quite empty, but all data is at the end, move the
91: * data to the beginning and retry.
92: */
1.2 markus 93: if (buffer->offset > buffer->alloc / 2) {
94: memmove(buffer->buf, buffer->buf + buffer->offset,
95: buffer->end - buffer->offset);
96: buffer->end -= buffer->offset;
97: buffer->offset = 0;
98: goto restart;
99: }
100: /* Increase the size of the buffer and retry. */
101: buffer->alloc += len + 32768;
1.16 ! markus 102: if (buffer->alloc > 0xa00000)
! 103: fatal("buffer_append_space: alloc %u not supported",
! 104: buffer->alloc);
1.2 markus 105: buffer->buf = xrealloc(buffer->buf, buffer->alloc);
106: goto restart;
1.14 stevesk 107: /* NOTREACHED */
1.1 deraadt 108: }
109:
110: /* Returns the number of bytes of data in the buffer. */
111:
1.9 markus 112: u_int
1.2 markus 113: buffer_len(Buffer *buffer)
1.1 deraadt 114: {
1.2 markus 115: return buffer->end - buffer->offset;
1.1 deraadt 116: }
117:
118: /* Gets data from the beginning of the buffer. */
119:
1.6 markus 120: void
1.14 stevesk 121: buffer_get(Buffer *buffer, void *buf, u_int len)
1.1 deraadt 122: {
1.2 markus 123: if (len > buffer->end - buffer->offset)
1.11 markus 124: fatal("buffer_get: trying to get more bytes %d than in buffer %d",
125: len, buffer->end - buffer->offset);
1.2 markus 126: memcpy(buf, buffer->buf + buffer->offset, len);
127: buffer->offset += len;
1.1 deraadt 128: }
129:
130: /* Consumes the given number of bytes from the beginning of the buffer. */
131:
1.6 markus 132: void
1.9 markus 133: buffer_consume(Buffer *buffer, u_int bytes)
1.1 deraadt 134: {
1.2 markus 135: if (bytes > buffer->end - buffer->offset)
1.5 markus 136: fatal("buffer_consume: trying to get more bytes than in buffer");
1.2 markus 137: buffer->offset += bytes;
138: }
1.1 deraadt 139:
140: /* Consumes the given number of bytes from the end of the buffer. */
141:
1.6 markus 142: void
1.9 markus 143: buffer_consume_end(Buffer *buffer, u_int bytes)
1.1 deraadt 144: {
1.2 markus 145: if (bytes > buffer->end - buffer->offset)
1.5 markus 146: fatal("buffer_consume_end: trying to get more bytes than in buffer");
1.2 markus 147: buffer->end -= bytes;
148: }
1.1 deraadt 149:
150: /* Returns a pointer to the first used byte in the buffer. */
151:
1.14 stevesk 152: void *
1.2 markus 153: buffer_ptr(Buffer *buffer)
1.1 deraadt 154: {
1.2 markus 155: return buffer->buf + buffer->offset;
1.1 deraadt 156: }
157:
158: /* Dumps the contents of the buffer to stderr. */
159:
1.6 markus 160: void
1.2 markus 161: buffer_dump(Buffer *buffer)
1.1 deraadt 162: {
1.2 markus 163: int i;
1.15 stevesk 164: u_char *ucp = buffer->buf;
1.2 markus 165:
1.13 markus 166: for (i = buffer->offset; i < buffer->end; i++) {
167: fprintf(stderr, "%02x", ucp[i]);
168: if ((i-buffer->offset)%16==15)
169: fprintf(stderr, "\r\n");
170: else if ((i-buffer->offset)%2==1)
171: fprintf(stderr, " ");
172: }
1.12 markus 173: fprintf(stderr, "\r\n");
1.1 deraadt 174: }