lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | ///////////////////////////////////////////////////////////////////////////
|
| 2 | //
|
| 3 | // Copyright (c) 2000-2003 Intel Corporation
|
| 4 | // All rights reserved.
|
| 5 | //
|
| 6 | // Redistribution and use in source and binary forms, with or without
|
| 7 | // modification, are permitted provided that the following conditions are met:
|
| 8 | //
|
| 9 | // * Redistributions of source code must retain the above copyright notice,
|
| 10 | // this list of conditions and the following disclaimer.
|
| 11 | // * Redistributions in binary form must reproduce the above copyright notice,
|
| 12 | // this list of conditions and the following disclaimer in the documentation
|
| 13 | // and/or other materials provided with the distribution.
|
| 14 | // * Neither name of Intel Corporation nor the names of its contributors
|
| 15 | // may be used to endorse or promote products derived from this software
|
| 16 | // without specific prior written permission.
|
| 17 | //
|
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
|
| 22 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
| 23 | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
| 24 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
| 25 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
| 26 | // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
| 27 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
| 28 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
| 29 | //
|
| 30 | ///////////////////////////////////////////////////////////////////////////
|
| 31 |
|
| 32 | #include <assert.h>
|
| 33 | #include <stdlib.h>
|
| 34 | #include <string.h>
|
| 35 | #include "xmlbuffer.h"
|
| 36 | #include "cwmp/xmlet.h"
|
| 37 |
|
| 38 | //#include "xmlparser.h"
|
| 39 | /*================================================================
|
| 40 | * XmlBufferSetSize
|
| 41 | *
|
| 42 | * Increases or decreases buffer cap so that at least
|
| 43 | * 'new_length' bytes can be stored
|
| 44 | *
|
| 45 | * On error, m's fields do not change.
|
| 46 | *
|
| 47 | * returns:
|
| 48 | * UPNP_E_SUCCESS
|
| 49 | * UPNP_E_OUTOF_MEMORY
|
| 50 | *
|
| 51 | *=================================================================*/
|
| 52 | static int
|
| 53 | XmlBufferSetSize(
|
| 54 | Pool * pool,
|
| 55 | XmlBuffer * m,
|
| 56 | IN size_t new_length )
|
| 57 | {
|
| 58 | size_t diff;
|
| 59 | size_t alloc_len;
|
| 60 | char *temp_buf;
|
| 61 |
|
| 62 | if ( new_length > m->length ) // increase length
|
| 63 | {
|
| 64 | // need more mem?
|
| 65 | if ( new_length < m->capacity )
|
| 66 | {
|
| 67 | return 0; // have enough mem; done
|
| 68 | }
|
| 69 |
|
| 70 | if (m->length == 0)
|
| 71 | {
|
| 72 | alloc_len = new_length + 1;
|
| 73 | temp_buf = PMALLOC(alloc_len);
|
| 74 | if ( temp_buf == NULL )
|
| 75 | return XML_INSUFFICIENT_MEMORY;
|
| 76 | m->buf = temp_buf;
|
| 77 | m->capacity = alloc_len;
|
| 78 | return 0;
|
| 79 | }
|
| 80 |
|
| 81 | diff = new_length - m->length + 1;
|
| 82 | alloc_len = MAXVAL( m->size_inc, diff ) + m->capacity;
|
| 83 | }
|
| 84 | else // decrease length
|
| 85 | {
|
| 86 | assert( new_length <= m->length );
|
| 87 | return 0;
|
| 88 | /* // if diff is 0..m->size_inc, don't PFREE
|
| 89 | if( ( m->capacity - new_length ) <= m->size_inc ) {
|
| 90 | return 0;
|
| 91 | }
|
| 92 |
|
| 93 | alloc_len = new_length + m->size_inc;
|
| 94 | */
|
| 95 | }
|
| 96 |
|
| 97 | assert( alloc_len > new_length );
|
| 98 |
|
| 99 | temp_buf = PREALLOC( m->buf, m->length, alloc_len );
|
| 100 | if ( temp_buf == NULL )
|
| 101 | {
|
| 102 | // try smaller size
|
| 103 | alloc_len = new_length + 1;
|
| 104 | temp_buf = PREALLOC( m->buf, m->length, alloc_len);
|
| 105 |
|
| 106 | if ( temp_buf == NULL )
|
| 107 | {
|
| 108 | return XML_INSUFFICIENT_MEMORY;
|
| 109 | }
|
| 110 | }
|
| 111 | // save
|
| 112 | m->buf = temp_buf;
|
| 113 | m->capacity = alloc_len;
|
| 114 | return 0;
|
| 115 | }
|
| 116 |
|
| 117 | /*================================================================
|
| 118 | * memcwmp_buffer_init
|
| 119 | *
|
| 120 | *
|
| 121 | *=================================================================*/
|
| 122 | void
|
| 123 | XmlBufferInit( XmlBuffer * m )
|
| 124 | {
|
| 125 | assert( m != NULL );
|
| 126 |
|
| 127 | m->size_inc = MEMBUF_DEF_SIZE_INC;
|
| 128 | m->buf = NULL;
|
| 129 | m->length = 0;
|
| 130 | m->capacity = 0;
|
| 131 | }
|
| 132 |
|
| 133 | /*================================================================
|
| 134 | * memcwmp_buffer_destroy
|
| 135 | *
|
| 136 | *
|
| 137 | *=================================================================*/
|
| 138 | void
|
| 139 | XmlBufferDestroy(
|
| 140 | Pool * pool,
|
| 141 | XmlBuffer * m )
|
| 142 | {
|
| 143 | if ( m == NULL )
|
| 144 | {
|
| 145 | return;
|
| 146 | }
|
| 147 |
|
| 148 | if (m->buf) //laoyou fixed
|
| 149 | PFREE( m->buf );
|
| 150 | XmlBufferInit( m );
|
| 151 | }
|
| 152 |
|
| 153 | /*================================================================
|
| 154 | * XmlBufferAssign
|
| 155 | *
|
| 156 | *
|
| 157 | *=================================================================*/
|
| 158 | int
|
| 159 | XmlBufferAssign(
|
| 160 | Pool * pool,
|
| 161 |
|
| 162 | XmlBuffer * m,
|
| 163 | IN const void *buf,
|
| 164 | IN size_t buf_len )
|
| 165 | {
|
| 166 | int return_code;
|
| 167 |
|
| 168 | assert( m != NULL );
|
| 169 |
|
| 170 | // set value to null
|
| 171 | if ( buf == NULL )
|
| 172 | {
|
| 173 | XmlBufferDestroy(
|
| 174 | #ifdef USE_CWMP_MEMORY_POOL
|
| 175 | pool ,
|
| 176 | #endif
|
| 177 |
|
| 178 | m );
|
| 179 | return XML_OK;
|
| 180 | }
|
| 181 | // alloc mem
|
| 182 | return_code = XmlBufferSetSize(
|
| 183 | #ifdef USE_CWMP_MEMORY_POOL
|
| 184 | pool ,
|
| 185 | #endif
|
| 186 | m, buf_len );
|
| 187 | if ( return_code != 0 )
|
| 188 | {
|
| 189 | return return_code;
|
| 190 | }
|
| 191 | // copy
|
| 192 | memcpy( m->buf, buf, buf_len );
|
| 193 | m->buf[buf_len] = 0; // null-terminate
|
| 194 |
|
| 195 | m->length = buf_len;
|
| 196 |
|
| 197 | return XML_OK;
|
| 198 |
|
| 199 | }
|
| 200 |
|
| 201 | /*================================================================
|
| 202 | * XmlBufferAssignString
|
| 203 | *
|
| 204 | *
|
| 205 | *=================================================================*/
|
| 206 | int
|
| 207 | XmlBufferAssignString(
|
| 208 | Pool * pool,
|
| 209 | XmlBuffer * m,
|
| 210 | IN const char *c_str )
|
| 211 | {
|
| 212 | return XmlBufferAssign(
|
| 213 | #ifdef USE_CWMP_MEMORY_POOL
|
| 214 | pool ,
|
| 215 | #endif
|
| 216 | m, c_str, strlen( c_str ) );
|
| 217 | }
|
| 218 |
|
| 219 | /*================================================================
|
| 220 | * XmlBufferAppend
|
| 221 | *
|
| 222 | *
|
| 223 | *=================================================================*/
|
| 224 | int
|
| 225 | XmlBufferAppend(
|
| 226 | Pool * pool,
|
| 227 |
|
| 228 | XmlBuffer * m,
|
| 229 | IN const void *buf )
|
| 230 | {
|
| 231 | assert( m != NULL );
|
| 232 |
|
| 233 | return XmlBufferInsert(
|
| 234 | #ifdef USE_CWMP_MEMORY_POOL
|
| 235 | pool ,
|
| 236 | #endif
|
| 237 |
|
| 238 | m, buf, 1, m->length );
|
| 239 | }
|
| 240 |
|
| 241 | /*================================================================
|
| 242 | * XmlBufferAppendString
|
| 243 | *
|
| 244 | *
|
| 245 | *=================================================================*/
|
| 246 | int
|
| 247 | XmlBufferAppendString(
|
| 248 | Pool * pool,
|
| 249 |
|
| 250 | XmlBuffer * m,
|
| 251 | IN const char *c_str )
|
| 252 | {
|
| 253 | return XmlBufferInsert(
|
| 254 | #ifdef USE_CWMP_MEMORY_POOL
|
| 255 | pool ,
|
| 256 | #endif
|
| 257 |
|
| 258 | m, c_str, strlen( c_str ), m->length );
|
| 259 | }
|
| 260 |
|
| 261 | /*================================================================
|
| 262 | * XmlBufferInsert
|
| 263 | *
|
| 264 | *
|
| 265 | *=================================================================*/
|
| 266 | int
|
| 267 | XmlBufferInsert(
|
| 268 | Pool * pool,
|
| 269 |
|
| 270 | XmlBuffer * m,
|
| 271 | IN const void *buf,
|
| 272 | IN size_t buf_len,
|
| 273 | int index )
|
| 274 | {
|
| 275 | int return_code;
|
| 276 |
|
| 277 | assert( m != NULL );
|
| 278 |
|
| 279 | if ( index < 0 || index > ( int )m->length )
|
| 280 | return XML_INDEX_SIZE_ERR;
|
| 281 |
|
| 282 | if ( buf == NULL || buf_len == 0 )
|
| 283 | {
|
| 284 | return 0;
|
| 285 | }
|
| 286 | // alloc mem
|
| 287 | return_code = XmlBufferSetSize(
|
| 288 | #ifdef USE_CWMP_MEMORY_POOL
|
| 289 | pool ,
|
| 290 | #endif
|
| 291 |
|
| 292 | m, m->length + buf_len );
|
| 293 | if ( return_code != 0 )
|
| 294 | {
|
| 295 | return return_code;
|
| 296 | }
|
| 297 | // insert data
|
| 298 | // move data to right of insertion point
|
| 299 | memmove( m->buf + index + buf_len, m->buf + index, m->length - index );
|
| 300 | memcpy( m->buf + index, buf, buf_len );
|
| 301 | m->length += buf_len;
|
| 302 | m->buf[m->length] = 0; // null-terminate
|
| 303 |
|
| 304 | return 0;
|
| 305 | }
|