2 b8a11905 2023-08-31 op * This is free and unencumbered software released into the public domain.
4 b8a11905 2023-08-31 op * Anyone is free to copy, modify, publish, use, compile, sell, or
5 b8a11905 2023-08-31 op * distribute this software, either in source code form or as a compiled
6 b8a11905 2023-08-31 op * binary, for any purpose, commercial or non-commercial, and by any
9 b8a11905 2023-08-31 op * In jurisdictions that recognize copyright laws, the author or authors
10 b8a11905 2023-08-31 op * of this software dedicate any and all copyright interest in the
11 b8a11905 2023-08-31 op * software to the public domain. We make this dedication for the benefit
12 b8a11905 2023-08-31 op * of the public at large and to the detriment of our heirs and
13 b8a11905 2023-08-31 op * successors. We intend this dedication to be an overt act of
14 b8a11905 2023-08-31 op * relinquishment in perpetuity of all present and future rights to this
15 b8a11905 2023-08-31 op * software under copyright law.
17 b8a11905 2023-08-31 op * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 b8a11905 2023-08-31 op * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 b8a11905 2023-08-31 op * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 b8a11905 2023-08-31 op * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 b8a11905 2023-08-31 op * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 b8a11905 2023-08-31 op * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 b8a11905 2023-08-31 op * OTHER DEALINGS IN THE SOFTWARE.
26 b8a11905 2023-08-31 op #include <assert.h>
27 b8a11905 2023-08-31 op #include <errno.h>
28 b8a11905 2023-08-31 op #include <poll.h>
29 b8a11905 2023-08-31 op #include <stdarg.h>
30 b8a11905 2023-08-31 op #include <stdio.h>
31 b8a11905 2023-08-31 op #include <stdlib.h>
32 b8a11905 2023-08-31 op #include <string.h>
33 b8a11905 2023-08-31 op #include <unistd.h>
35 b8a11905 2023-08-31 op #include "bufio.h"
38 b8a11905 2023-08-31 op buf_init(struct buffer *buf)
40 b8a11905 2023-08-31 op const size_t cap = BIO_CHUNK;
42 b8a11905 2023-08-31 op memset(buf, 0, sizeof(*buf));
43 b8a11905 2023-08-31 op if ((buf->buf = malloc(cap)) == NULL)
45 b8a11905 2023-08-31 op buf->cap = cap;
50 b8a11905 2023-08-31 op buf_grow(struct buffer *buf)
52 b8a11905 2023-08-31 op size_t newcap;
55 b8a11905 2023-08-31 op newcap = buf->cap + BIO_CHUNK;
56 b8a11905 2023-08-31 op t = realloc(buf->buf, newcap);
57 b8a11905 2023-08-31 op if (t == NULL)
60 b8a11905 2023-08-31 op buf->cap = newcap;
65 b8a11905 2023-08-31 op buf_has_line(struct buffer *buf, const char *nl)
67 b8a11905 2023-08-31 op return (memmem(buf->buf, buf->len, nl, strlen(nl)) != NULL);
71 b8a11905 2023-08-31 op buf_drain(struct buffer *buf, size_t l)
73 b8a11905 2023-08-31 op if (l >= buf->len) {
78 b8a11905 2023-08-31 op memmove(buf->buf, buf->buf + l, buf->len - l);
79 b8a11905 2023-08-31 op buf->len -= l;
83 b8a11905 2023-08-31 op buf_drain_line(struct buffer *buf, const char *nl)
85 b8a11905 2023-08-31 op uint8_t *endln;
88 b8a11905 2023-08-31 op nlen = strlen(nl);
89 b8a11905 2023-08-31 op if ((endln = memmem(buf->buf, buf->len, nl, nlen)) == NULL)
91 b8a11905 2023-08-31 op buf_drain(buf, endln + nlen - buf->buf);
95 b8a11905 2023-08-31 op buf_free(struct buffer *buf)
97 b8a11905 2023-08-31 op free(buf->buf);
98 b8a11905 2023-08-31 op memset(buf, 0, sizeof(*buf));
102 b8a11905 2023-08-31 op bufio_init(struct bufio *bio)
104 b8a11905 2023-08-31 op memset(bio, 0, sizeof(*bio));
105 b8a11905 2023-08-31 op bio->fd = -1;
107 b8a11905 2023-08-31 op if (buf_init(&bio->wbuf) == -1)
109 b8a11905 2023-08-31 op if (buf_init(&bio->rbuf) == -1) {
110 b8a11905 2023-08-31 op buf_free(&bio->wbuf);
117 d48ffc66 2023-08-31 op bufio_free(struct bufio *bio)
119 b8a11905 2023-08-31 op if (bio->fd != -1)
120 b8a11905 2023-08-31 op close(bio->fd);
122 b8a11905 2023-08-31 op buf_free(&bio->rbuf);
123 b8a11905 2023-08-31 op buf_free(&bio->wbuf);
127 d48ffc66 2023-08-31 op bufio_reset(struct bufio *bio)
129 d48ffc66 2023-08-31 op bufio_free(bio);
130 b8a11905 2023-08-31 op return (bufio_init(bio));
134 b8a11905 2023-08-31 op bufio_set_fd(struct bufio *bio, int fd)
136 b8a11905 2023-08-31 op bio->fd = fd;
140 d48ffc66 2023-08-31 op bufio_set_chunked(struct bufio *bio, int chunked)
142 d48ffc66 2023-08-31 op bio->chunked = chunked;
146 b8a11905 2023-08-31 op bufio_pollev(struct bufio *bio)
151 b8a11905 2023-08-31 op if (bio->wbuf.len != 0)
152 b8a11905 2023-08-31 op ev |= POLLOUT;
158 b8a11905 2023-08-31 op bufio_read(struct bufio *bio)
160 b8a11905 2023-08-31 op struct buffer *rbuf = &bio->rbuf;
163 b8a11905 2023-08-31 op assert(rbuf->cap >= rbuf->len);
164 b8a11905 2023-08-31 op if (rbuf->cap - rbuf->len < BIO_CHUNK) {
165 b8a11905 2023-08-31 op if (buf_grow(rbuf) == -1)
169 b8a11905 2023-08-31 op r = read(bio->fd, rbuf->buf + rbuf->len, rbuf->cap - rbuf->len);
172 b8a11905 2023-08-31 op rbuf->len += r;
177 d48ffc66 2023-08-31 op bufio_drain(struct bufio *bio, void *d, size_t len)
179 d48ffc66 2023-08-31 op struct buffer *rbuf = &bio->rbuf;
181 d48ffc66 2023-08-31 op if (len > rbuf->len)
182 d48ffc66 2023-08-31 op len = rbuf->len;
183 d48ffc66 2023-08-31 op memcpy(d, rbuf->buf, len);
184 d48ffc66 2023-08-31 op buf_drain(rbuf, len);
185 d48ffc66 2023-08-31 op return (len);
189 b8a11905 2023-08-31 op bufio_write(struct bufio *bio)
191 b8a11905 2023-08-31 op struct buffer *wbuf = &bio->wbuf;
194 b8a11905 2023-08-31 op w = write(bio->fd, wbuf->buf, wbuf->len);
197 b8a11905 2023-08-31 op buf_drain(wbuf, w);
202 d48ffc66 2023-08-31 op bufio_append(struct bufio *bio, const void *d, size_t len)
204 b8a11905 2023-08-31 op struct buffer *wbuf = &bio->wbuf;
206 d48ffc66 2023-08-31 op if (len == 0)
209 b8a11905 2023-08-31 op while (wbuf->cap - wbuf->len < len) {
210 b8a11905 2023-08-31 op if (buf_grow(wbuf) == -1)
214 b8a11905 2023-08-31 op memcpy(wbuf->buf + wbuf->len, d, len);
215 b8a11905 2023-08-31 op wbuf->len += len;
220 d48ffc66 2023-08-31 op bufio_compose(struct bufio *bio, const void *d, size_t len)
225 d48ffc66 2023-08-31 op if (bio->chunked) {
226 d48ffc66 2023-08-31 op r = snprintf(n, sizeof(n), "%zx\r\n", len);
227 d48ffc66 2023-08-31 op if (r < 0 || (size_t)r >= sizeof(n))
229 d48ffc66 2023-08-31 op if (bufio_append(bio, n, r) == -1)
233 d48ffc66 2023-08-31 op if (bufio_append(bio, d, len) == -1)
236 d48ffc66 2023-08-31 op if (bio->chunked)
237 d48ffc66 2023-08-31 op return bufio_append(bio, "\r\n", 2);
243 b8a11905 2023-08-31 op bufio_compose_str(struct bufio *bio, const char *str)
245 b8a11905 2023-08-31 op return (bufio_compose(bio, str, strlen(str)));
249 b8a11905 2023-08-31 op bufio_compose_fmt(struct bufio *bio, const char *fmt, ...)
255 b8a11905 2023-08-31 op va_start(ap, fmt);
256 b8a11905 2023-08-31 op r = vasprintf(&str, fmt, ap);
261 b8a11905 2023-08-31 op r = bufio_compose(bio, str, r);