Home | History | Annotate | Download | only in libxml2

Lines Matching refs:buf

2  * buf.c: memory buffers for libxml2
31 #include "buf.h"
40 * directly the input->buf->buffer structures.
60 #define UPDATE_COMPAT(buf) \
61 if (buf->size < INT_MAX) buf->compat_size = buf->size; \
62 else buf->compat_size = INT_MAX; \
63 if (buf->use < INT_MAX) buf->compat_use = buf->use; \
64 else buf->compat_use = INT_MAX;
71 #define CHECK_COMPAT(buf) \
72 if (buf->size != (size_t) buf->compat_size) \
73 if (buf->compat_size < INT_MAX) \
74 buf->size = buf->compat_size; \
75 if (buf->use != (size_t) buf->compat_use) \
76 if (buf->compat_use < INT_MAX) \
77 buf->use = buf->compat_use;
80 #define UPDATE_COMPAT(buf)
81 #define CHECK_COMPAT(buf)
92 xmlBufMemoryError(xmlBufPtr buf, const char *extra)
95 if ((buf) && (buf->error == 0))
96 buf->error = XML_ERR_NO_MEMORY;
107 xmlBufOverflowError(xmlBufPtr buf, const char *extra)
110 if ((buf) && (buf->error == 0))
111 buf->error = XML_BUF_OVERFLOW;
187 * @buf: the buffer
196 xmlBufDetach(xmlBufPtr buf) {
199 if (buf == NULL)
201 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE)
203 if (buf->buffer != NULL)
205 if (buf->error)
208 ret = buf->content;
209 buf->content = NULL;
210 buf->size = 0;
211 buf->use = 0;
212 buf->compat_use = 0;
213 buf->compat_size = 0;
260 * @buf: the buffer
267 xmlBufGetAllocationScheme(xmlBufPtr buf) {
268 if (buf == NULL) {
271 "xmlBufGetAllocationScheme: buf == NULL\n");
275 return(buf->alloc);
280 * @buf: the buffer to tune
288 xmlBufSetAllocationScheme(xmlBufPtr buf,
290 if ((buf == NULL) || (buf->error != 0)) {
293 "xmlBufSetAllocationScheme: buf == NULL or in error\n");
297 if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) ||
298 (buf->alloc == XML_BUFFER_ALLOC_IO))
305 buf->alloc = scheme;
306 if (buf->buffer)
307 buf->buffer->alloc = scheme;
315 buf->alloc = XML_BUFFER_ALLOC_IO;
316 buf->contentIO = buf->content;
323 * @buf: the buffer to free
329 xmlBufFree(xmlBufPtr buf) {
330 if (buf == NULL) {
333 "xmlBufFree: buf == NULL\n");
338 if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
339 (buf->contentIO != NULL)) {
340 xmlFree(buf->contentIO);
341 } else if ((buf->content != NULL) &&
342 (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) {
343 xmlFree(buf->content);
345 xmlFree(buf);
350 * @buf: the buffer
355 xmlBufEmpty(xmlBufPtr buf) {
356 if ((buf == NULL) || (buf->error != 0)) return;
357 if (buf->content == NULL) return;
358 CHECK_COMPAT(buf)
359 buf->use = 0;
360 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) {
361 buf->content = BAD_CAST "";
362 } else if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
363 (buf->contentIO != NULL)) {
364 size_t start_buf = buf->content - buf->contentIO;
366 buf->size += start_buf;
367 buf->content = buf->contentIO;
368 buf->content[0] = 0;
370 buf->content[0] = 0;
372 UPDATE_COMPAT(buf)
377 * @buf: the buffer to dump
388 xmlBufShrink(xmlBufPtr buf, size_t len) {
389 if ((buf == NULL) || (buf->error != 0)) return(0);
390 CHECK_COMPAT(buf)
392 if (len > buf->use) return(0);
394 buf->use -= len;
395 if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) ||
396 ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL))) {
401 buf->content += len;
402 buf->size -= len;
408 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
409 size_t start_buf = buf->content - buf->contentIO;
410 if (start_buf >= buf->size) {
411 memmove(buf->contentIO, &buf->content[0], buf->use);
412 buf->content = buf->contentIO;
413 buf->content[buf->use] = 0;
414 buf->size += start_buf;
418 memmove(buf->content, &buf->content[len], buf->use);
419 buf->content[buf->use] = 0;
421 UPDATE_COMPAT(buf)
427 * @buf: the buffer
431 * Error checking should be done on buf->error since using the return
437 xmlBufGrowInternal(xmlBufPtr buf, size_t len) {
441 if ((buf == NULL) || (buf->error != 0)) return(0);
442 CHECK_COMPAT(buf)
444 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
445 if (buf->use + len < buf->size)
446 return(buf->size - buf->use);
455 if (buf->size > (size_t) len)
456 size = buf->size * 2;
458 size = buf->use + len + 100;
460 size = buf->use + len + 100;
463 if (buf->alloc == XML_BUFFER_ALLOC_BOUNDED) {
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");
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);
480 xmlBufMemoryError(buf, "growing buffer");
483 buf->contentIO = newbuf;
484 buf->content = newbuf + start_buf;
486 newbuf = (xmlChar *) xmlRealloc(buf->content, size);
488 xmlBufMemoryError(buf, "growing buffer");
491 buf->content = newbuf;
493 buf->size = size;
494 UPDATE_COMPAT(buf)
495 return(buf->size - buf->use);
500 * @buf: the buffer
509 xmlBufGrow(xmlBufPtr buf, int len) {
512 if ((buf == NULL) || (len < 0)) return(-1);
515 ret = xmlBufGrowInternal(buf, len);
516 if (buf->error != 0)
523 * @buf: the buffer
531 xmlBufInflate(xmlBufPtr buf, size_t len) {
532 if (buf == NULL) return(-1);
533 xmlBufGrowInternal(buf, len + buf->size);
534 if (buf->error)
542 * @buf: the buffer to dump
548 xmlBufDump(FILE *file, xmlBufPtr buf) {
551 if ((buf == NULL) || (buf->error != 0)) {
554 "xmlBufDump: buf == NULL or in error\n");
558 if (buf->content == NULL) {
561 "xmlBufDump: buf->content == NULL\n");
565 CHECK_COMPAT(buf)
568 ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file);
574 * @buf: the buffer
582 xmlBufContent(const xmlBuf *buf)
584 if ((!buf) || (buf->error))
587 return(buf->content);
592 * @buf: the buffer
600 xmlBufEnd(xmlBufPtr buf)
602 if ((!buf) || (buf->error))
604 CHECK_COMPAT(buf)
606 return(&buf->content[buf->use]);
611 * @buf: the buffer
621 xmlBufAddLen(xmlBufPtr buf, size_t len) {
622 if ((buf == NULL) || (buf->error))
624 CHECK_COMPAT(buf)
625 if (len > (buf->size - buf->use))
627 buf->use += len;
628 UPDATE_COMPAT(buf)
629 if (buf->size > buf->use)
630 buf->content[buf->use] = 0;
638 * @buf: the buffer
646 xmlBufErase(xmlBufPtr buf, size_t len) {
647 if ((buf == NULL) || (buf->error))
649 CHECK_COMPAT(buf)
650 buf->use)
652 buf->use -= len;
653 buf->content[buf->use] = 0;
654 UPDATE_COMPAT(buf)
660 * @buf: the buffer
668 xmlBufLength(const xmlBufPtr buf)
670 if ((!buf) || (buf->error))
672 CHECK_COMPAT(buf)
674 return(buf->use);
679 * @buf: the buffer
687 xmlBufUse(const xmlBufPtr buf)
689 if ((!buf) || (buf->error))
691 CHECK_COMPAT(buf)
693 return(buf->use);
698 * @buf: the buffer
708 xmlBufAvail(const xmlBufPtr buf)
710 if ((!buf) || (buf->error))
712 CHECK_COMPAT(buf)
714 return(buf->size - buf->use);
719 * @buf: the buffer
726 xmlBufIsEmpty(const xmlBufPtr buf)
728 if ((!buf) || (buf->error))
730 CHECK_COMPAT(buf)
732 return(buf->use == 0);
737 * @buf: the buffer to resize
745 xmlBufResize(xmlBufPtr buf, size_t size)
751 if ((buf == NULL) || (buf->error))
753 CHECK_COMPAT(buf)
755 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
756 if (buf->alloc == XML_BUFFER_ALLOC_BOUNDED) {
761 xmlBufMemoryError(buf, "buffer error: text too long\n");
767 if (size < buf->size)
771 switch (buf->alloc){
775 newSize = (buf->size ? buf->size*2 : size + 10);
778 xmlBufMemoryError(buf, "growing buffer");
788 if (buf->use < BASE_BUFFER_SIZE)
791 newSize = buf->size * 2;
794 xmlBufMemoryError(buf, "growing buffer");
807 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
808 start_buf = buf->content - buf->contentIO;
812 memmove(buf->contentIO, buf->content, buf->use);
813 buf->content = buf->contentIO;
814 buf->content[buf->use] = 0;
815 buf->size += start_buf;
817 rebuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + newSize);
819 xmlBufMemoryError(buf, "growing buffer");
822 buf->contentIO = rebuf;
823 buf->content = rebuf + start_buf;
826 if (buf->content == NULL) {
828 } else if (buf->size - buf->use < 100) {
829 rebuf = (xmlChar *) xmlRealloc(buf->content, newSize);
838 memcpy(rebuf, buf->content, buf->use);
839 xmlFree(buf->content);
840 rebuf[buf->use] = 0;
844 xmlBufMemoryError(buf, "growing buffer");
847 buf->content = rebuf;
849 buf->size = newSize;
850 UPDATE_COMPAT(buf)
857 * @buf: the buffer to dump
868 xmlBufAdd(xmlBufPtr buf, const xmlChar *str, int len) {
871 if ((str == NULL) || (buf == NULL) || (buf->error))
873 CHECK_COMPAT(buf)
875 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
891 needSize = buf->use + len + 2;
892 if (needSize > buf->size){
893 if (buf->alloc == XML_BUFFER_ALLOC_BOUNDED) {
898 xmlBufMemoryError(buf, "buffer error: text too long\n");
902 if (!xmlBufResize(buf, needSize)){
903 xmlBufMemoryError(buf, "growing buffer");
908 memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
909 buf->use += len;
910 buf->content[buf->use] = 0;
911 UPDATE_COMPAT(buf)
917 * @buf: the buffer
928 xmlBufAddHead(xmlBufPtr buf, const xmlChar *str, int len) {
931 if ((buf == NULL) || (buf->error))
933 CHECK_COMPAT(buf)
934 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
956 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
957 size_t start_buf = buf->content - buf->contentIO;
963 buf->content -= len;
964 memmove(&buf->content[0], str, len);
965 buf->use += len;
966 buf->size += len;
967 UPDATE_COMPAT(buf)
971 needSize = buf->use + len + 2;
972 if (needSize > buf->size){
973 if (buf->alloc == XML_BUFFER_ALLOC_BOUNDED) {
978 xmlBufMemoryError(buf, "buffer error: text too long\n");
982 if (!xmlBufResize(buf, needSize)){
983 xmlBufMemoryError(buf, "growing buffer");
988 memmove(&buf->content[len], &buf->content[0], buf->use);
989 memmove(&buf->content[0], str, len);
990 buf->use += len;
991 buf->content[buf->use] = 0;
992 UPDATE_COMPAT(buf)
998 * @buf: the buffer to add to
1007 xmlBufCat(xmlBufPtr buf, const xmlChar *str) {
1008 if ((buf == NULL) || (buf->error))
1010 CHECK_COMPAT(buf)
1011 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
1013 return xmlBufAdd(buf, str, -1);
1018 * @buf: the buffer to dump
1027 xmlBufCCat(xmlBufPtr buf, const char *str) {
1030 if ((buf == NULL) || (buf->error))
1032 CHECK_COMPAT(buf)
1033 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
1042 if (buf->use + 10 >= buf->size) {
1043 if (!xmlBufResize(buf, buf->use+10)){
1044 xmlBufMemoryError(buf, "growing buffer");
1048 buf->content[buf->use++] = *cur;
1050 buf->content[buf->use] = 0;
1051 UPDATE_COMPAT(buf)
1057 * @buf: the XML buffer
1067 xmlBufWriteCHAR(xmlBufPtr buf, const xmlChar *string) {
1068 if ((buf == NULL) || (buf->error))
1070 CHECK_COMPAT(buf)
1071 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE)
1073 return(xmlBufCat(buf, string));
1078 * @buf: the XML buffer output
1088 xmlBufWriteChar(xmlBufPtr buf, const char *string) {
1089 if ((buf == NULL) || (buf->error))
1091 CHECK_COMPAT(buf)
1092 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE)
1094 return(xmlBufCCat(buf, string));
1100 * @buf: the XML buffer output
1111 xmlBufWriteQuotedString(xmlBufPtr buf, const xmlChar *string) {
1113 if ((buf == NULL) || (buf->error))
1115 CHECK_COMPAT(buf)
1116 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE)
1124 xmlBufCCat(buf, "\"");
1129 xmlBufAdd(buf, base, cur - base);
1130 xmlBufAdd(buf, BAD_CAST "&quot;", 6);
1139 xmlBufAdd(buf, base, cur - base);
1140 xmlBufCCat(buf, "\"");
1143 xmlBufCCat(buf, "\'");
1144 xmlBufCat(buf, string);
1145 xmlBufCCat(buf, "\'");
1148 xmlBufCCat(buf, "\"");
1149 xmlBufCat(buf, string);
1150 xmlBufCCat(buf, "\"");
1193 * @buf: new buffer wrapping the old one
1199 * The xmlBufPtr @buf wrapper is deallocated by this call in any case.
1204 xmlBufBackToBuffer(xmlBufPtr buf) {
1207 if ((buf == NULL) || (buf->error))
1209 CHECK_COMPAT(buf)
1210 if (buf->buffer == NULL) {
1211 xmlBufFree(buf);
1215 ret = buf->buffer;
1219 if (buf->use > INT_MAX) {
1225 xmlBufOverflowError(buf, "Used size too big for xmlBuffer");
1228 } else if (buf->size > INT_MAX) {
1235 xmlBufOverflowError(buf, "Allocated size too big for xmlBuffer");
1238 ret->use = (int) buf->use;
1239 ret->size = (int) buf->size;
1240 ret->alloc = buf->alloc;
1241 ret->content = buf->content;
1242 ret->contentIO = buf->contentIO;
1243 xmlFree(buf);
1249 * @buf: an xmlBufPtr
1250 * @buffer: the buffer to consume into @buf
1252 * The content of @buffer is appended to @buf and @buffer is freed
1257 xmlBufMergeBuffer(xmlBufPtr buf, xmlBufferPtr buffer) {
1260 if ((buf == NULL) || (buf->error)) {
1264 CHECK_COMPAT(buf)
1267 ret = xmlBufAdd(buf, buffer->content, buffer->use);
1275 * @buf: an xmlBufPtr
1283 xmlBufResetInput(xmlBufPtr buf, xmlParserInputPtr input) {
1284 if ((input == NULL) || (buf == NULL) || (buf->error))
1286 CHECK_COMPAT(buf)
1287 input->base = input->cur = buf->content;
1288 input->end = &buf->content[buf->use];
1294 * @buf: an xmlBufPtr
1302 xmlBufGetInputBase(xmlBufPtr buf
1305 if ((input == NULL) || (buf == NULL) || (buf->error))
1307 CHECK_COMPAT(buf)
1308 base = input->base - buf->content;
1313 if (base > buf->size) {
1314 xmlBufOverflowError(buf, "Input reference outside of the buffer");
1322 * @buf: an xmlBufPtr
1333 xmlBufSetInputBaseCur(xmlBufPtr buf, xmlParserInputPtr input,
1335 if ((input == NULL) || (buf == NULL) || (buf->error))
1337 CHECK_COMPAT(buf)
1338 input->base = &buf->content[base];
1340 input->end = &buf->content[buf->use];