UPnPsdk 0.1
Universal Plug and Play +, Software Development Kit
 
Loading...
Searching...
No Matches
membuffer.cpp
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 * Copyright (C) 2021+ GPL 3 and higher by Ingo Höft, <Ingo@Hoeft-online.de>
7 * Redistribution only with this Copyright remark. Last modified: 2025-05-29
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 * - Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 * - Neither name of Intel Corporation nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
29 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 ******************************************************************************/
34// Last compare with pupnp original source file on 2024-02-14, ver 1.14.18
42#include <membuffer.hpp>
43#include <upnp.hpp>
44
45#include <UPnPsdk/synclog.hpp>
46#include <UPnPsdk/port.hpp>
47#include <umock/stdlib.hpp>
48
50#include <cassert>
51#include <cstdlib>
52#include <cstring>
54
55
56char* str_alloc(const char* str, size_t str_len) {
57 char* s;
58
59 s = (char*)malloc(str_len + (size_t)1);
60 if (s == NULL) {
61 return NULL; /* no mem */
62 }
63
64 memcpy(s, str, str_len);
65 s[str_len] = '\0';
66
67 return s;
68}
69
70int memptr_cmp(memptr* m, const char* s) {
71 int cmp;
72
73 cmp = strncmp(m->buf, s, m->length);
74
75 if (cmp == 0 && m->length < strlen(s)) {
76 /* both strings equal for 'm->length' chars */
77 /* if m is shorter than s, then s is greater */
78 return -1;
79 }
80
81 return cmp;
82}
83
84int memptr_cmp_nocase(memptr* m, const char* s) {
85 int cmp;
86
87 cmp = strncasecmp(m->buf, s, m->length);
88 if (cmp == 0 && m->length < strlen(s)) {
89 /* both strings equal for 'm->length' chars */
90 /* if m is shorter than s, then s is greater */
91 return -1;
92 }
93
94 return cmp;
95}
96
97namespace {
106 membuffer* m) {
107 m->buf = NULL;
108 m->length = (size_t)0;
109 m->capacity = (size_t)0;
110}
111
113} // anonymous namespace
114
115int membuffer_set_size(membuffer* m, size_t new_length) {
116 size_t diff;
117 size_t alloc_len;
118 char* temp_buf;
119
120 if (new_length >= m->length) { /* increase length */
121 /* need more mem? */
122 if (new_length <= m->capacity) {
123 return 0; /* have enough mem; done */
124 }
125
126 diff = new_length - m->length;
127 alloc_len = std::max(m->size_inc, diff) + m->capacity;
128 } else { /* decrease length */
129
130 assert(new_length <= m->length);
131
132 /* if diff is 0..m->size_inc, don't free */
133 if ((m->capacity - new_length) <= m->size_inc) {
134 return 0;
135 }
136
137 alloc_len = new_length + m->size_inc;
138 }
139
140 assert(alloc_len >= new_length);
141
142 temp_buf = (char*)umock::stdlib_h.realloc(
143 m->buf, alloc_len + (size_t)1); /*LEAK_FIX_MK */
144
145 /*temp_buf = Realloc( m->buf,m->length, alloc_len + 1 );LEAK_FIX_MK */
146
147 if (temp_buf == NULL) {
148 /* try smaller size */
149 alloc_len = new_length;
150 temp_buf = (char*)umock::stdlib_h.realloc(
151 m->buf, alloc_len + (size_t)1); /*LEAK_FIX_MK */
152 /*temp_buf = Realloc( m->buf,m->length, alloc_len + 1
153 * );LEAK_FIX_MK */
154
155 if (temp_buf == NULL) {
156 return UPNP_E_OUTOF_MEMORY;
157 }
158 }
159 /* save */
160 m->buf = temp_buf;
161 m->capacity = alloc_len;
162 return 0;
163}
164
166 TRACE("Executing membuffer_init()")
167 assert(m != NULL);
168
169 m->size_inc = MEMBUF_DEF_SIZE_INC;
171}
172
174 TRACE("Executing membuffer_destroy()")
175 if (m == NULL) {
176 return;
177 }
178
179 if (m->buf != nullptr) {
180 umock::stdlib_h.free(m->buf);
181 }
183}
184
185int membuffer_assign(membuffer* m, const void* buf, size_t buf_len) {
186 int return_code;
187
188 assert(m != NULL);
189
190 /* set value to null */
191 if (buf == NULL) {
193 return 0;
194 }
195 /* alloc mem */
196 return_code = membuffer_set_size(m, buf_len);
197 if (return_code != 0)
198 return return_code;
199 /* copy */
200 if (buf_len) {
201 memcpy(m->buf, buf, buf_len);
202 m->buf[buf_len] = 0; /* null-terminate */
203 }
204 m->length = buf_len;
205
206 return 0;
207}
208
209int membuffer_assign_str(membuffer* m, const char* c_str) {
210 return membuffer_assign(m, c_str, strlen(c_str));
211}
212
213int membuffer_append(membuffer* m, const void* buf, size_t buf_len) {
214 assert(m != NULL);
215
216 return membuffer_insert(m, buf, buf_len, m->length);
217}
218
219int membuffer_append_str(membuffer* m, const char* c_str) {
220 return membuffer_insert(m, c_str, strlen(c_str), m->length);
221}
222
223int membuffer_insert(membuffer* m, const void* buf, size_t buf_len,
224 size_t index) {
225 int return_code;
226
227 assert(m != NULL);
228
229 if (index > m->length)
230 return UPNP_E_OUTOF_BOUNDS;
231 if (!buf || !buf_len) {
232 return 0;
233 }
234 /* alloc mem */
235 return_code = membuffer_set_size(m, m->length + buf_len);
236 if (return_code) {
237 return return_code;
238 }
239 /* insert data */
240 /* move data to right of insertion point */
241 memmove(m->buf + index + buf_len, m->buf + index, m->length - index);
242 memcpy(m->buf + index, buf, buf_len);
243 m->length += buf_len;
244 /* null-terminate */
245 m->buf[m->length] = 0;
246
247 return 0;
248}
249
250void membuffer_delete(membuffer* m, size_t index, size_t num_bytes) {
251 int return_value;
252 size_t new_length;
253 size_t copy_len;
254
255 assert(m != NULL);
256
257 if (!m || !m->length)
258 return;
259 /* shrink count if it goes beyond buffer */
260 if (index + num_bytes > m->length) {
261 num_bytes = m->length - index;
262 /* every thing at and after index purged */
263 copy_len = (size_t)0;
264 } else {
265 /* calc num bytes after deleted string */
266 copy_len = m->length - (index + num_bytes);
267 }
268 memmove(m->buf + index, m->buf + index + num_bytes, copy_len);
269 new_length = m->length - num_bytes;
270 /* trim buffer */
271 return_value = membuffer_set_size(m, new_length);
272 /* shrinking should always work */
273 assert(return_value == 0);
274 if (return_value != 0)
275 return;
276
277 /* don't modify until buffer is set */
278 m->length = new_length;
279 m->buf[new_length] = 0;
280}
281
283 char* buf;
284
285 assert(m != NULL);
286
287 buf = m->buf;
288
289 /* free all */
291
292 return buf;
293}
294
295void membuffer_attach(membuffer* m, char* new_buf, size_t buf_len) {
296 assert(m != NULL);
297
299 m->buf = new_buf;
300 m->length = buf_len;
301 m->capacity = buf_len;
302}
#define UPNP_E_OUTOF_BOUNDS
The operation completed successfully.
Definition API.hpp:355
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.cpp:70
void membuffer_destroy(membuffer *m)
Free's memory allocated for membuffer* m.
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.
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.cpp:56
void membuffer_attach(membuffer *m, char *new_buf, size_t buf_len)
Free existing memory in membuffer and assign the new buffer in its place.
void membuffer_init(membuffer *m)
Wrapper to membuffer_initialize().
int membuffer_append(membuffer *m, const void *buf, size_t buf_len)
Invokes function to appends data from a constant buffer to the buffer.
int membuffer_assign_str(membuffer *m, const char *c_str)
Wrapper function for membuffer_assign().
char * membuffer_detach(membuffer *m)
Detaches current buffer and returns it. The caller must free the returned buffer using free()....
int membuffer_append_str(membuffer *m, const char *c_str)
Invokes function to appends data from a constant string to the buffer.
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.cpp:84
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 ...
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.
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 buffer and the input parameters....
Manage blocks of dynamically allocated memory.
size_t size_inc
used to increase size; MUST be > 0; (read/write).
Definition membuffer.hpp:69
size_t capacity
total allocated memory without terminating null byte (read-only).
Definition membuffer.hpp:67
size_t length
length of buffer without terminating null byte (read-only).
Definition membuffer.hpp:65
char * buf
mem buffer; must not write beyond buf[length-1] (read/write).
Definition membuffer.hpp:63
size_t length
length of memory (read-only).
Definition membuffer.hpp:54
char * buf
start of memory (read/write).
Definition membuffer.hpp:52
pointer to a chunk of memory.
Definition membuffer.hpp:50
Maintains a block of dynamically allocated memory.
Definition membuffer.hpp:61
#define UPNP_E_OUTOF_MEMORY
Not enough resources are currently available to complete the operation.
Definition messages.hpp:57
void membuffer_initialize(membuffer *m)
Initialize the buffer.
Specifications to be portable between different platforms.
Define macro for synced logging to the console for detailed info and debug.