1.\" $OpenBSD: evbuffer_new.3,v 1.4 2014/11/19 23:55:54 dlg Exp $ 2.\" 3.\" Copyright (c) 2014 David Gwynne <dlg@openbsd.org> 4.\" 5.\" Permission to use, copy, modify, and distribute this software for any 6.\" purpose with or without fee is hereby granted, provided that the above 7.\" copyright notice and this permission notice appear in all copies. 8.\" 9.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16.\" 17.Dd $Mdocdate: November 19 2014 $ 18.Dt EVBUFFER_NEW 3 19.Os 20.Sh NAME 21.Nm evbuffer_new , 22.Nm evbuffer_free , 23.Nm evbuffer_setcb , 24.Nm evbuffer_add , 25.Nm evbuffer_add_buffer , 26.Nm evbuffer_add_printf , 27.Nm evbuffer_add_vprintf , 28.Nm evbuffer_drain , 29.Nm evbuffer_remove , 30.Nm evbuffer_write , 31.Nm evbuffer_read , 32.Nm evbuffer_find , 33.Nm evbuffer_readline , 34.Nm evbuffer_readln 35.Nd libevent utility API for buffered input/output 36.Sh SYNOPSIS 37.In event.h 38.Ft "struct evbuffer *" 39.Fn "evbuffer_new" "void" 40.Ft void 41.Fn "evbuffer_free" "struct evbuffer *buf" 42.Ft void 43.Fo evbuffer_setcb 44.Fa "struct evbuffer *buf" 45.Fa "void (*cb)(struct evbuffer *, size_t, size_t, void *)" 46.Fa "void *cbarg" 47.Fc 48.Ft int 49.Fn "evbuffer_expand" "struct evbuffer *buf" "size_t datlen" 50.Ft int 51.Fn "evbuffer_add" "struct evbuffer *buf" "const void *data" "size_t size" 52.Ft int 53.Fn "evbuffer_add_buffer" "struct evbuffer *dst" "struct evbuffer *src" 54.Ft int 55.Fn "evbuffer_add_printf" "struct evbuffer *buf" "const char *fmt" "..." 56.Ft int 57.Fn "evbuffer_add_vprintf" "struct evbuffer *buf" "const char *fmt" "va_list ap" 58.Ft void 59.Fn "evbuffer_drain" "struct evbuffer *buf" "size_t size" 60.Ft int 61.Fn "evbuffer_remove" "struct evbuffer *buf" "void *data" "size_t datlen" 62.Ft int 63.Fn "evbuffer_write" "struct evbuffer *buf" "int fd" 64.Ft int 65.Fn "evbuffer_read" "struct evbuffer *buf" "int fd" "int size" 66.Ft "u_char *" 67.Fn "evbuffer_find" "struct evbuffer *buf" "const u_char *data" "size_t size" 68.Ft "char *" 69.Fn "evbuffer_readline" "struct evbuffer *buf" 70.Ft "char *" 71.Fo evbuffer_readln 72.Fa "struct evbuffer *buf" 73.Fa "size_t *read_out" 74.Fa "enum evbuffer_eol_style eol_style" 75.Fc 76.Sh DESCRIPTION 77The evbuffer API provides an implementation of buffering for use with 78libevent. 79.Pp 80.Fn evbuffer_new 81allocates and initialises a new evbuffer structure. 82.Pp 83.Fn evbuffer_free 84deallocates the evbuffer structure 85.Fa buf 86and any referenced storage. 87.Pp 88.Fn evbuffer_setcb 89sets the callback 90.Fa cb 91to be invoked with argument 92.Fa cbarg 93when the data in evbuffer 94.Fa buf 95is modified. 96.Pp 97.Fn evbuffer_expand 98expands the available space in 99.Fa buf 100to at least 101.Fa datlen 102bytes. 103.Pp 104.Fn evbuffer_add 105appends a copy of 106.Fa size 107bytes from buffer 108.Fa data 109to the end of the evbuffer 110.Fa buf . 111.Pp 112.Fn evbuffer_add_buffer 113moves the data off the 114.Fa src 115evbuffer and appends it to 116.Fa dst . 117.Pp 118.Fn evbuffer_add_printf 119appends a 120.Xr printf 3 121style formatted string specified by 122.Fa fmt 123to the end of 124.Fa buf . 125.Pp 126.Fn evbuffer_add_vprintf 127appends a 128.Xr vprintf 3 129style formatted string specified by 130.Fa fmt 131with a va_list 132.Fa ap 133to the end of 134.Fa buf . 135.Pp 136.Fn evbuffer_drain 137deletes 138.Fa size 139bytes from the beginning of the evbuffer 140.Fa buf . 141.Pp 142.Fn evbuffer_remove 143reads and drains up to 144.Fa datlen 145bytes from the beginning of the evbuffer 146.Fa buf 147into 148.Fa data . 149.Pp 150.Fn evbuffer_write 151writes and drains the contents of evbuffer 152.Fa buf 153to the file descriptor 154.Fa fd . 155.Pp 156.Fn evbuffer_read 157appends up to 158.Fa size 159bytes on to the end of the evbuffer 160.Fa buf 161by reading from the file descriptor 162.Fa fd . 163.Pp 164.Fn evbuffer_find 165finds the 166.Fa size 167length string 168.Fa data 169in the evbuffer 170.Fa buf . 171.Pp 172.Fn evbuffer_readline 173reads and drains a single line from the evbuffer 174.Fa buf . 175A line is delimited by "\\n", "\\r", "\\r\\n", or "\\n\\r". 176It is the responsibility of the caller to free the returned line with 177.Xr free 3 . 178.Pp 179.Fn evbuffer_readln 180reads and drains a single line from the evbuffer 181.Fa buf . 182The length of the line will be stored in 183.Fa read_out 184on success. 185It is the responsibility of the caller to free the returned line with 186.Xr free 3 . 187The line delimiter is specified as one of the following: 188.Bl -tag -width xxx -offset indent 189.It Dv EVBUFFER_EOL_ANY 190Any sequence of newline or carriage return characters. 191.It Dv EVBUFFER_EOL_CRLF 192A new line optionally preceded by a carriage return. 193.It Dv EVBUFFER_EOL_CRLF_STRICT 194A carriage return followed by a new line character. 195.It Dv EVBUFFER_EOL_LF 196A new line character. 197.El 198.Sh RETURN VALUES 199.Fn evbuffer_new 200returns a pointer to a newly allocated buffer On success, 201or 202.Dv NULL 203on failure and sets errno to indicate the failure. 204.Pp 205.Fn evbuffer_expand , 206.Fn evbuffer_add , 207and 208.Fn evbuffer_add_buffer 209return 0 on success, 210or -1 on failure and set errno to indicate the failure. 211.Pp 212.Fn evbuffer_add_printf , 213and 214.Fn evbuffer_add_vprintf 215returns the number of bytes added on success, 216or -1 on failure. 217.Pp 218.Fn evbuffer_remove 219returns the number of bytes read. 220.Pp 221.Fn evbuffer_write 222returns the number of bytes written and drained on success, 223or -1 on failure and sets errno to indicate the failure. 224.Pp 225.Fn evbuffer_read 226returns the number of bytes appended to the evbuffer on success, 227or -1 on failure and sets errno to indicate the failure. 228.Pp 229.Fn evbuffer_find 230returns a pointer to the start of the string within the evbuffer on success, 231or 232.Dv NULL 233on failure. 234.Pp 235.Fn evbuffer_readline , 236and 237.Fn evbuffer_readln 238return a pointer to the line on success, 239or 240.Dv NULL 241on failure. 242.Sh SEE ALSO 243.Xr errno 2 , 244.Xr event 3 , 245.Xr free 3 , 246.Xr printf 3 247.Sh AUTHORS 248The 249.Nm event 250library was written by 251.An Niels Provos . 252