libUPnP  1.8.1
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 <stdlib.h>
41 #include "upnputil.h"
42 
43 #define MINVAL( a, b ) ( (a) < (b) ? (a) : (b) )
44 #define MAXVAL( a, b ) ( (a) > (b) ? (a) : (b) )
45 
47 typedef struct {
49  char *buf;
51  size_t length;
52 } memptr;
53 
56 typedef struct {
58  char *buf;
60  size_t length;
62  size_t capacity;
64  size_t size_inc;
66 #define MEMBUF_DEF_SIZE_INC (size_t)5
67 } membuffer;
68 
69 #ifdef __cplusplus
70 extern "C" {
71 #endif /* __cplusplus */
72 
80 char *str_alloc(
82  const char *str,
84  size_t str_len);
85 
96 int memptr_cmp(
98  memptr *m,
100  const char *s);
101 
116  memptr *m,
118  const char *s);
119 
130  membuffer *m,
132  size_t new_length);
133 
140 void membuffer_init(
142  membuffer *m);
143 
147 void membuffer_destroy(
149  membuffer *m);
150 
159 int membuffer_assign(
161  membuffer *m,
163  const void *buf,
165  size_t buf_len);
166 
176  membuffer *m,
178  const char *c_str);
179 
185 int membuffer_append(
187  membuffer *m,
189  const void *buf,
191  size_t buf_len);
192 
200  membuffer *m,
202  const char *c_str);
203 
211 int membuffer_insert(
213  membuffer * m,
215  const void *buf,
217  size_t buf_len,
219  size_t index);
220 
226 void membuffer_delete(
229  membuffer * m,
231  size_t index,
233  size_t num_bytes);
234 
235 /*
236  * \brief Detaches current buffer and returns it. The caller must free the
237  * returned buffer using free(). After this call, length becomes 0.
238  *
239  * \return A pointer to the current buffer.
240  */
241 char *membuffer_detach(
243  membuffer *m);
244 
245 /*
246  * \brief Free existing memory in membuffer and assign the new buffer in its
247  * place.
248  *
249  * \note 'new_buf' must be allocted using malloc or realloc so that it can be
250  * freed using free().
251  */
252 void membuffer_attach(
254  membuffer *m,
257  char *new_buf,
259  size_t buf_len);
260 
261 #ifdef __cplusplus
262 } /* extern "C" */
263 #endif /* __cplusplus */
264 
265 #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:63
void membuffer_destroy(membuffer *m)
Free&#39;s memory allocated for membuffer* m.
Definition: membuffer.c:160
void membuffer_attach(membuffer *m, char *new_buf, size_t buf_len)
Definition: membuffer.c:287
void membuffer_init(membuffer *m)
Wrapper to membuffer_initialize().
Definition: membuffer.c:152
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:48
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:78
size_t capacity
Definition: membuffer.h:62
int membuffer_set_size(membuffer *m, size_t new_length)
Increases or decreases buffer cap so that at least &#39;new_length&#39; bytes can be stored.
Definition: membuffer.c:104
int membuffer_assign_str(membuffer *m, const char *c_str)
Wrapper function for membuffer_assign().
Definition: membuffer.c:195
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:207
char * buf
Definition: membuffer.h:58
char * buf
Definition: membuffer.h:49
size_t length
Definition: membuffer.h:60
size_t size_inc
Definition: membuffer.h:64
size_t length
Definition: membuffer.h:51
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:212
char * membuffer_detach(membuffer *m)
Definition: membuffer.c:273
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:200
Definition: membuffer.h:56
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:240
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:170