Blob


1 /*
2 * This is free and unencumbered software released into the public domain.
3 *
4 * Anyone is free to copy, modify, publish, use, compile, sell, or
5 * distribute this software, either in source code form or as a compiled
6 * binary, for any purpose, commercial or non-commercial, and by any
7 * means.
8 *
9 * In jurisdictions that recognize copyright laws, the author or authors
10 * of this software dedicate any and all copyright interest in the
11 * software to the public domain. We make this dedication for the benefit
12 * of the public at large and to the detriment of our heirs and
13 * successors. We intend this dedication to be an overt act of
14 * relinquishment in perpetuity of all present and future rights to this
15 * software under copyright law.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
26 #include "config.h"
28 #include <assert.h>
29 #include <errno.h>
30 #include <poll.h>
31 #include <stdarg.h>
32 #include <stdint.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
38 #include "bufio.h"
40 int
41 buf_init(struct buffer *buf)
42 {
43 const size_t cap = BIO_CHUNK;
45 memset(buf, 0, sizeof(*buf));
46 if ((buf->buf = malloc(cap)) == NULL)
47 return (-1);
48 buf->cap = cap;
49 return (0);
50 }
52 static int
53 buf_grow(struct buffer *buf)
54 {
55 size_t newcap;
56 void *t;
58 newcap = buf->cap + BIO_CHUNK;
59 t = realloc(buf->buf, newcap);
60 if (t == NULL)
61 return (-1);
62 buf->buf = t;
63 buf->cap = newcap;
64 return (0);
65 }
67 int
68 buf_write(struct buffer *buf, const void *d, size_t len)
69 {
70 while (buf->len + len > buf->cap) {
71 if (buf_grow(buf) == -1)
72 return (-1);
73 }
74 memcpy(buf->buf + buf->len, d, len);
75 buf->len += len;
76 return (0);
77 }
79 int
80 buf_has_line(struct buffer *buf, const char *nl)
81 {
82 return (memmem(buf->buf, buf->len, nl, strlen(nl)) != NULL);
83 }
85 void
86 buf_drain(struct buffer *buf, size_t l)
87 {
88 if (l >= buf->len) {
89 buf->len = 0;
90 return;
91 }
93 memmove(buf->buf, buf->buf + l, buf->len - l);
94 buf->len -= l;
95 }
97 void
98 buf_drain_line(struct buffer *buf, const char *nl)
99 {
100 uint8_t *endln;
101 size_t nlen;
103 nlen = strlen(nl);
104 if ((endln = memmem(buf->buf, buf->len, nl, nlen)) == NULL)
105 return;
106 buf_drain(buf, endln + nlen - buf->buf);
109 void
110 buf_free(struct buffer *buf)
112 free(buf->buf);
113 memset(buf, 0, sizeof(*buf));
116 int
117 bufio_init(struct bufio *bio)
119 memset(bio, 0, sizeof(*bio));
120 bio->fd = -1;
122 if (buf_init(&bio->wbuf) == -1)
123 return (-1);
124 if (buf_init(&bio->rbuf) == -1) {
125 buf_free(&bio->wbuf);
126 return (-1);
128 return (0);
131 void
132 bufio_free(struct bufio *bio)
134 if (bio->fd != -1)
135 close(bio->fd);
137 buf_free(&bio->rbuf);
138 buf_free(&bio->wbuf);
141 int
142 bufio_reset(struct bufio *bio)
144 bufio_free(bio);
145 return (bufio_init(bio));
148 void
149 bufio_set_fd(struct bufio *bio, int fd)
151 bio->fd = fd;
154 void
155 bufio_set_chunked(struct bufio *bio, int chunked)
157 bio->chunked = chunked;
160 short
161 bufio_pollev(struct bufio *bio)
163 short ev;
165 ev = POLLIN;
166 if (bio->wbuf.len != 0)
167 ev |= POLLOUT;
169 return (ev);
172 ssize_t
173 bufio_read(struct bufio *bio)
175 struct buffer *rbuf = &bio->rbuf;
176 ssize_t r;
178 assert(rbuf->cap >= rbuf->len);
179 if (rbuf->cap - rbuf->len < BIO_CHUNK) {
180 if (buf_grow(rbuf) == -1)
181 return (-1);
184 r = read(bio->fd, rbuf->buf + rbuf->len, rbuf->cap - rbuf->len);
185 if (r == -1)
186 return (-1);
187 rbuf->len += r;
188 return (r);
191 size_t
192 bufio_drain(struct bufio *bio, void *d, size_t len)
194 struct buffer *rbuf = &bio->rbuf;
196 if (len > rbuf->len)
197 len = rbuf->len;
198 memcpy(d, rbuf->buf, len);
199 buf_drain(rbuf, len);
200 return (len);
203 ssize_t
204 bufio_write(struct bufio *bio)
206 struct buffer *wbuf = &bio->wbuf;
207 ssize_t w;
209 w = write(bio->fd, wbuf->buf, wbuf->len);
210 if (w == -1)
211 return (-1);
212 buf_drain(wbuf, w);
213 return (w);
216 static int
217 bufio_append(struct bufio *bio, const void *d, size_t len)
219 struct buffer *wbuf = &bio->wbuf;
221 if (len == 0)
222 return (0);
224 while (wbuf->cap - wbuf->len < len) {
225 if (buf_grow(wbuf) == -1)
226 return (-1);
229 memcpy(wbuf->buf + wbuf->len, d, len);
230 wbuf->len += len;
231 return (0);
234 int
235 bufio_compose(struct bufio *bio, const void *d, size_t len)
237 char n[16];
238 int r;
240 if (bio->chunked) {
241 r = snprintf(n, sizeof(n), "%zx\r\n", len);
242 if (r < 0 || (size_t)r >= sizeof(n))
243 return (-1);
244 if (bufio_append(bio, n, r) == -1)
245 return (-1);
248 if (bufio_append(bio, d, len) == -1)
249 return (-1);
251 if (bio->chunked)
252 return bufio_append(bio, "\r\n", 2);
254 return (0);
257 int
258 bufio_compose_str(struct bufio *bio, const char *str)
260 return (bufio_compose(bio, str, strlen(str)));
263 int
264 bufio_compose_fmt(struct bufio *bio, const char *fmt, ...)
266 va_list ap;
267 char *str;
268 int r;
270 va_start(ap, fmt);
271 r = vasprintf(&str, fmt, ap);
272 va_end(ap);
274 if (r == -1)
275 return (-1);
276 r = bufio_compose(bio, str, r);
277 free(str);
278 return (r);