libUPnP  1.14.18
membuffer.h
Go to the documentation of this file.
1 /*******************************************************************************
2  *
3  * Copyright (c) 2000-2003 Intel Corporation
4  * All rights reserved.
5  * Copyright (c) 2012 France Telecom All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * - Redistributions of source code must retain the above copyright notice,
11  * this list of conditions and the following disclaimer.
12  * - Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  * - Neither name of Intel Corporation nor the names of its contributors
16  * may be used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
27  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  ******************************************************************************/
32 
33 #ifndef GENLIB_UTIL_MEMBUFFER_H
34 #define GENLIB_UTIL_MEMBUFFER_H
35 
40 #include "upnputil.h"
41 #include <stdlib.h>
42 
43 #define MINVAL(a, b) ((a) < (b) ? (a) : (b))
44 #define MAXVAL(a, b) ((a) > (b) ? (a) : (b))
45 
47 typedef struct
48 {
50  char *buf;
52  size_t length;
53 } memptr;
54 
57 typedef struct
58 {
60  char *buf;
62  size_t length;
64  size_t capacity;
66  size_t size_inc;
68 #define MEMBUF_DEF_SIZE_INC (size_t)5
69 } membuffer;
70 
71 #ifdef __cplusplus
72 extern "C" {
73 #endif /* __cplusplus */
74 
82 char *str_alloc(
84  const char *str,
86  size_t str_len);
87 
98 int memptr_cmp(
100  memptr *m,
102  const char *s);
103 
118  memptr *m,
120  const char *s);
121 
132  membuffer *m,
134  size_t new_length);
135 
142 void membuffer_init(
144  membuffer *m);
145 
149 void membuffer_destroy(
151  membuffer *m);
152 
161 int membuffer_assign(
163  membuffer *m,
165  const void *buf,
167  size_t buf_len);
168 
178  membuffer *m,
180  const char *c_str);
181 
187 int membuffer_append(
189  membuffer *m,
191  const void *buf,
193  size_t buf_len);
194 
202  membuffer *m,
204  const char *c_str);
205 
213 int membuffer_insert(
216  membuffer *m,
218  const void *buf,
220  size_t buf_len,
222  size_t index);
223 
229 void membuffer_delete(
232  membuffer *m,
234  size_t index,
236  size_t num_bytes);
237 
238 /*
239  * \brief Detaches current buffer and returns it. The caller must free the
240  * returned buffer using free(). After this call, length becomes 0.
241  *
242  * \return A pointer to the current buffer.
243  */
244 char *membuffer_detach(
246  membuffer *m);
247 
248 /*
249  * \brief Free existing memory in membuffer and assign the new buffer in its
250  * place.
251  *
252  * \note 'new_buf' must be allocted using malloc or realloc so that it can be
253  * freed using free().
254  */
255 void membuffer_attach(
257  membuffer *m,
260  char *new_buf,
262  size_t buf_len);
263 
264 #ifdef __cplusplus
265 } /* extern "C" */
266 #endif /* __cplusplus */
267 
268 #endif /* GENLIB_UTIL_MEMBUFFER_H */
int memptr_cmp(memptr *m, const char *s)
Compares characters of strings passed for number of bytes. If equal for the number of bytes...
Definition: membuffer.c:66
void membuffer_destroy(membuffer *m)
Free's memory allocated for membuffer* m.
Definition: membuffer.c:165
void membuffer_attach(membuffer *m, char *new_buf, size_t buf_len)
Definition: membuffer.c:292
void membuffer_init(membuffer *m)
Wrapper to membuffer_initialize().
Definition: membuffer.c:157
char * str_alloc(const char *str, size_t str_len)
Allocate memory and copy information from the input string to the newly allocated memory...
Definition: membuffer.c:51
int memptr_cmp_nocase(memptr *m, const char *s)
Compares characters of 2 strings irrespective of the case for a specific count of bytes...
Definition: membuffer.c:81
size_t capacity
Definition: membuffer.h:64
int membuffer_set_size(membuffer *m, size_t new_length)
Increases or decreases buffer cap so that at least 'new_length' bytes can be stored.
Definition: membuffer.c:107
int membuffer_assign_str(membuffer *m, const char *c_str)
Wrapper function for membuffer_assign().
Definition: membuffer.c:200
int membuffer_append_str(membuffer *m, const char *c_str)
Invokes function to appends data from a constant string to the buffer.
Definition: membuffer.c:212
char * buf
Definition: membuffer.h:60
char * buf
Definition: membuffer.h:50
size_t length
Definition: membuffer.h:62
size_t size_inc
Definition: membuffer.h:66
size_t length
Definition: membuffer.h:52
int membuffer_insert(membuffer *m, const void *buf, size_t buf_len, size_t index)
Allocates memory for the new data to be inserted. Does memory management by moving the data from the ...
Definition: membuffer.c:217
char * membuffer_detach(membuffer *m)
Definition: membuffer.c:278
int membuffer_append(membuffer *m, const void *buf, size_t buf_len)
Invokes function to appends data from a constant buffer to the buffer.
Definition: membuffer.c:205
Definition: membuffer.h:57
void membuffer_delete(membuffer *m, size_t index, size_t num_bytes)
Shrink the size of the buffer depending on the current size of the bufer and te input parameters...
Definition: membuffer.c:245
Definition: membuffer.h:47
int membuffer_assign(membuffer *m, const void *buf, size_t buf_len)
Allocate memory to membuffer *m and copy the contents of the in parameter const void *buf...
Definition: membuffer.c:175