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