8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / cmd-inet / usr.bin / telnet / ring.c
blob9f8735a6744c416b871889a0e569c6682fa0d0bf
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 #ifndef lint
46 static char sccsid[] = "@(#)ring.c 8.1 (Berkeley) 6/6/93";
47 #endif /* not lint */
50 * This defines a structure for a ring buffer.
52 * The circular buffer has two parts:
53 * (((
54 * full: [consume, supply)
55 * empty: [supply, consume)
56 * ]]]
60 #include <stdio.h>
61 #include <errno.h>
62 #include <string.h>
64 #include <sys/types.h>
65 #include <sys/socket.h>
66 #include <sys/sysmacros.h>
68 #include "ring.h"
69 #include "general.h"
72 #define ring_subtract(d, a, b) (((a)-(b) >= 0)? \
73 (a)-(b): (((a)-(b))+(d)->size))
75 #define ring_increment(d, a, c) (((a)+(c) < (d)->top)? \
76 (a)+(c) : (((a)+(c))-(d)->size))
78 #define ring_decrement(d, a, c) (((a)-(c) >= (d)->bottom)? \
79 (a)-(c) : (((a)-(c))-(d)->size))
83 * The following is a clock, used to determine full, empty, etc.
85 * There is some trickiness here. Since the ring buffers are initialized
86 * to ZERO on allocation, we need to make sure, when interpreting the
87 * clock, that when the times are EQUAL, then the buffer is FULL.
89 ulong_t ring_clock = 0;
92 #define ring_empty(d) (((d)->consume == (d)->supply) && \
93 ((d)->consumetime >= (d)->supplytime))
94 #define ring_full(d) (((d)->supply == (d)->consume) && \
95 ((d)->supplytime > (d)->consumetime))
101 /* Buffer state transition routines */
104 ring_init(ring, buffer, count)
105 Ring *ring;
106 unsigned char *buffer;
107 int count;
109 (void) memset(ring, 0, sizeof (*ring));
111 ring->size = count;
113 ring->supply = ring->consume = ring->bottom = buffer;
115 ring->top = ring->bottom+ring->size;
117 ring->clearto = 0;
119 return (1);
122 /* Mark routines */
125 * Mark the most recently supplied byte.
128 void
129 ring_mark(ring)
130 Ring *ring;
132 ring->mark = ring_decrement(ring, ring->supply, 1);
136 * Is the ring pointing to the mark?
140 ring_at_mark(ring)
141 Ring *ring;
143 if (ring->mark == ring->consume) {
144 return (1);
145 } else {
146 return (0);
151 * Clear any mark set on the ring.
154 void
155 ring_clear_mark(ring)
156 Ring *ring;
158 ring->mark = 0;
162 * Add characters from current segment to ring buffer.
164 void
165 ring_supplied(ring, count)
166 Ring *ring;
167 int count;
169 ring->supply = ring_increment(ring, ring->supply, count);
170 ring->supplytime = ++ring_clock;
174 * We have just consumed "c" bytes.
176 void
177 ring_consumed(ring, count)
178 Ring *ring;
179 int count;
181 if (count == 0) /* don't update anything */
182 return;
184 if (ring->mark &&
185 (ring_subtract(ring, ring->mark, ring->consume) < count)) {
186 ring->mark = 0;
189 if (ring->consume < ring->clearto &&
190 ring->clearto <= ring->consume + count)
191 ring->clearto = 0;
192 else if (ring->consume + count > ring->top &&
193 ring->bottom <= ring->clearto &&
194 ring->bottom + ((ring->consume + count) - ring->top))
195 ring->clearto = 0;
197 ring->consume = ring_increment(ring, ring->consume, count);
198 ring->consumetime = ++ring_clock;
200 * Try to encourage "ring_empty_consecutive()" to be large.
202 if (ring_empty(ring)) {
203 ring->consume = ring->supply = ring->bottom;
209 /* Buffer state query routines */
212 /* Number of bytes that may be supplied */
214 ring_empty_count(ring)
215 Ring *ring;
217 if (ring_empty(ring)) { /* if empty */
218 return (ring->size);
219 } else {
220 return (ring_subtract(ring, ring->consume, ring->supply));
224 /* number of CONSECUTIVE bytes that may be supplied */
226 ring_empty_consecutive(ring)
227 Ring *ring;
229 if ((ring->consume < ring->supply) || ring_empty(ring)) {
231 * if consume is "below" supply, or empty, then
232 * return distance to the top
234 return (ring_subtract(ring, ring->top, ring->supply));
235 } else {
237 * else, return what we may.
239 return (ring_subtract(ring, ring->consume, ring->supply));
244 * Return the number of bytes that are available for consuming
245 * (but don't give more than enough to get to cross over set mark)
249 ring_full_count(ring)
250 Ring *ring;
252 if ((ring->mark == 0) || (ring->mark == ring->consume)) {
253 if (ring_full(ring)) {
254 return (ring->size); /* nothing consumed, but full */
255 } else {
256 return (ring_subtract(ring, ring->supply,
257 ring->consume));
259 } else {
260 return (ring_subtract(ring, ring->mark, ring->consume));
265 * Return the number of CONSECUTIVE bytes available for consuming.
266 * However, don't return more than enough to cross over set mark.
269 ring_full_consecutive(ring)
270 Ring *ring;
272 if ((ring->mark == 0) || (ring->mark == ring->consume)) {
273 if ((ring->supply < ring->consume) || ring_full(ring)) {
274 return (ring_subtract(ring, ring->top, ring->consume));
275 } else {
276 return (ring_subtract(ring, ring->supply,
277 ring->consume));
279 } else {
280 if (ring->mark < ring->consume) {
281 return (ring_subtract(ring, ring->top, ring->consume));
282 } else { /* Else, distance to mark */
283 return (ring_subtract(ring, ring->mark, ring->consume));
289 * Move data into the "supply" portion of of the ring buffer.
291 void
292 ring_supply_data(ring, buffer, count)
293 Ring *ring;
294 unsigned char *buffer;
295 int count;
297 int i;
299 while (count) {
300 i = MIN(count, ring_empty_consecutive(ring));
301 (void) memcpy(ring->supply, buffer, i);
302 ring_supplied(ring, i);
303 count -= i;
304 buffer += i;
308 #ifdef notdef
311 * Move data from the "consume" portion of the ring buffer
313 void
314 ring_consume_data(ring, buffer, count)
315 Ring *ring;
316 unsigned char *buffer;
317 int count;
319 int i;
321 while (count) {
322 i = MIN(count, ring_full_consecutive(ring));
323 memcpy(buffer, ring->consume, i);
324 ring_consumed(ring, i);
325 count -= i;
326 buffer += i;
329 #endif
331 void
332 ring_encrypt(ring, encryptor)
333 Ring *ring;
334 void (*encryptor)();
336 unsigned char *s, *c;
338 if (ring_empty(ring) || ring->clearto == ring->supply)
339 return;
341 if ((c = ring->clearto) == NULL)
342 c = ring->consume;
344 s = ring->supply;
346 if (s <= c) {
347 (*encryptor)(c, ring->top - c);
348 (*encryptor)(ring->bottom, s - ring->bottom);
349 } else
350 (*encryptor)(c, s - c);
352 ring->clearto = ring->supply;
355 void
356 ring_clearto(ring)
357 Ring *ring;
359 if (!ring_empty(ring))
360 ring->clearto = ring->supply;
361 else
362 ring->clearto = 0;