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 
34 /*!
35  * \file
36  */
37 
38 
39 #include "ixmlmembuf.h"
40 #include "ixml.h"
41 
42 
43 #include <assert.h>
44 #include <stdlib.h>
45 #include <string.h>
46 
47 
48 /*!
49  * \brief Increases or decreases buffer capacity so that at least 'new_length'
50  * bytes can be stored.
51  *
52  * On error, m's fields do not change.
53  *
54  * \return
55  * 	\li UPNP_E_SUCCESS
56  * 	\li UPNP_E_OUTOF_MEMORY
57  */
ixml_membuf_set_size(ixml_membuf * m,size_t new_length)58 static int ixml_membuf_set_size(
59 	/*! [in,out] The memory buffer. */
60 	ixml_membuf *m,
61 	/*! [in] The new lenght. */
62 	size_t new_length)
63 {
64 	size_t diff;
65 	size_t alloc_len;
66 	char *temp_buf;
67 
68 	if (new_length >= m->length) {
69 		/* increase length */
70 		/* need more mem? */
71 		if (new_length <= m->capacity) {
72 			/* have enough mem; done */
73 			return 0;
74 		}
75 
76 		diff = new_length - m->length;
77 		alloc_len = MAXVAL(m->size_inc, diff) + m->capacity;
78 	} else {
79 		/* decrease length */
80 		assert(new_length <= m->length);
81 
82 		/* if diff is 0..m->size_inc, don't free */
83 		if ((m->capacity - new_length) <= m->size_inc) {
84 			return 0;
85 		}
86 		alloc_len = new_length + m->size_inc;
87 	}
88 
89 	assert(alloc_len >= new_length);
90 
91 	temp_buf = realloc(m->buf, alloc_len + (size_t)1);
92 	if (temp_buf == NULL) {
93 		/* try smaller size */
94 		alloc_len = new_length;
95 		temp_buf = realloc(m->buf, alloc_len + (size_t)1);
96 		if (temp_buf == NULL) {
97 			return IXML_INSUFFICIENT_MEMORY;
98 		}
99 	}
100 	/* save */
101 	m->buf = temp_buf;
102 	m->capacity = alloc_len;
103 
104 	return 0;
105 }
106 
107 
ixml_membuf_init(ixml_membuf * m)108 void ixml_membuf_init(ixml_membuf *m)
109 {
110 	assert(m != NULL);
111 
112 	m->size_inc = MEMBUF_DEF_SIZE_INC;
113 	m->buf = NULL;
114 	m->length = (size_t)0;
115 	m->capacity = (size_t)0;
116 }
117 
118 
ixml_membuf_destroy(ixml_membuf * m)119 void ixml_membuf_destroy(ixml_membuf *m)
120 {
121 	if (m == NULL) {
122 		return;
123 	}
124 
125 	free(m->buf);
126 	ixml_membuf_init(m);
127 }
128 
129 
ixml_membuf_assign(ixml_membuf * m,const void * buf,size_t buf_len)130 int ixml_membuf_assign(
131 	ixml_membuf *m,
132 	const void *buf,
133 	size_t buf_len)
134 {
135 	int return_code;
136 
137 	assert(m != NULL);
138 
139 	/* set value to null */
140 	if (buf == NULL) {
141 		ixml_membuf_destroy(m);
142 		return IXML_SUCCESS;
143 	}
144 	/* alloc mem */
145 	return_code = ixml_membuf_set_size(m, buf_len);
146 	if (return_code != 0) {
147 		return return_code;
148 	}
149 
150 	/* copy */
151 	memcpy(m->buf, buf, buf_len);
152 
153 	/* null-terminate */
154 	m->buf[buf_len] = 0;
155 	m->length = buf_len;
156 
157 	return IXML_SUCCESS;
158 }
159 
160 
ixml_membuf_assign_str(ixml_membuf * m,const char * c_str)161 int ixml_membuf_assign_str(
162 	ixml_membuf *m,
163 	const char *c_str)
164 {
165 	return ixml_membuf_assign(m, c_str, strlen(c_str));
166 }
167 
168 
ixml_membuf_append(ixml_membuf * m,const void * buf)169 int ixml_membuf_append(
170 	/*! [in,out] The memory buffer */
171 	ixml_membuf *m,
172 	/*! [in] The buffer to append */
173 	const void *buf)
174 {
175 	assert(m != NULL);
176 
177 	return ixml_membuf_insert(m, buf, (size_t)1, m->length);
178 }
179 
180 
ixml_membuf_append_str(ixml_membuf * m,const char * c_str)181 int ixml_membuf_append_str(
182 	/*! [in,out] The memory buffer */
183 	ixml_membuf *m,
184 	/*! [in] The characters to append (null-terminated) */
185 	const char *c_str)
186 {
187 	return ixml_membuf_insert(m, c_str, strlen(c_str), m->length);
188 }
189 
190 
ixml_membuf_insert(ixml_membuf * m,const void * buf,size_t buf_len,size_t index)191 int ixml_membuf_insert(
192 	/*! [in,out] The memory buffer */
193 	ixml_membuf *m,
194 	/*! [in] The buffer to insert */
195 	const void *buf,
196 	/*! [in] The length of the buffer */
197 	size_t buf_len,
198 	/*! [in] Position where to insert the buffer */
199 	size_t index)
200 {
201 	int return_code = 0;
202 
203 	assert(m != NULL);
204 
205 	if (index > m->length) {
206 		return IXML_INDEX_SIZE_ERR;
207 	}
208 
209 	if (buf == NULL || buf_len == (size_t)0) {
210 		return 0;
211 	}
212 	/* alloc mem */
213 	return_code = ixml_membuf_set_size(m, m->length + buf_len);
214 	if (return_code != 0) {
215 		return return_code;
216 	}
217 	/* insert data */
218 	/* move data to right of insertion point */
219 	memmove(m->buf + index + buf_len, m->buf + index, m->length - index);
220 	memcpy(m->buf + index, buf, buf_len);
221 	m->length += buf_len;
222 	/* Null terminate */
223 	m->buf[m->length] = 0;
224 
225 	return 0;
226 }
227 
228