1eda27f95SNick Mathewson /*
2e49e2891SNick Mathewson  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
3eda27f95SNick Mathewson  *
4eda27f95SNick Mathewson  * Redistribution and use in source and binary forms, with or without
5eda27f95SNick Mathewson  * modification, are permitted provided that the following conditions
6eda27f95SNick Mathewson  * are met:
7eda27f95SNick Mathewson  * 1. Redistributions of source code must retain the above copyright
8eda27f95SNick Mathewson  *    notice, this list of conditions and the following disclaimer.
9eda27f95SNick Mathewson  * 2. Redistributions in binary form must reproduce the above copyright
10eda27f95SNick Mathewson  *    notice, this list of conditions and the following disclaimer in the
11eda27f95SNick Mathewson  *    documentation and/or other materials provided with the distribution.
12eda27f95SNick Mathewson  * 3. The name of the author may not be used to endorse or promote products
13eda27f95SNick Mathewson  *    derived from this software without specific prior written permission.
14eda27f95SNick Mathewson  *
15eda27f95SNick Mathewson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16eda27f95SNick Mathewson  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17eda27f95SNick Mathewson  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18eda27f95SNick Mathewson  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19eda27f95SNick Mathewson  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20eda27f95SNick Mathewson  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21eda27f95SNick Mathewson  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22eda27f95SNick Mathewson  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23eda27f95SNick Mathewson  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24eda27f95SNick Mathewson  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25eda27f95SNick Mathewson  */
26ec2f4cbcSNick Mathewson 
273f8c7cd0SNick Mathewson #ifndef EVENT2_BUFFER_COMPAT_H_INCLUDED_
283f8c7cd0SNick Mathewson #define EVENT2_BUFFER_COMPAT_H_INCLUDED_
29ec2f4cbcSNick Mathewson 
304545fa9bSTrond Norbye #include <event2/visibility.h>
314545fa9bSTrond Norbye 
322888faccSNick Mathewson /** @file event2/buffer_compat.h
33ec2f4cbcSNick Mathewson 
34ec2f4cbcSNick Mathewson 	Obsolete and deprecated versions of the functions in buffer.h: provided
35ec2f4cbcSNick Mathewson 	only for backward compatibility.
36ec2f4cbcSNick Mathewson  */
37ec2f4cbcSNick Mathewson 
38ec2f4cbcSNick Mathewson 
39ec2f4cbcSNick Mathewson /**
40575ff678SAzat Khuzhin    Obsolete alias for evbuffer_readln(buffer, NULL, EVBUFFER_EOL_ANY).
41ec2f4cbcSNick Mathewson 
42bdbd5e0eSNick Mathewson    @deprecated This function is deprecated because its behavior is not correct
43bdbd5e0eSNick Mathewson       for almost any protocol, and also because it's wholly subsumed by
44bdbd5e0eSNick Mathewson       evbuffer_readln().
45bdbd5e0eSNick Mathewson 
46ec2f4cbcSNick Mathewson    @param buffer the evbuffer to read from
47ec2f4cbcSNick Mathewson    @return pointer to a single line, or NULL if an error occurred
48bdbd5e0eSNick Mathewson 
49ec2f4cbcSNick Mathewson */
504545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
51ec2f4cbcSNick Mathewson char *evbuffer_readline(struct evbuffer *buffer);
52ec2f4cbcSNick Mathewson 
53f1b1bad4SNick Mathewson /** Type definition for a callback that is invoked whenever data is added or
54f1b1bad4SNick Mathewson     removed from an evbuffer.
55f1b1bad4SNick Mathewson 
56f1b1bad4SNick Mathewson     An evbuffer may have one or more callbacks set at a time.  The order
570b4ab122SNick Mathewson     in which they are executed is undefined.
58f1b1bad4SNick Mathewson 
59f1b1bad4SNick Mathewson     A callback function may add more callbacks, or remove itself from the
60f1b1bad4SNick Mathewson     list of callbacks, or add or remove data from the buffer.  It may not
61f1b1bad4SNick Mathewson     remove another callback from the list.
62f1b1bad4SNick Mathewson 
63f1b1bad4SNick Mathewson     If a callback adds or removes data from the buffer or from another
64f1b1bad4SNick Mathewson     buffer, this can cause a recursive invocation of your callback or
65f1b1bad4SNick Mathewson     other callbacks.  If you ask for an infinite loop, you might just get
66f1b1bad4SNick Mathewson     one: watch out!
67f1b1bad4SNick Mathewson 
68f1b1bad4SNick Mathewson     @param buffer the buffer whose size has changed
69f1b1bad4SNick Mathewson     @param old_len the previous length of the buffer
70f1b1bad4SNick Mathewson     @param new_len the current length of the buffer
71f1b1bad4SNick Mathewson     @param arg a pointer to user data
72f1b1bad4SNick Mathewson */
73f1b1bad4SNick Mathewson typedef void (*evbuffer_cb)(struct evbuffer *buffer, size_t old_len, size_t new_len, void *arg);
74f1b1bad4SNick Mathewson 
75ec2f4cbcSNick Mathewson /**
76ec2f4cbcSNick Mathewson   Replace all callbacks on an evbuffer with a single new callback, or
77ec2f4cbcSNick Mathewson   remove them.
78ec2f4cbcSNick Mathewson 
79ec2f4cbcSNick Mathewson   Subsequent calls to evbuffer_setcb() replace callbacks set by previous
80ec2f4cbcSNick Mathewson   calls.  Setting the callback to NULL removes any previously set callback.
81ec2f4cbcSNick Mathewson 
82bdbd5e0eSNick Mathewson   @deprecated This function is deprecated because it clears all previous
83bdbd5e0eSNick Mathewson      callbacks set on the evbuffer, which can cause confusing behavior if
84bdbd5e0eSNick Mathewson      multiple parts of the code all want to add their own callbacks on a
85bdbd5e0eSNick Mathewson      buffer.  Instead, use evbuffer_add(), evbuffer_del(), and
86bdbd5e0eSNick Mathewson      evbuffer_setflags() to manage your own evbuffer callbacks without
87bdbd5e0eSNick Mathewson      interfering with callbacks set by others.
88bdbd5e0eSNick Mathewson 
89ec2f4cbcSNick Mathewson   @param buffer the evbuffer to be monitored
90ec2f4cbcSNick Mathewson   @param cb the callback function to invoke when the evbuffer is modified,
91ec2f4cbcSNick Mathewson 	 or NULL to remove all callbacks.
92ec2f4cbcSNick Mathewson   @param cbarg an argument to be provided to the callback function
93*598f247dSAzat Khuzhin   @return 0 if successful, or -1 on error
94ec2f4cbcSNick Mathewson  */
954545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
96*598f247dSAzat Khuzhin int evbuffer_setcb(struct evbuffer *buffer, evbuffer_cb cb, void *cbarg);
97ec2f4cbcSNick Mathewson 
98f90500a5SNick Mathewson 
99f90500a5SNick Mathewson /**
100f90500a5SNick Mathewson   Find a string within an evbuffer.
101f90500a5SNick Mathewson 
102f90500a5SNick Mathewson   @param buffer the evbuffer to be searched
103f90500a5SNick Mathewson   @param what the string to be searched for
104f90500a5SNick Mathewson   @param len the length of the search string
105f90500a5SNick Mathewson   @return a pointer to the beginning of the search string, or NULL if the search failed.
106f90500a5SNick Mathewson  */
1074545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
108f90500a5SNick Mathewson unsigned char *evbuffer_find(struct evbuffer *buffer, const unsigned char *what, size_t len);
109f90500a5SNick Mathewson 
110a8f6d961SNick Mathewson /** deprecated in favor of calling the functions directly */
111a8f6d961SNick Mathewson #define EVBUFFER_LENGTH(x)	evbuffer_get_length(x)
112a8f6d961SNick Mathewson /** deprecated in favor of calling the functions directly */
11307e9e9b4SNick Mathewson #define EVBUFFER_DATA(x)	evbuffer_pullup((x), -1)
114a8f6d961SNick Mathewson 
115ec2f4cbcSNick Mathewson #endif
116ec2f4cbcSNick Mathewson 
117