[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.20

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