[BACK]Return to buffer.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/buffer.c, Revision 1.25

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