1/*
2 * Copyright (c) 1988, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#if 0
31#ifndef lint
32static const char sccsid[] = "@(#)ring.c	8.2 (Berkeley) 5/30/95";
33#endif
34#endif
35#include <sys/cdefs.h>
36__FBSDID("$FreeBSD: stable/11/contrib/telnet/telnet/ring.c 351432 2019-08-23 17:40:47Z emaste $");
37
38/*
39 * This defines a structure for a ring buffer.
40 *
41 * The circular buffer has two parts:
42 *(((
43 *	full:	[consume, supply)
44 *	empty:	[supply, consume)
45 *]]]
46 *
47 */
48
49#include	<errno.h>
50#include	<stdio.h>
51#include	<string.h>
52
53#ifdef	size_t
54#undef	size_t
55#endif
56
57#include	<sys/types.h>
58#ifndef	FILIO_H
59#include	<sys/ioctl.h>
60#endif
61#include	<sys/socket.h>
62
63#include	"ring.h"
64#include	"general.h"
65
66/* Internal macros */
67
68#if	!defined(MIN)
69#define	MIN(a,b)	(((a)<(b))? (a):(b))
70#endif	/* !defined(MIN) */
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 */
89static u_long 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/* Buffer state transition routines */
98
99int
100ring_init(Ring *ring, unsigned char *buffer, int count)
101{
102    memset((char *)ring, 0, sizeof *ring);
103
104    ring->size = count;
105
106    ring->supply = ring->consume = ring->bottom = buffer;
107
108    ring->top = ring->bottom+ring->size;
109
110#ifdef	ENCRYPTION
111    ring->clearto = 0;
112#endif	/* ENCRYPTION */
113
114    return 1;
115}
116
117/* Mark routines */
118
119/*
120 * Mark the most recently supplied byte.
121 */
122
123void
124ring_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
133int
134ring_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
147void
148ring_clear_mark(Ring *ring)
149{
150    ring->mark = 0;
151}
152
153/*
154 * Add characters from current segment to ring buffer.
155 */
156void
157ring_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 */
166void
167ring_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#ifdef	ENCRYPTION
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->bottom + ((ring->consume + count) - ring->top))
183	ring->clearto = 0;
184#endif	/* ENCRYPTION */
185    ring->consume = ring_increment(ring, ring->consume, count);
186    ring->consumetime = ++ring_clock;
187    /*
188     * Try to encourage "ring_empty_consecutive()" to be large.
189     */
190    if (ring_empty(ring)) {
191	ring->consume = ring->supply = ring->bottom;
192    }
193}
194
195
196
197/* Buffer state query routines */
198
199
200/* Number of bytes that may be supplied */
201int
202ring_empty_count(Ring *ring)
203{
204    if (ring_empty(ring)) {	/* if empty */
205	    return ring->size;
206    } else {
207	return ring_subtract(ring, ring->consume, ring->supply);
208    }
209}
210
211/* number of CONSECUTIVE bytes that may be supplied */
212int
213ring_empty_consecutive(Ring *ring)
214{
215    if ((ring->consume < ring->supply) || ring_empty(ring)) {
216			    /*
217			     * if consume is "below" supply, or empty, then
218			     * return distance to the top
219			     */
220	return ring_subtract(ring, ring->top, ring->supply);
221    } else {
222				    /*
223				     * else, return what we may.
224				     */
225	return ring_subtract(ring, ring->consume, ring->supply);
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
233int
234ring_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, ring->consume);
241	}
242    } else {
243	return ring_subtract(ring, ring->mark, ring->consume);
244    }
245}
246
247/*
248 * Return the number of CONSECUTIVE bytes available for consuming.
249 * However, don't return more than enough to cross over set mark.
250 */
251int
252ring_full_consecutive(Ring *ring)
253{
254    if ((ring->mark == 0) || (ring->mark == ring->consume)) {
255	if ((ring->supply < ring->consume) || ring_full(ring)) {
256	    return ring_subtract(ring, ring->top, ring->consume);
257	} else {
258	    return ring_subtract(ring, ring->supply, ring->consume);
259	}
260    } else {
261	if (ring->mark < ring->consume) {
262	    return ring_subtract(ring, ring->top, ring->consume);
263	} else {	/* Else, distance to mark */
264	    return ring_subtract(ring, ring->mark, ring->consume);
265	}
266    }
267}
268
269/*
270 * Move data into the "supply" portion of of the ring buffer.
271 */
272void
273ring_supply_data(Ring *ring, unsigned char *buffer, int count)
274{
275    int i;
276
277    while (count) {
278	i = MIN(count, ring_empty_consecutive(ring));
279	memcpy(ring->supply, buffer, i);
280	ring_supplied(ring, i);
281	count -= i;
282	buffer += i;
283    }
284}
285
286#ifdef	ENCRYPTION
287void
288ring_encrypt(Ring *ring, void (*encryptor)(unsigned char *, int))
289{
290    unsigned char *s, *c;
291
292    if (ring_empty(ring) || ring->clearto == ring->supply)
293	return;
294
295    if (!(c = ring->clearto))
296	c = ring->consume;
297
298    s = ring->supply;
299
300    if (s <= c) {
301	(*encryptor)(c, ring->top - c);
302	(*encryptor)(ring->bottom, s - ring->bottom);
303    } else
304	(*encryptor)(c, s - c);
305
306    ring->clearto = ring->supply;
307}
308
309    void
310ring_clearto(ring)
311    Ring *ring;
312{
313    if (!ring_empty(ring))
314	ring->clearto = ring->supply;
315    else
316	ring->clearto = 0;
317}
318#endif	/* ENCRYPTION */
319