-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
mxml-file.c
2275 lines (1860 loc) · 59.8 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 Mini-XML, a small XML file parsing library.
//
// https://www.msweet.org/mxml
//
// Copyright © 2003-2024 by Michael R Sweet.
//
// Licensed under Apache License v2.0. See the file "LICENSE" for more
// information.
//
#ifndef _WIN32
# include <unistd.h>
#endif // !_WIN32
#include "mxml-private.h"
//
// Local types...
//
typedef enum _mxml_encoding_e // Character encoding
{
_MXML_ENCODING_UTF8, // UTF-8
_MXML_ENCODING_UTF16BE, // UTF-16 Big-Endian
_MXML_ENCODING_UTF16LE // UTF-16 Little-Endian
} _mxml_encoding_t;
typedef struct _mxml_stringbuf_s // String buffer
{
char *buffer, // Buffer
*bufptr; // Pointer into buffer
size_t bufsize; // Size of buffer
bool bufalloc; // Allocate buffer?
} _mxml_stringbuf_t;
//
// Macro to test for a bad XML character...
//
#define mxml_bad_char(ch) ((ch) < ' ' && (ch) != '\n' && (ch) != '\r' && (ch) != '\t')
//
// Local functions...
//
static bool mxml_add_char(mxml_options_t *options, int ch, char **ptr, char **buffer, size_t *bufsize);
static int mxml_get_entity(mxml_options_t *options, mxml_io_cb_t io_cb, void *io_cbdata, _mxml_encoding_t *encoding, mxml_node_t *parent, int *line);
static int mxml_getc(mxml_options_t *options, mxml_io_cb_t io_cb, void *io_cbdata, _mxml_encoding_t *encoding);
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, mxml_options_t *options, mxml_io_cb_t io_cb, void *io_cbdata);
static int mxml_parse_element(mxml_options_t *options, mxml_io_cb_t io_cb, void *io_cbdata, mxml_node_t *node, _mxml_encoding_t *encoding, int *line);
static size_t mxml_read_cb_fd(int *fd, void *buffer, size_t bytes);
static size_t mxml_read_cb_file(FILE *fp, void *buffer, size_t bytes);
static size_t mxml_read_cb_string(_mxml_stringbuf_t *sb, void *buffer, size_t bytes);
static double mxml_strtod(mxml_options_t *options, const char *buffer, char **bufptr);
static size_t mxml_io_cb_fd(int *fd, void *buffer, size_t bytes);
static size_t mxml_io_cb_file(FILE *fp, void *buffer, size_t bytes);
static size_t mxml_io_cb_string(_mxml_stringbuf_t *sb, void *buffer, size_t bytes);
static int mxml_write_node(mxml_node_t *node, mxml_options_t *options, mxml_io_cb_t io_cb, void *io_cbdata, int col);
static int mxml_write_string(const char *s, mxml_io_cb_t io_cb, void *io_cbdata, bool use_entities, int col);
static int mxml_write_ws(mxml_node_t *node, mxml_options_t *options, mxml_io_cb_t io_cb, void *io_cbdata, mxml_ws_t ws, int col);
//
// 'mxmlLoadFd()' - Load a file descriptor into an XML node tree.
//
// This function loads the file descriptor `fd` into an XML node tree. The
// nodes in the specified file are added to the specified node `top` - if `NULL`
// the XML file MUST be well-formed with a single parent processing instruction
// node like `<?xml version="1.0"?>` at the start of the file.
//
// Load options are provides via the `options` argument. If `NULL`, all values
// will be loaded into `MXML_TYPE_TEXT` nodes. Use the @link mxmlOptionsNew@
// function to create options when loading XML data.
//
mxml_node_t * // O - First node or `NULL` if the file could not be read.
mxmlLoadFd(
mxml_node_t *top, // I - Top node
mxml_options_t *options, // I - Options
int fd) // I - File descriptor to read from
{
// Range check input...
if (fd < 0)
return (NULL);
// Read the XML data...
return (mxml_load_data(top, options, (mxml_io_cb_t)mxml_read_cb_fd, &fd));
}
//
// 'mxmlLoadFile()' - Load a file into an XML node tree.
//
// This function loads the `FILE` pointer `fp` into an XML node tree. The
// nodes in the specified file are added to the specified node `top` - if `NULL`
// the XML file MUST be well-formed with a single parent processing instruction
// node like `<?xml version="1.0"?>` at the start of the file.
//
// Load options are provides via the `options` argument. If `NULL`, all values
// will be loaded into `MXML_TYPE_TEXT` nodes. Use the @link mxmlOptionsNew@
// function to create options when loading XML data.
//
mxml_node_t * // O - First node or `NULL` if the file could not be read.
mxmlLoadFile(
mxml_node_t *top, // I - Top node
mxml_options_t *options, // I - Options
FILE *fp) // I - File to read from
{
// Range check input...
if (!fp)
return (NULL);
// Read the XML data...
return (mxml_load_data(top, options, (mxml_io_cb_t)mxml_read_cb_file, fp));
}
//
// 'mxmlLoadFilename()' - Load a file into an XML node tree.
//
// This function loads the named file `filename` into an XML node tree. The
// nodes in the specified file are added to the specified node `top` - if `NULL`
// the XML file MUST be well-formed with a single parent processing instruction
// node like `<?xml version="1.0"?>` at the start of the file.
//
// Load options are provides via the `options` argument. If `NULL`, all values
// will be loaded into `MXML_TYPE_TEXT` nodes. Use the @link mxmlOptionsNew@
// function to create options when loading XML data.
//
mxml_node_t * // O - First node or `NULL` if the file could not be read.
mxmlLoadFilename(
mxml_node_t *top, // I - Top node
mxml_options_t *options, // I - Options
const char *filename) // I - File to read from
{
FILE *fp; // File pointer
mxml_node_t *ret; // Node
// Range check input...
if (!filename)
return (NULL);
// Open the file...
if ((fp = fopen(filename, "r")) == NULL)
return (NULL);
// Read the XML data...
ret = mxml_load_data(top, options, (mxml_io_cb_t)mxml_read_cb_file, fp);
// Close the file and return...
fclose(fp);
return (ret);
}
//
// 'mxmlLoadIO()' - Load an XML node tree using a read callback.
//
// This function loads data into an XML node tree using a read callback. The
// nodes in the specified file are added to the specified node `top` - if `NULL`
// the XML file MUST be well-formed with a single parent processing instruction
// node like `<?xml version="1.0"?>` at the start of the file.
//
// Load options are provides via the `options` argument. If `NULL`, all values
// will be loaded into `MXML_TYPE_TEXT` nodes. Use the @link mxmlOptionsNew@
// function to create options when loading XML data.
//
// The read callback function `io_cb` is called to read a number of bytes from
// the source. The callback data pointer `io_cbdata` is passed to the read
// callback with a pointer to a buffer and the maximum number of bytes to read,
// for example:
//
// ```c
// size_t my_io_cb(void *cbdata, void *buffer, size_t bytes)
// {
// ... copy up to "bytes" bytes into buffer ...
// ... return the number of bytes "read" or 0 on error ...
// }
// ```
//
mxml_node_t * // O - First node or `NULL` if the file could not be read.
mxmlLoadIO(
mxml_node_t *top, // I - Top node
mxml_options_t *options, // I - Options
mxml_io_cb_t io_cb, // I - Read callback function
void *io_cbdata) // I - Read callback data
{
// Range check input...
if (!io_cb)
return (NULL);
// Read the XML data...
return (mxml_load_data(top, options, io_cb, io_cbdata));
}
//
// 'mxmlLoadString()' - Load a string into an XML node tree.
//
// This function loads the string into an XML node tree. The nodes in the
// specified file are added to the specified node `top` - if `NULL` the XML file
// MUST be well-formed with a single parent processing instruction node like
// `<?xml version="1.0"?>` at the start of the file.
//
// Load options are provides via the `options` argument. If `NULL`, all values
// will be loaded into `MXML_TYPE_TEXT` nodes. Use the @link mxmlOptionsNew@
// function to create options when loading XML data.
//
mxml_node_t * // O - First node or `NULL` if the string has errors.
mxmlLoadString(
mxml_node_t *top, // I - Top node
mxml_options_t *options, // I - Options
const char *s) // I - String to load
{
_mxml_stringbuf_t sb; // String buffer
// Range check input...
if (!s)
return (NULL);
// Setup string buffer...
sb.buffer = (char *)s;
sb.bufptr = (char *)s;
sb.bufsize = strlen(s);
sb.bufalloc = false;
// Read the XML data...
return (mxml_load_data(top, options, (mxml_io_cb_t)mxml_read_cb_string, &sb));
}
//
// 'mxmlSaveAllocString()' - Save an XML tree to an allocated string.
//
// This function saves the XML tree `node` to an allocated string. The string
// should be freed using `free` (or the string free callback set using
// @link mxmlSetStringCallbacks@) when you are done with it.
//
// `NULL` is returned if the node would produce an empty string or if the string
// cannot be allocated.
//
// Save options are provides via the `options` argument. If `NULL`, the XML
// output will be wrapped at column 72 with no additional whitespace. Use the
// @link mxmlOptionsNew@ function to create options for saving XML data.
//
char * // O - Allocated string or `NULL`
mxmlSaveAllocString(
mxml_node_t *node, // I - Node to write
mxml_options_t *options) // I - Options
{
_mxml_stringbuf_t sb; // String buffer
// Setup a string buffer
if ((sb.buffer = malloc(1024)) == NULL)
return (NULL);
sb.bufptr = sb.buffer;
sb.bufsize = 1024;
sb.bufalloc = true;
// Write the top node...
if (mxml_write_node(node, options, (mxml_io_cb_t)mxml_io_cb_string, &sb, 0) < 0)
{
free(sb.buffer);
return (NULL);
}
// Nul-terminate the string...
*(sb.bufptr) = '\0';
// Return the allocated string...
return (sb.buffer);
}
//
// 'mxmlSaveFd()' - Save an XML tree to a file descriptor.
//
// This function saves the XML tree `node` to a file descriptor.
//
// Save options are provides via the `options` argument. If `NULL`, the XML
// output will be wrapped at column 72 with no additional whitespace. Use the
// @link mxmlOptionsNew@ function to create options for saving XML data.
//
bool // O - `true` on success, `false` on error.
mxmlSaveFd(mxml_node_t *node, // I - Node to write
mxml_options_t *options, // I - Options
int fd) // I - File descriptor to write to
{
int col; // Final column
// Write the node...
if ((col = mxml_write_node(node, options, (mxml_io_cb_t)mxml_io_cb_fd, &fd, 0)) < 0)
return (false);
// Make sure the file ends with a newline...
if (col > 0)
{
if (write(fd, "\n", 1) < 0)
return (false);
}
return (true);
}
//
// 'mxmlSaveFile()' - Save an XML tree to a file.
//
// This function saves the XML tree `node` to a stdio `FILE`.
//
// Save options are provides via the `options` argument. If `NULL`, the XML
// output will be wrapped at column 72 with no additional whitespace. Use the
// @link mxmlOptionsNew@ function to create options for saving XML data.
//
bool // O - `true` on success, `false` on error.
mxmlSaveFile(
mxml_node_t *node, // I - Node to write
mxml_options_t *options, // I - Options
FILE *fp) // I - File to write to
{
int col; // Final column
// Write the node...
if ((col = mxml_write_node(node, options, (mxml_io_cb_t)mxml_io_cb_file, fp, 0)) < 0)
return (false);
// Make sure the file ends with a newline...
if (col > 0)
{
if (putc('\n', fp) < 0)
return (false);
}
return (true);
}
//
// 'mxmlSaveFilename()' - Save an XML tree to a file.
//
// This function saves the XML tree `node` to a named file.
//
// Save options are provides via the `options` argument. If `NULL`, the XML
// output will be wrapped at column 72 with no additional whitespace. Use the
// @link mxmlOptionsNew@ function to create options for saving XML data.
//
bool // O - `true` on success, `false` on error.
mxmlSaveFilename(
mxml_node_t *node, // I - Node to write
mxml_options_t *options, // I - Options
const char *filename) // I - File to write to
{
bool ret = true; // Return value
FILE *fp; // File pointer
int col; // Final column
// Open the file...
if ((fp = fopen(filename, "w")) == NULL)
return (false);
// Write the node...
if ((col = mxml_write_node(node, options, (mxml_io_cb_t)mxml_io_cb_file, fp, 0)) < 0)
{
ret = false;
}
else if (col > 0)
{
// Make sure the file ends with a newline...
if (putc('\n', fp) < 0)
ret = false;
}
fclose(fp);
return (ret);
}
//
// 'mxmlSaveIO()' - Save an XML tree using a callback.
//
// This function saves the XML tree `node` using a write callback function
// `io_cb`. The write callback is called with the callback data pointer
// `io_cbdata`, a buffer pointer, and the number of bytes to write, for
// example:
//
// ```c
// size_t my_io_cb(void *cbdata, const void *buffer, size_t bytes)
// {
// ... write/copy bytes from buffer to the output ...
// ... return the number of bytes written/copied or 0 on error ...
// }
// ```
//
// Save options are provides via the `options` argument. If `NULL`, the XML
// output will be wrapped at column 72 with no additional whitespace. Use the
// @link mxmlOptionsNew@ function to create options for saving XML data.
//
bool // O - `true` on success, `false` on error.
mxmlSaveIO(
mxml_node_t *node, // I - Node to write
mxml_options_t *options, // I - Options
mxml_io_cb_t io_cb, // I - Write callback function
void *io_cbdata) // I - Write callback data
{
int col; // Final column
// Range check input...
if (!node || !io_cb)
return (false);
// Write the node...
if ((col = mxml_write_node(node, options, io_cb, io_cbdata, 0)) < 0)
return (false);
if (col > 0)
{
// Make sure the file ends with a newline...
if ((io_cb)(io_cbdata, "\n", 1) != 1)
return (false);
}
return (true);
}
//
// 'mxmlSaveString()' - Save an XML node tree to a string.
//
// This function saves the XML tree `node` to a fixed-size string buffer.
//
// Save options are provides via the `options` argument. If `NULL`, the XML
// output will be wrapped at column 72 with no additional whitespace. Use the
// @link mxmlOptionsNew@ function to create options for saving XML data.
//
size_t // O - Size of string
mxmlSaveString(
mxml_node_t *node, // I - Node to write
mxml_options_t *options, // I - Options
char *buffer, // I - String buffer
size_t bufsize) // I - Size of string buffer
{
_mxml_stringbuf_t sb; // String buffer
// Setup the string buffer...
sb.buffer = buffer;
sb.bufptr = buffer;
sb.bufsize = bufsize;
sb.bufalloc = false;
// Write the node...
if (mxml_write_node(node, options, (mxml_io_cb_t)mxml_io_cb_string, &sb, 0) < 0)
return (false);
// Nul-terminate the string...
if (sb.bufptr < (sb.buffer + sb.bufsize))
*(sb.bufptr) = '\0';
// Return the number of characters...
return ((size_t)(sb.bufptr - sb.buffer));
}
//
// 'mxml_add_char()' - Add a character to a buffer, expanding as needed.
//
static bool // O - `true` on success, `false` on error
mxml_add_char(mxml_options_t *options, // I - Options
int ch, // I - Character to add
char **bufptr, // IO - Current position in buffer
char **buffer, // IO - Current buffer
size_t *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)
{
_mxml_error(options, "Unable to expand string buffer to %lu bytes.", (unsigned long)*bufsize);
return (false);
}
*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 (true);
}
//
// 'mxml_get_entity()' - Get the character corresponding to an entity...
//
static int // O - Character value or `EOF` on error
mxml_get_entity(
mxml_options_t *options, // I - Options
mxml_io_cb_t io_cb, // I - Read callback function
void *io_cbdata, // I - Read callback data
_mxml_encoding_t *encoding, // IO - Character encoding
mxml_node_t *parent, // I - Parent node
int *line) // IO - Current line number
{
int ch; // Current character
char entity[64], // Entity string
*entptr; // Pointer into entity
// Read a HTML character entity of the form "&NAME;", "&#NUMBER;", or "&#xHEX"...
entptr = entity;
while ((ch = mxml_getc(options, io_cb, io_cbdata, encoding)) != EOF)
{
if (ch > 126 || (!isalnum(ch) && ch != '#'))
{
break;
}
else if (entptr < (entity + sizeof(entity) - 1))
{
*entptr++ = ch;
}
else
{
_mxml_error(options, "Entity name too long under parent <%s> on line %d.", mxmlGetElement(parent), *line);
break;
}
}
*entptr = '\0';
if (ch != ';')
{
_mxml_error(options, "Character entity '%s' not terminated under parent <%s> on line %d.", entity, mxmlGetElement(parent), *line);
if (ch == '\n')
(*line)++;
return (EOF);
}
if ((ch = _mxml_entity_value(options, entity)) < 0)
{
_mxml_error(options, "Entity '&%s;' not supported under parent <%s> on line %d.", entity, mxmlGetElement(parent), *line);
return (EOF);
}
if (mxml_bad_char(ch))
{
_mxml_error(options, "Bad control character 0x%02x under parent <%s> on line %d not allowed by XML standard.", ch, mxmlGetElement(parent), *line);
return (EOF);
}
return (ch);
}
//
// 'mxml_getc()' - Read a character from a file descriptor.
//
static int // O - Character or `EOF`
mxml_getc(mxml_options_t *options, // I - Options
mxml_io_cb_t io_cb, // I - Read callback function
void *io_cbdata, // I - Read callback data
_mxml_encoding_t *encoding) // IO - Encoding
{
int ch; // Current character
unsigned char buffer[4]; // Read buffer
// Grab the next character...
read_first_byte:
if ((io_cb)(io_cbdata, buffer, 1) != 1)
return (EOF);
ch = buffer[0];
switch (*encoding)
{
case _MXML_ENCODING_UTF8 :
// Got a UTF-8 character; convert UTF-8 to Unicode and return...
if (!(ch & 0x80))
{
// ASCII
break;
}
else if (ch == 0xfe)
{
// UTF-16 big-endian BOM?
if ((io_cb)(io_cbdata, buffer + 1, 1) != 1)
return (EOF);
if (buffer[1] != 0xff)
return (EOF);
// Yes, switch to UTF-16 BE and try reading again...
*encoding = _MXML_ENCODING_UTF16BE;
goto read_first_byte;
}
else if (ch == 0xff)
{
// UTF-16 little-endian BOM?
if ((io_cb)(io_cbdata, buffer + 1, 1) != 1)
return (EOF);
if (buffer[1] != 0xfe)
return (EOF);
// Yes, switch to UTF-16 LE and try reading again...
*encoding = _MXML_ENCODING_UTF16LE;
goto read_first_byte;
}
else if ((ch & 0xe0) == 0xc0)
{
// Two-byte value...
if ((io_cb)(io_cbdata, buffer + 1, 1) != 1)
return (EOF);
if ((buffer[1] & 0xc0) != 0x80)
return (EOF);
ch = ((ch & 0x1f) << 6) | (buffer[1] & 0x3f);
if (ch < 0x80)
{
_mxml_error(options, "Invalid UTF-8 sequence for character 0x%04x.", ch);
return (EOF);
}
}
else if ((ch & 0xf0) == 0xe0)
{
// Three-byte value...
if ((io_cb)(io_cbdata, buffer + 1, 2) != 2)
return (EOF);
if ((buffer[1] & 0xc0) != 0x80 || (buffer[2] & 0xc0) != 0x80)
return (EOF);
ch = ((ch & 0x0f) << 12) | ((buffer[1] & 0x3f) << 6) | (buffer[2] & 0x3f);
if (ch < 0x800)
{
_mxml_error(options, "Invalid UTF-8 sequence for character 0x%04x.", ch);
return (EOF);
}
// Ignore (strip) Byte Order Mark (BOM)...
if (ch == 0xfeff)
goto read_first_byte;
}
else if ((ch & 0xf8) == 0xf0)
{
// Four-byte value...
if ((io_cb)(io_cbdata, buffer + 1, 3) != 3)
return (EOF);
if ((buffer[1] & 0xc0) != 0x80 || (buffer[2] & 0xc0) != 0x80 || (buffer[3] & 0xc0) != 0x80)
return (EOF);
ch = ((ch & 0x07) << 18) | ((buffer[1] & 0x3f) << 12) | ((buffer[2] & 0x3f) << 6) | (buffer[3] & 0x3f);
if (ch < 0x10000)
{
_mxml_error(options, "Invalid UTF-8 sequence for character 0x%04x.", ch);
return (EOF);
}
}
else
{
return (EOF);
}
break;
case _MXML_ENCODING_UTF16BE :
// Read UTF-16 big-endian char...
if ((io_cb)(io_cbdata, buffer + 1, 1) != 1)
return (EOF);
ch = (ch << 8) | buffer[1];
if (ch >= 0xd800 && ch <= 0xdbff)
{
// Multi-word UTF-16 char...
int lch; // Lower bits
if ((io_cb)(io_cbdata, buffer + 2, 2) != 2)
return (EOF);
lch = (buffer[2] << 8) | buffer[3];
if (lch < 0xdc00 || lch >= 0xdfff)
return (EOF);
ch = (((ch & 0x3ff) << 10) | (lch & 0x3ff)) + 0x10000;
}
break;
case _MXML_ENCODING_UTF16LE :
// Read UTF-16 little-endian char...
if ((io_cb)(io_cbdata, buffer + 1, 1) != 1)
return (EOF);
ch |= buffer[1] << 8;
if (ch >= 0xd800 && ch <= 0xdbff)
{
// Multi-word UTF-16 char...
int lch; // Lower bits
if ((io_cb)(io_cbdata, buffer + 2, 2) != 2)
return (EOF);
lch = (buffer[3] << 8) | buffer[2];
if (lch < 0xdc00 || lch >= 0xdfff)
return (EOF);
ch = (((ch & 0x3ff) << 10) | (lch & 0x3ff)) + 0x10000;
}
break;
}
if (mxml_bad_char(ch))
{
_mxml_error(options, "Bad control character 0x%02x not allowed by XML standard.", ch);
return (EOF);
}
return (ch);
}
//
// 'mxml_load_data()' - Load data into an XML node tree.
//
static mxml_node_t * // O - First node or `NULL` if the XML could not be read.
mxml_load_data(
mxml_node_t *top, // I - Top node
mxml_options_t *options, // I - Options
mxml_io_cb_t io_cb, // I - Read callback function
void *io_cbdata) // I - Read callback data
{
mxml_node_t *node = NULL, // Current node
*first = NULL, // First node added
*parent = NULL; // Current parent node
int line = 1, // Current line number
ch; // Character from file
bool whitespace = false; // Whitespace seen?
char *buffer, // String buffer
*bufptr; // Pointer into buffer
size_t bufsize; // Size of buffer
mxml_type_t type; // Current node type
_mxml_encoding_t encoding = _MXML_ENCODING_UTF8;
// Character encoding
static const char * const types[] = // Type strings...
{
"MXML_TYPE_CDATA", // CDATA
"MXML_TYPE_COMMENT", // Comment
"MXML_TYPE_DECLARATION",// Declaration
"MXML_TYPE_DIRECTIVE",// Processing instruction/directive
"MXML_TYPE_ELEMENT", // XML element with attributes
"MXML_TYPE_INTEGER", // Integer value
"MXML_TYPE_OPAQUE", // Opaque string
"MXML_TYPE_REAL", // Real value
"MXML_TYPE_TEXT", // Text fragment
"MXML_TYPE_CUSTOM" // Custom data
};
// Read elements and other nodes from the file...
if ((buffer = malloc(64)) == NULL)
{
_mxml_error(options, "Unable to allocate string buffer.");
return (NULL);
}
bufsize = 64;
bufptr = buffer;
parent = top;
first = NULL;
if (options && options->type_cb && parent)
type = (options->type_cb)(options->type_cbdata, parent);
else if (options && !options->type_cb)
type = options->type_value;
else
type = MXML_TYPE_IGNORE;
if ((ch = mxml_getc(options, io_cb, io_cbdata, &encoding)) == EOF)
{
free(buffer);
return (NULL);
}
else if (ch != '<' && !top)
{
free(buffer);
_mxml_error(options, "XML does not start with '<' (saw '%c').", ch);
return (NULL);
}
do
{
if ((ch == '<' || (mxml_isspace(ch) && type != MXML_TYPE_OPAQUE && type != MXML_TYPE_CUSTOM)) && bufptr > buffer)
{
// Add a new value node...
*bufptr = '\0';
switch (type)
{
case MXML_TYPE_INTEGER :
node = mxmlNewInteger(parent, strtol(buffer, &bufptr, 0));
break;
case MXML_TYPE_OPAQUE :
node = mxmlNewOpaque(parent, buffer);
break;
case MXML_TYPE_REAL :
node = mxmlNewReal(parent, mxml_strtod(options, buffer, &bufptr));
break;
case MXML_TYPE_TEXT :
node = mxmlNewText(parent, whitespace, buffer);
break;
case MXML_TYPE_CUSTOM :
if (options && options->custload_cb)
{
// Use the callback to fill in the custom data...
node = mxmlNewCustom(parent, /*data*/NULL, /*free_cb*/NULL, /*free_cbdata*/NULL);
if (!(options->custload_cb)(options->cust_cbdata, node, buffer))
{
_mxml_error(options, "Bad custom value '%s' in parent <%s> on line %d.", buffer, parent ? parent->value.element.name : "null", line);
mxmlDelete(node);
node = NULL;
}
break;
}
default : // Ignore...
node = NULL;
break;
}
if (*bufptr)
{
// Bad integer/real number value...
_mxml_error(options, "Bad %s value '%s' in parent <%s> on line %d.", type == MXML_TYPE_INTEGER ? "integer" : "real", buffer, parent ? parent->value.element.name : "null", line);
break;
}
MXML_DEBUG("mxml_load_data: node=%p(%s), parent=%p\n", node, buffer, parent);
bufptr = buffer;
whitespace = mxml_isspace(ch) && type == MXML_TYPE_TEXT;
if (!node && type != MXML_TYPE_IGNORE)
{
// Print error and return...
_mxml_error(options, "Unable to add value node of type %s to parent <%s> on line %d.", types[type], parent ? parent->value.element.name : "null", line);
goto error;
}
if (options && options->sax_cb)
{
if (!(options->sax_cb)(options->sax_cbdata, node, MXML_SAX_EVENT_DATA))
goto error;
if (!mxmlRelease(node))
node = NULL;
}
if (!first && node)
first = node;
}
else if (mxml_isspace(ch) && type == MXML_TYPE_TEXT)
{
whitespace = true;
}
if (ch == '\n')
line ++;
// Add lone whitespace node if we have an element and existing whitespace...
if (ch == '<' && whitespace && type == MXML_TYPE_TEXT)
{
if (parent)
{
node = mxmlNewText(parent, whitespace, "");
if (options && options->sax_cb)
{
if (!(options->sax_cb)(options->sax_cbdata, node, MXML_SAX_EVENT_DATA))
goto error;
if (!mxmlRelease(node))
node = NULL;
}
if (!first && node)
first = node;
}
whitespace = false;
}
if (ch == '<')
{
// Start of open/close tag...
bufptr = buffer;
while ((ch = mxml_getc(options, io_cb, io_cbdata, &encoding)) != EOF)
{
if (mxml_isspace(ch) || ch == '>' || (ch == '/' && bufptr > buffer))
{
break;
}
else if (ch == '<')
{
_mxml_error(options, "Bare < in element.");
goto error;
}
else if (ch == '&')
{
if ((ch = mxml_get_entity(options, io_cb, io_cbdata, &encoding, parent, &line)) == EOF)
goto error;
if (!mxml_add_char(options, ch, &bufptr, &buffer, &bufsize))
goto error;
}
else if (ch < '0' && ch != '!' && ch != '-' && ch != '.' && ch != '/')