* xmlBufGrowInternal: * @buf: the buffer * @len: the minimum free size to allocate * * Grow the available space of an XML buffer, @len is the target value * Error checking should be done on buf->error since using the return * value doesn't work that well * * Returns 0 in case of error or the length made available otherwise */
| 373 | * Returns 0 in case of error or the length made available otherwise |
| 374 | */ |
| 375 | static size_t |
| 376 | xmlBufGrowInternal(xmlBufPtr buf, size_t len) { |
| 377 | size_t size; |
| 378 | xmlChar *newbuf; |
| 379 | |
| 380 | if ((buf == NULL) || (buf->error != 0)) return(0); |
| 381 | CHECK_COMPAT(buf) |
| 382 | |
| 383 | if (len < buf->size - buf->use) |
| 384 | return(buf->size - buf->use - 1); |
| 385 | if (len >= SIZE_MAX - buf->use) { |
| 386 | xmlBufMemoryError(buf); |
| 387 | return(0); |
| 388 | } |
| 389 | |
| 390 | if (buf->size > (size_t) len) { |
| 391 | size = buf->size > SIZE_MAX / 2 ? SIZE_MAX : buf->size * 2; |
| 392 | } else { |
| 393 | size = buf->use + len; |
| 394 | size = size > SIZE_MAX - 100 ? SIZE_MAX : size + 100; |
| 395 | } |
| 396 | |
| 397 | if (buf->alloc == XML_BUFFER_ALLOC_BOUNDED) { |
| 398 | /* |
| 399 | * Used to provide parsing limits |
| 400 | */ |
| 401 | if ((buf->use + len + 1 >= XML_MAX_TEXT_LENGTH) || |
| 402 | (buf->size >= XML_MAX_TEXT_LENGTH)) { |
| 403 | xmlBufMemoryError(buf); |
| 404 | return(0); |
| 405 | } |
| 406 | if (size >= XML_MAX_TEXT_LENGTH) |
| 407 | size = XML_MAX_TEXT_LENGTH; |
| 408 | } |
| 409 | if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) { |
| 410 | size_t start_buf = buf->content - buf->contentIO; |
| 411 | |
| 412 | newbuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + size); |
| 413 | if (newbuf == NULL) { |
| 414 | xmlBufMemoryError(buf); |
| 415 | return(0); |
| 416 | } |
| 417 | buf->contentIO = newbuf; |
| 418 | buf->content = newbuf + start_buf; |
| 419 | } else { |
| 420 | newbuf = (xmlChar *) xmlRealloc(buf->content, size); |
| 421 | if (newbuf == NULL) { |
| 422 | xmlBufMemoryError(buf); |
| 423 | return(0); |
| 424 | } |
| 425 | buf->content = newbuf; |
| 426 | } |
| 427 | buf->size = size; |
| 428 | UPDATE_COMPAT(buf) |
| 429 | return(buf->size - buf->use - 1); |
| 430 | } |
| 431 | |
| 432 | /** |
no test coverage detected