Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 4 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 5 | * Functions for manipulating fifo buffers (that can grow if needed). |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 6 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 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". |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 12 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 13 | |
| 14 | #include "includes.h" |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 15 | RCSID("$OpenBSD: buffer.c,v 1.8 2000/09/07 20:27:50 deraadt Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 16 | |
| 17 | #include "xmalloc.h" |
| 18 | #include "buffer.h" |
| 19 | #include "ssh.h" |
| 20 | |
| 21 | /* Initializes the buffer structure. */ |
| 22 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 23 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 24 | buffer_init(Buffer *buffer) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 25 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 26 | buffer->alloc = 4096; |
| 27 | buffer->buf = xmalloc(buffer->alloc); |
| 28 | buffer->offset = 0; |
| 29 | buffer->end = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | /* Frees any memory used for the buffer. */ |
| 33 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 34 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 35 | buffer_free(Buffer *buffer) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 36 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 37 | memset(buffer->buf, 0, buffer->alloc); |
| 38 | xfree(buffer->buf); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 39 | } |
| 40 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 41 | /* |
| 42 | * Clears any data from the buffer, making it empty. This does not actually |
| 43 | * zero the memory. |
| 44 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 45 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 46 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 47 | buffer_clear(Buffer *buffer) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 48 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 49 | buffer->offset = 0; |
| 50 | buffer->end = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /* Appends data to the buffer, expanding it if necessary. */ |
| 54 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 55 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 56 | buffer_append(Buffer *buffer, const char *data, unsigned int len) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 57 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 58 | char *cp; |
| 59 | buffer_append_space(buffer, &cp, len); |
| 60 | memcpy(cp, data, len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 61 | } |
| 62 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 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 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 68 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 69 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 70 | buffer_append_space(Buffer *buffer, char **datap, unsigned int len) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 71 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 72 | /* If the buffer is empty, start using it from the beginning. */ |
| 73 | if (buffer->offset == buffer->end) { |
| 74 | buffer->offset = 0; |
| 75 | buffer->end = 0; |
| 76 | } |
| 77 | restart: |
| 78 | /* If there is enough space to store all data, store it now. */ |
| 79 | if (buffer->end + len < buffer->alloc) { |
| 80 | *datap = buffer->buf + buffer->end; |
| 81 | buffer->end += len; |
| 82 | return; |
| 83 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 84 | /* |
| 85 | * If the buffer is quite empty, but all data is at the end, move the |
| 86 | * data to the beginning and retry. |
| 87 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 88 | if (buffer->offset > buffer->alloc / 2) { |
| 89 | memmove(buffer->buf, buffer->buf + buffer->offset, |
| 90 | buffer->end - buffer->offset); |
| 91 | buffer->end -= buffer->offset; |
| 92 | buffer->offset = 0; |
| 93 | goto restart; |
| 94 | } |
| 95 | /* Increase the size of the buffer and retry. */ |
| 96 | buffer->alloc += len + 32768; |
| 97 | buffer->buf = xrealloc(buffer->buf, buffer->alloc); |
| 98 | goto restart; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /* Returns the number of bytes of data in the buffer. */ |
| 102 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 103 | unsigned int |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 104 | buffer_len(Buffer *buffer) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 105 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 106 | return buffer->end - buffer->offset; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /* Gets data from the beginning of the buffer. */ |
| 110 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 111 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 112 | buffer_get(Buffer *buffer, char *buf, unsigned int len) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 113 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 114 | if (len > buffer->end - buffer->offset) |
Damien Miller | 22c7726 | 2000-04-13 12:26:34 +1000 | [diff] [blame] | 115 | fatal("buffer_get: trying to get more bytes than in buffer"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 116 | memcpy(buf, buffer->buf + buffer->offset, len); |
| 117 | buffer->offset += len; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /* Consumes the given number of bytes from the beginning of the buffer. */ |
| 121 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 122 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 123 | buffer_consume(Buffer *buffer, unsigned int bytes) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 124 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 125 | if (bytes > buffer->end - buffer->offset) |
Damien Miller | 22c7726 | 2000-04-13 12:26:34 +1000 | [diff] [blame] | 126 | fatal("buffer_consume: trying to get more bytes than in buffer"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 127 | buffer->offset += bytes; |
| 128 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 129 | |
| 130 | /* Consumes the given number of bytes from the end of the buffer. */ |
| 131 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 132 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 133 | buffer_consume_end(Buffer *buffer, unsigned int bytes) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 134 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 135 | if (bytes > buffer->end - buffer->offset) |
Damien Miller | 22c7726 | 2000-04-13 12:26:34 +1000 | [diff] [blame] | 136 | fatal("buffer_consume_end: trying to get more bytes than in buffer"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 137 | buffer->end -= bytes; |
| 138 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 139 | |
| 140 | /* Returns a pointer to the first used byte in the buffer. */ |
| 141 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 142 | char * |
| 143 | buffer_ptr(Buffer *buffer) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 144 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 145 | return buffer->buf + buffer->offset; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /* Dumps the contents of the buffer to stderr. */ |
| 149 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 150 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 151 | buffer_dump(Buffer *buffer) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 152 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 153 | int i; |
| 154 | unsigned char *ucp = (unsigned char *) buffer->buf; |
| 155 | |
| 156 | for (i = buffer->offset; i < buffer->end; i++) |
| 157 | fprintf(stderr, " %02x", ucp[i]); |
| 158 | fprintf(stderr, "\n"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 159 | } |