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: 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 **************************************************************************/
38#include <ixml/ixmlmembuf.hpp>
39#include <ixml/ixml.hpp>
40
41#include <cassert>
42#include <cstring>
43
56 ixml_membuf* m,
58 size_t new_length) {
59 size_t diff;
60 size_t alloc_len;
61 char* temp_buf;
62
63 if (new_length >= m->length) {
64 /* increase length */
65 /* need more mem? */
66 if (new_length <= m->capacity) {
67 /* have enough mem; done */
68 return 0;
69 }
70
71 diff = new_length - m->length;
72 alloc_len = MAXVAL(m->size_inc, diff) + m->capacity;
73 } else {
74 /* decrease length */
75 assert(new_length <= m->length);
76
77 /* if diff is 0..m->size_inc, don't free */
78 if ((m->capacity - new_length) <= m->size_inc) {
79 return 0;
80 }
81 alloc_len = new_length + m->size_inc;
82 }
83
84 assert(alloc_len >= new_length);
85
86 temp_buf = (char*)realloc(m->buf, alloc_len + (size_t)1);
87 if (temp_buf == NULL) {
88 /* try smaller size */
89 alloc_len = new_length;
90 temp_buf = (char*)realloc(m->buf, alloc_len + (size_t)1);
91 if (temp_buf == NULL) {
92 return IXML_INSUFFICIENT_MEMORY;
93 }
94 }
95 /* save */
96 m->buf = temp_buf;
97 m->capacity = alloc_len;
98
99 return 0;
100}
101
103 assert(m != NULL);
104
105 m->size_inc = MEMBUF_DEF_SIZE_INC;
106 m->buf = NULL;
107 m->length = (size_t)0;
108 m->capacity = (size_t)0;
109}
110
112 if (m == NULL) {
113 return;
114 }
115
116 free(m->buf);
118}
119
120int ixml_membuf_assign(ixml_membuf* m, const void* buf, size_t buf_len) {
121 int return_code;
122
123 assert(m != NULL);
124
125 /* set value to null */
126 if (buf == NULL) {
128 return IXML_SUCCESS;
129 }
130 /* alloc mem */
131 return_code = ixml_membuf_set_size(m, buf_len);
132 if (return_code != 0) {
133 return return_code;
134 }
135
136 /* copy */
137 memcpy(m->buf, buf, buf_len);
138
139 /* null-terminate */
140 m->buf[buf_len] = 0;
141 m->length = buf_len;
142
143 return IXML_SUCCESS;
144}
145
146int ixml_membuf_assign_str(ixml_membuf* m, const char* c_str) {
147 return ixml_membuf_assign(m, c_str, strlen(c_str));
148}
149
152 ixml_membuf* m,
154 const void* buf) {
155 assert(m != NULL);
156
157 return ixml_membuf_insert(m, buf, (size_t)1, m->length);
158}
159
162 ixml_membuf* m,
164 const char* c_str) {
165 return ixml_membuf_insert(m, c_str, strlen(c_str), m->length);
166}
167
170 ixml_membuf* m,
172 const void* buf,
174 size_t buf_len,
176 size_t index) {
177 int return_code = 0;
178
179 assert(m != NULL);
180
181 if (index > m->length) {
182 return IXML_INDEX_SIZE_ERR;
183 }
184
185 if (buf == NULL || buf_len == (size_t)0) {
186 return 0;
187 }
188 /* alloc mem */
189 return_code = ixml_membuf_set_size(m, m->length + buf_len);
190 if (return_code != 0) {
191 return return_code;
192 }
193 /* insert data */
194 /* move data to right of insertion point */
195 memmove(m->buf + index + buf_len, m->buf + index, m->length - index);
196 memcpy(m->buf + index, buf, buf_len);
197 m->length += buf_len;
198 /* Null terminate */
199 m->buf[m->length] = 0;
200
201 return 0;
202}
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.