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