xref: /illumos-gate/usr/src/cmd/cmd-inet/usr.bin/telnet/ring.c (revision 628e3cbed6489fa1db545d8524a06cd6535af456)
1 /*
2  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 #pragma ident	"%Z%%M%	%I%	%E% SMI"
7 
8 /*
9  * usr/src/cmd/cmd-inet/usr.bin/telnet/ring.c
10  */
11 
12 /*
13  * Copyright (c) 1988, 1993
14  *	The Regents of the University of California.  All rights reserved.
15  *
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.
31  *
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.
43  */
44 
45 #ifndef lint
46 static char sccsid[] = "@(#)ring.c	8.1 (Berkeley) 6/6/93";
47 #endif /* not lint */
48 
49 /*
50  * This defines a structure for a ring buffer.
51  *
52  * The circular buffer has two parts:
53  * (((
54  *	full:	[consume, supply)
55  *	empty:	[supply, consume)
56  * ]]]
57  *
58  */
59 
60 #include	<stdio.h>
61 #include	<errno.h>
62 #include	<string.h>
63 
64 #include	<sys/types.h>
65 #include	<sys/socket.h>
66 #include	<sys/sysmacros.h>
67 
68 #include	"ring.h"
69 #include	"general.h"
70 
71 
72 #define	ring_subtract(d, a, b)	(((a)-(b) >= 0)? \
73 					(a)-(b): (((a)-(b))+(d)->size))
74 
75 #define	ring_increment(d, a, c)	(((a)+(c) < (d)->top)? \
76 					(a)+(c) : (((a)+(c))-(d)->size))
77 
78 #define	ring_decrement(d, a, c)	(((a)-(c) >= (d)->bottom)? \
79 					(a)-(c) : (((a)-(c))-(d)->size))
80 
81 
82 /*
83  * The following is a clock, used to determine full, empty, etc.
84  *
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.
88  */
89 ulong_t ring_clock = 0;
90 
91 
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))
96 
97 
98 
99 
100 
101 /* Buffer state transition routines */
102 
103 int
104     ring_init(ring, buffer, count)
105 Ring *ring;
106     unsigned char *buffer;
107     int count;
108 {
109 	(void) memset(ring, 0, sizeof (*ring));
110 
111 	ring->size = count;
112 
113 	ring->supply = ring->consume = ring->bottom = buffer;
114 
115 	ring->top = ring->bottom+ring->size;
116 
117 	ring->clearto = 0;
118 
119 	return (1);
120 }
121 
122 /* Mark routines */
123 
124 /*
125  * Mark the most recently supplied byte.
126  */
127 
128 void
129 ring_mark(ring)
130 	Ring *ring;
131 {
132 	ring->mark = ring_decrement(ring, ring->supply, 1);
133 }
134 
135 /*
136  * Is the ring pointing to the mark?
137  */
138 
139 int
140 ring_at_mark(ring)
141 	Ring *ring;
142 {
143 	if (ring->mark == ring->consume) {
144 		return (1);
145 	} else {
146 		return (0);
147 	}
148 }
149 
150 /*
151  * Clear any mark set on the ring.
152  */
153 
154 void
155 ring_clear_mark(ring)
156 	Ring *ring;
157 {
158 	ring->mark = 0;
159 }
160 
161 /*
162  * Add characters from current segment to ring buffer.
163  */
164     void
165 ring_supplied(ring, count)
166     Ring *ring;
167     int count;
168 {
169     ring->supply = ring_increment(ring, ring->supply, count);
170     ring->supplytime = ++ring_clock;
171 }
172 
173 /*
174  * We have just consumed "c" bytes.
175  */
176 void
177 ring_consumed(ring, count)
178 	Ring *ring;
179 	int count;
180 {
181 	if (count == 0)	/* don't update anything */
182 		return;
183 
184 	if (ring->mark &&
185 	    (ring_subtract(ring, ring->mark, ring->consume) < count)) {
186 		ring->mark = 0;
187 	}
188 
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;
196 
197 	ring->consume = ring_increment(ring, ring->consume, count);
198 	ring->consumetime = ++ring_clock;
199 	/*
200 	 * Try to encourage "ring_empty_consecutive()" to be large.
201 	 */
202 	if (ring_empty(ring)) {
203 		ring->consume = ring->supply = ring->bottom;
204 	}
205 }
206 
207 
208 
209 /* Buffer state query routines */
210 
211 
212 /* Number of bytes that may be supplied */
213 int
214 ring_empty_count(ring)
215 	Ring *ring;
216 {
217 	if (ring_empty(ring)) {	/* if empty */
218 		return (ring->size);
219 	} else {
220 		return (ring_subtract(ring, ring->consume, ring->supply));
221 	}
222 }
223 
224 /* number of CONSECUTIVE bytes that may be supplied */
225 int
226 ring_empty_consecutive(ring)
227 	Ring *ring;
228 {
229 	if ((ring->consume < ring->supply) || ring_empty(ring)) {
230 		/*
231 		 * if consume is "below" supply, or empty, then
232 		 * return distance to the top
233 		 */
234 		return (ring_subtract(ring, ring->top, ring->supply));
235 	} else {
236 		/*
237 		 * else, return what we may.
238 		 */
239 		return (ring_subtract(ring, ring->consume, ring->supply));
240 	}
241 }
242 
243 /*
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)
246  */
247 
248 int
249 ring_full_count(ring)
250 	Ring *ring;
251 {
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));
258 		}
259 	} else {
260 		return (ring_subtract(ring, ring->mark, ring->consume));
261 	}
262 }
263 
264 /*
265  * Return the number of CONSECUTIVE bytes available for consuming.
266  * However, don't return more than enough to cross over set mark.
267  */
268 int
269 ring_full_consecutive(ring)
270 	Ring *ring;
271 {
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));
278 		}
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));
284 		}
285 	}
286 }
287 
288 /*
289  * Move data into the "supply" portion of of the ring buffer.
290  */
291 void
292 ring_supply_data(ring, buffer, count)
293 	Ring *ring;
294 	unsigned char *buffer;
295 	int count;
296 {
297 	int i;
298 
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;
305 	}
306 }
307 
308 #ifdef notdef
309 
310 /*
311  * Move data from the "consume" portion of the ring buffer
312  */
313 void
314 ring_consume_data(ring, buffer, count)
315 	Ring *ring;
316 	unsigned char *buffer;
317 	int count;
318 {
319 	int i;
320 
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;
327 	}
328 }
329 #endif
330 
331 void
332 ring_encrypt(ring, encryptor)
333 	Ring *ring;
334 	void (*encryptor)();
335 {
336 	unsigned char *s, *c;
337 
338 	if (ring_empty(ring) || ring->clearto == ring->supply)
339 		return;
340 
341 	if ((c = ring->clearto) == NULL)
342 		c = ring->consume;
343 
344 	s = ring->supply;
345 
346 	if (s <= c) {
347 		(*encryptor)(c, ring->top - c);
348 		(*encryptor)(ring->bottom, s - ring->bottom);
349 	} else
350 		(*encryptor)(c, s - c);
351 
352 	ring->clearto = ring->supply;
353 }
354 
355     void
356 ring_clearto(ring)
357     Ring *ring;
358 {
359     if (!ring_empty(ring))
360 	ring->clearto = ring->supply;
361     else
362 	ring->clearto = 0;
363 }
364