Blame


1 b8a11905 2023-08-31 op /*
2 b8a11905 2023-08-31 op * This is free and unencumbered software released into the public domain.
3 b8a11905 2023-08-31 op *
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
7 b8a11905 2023-08-31 op * means.
8 b8a11905 2023-08-31 op *
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.
16 b8a11905 2023-08-31 op *
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.
24 b8a11905 2023-08-31 op */
25 b8a11905 2023-08-31 op
26 cc89e101 2023-09-02 op #include "config.h"
27 cc89e101 2023-09-02 op
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>
37 b8a11905 2023-08-31 op
38 b8a11905 2023-08-31 op #include "bufio.h"
39 b8a11905 2023-08-31 op
40 b8a11905 2023-08-31 op int
41 b8a11905 2023-08-31 op buf_init(struct buffer *buf)
42 b8a11905 2023-08-31 op {
43 b8a11905 2023-08-31 op const size_t cap = BIO_CHUNK;
44 b8a11905 2023-08-31 op
45 b8a11905 2023-08-31 op memset(buf, 0, sizeof(*buf));
46 b8a11905 2023-08-31 op if ((buf->buf = malloc(cap)) == NULL)
47 b8a11905 2023-08-31 op return (-1);
48 b8a11905 2023-08-31 op buf->cap = cap;
49 b8a11905 2023-08-31 op return (0);
50 b8a11905 2023-08-31 op }
51 b8a11905 2023-08-31 op
52 b8a11905 2023-08-31 op static int
53 b8a11905 2023-08-31 op buf_grow(struct buffer *buf)
54 b8a11905 2023-08-31 op {
55 b8a11905 2023-08-31 op size_t newcap;
56 b8a11905 2023-08-31 op void *t;
57 b8a11905 2023-08-31 op
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)
61 b8a11905 2023-08-31 op return (-1);
62 b8a11905 2023-08-31 op buf->buf = t;
63 b8a11905 2023-08-31 op buf->cap = newcap;
64 b8a11905 2023-08-31 op return (0);
65 b8a11905 2023-08-31 op }
66 b8a11905 2023-08-31 op
67 b8a11905 2023-08-31 op int
68 b8a11905 2023-08-31 op buf_has_line(struct buffer *buf, const char *nl)
69 b8a11905 2023-08-31 op {
70 b8a11905 2023-08-31 op return (memmem(buf->buf, buf->len, nl, strlen(nl)) != NULL);
71 b8a11905 2023-08-31 op }
72 b8a11905 2023-08-31 op
73 b8a11905 2023-08-31 op void
74 b8a11905 2023-08-31 op buf_drain(struct buffer *buf, size_t l)
75 b8a11905 2023-08-31 op {
76 b8a11905 2023-08-31 op if (l >= buf->len) {
77 b8a11905 2023-08-31 op buf->len = 0;
78 b8a11905 2023-08-31 op return;
79 b8a11905 2023-08-31 op }
80 b8a11905 2023-08-31 op
81 b8a11905 2023-08-31 op memmove(buf->buf, buf->buf + l, buf->len - l);
82 b8a11905 2023-08-31 op buf->len -= l;
83 b8a11905 2023-08-31 op }
84 b8a11905 2023-08-31 op
85 b8a11905 2023-08-31 op void
86 b8a11905 2023-08-31 op buf_drain_line(struct buffer *buf, const char *nl)
87 b8a11905 2023-08-31 op {
88 b8a11905 2023-08-31 op uint8_t *endln;
89 b8a11905 2023-08-31 op size_t nlen;
90 b8a11905 2023-08-31 op
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)
93 b8a11905 2023-08-31 op return;
94 b8a11905 2023-08-31 op buf_drain(buf, endln + nlen - buf->buf);
95 b8a11905 2023-08-31 op }
96 b8a11905 2023-08-31 op
97 b8a11905 2023-08-31 op void
98 b8a11905 2023-08-31 op buf_free(struct buffer *buf)
99 b8a11905 2023-08-31 op {
100 b8a11905 2023-08-31 op free(buf->buf);
101 b8a11905 2023-08-31 op memset(buf, 0, sizeof(*buf));
102 b8a11905 2023-08-31 op }
103 b8a11905 2023-08-31 op
104 b8a11905 2023-08-31 op int
105 b8a11905 2023-08-31 op bufio_init(struct bufio *bio)
106 b8a11905 2023-08-31 op {
107 b8a11905 2023-08-31 op memset(bio, 0, sizeof(*bio));
108 b8a11905 2023-08-31 op bio->fd = -1;
109 b8a11905 2023-08-31 op
110 b8a11905 2023-08-31 op if (buf_init(&bio->wbuf) == -1)
111 b8a11905 2023-08-31 op return (-1);
112 b8a11905 2023-08-31 op if (buf_init(&bio->rbuf) == -1) {
113 b8a11905 2023-08-31 op buf_free(&bio->wbuf);
114 b8a11905 2023-08-31 op return (-1);
115 b8a11905 2023-08-31 op }
116 b8a11905 2023-08-31 op return (0);
117 b8a11905 2023-08-31 op }
118 b8a11905 2023-08-31 op
119 d48ffc66 2023-08-31 op void
120 d48ffc66 2023-08-31 op bufio_free(struct bufio *bio)
121 b8a11905 2023-08-31 op {
122 b8a11905 2023-08-31 op if (bio->fd != -1)
123 b8a11905 2023-08-31 op close(bio->fd);
124 b8a11905 2023-08-31 op
125 b8a11905 2023-08-31 op buf_free(&bio->rbuf);
126 b8a11905 2023-08-31 op buf_free(&bio->wbuf);
127 d48ffc66 2023-08-31 op }
128 d48ffc66 2023-08-31 op
129 d48ffc66 2023-08-31 op int
130 d48ffc66 2023-08-31 op bufio_reset(struct bufio *bio)
131 d48ffc66 2023-08-31 op {
132 d48ffc66 2023-08-31 op bufio_free(bio);
133 b8a11905 2023-08-31 op return (bufio_init(bio));
134 b8a11905 2023-08-31 op }
135 b8a11905 2023-08-31 op
136 b8a11905 2023-08-31 op void
137 b8a11905 2023-08-31 op bufio_set_fd(struct bufio *bio, int fd)
138 b8a11905 2023-08-31 op {
139 b8a11905 2023-08-31 op bio->fd = fd;
140 b8a11905 2023-08-31 op }
141 b8a11905 2023-08-31 op
142 d48ffc66 2023-08-31 op void
143 d48ffc66 2023-08-31 op bufio_set_chunked(struct bufio *bio, int chunked)
144 d48ffc66 2023-08-31 op {
145 d48ffc66 2023-08-31 op bio->chunked = chunked;
146 d48ffc66 2023-08-31 op }
147 d48ffc66 2023-08-31 op
148 b8a11905 2023-08-31 op short
149 b8a11905 2023-08-31 op bufio_pollev(struct bufio *bio)
150 b8a11905 2023-08-31 op {
151 b8a11905 2023-08-31 op short ev;
152 b8a11905 2023-08-31 op
153 b8a11905 2023-08-31 op ev = POLLIN;
154 b8a11905 2023-08-31 op if (bio->wbuf.len != 0)
155 b8a11905 2023-08-31 op ev |= POLLOUT;
156 b8a11905 2023-08-31 op
157 b8a11905 2023-08-31 op return (ev);
158 b8a11905 2023-08-31 op }
159 b8a11905 2023-08-31 op
160 b8a11905 2023-08-31 op ssize_t
161 b8a11905 2023-08-31 op bufio_read(struct bufio *bio)
162 b8a11905 2023-08-31 op {
163 b8a11905 2023-08-31 op struct buffer *rbuf = &bio->rbuf;
164 b8a11905 2023-08-31 op ssize_t r;
165 b8a11905 2023-08-31 op
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)
169 b8a11905 2023-08-31 op return (-1);
170 b8a11905 2023-08-31 op }
171 b8a11905 2023-08-31 op
172 b8a11905 2023-08-31 op r = read(bio->fd, rbuf->buf + rbuf->len, rbuf->cap - rbuf->len);
173 b8a11905 2023-08-31 op if (r == -1)
174 b8a11905 2023-08-31 op return (-1);
175 b8a11905 2023-08-31 op rbuf->len += r;
176 b8a11905 2023-08-31 op return (r);
177 b8a11905 2023-08-31 op }
178 b8a11905 2023-08-31 op
179 d48ffc66 2023-08-31 op size_t
180 d48ffc66 2023-08-31 op bufio_drain(struct bufio *bio, void *d, size_t len)
181 d48ffc66 2023-08-31 op {
182 d48ffc66 2023-08-31 op struct buffer *rbuf = &bio->rbuf;
183 d48ffc66 2023-08-31 op
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);
189 d48ffc66 2023-08-31 op }
190 d48ffc66 2023-08-31 op
191 b8a11905 2023-08-31 op ssize_t
192 b8a11905 2023-08-31 op bufio_write(struct bufio *bio)
193 b8a11905 2023-08-31 op {
194 b8a11905 2023-08-31 op struct buffer *wbuf = &bio->wbuf;
195 b8a11905 2023-08-31 op ssize_t w;
196 b8a11905 2023-08-31 op
197 b8a11905 2023-08-31 op w = write(bio->fd, wbuf->buf, wbuf->len);
198 b8a11905 2023-08-31 op if (w == -1)
199 b8a11905 2023-08-31 op return (-1);
200 b8a11905 2023-08-31 op buf_drain(wbuf, w);
201 b8a11905 2023-08-31 op return (w);
202 b8a11905 2023-08-31 op }
203 b8a11905 2023-08-31 op
204 d48ffc66 2023-08-31 op static int
205 d48ffc66 2023-08-31 op bufio_append(struct bufio *bio, const void *d, size_t len)
206 b8a11905 2023-08-31 op {
207 b8a11905 2023-08-31 op struct buffer *wbuf = &bio->wbuf;
208 b8a11905 2023-08-31 op
209 d48ffc66 2023-08-31 op if (len == 0)
210 d48ffc66 2023-08-31 op return (0);
211 d48ffc66 2023-08-31 op
212 b8a11905 2023-08-31 op while (wbuf->cap - wbuf->len < len) {
213 b8a11905 2023-08-31 op if (buf_grow(wbuf) == -1)
214 b8a11905 2023-08-31 op return (-1);
215 b8a11905 2023-08-31 op }
216 b8a11905 2023-08-31 op
217 b8a11905 2023-08-31 op memcpy(wbuf->buf + wbuf->len, d, len);
218 b8a11905 2023-08-31 op wbuf->len += len;
219 b8a11905 2023-08-31 op return (0);
220 b8a11905 2023-08-31 op }
221 b8a11905 2023-08-31 op
222 b8a11905 2023-08-31 op int
223 d48ffc66 2023-08-31 op bufio_compose(struct bufio *bio, const void *d, size_t len)
224 d48ffc66 2023-08-31 op {
225 d48ffc66 2023-08-31 op char n[16];
226 d48ffc66 2023-08-31 op int r;
227 d48ffc66 2023-08-31 op
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))
231 d48ffc66 2023-08-31 op return (-1);
232 d48ffc66 2023-08-31 op if (bufio_append(bio, n, r) == -1)
233 d48ffc66 2023-08-31 op return (-1);
234 d48ffc66 2023-08-31 op }
235 d48ffc66 2023-08-31 op
236 d48ffc66 2023-08-31 op if (bufio_append(bio, d, len) == -1)
237 d48ffc66 2023-08-31 op return (-1);
238 d48ffc66 2023-08-31 op
239 d48ffc66 2023-08-31 op if (bio->chunked)
240 d48ffc66 2023-08-31 op return bufio_append(bio, "\r\n", 2);
241 d48ffc66 2023-08-31 op
242 d48ffc66 2023-08-31 op return (0);
243 d48ffc66 2023-08-31 op }
244 d48ffc66 2023-08-31 op
245 d48ffc66 2023-08-31 op int
246 b8a11905 2023-08-31 op bufio_compose_str(struct bufio *bio, const char *str)
247 b8a11905 2023-08-31 op {
248 b8a11905 2023-08-31 op return (bufio_compose(bio, str, strlen(str)));
249 b8a11905 2023-08-31 op }
250 b8a11905 2023-08-31 op
251 b8a11905 2023-08-31 op int
252 b8a11905 2023-08-31 op bufio_compose_fmt(struct bufio *bio, const char *fmt, ...)
253 b8a11905 2023-08-31 op {
254 b8a11905 2023-08-31 op va_list ap;
255 b8a11905 2023-08-31 op char *str;
256 b8a11905 2023-08-31 op int r;
257 b8a11905 2023-08-31 op
258 b8a11905 2023-08-31 op va_start(ap, fmt);
259 b8a11905 2023-08-31 op r = vasprintf(&str, fmt, ap);
260 b8a11905 2023-08-31 op va_end(ap);
261 b8a11905 2023-08-31 op
262 b8a11905 2023-08-31 op if (r == -1)
263 b8a11905 2023-08-31 op return (-1);
264 b8a11905 2023-08-31 op r = bufio_compose(bio, str, r);
265 b8a11905 2023-08-31 op free(str);
266 b8a11905 2023-08-31 op return (r);
267 b8a11905 2023-08-31 op }