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 6be81433 2024-04-14 op #ifndef BUFIO_WITHOUT_TLS
27 6be81433 2024-04-14 op struct tls;
28 6be81433 2024-04-14 op #endif
29 6be81433 2024-04-14 op
30 6be81433 2024-04-14 op #define BIO_CHUNK 128
31 6be81433 2024-04-14 op struct buf {
32 b8a11905 2023-08-31 op uint8_t *buf;
33 b8a11905 2023-08-31 op size_t len;
34 b8a11905 2023-08-31 op size_t cap;
35 6be81433 2024-04-14 op size_t cur;
36 b8a11905 2023-08-31 op };
37 b8a11905 2023-08-31 op
38 b8a11905 2023-08-31 op struct bufio {
39 b8a11905 2023-08-31 op int fd;
40 d48ffc66 2023-08-31 op int chunked;
41 6be81433 2024-04-14 op #ifndef BUFIO_WITHOUT_TLS
42 6be81433 2024-04-14 op struct tls *ctx;
43 6be81433 2024-04-14 op #endif
44 6be81433 2024-04-14 op int wantev;
45 6be81433 2024-04-14 op struct buf wbuf;
46 6be81433 2024-04-14 op struct buf rbuf;
47 b8a11905 2023-08-31 op };
48 b8a11905 2023-08-31 op
49 6be81433 2024-04-14 op #define BUFIO_WANT_READ 0x1
50 6be81433 2024-04-14 op #define BUFIO_WANT_WRITE 0x2
51 b8a11905 2023-08-31 op
52 6be81433 2024-04-14 op int buf_init(struct buf *);
53 6be81433 2024-04-14 op int buf_write(struct buf *, const void *, size_t);
54 6be81433 2024-04-14 op int buf_has_line(struct buf *, const char *);
55 6be81433 2024-04-14 op char *buf_getdelim(struct buf *, const char *, size_t *);
56 6be81433 2024-04-14 op void buf_drain(struct buf *, size_t);
57 6be81433 2024-04-14 op void buf_drain_line(struct buf *, const char *);
58 6be81433 2024-04-14 op void buf_free(struct buf *);
59 6be81433 2024-04-14 op
60 b8a11905 2023-08-31 op int bufio_init(struct bufio *);
61 d48ffc66 2023-08-31 op void bufio_free(struct bufio *);
62 6be81433 2024-04-14 op int bufio_close(struct bufio *);
63 b8a11905 2023-08-31 op int bufio_reset(struct bufio *);
64 b8a11905 2023-08-31 op void bufio_set_fd(struct bufio *, int);
65 d48ffc66 2023-08-31 op void bufio_set_chunked(struct bufio *, int);
66 6be81433 2024-04-14 op int bufio_starttls(struct bufio *, const char *, int,
67 6be81433 2024-04-14 op const uint8_t *, size_t, const uint8_t *, size_t);
68 6be81433 2024-04-14 op int bufio_ev(struct bufio *);
69 6be81433 2024-04-14 op int bufio_handshake(struct bufio *);
70 b8a11905 2023-08-31 op ssize_t bufio_read(struct bufio *);
71 d48ffc66 2023-08-31 op size_t bufio_drain(struct bufio *, void *, size_t);
72 b8a11905 2023-08-31 op ssize_t bufio_write(struct bufio *);
73 b8a11905 2023-08-31 op int bufio_compose(struct bufio *, const void *, size_t);
74 b8a11905 2023-08-31 op int bufio_compose_str(struct bufio *, const char *);
75 b8a11905 2023-08-31 op int bufio_compose_fmt(struct bufio *, const char *, ...)
76 b8a11905 2023-08-31 op __attribute__((__format__ (printf, 2, 3)));
77 6be81433 2024-04-14 op void bufio_rewind_cursor(struct bufio *);
78 6be81433 2024-04-14 op
79 6be81433 2024-04-14 op /* callbacks for pdjson */
80 6be81433 2024-04-14 op int bufio_get_cb(void *);
81 6be81433 2024-04-14 op int bufio_peek_cb(void *);