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