1/* 2 * evbuffer imported from libevent 1.4.13 (event.h) 3 * Adapted for forked-daapd 4 * 5 * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30#ifndef __EVBUFFER_H__ 31#define __EVBUFFER_H__ 32 33#include <sys/types.h> 34#include <stdint.h> 35#include <stdarg.h> 36 37struct evbuffer { 38 u_char *buffer; 39 u_char *orig_buffer; 40 41 size_t misalign; 42 size_t totallen; 43 size_t off; 44 45 void (*cb)(struct evbuffer *, size_t, size_t, void *); 46 void *cbarg; 47}; 48 49 50#define EVBUFFER_LENGTH(x) (x)->off 51#define EVBUFFER_DATA(x) (x)->buffer 52#define EVBUFFER_INPUT(x) (x)->input 53#define EVBUFFER_OUTPUT(x) (x)->output 54 55 56/** 57 Allocate storage for a new evbuffer. 58 59 @return a pointer to a newly allocated evbuffer struct, or NULL if an error 60 occurred 61 */ 62struct evbuffer *evbuffer_new(void); 63 64 65/** 66 Deallocate storage for an evbuffer. 67 68 @param pointer to the evbuffer to be freed 69 */ 70void evbuffer_free(struct evbuffer *); 71 72 73/** 74 Expands the available space in an event buffer. 75 76 Expands the available space in the event buffer to at least datlen 77 78 @param buf the event buffer to be expanded 79 @param datlen the new minimum length requirement 80 @return 0 if successful, or -1 if an error occurred 81*/ 82int evbuffer_expand(struct evbuffer *, size_t); 83 84 85/** 86 Append data to the end of an evbuffer. 87 88 @param buf the event buffer to be appended to 89 @param data pointer to the beginning of the data buffer 90 @param datlen the number of bytes to be copied from the data buffer 91 */ 92int evbuffer_add(struct evbuffer *, const void *, size_t); 93 94 95 96/** 97 Read data from an event buffer and drain the bytes read. 98 99 @param buf the event buffer to be read from 100 @param data the destination buffer to store the result 101 @param datlen the maximum size of the destination buffer 102 @return the number of bytes read 103 */ 104int evbuffer_remove(struct evbuffer *, void *, size_t); 105 106 107/** 108 * Read a single line from an event buffer. 109 * 110 * Reads a line terminated by either '\r\n', '\n\r' or '\r' or '\n'. 111 * The returned buffer needs to be freed by the caller. 112 * 113 * @param buffer the evbuffer to read from 114 * @return pointer to a single line, or NULL if an error occurred 115 */ 116char *evbuffer_readline(struct evbuffer *); 117 118 119/** 120 Move data from one evbuffer into another evbuffer. 121 122 This is a destructive add. The data from one buffer moves into 123 the other buffer. The destination buffer is expanded as needed. 124 125 @param outbuf the output buffer 126 @param inbuf the input buffer 127 @return 0 if successful, or -1 if an error occurred 128 */ 129int evbuffer_add_buffer(struct evbuffer *, struct evbuffer *); 130 131 132/** 133 Append a formatted string to the end of an evbuffer. 134 135 @param buf the evbuffer that will be appended to 136 @param fmt a format string 137 @param ... arguments that will be passed to printf(3) 138 @return The number of bytes added if successful, or -1 if an error occurred. 139 */ 140int evbuffer_add_printf(struct evbuffer *, const char *fmt, ...) 141#ifdef __GNUC__ 142 __attribute__((format(printf, 2, 3))) 143#endif 144; 145 146 147/** 148 Append a va_list formatted string to the end of an evbuffer. 149 150 @param buf the evbuffer that will be appended to 151 @param fmt a format string 152 @param ap a varargs va_list argument array that will be passed to vprintf(3) 153 @return The number of bytes added if successful, or -1 if an error occurred. 154 */ 155int evbuffer_add_vprintf(struct evbuffer *, const char *fmt, va_list ap); 156 157 158/** 159 Remove a specified number of bytes data from the beginning of an evbuffer. 160 161 @param buf the evbuffer to be drained 162 @param len the number of bytes to drain from the beginning of the buffer 163 */ 164void evbuffer_drain(struct evbuffer *, size_t); 165 166 167/** 168 Write the contents of an evbuffer to a file descriptor. 169 170 The evbuffer will be drained after the bytes have been successfully written. 171 172 @param buffer the evbuffer to be written and drained 173 @param fd the file descriptor to be written to 174 @return the number of bytes written, or -1 if an error occurred 175 @see evbuffer_read() 176 */ 177int evbuffer_write(struct evbuffer *, int); 178 179 180/** 181 Read from a file descriptor and store the result in an evbuffer. 182 183 @param buf the evbuffer to store the result 184 @param fd the file descriptor to read from 185 @param howmuch the number of bytes to be read 186 @return the number of bytes read, or -1 if an error occurred 187 @see evbuffer_write() 188 */ 189int evbuffer_read(struct evbuffer *, int, int); 190 191 192/** 193 Find a string within an evbuffer. 194 195 @param buffer the evbuffer to be searched 196 @param what the string to be searched for 197 @param len the length of the search string 198 @return a pointer to the beginning of the search string, or NULL if the search failed. 199 */ 200u_char *evbuffer_find(struct evbuffer *, const u_char *, size_t); 201 202/** 203 Set a callback to invoke when the evbuffer is modified. 204 205 @param buffer the evbuffer to be monitored 206 @param cb the callback function to invoke when the evbuffer is modified 207 @param cbarg an argument to be provided to the callback function 208 */ 209void evbuffer_setcb(struct evbuffer *, void (*)(struct evbuffer *, size_t, size_t, void *), void *); 210 211#endif /* __EVBUFFER_H__ */