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