1/*	$OpenBSD: iobuf.h,v 1.1.1.1 2018/04/27 16:14:36 eric Exp $	*/
2
3/*
4 * Copyright (c) 2017 Eric Faurot <eric@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19struct ioqbuf {
20	struct ioqbuf	*next;
21	char		*buf;
22	size_t		 size;
23	size_t		 wpos;
24	size_t		 rpos;
25};
26
27struct iobuf {
28	char		*buf;
29	size_t		 max;
30	size_t		 size;
31	size_t		 wpos;
32	size_t		 rpos;
33
34	size_t		 queued;
35	struct ioqbuf	*outq;
36	struct ioqbuf	*outqlast;
37};
38
39#define IOBUF_WANT_READ		-1
40#define IOBUF_WANT_WRITE	-2
41#define IOBUF_CLOSED		-3
42#define IOBUF_ERROR		-4
43#define IOBUF_SSLERROR		-5
44
45int	iobuf_init(struct iobuf *, size_t, size_t);
46void	iobuf_clear(struct iobuf *);
47
48int	iobuf_extend(struct iobuf *, size_t);
49void	iobuf_normalize(struct iobuf *);
50void	iobuf_drop(struct iobuf *, size_t);
51size_t	iobuf_space(struct iobuf *);
52size_t	iobuf_len(struct iobuf *);
53size_t	iobuf_left(struct iobuf *);
54char   *iobuf_data(struct iobuf *);
55char   *iobuf_getline(struct iobuf *, size_t *);
56ssize_t	iobuf_read(struct iobuf *, int);
57ssize_t	iobuf_read_ssl(struct iobuf *, void *);
58
59size_t  iobuf_queued(struct iobuf *);
60void*   iobuf_reserve(struct iobuf *, size_t);
61int	iobuf_queue(struct iobuf *, const void*, size_t);
62int	iobuf_queuev(struct iobuf *, const struct iovec *, int);
63int	iobuf_fqueue(struct iobuf *, const char *, ...);
64int	iobuf_vfqueue(struct iobuf *, const char *, va_list);
65int	iobuf_flush(struct iobuf *, int);
66int	iobuf_flush_ssl(struct iobuf *, void *);
67ssize_t	iobuf_write(struct iobuf *, int);
68ssize_t	iobuf_write_ssl(struct iobuf *, void *);
69