UPnPsdk 0.1
Universal Plug and Play +, Software Development Kit
 
Loading...
Searching...
No Matches
ixmlmembuf.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) 2022 GPL 3 and higher by Ingo Höft, <Ingo@Hoeft-online.de>
7 * Redistribution only with this Copyright remark. Last modified: 2026-03-31
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 source file, based on 2026-03-16, ver 1.14.30
35
40#include <ixml/ixmlmembuf.hpp>
41#include <ixml/ixml.hpp>
42
43#include <cassert>
44// #include <stdlib.h>
45#include <cstring>
46
59 ixml_membuf* m,
61 size_t new_length) {
62 size_t diff;
63 size_t alloc_len;
64 char* temp_buf;
65
66 if (new_length >= m->length) {
67 /* increase length */
68 /* need more mem? */
69 if (new_length <= m->capacity) {
70 /* have enough mem; done */
71 return 0;
72 }
73
74 diff = new_length - m->length;
75 alloc_len = MAXVAL(m->size_inc, diff) + m->capacity;
76 } else {
77 /* decrease length */
78 assert(new_length <= m->length);
79
80 /* if diff is 0..m->size_inc, don't free */
81 if ((m->capacity - new_length) <= m->size_inc) {
82 return 0;
83 }
84 alloc_len = new_length + m->size_inc;
85 }
86
87 assert(alloc_len >= new_length);
88
89 temp_buf = (char*)realloc(m->buf, alloc_len + (size_t)1);
90 if (!temp_buf) {
91 /* try smaller size */
92 alloc_len = new_length;
93 temp_buf = (char*)realloc(m->buf, alloc_len + (size_t)1);
94 if (!temp_buf) {
95 return IXML_INSUFFICIENT_MEMORY;
96 }
97 }
98 /* save */
99 m->buf = temp_buf;
100 m->capacity = alloc_len;
101
102 return 0;
103}
104
106 assert(m != NULL);
107
108 m->size_inc = MEMBUF_DEF_SIZE_INC;
109 m->buf = NULL;
110 m->length = (size_t)0;
111 m->capacity = (size_t)0;
112}
113
115 if (!m) {
116 return;
117 }
118
119 free(m->buf);
121}
122
123int ixml_membuf_assign(ixml_membuf* m, const void* buf, size_t buf_len) {
124 int return_code;
125
126 assert(m != NULL);
127
128 /* set value to null */
129 if (!buf) {
131 return IXML_SUCCESS;
132 }
133 /* alloc mem */
134 return_code = ixml_membuf_set_size(m, buf_len);
135 if (return_code != 0) {
136 return return_code;
137 }
138
139 /* copy */
140 memcpy(m->buf, buf, buf_len);
141
142 /* null-terminate */
143 m->buf[buf_len] = 0;
144 m->length = buf_len;
145
146 return IXML_SUCCESS;
147}
148
149int ixml_membuf_assign_str(ixml_membuf* m, const char* c_str) {
150 return ixml_membuf_assign(m, c_str, strlen(c_str));
151}
152
155 ixml_membuf* m,
157 const void* buf) {
158 assert(m != NULL);
159
160 return ixml_membuf_insert(m, buf, (size_t)1, m->length);
161}
162
165 ixml_membuf* m,
167 const char* c_str) {
168 return ixml_membuf_insert(m, c_str, strlen(c_str), m->length);
169}
170
173 ixml_membuf* m,
175 const void* buf,
177 size_t buf_len,
179 size_t index) {
180 int return_code = 0;
181
182 assert(m != NULL);
183
184 if (index > m->length) {
185 return IXML_INDEX_SIZE_ERR;
186 }
187
188 if (!buf || buf_len == (size_t)0) {
189 return 0;
190 }
191 /* alloc mem */
192 return_code = ixml_membuf_set_size(m, m->length + buf_len);
193 if (return_code != 0) {
194 return return_code;
195 }
196 /* insert data */
197 /* move data to right of insertion point */
198 memmove(m->buf + index + buf_len, m->buf + index, m->length - index);
199 memcpy(m->buf + index, buf, buf_len);
200 m->length += buf_len;
201 /* Null terminate */
202 m->buf[m->length] = 0;
203
204 return 0;
205}
void ixml_membuf_destroy(ixml_membuf *m)
ixml_membuf clearing routine.
void ixml_membuf_init(ixml_membuf *m)
ixml_membuf initialization routine.
static int ixml_membuf_set_size(ixml_membuf *m, size_t new_length)
Increases or decreases buffer capacity so that at least 'new_length' bytes can be stored.
int ixml_membuf_assign_str(ixml_membuf *m, const char *c_str)
Copies a NULL terminated string to the ixml_buffer.
int ixml_membuf_append(ixml_membuf *m, const void *buf)
Appends one byte to the designated ixml_membuffer.
int ixml_membuf_append_str(ixml_membuf *m, const char *c_str)
Appends the contents of a NULL terminated string to the designated ixml_membuf.
int ixml_membuf_assign(ixml_membuf *m, const void *buf, size_t buf_len)
Copies the contents o a buffer to the designated ixml_membuf.
int ixml_membuf_insert(ixml_membuf *m, const void *buf, size_t buf_len, size_t index)
The ixml_membuf type.