]>
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" | |
8efc0c15 | 15 | |
16 | #include "xmalloc.h" | |
17 | #include "buffer.h" | |
42f11eb2 | 18 | #include "log.h" |
8efc0c15 | 19 | |
20 | /* Initializes the buffer structure. */ | |
21 | ||
6ae2364d | 22 | void |
5260325f | 23 | buffer_init(Buffer *buffer) |
8efc0c15 | 24 | { |
20419cc1 | 25 | const u_int len = 4096; |
26 | ||
27 | buffer->alloc = 0; | |
28 | buffer->buf = xmalloc(len); | |
29 | buffer->alloc = len; | |
5260325f | 30 | buffer->offset = 0; |
31 | buffer->end = 0; | |
8efc0c15 | 32 | } |
33 | ||
34 | /* Frees any memory used for the buffer. */ | |
35 | ||
6ae2364d | 36 | void |
5260325f | 37 | buffer_free(Buffer *buffer) |
8efc0c15 | 38 | { |
20419cc1 | 39 | if (buffer->alloc > 0) { |
40 | memset(buffer->buf, 0, buffer->alloc); | |
5bd34316 | 41 | buffer->alloc = 0; |
20419cc1 | 42 | xfree(buffer->buf); |
43 | } | |
8efc0c15 | 44 | } |
45 | ||
aa3378df | 46 | /* |
47 | * Clears any data from the buffer, making it empty. This does not actually | |
48 | * zero the memory. | |
49 | */ | |
8efc0c15 | 50 | |
6ae2364d | 51 | void |
5260325f | 52 | buffer_clear(Buffer *buffer) |
8efc0c15 | 53 | { |
5260325f | 54 | buffer->offset = 0; |
55 | buffer->end = 0; | |
8efc0c15 | 56 | } |
57 | ||
58 | /* Appends data to the buffer, expanding it if necessary. */ | |
59 | ||
6ae2364d | 60 | void |
6c0fa2b1 | 61 | buffer_append(Buffer *buffer, const void *data, u_int len) |
8efc0c15 | 62 | { |
6c0fa2b1 | 63 | void *p; |
64 | p = buffer_append_space(buffer, len); | |
65 | memcpy(p, data, len); | |
8efc0c15 | 66 | } |
67 | ||
aa3378df | 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 | */ | |
8efc0c15 | 73 | |
6c0fa2b1 | 74 | void * |
75 | buffer_append_space(Buffer *buffer, u_int len) | |
8efc0c15 | 76 | { |
49525395 | 77 | u_int newlen; |
6c0fa2b1 | 78 | void *p; |
79 | ||
fa1d7d85 | 80 | if (len > BUFFER_MAX_CHUNK) |
22d62d31 | 81 | fatal("buffer_append_space: len %u not supported", len); |
82 | ||
5260325f | 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) { | |
6c0fa2b1 | 91 | p = buffer->buf + buffer->end; |
5260325f | 92 | buffer->end += len; |
6c0fa2b1 | 93 | return p; |
5260325f | 94 | } |
aa3378df | 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 | */ | |
fa1d7d85 | 99 | if (buffer->offset > MIN(buffer->alloc, BUFFER_MAX_CHUNK)) { |
5260325f | 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. */ | |
b6453d99 | 107 | |
49525395 | 108 | newlen = buffer->alloc + len + 32768; |
fa1d7d85 | 109 | if (newlen > BUFFER_MAX_LEN) |
22d62d31 | 110 | fatal("buffer_append_space: alloc %u not supported", |
49525395 | 111 | newlen); |
112 | buffer->buf = xrealloc(buffer->buf, newlen); | |
113 | buffer->alloc = newlen; | |
5260325f | 114 | goto restart; |
6c0fa2b1 | 115 | /* NOTREACHED */ |
8efc0c15 | 116 | } |
117 | ||
118 | /* Returns the number of bytes of data in the buffer. */ | |
119 | ||
1e3b8b07 | 120 | u_int |
5260325f | 121 | buffer_len(Buffer *buffer) |
8efc0c15 | 122 | { |
5260325f | 123 | return buffer->end - buffer->offset; |
8efc0c15 | 124 | } |
125 | ||
126 | /* Gets data from the beginning of the buffer. */ | |
127 | ||
b82a59f2 | 128 | int |
129 | buffer_get_ret(Buffer *buffer, void *buf, u_int len) | |
8efc0c15 | 130 | { |
b82a59f2 | 131 | if (len > buffer->end - buffer->offset) { |
132 | error("buffer_get_ret: trying to get more bytes %d than in buffer %d", | |
b37caf1a | 133 | len, buffer->end - buffer->offset); |
b82a59f2 | 134 | return (-1); |
135 | } | |
5260325f | 136 | memcpy(buf, buffer->buf + buffer->offset, len); |
137 | buffer->offset += len; | |
b82a59f2 | 138 | return (0); |
139 | } | |
140 | ||
141 | void | |
142 | buffer_get(Buffer *buffer, void *buf, u_int len) | |
143 | { | |
144 | if (buffer_get_ret(buffer, buf, len) == -1) | |
145 | fatal("buffer_get: buffer error"); | |
8efc0c15 | 146 | } |
147 | ||
148 | /* Consumes the given number of bytes from the beginning of the buffer. */ | |
149 | ||
b82a59f2 | 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 | ||
6ae2364d | 161 | void |
1e3b8b07 | 162 | buffer_consume(Buffer *buffer, u_int bytes) |
8efc0c15 | 163 | { |
b82a59f2 | 164 | if (buffer_consume_ret(buffer, bytes) == -1) |
165 | fatal("buffer_consume: buffer error"); | |
5260325f | 166 | } |
8efc0c15 | 167 | |
168 | /* Consumes the given number of bytes from the end of the buffer. */ | |
169 | ||
b82a59f2 | 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 | ||
6ae2364d | 179 | void |
1e3b8b07 | 180 | buffer_consume_end(Buffer *buffer, u_int bytes) |
8efc0c15 | 181 | { |
b82a59f2 | 182 | if (buffer_consume_end_ret(buffer, bytes) == -1) |
f54651ce | 183 | fatal("buffer_consume_end: trying to get more bytes than in buffer"); |
5260325f | 184 | } |
8efc0c15 | 185 | |
186 | /* Returns a pointer to the first used byte in the buffer. */ | |
187 | ||
6c0fa2b1 | 188 | void * |
5260325f | 189 | buffer_ptr(Buffer *buffer) |
8efc0c15 | 190 | { |
5260325f | 191 | return buffer->buf + buffer->offset; |
8efc0c15 | 192 | } |
193 | ||
194 | /* Dumps the contents of the buffer to stderr. */ | |
195 | ||
6ae2364d | 196 | void |
5260325f | 197 | buffer_dump(Buffer *buffer) |
8efc0c15 | 198 | { |
f04181fe | 199 | u_int i; |
e6207598 | 200 | u_char *ucp = buffer->buf; |
5260325f | 201 | |
8002af61 | 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 | } | |
0490e609 | 209 | fprintf(stderr, "\r\n"); |
8efc0c15 | 210 | } |