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 ! */
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"
32 #define SIZE_MAX ((size_t) -1)
35 #define WITH_BUFFER_COMPAT
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.
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 */
88 * @extra: extra information
90 * Handle an out of memory condition
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
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
;
120 * routine to create an XML buffer.
121 * returns the new structure.
127 ret
= (xmlBufPtr
) xmlMalloc(sizeof(xmlBuf
));
129 xmlBufMemoryError(NULL
, "creating buffer");
135 ret
->size
= xmlDefaultBufferSize
;
137 ret
->alloc
= xmlBufferAllocScheme
;
138 ret
->content
= (xmlChar
*) xmlMallocAtomic(ret
->size
);
139 if (ret
->content
== NULL
) {
140 xmlBufMemoryError(ret
, "creating buffer");
145 ret
->contentIO
= NULL
;
151 * @size: initial size of buffer
153 * routine to create an XML buffer.
154 * returns the new structure.
157 xmlBufCreateSize(size_t size
) {
160 if (size
== SIZE_MAX
)
162 ret
= (xmlBufPtr
) xmlMalloc(sizeof(xmlBuf
));
164 xmlBufMemoryError(NULL
, "creating buffer");
170 ret
->alloc
= xmlBufferAllocScheme
;
171 ret
->size
= (size
? size
+ 1 : 0); /* +1 for ending null */
174 ret
->content
= (xmlChar
*) xmlMallocAtomic(ret
->size
);
175 if (ret
->content
== NULL
) {
176 xmlBufMemoryError(ret
, "creating buffer");
183 ret
->contentIO
= NULL
;
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.
198 xmlBufDetach(xmlBufPtr buf
) {
203 if (buf
->buffer
!= NULL
)
218 * xmlBufGetAllocationScheme:
221 * Get the buffer allocation scheme
223 * Returns the scheme or -1 in case of error
226 xmlBufGetAllocationScheme(xmlBufPtr buf
) {
229 xmlGenericError(xmlGenericErrorContext
,
230 "xmlBufGetAllocationScheme: buf == NULL\n");
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)) {
251 xmlGenericError(xmlGenericErrorContext
,
252 "xmlBufSetAllocationScheme: buf == NULL or in error\n");
256 if (buf
->alloc
== XML_BUFFER_ALLOC_IO
)
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
)) {
264 buf
->buffer
->alloc
= scheme
;
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
;
280 * @buf: the buffer to free
282 * Frees an XML buffer. It frees both the content and the structure which
286 xmlBufFree(xmlBufPtr buf
) {
289 xmlGenericError(xmlGenericErrorContext
,
290 "xmlBufFree: buf == NULL\n");
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
);
311 xmlBufEmpty(xmlBufPtr buf
) {
312 if ((buf
== NULL
) || (buf
->error
!= 0)) return;
313 if (buf
->content
== NULL
) return;
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
;
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
342 xmlBufShrink(xmlBufPtr buf
, size_t len
) {
343 if ((buf
== NULL
) || (buf
->error
!= 0)) return(0);
345 if (len
== 0) return(0);
346 if (len
> buf
->use
) return(0);
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
358 * sometimes though it maybe be better to really shrink
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
;
371 memmove(buf
->content
, &buf
->content
[len
], buf
->use
);
372 buf
->content
[buf
->use
] = 0;
379 * xmlBufGrowInternal:
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
390 xmlBufGrowInternal(xmlBufPtr buf
, size_t len
) {
394 if ((buf
== NULL
) || (buf
->error
!= 0)) return(0);
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");
404 if (buf
->size
> (size_t) len
) {
405 size
= buf
->size
> SIZE_MAX
/ 2 ? SIZE_MAX
: buf
->size
* 2;
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");
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");
431 buf
->contentIO
= newbuf
;
432 buf
->content
= newbuf
+ start_buf
;
434 newbuf
= (xmlChar
*) xmlRealloc(buf
->content
, size
);
435 if (newbuf
== NULL
) {
436 xmlBufMemoryError(buf
, "growing buffer");
439 buf
->content
= newbuf
;
443 return(buf
->size
- buf
->use
- 1);
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
) {
460 if ((buf
== NULL
) || (len
< 0)) return(-1);
463 ret
= xmlBufGrowInternal(buf
, len
);
466 return(ret
> INT_MAX
? INT_MAX
: ret
);
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
478 xmlBufDump(FILE *file
, xmlBufPtr buf
) {
481 if ((buf
== NULL
) || (buf
->error
!= 0)) {
483 xmlGenericError(xmlGenericErrorContext
,
484 "xmlBufDump: buf == NULL or in error\n");
488 if (buf
->content
== NULL
) {
490 xmlGenericError(xmlGenericErrorContext
,
491 "xmlBufDump: buf->content == NULL\n");
498 ret
= fwrite(buf
->content
, 1, buf
->use
, file
);
506 * Function to extract the content of a buffer
508 * Returns the internal content
512 xmlBufContent(const xmlBuf
*buf
)
514 if ((!buf
) || (buf
->error
))
517 return(buf
->content
);
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
530 xmlBufEnd(xmlBufPtr buf
)
532 if ((!buf
) || (buf
->error
))
536 return(&buf
->content
[buf
->use
]);
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
))
555 if (len
>= (buf
->size
- buf
->use
))
558 buf
->content
[buf
->use
] = 0;
567 * Function to get the length of a buffer
569 * Returns the length of data in the internal content
573 xmlBufLength(const xmlBufPtr buf
)
575 if ((!buf
) || (buf
->error
))
586 * Function to get the length of a buffer
588 * Returns the length of data in the internal content
592 xmlBufUse(const xmlBufPtr buf
)
594 if ((!buf
) || (buf
->error
))
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.
614 xmlBufAvail(const xmlBufPtr buf
)
616 if ((!buf
) || (buf
->error
))
620 return((buf
->size
> buf
->use
) ? (buf
->size
- buf
->use
- 1) : 0);
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
))
638 return(buf
->use
== 0);
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
)
654 xmlChar
* rebuf
= NULL
;
657 if ((buf
== NULL
) || (buf
->error
))
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");
671 /* Don't resize if we don't have to */
672 if (size
< buf
->size
)
675 /* figure out new size */
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);
685 while (size
> newSize
) {
686 if (newSize
> SIZE_MAX
/ 2) {
687 xmlBufMemoryError(buf
, "growing buffer");
693 case XML_BUFFER_ALLOC_EXACT
:
694 newSize
= (size
> SIZE_MAX
- 10 ? SIZE_MAX
: size
+ 10);
696 case XML_BUFFER_ALLOC_HYBRID
:
697 if (buf
->use
< BASE_BUFFER_SIZE
)
701 while (size
> newSize
) {
702 if (newSize
> SIZE_MAX
/ 2) {
703 xmlBufMemoryError(buf
, "growing buffer");
712 newSize
= (size
> SIZE_MAX
- 10 ? SIZE_MAX
: size
+ 10);
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
;
726 rebuf
= (xmlChar
*) xmlRealloc(buf
->contentIO
, start_buf
+ newSize
);
728 xmlBufMemoryError(buf
, "growing buffer");
731 buf
->contentIO
= rebuf
;
732 buf
->content
= rebuf
+ start_buf
;
735 if (buf
->content
== NULL
) {
736 rebuf
= (xmlChar
*) xmlMallocAtomic(newSize
);
740 } else if (buf
->size
- buf
->use
< 100) {
741 rebuf
= (xmlChar
*) xmlRealloc(buf
->content
, newSize
);
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
);
750 memcpy(rebuf
, buf
->content
, buf
->use
);
751 xmlFree(buf
->content
);
756 xmlBufMemoryError(buf
, "growing buffer");
759 buf
->content
= rebuf
;
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
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
) {
783 if ((str
== NULL
) || (buf
== NULL
) || (buf
->error
))
789 xmlGenericError(xmlGenericErrorContext
,
790 "xmlBufAdd: len < 0\n");
794 if (len
== 0) return 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");
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");
818 if (!xmlBufResize(buf
, needSize
)){
819 xmlBufMemoryError(buf
, "growing buffer");
820 return XML_ERR_NO_MEMORY
;
824 memmove(&buf
->content
[buf
->use
], str
, len
);
826 buf
->content
[buf
->use
] = 0;
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
))
846 if (str
== NULL
) return -1;
847 return xmlBufAdd(buf
, str
, -1);
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
))
883 if (xmlStrchr(string
, '\"')) {
884 if (xmlStrchr(string
, '\'')) {
886 xmlGenericError(xmlGenericErrorContext
,
887 "xmlBufWriteQuotedString: string contains quote and double-quotes !\n");
889 xmlBufCCat(buf
, "\"");
894 xmlBufAdd(buf
, base
, cur
- base
);
895 xmlBufAdd(buf
, BAD_CAST
""", 6);
904 xmlBufAdd(buf
, base
, cur
- base
);
905 xmlBufCCat(buf
, "\"");
908 xmlBufCCat(buf
, "\'");
909 xmlBufCat(buf
, string
);
910 xmlBufCCat(buf
, "\'");
913 xmlBufCCat(buf
, "\"");
914 xmlBufCat(buf
, string
);
915 xmlBufCCat(buf
, "\"");
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
929 * Returns a new xmlBufPtr unless the call failed and NULL is returned
932 xmlBufFromBuffer(xmlBufferPtr buffer
) {
938 ret
= (xmlBufPtr
) xmlMalloc(sizeof(xmlBuf
));
940 xmlBufMemoryError(NULL
, "creating buffer");
943 ret
->use
= buffer
->use
;
944 ret
->size
= buffer
->size
;
947 ret
->buffer
= buffer
;
948 ret
->alloc
= buffer
->alloc
;
949 ret
->content
= buffer
->content
;
950 ret
->contentIO
= buffer
->contentIO
;
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
968 xmlBufBackToBuffer(xmlBufPtr buf
) {
974 if ((buf
->error
) || (buf
->buffer
== NULL
)) {
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");
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
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
;
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
;
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
) {
1026 if ((buf
== NULL
) || (buf
->error
)) {
1027 xmlBufferFree(buffer
);
1031 if ((buffer
!= NULL
) && (buffer
->content
!= NULL
) &&
1032 (buffer
->use
> 0)) {
1033 ret
= xmlBufAdd(buf
, buffer
->content
, buffer
->use
);
1035 xmlBufferFree(buffer
);
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
))
1053 input
->base
= input
->cur
= buf
->content
;
1054 input
->end
= &buf
->content
[buf
->use
];
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
1068 xmlBufGetInputBase(xmlBufPtr buf
, xmlParserInputPtr input
) {
1071 if ((input
== NULL
) || (buf
== NULL
) || (buf
->error
))
1074 base
= input
->base
- buf
->content
;
1076 * We could do some pointer arithmetic checks but that's probably
1079 if (base
> buf
->size
) {
1080 xmlBufOverflowError(buf
, "Input reference outside of the buffer");
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
) {
1103 if ((buf
== NULL
) || (buf
->error
)) {
1104 input
->base
= input
->cur
= input
->end
= BAD_CAST
"";
1108 input
->base
= &buf
->content
[base
];
1109 input
->cur
= input
->base
+ cur
;
1110 input
->end
= &buf
->content
[buf
->use
];