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