Assorted whitespace cleanup and typo fixes.
[haiku.git] / src / bin / network / telnet / ring.c
blob07061338865b683675cb3c52ec89f43d03538578
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. 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.
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.
34 #if 0
35 #ifndef lint
36 static const char sccsid[] = "@(#)ring.c 8.2 (Berkeley) 5/30/95";
37 #endif
38 #endif
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD: src/contrib/telnet/telnet/ring.c,v 1.7 2003/05/04 02:54:48 obrien Exp $");
43 * This defines a structure for a ring buffer.
45 * The circular buffer has two parts:
46 *(((
47 * full: [consume, supply)
48 * empty: [supply, consume)
49 *]]]
53 #include <errno.h>
54 #include <stdio.h>
55 #include <string.h>
57 #ifdef size_t
58 #undef size_t
59 #endif
61 #include <sys/types.h>
62 #ifndef FILIO_H
63 #include <sys/ioctl.h>
64 #endif
65 #include <sys/socket.h>
67 #include "ring.h"
68 #include "general.h"
70 /* Internal macros */
72 #if !defined(MIN)
73 #define MIN(a,b) (((a)<(b))? (a):(b))
74 #endif /* !defined(MIN) */
76 #define ring_subtract(d,a,b) (((a)-(b) >= 0)? \
77 (a)-(b): (((a)-(b))+(d)->size))
79 #define ring_increment(d,a,c) (((a)+(c) < (d)->top)? \
80 (a)+(c) : (((a)+(c))-(d)->size))
82 #define ring_decrement(d,a,c) (((a)-(c) >= (d)->bottom)? \
83 (a)-(c) : (((a)-(c))-(d)->size))
87 * The following is a clock, used to determine full, empty, etc.
89 * There is some trickiness here. Since the ring buffers are initialized
90 * to ZERO on allocation, we need to make sure, when interpreting the
91 * clock, that when the times are EQUAL, then the buffer is FULL.
93 static u_long ring_clock = 0;
96 #define ring_empty(d) (((d)->consume == (d)->supply) && \
97 ((d)->consumetime >= (d)->supplytime))
98 #define ring_full(d) (((d)->supply == (d)->consume) && \
99 ((d)->supplytime > (d)->consumetime))
101 /* Buffer state transition routines */
104 ring_init(Ring *ring, unsigned char *buffer, int count)
106 memset((char *)ring, 0, sizeof *ring);
108 ring->size = count;
110 ring->supply = ring->consume = ring->bottom = buffer;
112 ring->top = ring->bottom+ring->size;
114 #ifdef ENCRYPTION
115 ring->clearto = 0;
116 #endif /* ENCRYPTION */
118 return 1;
121 /* Mark routines */
124 * Mark the most recently supplied byte.
127 void
128 ring_mark(Ring *ring)
130 ring->mark = ring_decrement(ring, ring->supply, 1);
134 * Is the ring pointing to the mark?
138 ring_at_mark(Ring *ring)
140 if (ring->mark == ring->consume) {
141 return 1;
142 } else {
143 return 0;
148 * Clear any mark set on the ring.
151 void
152 ring_clear_mark(Ring *ring)
154 ring->mark = 0;
158 * Add characters from current segment to ring buffer.
160 void
161 ring_supplied(Ring *ring, int count)
163 ring->supply = ring_increment(ring, ring->supply, count);
164 ring->supplytime = ++ring_clock;
168 * We have just consumed "c" bytes.
170 void
171 ring_consumed(Ring *ring, int count)
173 if (count == 0) /* don't update anything */
174 return;
176 if (ring->mark &&
177 (ring_subtract(ring, ring->mark, ring->consume) < count)) {
178 ring->mark = 0;
180 #ifdef ENCRYPTION
181 if (ring->consume < ring->clearto &&
182 ring->clearto <= ring->consume + count)
183 ring->clearto = 0;
184 else if (ring->consume + count > ring->top &&
185 ring->bottom <= ring->clearto &&
186 ring->bottom + ((ring->consume + count) - ring->top))
187 ring->clearto = 0;
188 #endif /* ENCRYPTION */
189 ring->consume = ring_increment(ring, ring->consume, count);
190 ring->consumetime = ++ring_clock;
192 * Try to encourage "ring_empty_consecutive()" to be large.
194 if (ring_empty(ring)) {
195 ring->consume = ring->supply = ring->bottom;
201 /* Buffer state query routines */
204 /* Number of bytes that may be supplied */
206 ring_empty_count(Ring *ring)
208 if (ring_empty(ring)) { /* if empty */
209 return ring->size;
210 } else {
211 return ring_subtract(ring, ring->consume, ring->supply);
215 /* number of CONSECUTIVE bytes that may be supplied */
217 ring_empty_consecutive(Ring *ring)
219 if ((ring->consume < ring->supply) || ring_empty(ring)) {
221 * if consume is "below" supply, or empty, then
222 * return distance to the top
224 return ring_subtract(ring, ring->top, ring->supply);
225 } else {
227 * else, return what we may.
229 return ring_subtract(ring, ring->consume, ring->supply);
233 /* Return the number of bytes that are available for consuming
234 * (but don't give more than enough to get to cross over set mark)
238 ring_full_count(Ring *ring)
240 if ((ring->mark == 0) || (ring->mark == ring->consume)) {
241 if (ring_full(ring)) {
242 return ring->size; /* nothing consumed, but full */
243 } else {
244 return ring_subtract(ring, ring->supply, ring->consume);
246 } else {
247 return ring_subtract(ring, ring->mark, ring->consume);
252 * Return the number of CONSECUTIVE bytes available for consuming.
253 * However, don't return more than enough to cross over set mark.
256 ring_full_consecutive(Ring *ring)
258 if ((ring->mark == 0) || (ring->mark == ring->consume)) {
259 if ((ring->supply < ring->consume) || ring_full(ring)) {
260 return ring_subtract(ring, ring->top, ring->consume);
261 } else {
262 return ring_subtract(ring, ring->supply, ring->consume);
264 } else {
265 if (ring->mark < ring->consume) {
266 return ring_subtract(ring, ring->top, ring->consume);
267 } else { /* Else, distance to mark */
268 return ring_subtract(ring, ring->mark, ring->consume);
274 * Move data into the "supply" portion of of the ring buffer.
276 void
277 ring_supply_data(Ring *ring, unsigned char *buffer, int count)
279 int i;
281 while (count) {
282 i = MIN(count, ring_empty_consecutive(ring));
283 memcpy(ring->supply, buffer, i);
284 ring_supplied(ring, i);
285 count -= i;
286 buffer += i;
290 #ifdef ENCRYPTION
291 void
292 ring_encrypt(Ring *ring, void (*encryptor)(unsigned char *, int))
294 unsigned char *s, *c;
296 if (ring_empty(ring) || ring->clearto == ring->supply)
297 return;
299 if (!(c = ring->clearto))
300 c = ring->consume;
302 s = ring->supply;
304 if (s <= c) {
305 (*encryptor)(c, ring->top - c);
306 (*encryptor)(ring->bottom, s - ring->bottom);
307 } else
308 (*encryptor)(c, s - c);
310 ring->clearto = ring->supply;
313 void
314 ring_clearto(ring)
315 Ring *ring;
317 if (!ring_empty(ring))
318 ring->clearto = ring->supply;
319 else
320 ring->clearto = 0;
322 #endif /* ENCRYPTION */