include: Move InterlockedExchangeAdd64() definition before its first usage.
[wine.git] / libs / xml2 / buf.c
blobf876ea99fa998c3c8e70170b85fecd18027eb7f3
1 /*
2 * buf.c: memory buffers for libxml2
4 * new buffer structures and entry points to simplify the maintenance
5 * of libxml2 and ensure we keep good control over memory allocations
6 * and stay 64 bits clean.
7 * The new entry point use the xmlBufPtr opaque structure and
8 * xmlBuf...() counterparts to the old xmlBuf...() functions
10 * See Copyright for the status of this software.
12 * daniel@veillard.com
15 #define IN_LIBXML
16 #include "libxml.h"
18 #include <string.h> /* for memset() only ! */
19 #include <limits.h>
20 #include <ctype.h>
21 #include <stdlib.h>
23 #include <libxml/tree.h>
24 #include <libxml/globals.h>
25 #include <libxml/tree.h>
26 #include <libxml/parserInternals.h> /* for XML_MAX_TEXT_LENGTH */
28 #include "private/buf.h"
29 #include "private/error.h"
31 #ifndef SIZE_MAX
32 #define SIZE_MAX ((size_t) -1)
33 #endif
35 #define WITH_BUFFER_COMPAT
37 /**
38 * xmlBuf:
40 * A buffer structure. The base of the structure is somehow compatible
41 * with struct _xmlBuffer to limit risks on application which accessed
42 * directly the input->buf->buffer structures.
45 struct _xmlBuf {
46 xmlChar *content; /* The buffer content UTF8 */
47 unsigned int compat_use; /* for binary compatibility */
48 unsigned int compat_size; /* for binary compatibility */
49 xmlBufferAllocationScheme alloc; /* The realloc method */
50 xmlChar *contentIO; /* in IO mode we may have a different base */
51 size_t use; /* The buffer size used */
52 size_t size; /* The buffer size */
53 xmlBufferPtr buffer; /* wrapper for an old buffer */
54 int error; /* an error code if a failure occurred */
57 #ifdef WITH_BUFFER_COMPAT
59 * Macro for compatibility with xmlBuffer to be used after an xmlBuf
60 * is updated. This makes sure the compat fields are updated too.
62 #define UPDATE_COMPAT(buf) \
63 if (buf->size < INT_MAX) buf->compat_size = buf->size; \
64 else buf->compat_size = INT_MAX; \
65 if (buf->use < INT_MAX) buf->compat_use = buf->use; \
66 else buf->compat_use = INT_MAX;
69 * Macro for compatibility with xmlBuffer to be used in all the xmlBuf
70 * entry points, it checks that the compat fields have not been modified
71 * by direct call to xmlBuffer function from code compiled before 2.9.0 .
73 #define CHECK_COMPAT(buf) \
74 if (buf->size != (size_t) buf->compat_size) \
75 if (buf->compat_size < INT_MAX) \
76 buf->size = buf->compat_size; \
77 if (buf->use != (size_t) buf->compat_use) \
78 if (buf->compat_use < INT_MAX) \
79 buf->use = buf->compat_use;
81 #else /* ! WITH_BUFFER_COMPAT */
82 #define UPDATE_COMPAT(buf)
83 #define CHECK_COMPAT(buf)
84 #endif /* WITH_BUFFER_COMPAT */
86 /**
87 * xmlBufMemoryError:
88 * @extra: extra information
90 * Handle an out of memory condition
91 * To be improved...
93 static void
94 xmlBufMemoryError(xmlBufPtr buf, const char *extra)
96 __xmlSimpleError(XML_FROM_BUFFER, XML_ERR_NO_MEMORY, NULL, NULL, extra);
97 if ((buf) && (buf->error == 0))
98 buf->error = XML_ERR_NO_MEMORY;
102 * xmlBufOverflowError:
103 * @extra: extra information
105 * Handle a buffer overflow error
106 * To be improved...
108 static void
109 xmlBufOverflowError(xmlBufPtr buf, const char *extra)
111 __xmlSimpleError(XML_FROM_BUFFER, XML_BUF_OVERFLOW, NULL, NULL, extra);
112 if ((buf) && (buf->error == 0))
113 buf->error = XML_BUF_OVERFLOW;
118 * xmlBufCreate:
120 * routine to create an XML buffer.
121 * returns the new structure.
123 xmlBufPtr
124 xmlBufCreate(void) {
125 xmlBufPtr ret;
127 ret = (xmlBufPtr) xmlMalloc(sizeof(xmlBuf));
128 if (ret == NULL) {
129 xmlBufMemoryError(NULL, "creating buffer");
130 return(NULL);
132 ret->use = 0;
133 ret->error = 0;
134 ret->buffer = NULL;
135 ret->size = xmlDefaultBufferSize;
136 UPDATE_COMPAT(ret);
137 ret->alloc = xmlBufferAllocScheme;
138 ret->content = (xmlChar *) xmlMallocAtomic(ret->size);
139 if (ret->content == NULL) {
140 xmlBufMemoryError(ret, "creating buffer");
141 xmlFree(ret);
142 return(NULL);
144 ret->content[0] = 0;
145 ret->contentIO = NULL;
146 return(ret);
150 * xmlBufCreateSize:
151 * @size: initial size of buffer
153 * routine to create an XML buffer.
154 * returns the new structure.
156 xmlBufPtr
157 xmlBufCreateSize(size_t size) {
158 xmlBufPtr ret;
160 if (size == SIZE_MAX)
161 return(NULL);
162 ret = (xmlBufPtr) xmlMalloc(sizeof(xmlBuf));
163 if (ret == NULL) {
164 xmlBufMemoryError(NULL, "creating buffer");
165 return(NULL);
167 ret->use = 0;
168 ret->error = 0;
169 ret->buffer = NULL;
170 ret->alloc = xmlBufferAllocScheme;
171 ret->size = (size ? size + 1 : 0); /* +1 for ending null */
172 UPDATE_COMPAT(ret);
173 if (ret->size){
174 ret->content = (xmlChar *) xmlMallocAtomic(ret->size);
175 if (ret->content == NULL) {
176 xmlBufMemoryError(ret, "creating buffer");
177 xmlFree(ret);
178 return(NULL);
180 ret->content[0] = 0;
181 } else
182 ret->content = NULL;
183 ret->contentIO = NULL;
184 return(ret);
188 * xmlBufDetach:
189 * @buf: the buffer
191 * Remove the string contained in a buffer and give it back to the
192 * caller. The buffer is reset to an empty content.
193 * This doesn't work with immutable buffers as they can't be reset.
195 * Returns the previous string contained by the buffer.
197 xmlChar *
198 xmlBufDetach(xmlBufPtr buf) {
199 xmlChar *ret;
201 if (buf == NULL)
202 return(NULL);
203 if (buf->buffer != NULL)
204 return(NULL);
205 if (buf->error)
206 return(NULL);
208 ret = buf->content;
209 buf->content = NULL;
210 buf->size = 0;
211 buf->use = 0;
212 UPDATE_COMPAT(buf);
214 return ret;
218 * xmlBufGetAllocationScheme:
219 * @buf: the buffer
221 * Get the buffer allocation scheme
223 * Returns the scheme or -1 in case of error
226 xmlBufGetAllocationScheme(xmlBufPtr buf) {
227 if (buf == NULL) {
228 #ifdef DEBUG_BUFFER
229 xmlGenericError(xmlGenericErrorContext,
230 "xmlBufGetAllocationScheme: buf == NULL\n");
231 #endif
232 return(-1);
234 return(buf->alloc);
238 * xmlBufSetAllocationScheme:
239 * @buf: the buffer to tune
240 * @scheme: allocation scheme to use
242 * Sets the allocation scheme for this buffer
244 * returns 0 in case of success and -1 in case of failure
247 xmlBufSetAllocationScheme(xmlBufPtr buf,
248 xmlBufferAllocationScheme scheme) {
249 if ((buf == NULL) || (buf->error != 0)) {
250 #ifdef DEBUG_BUFFER
251 xmlGenericError(xmlGenericErrorContext,
252 "xmlBufSetAllocationScheme: buf == NULL or in error\n");
253 #endif
254 return(-1);
256 if (buf->alloc == XML_BUFFER_ALLOC_IO)
257 return(-1);
258 if ((scheme == XML_BUFFER_ALLOC_DOUBLEIT) ||
259 (scheme == XML_BUFFER_ALLOC_EXACT) ||
260 (scheme == XML_BUFFER_ALLOC_HYBRID) ||
261 (scheme == XML_BUFFER_ALLOC_BOUNDED)) {
262 buf->alloc = scheme;
263 if (buf->buffer)
264 buf->buffer->alloc = scheme;
265 return(0);
268 * Switching a buffer ALLOC_IO has the side effect of initializing
269 * the contentIO field with the current content
271 if (scheme == XML_BUFFER_ALLOC_IO) {
272 buf->alloc = XML_BUFFER_ALLOC_IO;
273 buf->contentIO = buf->content;
275 return(-1);
279 * xmlBufFree:
280 * @buf: the buffer to free
282 * Frees an XML buffer. It frees both the content and the structure which
283 * encapsulate it.
285 void
286 xmlBufFree(xmlBufPtr buf) {
287 if (buf == NULL) {
288 #ifdef DEBUG_BUFFER
289 xmlGenericError(xmlGenericErrorContext,
290 "xmlBufFree: buf == NULL\n");
291 #endif
292 return;
295 if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
296 (buf->contentIO != NULL)) {
297 xmlFree(buf->contentIO);
298 } else if (buf->content != NULL) {
299 xmlFree(buf->content);
301 xmlFree(buf);
305 * xmlBufEmpty:
306 * @buf: the buffer
308 * empty a buffer.
310 void
311 xmlBufEmpty(xmlBufPtr buf) {
312 if ((buf == NULL) || (buf->error != 0)) return;
313 if (buf->content == NULL) return;
314 CHECK_COMPAT(buf)
315 buf->use = 0;
316 if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
317 (buf->contentIO != NULL)) {
318 size_t start_buf = buf->content - buf->contentIO;
320 buf->size += start_buf;
321 buf->content = buf->contentIO;
322 buf->content[0] = 0;
323 } else {
324 buf->content[0] = 0;
326 UPDATE_COMPAT(buf)
330 * xmlBufShrink:
331 * @buf: the buffer to dump
332 * @len: the number of xmlChar to remove
334 * Remove the beginning of an XML buffer.
335 * NOTE that this routine behaviour differs from xmlBufferShrink()
336 * as it will return 0 on error instead of -1 due to size_t being
337 * used as the return type.
339 * Returns the number of byte removed or 0 in case of failure
341 size_t
342 xmlBufShrink(xmlBufPtr buf, size_t len) {
343 if ((buf == NULL) || (buf->error != 0)) return(0);
344 CHECK_COMPAT(buf)
345 if (len == 0) return(0);
346 if (len > buf->use) return(0);
348 buf->use -= len;
349 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
351 * we just move the content pointer, but also make sure
352 * the perceived buffer size has shrunk accordingly
354 buf->content += len;
355 buf->size -= len;
358 * sometimes though it maybe be better to really shrink
359 * on IO buffers
361 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
362 size_t start_buf = buf->content - buf->contentIO;
363 if (start_buf >= buf->size) {
364 memmove(buf->contentIO, &buf->content[0], buf->use);
365 buf->content = buf->contentIO;
366 buf->content[buf->use] = 0;
367 buf->size += start_buf;
370 } else {
371 memmove(buf->content, &buf->content[len], buf->use);
372 buf->content[buf->use] = 0;
374 UPDATE_COMPAT(buf)
375 return(len);
379 * xmlBufGrowInternal:
380 * @buf: the buffer
381 * @len: the minimum free size to allocate
383 * Grow the available space of an XML buffer, @len is the target value
384 * Error checking should be done on buf->error since using the return
385 * value doesn't work that well
387 * Returns 0 in case of error or the length made available otherwise
389 static size_t
390 xmlBufGrowInternal(xmlBufPtr buf, size_t len) {
391 size_t size;
392 xmlChar *newbuf;
394 if ((buf == NULL) || (buf->error != 0)) return(0);
395 CHECK_COMPAT(buf)
397 if (len < buf->size - buf->use)
398 return(buf->size - buf->use - 1);
399 if (len >= SIZE_MAX - buf->use) {
400 xmlBufMemoryError(buf, "growing buffer past SIZE_MAX");
401 return(0);
404 if (buf->size > (size_t) len) {
405 size = buf->size > SIZE_MAX / 2 ? SIZE_MAX : buf->size * 2;
406 } else {
407 size = buf->use + len;
408 size = size > SIZE_MAX - 100 ? SIZE_MAX : size + 100;
411 if (buf->alloc == XML_BUFFER_ALLOC_BOUNDED) {
413 * Used to provide parsing limits
415 if ((buf->use + len + 1 >= XML_MAX_TEXT_LENGTH) ||
416 (buf->size >= XML_MAX_TEXT_LENGTH)) {
417 xmlBufMemoryError(buf, "buffer error: text too long\n");
418 return(0);
420 if (size >= XML_MAX_TEXT_LENGTH)
421 size = XML_MAX_TEXT_LENGTH;
423 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
424 size_t start_buf = buf->content - buf->contentIO;
426 newbuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + size);
427 if (newbuf == NULL) {
428 xmlBufMemoryError(buf, "growing buffer");
429 return(0);
431 buf->contentIO = newbuf;
432 buf->content = newbuf + start_buf;
433 } else {
434 newbuf = (xmlChar *) xmlRealloc(buf->content, size);
435 if (newbuf == NULL) {
436 xmlBufMemoryError(buf, "growing buffer");
437 return(0);
439 buf->content = newbuf;
441 buf->size = size;
442 UPDATE_COMPAT(buf)
443 return(buf->size - buf->use - 1);
447 * xmlBufGrow:
448 * @buf: the buffer
449 * @len: the minimum free size to allocate
451 * Grow the available space of an XML buffer, @len is the target value
452 * This is been kept compatible with xmlBufferGrow() as much as possible
454 * Returns -1 in case of error or the length made available otherwise
457 xmlBufGrow(xmlBufPtr buf, int len) {
458 size_t ret;
460 if ((buf == NULL) || (len < 0)) return(-1);
461 if (len == 0)
462 return(0);
463 ret = xmlBufGrowInternal(buf, len);
464 if (buf->error != 0)
465 return(-1);
466 return(ret > INT_MAX ? INT_MAX : ret);
470 * xmlBufDump:
471 * @file: the file output
472 * @buf: the buffer to dump
474 * Dumps an XML buffer to a FILE *.
475 * Returns the number of #xmlChar written
477 size_t
478 xmlBufDump(FILE *file, xmlBufPtr buf) {
479 size_t ret;
481 if ((buf == NULL) || (buf->error != 0)) {
482 #ifdef DEBUG_BUFFER
483 xmlGenericError(xmlGenericErrorContext,
484 "xmlBufDump: buf == NULL or in error\n");
485 #endif
486 return(0);
488 if (buf->content == NULL) {
489 #ifdef DEBUG_BUFFER
490 xmlGenericError(xmlGenericErrorContext,
491 "xmlBufDump: buf->content == NULL\n");
492 #endif
493 return(0);
495 CHECK_COMPAT(buf)
496 if (file == NULL)
497 file = stdout;
498 ret = fwrite(buf->content, 1, buf->use, file);
499 return(ret);
503 * xmlBufContent:
504 * @buf: the buffer
506 * Function to extract the content of a buffer
508 * Returns the internal content
511 xmlChar *
512 xmlBufContent(const xmlBuf *buf)
514 if ((!buf) || (buf->error))
515 return NULL;
517 return(buf->content);
521 * xmlBufEnd:
522 * @buf: the buffer
524 * Function to extract the end of the content of a buffer
526 * Returns the end of the internal content or NULL in case of error
529 xmlChar *
530 xmlBufEnd(xmlBufPtr buf)
532 if ((!buf) || (buf->error))
533 return NULL;
534 CHECK_COMPAT(buf)
536 return(&buf->content[buf->use]);
540 * xmlBufAddLen:
541 * @buf: the buffer
542 * @len: the size which were added at the end
544 * Sometime data may be added at the end of the buffer without
545 * using the xmlBuf APIs that is used to expand the used space
546 * and set the zero terminating at the end of the buffer
548 * Returns -1 in case of error and 0 otherwise
551 xmlBufAddLen(xmlBufPtr buf, size_t len) {
552 if ((buf == NULL) || (buf->error))
553 return(-1);
554 CHECK_COMPAT(buf)
555 if (len >= (buf->size - buf->use))
556 return(-1);
557 buf->use += len;
558 buf->content[buf->use] = 0;
559 UPDATE_COMPAT(buf)
560 return(0);
564 * xmlBufLength:
565 * @buf: the buffer
567 * Function to get the length of a buffer
569 * Returns the length of data in the internal content
572 size_t
573 xmlBufLength(const xmlBufPtr buf)
575 if ((!buf) || (buf->error))
576 return 0;
577 CHECK_COMPAT(buf)
579 return(buf->use);
583 * xmlBufUse:
584 * @buf: the buffer
586 * Function to get the length of a buffer
588 * Returns the length of data in the internal content
591 size_t
592 xmlBufUse(const xmlBufPtr buf)
594 if ((!buf) || (buf->error))
595 return 0;
596 CHECK_COMPAT(buf)
598 return(buf->use);
602 * xmlBufAvail:
603 * @buf: the buffer
605 * Function to find how much free space is allocated but not
606 * used in the buffer. It reserves one byte for the NUL
607 * terminator character that is usually needed, so there is
608 * no need to subtract 1 from the result anymore.
610 * Returns the amount, or 0 if none or if an error occurred.
613 size_t
614 xmlBufAvail(const xmlBufPtr buf)
616 if ((!buf) || (buf->error))
617 return 0;
618 CHECK_COMPAT(buf)
620 return((buf->size > buf->use) ? (buf->size - buf->use - 1) : 0);
624 * xmlBufIsEmpty:
625 * @buf: the buffer
627 * Tell if a buffer is empty
629 * Returns 0 if no, 1 if yes and -1 in case of error
632 xmlBufIsEmpty(const xmlBufPtr buf)
634 if ((!buf) || (buf->error))
635 return(-1);
636 CHECK_COMPAT(buf)
638 return(buf->use == 0);
642 * xmlBufResize:
643 * @buf: the buffer to resize
644 * @size: the desired size
646 * Resize a buffer to accommodate minimum size of @size.
648 * Returns 0 in case of problems, 1 otherwise
651 xmlBufResize(xmlBufPtr buf, size_t size)
653 size_t newSize;
654 xmlChar* rebuf = NULL;
655 size_t start_buf;
657 if ((buf == NULL) || (buf->error))
658 return(0);
659 CHECK_COMPAT(buf)
661 if (buf->alloc == XML_BUFFER_ALLOC_BOUNDED) {
663 * Used to provide parsing limits
665 if (size >= XML_MAX_TEXT_LENGTH) {
666 xmlBufMemoryError(buf, "buffer error: text too long\n");
667 return(0);
671 /* Don't resize if we don't have to */
672 if (size < buf->size)
673 return 1;
675 /* figure out new size */
676 switch (buf->alloc){
677 case XML_BUFFER_ALLOC_IO:
678 case XML_BUFFER_ALLOC_DOUBLEIT:
679 /*take care of empty case*/
680 if (buf->size == 0) {
681 newSize = (size > SIZE_MAX - 10 ? SIZE_MAX : size + 10);
682 } else {
683 newSize = buf->size;
685 while (size > newSize) {
686 if (newSize > SIZE_MAX / 2) {
687 xmlBufMemoryError(buf, "growing buffer");
688 return 0;
690 newSize *= 2;
692 break;
693 case XML_BUFFER_ALLOC_EXACT:
694 newSize = (size > SIZE_MAX - 10 ? SIZE_MAX : size + 10);
695 break;
696 case XML_BUFFER_ALLOC_HYBRID:
697 if (buf->use < BASE_BUFFER_SIZE)
698 newSize = size;
699 else {
700 newSize = buf->size;
701 while (size > newSize) {
702 if (newSize > SIZE_MAX / 2) {
703 xmlBufMemoryError(buf, "growing buffer");
704 return 0;
706 newSize *= 2;
709 break;
711 default:
712 newSize = (size > SIZE_MAX - 10 ? SIZE_MAX : size + 10);
713 break;
716 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
717 start_buf = buf->content - buf->contentIO;
719 if (start_buf > newSize) {
720 /* move data back to start */
721 memmove(buf->contentIO, buf->content, buf->use);
722 buf->content = buf->contentIO;
723 buf->content[buf->use] = 0;
724 buf->size += start_buf;
725 } else {
726 rebuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + newSize);
727 if (rebuf == NULL) {
728 xmlBufMemoryError(buf, "growing buffer");
729 return 0;
731 buf->contentIO = rebuf;
732 buf->content = rebuf + start_buf;
734 } else {
735 if (buf->content == NULL) {
736 rebuf = (xmlChar *) xmlMallocAtomic(newSize);
737 buf->use = 0;
738 if (rebuf != NULL)
739 rebuf[buf->use] = 0;
740 } else if (buf->size - buf->use < 100) {
741 rebuf = (xmlChar *) xmlRealloc(buf->content, newSize);
742 } else {
744 * if we are reallocating a buffer far from being full, it's
745 * better to make a new allocation and copy only the used range
746 * and free the old one.
748 rebuf = (xmlChar *) xmlMallocAtomic(newSize);
749 if (rebuf != NULL) {
750 memcpy(rebuf, buf->content, buf->use);
751 xmlFree(buf->content);
752 rebuf[buf->use] = 0;
755 if (rebuf == NULL) {
756 xmlBufMemoryError(buf, "growing buffer");
757 return 0;
759 buf->content = rebuf;
761 buf->size = newSize;
762 UPDATE_COMPAT(buf)
764 return 1;
768 * xmlBufAdd:
769 * @buf: the buffer to dump
770 * @str: the #xmlChar string
771 * @len: the number of #xmlChar to add
773 * Add a string range to an XML buffer. if len == -1, the length of
774 * str is recomputed.
776 * Returns 0 successful, a positive error code number otherwise
777 * and -1 in case of internal or API error.
780 xmlBufAdd(xmlBufPtr buf, const xmlChar *str, int len) {
781 size_t needSize;
783 if ((str == NULL) || (buf == NULL) || (buf->error))
784 return -1;
785 CHECK_COMPAT(buf)
787 if (len < -1) {
788 #ifdef DEBUG_BUFFER
789 xmlGenericError(xmlGenericErrorContext,
790 "xmlBufAdd: len < 0\n");
791 #endif
792 return -1;
794 if (len == 0) return 0;
796 if (len < 0)
797 len = xmlStrlen(str);
799 if (len < 0) return -1;
800 if (len == 0) return 0;
802 /* Note that both buf->size and buf->use can be zero here. */
803 if ((size_t) len >= buf->size - buf->use) {
804 if ((size_t) len >= SIZE_MAX - buf->use) {
805 xmlBufMemoryError(buf, "growing buffer past SIZE_MAX");
806 return(-1);
808 needSize = buf->use + len + 1;
809 if (buf->alloc == XML_BUFFER_ALLOC_BOUNDED) {
811 * Used to provide parsing limits
813 if (needSize >= XML_MAX_TEXT_LENGTH) {
814 xmlBufMemoryError(buf, "buffer error: text too long\n");
815 return(-1);
818 if (!xmlBufResize(buf, needSize)){
819 xmlBufMemoryError(buf, "growing buffer");
820 return XML_ERR_NO_MEMORY;
824 memmove(&buf->content[buf->use], str, len);
825 buf->use += len;
826 buf->content[buf->use] = 0;
827 UPDATE_COMPAT(buf)
828 return 0;
832 * xmlBufCat:
833 * @buf: the buffer to add to
834 * @str: the #xmlChar string
836 * Append a zero terminated string to an XML buffer.
838 * Returns 0 successful, a positive error code number otherwise
839 * and -1 in case of internal or API error.
842 xmlBufCat(xmlBufPtr buf, const xmlChar *str) {
843 if ((buf == NULL) || (buf->error))
844 return(-1);
845 CHECK_COMPAT(buf)
846 if (str == NULL) return -1;
847 return xmlBufAdd(buf, str, -1);
851 * xmlBufCCat:
852 * @buf: the buffer to dump
853 * @str: the C char string
855 * Append a zero terminated C string to an XML buffer.
857 * Returns 0 successful, a positive error code number otherwise
858 * and -1 in case of internal or API error.
861 xmlBufCCat(xmlBufPtr buf, const char *str) {
862 return xmlBufCat(buf, (const xmlChar *) str);
866 * xmlBufWriteQuotedString:
867 * @buf: the XML buffer output
868 * @string: the string to add
870 * routine which manage and grows an output buffer. This one writes
871 * a quoted or double quoted #xmlChar string, checking first if it holds
872 * quote or double-quotes internally
874 * Returns 0 if successful, a positive error code number otherwise
875 * and -1 in case of internal or API error.
878 xmlBufWriteQuotedString(xmlBufPtr buf, const xmlChar *string) {
879 const xmlChar *cur, *base;
880 if ((buf == NULL) || (buf->error))
881 return(-1);
882 CHECK_COMPAT(buf)
883 if (xmlStrchr(string, '\"')) {
884 if (xmlStrchr(string, '\'')) {
885 #ifdef DEBUG_BUFFER
886 xmlGenericError(xmlGenericErrorContext,
887 "xmlBufWriteQuotedString: string contains quote and double-quotes !\n");
888 #endif
889 xmlBufCCat(buf, "\"");
890 base = cur = string;
891 while(*cur != 0){
892 if(*cur == '"'){
893 if (base != cur)
894 xmlBufAdd(buf, base, cur - base);
895 xmlBufAdd(buf, BAD_CAST "&quot;", 6);
896 cur++;
897 base = cur;
899 else {
900 cur++;
903 if (base != cur)
904 xmlBufAdd(buf, base, cur - base);
905 xmlBufCCat(buf, "\"");
907 else{
908 xmlBufCCat(buf, "\'");
909 xmlBufCat(buf, string);
910 xmlBufCCat(buf, "\'");
912 } else {
913 xmlBufCCat(buf, "\"");
914 xmlBufCat(buf, string);
915 xmlBufCCat(buf, "\"");
917 return(0);
921 * xmlBufFromBuffer:
922 * @buffer: incoming old buffer to convert to a new one
924 * Helper routine to switch from the old buffer structures in use
925 * in various APIs. It creates a wrapper xmlBufPtr which will be
926 * used for internal processing until the xmlBufBackToBuffer() is
927 * issued.
929 * Returns a new xmlBufPtr unless the call failed and NULL is returned
931 xmlBufPtr
932 xmlBufFromBuffer(xmlBufferPtr buffer) {
933 xmlBufPtr ret;
935 if (buffer == NULL)
936 return(NULL);
938 ret = (xmlBufPtr) xmlMalloc(sizeof(xmlBuf));
939 if (ret == NULL) {
940 xmlBufMemoryError(NULL, "creating buffer");
941 return(NULL);
943 ret->use = buffer->use;
944 ret->size = buffer->size;
945 UPDATE_COMPAT(ret);
946 ret->error = 0;
947 ret->buffer = buffer;
948 ret->alloc = buffer->alloc;
949 ret->content = buffer->content;
950 ret->contentIO = buffer->contentIO;
952 return(ret);
956 * xmlBufBackToBuffer:
957 * @buf: new buffer wrapping the old one
959 * Function to be called once internal processing had been done to
960 * update back the buffer provided by the user. This can lead to
961 * a failure in case the size accumulated in the xmlBuf is larger
962 * than what an xmlBuffer can support on 64 bits (INT_MAX)
963 * The xmlBufPtr @buf wrapper is deallocated by this call in any case.
965 * Returns the old xmlBufferPtr unless the call failed and NULL is returned
967 xmlBufferPtr
968 xmlBufBackToBuffer(xmlBufPtr buf) {
969 xmlBufferPtr ret;
971 if (buf == NULL)
972 return(NULL);
973 CHECK_COMPAT(buf)
974 if ((buf->error) || (buf->buffer == NULL)) {
975 xmlBufFree(buf);
976 return(NULL);
979 ret = buf->buffer;
981 * What to do in case of error in the buffer ???
983 if (buf->use > INT_MAX) {
985 * Worse case, we really allocated and used more than the
986 * maximum allowed memory for an xmlBuffer on this architecture.
987 * Keep the buffer but provide a truncated size value.
989 xmlBufOverflowError(buf, "Used size too big for xmlBuffer");
990 ret->use = INT_MAX;
991 ret->size = INT_MAX;
992 } else if (buf->size > INT_MAX) {
994 * milder case, we allocated more than the maximum allowed memory
995 * for an xmlBuffer on this architecture, but used less than the
996 * limit.
997 * Keep the buffer but provide a truncated size value.
999 xmlBufOverflowError(buf, "Allocated size too big for xmlBuffer");
1000 ret->use = buf->use;
1001 ret->size = INT_MAX;
1002 } else {
1003 ret->use = buf->use;
1004 ret->size = buf->size;
1006 ret->alloc = buf->alloc;
1007 ret->content = buf->content;
1008 ret->contentIO = buf->contentIO;
1009 xmlFree(buf);
1010 return(ret);
1014 * xmlBufMergeBuffer:
1015 * @buf: an xmlBufPtr
1016 * @buffer: the buffer to consume into @buf
1018 * The content of @buffer is appended to @buf and @buffer is freed
1020 * Returns -1 in case of error, 0 otherwise, in any case @buffer is freed
1023 xmlBufMergeBuffer(xmlBufPtr buf, xmlBufferPtr buffer) {
1024 int ret = 0;
1026 if ((buf == NULL) || (buf->error)) {
1027 xmlBufferFree(buffer);
1028 return(-1);
1030 CHECK_COMPAT(buf)
1031 if ((buffer != NULL) && (buffer->content != NULL) &&
1032 (buffer->use > 0)) {
1033 ret = xmlBufAdd(buf, buffer->content, buffer->use);
1035 xmlBufferFree(buffer);
1036 return(ret);
1040 * xmlBufResetInput:
1041 * @buf: an xmlBufPtr
1042 * @input: an xmlParserInputPtr
1044 * Update the input to use the current set of pointers from the buffer.
1046 * Returns -1 in case of error, 0 otherwise
1049 xmlBufResetInput(xmlBufPtr buf, xmlParserInputPtr input) {
1050 if ((input == NULL) || (buf == NULL) || (buf->error))
1051 return(-1);
1052 CHECK_COMPAT(buf)
1053 input->base = input->cur = buf->content;
1054 input->end = &buf->content[buf->use];
1055 return(0);
1059 * xmlBufGetInputBase:
1060 * @buf: an xmlBufPtr
1061 * @input: an xmlParserInputPtr
1063 * Get the base of the @input relative to the beginning of the buffer
1065 * Returns the size_t corresponding to the displacement
1067 size_t
1068 xmlBufGetInputBase(xmlBufPtr buf, xmlParserInputPtr input) {
1069 size_t base;
1071 if ((input == NULL) || (buf == NULL) || (buf->error))
1072 return(0);
1073 CHECK_COMPAT(buf)
1074 base = input->base - buf->content;
1076 * We could do some pointer arithmetic checks but that's probably
1077 * sufficient.
1079 if (base > buf->size) {
1080 xmlBufOverflowError(buf, "Input reference outside of the buffer");
1081 base = 0;
1083 return(base);
1087 * xmlBufSetInputBaseCur:
1088 * @buf: an xmlBufPtr
1089 * @input: an xmlParserInputPtr
1090 * @base: the base value relative to the beginning of the buffer
1091 * @cur: the cur value relative to the beginning of the buffer
1093 * Update the input to use the base and cur relative to the buffer
1094 * after a possible reallocation of its content
1096 * Returns -1 in case of error, 0 otherwise
1099 xmlBufSetInputBaseCur(xmlBufPtr buf, xmlParserInputPtr input,
1100 size_t base, size_t cur) {
1101 if (input == NULL)
1102 return(-1);
1103 if ((buf == NULL) || (buf->error)) {
1104 input->base = input->cur = input->end = BAD_CAST "";
1105 return(-1);
1107 CHECK_COMPAT(buf)
1108 input->base = &buf->content[base];
1109 input->cur = input->base + cur;
1110 input->end = &buf->content[buf->use];
1111 return(0);