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.
18 #include <string.h> /* for memset() only ! */
27 #include <libxml/tree.h>
28 #include <libxml/globals.h>
29 #include <libxml/tree.h>
30 #include <libxml/parserInternals.h> /* for XML_MAX_TEXT_LENGTH */
34 #define SIZE_MAX ((size_t) -1)
37 #define WITH_BUFFER_COMPAT
42 * A buffer structure. The base of the structure is somehow compatible
43 * with struct _xmlBuffer to limit risks on application which accessed
44 * directly the input->buf->buffer structures.
48 xmlChar
*content
; /* The buffer content UTF8 */
49 unsigned int compat_use
; /* for binary compatibility */
50 unsigned int compat_size
; /* for binary compatibility */
51 xmlBufferAllocationScheme alloc
; /* The realloc method */
52 xmlChar
*contentIO
; /* in IO mode we may have a different base */
53 size_t use
; /* The buffer size used */
54 size_t size
; /* The buffer size */
55 xmlBufferPtr buffer
; /* wrapper for an old buffer */
56 int error
; /* an error code if a failure occurred */
59 #ifdef WITH_BUFFER_COMPAT
61 * Macro for compatibility with xmlBuffer to be used after an xmlBuf
62 * is updated. This makes sure the compat fields are updated too.
64 #define UPDATE_COMPAT(buf) \
65 if (buf->size < INT_MAX) buf->compat_size = buf->size; \
66 else buf->compat_size = INT_MAX; \
67 if (buf->use < INT_MAX) buf->compat_use = buf->use; \
68 else buf->compat_use = INT_MAX;
71 * Macro for compatibility with xmlBuffer to be used in all the xmlBuf
72 * entry points, it checks that the compat fields have not been modified
73 * by direct call to xmlBuffer function from code compiled before 2.9.0 .
75 #define CHECK_COMPAT(buf) \
76 if (buf->size != (size_t) buf->compat_size) \
77 if (buf->compat_size < INT_MAX) \
78 buf->size = buf->compat_size; \
79 if (buf->use != (size_t) buf->compat_use) \
80 if (buf->compat_use < INT_MAX) \
81 buf->use = buf->compat_use;
83 #else /* ! WITH_BUFFER_COMPAT */
84 #define UPDATE_COMPAT(buf)
85 #define CHECK_COMPAT(buf)
86 #endif /* WITH_BUFFER_COMPAT */
90 * @extra: extra information
92 * Handle an out of memory condition
96 xmlBufMemoryError(xmlBufPtr buf
, const char *extra
)
98 __xmlSimpleError(XML_FROM_BUFFER
, XML_ERR_NO_MEMORY
, NULL
, NULL
, extra
);
99 if ((buf
) && (buf
->error
== 0))
100 buf
->error
= XML_ERR_NO_MEMORY
;
104 * xmlBufOverflowError:
105 * @extra: extra information
107 * Handle a buffer overflow error
111 xmlBufOverflowError(xmlBufPtr buf
, const char *extra
)
113 __xmlSimpleError(XML_FROM_BUFFER
, XML_BUF_OVERFLOW
, NULL
, NULL
, extra
);
114 if ((buf
) && (buf
->error
== 0))
115 buf
->error
= XML_BUF_OVERFLOW
;
122 * routine to create an XML buffer.
123 * returns the new structure.
129 ret
= (xmlBufPtr
) xmlMalloc(sizeof(xmlBuf
));
131 xmlBufMemoryError(NULL
, "creating buffer");
138 ret
->size
= xmlDefaultBufferSize
;
139 ret
->compat_size
= xmlDefaultBufferSize
;
140 ret
->alloc
= xmlBufferAllocScheme
;
141 ret
->content
= (xmlChar
*) xmlMallocAtomic(ret
->size
* sizeof(xmlChar
));
142 if (ret
->content
== NULL
) {
143 xmlBufMemoryError(ret
, "creating buffer");
148 ret
->contentIO
= NULL
;
154 * @size: initial size of buffer
156 * routine to create an XML buffer.
157 * returns the new structure.
160 xmlBufCreateSize(size_t size
) {
163 if (size
== SIZE_MAX
)
165 ret
= (xmlBufPtr
) xmlMalloc(sizeof(xmlBuf
));
167 xmlBufMemoryError(NULL
, "creating buffer");
174 ret
->alloc
= xmlBufferAllocScheme
;
175 ret
->size
= (size
? size
+ 1 : 0); /* +1 for ending null */
176 ret
->compat_size
= (ret
->size
> INT_MAX
? INT_MAX
: ret
->size
);
178 ret
->content
= (xmlChar
*) xmlMallocAtomic(ret
->size
* sizeof(xmlChar
));
179 if (ret
->content
== NULL
) {
180 xmlBufMemoryError(ret
, "creating buffer");
187 ret
->contentIO
= NULL
;
195 * Remove the string contained in a buffer and give it back to the
196 * caller. The buffer is reset to an empty content.
197 * This doesn't work with immutable buffers as they can't be reset.
199 * Returns the previous string contained by the buffer.
202 xmlBufDetach(xmlBufPtr buf
) {
207 if (buf
->alloc
== XML_BUFFER_ALLOC_IMMUTABLE
)
209 if (buf
->buffer
!= NULL
)
219 buf
->compat_size
= 0;
226 * xmlBufCreateStatic:
227 * @mem: the memory area
228 * @size: the size in byte
230 * routine to create an XML buffer from an immutable memory area.
231 * The area won't be modified nor copied, and is expected to be
232 * present until the end of the buffer lifetime.
234 * returns the new structure.
237 xmlBufCreateStatic(void *mem
, size_t size
) {
243 ret
= (xmlBufPtr
) xmlMalloc(sizeof(xmlBuf
));
245 xmlBufMemoryError(NULL
, "creating buffer");
248 if (size
< INT_MAX
) {
249 ret
->compat_use
= size
;
250 ret
->compat_size
= size
;
252 ret
->compat_use
= INT_MAX
;
253 ret
->compat_size
= INT_MAX
;
257 ret
->alloc
= XML_BUFFER_ALLOC_IMMUTABLE
;
258 ret
->content
= (xmlChar
*) mem
;
265 * xmlBufGetAllocationScheme:
268 * Get the buffer allocation scheme
270 * Returns the scheme or -1 in case of error
273 xmlBufGetAllocationScheme(xmlBufPtr buf
) {
276 xmlGenericError(xmlGenericErrorContext
,
277 "xmlBufGetAllocationScheme: buf == NULL\n");
285 * xmlBufSetAllocationScheme:
286 * @buf: the buffer to tune
287 * @scheme: allocation scheme to use
289 * Sets the allocation scheme for this buffer
291 * returns 0 in case of success and -1 in case of failure
294 xmlBufSetAllocationScheme(xmlBufPtr buf
,
295 xmlBufferAllocationScheme scheme
) {
296 if ((buf
== NULL
) || (buf
->error
!= 0)) {
298 xmlGenericError(xmlGenericErrorContext
,
299 "xmlBufSetAllocationScheme: buf == NULL or in error\n");
303 if ((buf
->alloc
== XML_BUFFER_ALLOC_IMMUTABLE
) ||
304 (buf
->alloc
== XML_BUFFER_ALLOC_IO
))
306 if ((scheme
== XML_BUFFER_ALLOC_DOUBLEIT
) ||
307 (scheme
== XML_BUFFER_ALLOC_EXACT
) ||
308 (scheme
== XML_BUFFER_ALLOC_HYBRID
) ||
309 (scheme
== XML_BUFFER_ALLOC_IMMUTABLE
) ||
310 (scheme
== XML_BUFFER_ALLOC_BOUNDED
)) {
313 buf
->buffer
->alloc
= scheme
;
317 * Switching a buffer ALLOC_IO has the side effect of initializing
318 * the contentIO field with the current content
320 if (scheme
== XML_BUFFER_ALLOC_IO
) {
321 buf
->alloc
= XML_BUFFER_ALLOC_IO
;
322 buf
->contentIO
= buf
->content
;
329 * @buf: the buffer to free
331 * Frees an XML buffer. It frees both the content and the structure which
335 xmlBufFree(xmlBufPtr buf
) {
338 xmlGenericError(xmlGenericErrorContext
,
339 "xmlBufFree: buf == NULL\n");
344 if ((buf
->alloc
== XML_BUFFER_ALLOC_IO
) &&
345 (buf
->contentIO
!= NULL
)) {
346 xmlFree(buf
->contentIO
);
347 } else if ((buf
->content
!= NULL
) &&
348 (buf
->alloc
!= XML_BUFFER_ALLOC_IMMUTABLE
)) {
349 xmlFree(buf
->content
);
361 xmlBufEmpty(xmlBufPtr buf
) {
362 if ((buf
== NULL
) || (buf
->error
!= 0)) return;
363 if (buf
->content
== NULL
) return;
366 if (buf
->alloc
== XML_BUFFER_ALLOC_IMMUTABLE
) {
367 buf
->content
= BAD_CAST
"";
368 } else if ((buf
->alloc
== XML_BUFFER_ALLOC_IO
) &&
369 (buf
->contentIO
!= NULL
)) {
370 size_t start_buf
= buf
->content
- buf
->contentIO
;
372 buf
->size
+= start_buf
;
373 buf
->content
= buf
->contentIO
;
383 * @buf: the buffer to dump
384 * @len: the number of xmlChar to remove
386 * Remove the beginning of an XML buffer.
387 * NOTE that this routine behaviour differs from xmlBufferShrink()
388 * as it will return 0 on error instead of -1 due to size_t being
389 * used as the return type.
391 * Returns the number of byte removed or 0 in case of failure
394 xmlBufShrink(xmlBufPtr buf
, size_t len
) {
395 if ((buf
== NULL
) || (buf
->error
!= 0)) return(0);
397 if (len
== 0) return(0);
398 if (len
> buf
->use
) return(0);
401 if ((buf
->alloc
== XML_BUFFER_ALLOC_IMMUTABLE
) ||
402 ((buf
->alloc
== XML_BUFFER_ALLOC_IO
) && (buf
->contentIO
!= NULL
))) {
404 * we just move the content pointer, but also make sure
405 * the perceived buffer size has shrunk accordingly
411 * sometimes though it maybe be better to really shrink
414 if ((buf
->alloc
== XML_BUFFER_ALLOC_IO
) && (buf
->contentIO
!= NULL
)) {
415 size_t start_buf
= buf
->content
- buf
->contentIO
;
416 if (start_buf
>= buf
->size
) {
417 memmove(buf
->contentIO
, &buf
->content
[0], buf
->use
);
418 buf
->content
= buf
->contentIO
;
419 buf
->content
[buf
->use
] = 0;
420 buf
->size
+= start_buf
;
424 memmove(buf
->content
, &buf
->content
[len
], buf
->use
);
425 buf
->content
[buf
->use
] = 0;
432 * xmlBufGrowInternal:
434 * @len: the minimum free size to allocate
436 * Grow the available space of an XML buffer, @len is the target value
437 * Error checking should be done on buf->error since using the return
438 * value doesn't work that well
440 * Returns 0 in case of error or the length made available otherwise
443 xmlBufGrowInternal(xmlBufPtr buf
, size_t len
) {
447 if ((buf
== NULL
) || (buf
->error
!= 0)) return(0);
450 if (buf
->alloc
== XML_BUFFER_ALLOC_IMMUTABLE
) return(0);
451 if (len
< buf
->size
- buf
->use
)
452 return(buf
->size
- buf
->use
);
453 if (len
> SIZE_MAX
- buf
->use
)
456 if (buf
->size
> (size_t) len
) {
457 size
= buf
->size
> SIZE_MAX
/ 2 ? SIZE_MAX
: buf
->size
* 2;
459 size
= buf
->use
+ len
;
460 size
= size
> SIZE_MAX
- 100 ? SIZE_MAX
: size
+ 100;
463 if (buf
->alloc
== XML_BUFFER_ALLOC_BOUNDED
) {
465 * Used to provide parsing limits
467 if ((buf
->use
+ len
>= XML_MAX_TEXT_LENGTH
) ||
468 (buf
->size
>= XML_MAX_TEXT_LENGTH
)) {
469 xmlBufMemoryError(buf
, "buffer error: text too long\n");
472 if (size
>= XML_MAX_TEXT_LENGTH
)
473 size
= XML_MAX_TEXT_LENGTH
;
475 if ((buf
->alloc
== XML_BUFFER_ALLOC_IO
) && (buf
->contentIO
!= NULL
)) {
476 size_t start_buf
= buf
->content
- buf
->contentIO
;
478 newbuf
= (xmlChar
*) xmlRealloc(buf
->contentIO
, start_buf
+ size
);
479 if (newbuf
== NULL
) {
480 xmlBufMemoryError(buf
, "growing buffer");
483 buf
->contentIO
= newbuf
;
484 buf
->content
= newbuf
+ start_buf
;
486 newbuf
= (xmlChar
*) xmlRealloc(buf
->content
, size
);
487 if (newbuf
== NULL
) {
488 xmlBufMemoryError(buf
, "growing buffer");
491 buf
->content
= newbuf
;
495 return(buf
->size
- buf
->use
);
501 * @len: the minimum free size to allocate
503 * Grow the available space of an XML buffer, @len is the target value
504 * This is been kept compatible with xmlBufferGrow() as much as possible
506 * Returns -1 in case of error or the length made available otherwise
509 xmlBufGrow(xmlBufPtr buf
, int len
) {
512 if ((buf
== NULL
) || (len
< 0)) return(-1);
515 ret
= xmlBufGrowInternal(buf
, len
);
524 * @len: the minimum extra free size to allocate
526 * Grow the available space of an XML buffer, adding at least @len bytes
528 * Returns 0 if successful or -1 in case of error
531 xmlBufInflate(xmlBufPtr buf
, size_t len
) {
532 if (buf
== NULL
) return(-1);
533 xmlBufGrowInternal(buf
, len
+ buf
->size
);
541 * @file: the file output
542 * @buf: the buffer to dump
544 * Dumps an XML buffer to a FILE *.
545 * Returns the number of #xmlChar written
548 xmlBufDump(FILE *file
, xmlBufPtr buf
) {
551 if ((buf
== NULL
) || (buf
->error
!= 0)) {
553 xmlGenericError(xmlGenericErrorContext
,
554 "xmlBufDump: buf == NULL or in error\n");
558 if (buf
->content
== NULL
) {
560 xmlGenericError(xmlGenericErrorContext
,
561 "xmlBufDump: buf->content == NULL\n");
568 ret
= fwrite(buf
->content
, sizeof(xmlChar
), buf
->use
, file
);
576 * Function to extract the content of a buffer
578 * Returns the internal content
582 xmlBufContent(const xmlBuf
*buf
)
584 if ((!buf
) || (buf
->error
))
587 return(buf
->content
);
594 * Function to extract the end of the content of a buffer
596 * Returns the end of the internal content or NULL in case of error
600 xmlBufEnd(xmlBufPtr buf
)
602 if ((!buf
) || (buf
->error
))
606 return(&buf
->content
[buf
->use
]);
612 * @len: the size which were added at the end
614 * Sometime data may be added at the end of the buffer without
615 * using the xmlBuf APIs that is used to expand the used space
616 * and set the zero terminating at the end of the buffer
618 * Returns -1 in case of error and 0 otherwise
621 xmlBufAddLen(xmlBufPtr buf
, size_t len
) {
622 if ((buf
== NULL
) || (buf
->error
))
625 if (len
> (buf
->size
- buf
->use
))
629 if (buf
->size
> buf
->use
)
630 buf
->content
[buf
->use
] = 0;
639 * @len: the size to erase at the end
641 * Sometime data need to be erased at the end of the buffer
643 * Returns -1 in case of error and 0 otherwise
646 xmlBufErase(xmlBufPtr buf
, size_t len
) {
647 if ((buf
== NULL
) || (buf
->error
))
653 buf
->content
[buf
->use
] = 0;
662 * Function to get the length of a buffer
664 * Returns the length of data in the internal content
668 xmlBufLength(const xmlBufPtr buf
)
670 if ((!buf
) || (buf
->error
))
681 * Function to get the length of a buffer
683 * Returns the length of data in the internal content
687 xmlBufUse(const xmlBufPtr buf
)
689 if ((!buf
) || (buf
->error
))
700 * Function to find how much free space is allocated but not
701 * used in the buffer. It does not account for the terminating zero
704 * Returns the amount or 0 if none or an error occurred
708 xmlBufAvail(const xmlBufPtr buf
)
710 if ((!buf
) || (buf
->error
))
714 return(buf
->size
- buf
->use
);
721 * Tell if a buffer is empty
723 * Returns 0 if no, 1 if yes and -1 in case of error
726 xmlBufIsEmpty(const xmlBufPtr buf
)
728 if ((!buf
) || (buf
->error
))
732 return(buf
->use
== 0);
737 * @buf: the buffer to resize
738 * @size: the desired size
740 * Resize a buffer to accommodate minimum size of @size.
742 * Returns 0 in case of problems, 1 otherwise
745 xmlBufResize(xmlBufPtr buf
, size_t size
)
748 xmlChar
* rebuf
= NULL
;
751 if ((buf
== NULL
) || (buf
->error
))
755 if (buf
->alloc
== XML_BUFFER_ALLOC_IMMUTABLE
) return(0);
756 if (buf
->alloc
== XML_BUFFER_ALLOC_BOUNDED
) {
758 * Used to provide parsing limits
760 if (size
>= XML_MAX_TEXT_LENGTH
) {
761 xmlBufMemoryError(buf
, "buffer error: text too long\n");
766 /* Don't resize if we don't have to */
767 if (size
< buf
->size
)
770 /* figure out new size */
772 case XML_BUFFER_ALLOC_IO
:
773 case XML_BUFFER_ALLOC_DOUBLEIT
:
774 /*take care of empty case*/
775 if (buf
->size
== 0) {
776 newSize
= (size
> SIZE_MAX
- 10 ? SIZE_MAX
: size
+ 10);
780 while (size
> newSize
) {
781 if (newSize
> SIZE_MAX
/ 2) {
782 xmlBufMemoryError(buf
, "growing buffer");
788 case XML_BUFFER_ALLOC_EXACT
:
789 newSize
= (size
> SIZE_MAX
- 10 ? SIZE_MAX
: size
+ 10);
791 case XML_BUFFER_ALLOC_HYBRID
:
792 if (buf
->use
< BASE_BUFFER_SIZE
)
796 while (size
> newSize
) {
797 if (newSize
> SIZE_MAX
/ 2) {
798 xmlBufMemoryError(buf
, "growing buffer");
807 newSize
= (size
> SIZE_MAX
- 10 ? SIZE_MAX
: size
+ 10);
811 if ((buf
->alloc
== XML_BUFFER_ALLOC_IO
) && (buf
->contentIO
!= NULL
)) {
812 start_buf
= buf
->content
- buf
->contentIO
;
814 if (start_buf
> newSize
) {
815 /* move data back to start */
816 memmove(buf
->contentIO
, buf
->content
, buf
->use
);
817 buf
->content
= buf
->contentIO
;
818 buf
->content
[buf
->use
] = 0;
819 buf
->size
+= start_buf
;
821 rebuf
= (xmlChar
*) xmlRealloc(buf
->contentIO
, start_buf
+ newSize
);
823 xmlBufMemoryError(buf
, "growing buffer");
826 buf
->contentIO
= rebuf
;
827 buf
->content
= rebuf
+ start_buf
;
830 if (buf
->content
== NULL
) {
831 rebuf
= (xmlChar
*) xmlMallocAtomic(newSize
);
832 } else if (buf
->size
- buf
->use
< 100) {
833 rebuf
= (xmlChar
*) xmlRealloc(buf
->content
, newSize
);
836 * if we are reallocating a buffer far from being full, it's
837 * better to make a new allocation and copy only the used range
838 * and free the old one.
840 rebuf
= (xmlChar
*) xmlMallocAtomic(newSize
);
842 memcpy(rebuf
, buf
->content
, buf
->use
);
843 xmlFree(buf
->content
);
848 xmlBufMemoryError(buf
, "growing buffer");
851 buf
->content
= rebuf
;
861 * @buf: the buffer to dump
862 * @str: the #xmlChar string
863 * @len: the number of #xmlChar to add
865 * Add a string range to an XML buffer. if len == -1, the length of
868 * Returns 0 successful, a positive error code number otherwise
869 * and -1 in case of internal or API error.
872 xmlBufAdd(xmlBufPtr buf
, const xmlChar
*str
, int len
) {
875 if ((str
== NULL
) || (buf
== NULL
) || (buf
->error
))
879 if (buf
->alloc
== XML_BUFFER_ALLOC_IMMUTABLE
) return -1;
882 xmlGenericError(xmlGenericErrorContext
,
883 "xmlBufAdd: len < 0\n");
887 if (len
== 0) return 0;
890 len
= xmlStrlen(str
);
892 if (len
< 0) return -1;
893 if (len
== 0) return 0;
895 if ((size_t) len
>= buf
->size
- buf
->use
) {
896 if ((size_t) len
>= SIZE_MAX
- buf
->use
)
898 needSize
= buf
->use
+ len
+ 1;
899 if (buf
->alloc
== XML_BUFFER_ALLOC_BOUNDED
) {
901 * Used to provide parsing limits
903 if (needSize
>= XML_MAX_TEXT_LENGTH
) {
904 xmlBufMemoryError(buf
, "buffer error: text too long\n");
908 if (!xmlBufResize(buf
, needSize
)){
909 xmlBufMemoryError(buf
, "growing buffer");
910 return XML_ERR_NO_MEMORY
;
914 memmove(&buf
->content
[buf
->use
], str
, len
*sizeof(xmlChar
));
916 buf
->content
[buf
->use
] = 0;
924 * @str: the #xmlChar string
925 * @len: the number of #xmlChar to add
927 * Add a string range to the beginning of an XML buffer.
928 * if len == -1, the length of @str is recomputed.
930 * Returns 0 successful, a positive error code number otherwise
931 * and -1 in case of internal or API error.
934 xmlBufAddHead(xmlBufPtr buf
, const xmlChar
*str
, int len
) {
935 unsigned int needSize
;
937 if ((buf
== NULL
) || (buf
->error
))
940 if (buf
->alloc
== XML_BUFFER_ALLOC_IMMUTABLE
) return -1;
943 xmlGenericError(xmlGenericErrorContext
,
944 "xmlBufAddHead: str == NULL\n");
950 xmlGenericError(xmlGenericErrorContext
,
951 "xmlBufAddHead: len < 0\n");
955 if (len
== 0) return 0;
958 len
= xmlStrlen(str
);
960 if (len
<= 0) return -1;
962 if ((buf
->alloc
== XML_BUFFER_ALLOC_IO
) && (buf
->contentIO
!= NULL
)) {
963 size_t start_buf
= buf
->content
- buf
->contentIO
;
965 if (start_buf
> (unsigned int) len
) {
967 * We can add it in the space previously shrunk
970 memmove(&buf
->content
[0], str
, len
);
977 needSize
= buf
->use
+ len
+ 2;
978 if (needSize
> buf
->size
){
979 if (buf
->alloc
== XML_BUFFER_ALLOC_BOUNDED
) {
981 * Used to provide parsing limits
983 if (needSize
>= XML_MAX_TEXT_LENGTH
) {
984 xmlBufMemoryError(buf
, "buffer error: text too long\n");
988 if (!xmlBufResize(buf
, needSize
)){
989 xmlBufMemoryError(buf
, "growing buffer");
990 return XML_ERR_NO_MEMORY
;
994 memmove(&buf
->content
[len
], &buf
->content
[0], buf
->use
);
995 memmove(&buf
->content
[0], str
, len
);
997 buf
->content
[buf
->use
] = 0;
1004 * @buf: the buffer to add to
1005 * @str: the #xmlChar string
1007 * Append a zero terminated string to an XML buffer.
1009 * Returns 0 successful, a positive error code number otherwise
1010 * and -1 in case of internal or API error.
1013 xmlBufCat(xmlBufPtr buf
, const xmlChar
*str
) {
1014 if ((buf
== NULL
) || (buf
->error
))
1017 if (buf
->alloc
== XML_BUFFER_ALLOC_IMMUTABLE
) return -1;
1018 if (str
== NULL
) return -1;
1019 return xmlBufAdd(buf
, str
, -1);
1024 * @buf: the buffer to dump
1025 * @str: the C char string
1027 * Append a zero terminated C string to an XML buffer.
1029 * Returns 0 successful, a positive error code number otherwise
1030 * and -1 in case of internal or API error.
1033 xmlBufCCat(xmlBufPtr buf
, const char *str
) {
1034 return xmlBufCat(buf
, (const xmlChar
*) str
);
1039 * @buf: the XML buffer
1040 * @string: the string to add
1042 * routine which manages and grows an output buffer. This one adds
1043 * xmlChars at the end of the buffer.
1045 * Returns 0 if successful, a positive error code number otherwise
1046 * and -1 in case of internal or API error.
1049 xmlBufWriteCHAR(xmlBufPtr buf
, const xmlChar
*string
) {
1050 if ((buf
== NULL
) || (buf
->error
))
1053 if (buf
->alloc
== XML_BUFFER_ALLOC_IMMUTABLE
)
1055 return(xmlBufCat(buf
, string
));
1060 * @buf: the XML buffer output
1061 * @string: the string to add
1063 * routine which manage and grows an output buffer. This one add
1064 * C chars at the end of the array.
1066 * Returns 0 if successful, a positive error code number otherwise
1067 * and -1 in case of internal or API error.
1070 xmlBufWriteChar(xmlBufPtr buf
, const char *string
) {
1071 if ((buf
== NULL
) || (buf
->error
))
1074 if (buf
->alloc
== XML_BUFFER_ALLOC_IMMUTABLE
)
1076 return(xmlBufCCat(buf
, string
));
1081 * xmlBufWriteQuotedString:
1082 * @buf: the XML buffer output
1083 * @string: the string to add
1085 * routine which manage and grows an output buffer. This one writes
1086 * a quoted or double quoted #xmlChar string, checking first if it holds
1087 * quote or double-quotes internally
1089 * Returns 0 if successful, a positive error code number otherwise
1090 * and -1 in case of internal or API error.
1093 xmlBufWriteQuotedString(xmlBufPtr buf
, const xmlChar
*string
) {
1094 const xmlChar
*cur
, *base
;
1095 if ((buf
== NULL
) || (buf
->error
))
1098 if (buf
->alloc
== XML_BUFFER_ALLOC_IMMUTABLE
)
1100 if (xmlStrchr(string
, '\"')) {
1101 if (xmlStrchr(string
, '\'')) {
1103 xmlGenericError(xmlGenericErrorContext
,
1104 "xmlBufWriteQuotedString: string contains quote and double-quotes !\n");
1106 xmlBufCCat(buf
, "\"");
1107 base
= cur
= string
;
1111 xmlBufAdd(buf
, base
, cur
- base
);
1112 xmlBufAdd(buf
, BAD_CAST
""", 6);
1121 xmlBufAdd(buf
, base
, cur
- base
);
1122 xmlBufCCat(buf
, "\"");
1125 xmlBufCCat(buf
, "\'");
1126 xmlBufCat(buf
, string
);
1127 xmlBufCCat(buf
, "\'");
1130 xmlBufCCat(buf
, "\"");
1131 xmlBufCat(buf
, string
);
1132 xmlBufCCat(buf
, "\"");
1139 * @buffer: incoming old buffer to convert to a new one
1141 * Helper routine to switch from the old buffer structures in use
1142 * in various APIs. It creates a wrapper xmlBufPtr which will be
1143 * used for internal processing until the xmlBufBackToBuffer() is
1146 * Returns a new xmlBufPtr unless the call failed and NULL is returned
1149 xmlBufFromBuffer(xmlBufferPtr buffer
) {
1155 ret
= (xmlBufPtr
) xmlMalloc(sizeof(xmlBuf
));
1157 xmlBufMemoryError(NULL
, "creating buffer");
1160 ret
->use
= buffer
->use
;
1161 ret
->size
= buffer
->size
;
1162 ret
->compat_use
= buffer
->use
;
1163 ret
->compat_size
= buffer
->size
;
1165 ret
->buffer
= buffer
;
1166 ret
->alloc
= buffer
->alloc
;
1167 ret
->content
= buffer
->content
;
1168 ret
->contentIO
= buffer
->contentIO
;
1174 * xmlBufBackToBuffer:
1175 * @buf: new buffer wrapping the old one
1177 * Function to be called once internal processing had been done to
1178 * update back the buffer provided by the user. This can lead to
1179 * a failure in case the size accumulated in the xmlBuf is larger
1180 * than what an xmlBuffer can support on 64 bits (INT_MAX)
1181 * The xmlBufPtr @buf wrapper is deallocated by this call in any case.
1183 * Returns the old xmlBufferPtr unless the call failed and NULL is returned
1186 xmlBufBackToBuffer(xmlBufPtr buf
) {
1192 if ((buf
->error
) || (buf
->buffer
== NULL
)) {
1199 * What to do in case of error in the buffer ???
1201 if (buf
->use
> INT_MAX
) {
1203 * Worse case, we really allocated and used more than the
1204 * maximum allowed memory for an xmlBuffer on this architecture.
1205 * Keep the buffer but provide a truncated size value.
1207 xmlBufOverflowError(buf
, "Used size too big for xmlBuffer");
1209 ret
->size
= INT_MAX
;
1210 } else if (buf
->size
> INT_MAX
) {
1212 * milder case, we allocated more than the maximum allowed memory
1213 * for an xmlBuffer on this architecture, but used less than the
1215 * Keep the buffer but provide a truncated size value.
1217 xmlBufOverflowError(buf
, "Allocated size too big for xmlBuffer");
1218 ret
->use
= (int) buf
->use
;
1219 ret
->size
= INT_MAX
;
1221 ret
->use
= (int) buf
->use
;
1222 ret
->size
= (int) buf
->size
;
1224 ret
->alloc
= buf
->alloc
;
1225 ret
->content
= buf
->content
;
1226 ret
->contentIO
= buf
->contentIO
;
1232 * xmlBufMergeBuffer:
1233 * @buf: an xmlBufPtr
1234 * @buffer: the buffer to consume into @buf
1236 * The content of @buffer is appended to @buf and @buffer is freed
1238 * Returns -1 in case of error, 0 otherwise, in any case @buffer is freed
1241 xmlBufMergeBuffer(xmlBufPtr buf
, xmlBufferPtr buffer
) {
1244 if ((buf
== NULL
) || (buf
->error
)) {
1245 xmlBufferFree(buffer
);
1249 if ((buffer
!= NULL
) && (buffer
->content
!= NULL
) &&
1250 (buffer
->use
> 0)) {
1251 ret
= xmlBufAdd(buf
, buffer
->content
, buffer
->use
);
1253 xmlBufferFree(buffer
);
1259 * @buf: an xmlBufPtr
1260 * @input: an xmlParserInputPtr
1262 * Update the input to use the current set of pointers from the buffer.
1264 * Returns -1 in case of error, 0 otherwise
1267 xmlBufResetInput(xmlBufPtr buf
, xmlParserInputPtr input
) {
1268 if ((input
== NULL
) || (buf
== NULL
) || (buf
->error
))
1271 input
->base
= input
->cur
= buf
->content
;
1272 input
->end
= &buf
->content
[buf
->use
];
1277 * xmlBufGetInputBase:
1278 * @buf: an xmlBufPtr
1279 * @input: an xmlParserInputPtr
1281 * Get the base of the @input relative to the beginning of the buffer
1283 * Returns the size_t corresponding to the displacement
1286 xmlBufGetInputBase(xmlBufPtr buf
, xmlParserInputPtr input
) {
1289 if ((input
== NULL
) || (buf
== NULL
) || (buf
->error
))
1292 base
= input
->base
- buf
->content
;
1294 * We could do some pointer arithmetic checks but that's probably
1297 if (base
> buf
->size
) {
1298 xmlBufOverflowError(buf
, "Input reference outside of the buffer");
1305 * xmlBufSetInputBaseCur:
1306 * @buf: an xmlBufPtr
1307 * @input: an xmlParserInputPtr
1308 * @base: the base value relative to the beginning of the buffer
1309 * @cur: the cur value relative to the beginning of the buffer
1311 * Update the input to use the base and cur relative to the buffer
1312 * after a possible reallocation of its content
1314 * Returns -1 in case of error, 0 otherwise
1317 xmlBufSetInputBaseCur(xmlBufPtr buf
, xmlParserInputPtr input
,
1318 size_t base
, size_t cur
) {
1321 if ((buf
== NULL
) || (buf
->error
)) {
1322 input
->base
= input
->cur
= input
->end
= BAD_CAST
"";
1326 input
->base
= &buf
->content
[base
];
1327 input
->cur
= input
->base
+ cur
;
1328 input
->end
= &buf
->content
[buf
->use
];
1333 #include "elfgcchack.h"