xref: /illumos-gate/usr/src/cmd/cmd-inet/usr.bin/telnet/ring.c (revision 10648eb329f44a6c9d85dd91c96b522b6baf6f73)
1 /*
2  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*
7  * usr/src/cmd/cmd-inet/usr.bin/telnet/ring.c
8  */
9 
10 /*
11  * Copyright (c) 1988, 1993
12  *	The Regents of the University of California.  All rights reserved.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. All advertising materials mentioning features or use of this software
23  *    must display the following acknowledgement:
24  *	This product includes software developed by the University of
25  *	California, Berkeley and its contributors.
26  * 4. Neither the name of the University nor the names of its contributors
27  *    may be used to endorse or promote products derived from this software
28  *    without specific prior written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40  * SUCH DAMAGE.
41  */
42 
43 #ifndef lint
44 static char sccsid[] = "@(#)ring.c	8.1 (Berkeley) 6/6/93";
45 #endif /* not lint */
46 
47 /*
48  * This defines a structure for a ring buffer.
49  *
50  * The circular buffer has two parts:
51  * (((
52  *	full:	[consume, supply)
53  *	empty:	[supply, consume)
54  * ]]]
55  *
56  */
57 
58 #include	<stdio.h>
59 #include	<errno.h>
60 #include	<string.h>
61 
62 #include	<sys/types.h>
63 #include	<sys/socket.h>
64 #include	<sys/sysmacros.h>
65 
66 #include	"ring.h"
67 #include	"general.h"
68 
69 
70 #define	ring_subtract(d, a, b)	(((a)-(b) >= 0)? \
71 					(a)-(b): (((a)-(b))+(d)->size))
72 
73 #define	ring_increment(d, a, c)	(((a)+(c) < (d)->top)? \
74 					(a)+(c) : (((a)+(c))-(d)->size))
75 
76 #define	ring_decrement(d, a, c)	(((a)-(c) >= (d)->bottom)? \
77 					(a)-(c) : (((a)-(c))-(d)->size))
78 
79 
80 /*
81  * The following is a clock, used to determine full, empty, etc.
82  *
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.
86  */
87 ulong_t ring_clock = 0;
88 
89 
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))
94 
95 
96 
97 
98 
99 /* Buffer state transition routines */
100 
101 int
ring_init(Ring * ring,unsigned char * buffer,int count)102 ring_init(Ring *ring, unsigned char *buffer, int count)
103 {
104 	(void) memset(ring, 0, sizeof (*ring));
105 
106 	ring->size = count;
107 
108 	ring->supply = ring->consume = ring->bottom = buffer;
109 
110 	ring->top = ring->bottom+ring->size;
111 
112 	ring->clearto = 0;
113 
114 	return (1);
115 }
116 
117 /* Mark routines */
118 
119 /*
120  * Mark the most recently supplied byte.
121  */
122 
123 void
ring_mark(Ring * ring)124 ring_mark(Ring *ring)
125 {
126 	ring->mark = ring_decrement(ring, ring->supply, 1);
127 }
128 
129 /*
130  * Is the ring pointing to the mark?
131  */
132 
133 int
ring_at_mark(Ring * ring)134 ring_at_mark(Ring *ring)
135 {
136 	if (ring->mark == ring->consume) {
137 		return (1);
138 	} else {
139 		return (0);
140 	}
141 }
142 
143 /*
144  * Clear any mark set on the ring.
145  */
146 
147 void
ring_clear_mark(Ring * ring)148 ring_clear_mark(Ring *ring)
149 {
150 	ring->mark = 0;
151 }
152 
153 /*
154  * Add characters from current segment to ring buffer.
155  */
156     void
ring_supplied(Ring * ring,int count)157 ring_supplied(Ring *ring, int count)
158 {
159 	ring->supply = ring_increment(ring, ring->supply, count);
160 	ring->supplytime = ++ring_clock;
161 }
162 
163 /*
164  * We have just consumed "c" bytes.
165  */
166 void
ring_consumed(Ring * ring,int count)167 ring_consumed(Ring *ring, int count)
168 {
169 	if (count == 0)	/* don't update anything */
170 		return;
171 
172 	if (ring->mark &&
173 	    (ring_subtract(ring, ring->mark, ring->consume) < count)) {
174 		ring->mark = 0;
175 	}
176 
177 	if (ring->consume < ring->clearto &&
178 	    ring->clearto <= ring->consume + count)
179 		ring->clearto = 0;
180 	else if (ring->consume + count > ring->top &&
181 	    ring->bottom <= ring->clearto)
182 		ring->clearto = 0;
183 
184 	ring->consume = ring_increment(ring, ring->consume, count);
185 	ring->consumetime = ++ring_clock;
186 	/*
187 	 * Try to encourage "ring_empty_consecutive()" to be large.
188 	 */
189 	if (ring_empty(ring)) {
190 		ring->consume = ring->supply = ring->bottom;
191 	}
192 }
193 
194 
195 
196 /* Buffer state query routines */
197 
198 
199 /* Number of bytes that may be supplied */
200 int
ring_empty_count(Ring * ring)201 ring_empty_count(Ring *ring)
202 {
203 	if (ring_empty(ring)) {	/* if empty */
204 		return (ring->size);
205 	} else {
206 		return (ring_subtract(ring, ring->consume, ring->supply));
207 	}
208 }
209 
210 /* number of CONSECUTIVE bytes that may be supplied */
211 int
ring_empty_consecutive(Ring * ring)212 ring_empty_consecutive(Ring *ring)
213 {
214 	if ((ring->consume < ring->supply) || ring_empty(ring)) {
215 		/*
216 		 * if consume is "below" supply, or empty, then
217 		 * return distance to the top
218 		 */
219 		return (ring_subtract(ring, ring->top, ring->supply));
220 	} else {
221 		/*
222 		 * else, return what we may.
223 		 */
224 		return (ring_subtract(ring, ring->consume, ring->supply));
225 	}
226 }
227 
228 /*
229  * Return the number of bytes that are available for consuming
230  * (but don't give more than enough to get to cross over set mark)
231  */
232 
233 int
ring_full_count(Ring * ring)234 ring_full_count(Ring *ring)
235 {
236 	if ((ring->mark == 0) || (ring->mark == ring->consume)) {
237 		if (ring_full(ring)) {
238 			return (ring->size);	/* nothing consumed, but full */
239 		} else {
240 			return (ring_subtract(ring, ring->supply,
241 			    ring->consume));
242 		}
243 	} else {
244 		return (ring_subtract(ring, ring->mark, ring->consume));
245 	}
246 }
247 
248 /*
249  * Return the number of CONSECUTIVE bytes available for consuming.
250  * However, don't return more than enough to cross over set mark.
251  */
252 int
ring_full_consecutive(Ring * ring)253 ring_full_consecutive(Ring *ring)
254 {
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,
260 			    ring->consume));
261 		}
262 	} else {
263 		if (ring->mark < ring->consume) {
264 			return (ring_subtract(ring, ring->top, ring->consume));
265 		} else {	/* Else, distance to mark */
266 			return (ring_subtract(ring, ring->mark, ring->consume));
267 		}
268 	}
269 }
270 
271 /*
272  * Move data into the "supply" portion of of the ring buffer.
273  */
274 void
ring_supply_data(Ring * ring,unsigned char * buffer,int count)275 ring_supply_data(Ring *ring, unsigned char *buffer, int count)
276 {
277 	int i;
278 
279 	while (count) {
280 		i = MIN(count, ring_empty_consecutive(ring));
281 		(void) memcpy(ring->supply, buffer, i);
282 		ring_supplied(ring, i);
283 		count -= i;
284 		buffer += i;
285 	}
286 }
287 
288 #ifdef notdef
289 
290 /*
291  * Move data from the "consume" portion of the ring buffer
292  */
293 void
ring_consume_data(Ring * ring,unsigned char * buffer,int count)294 ring_consume_data(Ring *ring, unsigned char *buffer, int count)
295 {
296 	int i;
297 
298 	while (count) {
299 		i = MIN(count, ring_full_consecutive(ring));
300 		memcpy(buffer, ring->consume, i);
301 		ring_consumed(ring, i);
302 		count -= i;
303 		buffer += i;
304 	}
305 }
306 #endif
307 
308 void
ring_encrypt(Ring * ring,void (* encryptor)())309 ring_encrypt(Ring *ring, void (*encryptor)())
310 {
311 	unsigned char *s, *c;
312 
313 	if (ring_empty(ring) || ring->clearto == ring->supply)
314 		return;
315 
316 	if ((c = ring->clearto) == NULL)
317 		c = ring->consume;
318 
319 	s = ring->supply;
320 
321 	if (s <= c) {
322 		(*encryptor)(c, ring->top - c);
323 		(*encryptor)(ring->bottom, s - ring->bottom);
324 	} else
325 		(*encryptor)(c, s - c);
326 
327 	ring->clearto = ring->supply;
328 }
329 
330     void
ring_clearto(Ring * ring)331 ring_clearto(Ring *ring)
332 {
333 	if (!ring_empty(ring))
334 		ring->clearto = ring->supply;
335 	else
336 		ring->clearto = 0;
337 }
338