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 1fcd7cb2 2023-08-31 op #define BIO_CHUNK 1024
27 b8a11905 2023-08-31 op struct buffer {
28 b8a11905 2023-08-31 op uint8_t *buf;
29 b8a11905 2023-08-31 op size_t len;
30 b8a11905 2023-08-31 op size_t cap;
31 b8a11905 2023-08-31 op };
32 b8a11905 2023-08-31 op
33 b8a11905 2023-08-31 op struct bufio {
34 b8a11905 2023-08-31 op int fd;
35 d48ffc66 2023-08-31 op int chunked;
36 b8a11905 2023-08-31 op struct buffer wbuf;
37 b8a11905 2023-08-31 op struct buffer rbuf;
38 b8a11905 2023-08-31 op };
39 b8a11905 2023-08-31 op
40 b8a11905 2023-08-31 op int buf_init(struct buffer *);
41 2216d3fb 2023-09-07 op int buf_write(struct buffer *, const void *, size_t);
42 b8a11905 2023-08-31 op int buf_has_line(struct buffer *, const char *);
43 b8a11905 2023-08-31 op void buf_drain(struct buffer *, size_t);
44 b8a11905 2023-08-31 op void buf_drain_line(struct buffer *, const char *);
45 b8a11905 2023-08-31 op void buf_free(struct buffer *);
46 b8a11905 2023-08-31 op
47 b8a11905 2023-08-31 op int bufio_init(struct bufio *);
48 d48ffc66 2023-08-31 op void bufio_free(struct bufio *);
49 b8a11905 2023-08-31 op int bufio_reset(struct bufio *);
50 b8a11905 2023-08-31 op void bufio_set_fd(struct bufio *, int);
51 d48ffc66 2023-08-31 op void bufio_set_chunked(struct bufio *, int);
52 b8a11905 2023-08-31 op short bufio_pollev(struct bufio *);
53 b8a11905 2023-08-31 op ssize_t bufio_read(struct bufio *);
54 d48ffc66 2023-08-31 op size_t bufio_drain(struct bufio *, void *, size_t);
55 b8a11905 2023-08-31 op ssize_t bufio_write(struct bufio *);
56 b8a11905 2023-08-31 op int bufio_compose(struct bufio *, const void *, size_t);
57 b8a11905 2023-08-31 op int bufio_compose_str(struct bufio *, const char *);
58 b8a11905 2023-08-31 op int bufio_compose_fmt(struct bufio *, const char *, ...)
59 b8a11905 2023-08-31 op __attribute__((__format__ (printf, 2, 3)));