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