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