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 cc89e101 2023-09-02 op #include "config.h"
28 b8a11905 2023-08-31 op #include <assert.h>
29 b8a11905 2023-08-31 op #include <errno.h>
30 b8a11905 2023-08-31 op #include <poll.h>
31 b8a11905 2023-08-31 op #include <stdarg.h>
32 cc89e101 2023-09-02 op #include <stdint.h>
33 b8a11905 2023-08-31 op #include <stdio.h>
34 b8a11905 2023-08-31 op #include <stdlib.h>
35 b8a11905 2023-08-31 op #include <string.h>
36 b8a11905 2023-08-31 op #include <unistd.h>
38 b8a11905 2023-08-31 op #include "bufio.h"
41 b8a11905 2023-08-31 op buf_init(struct buffer *buf)
43 b8a11905 2023-08-31 op const size_t cap = BIO_CHUNK;
45 b8a11905 2023-08-31 op memset(buf, 0, sizeof(*buf));
46 b8a11905 2023-08-31 op if ((buf->buf = malloc(cap)) == NULL)
48 b8a11905 2023-08-31 op buf->cap = cap;
53 b8a11905 2023-08-31 op buf_grow(struct buffer *buf)
55 b8a11905 2023-08-31 op size_t newcap;
58 b8a11905 2023-08-31 op newcap = buf->cap + BIO_CHUNK;
59 b8a11905 2023-08-31 op t = realloc(buf->buf, newcap);
60 b8a11905 2023-08-31 op if (t == NULL)
63 b8a11905 2023-08-31 op buf->cap = newcap;
68 2216d3fb 2023-09-07 op buf_write(struct buffer *buf, const void *d, size_t len)
70 d20f7b51 2023-09-08 op while (buf->len + len > buf->cap) {
71 2216d3fb 2023-09-07 op if (buf_grow(buf) == -1)
74 2216d3fb 2023-09-07 op memcpy(buf->buf + buf->len, d, len);
75 2216d3fb 2023-09-07 op buf->len += len;
80 b8a11905 2023-08-31 op buf_has_line(struct buffer *buf, const char *nl)
82 b8a11905 2023-08-31 op return (memmem(buf->buf, buf->len, nl, strlen(nl)) != NULL);
86 b8a11905 2023-08-31 op buf_drain(struct buffer *buf, size_t l)
88 b8a11905 2023-08-31 op if (l >= buf->len) {
93 b8a11905 2023-08-31 op memmove(buf->buf, buf->buf + l, buf->len - l);
94 b8a11905 2023-08-31 op buf->len -= l;
98 b8a11905 2023-08-31 op buf_drain_line(struct buffer *buf, const char *nl)
100 b8a11905 2023-08-31 op uint8_t *endln;
103 b8a11905 2023-08-31 op nlen = strlen(nl);
104 b8a11905 2023-08-31 op if ((endln = memmem(buf->buf, buf->len, nl, nlen)) == NULL)
106 b8a11905 2023-08-31 op buf_drain(buf, endln + nlen - buf->buf);
110 b8a11905 2023-08-31 op buf_free(struct buffer *buf)
112 b8a11905 2023-08-31 op free(buf->buf);
113 b8a11905 2023-08-31 op memset(buf, 0, sizeof(*buf));
117 b8a11905 2023-08-31 op bufio_init(struct bufio *bio)
119 b8a11905 2023-08-31 op memset(bio, 0, sizeof(*bio));
120 b8a11905 2023-08-31 op bio->fd = -1;
122 b8a11905 2023-08-31 op if (buf_init(&bio->wbuf) == -1)
124 b8a11905 2023-08-31 op if (buf_init(&bio->rbuf) == -1) {
125 b8a11905 2023-08-31 op buf_free(&bio->wbuf);
132 d48ffc66 2023-08-31 op bufio_free(struct bufio *bio)
134 b8a11905 2023-08-31 op if (bio->fd != -1)
135 b8a11905 2023-08-31 op close(bio->fd);
137 b8a11905 2023-08-31 op buf_free(&bio->rbuf);
138 b8a11905 2023-08-31 op buf_free(&bio->wbuf);
142 d48ffc66 2023-08-31 op bufio_reset(struct bufio *bio)
144 d48ffc66 2023-08-31 op bufio_free(bio);
145 b8a11905 2023-08-31 op return (bufio_init(bio));
149 b8a11905 2023-08-31 op bufio_set_fd(struct bufio *bio, int fd)
151 b8a11905 2023-08-31 op bio->fd = fd;
155 d48ffc66 2023-08-31 op bufio_set_chunked(struct bufio *bio, int chunked)
157 d48ffc66 2023-08-31 op bio->chunked = chunked;
161 b8a11905 2023-08-31 op bufio_pollev(struct bufio *bio)
166 b8a11905 2023-08-31 op if (bio->wbuf.len != 0)
167 b8a11905 2023-08-31 op ev |= POLLOUT;
173 b8a11905 2023-08-31 op bufio_read(struct bufio *bio)
175 b8a11905 2023-08-31 op struct buffer *rbuf = &bio->rbuf;
178 b8a11905 2023-08-31 op assert(rbuf->cap >= rbuf->len);
179 b8a11905 2023-08-31 op if (rbuf->cap - rbuf->len < BIO_CHUNK) {
180 b8a11905 2023-08-31 op if (buf_grow(rbuf) == -1)
184 b8a11905 2023-08-31 op r = read(bio->fd, rbuf->buf + rbuf->len, rbuf->cap - rbuf->len);
187 b8a11905 2023-08-31 op rbuf->len += r;
192 d48ffc66 2023-08-31 op bufio_drain(struct bufio *bio, void *d, size_t len)
194 d48ffc66 2023-08-31 op struct buffer *rbuf = &bio->rbuf;
196 d48ffc66 2023-08-31 op if (len > rbuf->len)
197 d48ffc66 2023-08-31 op len = rbuf->len;
198 d48ffc66 2023-08-31 op memcpy(d, rbuf->buf, len);
199 d48ffc66 2023-08-31 op buf_drain(rbuf, len);
200 d48ffc66 2023-08-31 op return (len);
204 b8a11905 2023-08-31 op bufio_write(struct bufio *bio)
206 b8a11905 2023-08-31 op struct buffer *wbuf = &bio->wbuf;
209 b8a11905 2023-08-31 op w = write(bio->fd, wbuf->buf, wbuf->len);
212 b8a11905 2023-08-31 op buf_drain(wbuf, w);
217 d48ffc66 2023-08-31 op bufio_append(struct bufio *bio, const void *d, size_t len)
219 b8a11905 2023-08-31 op struct buffer *wbuf = &bio->wbuf;
221 d48ffc66 2023-08-31 op if (len == 0)
224 b8a11905 2023-08-31 op while (wbuf->cap - wbuf->len < len) {
225 b8a11905 2023-08-31 op if (buf_grow(wbuf) == -1)
229 b8a11905 2023-08-31 op memcpy(wbuf->buf + wbuf->len, d, len);
230 b8a11905 2023-08-31 op wbuf->len += len;
235 d48ffc66 2023-08-31 op bufio_compose(struct bufio *bio, const void *d, size_t len)
240 d48ffc66 2023-08-31 op if (bio->chunked) {
241 d48ffc66 2023-08-31 op r = snprintf(n, sizeof(n), "%zx\r\n", len);
242 d48ffc66 2023-08-31 op if (r < 0 || (size_t)r >= sizeof(n))
244 d48ffc66 2023-08-31 op if (bufio_append(bio, n, r) == -1)
248 d48ffc66 2023-08-31 op if (bufio_append(bio, d, len) == -1)
251 d48ffc66 2023-08-31 op if (bio->chunked)
252 d48ffc66 2023-08-31 op return bufio_append(bio, "\r\n", 2);
258 b8a11905 2023-08-31 op bufio_compose_str(struct bufio *bio, const char *str)
260 b8a11905 2023-08-31 op return (bufio_compose(bio, str, strlen(str)));
264 b8a11905 2023-08-31 op bufio_compose_fmt(struct bufio *bio, const char *fmt, ...)
270 b8a11905 2023-08-31 op va_start(ap, fmt);
271 b8a11905 2023-08-31 op r = vasprintf(&str, fmt, ap);
276 b8a11905 2023-08-31 op r = bufio_compose(bio, str, r);