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