larn(6): Fix two "use of index before limits check" issues.
[dragonfly.git] / usr.bin / telnet / ring.c
blobaf313bca12fceb5f66269a81abcd6e02ad689a14
1 /*
2 * Copyright (c) 1988, 1993
3 * The Regents of the University of California. All rights reserved.
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * @(#)ring.c 8.2 (Berkeley) 5/30/95
30 * $FreeBSD: src/crypto/telnet/telnet/ring.c,v 1.2.8.2 2002/04/13 10:59:08 markm Exp $
34 * This defines a structure for a ring buffer.
36 * The circular buffer has two parts:
37 *(((
38 * full: [consume, supply)
39 * empty: [supply, consume)
40 *]]]
44 #include <errno.h>
45 #include <stdio.h>
46 #include <string.h>
48 #ifdef size_t
49 #undef size_t
50 #endif
52 #include <sys/types.h>
53 #ifndef FILIO_H
54 #include <sys/ioctl.h>
55 #endif
56 #include <sys/socket.h>
58 #include "ring.h"
59 #include "general.h"
61 /* Internal macros */
63 #if !defined(MIN)
64 #define MIN(a,b) (((a)<(b))? (a):(b))
65 #endif /* !defined(MIN) */
67 #define ring_subtract(d,a,b) (((a)-(b) >= 0)? \
68 (a)-(b): (((a)-(b))+(d)->size))
70 #define ring_increment(d,a,c) (((a)+(c) < (d)->top)? \
71 (a)+(c) : (((a)+(c))-(d)->size))
73 #define ring_decrement(d,a,c) (((a)-(c) >= (d)->bottom)? \
74 (a)-(c) : (((a)-(c))-(d)->size))
78 * The following is a clock, used to determine full, empty, etc.
80 * There is some trickiness here. Since the ring buffers are initialized
81 * to ZERO on allocation, we need to make sure, when interpreting the
82 * clock, that when the times are EQUAL, then the buffer is FULL.
84 static u_long ring_clock = 0;
87 #define ring_empty(d) (((d)->consume == (d)->supply) && \
88 ((d)->consumetime >= (d)->supplytime))
89 #define ring_full(d) (((d)->supply == (d)->consume) && \
90 ((d)->supplytime > (d)->consumetime))
92 /* Buffer state transition routines */
94 int
95 ring_init(Ring *ring, unsigned char *buffer, int count)
97 memset((char *)ring, 0, sizeof *ring);
99 ring->size = count;
101 ring->supply = ring->consume = ring->bottom = buffer;
103 ring->top = ring->bottom+ring->size;
105 #ifdef ENCRYPTION
106 ring->clearto = 0;
107 #endif /* ENCRYPTION */
109 return 1;
112 /* Mark routines */
115 * Mark the most recently supplied byte.
118 void
119 ring_mark(Ring *ring)
121 ring->mark = ring_decrement(ring, ring->supply, 1);
125 * Is the ring pointing to the mark?
129 ring_at_mark(Ring *ring)
131 if (ring->mark == ring->consume) {
132 return 1;
133 } else {
134 return 0;
139 * Clear any mark set on the ring.
142 void
143 ring_clear_mark(Ring *ring)
145 ring->mark = 0;
149 * Add characters from current segment to ring buffer.
151 void
152 ring_supplied(Ring *ring, int count)
154 ring->supply = ring_increment(ring, ring->supply, count);
155 ring->supplytime = ++ring_clock;
159 * We have just consumed "c" bytes.
161 void
162 ring_consumed(Ring *ring, int count)
164 if (count == 0) /* don't update anything */
165 return;
167 if (ring->mark &&
168 (ring_subtract(ring, ring->mark, ring->consume) < count)) {
169 ring->mark = 0;
171 #ifdef ENCRYPTION
172 if (ring->consume < ring->clearto &&
173 ring->clearto <= ring->consume + count)
174 ring->clearto = 0;
175 else if (ring->consume + count > ring->top &&
176 ring->bottom <= ring->clearto &&
177 ring->bottom + ((ring->consume + count) - ring->top))
178 ring->clearto = 0;
179 #endif /* ENCRYPTION */
180 ring->consume = ring_increment(ring, ring->consume, count);
181 ring->consumetime = ++ring_clock;
183 * Try to encourage "ring_empty_consecutive()" to be large.
185 if (ring_empty(ring)) {
186 ring->consume = ring->supply = ring->bottom;
192 /* Buffer state query routines */
195 /* Number of bytes that may be supplied */
197 ring_empty_count(Ring *ring)
199 if (ring_empty(ring)) { /* if empty */
200 return ring->size;
201 } else {
202 return ring_subtract(ring, ring->consume, ring->supply);
206 /* number of CONSECUTIVE bytes that may be supplied */
208 ring_empty_consecutive(Ring *ring)
210 if ((ring->consume < ring->supply) || ring_empty(ring)) {
212 * if consume is "below" supply, or empty, then
213 * return distance to the top
215 return ring_subtract(ring, ring->top, ring->supply);
216 } else {
218 * else, return what we may.
220 return ring_subtract(ring, ring->consume, ring->supply);
224 /* Return the number of bytes that are available for consuming
225 * (but don't give more than enough to get to cross over set mark)
229 ring_full_count(Ring *ring)
231 if ((ring->mark == 0) || (ring->mark == ring->consume)) {
232 if (ring_full(ring)) {
233 return ring->size; /* nothing consumed, but full */
234 } else {
235 return ring_subtract(ring, ring->supply, ring->consume);
237 } else {
238 return ring_subtract(ring, ring->mark, ring->consume);
243 * Return the number of CONSECUTIVE bytes available for consuming.
244 * However, don't return more than enough to cross over set mark.
247 ring_full_consecutive(Ring *ring)
249 if ((ring->mark == 0) || (ring->mark == ring->consume)) {
250 if ((ring->supply < ring->consume) || ring_full(ring)) {
251 return ring_subtract(ring, ring->top, ring->consume);
252 } else {
253 return ring_subtract(ring, ring->supply, ring->consume);
255 } else {
256 if (ring->mark < ring->consume) {
257 return ring_subtract(ring, ring->top, ring->consume);
258 } else { /* Else, distance to mark */
259 return ring_subtract(ring, ring->mark, ring->consume);
265 * Move data into the "supply" portion of of the ring buffer.
267 void
268 ring_supply_data(Ring *ring, unsigned char *buffer, int count)
270 int i;
272 while (count) {
273 i = MIN(count, ring_empty_consecutive(ring));
274 memcpy(ring->supply, buffer, i);
275 ring_supplied(ring, i);
276 count -= i;
277 buffer += i;
281 #ifdef ENCRYPTION
282 void
283 ring_encrypt(Ring *ring, void (*encryptor)(unsigned char *, int))
285 unsigned char *s, *c;
287 if (ring_empty(ring) || ring->clearto == ring->supply)
288 return;
290 if (!(c = ring->clearto))
291 c = ring->consume;
293 s = ring->supply;
295 if (s <= c) {
296 (*encryptor)(c, ring->top - c);
297 (*encryptor)(ring->bottom, s - ring->bottom);
298 } else
299 (*encryptor)(c, s - c);
301 ring->clearto = ring->supply;
304 void
305 ring_clearto(Ring *ring)
307 if (!ring_empty(ring))
308 ring->clearto = ring->supply;
309 else
310 ring->clearto = 0;
312 #endif /* ENCRYPTION */