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