1 /*
2 * Copyright (c) 1988, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #include "telnet_locl.h"
35
36 RCSID("$Id$");
37
38 /*
39 * This defines a structure for a ring buffer.
40 *
41 * The circular buffer has two parts:
42 *(((
43 * full: [consume, supply)
44 * empty: [supply, consume)
45 *]]]
46 *
47 */
48
49 /* Internal macros */
50
51 #define ring_subtract(d,a,b) (((a)-(b) >= 0)? \
52 (a)-(b): (((a)-(b))+(d)->size))
53
54 #define ring_increment(d,a,c) (((a)+(c) < (d)->top)? \
55 (a)+(c) : (((a)+(c))-(d)->size))
56
57 #define ring_decrement(d,a,c) (((a)-(c) >= (d)->bottom)? \
58 (a)-(c) : (((a)-(c))-(d)->size))
59
60
61 /*
62 * The following is a clock, used to determine full, empty, etc.
63 *
64 * There is some trickiness here. Since the ring buffers are initialized
65 * to ZERO on allocation, we need to make sure, when interpreting the
66 * clock, that when the times are EQUAL, then the buffer is FULL.
67 */
68 static u_long ring_clock = 0;
69
70
71 #define ring_empty(d) (((d)->consume == (d)->supply) && \
72 ((d)->consumetime >= (d)->supplytime))
73 #define ring_full(d) (((d)->supply == (d)->consume) && \
74 ((d)->supplytime > (d)->consumetime))
75
76
77
78
79
80 /* Buffer state transition routines */
81
82 int
ring_init(Ring * ring,unsigned char * buffer,int count)83 ring_init(Ring *ring, unsigned char *buffer, int count)
84 {
85 memset(ring, 0, sizeof *ring);
86
87 ring->size = count;
88
89 ring->supply = ring->consume = ring->bottom = buffer;
90
91 ring->top = ring->bottom+ring->size;
92
93 #if defined(ENCRYPTION)
94 ring->clearto = 0;
95 #endif
96
97 return 1;
98 }
99
100 /* Mark routines */
101
102 /*
103 * Mark the most recently supplied byte.
104 */
105
106 void
ring_mark(Ring * ring)107 ring_mark(Ring *ring)
108 {
109 ring->mark = ring_decrement(ring, ring->supply, 1);
110 }
111
112 /*
113 * Is the ring pointing to the mark?
114 */
115
116 int
ring_at_mark(Ring * ring)117 ring_at_mark(Ring *ring)
118 {
119 if (ring->mark == ring->consume) {
120 return 1;
121 } else {
122 return 0;
123 }
124 }
125
126 /*
127 * Clear any mark set on the ring.
128 */
129
130 void
ring_clear_mark(Ring * ring)131 ring_clear_mark(Ring *ring)
132 {
133 ring->mark = 0;
134 }
135
136 /*
137 * Add characters from current segment to ring buffer.
138 */
139 void
ring_supplied(Ring * ring,int count)140 ring_supplied(Ring *ring, int count)
141 {
142 ring->supply = ring_increment(ring, ring->supply, count);
143 ring->supplytime = ++ring_clock;
144 }
145
146 /*
147 * We have just consumed "c" bytes.
148 */
149 void
ring_consumed(Ring * ring,int count)150 ring_consumed(Ring *ring, int count)
151 {
152 if (count == 0) /* don't update anything */
153 return;
154
155 if (ring->mark &&
156 (ring_subtract(ring, ring->mark, ring->consume) < count)) {
157 ring->mark = 0;
158 }
159 #if defined(ENCRYPTION)
160 if (ring->consume < ring->clearto &&
161 ring->clearto <= ring->consume + count)
162 ring->clearto = 0;
163 else if (ring->consume + count > ring->top &&
164 ring->bottom <= ring->clearto &&
165 ring->bottom + ((ring->consume + count) - ring->top))
166 ring->clearto = 0;
167 #endif
168 ring->consume = ring_increment(ring, ring->consume, count);
169 ring->consumetime = ++ring_clock;
170 /*
171 * Try to encourage "ring_empty_consecutive()" to be large.
172 */
173 if (ring_empty(ring)) {
174 ring->consume = ring->supply = ring->bottom;
175 }
176 }
177
178
179
180 /* Buffer state query routines */
181
182
183 /* Number of bytes that may be supplied */
184 int
ring_empty_count(Ring * ring)185 ring_empty_count(Ring *ring)
186 {
187 if (ring_empty(ring)) { /* if empty */
188 return ring->size;
189 } else {
190 return ring_subtract(ring, ring->consume, ring->supply);
191 }
192 }
193
194 /* number of CONSECUTIVE bytes that may be supplied */
195 int
ring_empty_consecutive(Ring * ring)196 ring_empty_consecutive(Ring *ring)
197 {
198 if ((ring->consume < ring->supply) || ring_empty(ring)) {
199 /*
200 * if consume is "below" supply, or empty, then
201 * return distance to the top
202 */
203 return ring_subtract(ring, ring->top, ring->supply);
204 } else {
205 /*
206 * else, return what we may.
207 */
208 return ring_subtract(ring, ring->consume, ring->supply);
209 }
210 }
211
212 /* Return the number of bytes that are available for consuming
213 * (but don't give more than enough to get to cross over set mark)
214 */
215
216 int
ring_full_count(Ring * ring)217 ring_full_count(Ring *ring)
218 {
219 if ((ring->mark == 0) || (ring->mark == ring->consume)) {
220 if (ring_full(ring)) {
221 return ring->size; /* nothing consumed, but full */
222 } else {
223 return ring_subtract(ring, ring->supply, ring->consume);
224 }
225 } else {
226 return ring_subtract(ring, ring->mark, ring->consume);
227 }
228 }
229
230 /*
231 * Return the number of CONSECUTIVE bytes available for consuming.
232 * However, don't return more than enough to cross over set mark.
233 */
234 int
ring_full_consecutive(Ring * ring)235 ring_full_consecutive(Ring *ring)
236 {
237 if ((ring->mark == 0) || (ring->mark == ring->consume)) {
238 if ((ring->supply < ring->consume) || ring_full(ring)) {
239 return ring_subtract(ring, ring->top, ring->consume);
240 } else {
241 return ring_subtract(ring, ring->supply, ring->consume);
242 }
243 } else {
244 if (ring->mark < ring->consume) {
245 return ring_subtract(ring, ring->top, ring->consume);
246 } else { /* Else, distance to mark */
247 return ring_subtract(ring, ring->mark, ring->consume);
248 }
249 }
250 }
251
252 /*
253 * Move data into the "supply" portion of of the ring buffer.
254 */
255 void
ring_supply_data(Ring * ring,unsigned char * buffer,int count)256 ring_supply_data(Ring *ring, unsigned char *buffer, int count)
257 {
258 int i;
259
260 while (count) {
261 i = min(count, ring_empty_consecutive(ring));
262 memmove(ring->supply, buffer, i);
263 ring_supplied(ring, i);
264 count -= i;
265 buffer += i;
266 }
267 }
268
269 #ifdef notdef
270
271 /*
272 * Move data from the "consume" portion of the ring buffer
273 */
274 void
ring_consume_data(Ring * ring,unsigned char * buffer,int count)275 ring_consume_data(Ring *ring, unsigned char *buffer, int count)
276 {
277 int i;
278
279 while (count) {
280 i = min(count, ring_full_consecutive(ring));
281 memmove(buffer, ring->consume, i);
282 ring_consumed(ring, i);
283 count -= i;
284 buffer += i;
285 }
286 }
287 #endif
288
289 #if defined(ENCRYPTION)
290 void
ring_encrypt(Ring * ring,void (* encryptor)(unsigned char *,int))291 ring_encrypt(Ring *ring, void (*encryptor)(unsigned char *, int))
292 {
293 unsigned char *s, *c;
294
295 if (ring_empty(ring) || ring->clearto == ring->supply)
296 return;
297
298 if (!(c = ring->clearto))
299 c = ring->consume;
300
301 s = ring->supply;
302
303 if (s <= c) {
304 (*encryptor)(c, ring->top - c);
305 (*encryptor)(ring->bottom, s - ring->bottom);
306 } else
307 (*encryptor)(c, s - c);
308
309 ring->clearto = ring->supply;
310 }
311
312 void
ring_clearto(Ring * ring)313 ring_clearto(Ring *ring)
314 {
315 if (!ring_empty(ring))
316 ring->clearto = ring->supply;
317 else
318 ring->clearto = 0;
319 }
320 #endif
321
322