chiark / gitweb /
General build system spring-cleaning.
[mLib] / buf / pkbuf.c
1 /* -*-c-*-
2  *
3  * Simple packet buffering
4  *
5  * (c) 2000 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of the mLib utilities library.
11  *
12  * mLib is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU Library General Public License as
14  * published by the Free Software Foundation; either version 2 of the
15  * License, or (at your option) any later version.
16  *
17  * mLib is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU Library General Public License for more details.
21  *
22  * You should have received a copy of the GNU Library General Public
23  * License along with mLib; if not, write to the Free
24  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25  * MA 02111-1307, USA.
26  */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "alloc.h"
35 #include "arena.h"
36 #include "pkbuf.h"
37
38 /*----- Main code ---------------------------------------------------------*/
39
40 /* --- @pkbuf_flush@ --- *
41  *
42  * Arguments:   @pkbuf *pk@ = pointer to buffer block
43  *              @octet *p@ = pointer to where to start searching
44  *              @size_t len@ = length of new material added
45  *
46  * Returns:     ---
47  *
48  * Use:         Flushes any complete packets in a packet buffer.  New
49  *              material is assumed to have been added starting at @p@.  If
50  *              @p@ is null, then the scan starts at the beginning of the
51  *              buffer, and the size of data already in the buffer is used in
52  *              place of @len@.
53  *
54  *              It is assumed that the buffer is initially enabled.  You
55  *              shouldn't be contributing data to a disabled buffer anyway.
56  *              However, the buffer handler may at some point disable itself,
57  *              and @pkbuf_flush@ can cope with this eventuality.  Any
58  *              pending data is left at the start of the buffer and can be
59  *              flushed out by calling @pkbuf_flush(b, 0, 0)@ if the buffer
60  *              is ever re-enabled.
61  */
62
63 void pkbuf_flush(pkbuf *pk, octet *p, size_t len)
64 {
65   size_t l;
66   size_t o, keep;
67
68   if (pk->f & PKBUF_CLOSE) {
69     pk->func(0, 0, pk, 0, pk->p);
70     return;
71   }
72
73   /* --- Initialize variables as necessary --- */
74
75   if (!p) {
76     p = pk->buf;
77     len = pk->len;
78   }
79   l = p + len - pk->buf;
80   o = 0;
81
82   /* --- Now grind through any packets which have accumulated --- */
83
84   pk->len = l;
85   while (l >= pk->want) {
86     size_t sz = pk->want;
87
88     /* --- Pass a packet to the user handler --- */
89
90     keep = 0;
91     pk->func(pk->buf + o, sz, pk, &keep, pk->p);
92
93     /* --- Adjust all the pointers for the next packet --- */
94
95     sz -= keep;
96     o += sz;
97     l -= sz;
98
99     /* --- Abort here if disabled --- */
100
101     if (!(pk->f & PKBUF_ENABLE))
102       break;
103   }
104
105   /* --- Shunt data around in the buffer --- */
106
107   if (o > 0 && l != 0)
108     memmove(pk->buf, pk->buf + o, l);
109   pk->len = l;
110 }
111
112 /* --- @pkbuf_close@ --- *
113  *
114  * Arguments:   @pkbuf *pk@ = pointer to buffer block
115  *
116  * Returns:     ---
117  *
118  * Use:         Informs the client that no more data is likely to arrive.  If
119  *              there is a partial packet in the buffer, it is discarded.
120  */
121
122 void pkbuf_close(pkbuf *pk)
123 {
124   if (pk->buf) {
125     x_free(pk->a, pk->buf);
126     pk->buf = 0;
127   }
128   pk->f |= PKBUF_CLOSE;
129   if (pk->f & PKBUF_ENABLE)
130     pk->func(0, 0, pk, 0, pk->p);
131 }
132
133 /* --- @pkbuf_free@ --- *
134  *
135  * Arguments:   @pkbuf *pk@ = pointer to buffer block
136  *              @octet **p@ = output pointer to free space
137  *
138  * Returns:     Free buffer size.
139  *
140  * Use:         Returns the free portion of a packet buffer.  Data can then
141  *              be written to this portion, and split out into packets by
142  *              calling @pkbuf_flush@.  A buffer is allocated if none
143  *              currently exists.
144  */
145
146 size_t pkbuf_free(pkbuf *pk, octet **p)
147 {
148   if (!pk->buf)
149     pk->buf = x_alloc(pk->a, pk->sz);
150   *p = pk->buf + pk->len;
151   return (pk->sz - pk->len);
152 }
153
154 /* --- @pkbuf_snarf@ --- *
155  *
156  * Arguments:   @pkbuf *pk@ = pointer to buffer block
157  *              @const void *p@ = pointer to input data buffer
158  *              @size_t sz@ = size of data in input buffer
159  *
160  * Returns:     ---
161  *
162  * Use:         Snarfs the data from the input buffer and spits it out as
163  *              packets.  This interface ignores the complexities of dealing
164  *              with disablement: you should be using @pkbuf_free@ to
165  *              contribute data if you want to cope with that.
166  */
167
168 void pkbuf_snarf(pkbuf *pk, const void *p, size_t sz)
169 {
170   const octet *pp = p;
171   while (sz && (pk->f & PKBUF_ENABLE)) {
172     size_t bsz;
173     octet *bp;
174
175     bsz = pkbuf_free(pk, &bp);
176     if (bsz > sz)
177       bsz = sz;
178     memcpy(bp, pp, bsz);
179     pkbuf_flush(pk, bp, bsz);
180     pp += bsz;
181     sz -= bsz;
182   }
183 }
184
185 /* --- @pkbuf_want@ --- *
186  *
187  * Arguments:   @pkbuf *pk@ = pointer to buffer block
188  *              @size_t want@ = how many octets wanted for next packet
189  *
190  * Returns:     ---
191  *
192  * Use:         Sets the desired size for the next packet to be read.  If
193  *              it's larger than the current buffer, the buffer is extended.
194  */
195
196 void pkbuf_want(pkbuf *pk, size_t want)
197 {
198   pk->want = want;
199   if (want > pk->sz) {
200     do pk->sz <<= 1; while (want > pk->sz);
201     if (pk->buf) {
202       if (pk->len)
203         pk->buf = x_realloc(pk->a, pk->buf, pk->sz, pk->len);
204       else {
205         x_free(pk->a, pk->buf);
206         pk->buf = 0;
207       }
208     }
209   }
210 }
211
212 /* --- @pkbuf_init@ --- *
213  *
214  * Arguments:   @pkbuf *pk@ = pointer to buffer block
215  *              @pkbuf *func@ = handler function
216  *              @void *p@ = argument pointer for @func@
217  *
218  * Returns:     ---
219  *
220  * Use:         Initializes a packet buffer block.  Any packets are passed to
221  *              the provided function for handling.
222  */
223
224 void pkbuf_init(pkbuf *pk, pkbuf_func *func, void *p)
225 {
226   pk->func = func;
227   pk->p = p;
228   pk->len = 0;
229   pk->f = PKBUF_ENABLE;
230   pk->buf = 0;
231   pk->sz = 256;
232   pk->want = 1;
233   pk->a = arena_global;
234 }
235
236 /* --- @pkbuf_destroy@ --- *
237  *
238  * Arguments:   @pkbuf *pk@ = pointer to buffer block
239  *
240  * Returns:     ---
241  *
242  * Use:         Deallocates a line buffer and frees any resources it owned.
243  */
244
245 void pkbuf_destroy(pkbuf *pk)
246 {
247   if (pk->buf) {
248     x_free(pk->a, pk->buf);
249     pk->buf = 0;
250   }
251 }
252
253 /*----- That's all, folks -------------------------------------------------*/