]>
Commit | Line | Data |
---|---|---|
8efc0c15 | 1 | /* |
5260325f | 2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
5260325f | 3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
4 | * All rights reserved | |
5260325f | 5 | * Functions for manipulating fifo buffers (that can grow if needed). |
6ae2364d | 6 | * |
bcbf86ec | 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". | |
5260325f | 12 | */ |
8efc0c15 | 13 | |
14 | #include "includes.h" | |
b82a59f2 | 15 | RCSID("$OpenBSD: buffer.c,v 1.22 2004/10/29 23:56:17 djm Exp $"); |
8efc0c15 | 16 | |
17 | #include "xmalloc.h" | |
18 | #include "buffer.h" | |
42f11eb2 | 19 | #include "log.h" |
8efc0c15 | 20 | |
21 | /* Initializes the buffer structure. */ | |
22 | ||
6ae2364d | 23 | void |
5260325f | 24 | buffer_init(Buffer *buffer) |
8efc0c15 | 25 | { |
20419cc1 | 26 | const u_int len = 4096; |
27 | ||
28 | buffer->alloc = 0; | |
29 | buffer->buf = xmalloc(len); | |
30 | buffer->alloc = len; | |
5260325f | 31 | buffer->offset = 0; |
32 | buffer->end = 0; | |
8efc0c15 | 33 | } |
34 | ||
35 | /* Frees any memory used for the buffer. */ | |
36 | ||
6ae2364d | 37 | void |
5260325f | 38 | buffer_free(Buffer *buffer) |
8efc0c15 | 39 | { |
20419cc1 | 40 | if (buffer->alloc > 0) { |
41 | memset(buffer->buf, 0, buffer->alloc); | |
5bd34316 | 42 | buffer->alloc = 0; |
20419cc1 | 43 | xfree(buffer->buf); |
44 | } | |
8efc0c15 | 45 | } |
46 | ||
aa3378df | 47 | /* |
48 | * Clears any data from the buffer, making it empty. This does not actually | |
49 | * zero the memory. | |
50 | */ | |
8efc0c15 | 51 | |
6ae2364d | 52 | void |
5260325f | 53 | buffer_clear(Buffer *buffer) |
8efc0c15 | 54 | { |
5260325f | 55 | buffer->offset = 0; |
56 | buffer->end = 0; | |
8efc0c15 | 57 | } |
58 | ||
59 | /* Appends data to the buffer, expanding it if necessary. */ | |
60 | ||
6ae2364d | 61 | void |
6c0fa2b1 | 62 | buffer_append(Buffer *buffer, const void *data, u_int len) |
8efc0c15 | 63 | { |
6c0fa2b1 | 64 | void *p; |
65 | p = buffer_append_space(buffer, len); | |
66 | memcpy(p, data, len); | |
8efc0c15 | 67 | } |
68 | ||
aa3378df | 69 | /* |
70 | * Appends space to the buffer, expanding the buffer if necessary. This does | |
71 | * not actually copy the data into the buffer, but instead returns a pointer | |
72 | * to the allocated region. | |
73 | */ | |
8efc0c15 | 74 | |
6c0fa2b1 | 75 | void * |
76 | buffer_append_space(Buffer *buffer, u_int len) | |
8efc0c15 | 77 | { |
49525395 | 78 | u_int newlen; |
6c0fa2b1 | 79 | void *p; |
80 | ||
22d62d31 | 81 | if (len > 0x100000) |
82 | fatal("buffer_append_space: len %u not supported", len); | |
83 | ||
5260325f | 84 | /* If the buffer is empty, start using it from the beginning. */ |
85 | if (buffer->offset == buffer->end) { | |
86 | buffer->offset = 0; | |
87 | buffer->end = 0; | |
88 | } | |
89 | restart: | |
90 | /* If there is enough space to store all data, store it now. */ | |
91 | if (buffer->end + len < buffer->alloc) { | |
6c0fa2b1 | 92 | p = buffer->buf + buffer->end; |
5260325f | 93 | buffer->end += len; |
6c0fa2b1 | 94 | return p; |
5260325f | 95 | } |
aa3378df | 96 | /* |
97 | * If the buffer is quite empty, but all data is at the end, move the | |
98 | * data to the beginning and retry. | |
99 | */ | |
5260325f | 100 | if (buffer->offset > buffer->alloc / 2) { |
101 | memmove(buffer->buf, buffer->buf + buffer->offset, | |
102 | buffer->end - buffer->offset); | |
103 | buffer->end -= buffer->offset; | |
104 | buffer->offset = 0; | |
105 | goto restart; | |
106 | } | |
107 | /* Increase the size of the buffer and retry. */ | |
b6453d99 | 108 | |
49525395 | 109 | newlen = buffer->alloc + len + 32768; |
110 | if (newlen > 0xa00000) | |
22d62d31 | 111 | fatal("buffer_append_space: alloc %u not supported", |
49525395 | 112 | newlen); |
113 | buffer->buf = xrealloc(buffer->buf, newlen); | |
114 | buffer->alloc = newlen; | |
5260325f | 115 | goto restart; |
6c0fa2b1 | 116 | /* NOTREACHED */ |
8efc0c15 | 117 | } |
118 | ||
119 | /* Returns the number of bytes of data in the buffer. */ | |
120 | ||
1e3b8b07 | 121 | u_int |
5260325f | 122 | buffer_len(Buffer *buffer) |
8efc0c15 | 123 | { |
5260325f | 124 | return buffer->end - buffer->offset; |
8efc0c15 | 125 | } |
126 | ||
127 | /* Gets data from the beginning of the buffer. */ | |
128 | ||
b82a59f2 | 129 | int |
130 | buffer_get_ret(Buffer *buffer, void *buf, u_int len) | |
8efc0c15 | 131 | { |
b82a59f2 | 132 | if (len > buffer->end - buffer->offset) { |
133 | error("buffer_get_ret: trying to get more bytes %d than in buffer %d", | |
b37caf1a | 134 | len, buffer->end - buffer->offset); |
b82a59f2 | 135 | return (-1); |
136 | } | |
5260325f | 137 | memcpy(buf, buffer->buf + buffer->offset, len); |
138 | buffer->offset += len; | |
b82a59f2 | 139 | return (0); |
140 | } | |
141 | ||
142 | void | |
143 | buffer_get(Buffer *buffer, void *buf, u_int len) | |
144 | { | |
145 | if (buffer_get_ret(buffer, buf, len) == -1) | |
146 | fatal("buffer_get: buffer error"); | |
8efc0c15 | 147 | } |
148 | ||
149 | /* Consumes the given number of bytes from the beginning of the buffer. */ | |
150 | ||
b82a59f2 | 151 | int |
152 | buffer_consume_ret(Buffer *buffer, u_int bytes) | |
153 | { | |
154 | if (bytes > buffer->end - buffer->offset) { | |
155 | error("buffer_consume_ret: trying to get more bytes than in buffer"); | |
156 | return (-1); | |
157 | } | |
158 | buffer->offset += bytes; | |
159 | return (0); | |
160 | } | |
161 | ||
6ae2364d | 162 | void |
1e3b8b07 | 163 | buffer_consume(Buffer *buffer, u_int bytes) |
8efc0c15 | 164 | { |
b82a59f2 | 165 | if (buffer_consume_ret(buffer, bytes) == -1) |
166 | fatal("buffer_consume: buffer error"); | |
5260325f | 167 | } |
8efc0c15 | 168 | |
169 | /* Consumes the given number of bytes from the end of the buffer. */ | |
170 | ||
b82a59f2 | 171 | int |
172 | buffer_consume_end_ret(Buffer *buffer, u_int bytes) | |
173 | { | |
174 | if (bytes > buffer->end - buffer->offset) | |
175 | return (-1); | |
176 | buffer->end -= bytes; | |
177 | return (0); | |
178 | } | |
179 | ||
6ae2364d | 180 | void |
1e3b8b07 | 181 | buffer_consume_end(Buffer *buffer, u_int bytes) |
8efc0c15 | 182 | { |
b82a59f2 | 183 | if (buffer_consume_end_ret(buffer, bytes) == -1) |
f54651ce | 184 | fatal("buffer_consume_end: trying to get more bytes than in buffer"); |
5260325f | 185 | } |
8efc0c15 | 186 | |
187 | /* Returns a pointer to the first used byte in the buffer. */ | |
188 | ||
6c0fa2b1 | 189 | void * |
5260325f | 190 | buffer_ptr(Buffer *buffer) |
8efc0c15 | 191 | { |
5260325f | 192 | return buffer->buf + buffer->offset; |
8efc0c15 | 193 | } |
194 | ||
195 | /* Dumps the contents of the buffer to stderr. */ | |
196 | ||
6ae2364d | 197 | void |
5260325f | 198 | buffer_dump(Buffer *buffer) |
8efc0c15 | 199 | { |
f04181fe | 200 | u_int i; |
e6207598 | 201 | u_char *ucp = buffer->buf; |
5260325f | 202 | |
8002af61 | 203 | for (i = buffer->offset; i < buffer->end; i++) { |
204 | fprintf(stderr, "%02x", ucp[i]); | |
205 | if ((i-buffer->offset)%16==15) | |
206 | fprintf(stderr, "\r\n"); | |
207 | else if ((i-buffer->offset)%2==1) | |
208 | fprintf(stderr, " "); | |
209 | } | |
0490e609 | 210 | fprintf(stderr, "\r\n"); |
8efc0c15 | 211 | } |