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 b8a11905 2023-08-31 op buf_has_line(struct buffer *buf, const char *nl)
70 b8a11905 2023-08-31 op return (memmem(buf->buf, buf->len, nl, strlen(nl)) != NULL);
74 b8a11905 2023-08-31 op buf_drain(struct buffer *buf, size_t l)
76 b8a11905 2023-08-31 op if (l >= buf->len) {
81 b8a11905 2023-08-31 op memmove(buf->buf, buf->buf + l, buf->len - l);
82 b8a11905 2023-08-31 op buf->len -= l;
86 b8a11905 2023-08-31 op buf_drain_line(struct buffer *buf, const char *nl)
88 b8a11905 2023-08-31 op uint8_t *endln;
91 b8a11905 2023-08-31 op nlen = strlen(nl);
92 b8a11905 2023-08-31 op if ((endln = memmem(buf->buf, buf->len, nl, nlen)) == NULL)
94 b8a11905 2023-08-31 op buf_drain(buf, endln + nlen - buf->buf);
98 b8a11905 2023-08-31 op buf_free(struct buffer *buf)
100 b8a11905 2023-08-31 op free(buf->buf);
101 b8a11905 2023-08-31 op memset(buf, 0, sizeof(*buf));
105 b8a11905 2023-08-31 op bufio_init(struct bufio *bio)
107 b8a11905 2023-08-31 op memset(bio, 0, sizeof(*bio));
108 b8a11905 2023-08-31 op bio->fd = -1;
110 b8a11905 2023-08-31 op if (buf_init(&bio->wbuf) == -1)
112 b8a11905 2023-08-31 op if (buf_init(&bio->rbuf) == -1) {
113 b8a11905 2023-08-31 op buf_free(&bio->wbuf);
120 d48ffc66 2023-08-31 op bufio_free(struct bufio *bio)
122 b8a11905 2023-08-31 op if (bio->fd != -1)
123 b8a11905 2023-08-31 op close(bio->fd);
125 b8a11905 2023-08-31 op buf_free(&bio->rbuf);
126 b8a11905 2023-08-31 op buf_free(&bio->wbuf);
130 d48ffc66 2023-08-31 op bufio_reset(struct bufio *bio)
132 d48ffc66 2023-08-31 op bufio_free(bio);
133 b8a11905 2023-08-31 op return (bufio_init(bio));
137 b8a11905 2023-08-31 op bufio_set_fd(struct bufio *bio, int fd)
139 b8a11905 2023-08-31 op bio->fd = fd;
143 d48ffc66 2023-08-31 op bufio_set_chunked(struct bufio *bio, int chunked)
145 d48ffc66 2023-08-31 op bio->chunked = chunked;
149 b8a11905 2023-08-31 op bufio_pollev(struct bufio *bio)
154 b8a11905 2023-08-31 op if (bio->wbuf.len != 0)
155 b8a11905 2023-08-31 op ev |= POLLOUT;
161 b8a11905 2023-08-31 op bufio_read(struct bufio *bio)
163 b8a11905 2023-08-31 op struct buffer *rbuf = &bio->rbuf;
166 b8a11905 2023-08-31 op assert(rbuf->cap >= rbuf->len);
167 b8a11905 2023-08-31 op if (rbuf->cap - rbuf->len < BIO_CHUNK) {
168 b8a11905 2023-08-31 op if (buf_grow(rbuf) == -1)
172 b8a11905 2023-08-31 op r = read(bio->fd, rbuf->buf + rbuf->len, rbuf->cap - rbuf->len);
175 b8a11905 2023-08-31 op rbuf->len += r;
180 d48ffc66 2023-08-31 op bufio_drain(struct bufio *bio, void *d, size_t len)
182 d48ffc66 2023-08-31 op struct buffer *rbuf = &bio->rbuf;
184 d48ffc66 2023-08-31 op if (len > rbuf->len)
185 d48ffc66 2023-08-31 op len = rbuf->len;
186 d48ffc66 2023-08-31 op memcpy(d, rbuf->buf, len);
187 d48ffc66 2023-08-31 op buf_drain(rbuf, len);
188 d48ffc66 2023-08-31 op return (len);
192 b8a11905 2023-08-31 op bufio_write(struct bufio *bio)
194 b8a11905 2023-08-31 op struct buffer *wbuf = &bio->wbuf;
197 b8a11905 2023-08-31 op w = write(bio->fd, wbuf->buf, wbuf->len);
200 b8a11905 2023-08-31 op buf_drain(wbuf, w);
205 d48ffc66 2023-08-31 op bufio_append(struct bufio *bio, const void *d, size_t len)
207 b8a11905 2023-08-31 op struct buffer *wbuf = &bio->wbuf;
209 d48ffc66 2023-08-31 op if (len == 0)
212 b8a11905 2023-08-31 op while (wbuf->cap - wbuf->len < len) {
213 b8a11905 2023-08-31 op if (buf_grow(wbuf) == -1)
217 b8a11905 2023-08-31 op memcpy(wbuf->buf + wbuf->len, d, len);
218 b8a11905 2023-08-31 op wbuf->len += len;
223 d48ffc66 2023-08-31 op bufio_compose(struct bufio *bio, const void *d, size_t len)
228 d48ffc66 2023-08-31 op if (bio->chunked) {
229 d48ffc66 2023-08-31 op r = snprintf(n, sizeof(n), "%zx\r\n", len);
230 d48ffc66 2023-08-31 op if (r < 0 || (size_t)r >= sizeof(n))
232 d48ffc66 2023-08-31 op if (bufio_append(bio, n, r) == -1)
236 d48ffc66 2023-08-31 op if (bufio_append(bio, d, len) == -1)
239 d48ffc66 2023-08-31 op if (bio->chunked)
240 d48ffc66 2023-08-31 op return bufio_append(bio, "\r\n", 2);
246 b8a11905 2023-08-31 op bufio_compose_str(struct bufio *bio, const char *str)
248 b8a11905 2023-08-31 op return (bufio_compose(bio, str, strlen(str)));
252 b8a11905 2023-08-31 op bufio_compose_fmt(struct bufio *bio, const char *fmt, ...)
258 b8a11905 2023-08-31 op va_start(ap, fmt);
259 b8a11905 2023-08-31 op r = vasprintf(&str, fmt, ap);
264 b8a11905 2023-08-31 op r = bufio_compose(bio, str, r);