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 | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 15 | |
| 16 | #include "xmalloc.h" |
| 17 | #include "buffer.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 18 | #include "log.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 19 | |
| 20 | /* Initializes the buffer structure. */ |
| 21 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 22 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 23 | buffer_init(Buffer *buffer) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 24 | { |
Damien Miller | 5efcecc | 2003-09-17 07:31:14 +1000 | [diff] [blame] | 25 | const u_int len = 4096; |
| 26 | |
| 27 | buffer->alloc = 0; |
| 28 | buffer->buf = xmalloc(len); |
| 29 | buffer->alloc = len; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 30 | buffer->offset = 0; |
| 31 | buffer->end = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | /* Frees any memory used for the buffer. */ |
| 35 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 36 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 37 | buffer_free(Buffer *buffer) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 38 | { |
Damien Miller | 5efcecc | 2003-09-17 07:31:14 +1000 | [diff] [blame] | 39 | if (buffer->alloc > 0) { |
| 40 | memset(buffer->buf, 0, buffer->alloc); |
Darren Tucker | d2730d3 | 2003-09-22 21:00:19 +1000 | [diff] [blame] | 41 | buffer->alloc = 0; |
Damien Miller | 5efcecc | 2003-09-17 07:31:14 +1000 | [diff] [blame] | 42 | xfree(buffer->buf); |
| 43 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 44 | } |
| 45 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 46 | /* |
| 47 | * Clears any data from the buffer, making it empty. This does not actually |
| 48 | * zero the memory. |
| 49 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 50 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 51 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 52 | buffer_clear(Buffer *buffer) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 53 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 54 | buffer->offset = 0; |
| 55 | buffer->end = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /* Appends data to the buffer, expanding it if necessary. */ |
| 59 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 60 | void |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 61 | buffer_append(Buffer *buffer, const void *data, u_int len) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 62 | { |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 63 | void *p; |
| 64 | p = buffer_append_space(buffer, len); |
| 65 | memcpy(p, data, len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 66 | } |
| 67 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 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 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 73 | |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 74 | void * |
| 75 | buffer_append_space(Buffer *buffer, u_int len) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 76 | { |
Ben Lindstrom | f2b4e4e | 2003-09-16 03:31:03 +0000 | [diff] [blame] | 77 | u_int newlen; |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 78 | void *p; |
| 79 | |
Darren Tucker | 11327cc | 2005-03-14 23:22:25 +1100 | [diff] [blame] | 80 | if (len > BUFFER_MAX_CHUNK) |
Damien Miller | 468cd71 | 2002-06-26 19:14:25 +1000 | [diff] [blame] | 81 | fatal("buffer_append_space: len %u not supported", len); |
| 82 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 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) { |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 91 | p = buffer->buf + buffer->end; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 92 | buffer->end += len; |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 93 | return p; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 94 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 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 | */ |
Darren Tucker | 11327cc | 2005-03-14 23:22:25 +1100 | [diff] [blame] | 99 | if (buffer->offset > MIN(buffer->alloc, BUFFER_MAX_CHUNK)) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 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. */ |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 107 | |
Ben Lindstrom | f2b4e4e | 2003-09-16 03:31:03 +0000 | [diff] [blame] | 108 | newlen = buffer->alloc + len + 32768; |
Darren Tucker | 11327cc | 2005-03-14 23:22:25 +1100 | [diff] [blame] | 109 | if (newlen > BUFFER_MAX_LEN) |
Damien Miller | 468cd71 | 2002-06-26 19:14:25 +1000 | [diff] [blame] | 110 | fatal("buffer_append_space: alloc %u not supported", |
Ben Lindstrom | f2b4e4e | 2003-09-16 03:31:03 +0000 | [diff] [blame] | 111 | newlen); |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame^] | 112 | buffer->buf = xrealloc(buffer->buf, 1, newlen); |
Ben Lindstrom | f2b4e4e | 2003-09-16 03:31:03 +0000 | [diff] [blame] | 113 | buffer->alloc = newlen; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 114 | goto restart; |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 115 | /* NOTREACHED */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /* Returns the number of bytes of data in the buffer. */ |
| 119 | |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 120 | u_int |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 121 | buffer_len(Buffer *buffer) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 122 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 123 | return buffer->end - buffer->offset; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /* Gets data from the beginning of the buffer. */ |
| 127 | |
Darren Tucker | 50dbe83 | 2004-11-05 20:41:24 +1100 | [diff] [blame] | 128 | int |
| 129 | buffer_get_ret(Buffer *buffer, void *buf, u_int len) |
| 130 | { |
| 131 | if (len > buffer->end - buffer->offset) { |
| 132 | error("buffer_get_ret: trying to get more bytes %d than in buffer %d", |
| 133 | len, buffer->end - buffer->offset); |
| 134 | return (-1); |
| 135 | } |
| 136 | memcpy(buf, buffer->buf + buffer->offset, len); |
| 137 | buffer->offset += len; |
| 138 | return (0); |
| 139 | } |
| 140 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 141 | void |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 142 | buffer_get(Buffer *buffer, void *buf, u_int len) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 143 | { |
Darren Tucker | 50dbe83 | 2004-11-05 20:41:24 +1100 | [diff] [blame] | 144 | if (buffer_get_ret(buffer, buf, len) == -1) |
| 145 | fatal("buffer_get: buffer error"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /* Consumes the given number of bytes from the beginning of the buffer. */ |
| 149 | |
Darren Tucker | 50dbe83 | 2004-11-05 20:41:24 +1100 | [diff] [blame] | 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 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 161 | void |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 162 | buffer_consume(Buffer *buffer, u_int bytes) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 163 | { |
Darren Tucker | 50dbe83 | 2004-11-05 20:41:24 +1100 | [diff] [blame] | 164 | if (buffer_consume_ret(buffer, bytes) == -1) |
| 165 | fatal("buffer_consume: buffer error"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 166 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 167 | |
| 168 | /* Consumes the given number of bytes from the end of the buffer. */ |
| 169 | |
Darren Tucker | 50dbe83 | 2004-11-05 20:41:24 +1100 | [diff] [blame] | 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 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 179 | void |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 180 | buffer_consume_end(Buffer *buffer, u_int bytes) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 181 | { |
Darren Tucker | 50dbe83 | 2004-11-05 20:41:24 +1100 | [diff] [blame] | 182 | if (buffer_consume_end_ret(buffer, bytes) == -1) |
Damien Miller | 22c7726 | 2000-04-13 12:26:34 +1000 | [diff] [blame] | 183 | fatal("buffer_consume_end: trying to get more bytes than in buffer"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 184 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 185 | |
| 186 | /* Returns a pointer to the first used byte in the buffer. */ |
| 187 | |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 188 | void * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 189 | buffer_ptr(Buffer *buffer) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 190 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 191 | return buffer->buf + buffer->offset; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | /* Dumps the contents of the buffer to stderr. */ |
| 195 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 196 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 197 | buffer_dump(Buffer *buffer) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 198 | { |
Darren Tucker | 631a4a9 | 2003-09-22 21:08:21 +1000 | [diff] [blame] | 199 | u_int i; |
Damien Miller | 4a8ed54 | 2002-01-22 23:33:31 +1100 | [diff] [blame] | 200 | u_char *ucp = buffer->buf; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 201 | |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 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 | } |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 209 | fprintf(stderr, "\r\n"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 210 | } |