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