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

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.18    ! markus     15: RCSID("$OpenBSD: buffer.c,v 1.17 2003/09/16 03:03:47 deraadt 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);
        !            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.16      markus     80:        if (len > 0x100000)
                     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.2       markus     99:        if (buffer->offset > buffer->alloc / 2) {
                    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.17      deraadt   107:
                    108:        newlen = buffer->alloc + len + 32768;
                    109:        if (newlen > 0xa00000)
1.16      markus    110:                fatal("buffer_append_space: alloc %u not supported",
1.17      deraadt   111:                    newlen);
                    112:        buffer->buf = xrealloc(buffer->buf, newlen);
                    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.6       markus    128: void
1.14      stevesk   129: buffer_get(Buffer *buffer, void *buf, u_int len)
1.1       deraadt   130: {
1.2       markus    131:        if (len > buffer->end - buffer->offset)
1.11      markus    132:                fatal("buffer_get: trying to get more bytes %d than in buffer %d",
                    133:                    len, buffer->end - buffer->offset);
1.2       markus    134:        memcpy(buf, buffer->buf + buffer->offset, len);
                    135:        buffer->offset += len;
1.1       deraadt   136: }
                    137:
                    138: /* Consumes the given number of bytes from the beginning of the buffer. */
                    139:
1.6       markus    140: void
1.9       markus    141: buffer_consume(Buffer *buffer, u_int bytes)
1.1       deraadt   142: {
1.2       markus    143:        if (bytes > buffer->end - buffer->offset)
1.5       markus    144:                fatal("buffer_consume: trying to get more bytes than in buffer");
1.2       markus    145:        buffer->offset += bytes;
                    146: }
1.1       deraadt   147:
                    148: /* Consumes the given number of bytes from the end of the buffer. */
                    149:
1.6       markus    150: void
1.9       markus    151: buffer_consume_end(Buffer *buffer, u_int bytes)
1.1       deraadt   152: {
1.2       markus    153:        if (bytes > buffer->end - buffer->offset)
1.5       markus    154:                fatal("buffer_consume_end: trying to get more bytes than in buffer");
1.2       markus    155:        buffer->end -= bytes;
                    156: }
1.1       deraadt   157:
                    158: /* Returns a pointer to the first used byte in the buffer. */
                    159:
1.14      stevesk   160: void *
1.2       markus    161: buffer_ptr(Buffer *buffer)
1.1       deraadt   162: {
1.2       markus    163:        return buffer->buf + buffer->offset;
1.1       deraadt   164: }
                    165:
                    166: /* Dumps the contents of the buffer to stderr. */
                    167:
1.6       markus    168: void
1.2       markus    169: buffer_dump(Buffer *buffer)
1.1       deraadt   170: {
1.2       markus    171:        int i;
1.15      stevesk   172:        u_char *ucp = buffer->buf;
1.2       markus    173:
1.13      markus    174:        for (i = buffer->offset; i < buffer->end; i++) {
                    175:                fprintf(stderr, "%02x", ucp[i]);
                    176:                if ((i-buffer->offset)%16==15)
                    177:                        fprintf(stderr, "\r\n");
                    178:                else if ((i-buffer->offset)%2==1)
                    179:                        fprintf(stderr, " ");
                    180:        }
1.12      markus    181:        fprintf(stderr, "\r\n");
1.1       deraadt   182: }