1/*	$NetBSD: ring.c,v 1.17 2024/05/13 00:32:39 msaitoh Exp $	*/
2
3/*
4 * Copyright (c) 1988, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33#ifndef lint
34#if 0
35static char sccsid[] = "@(#)ring.c	8.2 (Berkeley) 5/30/95";
36#else
37__RCSID("$NetBSD: ring.c,v 1.17 2024/05/13 00:32:39 msaitoh Exp $");
38#endif
39#endif /* not lint */
40
41/*
42 * This defines a structure for a ring buffer.
43 *
44 * The circular buffer has two parts:
45 *(((
46 *	full:	[consume, supply)
47 *	empty:	[supply, consume)
48 *]]]
49 *
50 */
51
52#include	<stdio.h>
53#include	<string.h>
54#include	<strings.h>
55#include	<errno.h>
56#include	<sys/types.h>
57#include	<sys/ioctl.h>
58#include	<sys/socket.h>
59
60#include	"ring.h"
61#include	"general.h"
62
63/* Internal macros */
64
65#if	!defined(MIN)
66#define	MIN(a,b)	(((a)<(b))? (a):(b))
67#endif	/* !defined(MIN) */
68
69#define	ring_subtract(d,a,b)	(((a)-(b) >= 0)? \
70					(a)-(b): (((a)-(b))+(d)->size))
71
72#define	ring_increment(d,a,c)	(((a)+(c) < (d)->top)? \
73					(a)+(c) : (((a)+(c))-(d)->size))
74
75#define	ring_decrement(d,a,c)	(((a)-(c) >= (d)->bottom)? \
76					(a)-(c) : (((a)-(c))-(d)->size))
77
78
79/*
80 * The following is a clock, used to determine full, empty, etc.
81 *
82 * There is some trickiness here.  Since the ring buffers are initialized
83 * to ZERO on allocation, we need to make sure, when interpreting the
84 * clock, that when the times are EQUAL, then the buffer is FULL.
85 */
86static u_long ring_clock = 0;
87
88
89#define	ring_empty(d) (((d)->consume == (d)->supply) && \
90				((d)->consumetime >= (d)->supplytime))
91#define	ring_full(d) (((d)->supply == (d)->consume) && \
92				((d)->supplytime > (d)->consumetime))
93
94
95
96
97
98/* Buffer state transition routines */
99
100int
101ring_init(Ring *ring, unsigned char *buffer, int count)
102{
103    memset(ring, 0, sizeof *ring);
104
105    ring->size = count;
106
107    ring->supply = ring->consume = ring->bottom = buffer;
108
109    ring->top = ring->bottom+ring->size;
110
111#ifdef	ENCRYPTION
112    ring->clearto = 0;
113#endif	/* ENCRYPTION */
114
115    return 1;
116}
117
118/* Mark routines */
119
120/*
121 * Mark the most recently supplied byte.
122 */
123
124void
125ring_mark(Ring *ring)
126{
127    ring->mark = ring_decrement(ring, ring->supply, 1);
128}
129
130/*
131 * Is the ring pointing to the mark?
132 */
133
134int
135ring_at_mark(Ring *ring)
136{
137    if (ring->mark == ring->consume) {
138	return 1;
139    } else {
140	return 0;
141    }
142}
143
144/*
145 * Clear any mark set on the ring.
146 */
147
148void
149ring_clear_mark(Ring *ring)
150{
151    ring->mark = 0;
152}
153
154/*
155 * Add characters from current segment to ring buffer.
156 */
157void
158ring_supplied(Ring *ring, int count)
159{
160    ring->supply = ring_increment(ring, ring->supply, count);
161    ring->supplytime = ++ring_clock;
162}
163
164/*
165 * We have just consumed "c" bytes.
166 */
167void
168ring_consumed(Ring *ring, int count)
169{
170    if (count == 0)	/* don't update anything */
171	return;
172
173    if (ring->mark &&
174		(ring_subtract(ring, ring->mark, ring->consume) < count)) {
175	ring->mark = 0;
176    }
177#ifdef	ENCRYPTION
178    if (ring->consume < ring->clearto &&
179		ring->clearto <= ring->consume + count)
180	ring->clearto = 0;
181    else if (ring->consume + count > ring->top &&
182		ring->bottom <= ring->clearto)
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 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	memmove(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
309void
310ring_clearto(Ring *ring)
311{
312
313	if (!ring_empty(ring))
314		ring->clearto = ring->supply;
315	else
316		ring->clearto = 0;
317}
318#endif	/* ENCRYPTION */
319