This repository has been archived by the owner on Nov 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
/
buf.c
1275 lines (1126 loc) · 28.8 KB
/
buf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* buf.c: memory buffers for libxml2
*
* new buffer structures and entry points to simplify the maintenance
* of libxml2 and ensure we keep good control over memory allocations
* and stay 64 bits clean.
* The new entry point use the xmlBufPtr opaque structure and
* xmlBuf...() counterparts to the old xmlBuf...() functions
*
* See Copyright for the status of this software.
*
*/
#define IN_LIBXML
#include "libxml.h"
#include <string.h>
#include <limits.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include "private/buf.h"
#ifndef SIZE_MAX
#define SIZE_MAX ((size_t) -1)
#endif
#define WITH_BUFFER_COMPAT
#define BUF_FLAG_OOM (1u << 0)
#define BUF_FLAG_OVERFLOW (1u << 1)
#define BUF_FLAG_STATIC (1u << 2)
#define BUF_ERROR(buf) ((buf)->flags & (BUF_FLAG_OOM | BUF_FLAG_OVERFLOW))
#define BUF_STATIC(buf) ((buf)->flags & BUF_FLAG_STATIC)
/**
* xmlBuf:
*
* A buffer structure. The base of the structure is somehow compatible
* with struct _xmlBuffer to limit risks on application which accessed
* directly the input->buf->buffer structures.
*/
struct _xmlBuf {
xmlChar *content; /* The buffer content UTF8 */
#ifdef WITH_BUFFER_COMPAT
unsigned int compat_use; /* for binary compatibility */
unsigned int compat_size; /* for binary compatibility */
#endif
xmlChar *mem; /* Start of the allocation */
size_t use; /* The buffer size used */
size_t size; /* The buffer size, excluding terminating 0 */
size_t maxSize; /* The maximum buffer size */
unsigned flags; /* flags */
};
#ifdef WITH_BUFFER_COMPAT
/*
* Macro for compatibility with xmlBuffer to be used after an xmlBuf
* is updated. This makes sure the compat fields are updated too.
*/
#define UPDATE_COMPAT(buf) \
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;
/*
* Macro for compatibility with xmlBuffer to be used in all the xmlBuf
* entry points, it checks that the compat fields have not been modified
* by direct call to xmlBuffer function from code compiled before 2.9.0 .
*/
#define CHECK_COMPAT(buf) \
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;
#else /* ! WITH_BUFFER_COMPAT */
#define UPDATE_COMPAT(buf)
#define CHECK_COMPAT(buf)
#endif /* WITH_BUFFER_COMPAT */
/**
* xmlBufMemoryError:
* @extra: extra information
*
* Handle an out of memory condition
* To be improved...
*/
static void
xmlBufMemoryError(xmlBufPtr buf)
{
if (!BUF_ERROR(buf))
buf->flags |= BUF_FLAG_OOM;
}
/**
* xmlBufOverflowError:
* @extra: extra information
*
* Handle a buffer overflow error
* To be improved...
*/
static void
xmlBufOverflowError(xmlBufPtr buf)
{
if (!BUF_ERROR(buf))
buf->flags |= BUF_FLAG_OVERFLOW;
}
/**
* xmlBufCreate:
* @size: initial size of buffer
*
* routine to create an XML buffer.
* returns the new structure.
*/
xmlBufPtr
xmlBufCreate(size_t size) {
xmlBufPtr ret;
if (size == SIZE_MAX)
return(NULL);
ret = xmlMalloc(sizeof(*ret));
if (ret == NULL)
return(NULL);
ret->use = 0;
ret->flags = 0;
ret->size = size;
ret->maxSize = SIZE_MAX - 1;
ret->mem = xmlMalloc(ret->size + 1);
if (ret->mem == NULL) {
xmlFree(ret);
return(NULL);
}
ret->content = ret->mem;
ret->content[0] = 0;
UPDATE_COMPAT(ret);
return(ret);
}
/**
* xmlBufCreateMem:
* @mem: a memory area
* @size: size of the buffer excluding terminator
* @isStatic: whether the memory area is static
*
* Create a buffer initialized with memory.
*
* If @isStatic is set, uses the memory area directly as backing store.
* The memory must be zero-terminated and not be modified for the
* lifetime of the buffer. A static buffer can't be grown, modified or
* detached, but it can be shrunk.
*
* Returns a new buffer.
*/
xmlBufPtr
xmlBufCreateMem(const xmlChar *mem, size_t size, int isStatic) {
xmlBufPtr ret;
if (mem == NULL)
return(NULL);
ret = xmlMalloc(sizeof(*ret));
if (ret == NULL)
return(NULL);
if (isStatic) {
/* Check that memory is zero-terminated */
if (mem[size] != 0) {
xmlFree(ret);
return(NULL);
}
ret->flags = BUF_FLAG_STATIC;
ret->mem = (xmlChar *) mem;
} else {
ret->flags = 0;
ret->mem = xmlMalloc(size + 1);
if (ret->mem == NULL) {
xmlFree(ret);
return(NULL);
}
memcpy(ret->mem, mem, size);
ret->mem[size] = 0;
}
ret->use = size;
ret->size = size;
ret->maxSize = SIZE_MAX - 1;
ret->content = ret->mem;
UPDATE_COMPAT(ret);
return(ret);
}
/**
* 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.
*/
xmlChar *
xmlBufDetach(xmlBufPtr buf) {
xmlChar *ret;
if ((buf == NULL) || (BUF_ERROR(buf)) || (BUF_STATIC(buf)))
return(NULL);
if (buf->content != buf->mem) {
ret = xmlStrndup(buf->content, buf->use);
xmlFree(buf->mem);
} else {
ret = buf->mem;
}
buf->content = NULL;
buf->mem = NULL;
buf->size = 0;
buf->use = 0;
UPDATE_COMPAT(buf);
return ret;
}
/**
* xmlBufFree:
* @buf: the buffer to free
*
* Frees an XML buffer. It frees both the content and the structure which
* encapsulate it.
*/
void
xmlBufFree(xmlBufPtr buf) {
if (buf == NULL)
return;
if (!BUF_STATIC(buf))
xmlFree(buf->mem);
xmlFree(buf);
}
/**
* xmlBufEmpty:
* @buf: the buffer
*
* empty a buffer.
*/
void
xmlBufEmpty(xmlBufPtr buf) {
if ((buf == NULL) || (BUF_ERROR(buf)) || (BUF_STATIC(buf)))
return;
if (buf->mem == NULL)
return;
CHECK_COMPAT(buf)
buf->use = 0;
buf->size += buf->content - buf->mem;
buf->content = buf->mem;
buf->content[0] = 0;
UPDATE_COMPAT(buf)
}
/**
* xmlBufShrink:
* @buf: the buffer to dump
* @len: the number of xmlChar to remove
*
* DEPRECATED: Don't use.
*
* 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
*/
size_t
xmlBufShrink(xmlBufPtr buf, size_t len) {
if ((buf == NULL) || (BUF_ERROR(buf)))
return(0);
if (len == 0)
return(0);
CHECK_COMPAT(buf)
if (len > buf->use)
return(0);
buf->use -= len;
buf->content += len;
buf->size -= len;
UPDATE_COMPAT(buf)
return(len);
}
/**
* xmlBufGrowInternal:
* @buf: the buffer
* @len: the minimum free size to allocate
*
* Grow the available space of an XML buffer, @len is the target value
*
* Returns 0 on success, -1 in case of error
*/
static int
xmlBufGrowInternal(xmlBufPtr buf, size_t len) {
size_t size;
size_t start;
xmlChar *newbuf;
/*
* If there's enough space at the start of the buffer,
* move the contents.
*/
start = buf->content - buf->mem;
if (len <= start + buf->size - buf->use) {
memmove(buf->mem, buf->content, buf->use + 1);
buf->size += start;
buf->content = buf->mem;
return(0);
}
if (len > buf->maxSize - buf->use) {
xmlBufOverflowError(buf);
return(-1);
}
if (buf->size > (size_t) len) {
if (buf->size <= buf->maxSize / 2)
size = buf->size * 2;
else
size = buf->maxSize;
} else {
size = buf->use + len;
if (size <= buf->maxSize - 100)
size += 100;
}
if (buf->content == buf->mem) {
newbuf = xmlRealloc(buf->mem, size + 1);
if (newbuf == NULL) {
xmlBufMemoryError(buf);
return(-1);
}
} else {
newbuf = xmlMalloc(size + 1);
if (newbuf == NULL) {
xmlBufMemoryError(buf);
return(-1);
}
if (buf->content != NULL)
memcpy(newbuf, buf->content, buf->use + 1);
xmlFree(buf->mem);
}
buf->mem = newbuf;
buf->content = newbuf;
buf->size = size;
return(0);
}
/**
* 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 0 on succes, -1 in case of error
*/
int
xmlBufGrow(xmlBufPtr buf, size_t len) {
if ((buf == NULL) || (BUF_ERROR(buf)) || (BUF_STATIC(buf)))
return(-1);
CHECK_COMPAT(buf)
if (len <= buf->size - buf->use)
return(0);
if (xmlBufGrowInternal(buf, len) < 0)
return(-1);
UPDATE_COMPAT(buf)
return(0);
}
/**
* xmlBufContent:
* @buf: the buffer
*
* Function to extract the content of a buffer
*
* Returns the internal content
*/
xmlChar *
xmlBufContent(const xmlBuf *buf)
{
if ((!buf) || (BUF_ERROR(buf)))
return NULL;
return(buf->content);
}
/**
* 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
*/
xmlChar *
xmlBufEnd(xmlBufPtr buf)
{
if ((!buf) || (BUF_ERROR(buf)))
return NULL;
CHECK_COMPAT(buf)
return(&buf->content[buf->use]);
}
/**
* 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
*/
int
xmlBufAddLen(xmlBufPtr buf, size_t len) {
if ((buf == NULL) || (BUF_ERROR(buf)) || (BUF_STATIC(buf)))
return(-1);
CHECK_COMPAT(buf)
if (len > buf->size - buf->use)
return(-1);
buf->use += len;
buf->content[buf->use] = 0;
UPDATE_COMPAT(buf)
return(0);
}
/**
* xmlBufUse:
* @buf: the buffer
*
* Function to get the length of a buffer
*
* Returns the length of data in the internal content
*/
size_t
xmlBufUse(const xmlBufPtr buf)
{
if ((!buf) || (BUF_ERROR(buf)))
return 0;
CHECK_COMPAT(buf)
return(buf->use);
}
/**
* 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.
*/
size_t
xmlBufAvail(const xmlBufPtr buf)
{
if ((!buf) || (BUF_ERROR(buf)))
return 0;
CHECK_COMPAT(buf)
return(buf->size - buf->use);
}
/**
* xmlBufIsEmpty:
* @buf: the buffer
*
* Tell if a buffer is empty
*
* Returns 0 if no, 1 if yes and -1 in case of error
*/
int
xmlBufIsEmpty(const xmlBufPtr buf)
{
if ((!buf) || (BUF_ERROR(buf)))
return(-1);
CHECK_COMPAT(buf)
return(buf->use == 0);
}
/**
* 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 if successful, -1 in case of error.
*/
int
xmlBufAdd(xmlBufPtr buf, const xmlChar *str, size_t len) {
if ((buf == NULL) || (BUF_ERROR(buf)) || (BUF_STATIC(buf)))
return(-1);
if (len == 0)
return(0);
if (str == NULL)
return(-1);
CHECK_COMPAT(buf)
if (len > buf->size - buf->use) {
if (xmlBufGrowInternal(buf, len) < 0)
return(-1);
}
memmove(&buf->content[buf->use], str, len);
buf->use += len;
buf->content[buf->use] = 0;
UPDATE_COMPAT(buf)
return(0);
}
/**
* xmlBufCat:
* @buf: the buffer to add to
* @str: the #xmlChar string (optional)
*
* 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.
*/
int
xmlBufCat(xmlBufPtr buf, const xmlChar *str) {
if (str == NULL)
return(0);
return(xmlBufAdd(buf, str, strlen((const char *) str)));
}
/**
* 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
*/
xmlBufPtr
xmlBufFromBuffer(xmlBufferPtr buffer) {
xmlBufPtr ret;
if (buffer == NULL)
return(NULL);
ret = xmlMalloc(sizeof(xmlBuf));
if (ret == NULL)
return(NULL);
ret->use = buffer->use;
ret->flags = 0;
ret->maxSize = SIZE_MAX - 1;
if (buffer->content == NULL) {
ret->size = 50;
ret->mem = xmlMalloc(ret->size + 1);
ret->content = ret->mem;
if (ret->mem == NULL)
xmlBufMemoryError(ret);
else
ret->content[0] = 0;
} else {
ret->size = buffer->size - 1;
ret->content = buffer->content;
if (buffer->alloc == XML_BUFFER_ALLOC_IO)
ret->mem = buffer->contentIO;
else
ret->mem = buffer->content;
}
UPDATE_COMPAT(ret);
return(ret);
}
/**
* 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
*/
int
xmlBufBackToBuffer(xmlBufPtr buf, xmlBufferPtr ret) {
if ((buf == NULL) || (ret == NULL))
return(-1);
if ((BUF_ERROR(buf)) || (BUF_STATIC(buf)) ||
(buf->use >= INT_MAX)) {
xmlBufFree(buf);
ret->content = NULL;
ret->contentIO = NULL;
ret->use = 0;
ret->size = 0;
return(-1);
}
ret->use = buf->use;
if (buf->size >= INT_MAX) {
/* Keep the buffer but provide a truncated size value. */
ret->size = INT_MAX;
} else {
ret->size = buf->size + 1;
}
ret->alloc = XML_BUFFER_ALLOC_IO;
ret->content = buf->content;
ret->contentIO = buf->mem;
xmlFree(buf);
return(0);
}
/**
* 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
*/
int
xmlBufResetInput(xmlBufPtr buf, xmlParserInputPtr input) {
return(xmlBufUpdateInput(buf, input, 0));
}
/**
* xmlBufUpdateInput:
* @buf: an xmlBufPtr
* @input: an xmlParserInputPtr
* @pos: 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
*/
int
xmlBufUpdateInput(xmlBufPtr buf, xmlParserInputPtr input, size_t pos) {
if ((buf == NULL) || (input == NULL))
return(-1);
CHECK_COMPAT(buf)
input->base = buf->content;
input->cur = input->base + pos;
input->end = &buf->content[buf->use];
return(0);
}
/************************************************************************
* *
* Old buffer implementation *
* *
************************************************************************/
/**
* xmlSetBufferAllocationScheme:
* @scheme: allocation method to use
*
* DEPRECATED: Use xmlBufferSetAllocationScheme.
*
* Set the buffer allocation method. Types are
* XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
* XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
* improves performance
*/
void
xmlSetBufferAllocationScheme(xmlBufferAllocationScheme scheme ATTRIBUTE_UNUSED) {
}
/**
* xmlGetBufferAllocationScheme:
*
* DEPRECATED: Use xmlBufferSetAllocationScheme.
*
* Types are
* XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
* XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
* improves performance
* XML_BUFFER_ALLOC_HYBRID - use exact sizes on small strings to keep memory usage tight
* in normal usage, and doubleit on large strings to avoid
* pathological performance.
*
* Returns the current allocation scheme
*/
xmlBufferAllocationScheme
xmlGetBufferAllocationScheme(void) {
return(XML_BUFFER_ALLOC_EXACT);
}
/**
* xmlBufferCreate:
*
* routine to create an XML buffer.
* returns the new structure.
*/
xmlBufferPtr
xmlBufferCreate(void) {
xmlBufferPtr ret;
ret = xmlMalloc(sizeof(*ret));
if (ret == NULL)
return(NULL);
ret->use = 0;
ret->size = 256;
ret->alloc = XML_BUFFER_ALLOC_IO;
ret->contentIO = xmlMalloc(ret->size);
if (ret->contentIO == NULL) {
xmlFree(ret);
return(NULL);
}
ret->content = ret->contentIO;
ret->content[0] = 0;
return(ret);
}
/**
* xmlBufferCreateSize:
* @size: initial size of buffer
*
* routine to create an XML buffer.
* returns the new structure.
*/
xmlBufferPtr
xmlBufferCreateSize(size_t size) {
xmlBufferPtr ret;
if (size >= INT_MAX)
return(NULL);
ret = xmlMalloc(sizeof(*ret));
if (ret == NULL)
return(NULL);
ret->use = 0;
ret->alloc = XML_BUFFER_ALLOC_IO;
ret->size = (size ? size + 1 : 0); /* +1 for ending null */
if (ret->size) {
ret->contentIO = xmlMalloc(ret->size);
if (ret->contentIO == NULL) {
xmlFree(ret);
return(NULL);
}
ret->content = ret->contentIO;
ret->content[0] = 0;
} else {
ret->contentIO = NULL;
ret->content = NULL;
}
return(ret);
}
/**
* xmlBufferDetach:
* @buf: the buffer
*
* Remove the string contained in a buffer and gie 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.
*/
xmlChar *
xmlBufferDetach(xmlBufferPtr buf) {
xmlChar *ret;
if (buf == NULL)
return(NULL);
if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
(buf->content != buf->contentIO)) {
ret = xmlStrndup(buf->content, buf->use);
xmlFree(buf->contentIO);
} else {
ret = buf->content;
}
buf->contentIO = NULL;
buf->content = NULL;
buf->size = 0;
buf->use = 0;
return ret;
}
/**
* xmlBufferCreateStatic:
* @mem: the memory area
* @size: the size in byte
*
* Returns an XML buffer initialized with bytes.
*/
xmlBufferPtr
xmlBufferCreateStatic(void *mem, size_t size) {
xmlBufferPtr buf = xmlBufferCreateSize(size);
xmlBufferAdd(buf, mem, size);
return(buf);
}
/**
* xmlBufferSetAllocationScheme:
* @buf: the buffer to tune
* @scheme: allocation scheme to use
*
* Sets the allocation scheme for this buffer.
*
* For libxml2 before 2.14, it is recommended to set this to
* XML_BUFFER_ALLOC_DOUBLE_IT. Has no effect on 2.14 or later.
*/
void
xmlBufferSetAllocationScheme(xmlBufferPtr buf ATTRIBUTE_UNUSED,
xmlBufferAllocationScheme scheme ATTRIBUTE_UNUSED) {
}
/**
* xmlBufferFree:
* @buf: the buffer to free
*
* Frees an XML buffer. It frees both the content and the structure which
* encapsulate it.
*/
void
xmlBufferFree(xmlBufferPtr buf) {
if (buf == NULL)
return;
if (buf->alloc == XML_BUFFER_ALLOC_IO)
xmlFree(buf->contentIO);
else
xmlFree(buf->content);
xmlFree(buf);
}
/**
* xmlBufferEmpty:
* @buf: the buffer
*
* empty a buffer.
*/
void
xmlBufferEmpty(xmlBufferPtr buf) {
if (buf == NULL)
return;
if (buf->content == NULL)
return;
buf->use = 0;
if (buf->alloc == XML_BUFFER_ALLOC_IO) {
buf->size += buf->content - buf->contentIO;
buf->content = buf->contentIO;
buf->content[0] = 0;
} else {
buf->content[0] = 0;
}
}
/**
* xmlBufferShrink:
* @buf: the buffer to dump
* @len: the number of xmlChar to remove
*
* DEPRECATED: Don't use.
*
* Remove the beginning of an XML buffer.
*
* Returns the number of #xmlChar removed, or -1 in case of failure.
*/
int
xmlBufferShrink(xmlBufferPtr buf, unsigned int len) {
if (buf == NULL)
return(-1);
if (len == 0)
return(0);
if (len > buf->use)
return(-1);
buf->use -= len;
if (buf->alloc == XML_BUFFER_ALLOC_IO) {
buf->content += len;
buf->size -= len;
} else {
memmove(buf->content, &buf->content[len], buf->use + 1);
}
return(len);
}
/**
* xmlBufferGrow:
* @buf: the buffer
* @len: the minimum free size to allocate
*
* DEPRECATED: Don't use.
*
* Grow the available space of an XML buffer.
*
* Returns the new available space or -1 in case of error
*/
int
xmlBufferGrow(xmlBufferPtr buf, unsigned int len) {
unsigned int size;
xmlChar *newbuf;
if (buf == NULL)
return(-1);
if (len < buf->size - buf->use)
return(0);
if (len >= INT_MAX - buf->use)
return(-1);
if (buf->size > (size_t) len) {
if (buf->size <= INT_MAX / 2)
size = buf->size * 2;
else
size = INT_MAX;
} else {
size = buf->use + len + 1;
if (size <= INT_MAX - 100)
size += 100;
}
if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
(buf->content != buf->contentIO)) {
newbuf = xmlMalloc(size);
if (newbuf == NULL)
return(-1);
if (buf->content != NULL)
memcpy(newbuf, buf->content, buf->use + 1);
xmlFree(buf->contentIO);
} else {
newbuf = xmlRealloc(buf->content, size);
if (newbuf == NULL)
return(-1);
}
if (buf->alloc == XML_BUFFER_ALLOC_IO)
buf->contentIO = newbuf;
buf->content = newbuf;
buf->size = size;
return(buf->size - buf->use - 1);
}