]>
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" | |
22d62d31 | 15 | RCSID("$OpenBSD: buffer.c,v 1.16 2002/06/26 08:54:18 markus 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 | { |
5260325f | 26 | buffer->alloc = 4096; |
27 | buffer->buf = xmalloc(buffer->alloc); | |
28 | buffer->offset = 0; | |
29 | buffer->end = 0; | |
8efc0c15 | 30 | } |
31 | ||
32 | /* Frees any memory used for the buffer. */ | |
33 | ||
6ae2364d | 34 | void |
5260325f | 35 | buffer_free(Buffer *buffer) |
8efc0c15 | 36 | { |
5260325f | 37 | memset(buffer->buf, 0, buffer->alloc); |
38 | xfree(buffer->buf); | |
8efc0c15 | 39 | } |
40 | ||
aa3378df | 41 | /* |
42 | * Clears any data from the buffer, making it empty. This does not actually | |
43 | * zero the memory. | |
44 | */ | |
8efc0c15 | 45 | |
6ae2364d | 46 | void |
5260325f | 47 | buffer_clear(Buffer *buffer) |
8efc0c15 | 48 | { |
5260325f | 49 | buffer->offset = 0; |
50 | buffer->end = 0; | |
8efc0c15 | 51 | } |
52 | ||
53 | /* Appends data to the buffer, expanding it if necessary. */ | |
54 | ||
6ae2364d | 55 | void |
6c0fa2b1 | 56 | buffer_append(Buffer *buffer, const void *data, u_int len) |
8efc0c15 | 57 | { |
6c0fa2b1 | 58 | void *p; |
59 | p = buffer_append_space(buffer, len); | |
60 | memcpy(p, data, len); | |
8efc0c15 | 61 | } |
62 | ||
aa3378df | 63 | /* |
64 | * Appends space to the buffer, expanding the buffer if necessary. This does | |
65 | * not actually copy the data into the buffer, but instead returns a pointer | |
66 | * to the allocated region. | |
67 | */ | |
8efc0c15 | 68 | |
6c0fa2b1 | 69 | void * |
70 | buffer_append_space(Buffer *buffer, u_int len) | |
8efc0c15 | 71 | { |
6c0fa2b1 | 72 | void *p; |
73 | ||
22d62d31 | 74 | if (len > 0x100000) |
75 | fatal("buffer_append_space: len %u not supported", len); | |
76 | ||
5260325f | 77 | /* If the buffer is empty, start using it from the beginning. */ |
78 | if (buffer->offset == buffer->end) { | |
79 | buffer->offset = 0; | |
80 | buffer->end = 0; | |
81 | } | |
82 | restart: | |
83 | /* If there is enough space to store all data, store it now. */ | |
84 | if (buffer->end + len < buffer->alloc) { | |
6c0fa2b1 | 85 | p = buffer->buf + buffer->end; |
5260325f | 86 | buffer->end += len; |
6c0fa2b1 | 87 | return p; |
5260325f | 88 | } |
aa3378df | 89 | /* |
90 | * If the buffer is quite empty, but all data is at the end, move the | |
91 | * data to the beginning and retry. | |
92 | */ | |
5260325f | 93 | if (buffer->offset > buffer->alloc / 2) { |
94 | memmove(buffer->buf, buffer->buf + buffer->offset, | |
95 | buffer->end - buffer->offset); | |
96 | buffer->end -= buffer->offset; | |
97 | buffer->offset = 0; | |
98 | goto restart; | |
99 | } | |
100 | /* Increase the size of the buffer and retry. */ | |
101 | buffer->alloc += len + 32768; | |
22d62d31 | 102 | if (buffer->alloc > 0xa00000) |
103 | fatal("buffer_append_space: alloc %u not supported", | |
104 | buffer->alloc); | |
5260325f | 105 | buffer->buf = xrealloc(buffer->buf, buffer->alloc); |
106 | goto restart; | |
6c0fa2b1 | 107 | /* NOTREACHED */ |
8efc0c15 | 108 | } |
109 | ||
110 | /* Returns the number of bytes of data in the buffer. */ | |
111 | ||
1e3b8b07 | 112 | u_int |
5260325f | 113 | buffer_len(Buffer *buffer) |
8efc0c15 | 114 | { |
5260325f | 115 | return buffer->end - buffer->offset; |
8efc0c15 | 116 | } |
117 | ||
118 | /* Gets data from the beginning of the buffer. */ | |
119 | ||
6ae2364d | 120 | void |
6c0fa2b1 | 121 | buffer_get(Buffer *buffer, void *buf, u_int len) |
8efc0c15 | 122 | { |
5260325f | 123 | if (len > buffer->end - buffer->offset) |
b37caf1a | 124 | fatal("buffer_get: trying to get more bytes %d than in buffer %d", |
125 | len, buffer->end - buffer->offset); | |
5260325f | 126 | memcpy(buf, buffer->buf + buffer->offset, len); |
127 | buffer->offset += len; | |
8efc0c15 | 128 | } |
129 | ||
130 | /* Consumes the given number of bytes from the beginning of the buffer. */ | |
131 | ||
6ae2364d | 132 | void |
1e3b8b07 | 133 | buffer_consume(Buffer *buffer, u_int bytes) |
8efc0c15 | 134 | { |
5260325f | 135 | if (bytes > buffer->end - buffer->offset) |
f54651ce | 136 | fatal("buffer_consume: trying to get more bytes than in buffer"); |
5260325f | 137 | buffer->offset += bytes; |
138 | } | |
8efc0c15 | 139 | |
140 | /* Consumes the given number of bytes from the end of the buffer. */ | |
141 | ||
6ae2364d | 142 | void |
1e3b8b07 | 143 | buffer_consume_end(Buffer *buffer, u_int bytes) |
8efc0c15 | 144 | { |
5260325f | 145 | if (bytes > buffer->end - buffer->offset) |
f54651ce | 146 | fatal("buffer_consume_end: trying to get more bytes than in buffer"); |
5260325f | 147 | buffer->end -= bytes; |
148 | } | |
8efc0c15 | 149 | |
150 | /* Returns a pointer to the first used byte in the buffer. */ | |
151 | ||
6c0fa2b1 | 152 | void * |
5260325f | 153 | buffer_ptr(Buffer *buffer) |
8efc0c15 | 154 | { |
5260325f | 155 | return buffer->buf + buffer->offset; |
8efc0c15 | 156 | } |
157 | ||
158 | /* Dumps the contents of the buffer to stderr. */ | |
159 | ||
6ae2364d | 160 | void |
5260325f | 161 | buffer_dump(Buffer *buffer) |
8efc0c15 | 162 | { |
5260325f | 163 | int i; |
e6207598 | 164 | u_char *ucp = buffer->buf; |
5260325f | 165 | |
8002af61 | 166 | for (i = buffer->offset; i < buffer->end; i++) { |
167 | fprintf(stderr, "%02x", ucp[i]); | |
168 | if ((i-buffer->offset)%16==15) | |
169 | fprintf(stderr, "\r\n"); | |
170 | else if ((i-buffer->offset)%2==1) | |
171 | fprintf(stderr, " "); | |
172 | } | |
0490e609 | 173 | fprintf(stderr, "\r\n"); |
8efc0c15 | 174 | } |