]>
git.saurik.com Git - apple/network_cmds.git/blob - telnet.tproj/ring.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright (c) 1988, 1993
26 * The Regents of the University of California. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 static char sccsid
[] = "@(#)ring.c 8.2 (Berkeley) 5/30/95";
62 * This defines a structure for a ring buffer.
64 * The circular buffer has two parts:
66 * full: [consume, supply)
67 * empty: [supply, consume)
79 #include <sys/types.h>
81 #include <sys/ioctl.h>
83 #include <sys/socket.h>
91 #define MIN(a,b) (((a)<(b))? (a):(b))
92 #endif /* !defined(MIN) */
94 #define ring_subtract(d,a,b) (((a)-(b) >= 0)? \
95 (a)-(b): (((a)-(b))+(d)->size))
97 #define ring_increment(d,a,c) (((a)+(c) < (d)->top)? \
98 (a)+(c) : (((a)+(c))-(d)->size))
100 #define ring_decrement(d,a,c) (((a)-(c) >= (d)->bottom)? \
101 (a)-(c) : (((a)-(c))-(d)->size))
105 * The following is a clock, used to determine full, empty, etc.
107 * There is some trickiness here. Since the ring buffers are initialized
108 * to ZERO on allocation, we need to make sure, when interpreting the
109 * clock, that when the times are EQUAL, then the buffer is FULL.
111 static u_long ring_clock
= 0;
114 #define ring_empty(d) (((d)->consume == (d)->supply) && \
115 ((d)->consumetime >= (d)->supplytime))
116 #define ring_full(d) (((d)->supply == (d)->consume) && \
117 ((d)->supplytime > (d)->consumetime))
123 /* Buffer state transition routines */
125 ring_init(ring
, buffer
, count
)
127 unsigned char *buffer
;
130 memset((char *)ring
, 0, sizeof *ring
);
134 ring
->supply
= ring
->consume
= ring
->bottom
= buffer
;
136 ring
->top
= ring
->bottom
+ring
->size
;
140 #endif /* ENCRYPTION */
148 * Mark the most recently supplied byte.
155 ring
->mark
= ring_decrement(ring
, ring
->supply
, 1);
159 * Is the ring pointing to the mark?
166 if (ring
->mark
== ring
->consume
) {
174 * Clear any mark set on the ring.
178 ring_clear_mark(ring
)
185 * Add characters from current segment to ring buffer.
188 ring_supplied(ring
, count
)
192 ring
->supply
= ring_increment(ring
, ring
->supply
, count
);
193 ring
->supplytime
= ++ring_clock
;
197 * We have just consumed "c" bytes.
200 ring_consumed(ring
, count
)
204 if (count
== 0) /* don't update anything */
208 (ring_subtract(ring
, ring
->mark
, ring
->consume
) < count
)) {
212 if (ring
->consume
< ring
->clearto
&&
213 ring
->clearto
<= ring
->consume
+ count
)
215 else if (ring
->consume
+ count
> ring
->top
&&
216 ring
->bottom
<= ring
->clearto
&&
217 ring
->bottom
+ ((ring
->consume
+ count
) - ring
->top
))
219 #endif /* ENCRYPTION */
220 ring
->consume
= ring_increment(ring
, ring
->consume
, count
);
221 ring
->consumetime
= ++ring_clock
;
223 * Try to encourage "ring_empty_consecutive()" to be large.
225 if (ring_empty(ring
)) {
226 ring
->consume
= ring
->supply
= ring
->bottom
;
232 /* Buffer state query routines */
235 /* Number of bytes that may be supplied */
237 ring_empty_count(ring
)
240 if (ring_empty(ring
)) { /* if empty */
243 return ring_subtract(ring
, ring
->consume
, ring
->supply
);
247 /* number of CONSECUTIVE bytes that may be supplied */
249 ring_empty_consecutive(ring
)
252 if ((ring
->consume
< ring
->supply
) || ring_empty(ring
)) {
254 * if consume is "below" supply, or empty, then
255 * return distance to the top
257 return ring_subtract(ring
, ring
->top
, ring
->supply
);
260 * else, return what we may.
262 return ring_subtract(ring
, ring
->consume
, ring
->supply
);
266 /* Return the number of bytes that are available for consuming
267 * (but don't give more than enough to get to cross over set mark)
271 ring_full_count(ring
)
274 if ((ring
->mark
== 0) || (ring
->mark
== ring
->consume
)) {
275 if (ring_full(ring
)) {
276 return ring
->size
; /* nothing consumed, but full */
278 return ring_subtract(ring
, ring
->supply
, ring
->consume
);
281 return ring_subtract(ring
, ring
->mark
, ring
->consume
);
286 * Return the number of CONSECUTIVE bytes available for consuming.
287 * However, don't return more than enough to cross over set mark.
290 ring_full_consecutive(ring
)
293 if ((ring
->mark
== 0) || (ring
->mark
== ring
->consume
)) {
294 if ((ring
->supply
< ring
->consume
) || ring_full(ring
)) {
295 return ring_subtract(ring
, ring
->top
, ring
->consume
);
297 return ring_subtract(ring
, ring
->supply
, ring
->consume
);
300 if (ring
->mark
< ring
->consume
) {
301 return ring_subtract(ring
, ring
->top
, ring
->consume
);
302 } else { /* Else, distance to mark */
303 return ring_subtract(ring
, ring
->mark
, ring
->consume
);
309 * Move data into the "supply" portion of of the ring buffer.
312 ring_supply_data(ring
, buffer
, count
)
314 unsigned char *buffer
;
320 i
= MIN(count
, ring_empty_consecutive(ring
));
321 memmove(ring
->supply
, buffer
, i
);
322 ring_supplied(ring
, i
);
331 * Move data from the "consume" portion of the ring buffer
334 ring_consume_data(ring
, buffer
, count
)
336 unsigned char *buffer
;
342 i
= MIN(count
, ring_full_consecutive(ring
));
343 memmove(buffer
, ring
->consume
, i
);
344 ring_consumed(ring
, i
);
353 ring_encrypt(ring
, encryptor
)
357 unsigned char *s
, *c
;
359 if (ring_empty(ring
) || ring
->clearto
== ring
->supply
)
362 if (!(c
= ring
->clearto
))
368 (*encryptor
)(c
, ring
->top
- c
);
369 (*encryptor
)(ring
->bottom
, s
- ring
->bottom
);
371 (*encryptor
)(c
, s
- c
);
373 ring
->clearto
= ring
->supply
;
380 if (!ring_empty(ring
))
381 ring
->clearto
= ring
->supply
;
385 #endif /* ENCRYPTION */