xmlBufferGrow(xmlBufferPtr buf, unsigned int len) {
    unsigned int size;
    xmlChar *newbuf;

    if (buf == NULL) return(-1);

    if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
    if (len < buf->size - buf->use)
        return(0);
    if (len > UINT_MAX - buf->use)
        return(-1);

    if (buf->size > (size_t) len) {
        size = buf->size > UINT_MAX / 2 ? UINT_MAX : buf->size * 2;
    } else {
        size = buf->use + len;
        size = size > UINT_MAX - 100 ? UINT_MAX : size + 100;
    }

    if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
        size_t start_buf = buf->content - buf->contentIO;

	newbuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + size);
	if (newbuf == NULL) {
	    xmlTreeErrMemory("growing buffer");
	    return(-1);
	}
	buf->contentIO = newbuf;
	buf->content = newbuf + start_buf;
    } else {
	newbuf = (xmlChar *) xmlRealloc(buf->content, size);
	if (newbuf == NULL) {
	    xmlTreeErrMemory("growing buffer");
	    return(-1);
	}
	buf->content = newbuf;
    }
    buf->size = size;
    return(buf->size - buf->use);
}
xmlBufferGrow(xmlBufferPtr buf, unsigned int len) {
    unsigned int size;
    xmlChar *newbuf;

    if (buf == NULL) return(-1);

    if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
    if (len < buf->size - buf->use)
        return(0);
    if (len > UINT_MAX - buf->use)
        return(-1);

    if (buf->size > (size_t) len) {
        size = buf->size > UINT_MAX / 2 ? UINT_MAX : buf->size * 2;
    } else {
        size = buf->use + len;
        size = size > UINT_MAX - 100 ? UINT_MAX : size + 100;
    }

    if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
        size_t start_buf = buf->content - buf->contentIO;

	newbuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + size);
	if (newbuf == NULL) {
	    xmlTreeErrMemory("growing buffer");
	    return(-1);
	}
	buf->contentIO = newbuf;
	buf->content = newbuf + start_buf;
    } else {
	newbuf = (xmlChar *) xmlRealloc(buf->content, size);
	if (newbuf == NULL) {
	    xmlTreeErrMemory("growing buffer");
	    return(-1);
	}
	buf->content = newbuf;
    }
    buf->size = size;
    return(buf->size - buf->use);
}