dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / cmd-inet / usr.bin / telnet / ring.c
blob1dcc4322e346a8c2223ac5857a20ed7eb52e00bc
1 /*
2 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
4 */
6 #pragma ident "%Z%%M% %I% %E% SMI"
8 /*
9 * usr/src/cmd/cmd-inet/usr.bin/telnet/ring.c
13 * Copyright (c) 1988, 1993
14 * The Regents of the University of California. All rights reserved.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. All advertising materials mentioning features or use of this software
25 * must display the following acknowledgement:
26 * This product includes software developed by the University of
27 * California, Berkeley and its contributors.
28 * 4. Neither the name of the University nor the names of its contributors
29 * may be used to endorse or promote products derived from this software
30 * without specific prior written permission.
32 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * SUCH DAMAGE.
45 static char sccsid[] = "@(#)ring.c 8.1 (Berkeley) 6/6/93";
48 * This defines a structure for a ring buffer.
50 * The circular buffer has two parts:
51 * (((
52 * full: [consume, supply)
53 * empty: [supply, consume)
54 * ]]]
58 #include <stdio.h>
59 #include <errno.h>
60 #include <string.h>
62 #include <sys/types.h>
63 #include <sys/socket.h>
64 #include <sys/sysmacros.h>
66 #include "ring.h"
67 #include "general.h"
70 #define ring_subtract(d, a, b) (((a)-(b) >= 0)? \
71 (a)-(b): (((a)-(b))+(d)->size))
73 #define ring_increment(d, a, c) (((a)+(c) < (d)->top)? \
74 (a)+(c) : (((a)+(c))-(d)->size))
76 #define ring_decrement(d, a, c) (((a)-(c) >= (d)->bottom)? \
77 (a)-(c) : (((a)-(c))-(d)->size))
81 * The following is a clock, used to determine full, empty, etc.
83 * There is some trickiness here. Since the ring buffers are initialized
84 * to ZERO on allocation, we need to make sure, when interpreting the
85 * clock, that when the times are EQUAL, then the buffer is FULL.
87 ulong_t ring_clock = 0;
90 #define ring_empty(d) (((d)->consume == (d)->supply) && \
91 ((d)->consumetime >= (d)->supplytime))
92 #define ring_full(d) (((d)->supply == (d)->consume) && \
93 ((d)->supplytime > (d)->consumetime))
99 /* Buffer state transition routines */
102 ring_init(ring, buffer, count)
103 Ring *ring;
104 unsigned char *buffer;
105 int count;
107 (void) memset(ring, 0, sizeof (*ring));
109 ring->size = count;
111 ring->supply = ring->consume = ring->bottom = buffer;
113 ring->top = ring->bottom+ring->size;
115 ring->clearto = 0;
117 return (1);
120 /* Mark routines */
123 * Mark the most recently supplied byte.
126 void
127 ring_mark(ring)
128 Ring *ring;
130 ring->mark = ring_decrement(ring, ring->supply, 1);
134 * Is the ring pointing to the mark?
138 ring_at_mark(ring)
139 Ring *ring;
141 if (ring->mark == ring->consume) {
142 return (1);
143 } else {
144 return (0);
149 * Clear any mark set on the ring.
152 void
153 ring_clear_mark(ring)
154 Ring *ring;
156 ring->mark = 0;
160 * Add characters from current segment to ring buffer.
162 void
163 ring_supplied(ring, count)
164 Ring *ring;
165 int count;
167 ring->supply = ring_increment(ring, ring->supply, count);
168 ring->supplytime = ++ring_clock;
172 * We have just consumed "c" bytes.
174 void
175 ring_consumed(ring, count)
176 Ring *ring;
177 int count;
179 if (count == 0) /* don't update anything */
180 return;
182 if (ring->mark &&
183 (ring_subtract(ring, ring->mark, ring->consume) < count)) {
184 ring->mark = 0;
187 if (ring->consume < ring->clearto &&
188 ring->clearto <= ring->consume + count)
189 ring->clearto = 0;
190 else if (ring->consume + count > ring->top &&
191 ring->bottom <= ring->clearto &&
192 ring->bottom + ((ring->consume + count) - ring->top))
193 ring->clearto = 0;
195 ring->consume = ring_increment(ring, ring->consume, count);
196 ring->consumetime = ++ring_clock;
198 * Try to encourage "ring_empty_consecutive()" to be large.
200 if (ring_empty(ring)) {
201 ring->consume = ring->supply = ring->bottom;
207 /* Buffer state query routines */
210 /* Number of bytes that may be supplied */
212 ring_empty_count(ring)
213 Ring *ring;
215 if (ring_empty(ring)) { /* if empty */
216 return (ring->size);
217 } else {
218 return (ring_subtract(ring, ring->consume, ring->supply));
222 /* number of CONSECUTIVE bytes that may be supplied */
224 ring_empty_consecutive(ring)
225 Ring *ring;
227 if ((ring->consume < ring->supply) || ring_empty(ring)) {
229 * if consume is "below" supply, or empty, then
230 * return distance to the top
232 return (ring_subtract(ring, ring->top, ring->supply));
233 } else {
235 * else, return what we may.
237 return (ring_subtract(ring, ring->consume, ring->supply));
242 * Return the number of bytes that are available for consuming
243 * (but don't give more than enough to get to cross over set mark)
247 ring_full_count(ring)
248 Ring *ring;
250 if ((ring->mark == 0) || (ring->mark == ring->consume)) {
251 if (ring_full(ring)) {
252 return (ring->size); /* nothing consumed, but full */
253 } else {
254 return (ring_subtract(ring, ring->supply,
255 ring->consume));
257 } else {
258 return (ring_subtract(ring, ring->mark, ring->consume));
263 * Return the number of CONSECUTIVE bytes available for consuming.
264 * However, don't return more than enough to cross over set mark.
267 ring_full_consecutive(ring)
268 Ring *ring;
270 if ((ring->mark == 0) || (ring->mark == ring->consume)) {
271 if ((ring->supply < ring->consume) || ring_full(ring)) {
272 return (ring_subtract(ring, ring->top, ring->consume));
273 } else {
274 return (ring_subtract(ring, ring->supply,
275 ring->consume));
277 } else {
278 if (ring->mark < ring->consume) {
279 return (ring_subtract(ring, ring->top, ring->consume));
280 } else { /* Else, distance to mark */
281 return (ring_subtract(ring, ring->mark, ring->consume));
287 * Move data into the "supply" portion of of the ring buffer.
289 void
290 ring_supply_data(ring, buffer, count)
291 Ring *ring;
292 unsigned char *buffer;
293 int count;
295 int i;
297 while (count) {
298 i = MIN(count, ring_empty_consecutive(ring));
299 (void) memcpy(ring->supply, buffer, i);
300 ring_supplied(ring, i);
301 count -= i;
302 buffer += i;
306 #ifdef notdef
309 * Move data from the "consume" portion of the ring buffer
311 void
312 ring_consume_data(ring, buffer, count)
313 Ring *ring;
314 unsigned char *buffer;
315 int count;
317 int i;
319 while (count) {
320 i = MIN(count, ring_full_consecutive(ring));
321 memcpy(buffer, ring->consume, i);
322 ring_consumed(ring, i);
323 count -= i;
324 buffer += i;
327 #endif
329 void
330 ring_encrypt(ring, encryptor)
331 Ring *ring;
332 void (*encryptor)();
334 unsigned char *s, *c;
336 if (ring_empty(ring) || ring->clearto == ring->supply)
337 return;
339 if ((c = ring->clearto) == NULL)
340 c = ring->consume;
342 s = ring->supply;
344 if (s <= c) {
345 (*encryptor)(c, ring->top - c);
346 (*encryptor)(ring->bottom, s - ring->bottom);
347 } else
348 (*encryptor)(c, s - c);
350 ring->clearto = ring->supply;
353 void
354 ring_clearto(ring)
355 Ring *ring;
357 if (!ring_empty(ring))
358 ring->clearto = ring->supply;
359 else
360 ring->clearto = 0;