No empty .Rs/.Re
[netbsd-mini2440.git] / usr.bin / telnet / ring.c
bloba08fd658508d44e2784a956fd83ded3e68ceb67f
1 /* $NetBSD: ring.c,v 1.12 2003/07/14 15:56:29 itojun Exp $ */
3 /*
4 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)ring.c 8.2 (Berkeley) 5/30/95";
36 #else
37 __RCSID("$NetBSD: ring.c,v 1.12 2003/07/14 15:56:29 itojun Exp $");
38 #endif
39 #endif /* not lint */
42 * This defines a structure for a ring buffer.
44 * The circular buffer has two parts:
45 *(((
46 * full: [consume, supply)
47 * empty: [supply, consume)
48 *]]]
52 #include <stdio.h>
53 #include <string.h>
54 #include <strings.h>
55 #include <errno.h>
56 #include <sys/types.h>
57 #include <sys/ioctl.h>
58 #include <sys/socket.h>
60 #include "ring.h"
61 #include "general.h"
63 /* Internal macros */
65 #if !defined(MIN)
66 #define MIN(a,b) (((a)<(b))? (a):(b))
67 #endif /* !defined(MIN) */
69 #define ring_subtract(d,a,b) (((a)-(b) >= 0)? \
70 (a)-(b): (((a)-(b))+(d)->size))
72 #define ring_increment(d,a,c) (((a)+(c) < (d)->top)? \
73 (a)+(c) : (((a)+(c))-(d)->size))
75 #define ring_decrement(d,a,c) (((a)-(c) >= (d)->bottom)? \
76 (a)-(c) : (((a)-(c))-(d)->size))
80 * The following is a clock, used to determine full, empty, etc.
82 * There is some trickiness here. Since the ring buffers are initialized
83 * to ZERO on allocation, we need to make sure, when interpreting the
84 * clock, that when the times are EQUAL, then the buffer is FULL.
86 static u_long ring_clock = 0;
89 #define ring_empty(d) (((d)->consume == (d)->supply) && \
90 ((d)->consumetime >= (d)->supplytime))
91 #define ring_full(d) (((d)->supply == (d)->consume) && \
92 ((d)->supplytime > (d)->consumetime))
98 /* Buffer state transition routines */
101 ring_init(Ring *ring, unsigned char *buffer, int count)
103 memset((char *)ring, 0, sizeof *ring);
105 ring->size = count;
107 ring->supply = ring->consume = ring->bottom = buffer;
109 ring->top = ring->bottom+ring->size;
111 #ifdef ENCRYPTION
112 ring->clearto = 0;
113 #endif /* ENCRYPTION */
115 return 1;
118 /* Mark routines */
121 * Mark the most recently supplied byte.
124 void
125 ring_mark(Ring *ring)
127 ring->mark = ring_decrement(ring, ring->supply, 1);
131 * Is the ring pointing to the mark?
135 ring_at_mark(Ring *ring)
137 if (ring->mark == ring->consume) {
138 return 1;
139 } else {
140 return 0;
145 * Clear any mark set on the ring.
148 void
149 ring_clear_mark(Ring *ring)
151 ring->mark = 0;
155 * Add characters from current segment to ring buffer.
157 void
158 ring_supplied(Ring *ring, int count)
160 ring->supply = ring_increment(ring, ring->supply, count);
161 ring->supplytime = ++ring_clock;
165 * We have just consumed "c" bytes.
167 void
168 ring_consumed(Ring *ring, int count)
170 if (count == 0) /* don't update anything */
171 return;
173 if (ring->mark &&
174 (ring_subtract(ring, ring->mark, ring->consume) < count)) {
175 ring->mark = 0;
177 #ifdef ENCRYPTION
178 if (ring->consume < ring->clearto &&
179 ring->clearto <= ring->consume + count)
180 ring->clearto = 0;
181 else if (ring->consume + count > ring->top &&
182 ring->bottom <= ring->clearto &&
183 ring->bottom + ((ring->consume + count) - ring->top))
184 ring->clearto = 0;
185 #endif /* ENCRYPTION */
186 ring->consume = ring_increment(ring, ring->consume, count);
187 ring->consumetime = ++ring_clock;
189 * Try to encourage "ring_empty_consecutive()" to be large.
191 if (ring_empty(ring)) {
192 ring->consume = ring->supply = ring->bottom;
198 /* Buffer state query routines */
201 /* Number of bytes that may be supplied */
203 ring_empty_count(Ring *ring)
205 if (ring_empty(ring)) { /* if empty */
206 return ring->size;
207 } else {
208 return ring_subtract(ring, ring->consume, ring->supply);
212 /* number of CONSECUTIVE bytes that may be supplied */
214 ring_empty_consecutive(Ring *ring)
216 if ((ring->consume < ring->supply) || ring_empty(ring)) {
218 * if consume is "below" supply, or empty, then
219 * return distance to the top
221 return ring_subtract(ring, ring->top, ring->supply);
222 } else {
224 * else, return what we may.
226 return ring_subtract(ring, ring->consume, ring->supply);
230 /* Return the number of bytes that are available for consuming
231 * (but don't give more than enough to get to cross over set mark)
235 ring_full_count(Ring *ring)
237 if ((ring->mark == 0) || (ring->mark == ring->consume)) {
238 if (ring_full(ring)) {
239 return ring->size; /* nothing consumed, but full */
240 } else {
241 return ring_subtract(ring, ring->supply, ring->consume);
243 } else {
244 return ring_subtract(ring, ring->mark, ring->consume);
249 * Return the number of CONSECUTIVE bytes available for consuming.
250 * However, don't return more than enough to cross over set mark.
253 ring_full_consecutive(Ring *ring)
255 if ((ring->mark == 0) || (ring->mark == ring->consume)) {
256 if ((ring->supply < ring->consume) || ring_full(ring)) {
257 return ring_subtract(ring, ring->top, ring->consume);
258 } else {
259 return ring_subtract(ring, ring->supply, ring->consume);
261 } else {
262 if (ring->mark < ring->consume) {
263 return ring_subtract(ring, ring->top, ring->consume);
264 } else { /* Else, distance to mark */
265 return ring_subtract(ring, ring->mark, ring->consume);
271 * Move data into the "supply" portion of of the ring buffer.
273 void
274 ring_supply_data(Ring *ring, unsigned char *buffer, int count)
276 int i;
278 while (count) {
279 i = MIN(count, ring_empty_consecutive(ring));
280 memmove(ring->supply, buffer, i);
281 ring_supplied(ring, i);
282 count -= i;
283 buffer += i;
287 #ifdef notdef
290 * Move data from the "consume" portion of the ring buffer
292 void
293 ring_consume_data(Ring *ring, unsigned char *buffer, int count)
295 int i;
297 while (count) {
298 i = MIN(count, ring_full_consecutive(ring));
299 memmove(buffer, ring->consume, i);
300 ring_consumed(ring, i);
301 count -= i;
302 buffer += i;
305 #endif
307 #ifdef ENCRYPTION
308 void
309 ring_encrypt(Ring *ring, void (*encryptor)(unsigned char *, int))
311 unsigned char *s, *c;
313 if (ring_empty(ring) || ring->clearto == ring->supply)
314 return;
316 if (!(c = ring->clearto))
317 c = ring->consume;
319 s = ring->supply;
321 if (s <= c) {
322 (*encryptor)(c, ring->top - c);
323 (*encryptor)(ring->bottom, s - ring->bottom);
324 } else
325 (*encryptor)(c, s - c);
327 ring->clearto = ring->supply;
330 void
331 ring_clearto(Ring *ring)
334 if (!ring_empty(ring))
335 ring->clearto = ring->supply;
336 else
337 ring->clearto = 0;
339 #endif /* ENCRYPTION */