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