ReactOS 0.4.16-dev-340-g0540c21
buf.c File Reference
#include "libxml.h"
#include <string.h>
#include <limits.h>
#include <ctype.h>
#include <stdlib.h>
#include <libxml/tree.h>
#include <libxml/globals.h>
#include <libxml/parserInternals.h>
#include "buf.h"
Include dependency graph for buf.c:

Go to the source code of this file.

Classes

struct  _xmlBuf
 

Macros

#define IN_LIBXML
 
#define SIZE_MAX   ((size_t) -1)
 
#define WITH_BUFFER_COMPAT
 
#define UPDATE_COMPAT(buf)
 
#define CHECK_COMPAT(buf)
 

Functions

static void xmlBufMemoryError (xmlBufPtr buf, const char *extra)
 
static void xmlBufOverflowError (xmlBufPtr buf, const char *extra)
 
xmlBufPtr xmlBufCreate (void)
 
xmlBufPtr xmlBufCreateSize (size_t size)
 
xmlCharxmlBufDetach (xmlBufPtr buf)
 
xmlBufPtr xmlBufCreateStatic (void *mem, size_t size)
 
int xmlBufGetAllocationScheme (xmlBufPtr buf)
 
int xmlBufSetAllocationScheme (xmlBufPtr buf, xmlBufferAllocationScheme scheme)
 
void xmlBufFree (xmlBufPtr buf)
 
void xmlBufEmpty (xmlBufPtr buf)
 
size_t xmlBufShrink (xmlBufPtr buf, size_t len)
 
static size_t xmlBufGrowInternal (xmlBufPtr buf, size_t len)
 
int xmlBufGrow (xmlBufPtr buf, int len)
 
size_t xmlBufDump (FILE *file, xmlBufPtr buf)
 
xmlCharxmlBufContent (const xmlBuf *buf)
 
xmlCharxmlBufEnd (xmlBufPtr buf)
 
int xmlBufAddLen (xmlBufPtr buf, size_t len)
 
size_t xmlBufLength (const xmlBufPtr buf)
 
size_t xmlBufUse (const xmlBufPtr buf)
 
size_t xmlBufAvail (const xmlBufPtr buf)
 
int xmlBufIsEmpty (const xmlBufPtr buf)
 
int xmlBufResize (xmlBufPtr buf, size_t size)
 
int xmlBufAdd (xmlBufPtr buf, const xmlChar *str, int len)
 
int xmlBufCat (xmlBufPtr buf, const xmlChar *str)
 
int xmlBufCCat (xmlBufPtr buf, const char *str)
 
int xmlBufWriteQuotedString (xmlBufPtr buf, const xmlChar *string)
 
xmlBufPtr xmlBufFromBuffer (xmlBufferPtr buffer)
 
xmlBufferPtr xmlBufBackToBuffer (xmlBufPtr buf)
 
int xmlBufMergeBuffer (xmlBufPtr buf, xmlBufferPtr buffer)
 
int xmlBufResetInput (xmlBufPtr buf, xmlParserInputPtr input)
 
size_t xmlBufGetInputBase (xmlBufPtr buf, xmlParserInputPtr input)
 
int xmlBufSetInputBaseCur (xmlBufPtr buf, xmlParserInputPtr input, size_t base, size_t cur)
 

Macro Definition Documentation

◆ CHECK_COMPAT

#define CHECK_COMPAT (   buf)
Value:
if (buf->size != (size_t) buf->compat_size) \
if (buf->compat_size < INT_MAX) \
buf->size = buf->compat_size; \
if (buf->use != (size_t) buf->compat_use) \
if (buf->compat_use < INT_MAX) \
buf->use = buf->compat_use;
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
#define INT_MAX
Definition: intsafe.h:150

Definition at line 71 of file buf.c.

◆ IN_LIBXML

#define IN_LIBXML

Definition at line 15 of file buf.c.

◆ SIZE_MAX

#define SIZE_MAX   ((size_t) -1)

Definition at line 30 of file buf.c.

◆ UPDATE_COMPAT

#define UPDATE_COMPAT (   buf)
Value:
if (buf->size < INT_MAX) buf->compat_size = buf->size; \
else buf->compat_size = INT_MAX; \
if (buf->use < INT_MAX) buf->compat_use = buf->use; \
else buf->compat_use = INT_MAX;

Definition at line 60 of file buf.c.

◆ WITH_BUFFER_COMPAT

#define WITH_BUFFER_COMPAT

Definition at line 33 of file buf.c.

Function Documentation

◆ xmlBufAdd()

int xmlBufAdd ( xmlBufPtr  buf,
const xmlChar str,
int  len 
)

xmlBufAdd: @buf: the buffer to dump @str: the xmlChar string @len: the number of xmlChar to add

Add a string range to an XML buffer. if len == -1, the length of str is recomputed.

Returns 0 successful, a positive error code number otherwise and -1 in case of internal or API error.

Definition at line 821 of file buf.c.

821 {
822 size_t needSize;
823
824 if ((str == NULL) || (buf == NULL) || (buf->error))
825 return -1;
827
828 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
829 if (len < -1) {
830#ifdef DEBUG_BUFFER
832 "xmlBufAdd: len < 0\n");
833#endif
834 return -1;
835 }
836 if (len == 0) return 0;
837
838 if (len < 0)
839 len = xmlStrlen(str);
840
841 if (len < 0) return -1;
842 if (len == 0) return 0;
843
844 /* Note that both buf->size and buf->use can be zero here. */
845 if ((size_t) len >= buf->size - buf->use) {
846 if ((size_t) len >= SIZE_MAX - buf->use) {
847 xmlBufMemoryError(buf, "growing buffer past SIZE_MAX");
848 return(-1);
849 }
850 needSize = buf->use + len + 1;
851 if (buf->alloc == XML_BUFFER_ALLOC_BOUNDED) {
852 /*
853 * Used to provide parsing limits
854 */
855 if (needSize >= XML_MAX_TEXT_LENGTH) {
856 xmlBufMemoryError(buf, "buffer error: text too long\n");
857 return(-1);
858 }
859 }
860 if (!xmlBufResize(buf, needSize)){
861 xmlBufMemoryError(buf, "growing buffer");
862 return XML_ERR_NO_MEMORY;
863 }
864 }
865
866 memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
867 buf->use += len;
868 buf->content[buf->use] = 0;
870 return 0;
871}
int xmlBufResize(xmlBufPtr buf, size_t size)
Definition: buf.c:692
#define SIZE_MAX
Definition: buf.c:30
#define UPDATE_COMPAT(buf)
Definition: buf.c:60
#define CHECK_COMPAT(buf)
Definition: buf.c:71
static void xmlBufMemoryError(xmlBufPtr buf, const char *extra)
Definition: buf.c:92
#define NULL
Definition: types.h:112
GLenum GLsizei len
Definition: glext.h:6722
#define memmove(s1, s2, n)
Definition: mkisofs.h:881
#define XML_MAX_TEXT_LENGTH
const WCHAR * str
XMLPUBVAR void * xmlGenericErrorContext
Definition: globals.h:353
XMLPUBVAR xmlGenericErrorFunc xmlGenericError
Definition: globals.h:337
@ XML_BUFFER_ALLOC_BOUNDED
Definition: tree.h:80
@ XML_BUFFER_ALLOC_IMMUTABLE
Definition: tree.h:77
@ XML_ERR_NO_MEMORY
Definition: xmlerror.h:102
XMLPUBFUN int XMLCALL xmlStrlen(const xmlChar *str)
Definition: xmlstring.c:426
unsigned char xmlChar
Definition: xmlstring.h:28

Referenced by xmlBufCat(), xmlBufMergeBuffer(), and xmlBufWriteQuotedString().

◆ xmlBufAddLen()

int xmlBufAddLen ( xmlBufPtr  buf,
size_t  len 
)

xmlBufAddLen: @buf: the buffer @len: the size which were added at the end

Sometime data may be added at the end of the buffer without using the xmlBuf APIs that is used to expand the used space and set the zero terminating at the end of the buffer

Returns -1 in case of error and 0 otherwise

Definition at line 592 of file buf.c.

592 {
593 if ((buf == NULL) || (buf->error))
594 return(-1);
596 if (len >= (buf->size - buf->use))
597 return(-1);
598 buf->use += len;
599 buf->content[buf->use] = 0;
601 return(0);
602}

Referenced by xmlCharEncFirstLineInput(), and xmlCharEncInput().

◆ xmlBufAvail()

size_t xmlBufAvail ( const xmlBufPtr  buf)

xmlBufAvail: @buf: the buffer

Function to find how much free space is allocated but not used in the buffer. It reserves one byte for the NUL terminator character that is usually needed, so there is no need to subtract 1 from the result anymore.

Returns the amount, or 0 if none or if an error occurred.

Definition at line 655 of file buf.c.

656{
657 if ((!buf) || (buf->error))
658 return 0;
660
661 return((buf->size > buf->use) ? (buf->size - buf->use - 1) : 0);
662}

Referenced by xmlCharEncFirstLineInput(), and xmlCharEncInput().

◆ xmlBufBackToBuffer()

xmlBufferPtr xmlBufBackToBuffer ( xmlBufPtr  buf)

xmlBufBackToBuffer: @buf: new buffer wrapping the old one

Function to be called once internal processing had been done to update back the buffer provided by the user. This can lead to a failure in case the size accumulated in the xmlBuf is larger than what an xmlBuffer can support on 64 bits (INT_MAX) The xmlBufPtr @buf wrapper is deallocated by this call in any case.

Returns the old xmlBufferPtr unless the call failed and NULL is returned

Definition at line 1013 of file buf.c.

1013 {
1015
1016 if (buf == NULL)
1017 return(NULL);
1019 if ((buf->error) || (buf->buffer == NULL)) {
1020 xmlBufFree(buf);
1021 return(NULL);
1022 }
1023
1024 ret = buf->buffer;
1025 /*
1026 * What to do in case of error in the buffer ???
1027 */
1028 if (buf->use > INT_MAX) {
1029 /*
1030 * Worse case, we really allocated and used more than the
1031 * maximum allowed memory for an xmlBuffer on this architecture.
1032 * Keep the buffer but provide a truncated size value.
1033 */
1034 xmlBufOverflowError(buf, "Used size too big for xmlBuffer");
1035 ret->use = INT_MAX;
1036 ret->size = INT_MAX;
1037 } else if (buf->size > INT_MAX) {
1038 /*
1039 * milder case, we allocated more than the maximum allowed memory
1040 * for an xmlBuffer on this architecture, but used less than the
1041 * limit.
1042 * Keep the buffer but provide a truncated size value.
1043 */
1044 xmlBufOverflowError(buf, "Allocated size too big for xmlBuffer");
1045 ret->use = (int) buf->use;
1046 ret->size = INT_MAX;
1047 } else {
1048 ret->use = (int) buf->use;
1049 ret->size = (int) buf->size;
1050 }
1051 ret->alloc = buf->alloc;
1052 ret->content = buf->content;
1053 ret->contentIO = buf->contentIO;
1054 xmlFree(buf);
1055 return(ret);
1056}
static void xmlBufOverflowError(xmlBufPtr buf, const char *extra)
Definition: buf.c:107
void xmlBufFree(xmlBufPtr buf)
Definition: buf.c:322
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31
XMLPUBVAR xmlFreeFunc xmlFree
Definition: globals.h:251
int ret

◆ xmlBufCat()

int xmlBufCat ( xmlBufPtr  buf,
const xmlChar str 
)

xmlBufCat: @buf: the buffer to add to @str: the xmlChar string

Append a zero terminated string to an XML buffer.

Returns 0 successful, a positive error code number otherwise and -1 in case of internal or API error.

Definition at line 884 of file buf.c.

884 {
885 if ((buf == NULL) || (buf->error))
886 return(-1);
888 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
889 if (str == NULL) return -1;
890 return xmlBufAdd(buf, str, -1);
891}
int xmlBufAdd(xmlBufPtr buf, const xmlChar *str, int len)
Definition: buf.c:821

Referenced by xmlBufCCat(), and xmlBufWriteQuotedString().

◆ xmlBufCCat()

int xmlBufCCat ( xmlBufPtr  buf,
const char str 
)

xmlBufCCat: @buf: the buffer to dump @str: the C char string

Append a zero terminated C string to an XML buffer.

Returns 0 successful, a positive error code number otherwise and -1 in case of internal or API error.

Definition at line 904 of file buf.c.

904 {
905 return xmlBufCat(buf, (const xmlChar *) str);
906}
int xmlBufCat(xmlBufPtr buf, const xmlChar *str)
Definition: buf.c:884

Referenced by xmlBufWriteQuotedString().

◆ xmlBufContent()

xmlChar * xmlBufContent ( const xmlBuf buf)

xmlBufContent: @buf: the buffer

Function to extract the content of a buffer

Returns the internal content

Definition at line 553 of file buf.c.

554{
555 if ((!buf) || (buf->error))
556 return NULL;
557
558 return(buf->content);
559}

Referenced by xmlCharEncFirstLineInput(), xmlCharEncInput(), xmlParserInputGrow(), xmlParserInputShrink(), and xsltSaveResultToString().

◆ xmlBufCreate()

xmlBufPtr xmlBufCreate ( void  )

xmlBufCreate:

routine to create an XML buffer. returns the new structure.

Definition at line 122 of file buf.c.

122 {
124
125 ret = (xmlBufPtr) xmlMalloc(sizeof(xmlBuf));
126 if (ret == NULL) {
127 xmlBufMemoryError(NULL, "creating buffer");
128 return(NULL);
129 }
130 ret->use = 0;
131 ret->error = 0;
132 ret->buffer = NULL;
135 ret->alloc = xmlBufferAllocScheme;
136 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
137 if (ret->content == NULL) {
138 xmlBufMemoryError(ret, "creating buffer");
139 xmlFree(ret);
140 return(NULL);
141 }
142 ret->content[0] = 0;
143 ret->contentIO = NULL;
144 return(ret);
145}
XMLPUBVAR xmlBufferAllocationScheme xmlBufferAllocScheme
Definition: globals.h:293
XMLPUBVAR xmlMallocFunc xmlMallocAtomic
Definition: globals.h:249
XMLPUBVAR xmlMallocFunc xmlMalloc
Definition: globals.h:248
XMLPUBVAR int xmlDefaultBufferSize
Definition: globals.h:303
xmlBuf * xmlBufPtr
Definition: tree.h:114
Definition: buf.c:43

Referenced by xmlSwitchInputEncodingInt().

◆ xmlBufCreateSize()

xmlBufPtr xmlBufCreateSize ( size_t  size)

xmlBufCreateSize: @size: initial size of buffer

routine to create an XML buffer. returns the new structure.

Definition at line 155 of file buf.c.

155 {
157
158 if (size == SIZE_MAX)
159 return(NULL);
160 ret = (xmlBufPtr) xmlMalloc(sizeof(xmlBuf));
161 if (ret == NULL) {
162 xmlBufMemoryError(NULL, "creating buffer");
163 return(NULL);
164 }
165 ret->use = 0;
166 ret->error = 0;
167 ret->buffer = NULL;
168 ret->alloc = xmlBufferAllocScheme;
169 ret->size = (size ? size + 1 : 0); /* +1 for ending null */
171 if (ret->size){
172 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
173 if (ret->content == NULL) {
174 xmlBufMemoryError(ret, "creating buffer");
175 xmlFree(ret);
176 return(NULL);
177 }
178 ret->content[0] = 0;
179 } else
180 ret->content = NULL;
181 ret->contentIO = NULL;
182 return(ret);
183}
GLsizeiptr size
Definition: glext.h:5919

◆ xmlBufCreateStatic()

xmlBufPtr xmlBufCreateStatic ( void mem,
size_t  size 
)

xmlBufCreateStatic: @mem: the memory area @size: the size in byte

routine to create an XML buffer from an immutable memory area. The area won't be modified nor copied, and is expected to be present until the end of the buffer lifetime.

returns the new structure.

Definition at line 230 of file buf.c.

230 {
232
233 if (mem == NULL)
234 return(NULL);
235
236 ret = (xmlBufPtr) xmlMalloc(sizeof(xmlBuf));
237 if (ret == NULL) {
238 xmlBufMemoryError(NULL, "creating buffer");
239 return(NULL);
240 }
241 ret->use = size;
242 ret->size = size;
245 ret->content = (xmlChar *) mem;
246 ret->error = 0;
247 ret->buffer = NULL;
248 return(ret);
249}
Definition: mem.c:349

◆ xmlBufDetach()

xmlChar * xmlBufDetach ( xmlBufPtr  buf)

xmlBufDetach: @buf: the buffer

Remove the string contained in a buffer and give it back to the caller. The buffer is reset to an empty content. This doesn't work with immutable buffers as they can't be reset.

Returns the previous string contained by the buffer.

Definition at line 196 of file buf.c.

196 {
197 xmlChar *ret;
198
199 if (buf == NULL)
200 return(NULL);
201 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE)
202 return(NULL);
203 if (buf->buffer != NULL)
204 return(NULL);
205 if (buf->error)
206 return(NULL);
207
208 ret = buf->content;
209 buf->content = NULL;
210 buf->size = 0;
211 buf->use = 0;
213
214 return ret;
215}

◆ xmlBufDump()

size_t xmlBufDump ( FILE file,
xmlBufPtr  buf 
)

Definition at line 519 of file buf.c.

519 {
520 size_t ret;
521
522 if ((buf == NULL) || (buf->error != 0)) {
523#ifdef DEBUG_BUFFER
525 "xmlBufDump: buf == NULL or in error\n");
526#endif
527 return(0);
528 }
529 if (buf->content == NULL) {
530#ifdef DEBUG_BUFFER
532 "xmlBufDump: buf->content == NULL\n");
533#endif
534 return(0);
535 }
537 if (file == NULL)
538 file = stdout;
539 ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file);
540 return(ret);
541}
#define stdout
Definition: stdio.h:99
_Check_return_opt_ _CRTIMP size_t __cdecl fwrite(_In_reads_bytes_(_Size *_Count) const void *_Str, _In_ size_t _Size, _In_ size_t _Count, _Inout_ FILE *_File)
Definition: fci.c:127

◆ xmlBufEmpty()

void xmlBufEmpty ( xmlBufPtr  buf)

xmlBufEmpty: @buf: the buffer

empty a buffer.

Definition at line 348 of file buf.c.

348 {
349 if ((buf == NULL) || (buf->error != 0)) return;
350 if (buf->content == NULL) return;
352 buf->use = 0;
353 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) {
354 buf->content = BAD_CAST "";
355 } else if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
356 (buf->contentIO != NULL)) {
357 size_t start_buf = buf->content - buf->contentIO;
358
359 buf->size += start_buf;
360 buf->content = buf->contentIO;
361 buf->content[0] = 0;
362 } else {
363 buf->content[0] = 0;
364 }
366}
@ XML_BUFFER_ALLOC_IO
Definition: tree.h:78
#define BAD_CAST
Definition: xmlstring.h:35

◆ xmlBufEnd()

xmlChar * xmlBufEnd ( xmlBufPtr  buf)

xmlBufEnd: @buf: the buffer

Function to extract the end of the content of a buffer

Returns the end of the internal content or NULL in case of error

Definition at line 571 of file buf.c.

572{
573 if ((!buf) || (buf->error))
574 return NULL;
576
577 return(&buf->content[buf->use]);
578}

Referenced by xmlCharEncFirstLineInput(), xmlCharEncInput(), xmlParserInputGrow(), and xmlParserInputShrink().

◆ xmlBufFree()

void xmlBufFree ( xmlBufPtr  buf)

xmlBufFree: @buf: the buffer to free

Frees an XML buffer. It frees both the content and the structure which encapsulate it.

Definition at line 322 of file buf.c.

322 {
323 if (buf == NULL) {
324#ifdef DEBUG_BUFFER
326 "xmlBufFree: buf == NULL\n");
327#endif
328 return;
329 }
330
331 if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
332 (buf->contentIO != NULL)) {
333 xmlFree(buf->contentIO);
334 } else if ((buf->content != NULL) &&
335 (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) {
336 xmlFree(buf->content);
337 }
338 xmlFree(buf);
339}

Referenced by xmlBufBackToBuffer().

◆ xmlBufFromBuffer()

xmlBufPtr xmlBufFromBuffer ( xmlBufferPtr  buffer)

xmlBufFromBuffer: @buffer: incoming old buffer to convert to a new one

Helper routine to switch from the old buffer structures in use in various APIs. It creates a wrapper xmlBufPtr which will be used for internal processing until the xmlBufBackToBuffer() is issued.

Returns a new xmlBufPtr unless the call failed and NULL is returned

Definition at line 977 of file buf.c.

977 {
979
980 if (buffer == NULL)
981 return(NULL);
982
983 ret = (xmlBufPtr) xmlMalloc(sizeof(xmlBuf));
984 if (ret == NULL) {
985 xmlBufMemoryError(NULL, "creating buffer");
986 return(NULL);
987 }
988 ret->use = buffer->use;
989 ret->size = buffer->size;
991 ret->error = 0;
992 ret->buffer = buffer;
993 ret->alloc = buffer->alloc;
994 ret->content = buffer->content;
995 ret->contentIO = buffer->contentIO;
996
997 return(ret);
998}
GLuint buffer
Definition: glext.h:5915

◆ xmlBufGetAllocationScheme()

int xmlBufGetAllocationScheme ( xmlBufPtr  buf)

xmlBufGetAllocationScheme: @buf: the buffer

Get the buffer allocation scheme

Returns the scheme or -1 in case of error

Definition at line 260 of file buf.c.

260 {
261 if (buf == NULL) {
262#ifdef DEBUG_BUFFER
264 "xmlBufGetAllocationScheme: buf == NULL\n");
265#endif
266 return(-1);
267 }
268 return(buf->alloc);
269}

◆ xmlBufGetInputBase()

size_t xmlBufGetInputBase ( xmlBufPtr  buf,
xmlParserInputPtr  input 
)

xmlBufGetInputBase: @buf: an xmlBufPtr @input: an xmlParserInputPtr

Get the base of the @input relative to the beginning of the buffer

Returns the size_t corresponding to the displacement

Definition at line 1113 of file buf.c.

1113 {
1114 size_t base;
1115
1116 if ((input == NULL) || (buf == NULL) || (buf->error))
1117 return(-1);
1119 base = input->base - buf->content;
1120 /*
1121 * We could do some pointer arithmetic checks but that's probably
1122 * sufficient.
1123 */
1124 if (base > buf->size) {
1125 xmlBufOverflowError(buf, "Input reference outside of the buffer");
1126 base = 0;
1127 }
1128 return(base);
1129}
GLenum GLenum GLenum input
Definition: glext.h:9031

Referenced by xmlCtxtResetPush().

◆ xmlBufGrow()

int xmlBufGrow ( xmlBufPtr  buf,
int  len 
)

xmlBufGrow: @buf: the buffer @len: the minimum free size to allocate

Grow the available space of an XML buffer, @len is the target value This is been kept compatible with xmlBufferGrow() as much as possible

Returns -1 in case of error or the length made available otherwise

Definition at line 498 of file buf.c.

498 {
499 size_t ret;
500
501 if ((buf == NULL) || (len < 0)) return(-1);
502 if (len == 0)
503 return(0);
505 if (buf->error != 0)
506 return(-1);
507 return((int) ret);
508}
static size_t xmlBufGrowInternal(xmlBufPtr buf, size_t len)
Definition: buf.c:430

Referenced by xmlCharEncFirstLineInput(), and xmlCharEncInput().

◆ xmlBufGrowInternal()

static size_t xmlBufGrowInternal ( xmlBufPtr  buf,
size_t  len 
)
static

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

Definition at line 430 of file buf.c.

430 {
431 size_t size;
432 xmlChar *newbuf;
433
434 if ((buf == NULL) || (buf->error != 0)) return(0);
436
437 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
438 if (len < buf->size - buf->use)
439 return(buf->size - buf->use - 1);
440 if (len >= SIZE_MAX - buf->use) {
441 xmlBufMemoryError(buf, "growing buffer past SIZE_MAX");
442 return(0);
443 }
444
445 if (buf->size > (size_t) len) {
446 size = buf->size > SIZE_MAX / 2 ? SIZE_MAX : buf->size * 2;
447 } else {
448 size = buf->use + len;
449 size = size > SIZE_MAX - 100 ? SIZE_MAX : size + 100;
450 }
451
452 if (buf->alloc == XML_BUFFER_ALLOC_BOUNDED) {
453 /*
454 * Used to provide parsing limits
455 */
456 if ((buf->use + len + 1 >= XML_MAX_TEXT_LENGTH) ||
457 (buf->size >= XML_MAX_TEXT_LENGTH)) {
458 xmlBufMemoryError(buf, "buffer error: text too long\n");
459 return(0);
460 }
463 }
464 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
465 size_t start_buf = buf->content - buf->contentIO;
466
467 newbuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + size);
468 if (newbuf == NULL) {
469 xmlBufMemoryError(buf, "growing buffer");
470 return(0);
471 }
472 buf->contentIO = newbuf;
473 buf->content = newbuf + start_buf;
474 } else {
475 newbuf = (xmlChar *) xmlRealloc(buf->content, size);
476 if (newbuf == NULL) {
477 xmlBufMemoryError(buf, "growing buffer");
478 return(0);
479 }
480 buf->content = newbuf;
481 }
482 buf->size = size;
484 return(buf->size - buf->use - 1);
485}
XMLPUBVAR xmlReallocFunc xmlRealloc
Definition: globals.h:250

Referenced by xmlBufGrow().

◆ xmlBufIsEmpty()

int xmlBufIsEmpty ( const xmlBufPtr  buf)

xmlBufIsEmpty: @buf: the buffer

Tell if a buffer is empty

Returns 0 if no, 1 if yes and -1 in case of error

Definition at line 673 of file buf.c.

674{
675 if ((!buf) || (buf->error))
676 return(-1);
678
679 return(buf->use == 0);
680}

Referenced by xmlSwitchInputEncodingInt().

◆ xmlBufLength()

size_t xmlBufLength ( const xmlBufPtr  buf)

xmlBufLength: @buf: the buffer

Function to get the length of a buffer

Returns the length of data in the internal content

Definition at line 614 of file buf.c.

615{
616 if ((!buf) || (buf->error))
617 return 0;
619
620 return(buf->use);
621}

◆ xmlBufMemoryError()

static void xmlBufMemoryError ( xmlBufPtr  buf,
const char extra 
)
static

xmlBufMemoryError: @extra: extra information

Handle an out of memory condition To be improved...

Definition at line 92 of file buf.c.

93{
94 __xmlSimpleError(XML_FROM_BUFFER, XML_ERR_NO_MEMORY, NULL, NULL, extra);
95 if ((buf) && (buf->error == 0))
96 buf->error = XML_ERR_NO_MEMORY;
97}
@ extra
Definition: id3.c:95
@ XML_FROM_BUFFER
Definition: xmlerror.h:66

Referenced by xmlBufAdd(), xmlBufCreate(), xmlBufCreateSize(), xmlBufCreateStatic(), xmlBufFromBuffer(), xmlBufGrowInternal(), and xmlBufResize().

◆ xmlBufMergeBuffer()

int xmlBufMergeBuffer ( xmlBufPtr  buf,
xmlBufferPtr  buffer 
)

xmlBufMergeBuffer: @buf: an xmlBufPtr @buffer: the buffer to consume into @buf

The content of @buffer is appended to @buf and @buffer is freed

Returns -1 in case of error, 0 otherwise, in any case @buffer is freed

Definition at line 1068 of file buf.c.

1068 {
1069 int ret = 0;
1070
1071 if ((buf == NULL) || (buf->error)) {
1073 return(-1);
1074 }
1076 if ((buffer != NULL) && (buffer->content != NULL) &&
1077 (buffer->use > 0)) {
1078 ret = xmlBufAdd(buf, buffer->content, buffer->use);
1079 }
1081 return(ret);
1082}
XMLPUBFUN void XMLCALL xmlBufferFree(xmlBufferPtr buf)

◆ xmlBufOverflowError()

static void xmlBufOverflowError ( xmlBufPtr  buf,
const char extra 
)
static

xmlBufOverflowError: @extra: extra information

Handle a buffer overflow error To be improved...

Definition at line 107 of file buf.c.

108{
109 __xmlSimpleError(XML_FROM_BUFFER, XML_BUF_OVERFLOW, NULL, NULL, extra);
110 if ((buf) && (buf->error == 0))
111 buf->error = XML_BUF_OVERFLOW;
112}
@ XML_BUF_OVERFLOW
Definition: xmlerror.h:835

Referenced by xmlBufBackToBuffer(), and xmlBufGetInputBase().

◆ xmlBufResetInput()

int xmlBufResetInput ( xmlBufPtr  buf,
xmlParserInputPtr  input 
)

xmlBufResetInput: @buf: an xmlBufPtr @input: an xmlParserInputPtr

Update the input to use the current set of pointers from the buffer.

Returns -1 in case of error, 0 otherwise

Definition at line 1094 of file buf.c.

1094 {
1095 if ((input == NULL) || (buf == NULL) || (buf->error))
1096 return(-1);
1098 input->base = input->cur = buf->content;
1099 input->end = &buf->content[buf->use];
1100 return(0);
1101}

Referenced by xmlCreateMemoryParserCtxt(), xmlCtxtResetPush(), xmlNewInputFromFile(), xmlNewIOInputStream(), and xmlSwitchInputEncodingInt().

◆ xmlBufResize()

int xmlBufResize ( xmlBufPtr  buf,
size_t  size 
)

xmlBufResize: @buf: the buffer to resize @size: the desired size

Resize a buffer to accommodate minimum size of @size.

Returns 0 in case of problems, 1 otherwise

Definition at line 692 of file buf.c.

693{
694 size_t newSize;
695 xmlChar* rebuf = NULL;
696 size_t start_buf;
697
698 if ((buf == NULL) || (buf->error))
699 return(0);
701
702 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
703 if (buf->alloc == XML_BUFFER_ALLOC_BOUNDED) {
704 /*
705 * Used to provide parsing limits
706 */
707 if (size >= XML_MAX_TEXT_LENGTH) {
708 xmlBufMemoryError(buf, "buffer error: text too long\n");
709 return(0);
710 }
711 }
712
713 /* Don't resize if we don't have to */
714 if (size < buf->size)
715 return 1;
716
717 /* figure out new size */
718 switch (buf->alloc){
721 /*take care of empty case*/
722 if (buf->size == 0) {
723 newSize = (size > SIZE_MAX - 10 ? SIZE_MAX : size + 10);
724 } else {
725 newSize = buf->size;
726 }
727 while (size > newSize) {
728 if (newSize > SIZE_MAX / 2) {
729 xmlBufMemoryError(buf, "growing buffer");
730 return 0;
731 }
732 newSize *= 2;
733 }
734 break;
736 newSize = (size > SIZE_MAX - 10 ? SIZE_MAX : size + 10);
737 break;
739 if (buf->use < BASE_BUFFER_SIZE)
740 newSize = size;
741 else {
742 newSize = buf->size;
743 while (size > newSize) {
744 if (newSize > SIZE_MAX / 2) {
745 xmlBufMemoryError(buf, "growing buffer");
746 return 0;
747 }
748 newSize *= 2;
749 }
750 }
751 break;
752
753 default:
754 newSize = (size > SIZE_MAX - 10 ? SIZE_MAX : size + 10);
755 break;
756 }
757
758 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
759 start_buf = buf->content - buf->contentIO;
760
761 if (start_buf > newSize) {
762 /* move data back to start */
763 memmove(buf->contentIO, buf->content, buf->use);
764 buf->content = buf->contentIO;
765 buf->content[buf->use] = 0;
766 buf->size += start_buf;
767 } else {
768 rebuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + newSize);
769 if (rebuf == NULL) {
770 xmlBufMemoryError(buf, "growing buffer");
771 return 0;
772 }
773 buf->contentIO = rebuf;
774 buf->content = rebuf + start_buf;
775 }
776 } else {
777 if (buf->content == NULL) {
778 rebuf = (xmlChar *) xmlMallocAtomic(newSize);
779 buf->use = 0;
780 rebuf[buf->use] = 0;
781 } else if (buf->size - buf->use < 100) {
782 rebuf = (xmlChar *) xmlRealloc(buf->content, newSize);
783 } else {
784 /*
785 * if we are reallocating a buffer far from being full, it's
786 * better to make a new allocation and copy only the used range
787 * and free the old one.
788 */
789 rebuf = (xmlChar *) xmlMallocAtomic(newSize);
790 if (rebuf != NULL) {
791 memcpy(rebuf, buf->content, buf->use);
792 xmlFree(buf->content);
793 rebuf[buf->use] = 0;
794 }
795 }
796 if (rebuf == NULL) {
797 xmlBufMemoryError(buf, "growing buffer");
798 return 0;
799 }
800 buf->content = rebuf;
801 }
802 buf->size = newSize;
804
805 return 1;
806}
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
@ XML_BUFFER_ALLOC_EXACT
Definition: tree.h:76
@ XML_BUFFER_ALLOC_DOUBLEIT
Definition: tree.h:75
@ XML_BUFFER_ALLOC_HYBRID
Definition: tree.h:79
#define BASE_BUFFER_SIZE
Definition: tree.h:56

Referenced by xmlBufAdd().

◆ xmlBufSetAllocationScheme()

int xmlBufSetAllocationScheme ( xmlBufPtr  buf,
xmlBufferAllocationScheme  scheme 
)

xmlBufSetAllocationScheme: @buf: the buffer to tune @scheme: allocation scheme to use

Sets the allocation scheme for this buffer

returns 0 in case of success and -1 in case of failure

Definition at line 281 of file buf.c.

282 {
283 if ((buf == NULL) || (buf->error != 0)) {
284#ifdef DEBUG_BUFFER
286 "xmlBufSetAllocationScheme: buf == NULL or in error\n");
287#endif
288 return(-1);
289 }
290 if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) ||
291 (buf->alloc == XML_BUFFER_ALLOC_IO))
292 return(-1);
298 buf->alloc = scheme;
299 if (buf->buffer)
300 buf->buffer->alloc = scheme;
301 return(0);
302 }
303 /*
304 * Switching a buffer ALLOC_IO has the side effect of initializing
305 * the contentIO field with the current content
306 */
308 buf->alloc = XML_BUFFER_ALLOC_IO;
309 buf->contentIO = buf->content;
310 }
311 return(-1);
312}
DWORD scheme

◆ xmlBufSetInputBaseCur()

int xmlBufSetInputBaseCur ( xmlBufPtr  buf,
xmlParserInputPtr  input,
size_t  base,
size_t  cur 
)

xmlBufSetInputBaseCur: @buf: an xmlBufPtr @input: an xmlParserInputPtr @base: the base value relative to the beginning of the buffer @cur: the cur value relative to the beginning of the buffer

Update the input to use the base and cur relative to the buffer after a possible reallocation of its content

Returns -1 in case of error, 0 otherwise

Definition at line 1144 of file buf.c.

1145 {
1146 if (input == NULL)
1147 return(-1);
1148 if ((buf == NULL) || (buf->error)) {
1149 input->base = input->cur = input->end = BAD_CAST "";
1150 return(-1);
1151 }
1153 input->base = &buf->content[base];
1154 input->cur = input->base + cur;
1155 input->end = &buf->content[buf->use];
1156 return(0);
1157}
FxCollectionEntry * cur

Referenced by xmlCtxtResetPush().

◆ xmlBufShrink()

size_t xmlBufShrink ( xmlBufPtr  buf,
size_t  len 
)

xmlBufShrink: @buf: the buffer to dump @len: the number of xmlChar to remove

Remove the beginning of an XML buffer. NOTE that this routine behaviour differs from xmlBufferShrink() as it will return 0 on error instead of -1 due to size_t being used as the return type.

Returns the number of byte removed or 0 in case of failure

Definition at line 381 of file buf.c.

381 {
382 if ((buf == NULL) || (buf->error != 0)) return(0);
384 if (len == 0) return(0);
385 if (len > buf->use) return(0);
386
387 buf->use -= len;
388 if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) ||
389 ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL))) {
390 /*
391 * we just move the content pointer, but also make sure
392 * the perceived buffer size has shrunk accordingly
393 */
394 buf->content += len;
395 buf->size -= len;
396
397 /*
398 * sometimes though it maybe be better to really shrink
399 * on IO buffers
400 */
401 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
402 size_t start_buf = buf->content - buf->contentIO;
403 if (start_buf >= buf->size) {
404 memmove(buf->contentIO, &buf->content[0], buf->use);
405 buf->content = buf->contentIO;
406 buf->content[buf->use] = 0;
407 buf->size += start_buf;
408 }
409 }
410 } else {
411 memmove(buf->content, &buf->content[len], buf->use);
412 buf->content[buf->use] = 0;
413 }
415 return(len);
416}

Referenced by xmlCharEncFirstLineInput(), xmlCharEncInput(), xmlParserInputShrink(), and xmlSwitchInputEncodingInt().

◆ xmlBufUse()

size_t xmlBufUse ( const xmlBufPtr  buf)

xmlBufUse: @buf: the buffer

Function to get the length of a buffer

Returns the length of data in the internal content

Definition at line 633 of file buf.c.

634{
635 if ((!buf) || (buf->error))
636 return 0;
638
639 return(buf->use);
640}

Referenced by xmlCharEncFirstLineInput(), xmlCharEncInput(), xmlParserInputGrow(), xmlParserInputShrink(), xmlSwitchInputEncodingInt(), and xsltSaveResultToString().

◆ xmlBufWriteQuotedString()

int xmlBufWriteQuotedString ( xmlBufPtr  buf,
const xmlChar string 
)

xmlBufWriteQuotedString: @buf: the XML buffer output @string: the string to add

routine which manage and grows an output buffer. This one writes a quoted or double quoted xmlChar string, checking first if it holds quote or double-quotes internally

Returns 0 if successful, a positive error code number otherwise and -1 in case of internal or API error.

Definition at line 921 of file buf.c.

921 {
922 const xmlChar *cur, *base;
923 if ((buf == NULL) || (buf->error))
924 return(-1);
926 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE)
927 return(-1);
928 if (xmlStrchr(string, '\"')) {
929 if (xmlStrchr(string, '\'')) {
930#ifdef DEBUG_BUFFER
932 "xmlBufWriteQuotedString: string contains quote and double-quotes !\n");
933#endif
934 xmlBufCCat(buf, "\"");
935 base = cur = string;
936 while(*cur != 0){
937 if(*cur == '"'){
938 if (base != cur)
940 xmlBufAdd(buf, BAD_CAST "&quot;", 6);
941 cur++;
942 base = cur;
943 }
944 else {
945 cur++;
946 }
947 }
948 if (base != cur)
950 xmlBufCCat(buf, "\"");
951 }
952 else{
953 xmlBufCCat(buf, "\'");
954 xmlBufCat(buf, string);
955 xmlBufCCat(buf, "\'");
956 }
957 } else {
958 xmlBufCCat(buf, "\"");
959 xmlBufCat(buf, string);
960 xmlBufCCat(buf, "\"");
961 }
962 return(0);
963}
int xmlBufCCat(xmlBufPtr buf, const char *str)
Definition: buf.c:904
char string[160]
Definition: util.h:11
XMLPUBFUN const xmlChar *XMLCALL xmlStrchr(const xmlChar *str, xmlChar val)
Definition: xmlstring.c:325