forked from pivasoftware/microxml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mxml-file.c
3075 lines (2423 loc) · 65.4 KB
/
mxml-file.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
/*
* File loading code for microxml, a micro XML-like file parsing library.
*
* Copyright 2003-2011 by Michael R Sweet.
* Copyright 2011-2012 by Luka Perkov
*
* These coded instructions, statements, and computer programs are the
* property of Michael R Sweet and Luka Perkov. They are protected by
* Federal copyright law. Distribution and use rights are outlined in
* the file "COPYING" which should have been included with this file.
* If this file is missing or damaged, see the license at:
*
* http://www.minixml.org/
*
* Contents:
*
* mxmlLoadFd() - Load a file descriptor into an XML node tree.
* mxmlLoadFile() - Load a file into an XML node tree.
* mxmlLoadString() - Load a string into an XML node tree.
* mxmlSaveAllocString() - Save an XML tree to an allocated string.
* mxmlSaveFd() - Save an XML tree to a file descriptor.
* mxmlSaveFile() - Save an XML tree to a file.
* mxmlSaveString() - Save an XML node tree to a string.
* mxmlSAXLoadFd() - Load a file descriptor into an XML node tree
* using a SAX callback.
* mxmlSAXLoadFile() - Load a file into an XML node tree
* using a SAX callback.
* mxmlSAXLoadString() - Load a string into an XML node tree
* using a SAX callback.
* mxmlSetCustomHandlers() - Set the handling functions for custom data.
* mxmlSetErrorCallback() - Set the error message callback.
* mxmlSetWrapMargin() - Set the wrap margin when saving XML data.
* mxml_add_char() - Add a character to a buffer, expanding as needed.
* mxml_fd_getc() - Read a character from a file descriptor.
* mxml_fd_putc() - Write a character to a file descriptor.
* mxml_fd_read() - Read a buffer of data from a file descriptor.
* mxml_fd_write() - Write a buffer of data to a file descriptor.
* mxml_file_getc() - Get a character from a file.
* mxml_file_putc() - Write a character to a file.
* mxml_get_entity() - Get the character corresponding to an entity...
* mxml_load_data() - Load data into an XML node tree.
* mxml_parse_element() - Parse an element for any attributes...
* mxml_string_getc() - Get a character from a string.
* mxml_string_putc() - Write a character to a string.
* mxml_write_name() - Write a name string.
* mxml_write_node() - Save an XML node to a file.
* mxml_write_string() - Write a string, escaping & and < as needed.
* mxml_write_ws() - Do whitespace callback...
*/
/*
* Include necessary headers...
*/
#ifndef WIN32
# include <unistd.h>
#endif /* !WIN32 */
#include "mxml-private.h"
/*
* Character encoding...
*/
#define ENCODE_UTF8 0 /* UTF-8 */
#define ENCODE_UTF16BE 1 /* UTF-16 Big-Endian */
#define ENCODE_UTF16LE 2 /* UTF-16 Little-Endian */
/*
* Macro to test for a bad XML character...
*/
#define mxml_bad_char(ch) ((ch) < ' ' && (ch) != '\n' && (ch) != '\r' && (ch) != '\t')
/*
* Types and structures...
*/
typedef int (*_mxml_getc_cb_t)(void *, int *);
typedef int (*_mxml_putc_cb_t)(int, void *);
typedef struct _mxml_fdbuf_s /**** File descriptor buffer ****/
{
int fd; /* File descriptor */
unsigned char *current, /* Current position in buffer */
*end, /* End of buffer */
buffer[8192]; /* Character buffer */
} _mxml_fdbuf_t;
/*
* Local functions...
*/
static int mxml_add_char(int ch, char **ptr, char **buffer,
int *bufsize);
static int mxml_fd_getc(void *p, int *encoding);
static int mxml_fd_putc(int ch, void *p);
static int mxml_fd_read(_mxml_fdbuf_t *buf);
static int mxml_fd_write(_mxml_fdbuf_t *buf);
static int mxml_file_getc(void *p, int *encoding);
static int mxml_file_putc(int ch, void *p);
static int mxml_get_entity(mxml_node_t *parent, void *p,
int *encoding,
_mxml_getc_cb_t getc_cb);
static inline int mxml_isspace(int ch)
{
return (ch == ' ' || ch == '\t' || ch == '\r' ||
ch == '\n');
}
static mxml_node_t *mxml_load_data(mxml_node_t *top, void *p,
mxml_load_cb_t cb,
_mxml_getc_cb_t getc_cb,
mxml_sax_cb_t sax_cb, void *sax_data);
static int mxml_parse_element(mxml_node_t *node, void *p,
int *encoding,
_mxml_getc_cb_t getc_cb);
static int mxml_string_getc(void *p, int *encoding);
static int mxml_string_putc(int ch, void *p);
static int mxml_write_name(const char *s, void *p,
_mxml_putc_cb_t putc_cb);
static int mxml_write_node(mxml_node_t *node, void *p,
mxml_save_cb_t cb, int col,
_mxml_putc_cb_t putc_cb,
_mxml_global_t *global);
static int mxml_write_string(const char *s, void *p,
_mxml_putc_cb_t putc_cb);
static int mxml_write_ws(mxml_node_t *node, void *p,
mxml_save_cb_t cb, int ws,
int col, _mxml_putc_cb_t putc_cb);
/*
* 'mxmlLoadFd()' - Load a file descriptor into an XML node tree.
*
* The nodes in the specified file are added to the specified top node.
* If no top node is provided, the XML file MUST be well-formed with a
* single parent node like <?xml> for the entire file. The callback
* function returns the value type that should be used for child nodes.
* If MXML_NO_CALLBACK is specified then all child nodes will be either
* MXML_ELEMENT or MXML_TEXT nodes.
*
* The constants MXML_INTEGER_CALLBACK, MXML_OPAQUE_CALLBACK,
* MXML_REAL_CALLBACK, and MXML_TEXT_CALLBACK are defined for loading
* child nodes of the specified type.
*/
mxml_node_t * /* O - First node or NULL if the file could not be read. */
mxmlLoadFd(mxml_node_t *top, /* I - Top node */
int fd, /* I - File descriptor to read from */
mxml_load_cb_t cb) /* I - Callback function or MXML_NO_CALLBACK */
{
_mxml_fdbuf_t buf; /* File descriptor buffer */
/*
* Initialize the file descriptor buffer...
*/
buf.fd = fd;
buf.current = buf.buffer;
buf.end = buf.buffer;
/*
* Read the XML data...
*/
return (mxml_load_data(top, &buf, cb, mxml_fd_getc, MXML_NO_CALLBACK, NULL));
}
/*
* 'mxmlLoadFile()' - Load a file into an XML node tree.
*
* The nodes in the specified file are added to the specified top node.
* If no top node is provided, the XML file MUST be well-formed with a
* single parent node like <?xml> for the entire file. The callback
* function returns the value type that should be used for child nodes.
* If MXML_NO_CALLBACK is specified then all child nodes will be either
* MXML_ELEMENT or MXML_TEXT nodes.
*
* The constants MXML_INTEGER_CALLBACK, MXML_OPAQUE_CALLBACK,
* MXML_REAL_CALLBACK, and MXML_TEXT_CALLBACK are defined for loading
* child nodes of the specified type.
*/
mxml_node_t * /* O - First node or NULL if the file could not be read. */
mxmlLoadFile(mxml_node_t *top, /* I - Top node */
FILE *fp, /* I - File to read from */
mxml_load_cb_t cb) /* I - Callback function or MXML_NO_CALLBACK */
{
/*
* Read the XML data...
*/
return (mxml_load_data(top, fp, cb, mxml_file_getc, MXML_NO_CALLBACK, NULL));
}
/*
* 'mxmlLoadString()' - Load a string into an XML node tree.
*
* The nodes in the specified string are added to the specified top node.
* If no top node is provided, the XML string MUST be well-formed with a
* single parent node like <?xml> for the entire string. The callback
* function returns the value type that should be used for child nodes.
* If MXML_NO_CALLBACK is specified then all child nodes will be either
* MXML_ELEMENT or MXML_TEXT nodes.
*
* The constants MXML_INTEGER_CALLBACK, MXML_OPAQUE_CALLBACK,
* MXML_REAL_CALLBACK, and MXML_TEXT_CALLBACK are defined for loading
* child nodes of the specified type.
*/
mxml_node_t * /* O - First node or NULL if the string has errors. */
mxmlLoadString(mxml_node_t *top, /* I - Top node */
const char *s, /* I - String to load */
mxml_load_cb_t cb) /* I - Callback function or MXML_NO_CALLBACK */
{
/*
* Read the XML data...
*/
return (mxml_load_data(top, (void *)&s, cb, mxml_string_getc, MXML_NO_CALLBACK,
NULL));
}
/*
* 'mxmlSaveAllocString()' - Save an XML tree to an allocated string.
*
* This function returns a pointer to a string containing the textual
* representation of the XML node tree. The string should be freed
* using the free() function when you are done with it. NULL is returned
* if the node would produce an empty string or if the string cannot be
* allocated.
*
* The callback argument specifies a function that returns a whitespace
* string or NULL before and after each element. If MXML_NO_CALLBACK
* is specified, whitespace will only be added before MXML_TEXT nodes
* with leading whitespace and before attribute names inside opening
* element tags.
*/
char * /* O - Allocated string or NULL */
mxmlSaveAllocString(
mxml_node_t *node, /* I - Node to write */
mxml_save_cb_t cb) /* I - Whitespace callback or MXML_NO_CALLBACK */
{
int bytes; /* Required bytes */
char buffer[8192]; /* Temporary buffer */
char *s; /* Allocated string */
/*
* Write the node to the temporary buffer...
*/
bytes = mxmlSaveString(node, buffer, sizeof(buffer), cb);
if (bytes <= 0)
return (NULL);
if (bytes < (int)(sizeof(buffer) - 1))
{
/*
* Node fit inside the buffer, so just duplicate that string and
* return...
*/
return (strdup(buffer));
}
/*
* Allocate a buffer of the required size and save the node to the
* new buffer...
*/
if ((s = malloc(bytes + 1)) == NULL)
return (NULL);
mxmlSaveString(node, s, bytes + 1, cb);
/*
* Return the allocated string...
*/
return (s);
}
/*
* 'mxmlSaveFd()' - Save an XML tree to a file descriptor.
*
* The callback argument specifies a function that returns a whitespace
* string or NULL before and after each element. If MXML_NO_CALLBACK
* is specified, whitespace will only be added before MXML_TEXT nodes
* with leading whitespace and before attribute names inside opening
* element tags.
*/
int /* O - 0 on success, -1 on error. */
mxmlSaveFd(mxml_node_t *node, /* I - Node to write */
int fd, /* I - File descriptor to write to */
mxml_save_cb_t cb) /* I - Whitespace callback or MXML_NO_CALLBACK */
{
int col; /* Final column */
_mxml_fdbuf_t buf; /* File descriptor buffer */
_mxml_global_t *global = _mxml_global();
/* Global data */
/*
* Initialize the file descriptor buffer...
*/
buf.fd = fd;
buf.current = buf.buffer;
buf.end = buf.buffer + sizeof(buf.buffer);
/*
* Write the node...
*/
if ((col = mxml_write_node(node, &buf, cb, 0, mxml_fd_putc, global)) < 0)
return (-1);
if (col > 0)
if (mxml_fd_putc('\n', &buf) < 0)
return (-1);
/*
* Flush and return...
*/
return (mxml_fd_write(&buf));
}
/*
* 'mxmlSaveFile()' - Save an XML tree to a file.
*
* The callback argument specifies a function that returns a whitespace
* string or NULL before and after each element. If MXML_NO_CALLBACK
* is specified, whitespace will only be added before MXML_TEXT nodes
* with leading whitespace and before attribute names inside opening
* element tags.
*/
int /* O - 0 on success, -1 on error. */
mxmlSaveFile(mxml_node_t *node, /* I - Node to write */
FILE *fp, /* I - File to write to */
mxml_save_cb_t cb) /* I - Whitespace callback or MXML_NO_CALLBACK */
{
int col; /* Final column */
_mxml_global_t *global = _mxml_global();
/* Global data */
/*
* Write the node...
*/
if ((col = mxml_write_node(node, fp, cb, 0, mxml_file_putc, global)) < 0)
return (-1);
if (col > 0)
if (putc('\n', fp) < 0)
return (-1);
/*
* Return 0 (success)...
*/
return (0);
}
/*
* 'mxmlSaveString()' - Save an XML node tree to a string.
*
* This function returns the total number of bytes that would be
* required for the string but only copies (bufsize - 1) characters
* into the specified buffer.
*
* The callback argument specifies a function that returns a whitespace
* string or NULL before and after each element. If MXML_NO_CALLBACK
* is specified, whitespace will only be added before MXML_TEXT nodes
* with leading whitespace and before attribute names inside opening
* element tags.
*/
int /* O - Size of string */
mxmlSaveString(mxml_node_t *node, /* I - Node to write */
char *buffer, /* I - String buffer */
int bufsize, /* I - Size of string buffer */
mxml_save_cb_t cb) /* I - Whitespace callback or MXML_NO_CALLBACK */
{
int col; /* Final column */
char *ptr[2]; /* Pointers for putc_cb */
_mxml_global_t *global = _mxml_global();
/* Global data */
/*
* Write the node...
*/
ptr[0] = buffer;
ptr[1] = buffer + bufsize;
if ((col = mxml_write_node(node, ptr, cb, 0, mxml_string_putc, global)) < 0)
return (-1);
if (col > 0)
mxml_string_putc('\n', ptr);
/*
* Nul-terminate the buffer...
*/
if (ptr[0] >= ptr[1])
buffer[bufsize - 1] = '\0';
else
ptr[0][0] = '\0';
/*
* Return the number of characters...
*/
return (ptr[0] - buffer);
}
/*
* 'mxmlSAXLoadFd()' - Load a file descriptor into an XML node tree
* using a SAX callback.
*
* The nodes in the specified file are added to the specified top node.
* If no top node is provided, the XML file MUST be well-formed with a
* single parent node like <?xml> for the entire file. The callback
* function returns the value type that should be used for child nodes.
* If MXML_NO_CALLBACK is specified then all child nodes will be either
* MXML_ELEMENT or MXML_TEXT nodes.
*
* The constants MXML_INTEGER_CALLBACK, MXML_OPAQUE_CALLBACK,
* MXML_REAL_CALLBACK, and MXML_TEXT_CALLBACK are defined for loading
* child nodes of the specified type.
*
* The SAX callback must call mxmlRetain() for any nodes that need to
* be kept for later use. Otherwise, nodes are deleted when the parent
* node is closed or after each data, comment, CDATA, or directive node.
*
* @since Mini-XML 2.3@
*/
mxml_node_t * /* O - First node or NULL if the file could not be read. */
mxmlSAXLoadFd(mxml_node_t *top, /* I - Top node */
int fd, /* I - File descriptor to read from */
mxml_load_cb_t cb, /* I - Callback function or MXML_NO_CALLBACK */
mxml_sax_cb_t sax_cb, /* I - SAX callback or MXML_NO_CALLBACK */
void *sax_data) /* I - SAX user data */
{
_mxml_fdbuf_t buf; /* File descriptor buffer */
/*
* Initialize the file descriptor buffer...
*/
buf.fd = fd;
buf.current = buf.buffer;
buf.end = buf.buffer;
/*
* Read the XML data...
*/
return (mxml_load_data(top, &buf, cb, mxml_fd_getc, sax_cb, sax_data));
}
/*
* 'mxmlSAXLoadFile()' - Load a file into an XML node tree
* using a SAX callback.
*
* The nodes in the specified file are added to the specified top node.
* If no top node is provided, the XML file MUST be well-formed with a
* single parent node like <?xml> for the entire file. The callback
* function returns the value type that should be used for child nodes.
* If MXML_NO_CALLBACK is specified then all child nodes will be either
* MXML_ELEMENT or MXML_TEXT nodes.
*
* The constants MXML_INTEGER_CALLBACK, MXML_OPAQUE_CALLBACK,
* MXML_REAL_CALLBACK, and MXML_TEXT_CALLBACK are defined for loading
* child nodes of the specified type.
*
* The SAX callback must call mxmlRetain() for any nodes that need to
* be kept for later use. Otherwise, nodes are deleted when the parent
* node is closed or after each data, comment, CDATA, or directive node.
*
* @since Mini-XML 2.3@
*/
mxml_node_t * /* O - First node or NULL if the file could not be read. */
mxmlSAXLoadFile(
mxml_node_t *top, /* I - Top node */
FILE *fp, /* I - File to read from */
mxml_load_cb_t cb, /* I - Callback function or MXML_NO_CALLBACK */
mxml_sax_cb_t sax_cb, /* I - SAX callback or MXML_NO_CALLBACK */
void *sax_data) /* I - SAX user data */
{
/*
* Read the XML data...
*/
return (mxml_load_data(top, fp, cb, mxml_file_getc, sax_cb, sax_data));
}
/*
* 'mxmlSAXLoadString()' - Load a string into an XML node tree
* using a SAX callback.
*
* The nodes in the specified string are added to the specified top node.
* If no top node is provided, the XML string MUST be well-formed with a
* single parent node like <?xml> for the entire string. The callback
* function returns the value type that should be used for child nodes.
* If MXML_NO_CALLBACK is specified then all child nodes will be either
* MXML_ELEMENT or MXML_TEXT nodes.
*
* The constants MXML_INTEGER_CALLBACK, MXML_OPAQUE_CALLBACK,
* MXML_REAL_CALLBACK, and MXML_TEXT_CALLBACK are defined for loading
* child nodes of the specified type.
*
* The SAX callback must call mxmlRetain() for any nodes that need to
* be kept for later use. Otherwise, nodes are deleted when the parent
* node is closed or after each data, comment, CDATA, or directive node.
*
* @since Mini-XML 2.3@
*/
mxml_node_t * /* O - First node or NULL if the string has errors. */
mxmlSAXLoadString(
mxml_node_t *top, /* I - Top node */
const char *s, /* I - String to load */
mxml_load_cb_t cb, /* I - Callback function or MXML_NO_CALLBACK */
mxml_sax_cb_t sax_cb, /* I - SAX callback or MXML_NO_CALLBACK */
void *sax_data) /* I - SAX user data */
{
/*
* Read the XML data...
*/
return (mxml_load_data(top, (void *)&s, cb, mxml_string_getc, sax_cb, sax_data));
}
/*
* 'mxmlSetCustomHandlers()' - Set the handling functions for custom data.
*
* The load function accepts a node pointer and a data string and must
* return 0 on success and non-zero on error.
*
* The save function accepts a node pointer and must return a malloc'd
* string on success and NULL on error.
*
*/
void
mxmlSetCustomHandlers(
mxml_custom_load_cb_t load, /* I - Load function */
mxml_custom_save_cb_t save) /* I - Save function */
{
_mxml_global_t *global = _mxml_global();
/* Global data */
global->custom_load_cb = load;
global->custom_save_cb = save;
}
/*
* 'mxmlSetErrorCallback()' - Set the error message callback.
*/
void
mxmlSetErrorCallback(mxml_error_cb_t cb)/* I - Error callback function */
{
_mxml_global_t *global = _mxml_global();
/* Global data */
global->error_cb = cb;
}
/*
* 'mxmlSetWrapMargin()' - Set the wrap margin when saving XML data.
*
* Wrapping is disabled when "column" is 0.
*
* @since Mini-XML 2.3@
*/
void
mxmlSetWrapMargin(int column) /* I - Column for wrapping, 0 to disable wrapping */
{
_mxml_global_t *global = _mxml_global();
/* Global data */
global->wrap = column;
}
/*
* 'mxml_add_char()' - Add a character to a buffer, expanding as needed.
*/
static int /* O - 0 on success, -1 on error */
mxml_add_char(int ch, /* I - Character to add */
char **bufptr, /* IO - Current position in buffer */
char **buffer, /* IO - Current buffer */
int *bufsize) /* IO - Current buffer size */
{
char *newbuffer; /* New buffer value */
if (*bufptr >= (*buffer + *bufsize - 4))
{
/*
* Increase the size of the buffer...
*/
if (*bufsize < 1024)
(*bufsize) *= 2;
else
(*bufsize) += 1024;
if ((newbuffer = realloc(*buffer, *bufsize)) == NULL)
{
free(*buffer);
mxml_error("Unable to expand string buffer to %d bytes!", *bufsize);
return (-1);
}
*bufptr = newbuffer + (*bufptr - *buffer);
*buffer = newbuffer;
}
if (ch < 0x80)
{
/*
* Single byte ASCII...
*/
*(*bufptr)++ = ch;
}
else if (ch < 0x800)
{
/*
* Two-byte UTF-8...
*/
*(*bufptr)++ = 0xc0 | (ch >> 6);
*(*bufptr)++ = 0x80 | (ch & 0x3f);
}
else if (ch < 0x10000)
{
/*
* Three-byte UTF-8...
*/
*(*bufptr)++ = 0xe0 | (ch >> 12);
*(*bufptr)++ = 0x80 | ((ch >> 6) & 0x3f);
*(*bufptr)++ = 0x80 | (ch & 0x3f);
}
else
{
/*
* Four-byte UTF-8...
*/
*(*bufptr)++ = 0xf0 | (ch >> 18);
*(*bufptr)++ = 0x80 | ((ch >> 12) & 0x3f);
*(*bufptr)++ = 0x80 | ((ch >> 6) & 0x3f);
*(*bufptr)++ = 0x80 | (ch & 0x3f);
}
return (0);
}
/*
* 'mxml_fd_getc()' - Read a character from a file descriptor.
*/
static int /* O - Character or EOF */
mxml_fd_getc(void *p, /* I - File descriptor buffer */
int *encoding) /* IO - Encoding */
{
_mxml_fdbuf_t *buf; /* File descriptor buffer */
int ch, /* Current character */
temp; /* Temporary character */
/*
* Grab the next character in the buffer...
*/
buf = (_mxml_fdbuf_t *)p;
if (buf->current >= buf->end)
if (mxml_fd_read(buf) < 0)
return (EOF);
ch = *(buf->current)++;
switch (*encoding)
{
case ENCODE_UTF8 :
/*
* Got a UTF-8 character; convert UTF-8 to Unicode and return...
*/
if (!(ch & 0x80))
{
#if DEBUG > 1
printf("mxml_fd_getc: %c (0x%04x)\n", ch < ' ' ? '.' : ch, ch);
#endif /* DEBUG > 1 */
if (mxml_bad_char(ch))
{
mxml_error("Bad control character 0x%02x not allowed by XML standard!",
ch);
return (EOF);
}
return (ch);
}
else if (ch == 0xfe)
{
/*
* UTF-16 big-endian BOM?
*/
if (buf->current >= buf->end)
if (mxml_fd_read(buf) < 0)
return (EOF);
ch = *(buf->current)++;
if (ch != 0xff)
return (EOF);
*encoding = ENCODE_UTF16BE;
return (mxml_fd_getc(p, encoding));
}
else if (ch == 0xff)
{
/*
* UTF-16 little-endian BOM?
*/
if (buf->current >= buf->end)
if (mxml_fd_read(buf) < 0)
return (EOF);
ch = *(buf->current)++;
if (ch != 0xfe)
return (EOF);
*encoding = ENCODE_UTF16LE;
return (mxml_fd_getc(p, encoding));
}
else if ((ch & 0xe0) == 0xc0)
{
/*
* Two-byte value...
*/
if (buf->current >= buf->end)
if (mxml_fd_read(buf) < 0)
return (EOF);
temp = *(buf->current)++;
if ((temp & 0xc0) != 0x80)
return (EOF);
ch = ((ch & 0x1f) << 6) | (temp & 0x3f);
if (ch < 0x80)
{
mxml_error("Invalid UTF-8 sequence for character 0x%04x!", ch);
return (EOF);
}
}
else if ((ch & 0xf0) == 0xe0)
{
/*
* Three-byte value...
*/
if (buf->current >= buf->end)
if (mxml_fd_read(buf) < 0)
return (EOF);
temp = *(buf->current)++;
if ((temp & 0xc0) != 0x80)
return (EOF);
ch = ((ch & 0x0f) << 6) | (temp & 0x3f);
if (buf->current >= buf->end)
if (mxml_fd_read(buf) < 0)
return (EOF);
temp = *(buf->current)++;
if ((temp & 0xc0) != 0x80)
return (EOF);
ch = (ch << 6) | (temp & 0x3f);
if (ch < 0x800)
{
mxml_error("Invalid UTF-8 sequence for character 0x%04x!", ch);
return (EOF);
}
/*
* Ignore (strip) Byte Order Mark (BOM)...
*/
if (ch == 0xfeff)
return (mxml_fd_getc(p, encoding));
}
else if ((ch & 0xf8) == 0xf0)
{
/*
* Four-byte value...
*/
if (buf->current >= buf->end)
if (mxml_fd_read(buf) < 0)
return (EOF);
temp = *(buf->current)++;
if ((temp & 0xc0) != 0x80)
return (EOF);
ch = ((ch & 0x07) << 6) | (temp & 0x3f);
if (buf->current >= buf->end)
if (mxml_fd_read(buf) < 0)
return (EOF);
temp = *(buf->current)++;
if ((temp & 0xc0) != 0x80)
return (EOF);
ch = (ch << 6) | (temp & 0x3f);
if (buf->current >= buf->end)
if (mxml_fd_read(buf) < 0)
return (EOF);
temp = *(buf->current)++;
if ((temp & 0xc0) != 0x80)
return (EOF);
ch = (ch << 6) | (temp & 0x3f);
if (ch < 0x10000)
{
mxml_error("Invalid UTF-8 sequence for character 0x%04x!", ch);
return (EOF);
}
}
else
return (EOF);
break;
case ENCODE_UTF16BE :
/*
* Read UTF-16 big-endian char...
*/
if (buf->current >= buf->end)
if (mxml_fd_read(buf) < 0)
return (EOF);
temp = *(buf->current)++;
ch = (ch << 8) | temp;
if (mxml_bad_char(ch))
{
mxml_error("Bad control character 0x%02x not allowed by XML standard!",
ch);
return (EOF);
}
else if (ch >= 0xd800 && ch <= 0xdbff)
{
/*
* Multi-word UTF-16 char...
*/
int lch;
if (buf->current >= buf->end)
if (mxml_fd_read(buf) < 0)
return (EOF);
lch = *(buf->current)++;
if (buf->current >= buf->end)
if (mxml_fd_read(buf) < 0)
return (EOF);
temp = *(buf->current)++;
lch = (lch << 8) | temp;
if (lch < 0xdc00 || lch >= 0xdfff)
return (EOF);
ch = (((ch & 0x3ff) << 10) | (lch & 0x3ff)) + 0x10000;
}
break;
case ENCODE_UTF16LE :
/*
* Read UTF-16 little-endian char...
*/
if (buf->current >= buf->end)
if (mxml_fd_read(buf) < 0)
return (EOF);
temp = *(buf->current)++;
ch |= (temp << 8);
if (mxml_bad_char(ch))
{
mxml_error("Bad control character 0x%02x not allowed by XML standard!",
ch);
return (EOF);
}
else if (ch >= 0xd800 && ch <= 0xdbff)
{
/*
* Multi-word UTF-16 char...
*/
int lch;
if (buf->current >= buf->end)
if (mxml_fd_read(buf) < 0)
return (EOF);
lch = *(buf->current)++;
if (buf->current >= buf->end)
if (mxml_fd_read(buf) < 0)
return (EOF);
temp = *(buf->current)++;
lch |= (temp << 8);
if (lch < 0xdc00 || lch >= 0xdfff)
return (EOF);
ch = (((ch & 0x3ff) << 10) | (lch & 0x3ff)) + 0x10000;
}
break;
}
#if DEBUG > 1
printf("mxml_fd_getc: %c (0x%04x)\n", ch < ' ' ? '.' : ch, ch);
#endif /* DEBUG > 1 */
return (ch);
}