2 * Copyright (c) 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 * @(#)ring.c 8.2 (Berkeley) 5/30/95
30 * $FreeBSD: src/crypto/telnet/telnet/ring.c,v 1.2.8.2 2002/04/13 10:59:08 markm Exp $
34 * This defines a structure for a ring buffer.
36 * The circular buffer has two parts:
38 * full: [consume, supply)
39 * empty: [supply, consume)
52 #include <sys/types.h>
54 #include <sys/ioctl.h>
56 #include <sys/socket.h>
64 #define MIN(a,b) (((a)<(b))? (a):(b))
65 #endif /* !defined(MIN) */
67 #define ring_subtract(d,a,b) (((a)-(b) >= 0)? \
68 (a)-(b): (((a)-(b))+(d)->size))
70 #define ring_increment(d,a,c) (((a)+(c) < (d)->top)? \
71 (a)+(c) : (((a)+(c))-(d)->size))
73 #define ring_decrement(d,a,c) (((a)-(c) >= (d)->bottom)? \
74 (a)-(c) : (((a)-(c))-(d)->size))
78 * The following is a clock, used to determine full, empty, etc.
80 * There is some trickiness here. Since the ring buffers are initialized
81 * to ZERO on allocation, we need to make sure, when interpreting the
82 * clock, that when the times are EQUAL, then the buffer is FULL.
84 static u_long ring_clock
= 0;
87 #define ring_empty(d) (((d)->consume == (d)->supply) && \
88 ((d)->consumetime >= (d)->supplytime))
89 #define ring_full(d) (((d)->supply == (d)->consume) && \
90 ((d)->supplytime > (d)->consumetime))
92 /* Buffer state transition routines */
95 ring_init(Ring
*ring
, unsigned char *buffer
, int count
)
97 memset((char *)ring
, 0, sizeof *ring
);
101 ring
->supply
= ring
->consume
= ring
->bottom
= buffer
;
103 ring
->top
= ring
->bottom
+ring
->size
;
107 #endif /* ENCRYPTION */
115 * Mark the most recently supplied byte.
119 ring_mark(Ring
*ring
)
121 ring
->mark
= ring_decrement(ring
, ring
->supply
, 1);
125 * Is the ring pointing to the mark?
129 ring_at_mark(Ring
*ring
)
131 if (ring
->mark
== ring
->consume
) {
139 * Clear any mark set on the ring.
143 ring_clear_mark(Ring
*ring
)
149 * Add characters from current segment to ring buffer.
152 ring_supplied(Ring
*ring
, int count
)
154 ring
->supply
= ring_increment(ring
, ring
->supply
, count
);
155 ring
->supplytime
= ++ring_clock
;
159 * We have just consumed "c" bytes.
162 ring_consumed(Ring
*ring
, int count
)
164 if (count
== 0) /* don't update anything */
168 (ring_subtract(ring
, ring
->mark
, ring
->consume
) < count
)) {
172 if (ring
->consume
< ring
->clearto
&&
173 ring
->clearto
<= ring
->consume
+ count
)
175 else if (ring
->consume
+ count
> ring
->top
&&
176 ring
->bottom
<= ring
->clearto
&&
177 ring
->bottom
+ ((ring
->consume
+ count
) - ring
->top
))
179 #endif /* ENCRYPTION */
180 ring
->consume
= ring_increment(ring
, ring
->consume
, count
);
181 ring
->consumetime
= ++ring_clock
;
183 * Try to encourage "ring_empty_consecutive()" to be large.
185 if (ring_empty(ring
)) {
186 ring
->consume
= ring
->supply
= ring
->bottom
;
192 /* Buffer state query routines */
195 /* Number of bytes that may be supplied */
197 ring_empty_count(Ring
*ring
)
199 if (ring_empty(ring
)) { /* if empty */
202 return ring_subtract(ring
, ring
->consume
, ring
->supply
);
206 /* number of CONSECUTIVE bytes that may be supplied */
208 ring_empty_consecutive(Ring
*ring
)
210 if ((ring
->consume
< ring
->supply
) || ring_empty(ring
)) {
212 * if consume is "below" supply, or empty, then
213 * return distance to the top
215 return ring_subtract(ring
, ring
->top
, ring
->supply
);
218 * else, return what we may.
220 return ring_subtract(ring
, ring
->consume
, ring
->supply
);
224 /* Return the number of bytes that are available for consuming
225 * (but don't give more than enough to get to cross over set mark)
229 ring_full_count(Ring
*ring
)
231 if ((ring
->mark
== 0) || (ring
->mark
== ring
->consume
)) {
232 if (ring_full(ring
)) {
233 return ring
->size
; /* nothing consumed, but full */
235 return ring_subtract(ring
, ring
->supply
, ring
->consume
);
238 return ring_subtract(ring
, ring
->mark
, ring
->consume
);
243 * Return the number of CONSECUTIVE bytes available for consuming.
244 * However, don't return more than enough to cross over set mark.
247 ring_full_consecutive(Ring
*ring
)
249 if ((ring
->mark
== 0) || (ring
->mark
== ring
->consume
)) {
250 if ((ring
->supply
< ring
->consume
) || ring_full(ring
)) {
251 return ring_subtract(ring
, ring
->top
, ring
->consume
);
253 return ring_subtract(ring
, ring
->supply
, ring
->consume
);
256 if (ring
->mark
< ring
->consume
) {
257 return ring_subtract(ring
, ring
->top
, ring
->consume
);
258 } else { /* Else, distance to mark */
259 return ring_subtract(ring
, ring
->mark
, ring
->consume
);
265 * Move data into the "supply" portion of of the ring buffer.
268 ring_supply_data(Ring
*ring
, unsigned char *buffer
, int count
)
273 i
= MIN(count
, ring_empty_consecutive(ring
));
274 memcpy(ring
->supply
, buffer
, i
);
275 ring_supplied(ring
, i
);
283 ring_encrypt(Ring
*ring
, void (*encryptor
)(unsigned char *, int))
285 unsigned char *s
, *c
;
287 if (ring_empty(ring
) || ring
->clearto
== ring
->supply
)
290 if (!(c
= ring
->clearto
))
296 (*encryptor
)(c
, ring
->top
- c
);
297 (*encryptor
)(ring
->bottom
, s
- ring
->bottom
);
299 (*encryptor
)(c
, s
- c
);
301 ring
->clearto
= ring
->supply
;
305 ring_clearto(Ring
*ring
)
307 if (!ring_empty(ring
))
308 ring
->clearto
= ring
->supply
;
312 #endif /* ENCRYPTION */