Blame


1 94a3f4e9 2024-03-30 thomas /*
2 2403c80c 2024-03-30 thomas * bufio.h was written by Omar Polo <op@omarpolo.com>
3 2403c80c 2024-03-30 thomas *
4 94a3f4e9 2024-03-30 thomas * This is free and unencumbered software released into the public domain.
5 94a3f4e9 2024-03-30 thomas *
6 94a3f4e9 2024-03-30 thomas * Anyone is free to copy, modify, publish, use, compile, sell, or
7 94a3f4e9 2024-03-30 thomas * distribute this software, either in source code form or as a compiled
8 94a3f4e9 2024-03-30 thomas * binary, for any purpose, commercial or non-commercial, and by any
9 94a3f4e9 2024-03-30 thomas * means.
10 94a3f4e9 2024-03-30 thomas *
11 94a3f4e9 2024-03-30 thomas * In jurisdictions that recognize copyright laws, the author or authors
12 94a3f4e9 2024-03-30 thomas * of this software dedicate any and all copyright interest in the
13 94a3f4e9 2024-03-30 thomas * software to the public domain. We make this dedication for the benefit
14 94a3f4e9 2024-03-30 thomas * of the public at large and to the detriment of our heirs and
15 94a3f4e9 2024-03-30 thomas * successors. We intend this dedication to be an overt act of
16 94a3f4e9 2024-03-30 thomas * relinquishment in perpetuity of all present and future rights to this
17 94a3f4e9 2024-03-30 thomas * software under copyright law.
18 94a3f4e9 2024-03-30 thomas *
19 94a3f4e9 2024-03-30 thomas * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 94a3f4e9 2024-03-30 thomas * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 94a3f4e9 2024-03-30 thomas * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22 94a3f4e9 2024-03-30 thomas * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 94a3f4e9 2024-03-30 thomas * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 94a3f4e9 2024-03-30 thomas * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 94a3f4e9 2024-03-30 thomas * OTHER DEALINGS IN THE SOFTWARE.
26 94a3f4e9 2024-03-30 thomas */
27 94a3f4e9 2024-03-30 thomas
28 94a3f4e9 2024-03-30 thomas struct tls;
29 94a3f4e9 2024-03-30 thomas
30 94a3f4e9 2024-03-30 thomas #define BIO_CHUNK 128
31 94a3f4e9 2024-03-30 thomas struct buf {
32 94a3f4e9 2024-03-30 thomas uint8_t *buf;
33 94a3f4e9 2024-03-30 thomas size_t len;
34 94a3f4e9 2024-03-30 thomas size_t cap;
35 94a3f4e9 2024-03-30 thomas size_t cur;
36 94a3f4e9 2024-03-30 thomas };
37 94a3f4e9 2024-03-30 thomas
38 94a3f4e9 2024-03-30 thomas struct bufio {
39 94a3f4e9 2024-03-30 thomas int fd;
40 94a3f4e9 2024-03-30 thomas struct tls *ctx;
41 94a3f4e9 2024-03-30 thomas int wantev;
42 94a3f4e9 2024-03-30 thomas struct buf wbuf;
43 94a3f4e9 2024-03-30 thomas struct buf rbuf;
44 94a3f4e9 2024-03-30 thomas };
45 94a3f4e9 2024-03-30 thomas
46 94a3f4e9 2024-03-30 thomas #define BUFIO_WANT_READ 0x1
47 94a3f4e9 2024-03-30 thomas #define BUFIO_WANT_WRITE 0x2
48 94a3f4e9 2024-03-30 thomas
49 94a3f4e9 2024-03-30 thomas int buf_init(struct buf *);
50 94a3f4e9 2024-03-30 thomas int buf_has_line(struct buf *, const char *);
51 94a3f4e9 2024-03-30 thomas char *buf_getdelim(struct buf *, const char *, size_t *);
52 94a3f4e9 2024-03-30 thomas void buf_drain(struct buf *, size_t);
53 94a3f4e9 2024-03-30 thomas void buf_drain_line(struct buf *, const char *);
54 94a3f4e9 2024-03-30 thomas void buf_free(struct buf *);
55 94a3f4e9 2024-03-30 thomas
56 94a3f4e9 2024-03-30 thomas int bufio_init(struct bufio *);
57 94a3f4e9 2024-03-30 thomas void bufio_free(struct bufio *);
58 94a3f4e9 2024-03-30 thomas int bufio_close(struct bufio *);
59 94a3f4e9 2024-03-30 thomas int bufio_reset(struct bufio *);
60 94a3f4e9 2024-03-30 thomas void bufio_set_fd(struct bufio *, int);
61 94a3f4e9 2024-03-30 thomas int bufio_starttls(struct bufio *, const char *, int,
62 94a3f4e9 2024-03-30 thomas const uint8_t *, size_t, const uint8_t *, size_t);
63 94a3f4e9 2024-03-30 thomas int bufio_ev(struct bufio *);
64 94a3f4e9 2024-03-30 thomas int bufio_handshake(struct bufio *);
65 94a3f4e9 2024-03-30 thomas ssize_t bufio_read(struct bufio *);
66 94a3f4e9 2024-03-30 thomas size_t bufio_drain(struct bufio *, void *, size_t);
67 94a3f4e9 2024-03-30 thomas ssize_t bufio_write(struct bufio *);
68 94a3f4e9 2024-03-30 thomas const char *bufio_io_err(struct bufio *);
69 94a3f4e9 2024-03-30 thomas int bufio_compose(struct bufio *, const void *, size_t);
70 94a3f4e9 2024-03-30 thomas int bufio_compose_str(struct bufio *, const char *);
71 94a3f4e9 2024-03-30 thomas int bufio_compose_fmt(struct bufio *, const char *, ...)
72 94a3f4e9 2024-03-30 thomas __attribute__((__format__ (printf, 2, 3)));
73 94a3f4e9 2024-03-30 thomas void bufio_rewind_cursor(struct bufio *);
74 94a3f4e9 2024-03-30 thomas
75 94a3f4e9 2024-03-30 thomas /* callbacks for pdjson */
76 94a3f4e9 2024-03-30 thomas int bufio_get_cb(void *);
77 94a3f4e9 2024-03-30 thomas int bufio_peek_cb(void *);