Damien Miller | 8dbffe7 | 2006-08-05 11:02:17 +1000 | [diff] [blame^] | 1 | /* $OpenBSD: buffer.c,v 1.29 2006/07/26 02:35:17 stevesk Exp $ */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 5 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 6 | * Functions for manipulating fifo buffers (that can grow if needed). |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 7 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 8 | * As far as I am concerned, the code I have written for this software |
| 9 | * can be used freely for any purpose. Any derived versions of this |
| 10 | * software must be clearly marked as such, and if the derived work is |
| 11 | * incompatible with the protocol description in the RFC file, it must be |
| 12 | * called by a name other than "ssh" or "Secure Shell". |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 13 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 14 | |
| 15 | #include "includes.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 16 | |
Damien Miller | 8dbffe7 | 2006-08-05 11:02:17 +1000 | [diff] [blame^] | 17 | #include <sys/param.h> |
| 18 | |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 19 | #include <string.h> |
| 20 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 21 | #include "xmalloc.h" |
| 22 | #include "buffer.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 23 | #include "log.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 24 | |
Damien Miller | 499a0d5 | 2006-04-23 12:06:03 +1000 | [diff] [blame] | 25 | #define BUFFER_MAX_CHUNK 0x100000 |
| 26 | #define BUFFER_MAX_LEN 0xa00000 |
| 27 | #define BUFFER_ALLOCSZ 0x008000 |
| 28 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 29 | /* Initializes the buffer structure. */ |
| 30 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 31 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 32 | buffer_init(Buffer *buffer) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 33 | { |
Damien Miller | 5efcecc | 2003-09-17 07:31:14 +1000 | [diff] [blame] | 34 | const u_int len = 4096; |
| 35 | |
| 36 | buffer->alloc = 0; |
| 37 | buffer->buf = xmalloc(len); |
| 38 | buffer->alloc = len; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 39 | buffer->offset = 0; |
| 40 | buffer->end = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /* Frees any memory used for the buffer. */ |
| 44 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 45 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 46 | buffer_free(Buffer *buffer) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 47 | { |
Damien Miller | 5efcecc | 2003-09-17 07:31:14 +1000 | [diff] [blame] | 48 | if (buffer->alloc > 0) { |
| 49 | memset(buffer->buf, 0, buffer->alloc); |
Darren Tucker | d2730d3 | 2003-09-22 21:00:19 +1000 | [diff] [blame] | 50 | buffer->alloc = 0; |
Damien Miller | 5efcecc | 2003-09-17 07:31:14 +1000 | [diff] [blame] | 51 | xfree(buffer->buf); |
| 52 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 53 | } |
| 54 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 55 | /* |
| 56 | * Clears any data from the buffer, making it empty. This does not actually |
| 57 | * zero the memory. |
| 58 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 59 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 60 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 61 | buffer_clear(Buffer *buffer) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 62 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 63 | buffer->offset = 0; |
| 64 | buffer->end = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /* Appends data to the buffer, expanding it if necessary. */ |
| 68 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 69 | void |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 70 | buffer_append(Buffer *buffer, const void *data, u_int len) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 71 | { |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 72 | void *p; |
| 73 | p = buffer_append_space(buffer, len); |
| 74 | memcpy(p, data, len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 75 | } |
| 76 | |
Damien Miller | 499a0d5 | 2006-04-23 12:06:03 +1000 | [diff] [blame] | 77 | static int |
| 78 | buffer_compact(Buffer *buffer) |
| 79 | { |
| 80 | /* |
| 81 | * If the buffer is quite empty, but all data is at the end, move the |
| 82 | * data to the beginning. |
| 83 | */ |
| 84 | if (buffer->offset > MIN(buffer->alloc, BUFFER_MAX_CHUNK)) { |
| 85 | memmove(buffer->buf, buffer->buf + buffer->offset, |
| 86 | buffer->end - buffer->offset); |
| 87 | buffer->end -= buffer->offset; |
| 88 | buffer->offset = 0; |
| 89 | return (1); |
| 90 | } |
| 91 | return (0); |
| 92 | } |
| 93 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 94 | /* |
| 95 | * Appends space to the buffer, expanding the buffer if necessary. This does |
| 96 | * not actually copy the data into the buffer, but instead returns a pointer |
| 97 | * to the allocated region. |
| 98 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 99 | |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 100 | void * |
| 101 | buffer_append_space(Buffer *buffer, u_int len) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 102 | { |
Ben Lindstrom | f2b4e4e | 2003-09-16 03:31:03 +0000 | [diff] [blame] | 103 | u_int newlen; |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 104 | void *p; |
| 105 | |
Darren Tucker | 11327cc | 2005-03-14 23:22:25 +1100 | [diff] [blame] | 106 | if (len > BUFFER_MAX_CHUNK) |
Damien Miller | 468cd71 | 2002-06-26 19:14:25 +1000 | [diff] [blame] | 107 | fatal("buffer_append_space: len %u not supported", len); |
| 108 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 109 | /* If the buffer is empty, start using it from the beginning. */ |
| 110 | if (buffer->offset == buffer->end) { |
| 111 | buffer->offset = 0; |
| 112 | buffer->end = 0; |
| 113 | } |
| 114 | restart: |
| 115 | /* If there is enough space to store all data, store it now. */ |
| 116 | if (buffer->end + len < buffer->alloc) { |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 117 | p = buffer->buf + buffer->end; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 118 | buffer->end += len; |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 119 | return p; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 120 | } |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 121 | |
Damien Miller | 499a0d5 | 2006-04-23 12:06:03 +1000 | [diff] [blame] | 122 | /* Compact data back to the start of the buffer if necessary */ |
| 123 | if (buffer_compact(buffer)) |
| 124 | goto restart; |
| 125 | |
| 126 | /* Increase the size of the buffer and retry. */ |
| 127 | newlen = roundup(buffer->alloc + len, BUFFER_ALLOCSZ); |
Darren Tucker | 11327cc | 2005-03-14 23:22:25 +1100 | [diff] [blame] | 128 | if (newlen > BUFFER_MAX_LEN) |
Damien Miller | 468cd71 | 2002-06-26 19:14:25 +1000 | [diff] [blame] | 129 | fatal("buffer_append_space: alloc %u not supported", |
Ben Lindstrom | f2b4e4e | 2003-09-16 03:31:03 +0000 | [diff] [blame] | 130 | newlen); |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 131 | buffer->buf = xrealloc(buffer->buf, 1, newlen); |
Ben Lindstrom | f2b4e4e | 2003-09-16 03:31:03 +0000 | [diff] [blame] | 132 | buffer->alloc = newlen; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 133 | goto restart; |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 134 | /* NOTREACHED */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 135 | } |
| 136 | |
Damien Miller | 499a0d5 | 2006-04-23 12:06:03 +1000 | [diff] [blame] | 137 | /* |
| 138 | * Check whether an allocation of 'len' will fit in the buffer |
| 139 | * This must follow the same math as buffer_append_space |
| 140 | */ |
| 141 | int |
| 142 | buffer_check_alloc(Buffer *buffer, u_int len) |
| 143 | { |
| 144 | if (buffer->offset == buffer->end) { |
| 145 | buffer->offset = 0; |
| 146 | buffer->end = 0; |
| 147 | } |
| 148 | restart: |
| 149 | if (buffer->end + len < buffer->alloc) |
| 150 | return (1); |
| 151 | if (buffer_compact(buffer)) |
| 152 | goto restart; |
| 153 | if (roundup(buffer->alloc + len, BUFFER_ALLOCSZ) <= BUFFER_MAX_LEN) |
| 154 | return (1); |
| 155 | return (0); |
| 156 | } |
| 157 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 158 | /* Returns the number of bytes of data in the buffer. */ |
| 159 | |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 160 | u_int |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 161 | buffer_len(Buffer *buffer) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 162 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 163 | return buffer->end - buffer->offset; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /* Gets data from the beginning of the buffer. */ |
| 167 | |
Darren Tucker | 50dbe83 | 2004-11-05 20:41:24 +1100 | [diff] [blame] | 168 | int |
| 169 | buffer_get_ret(Buffer *buffer, void *buf, u_int len) |
| 170 | { |
| 171 | if (len > buffer->end - buffer->offset) { |
| 172 | error("buffer_get_ret: trying to get more bytes %d than in buffer %d", |
| 173 | len, buffer->end - buffer->offset); |
| 174 | return (-1); |
| 175 | } |
| 176 | memcpy(buf, buffer->buf + buffer->offset, len); |
| 177 | buffer->offset += len; |
| 178 | return (0); |
| 179 | } |
| 180 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 181 | void |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 182 | buffer_get(Buffer *buffer, void *buf, u_int len) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 183 | { |
Darren Tucker | 50dbe83 | 2004-11-05 20:41:24 +1100 | [diff] [blame] | 184 | if (buffer_get_ret(buffer, buf, len) == -1) |
| 185 | fatal("buffer_get: buffer error"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /* Consumes the given number of bytes from the beginning of the buffer. */ |
| 189 | |
Darren Tucker | 50dbe83 | 2004-11-05 20:41:24 +1100 | [diff] [blame] | 190 | int |
| 191 | buffer_consume_ret(Buffer *buffer, u_int bytes) |
| 192 | { |
| 193 | if (bytes > buffer->end - buffer->offset) { |
| 194 | error("buffer_consume_ret: trying to get more bytes than in buffer"); |
| 195 | return (-1); |
| 196 | } |
| 197 | buffer->offset += bytes; |
| 198 | return (0); |
| 199 | } |
| 200 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 201 | void |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 202 | buffer_consume(Buffer *buffer, u_int bytes) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 203 | { |
Darren Tucker | 50dbe83 | 2004-11-05 20:41:24 +1100 | [diff] [blame] | 204 | if (buffer_consume_ret(buffer, bytes) == -1) |
| 205 | fatal("buffer_consume: buffer error"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 206 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 207 | |
| 208 | /* Consumes the given number of bytes from the end of the buffer. */ |
| 209 | |
Darren Tucker | 50dbe83 | 2004-11-05 20:41:24 +1100 | [diff] [blame] | 210 | int |
| 211 | buffer_consume_end_ret(Buffer *buffer, u_int bytes) |
| 212 | { |
| 213 | if (bytes > buffer->end - buffer->offset) |
| 214 | return (-1); |
| 215 | buffer->end -= bytes; |
| 216 | return (0); |
| 217 | } |
| 218 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 219 | void |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 220 | buffer_consume_end(Buffer *buffer, u_int bytes) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 221 | { |
Darren Tucker | 50dbe83 | 2004-11-05 20:41:24 +1100 | [diff] [blame] | 222 | if (buffer_consume_end_ret(buffer, bytes) == -1) |
Damien Miller | 22c7726 | 2000-04-13 12:26:34 +1000 | [diff] [blame] | 223 | fatal("buffer_consume_end: trying to get more bytes than in buffer"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 224 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 225 | |
| 226 | /* Returns a pointer to the first used byte in the buffer. */ |
| 227 | |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 228 | void * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 229 | buffer_ptr(Buffer *buffer) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 230 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 231 | return buffer->buf + buffer->offset; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /* Dumps the contents of the buffer to stderr. */ |
| 235 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 236 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 237 | buffer_dump(Buffer *buffer) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 238 | { |
Darren Tucker | 631a4a9 | 2003-09-22 21:08:21 +1000 | [diff] [blame] | 239 | u_int i; |
Damien Miller | 4a8ed54 | 2002-01-22 23:33:31 +1100 | [diff] [blame] | 240 | u_char *ucp = buffer->buf; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 241 | |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 242 | for (i = buffer->offset; i < buffer->end; i++) { |
| 243 | fprintf(stderr, "%02x", ucp[i]); |
| 244 | if ((i-buffer->offset)%16==15) |
| 245 | fprintf(stderr, "\r\n"); |
| 246 | else if ((i-buffer->offset)%2==1) |
| 247 | fprintf(stderr, " "); |
| 248 | } |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 249 | fprintf(stderr, "\r\n"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 250 | } |