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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
|
/* markup.c -- simple XML-like parser
Copyright (C) 2015 Free Software Foundation, Inc.
This file is not part of the GNU gettext program, but is used with
GNU gettext.
This is a stripped down version of GLib's gmarkup.c. The original
copyright notice is as follows:
*/
/* gmarkup.c - Simple XML-like parser
*
* Copyright 2000, 2003 Red Hat, Inc.
* Copyright 2007, 2008 Ryan Lortie <desrt@desrt.ca>
*
* GLib is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* GLib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with GLib; see the file COPYING.LIB. If not,
* see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <assert.h>
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
/* Specification */
#include "markup.h"
#include "c-ctype.h"
#include "gettext.h"
#include "gl_linked_list.h"
#include "gl_xlist.h"
#include "unictype.h"
#include "unistr.h"
#include "xalloc.h"
#include "xvasprintf.h"
#define _(s) gettext(s)
/**
* The "markup" parser is intended to parse a simple markup format
* that's a subset of XML. This is a small, efficient, easy-to-use
* parser. It should not be used if you expect to interoperate with
* other applications generating full-scale XML. However, it's very
* useful for application data files, config files, etc. where you
* know your application will be the only one writing the file.
* Full-scale XML parsers should be able to parse the subset used by
* markup, so you can easily migrate to full-scale XML at a later
* time if the need arises.
*
* The parser is not guaranteed to signal an error on all invalid XML;
* the parser may accept documents that an XML parser would not.
* However, XML documents which are not well-formed (which is a weaker
* condition than being valid. See the XML specification
* <http://www.w3.org/TR/REC-xml/> for definitions of these terms.)
* are not considered valid GMarkup documents.
*
* Simplifications to XML include:
*
* - Only UTF-8 encoding is allowed
*
* - No user-defined entities
*
* - Processing instructions, comments and the doctype declaration
* are "passed through" but are not interpreted in any way
*
* - No DTD or validation
*
* The markup format does support:
*
* - Elements
*
* - Attributes
*
* - 5 standard entities: & < > " '
*
* - Character references
*
* - Sections marked as CDATA
*/
typedef enum
{
STATE_START,
STATE_AFTER_OPEN_ANGLE,
STATE_AFTER_CLOSE_ANGLE,
STATE_AFTER_ELISION_SLASH, /* the slash that obviates need for end element */
STATE_INSIDE_OPEN_TAG_NAME,
STATE_INSIDE_ATTRIBUTE_NAME,
STATE_AFTER_ATTRIBUTE_NAME,
STATE_BETWEEN_ATTRIBUTES,
STATE_AFTER_ATTRIBUTE_EQUALS_SIGN,
STATE_INSIDE_ATTRIBUTE_VALUE_SQ,
STATE_INSIDE_ATTRIBUTE_VALUE_DQ,
STATE_INSIDE_TEXT,
STATE_AFTER_CLOSE_TAG_SLASH,
STATE_INSIDE_CLOSE_TAG_NAME,
STATE_AFTER_CLOSE_TAG_NAME,
STATE_INSIDE_PASSTHROUGH,
STATE_ERROR
} markup_parse_state_ty;
typedef struct
{
const char *prev_element;
const markup_parser_ty *prev_parser;
void *prev_user_data;
} markup_recursion_tracker_ty;
typedef struct
{
char *buffer;
size_t bufmax;
size_t buflen;
} markup_string_ty;
struct _markup_parse_context_ty
{
const markup_parser_ty *parser;
markup_parse_flags_ty flags;
int line_number;
int char_number;
markup_parse_state_ty state;
void *user_data;
/* A piece of character data or an element that
* hasn't "ended" yet so we haven't yet called
* the callback for it.
*/
markup_string_ty *partial_chunk;
gl_list_t tag_stack; /* <markup_string_ty> */
char **attr_names;
char **attr_values;
int cur_attr;
int alloc_attrs;
const char *current_text;
ssize_t current_text_len;
const char *current_text_end;
/* used to save the start of the last interesting thingy */
const char *start;
const char *iter;
char *error_text;
unsigned int document_empty : 1;
unsigned int parsing : 1;
unsigned int awaiting_pop : 1;
int balance;
/* subparser support */
gl_list_t subparser_stack; /* <markup_recursion_tracker_ty *> */
const char *subparser_element;
};
static markup_string_ty *
markup_string_new (void)
{
return XZALLOC (markup_string_ty);
}
static char *
markup_string_free (markup_string_ty *string, bool free_segment)
{
if (free_segment)
{
free (string->buffer);
free (string);
return NULL;
}
else
{
char *result = string->buffer;
free (string);
return result;
}
}
static void
markup_string_free1 (markup_string_ty *string)
{
markup_string_free (string, true);
}
static void
markup_string_truncate (markup_string_ty *string, size_t length)
{
assert (string && length < string->buflen - 1);
string->buffer[length] = '\0';
string->buflen = length;
}
static void
markup_string_append (markup_string_ty *string, const char *to_append,
size_t length)
{
if (string->buflen + length + 1 > string->bufmax)
{
string->bufmax *= 2;
if (string->buflen + length + 1 > string->bufmax)
string->bufmax = string->buflen + length + 1;
string->buffer = xrealloc (string->buffer, string->bufmax);
}
memcpy (string->buffer + string->buflen, to_append, length);
string->buffer[length] = '\0';
string->buflen = length;
}
static inline void
string_blank (markup_string_ty *string)
{
if (string->bufmax > 0)
{
*string->buffer = '\0';
string->buflen = 0;
}
}
/* Creates a new parse context. A parse context is used to parse
marked-up documents. You can feed any number of documents into a
context, as long as no errors occur; once an error occurs, the
parse context can't continue to parse text (you have to free it and
create a new parse context). */
markup_parse_context_ty *
markup_parse_context_new (const markup_parser_ty *parser,
markup_parse_flags_ty flags,
void *user_data)
{
markup_parse_context_ty *context;
assert (parser != NULL);
context = XMALLOC (markup_parse_context_ty);
context->parser = parser;
context->flags = flags;
context->user_data = user_data;
context->line_number = 1;
context->char_number = 1;
context->partial_chunk = NULL;
context->state = STATE_START;
context->tag_stack =
gl_list_create_empty (GL_LINKED_LIST,
NULL, NULL,
(gl_listelement_dispose_fn) markup_string_free1,
true);
context->attr_names = NULL;
context->attr_values = NULL;
context->cur_attr = -1;
context->alloc_attrs = 0;
context->current_text = NULL;
context->current_text_len = -1;
context->current_text_end = NULL;
context->start = NULL;
context->iter = NULL;
context->error_text = NULL;
context->document_empty = true;
context->parsing = false;
context->awaiting_pop = false;
context->subparser_stack =
gl_list_create_empty (GL_LINKED_LIST,
NULL, NULL,
(gl_listelement_dispose_fn) free,
true);
context->subparser_element = NULL;
context->balance = 0;
return context;
}
static void clear_attributes (markup_parse_context_ty *context);
/* Frees a parse context. This function can't be called from inside
one of the markup_parser_ty functions or while a subparser is
pushed. */
void
markup_parse_context_free (markup_parse_context_ty *context)
{
assert (context != NULL);
assert (!context->parsing);
assert (gl_list_size (context->subparser_stack) == 0);
assert (!context->awaiting_pop);
clear_attributes (context);
free (context->attr_names);
free (context->attr_values);
gl_list_free (context->tag_stack);
gl_list_free (context->subparser_stack);
if (context->partial_chunk)
markup_string_free (context->partial_chunk, true);
free (context->error_text);
free (context);
}
static void pop_subparser_stack (markup_parse_context_ty *context);
static void
emit_error (markup_parse_context_ty *context, const char *error_text)
{
context->state = STATE_ERROR;
if (context->parser->error)
(*context->parser->error) (context, error_text, context->user_data);
/* report the error all the way up to free all the user-data */
while (gl_list_size (context->subparser_stack) > 0)
{
pop_subparser_stack (context);
context->awaiting_pop = false; /* already been freed */
if (context->parser->error)
(*context->parser->error) (context, error_text, context->user_data);
}
if (context->error_text)
free (context->error_text);
context->error_text = xstrdup (error_text);
}
#define IS_COMMON_NAME_END_CHAR(c) \
((c) == '=' || (c) == '/' || (c) == '>' || (c) == ' ')
static bool
slow_name_validate (markup_parse_context_ty *context, const char *name)
{
const char *p = name;
ucs4_t uc;
if (u8_check ((uint8_t *) name, strlen (name)) != NULL)
{
emit_error (context, _("invalid UTF-8 sequence"));
return false;
}
if (!(c_isalpha (*p)
|| (!IS_COMMON_NAME_END_CHAR (*p)
&& (*p == '_'
|| *p == ':'
|| (u8_mbtouc (&uc, (uint8_t *) name, strlen (name)) > 0
&& uc_is_alpha (uc))))))
{
char *error_text = xasprintf (_("'%s' is not a valid name: %c"),
name, *p);
emit_error (context, error_text);
free (error_text);
return false;
}
for (p = (char *) u8_next (&uc, (uint8_t *) name);
p != NULL;
p = (char *) u8_next (&uc, (uint8_t *) p))
{
/* is_name_char */
if (!(c_isalnum (*p) ||
(!IS_COMMON_NAME_END_CHAR (*p) &&
(*p == '.' ||
*p == '-' ||
*p == '_' ||
*p == ':' ||
uc_is_alpha (uc)))))
{
char *error_text = xasprintf (_("'%s' is not a valid name: '%c'"),
name, *p);
emit_error (context, error_text);
free (error_text);
return false;
}
}
return true;
}
/*
* Use me for elements, attributes etc.
*/
static bool
name_validate (markup_parse_context_ty *context, const char *name)
{
char mask;
const char *p;
/* name start char */
p = name;
if (IS_COMMON_NAME_END_CHAR (*p)
|| !(c_isalpha (*p) || *p == '_' || *p == ':'))
goto slow_validate;
for (mask = *p++; *p != '\0'; p++)
{
mask |= *p;
/* is_name_char */
if (!(c_isalnum (*p)
|| (!IS_COMMON_NAME_END_CHAR (*p)
&& (*p == '.' || *p == '-' || *p == '_' || *p == ':'))))
goto slow_validate;
}
if (mask & 0x80) /* un-common / non-ascii */
goto slow_validate;
return true;
slow_validate:
return slow_name_validate (context, name);
}
static bool
text_validate (markup_parse_context_ty *context,
const char *p,
int len)
{
if (u8_check ((const uint8_t *) p, len) != NULL)
{
emit_error (context, _("invalid UTF-8 sequence"));
return false;
}
else
return true;
}
/*
* re-write the GString in-place, unescaping anything that escaped.
* most XML does not contain entities, or escaping.
*/
static bool
unescape_string_inplace (markup_parse_context_ty *context,
markup_string_ty *string,
bool *is_ascii)
{
char mask, *to;
const char *from;
bool normalize_attribute;
if (string->buflen == 0)
return true;
*is_ascii = false;
/* are we unescaping an attribute or not ? */
if (context->state == STATE_INSIDE_ATTRIBUTE_VALUE_SQ
|| context->state == STATE_INSIDE_ATTRIBUTE_VALUE_DQ)
normalize_attribute = true;
else
normalize_attribute = false;
/*
* Meeks' theorem: unescaping can only shrink text.
* for < etc. this is obvious, for  more
* thought is required, but this is patently so.
*/
mask = 0;
for (from = to = string->buffer; *from != '\0'; from++, to++)
{
*to = *from;
mask |= *to;
if (normalize_attribute && (*to == '\t' || *to == '\n'))
*to = ' ';
if (*to == '\r')
{
*to = normalize_attribute ? ' ' : '\n';
if (from[1] == '\n')
from++;
}
if (*from == '&')
{
from++;
if (*from == '#')
{
int base = 10;
unsigned long l;
char *end = NULL;
from++;
if (*from == 'x')
{
base = 16;
from++;
}
errno = 0;
l = strtoul (from, &end, base);
if (end == from || errno != 0)
{
char *error_text =
xasprintf (_("invalid character reference: %s"),
errno != 0
? strerror (errno)
: _("not a valid number specification"));
emit_error (context, error_text);
free (error_text);
return false;
}
else if (*end != ';')
{
char *error_text =
xasprintf (_("invalid character reference: %s"),
_("no ending ';'"));
emit_error (context, error_text);
free (error_text);
return false;
}
else
{
/* characters XML 1.1 permits */
if ((0 < l && l <= 0xD7FF) ||
(0xE000 <= l && l <= 0xFFFD) ||
(0x10000 <= l && l <= 0x10FFFF))
{
char buf[8];
int length;
length = u8_uctomb ((uint8_t *) buf, l, 8);
memcpy (to, buf, length);
to += length - 1;
from = end;
if (l >= 0x80) /* not ascii */
mask |= 0x80;
}
else
{
char *error_text =
xasprintf (_("invalid character reference: %s"),
_("non-permitted character"));
emit_error (context, error_text);
free (error_text);
return false;
}
}
}
else if (strncmp (from, "lt;", 3) == 0)
{
*to = '<';
from += 2;
}
else if (strncmp (from, "gt;", 3) == 0)
{
*to = '>';
from += 2;
}
else if (strncmp (from, "amp;", 4) == 0)
{
*to = '&';
from += 3;
}
else if (strncmp (from, "quot;", 5) == 0)
{
*to = '"';
from += 4;
}
else if (strncmp (from, "apos;", 5) == 0)
{
*to = '\'';
from += 4;
}
else
{
const char *reason;
char *error_text;
if (*from == ';')
reason = _("empty");
else
{
const char *end = strchr (from, ';');
if (end)
reason = _("unknown");
else
reason = _("no ending ';'");
}
error_text = xasprintf (_("invalid entity reference: %s"),
reason);
emit_error (context, error_text);
free (error_text);
return false;
}
}
}
assert (to - string->buffer <= string->buflen);
if (to - string->buffer != string->buflen)
markup_string_truncate (string, to - string->buffer);
*is_ascii = !(mask & 0x80);
return true;
}
static inline bool
advance_char (markup_parse_context_ty *context)
{
context->iter++;
context->char_number++;
if (context->iter == context->current_text_end)
return false;
else if (*context->iter == '\n')
{
context->line_number++;
context->char_number = 1;
}
return true;
}
static inline bool
xml_isspace (char c)
{
return c == ' ' || c == '\t' || c == '\n' || c == '\r';
}
static void
skip_spaces (markup_parse_context_ty *context)
{
do
{
if (!xml_isspace (*context->iter))
return;
}
while (advance_char (context));
}
static void
advance_to_name_end (markup_parse_context_ty *context)
{
do
{
if (IS_COMMON_NAME_END_CHAR (*(context->iter)))
return;
if (xml_isspace (*(context->iter)))
return;
}
while (advance_char (context));
}
static void
add_to_partial (markup_parse_context_ty *context,
const char *text_start,
const char *text_end)
{
if (context->partial_chunk == NULL)
{ /* allocate a new chunk to parse into */
context->partial_chunk = markup_string_new ();
}
if (text_start != text_end)
markup_string_append (context->partial_chunk,
text_start, text_end - text_start);
}
static inline void
truncate_partial (markup_parse_context_ty *context)
{
if (context->partial_chunk != NULL)
string_blank (context->partial_chunk);
}
static inline const char*
current_element (markup_parse_context_ty *context)
{
const markup_string_ty *string = gl_list_get_at (context->tag_stack, 0);
return string->buffer;
}
static void
pop_subparser_stack (markup_parse_context_ty *context)
{
markup_recursion_tracker_ty *tracker;
assert (gl_list_size (context->subparser_stack) > 0);
tracker = (markup_recursion_tracker_ty *) gl_list_get_at (context->subparser_stack, 0);
context->awaiting_pop = true;
context->user_data = tracker->prev_user_data;
context->parser = tracker->prev_parser;
context->subparser_element = tracker->prev_element;
free (tracker);
gl_list_remove_at (context->subparser_stack, 0);
}
static void
push_partial_as_tag (markup_parse_context_ty *context)
{
gl_list_add_first (context->tag_stack, context->partial_chunk);
context->partial_chunk = NULL;
}
static void
pop_tag (markup_parse_context_ty *context)
{
gl_list_remove_at (context->tag_stack, 0);
}
static void
possibly_finish_subparser (markup_parse_context_ty *context)
{
if (current_element (context) == context->subparser_element)
pop_subparser_stack (context);
}
static void
ensure_no_outstanding_subparser (markup_parse_context_ty *context)
{
context->awaiting_pop = false;
}
static void
add_attribute (markup_parse_context_ty *context, markup_string_ty *string)
{
if (context->cur_attr + 2 >= context->alloc_attrs)
{
context->alloc_attrs += 5; /* silly magic number */
context->attr_names = xrealloc (context->attr_names, sizeof (char *) * context->alloc_attrs);
context->attr_values = xrealloc (context->attr_values, sizeof(char *) * context->alloc_attrs);
}
context->cur_attr++;
context->attr_names[context->cur_attr] = xstrdup (string->buffer);
context->attr_values[context->cur_attr] = NULL;
context->attr_names[context->cur_attr+1] = NULL;
context->attr_values[context->cur_attr+1] = NULL;
}
static void
clear_attributes (markup_parse_context_ty *context)
{
/* Go ahead and free the attributes. */
for (; context->cur_attr >= 0; context->cur_attr--)
{
int pos = context->cur_attr;
free (context->attr_names[pos]);
free (context->attr_values[pos]);
context->attr_names[pos] = context->attr_values[pos] = NULL;
}
assert (context->cur_attr == -1);
assert (context->attr_names == NULL ||
context->attr_names[0] == NULL);
assert (context->attr_values == NULL ||
context->attr_values[0] == NULL);
}
static void
markup_parse_context_push (markup_parse_context_ty *context,
const markup_parser_ty *parser,
void *user_data)
{
markup_recursion_tracker_ty *tracker;
tracker = XMALLOC (markup_recursion_tracker_ty);
tracker->prev_element = context->subparser_element;
tracker->prev_parser = context->parser;
tracker->prev_user_data = context->user_data;
context->subparser_element = current_element (context);
context->parser = parser;
context->user_data = user_data;
gl_list_add_first (context->subparser_stack, tracker);
}
static void
markup_parse_context_pop (markup_parse_context_ty *context)
{
if (!context->awaiting_pop)
possibly_finish_subparser (context);
assert (context->awaiting_pop);
context->awaiting_pop = false;
}
/* This has to be a separate function to ensure the alloca's
* are unwound on exit - otherwise we grow & blow the stack
* with large documents
*/
static inline void
emit_start_element (markup_parse_context_ty *context)
{
int i, j = 0;
const char *start_name;
const char **attr_names;
const char **attr_values;
/* In case we want to ignore qualified tags and we see that we have
* one here, we push a subparser. This will ignore all tags inside of
* the qualified tag.
*
* We deal with the end of the subparser from emit_end_element.
*/
if ((context->flags & MARKUP_IGNORE_QUALIFIED)
&& strchr (current_element (context), ':'))
{
static const markup_parser_ty ignore_parser;
markup_parse_context_push (context, &ignore_parser, NULL);
clear_attributes (context);
return;
}
attr_names = XCALLOC (context->cur_attr + 2, const char *);
attr_values = XCALLOC (context->cur_attr + 2, const char *);
for (i = 0; i < context->cur_attr + 1; i++)
{
/* Possibly omit qualified attribute names from the list */
if ((context->flags & MARKUP_IGNORE_QUALIFIED)
&& strchr (context->attr_names[i], ':'))
continue;
attr_names[j] = context->attr_names[i];
attr_values[j] = context->attr_values[i];
j++;
}
attr_names[j] = NULL;
attr_values[j] = NULL;
/* Call user callback for element start */
start_name = current_element (context);
if (context->parser->start_element && name_validate (context, start_name))
(* context->parser->start_element) (context,
start_name,
(const char **)attr_names,
(const char **)attr_values,
context->user_data);
free (attr_names);
free (attr_values);
clear_attributes (context);
}
static void
emit_end_element (markup_parse_context_ty *context)
{
assert (gl_list_size (context->tag_stack) != 0);
possibly_finish_subparser (context);
/* We might have just returned from our ignore subparser */
if ((context->flags & MARKUP_IGNORE_QUALIFIED)
&& strchr (current_element (context), ':'))
{
markup_parse_context_pop (context);
pop_tag (context);
return;
}
if (context->parser->end_element)
(* context->parser->end_element) (context,
current_element (context),
context->user_data);
ensure_no_outstanding_subparser (context);
pop_tag (context);
}
/* Feed some data to the parse context. The data need not be valid
UTF-8; an error will be signaled if it's invalid. The data need
not be an entire document; you can feed a document into the parser
incrementally, via multiple calls to this function. Typically, as
you receive data from a network connection or file, you feed each
received chunk of data into this function, aborting the process if
an error occurs. Once an error is reported, no further data may be
fed to the parse context; all errors are fatal. */
bool
markup_parse_context_parse (markup_parse_context_ty *context,
const char *text,
ssize_t text_len)
{
assert (context != NULL);
assert (text != NULL);
assert (context->state != STATE_ERROR);
assert (!context->parsing);
if (text_len < 0)
text_len = strlen (text);
if (text_len == 0)
return true;
context->parsing = true;
context->current_text = text;
context->current_text_len = text_len;
context->current_text_end = context->current_text + text_len;
context->iter = context->current_text;
context->start = context->iter;
while (context->iter != context->current_text_end)
{
switch (context->state)
{
case STATE_START:
/* Possible next state: AFTER_OPEN_ANGLE */
assert (gl_list_size (context->tag_stack) == 0);
/* whitespace is ignored outside of any elements */
skip_spaces (context);
if (context->iter != context->current_text_end)
{
if (*context->iter == '<')
{
/* Move after the open angle */
advance_char (context);
context->state = STATE_AFTER_OPEN_ANGLE;
/* this could start a passthrough */
context->start = context->iter;
/* document is now non-empty */
context->document_empty = false;
}
else
{
emit_error (context,
_("document must begin with an element"));
}
}
break;
case STATE_AFTER_OPEN_ANGLE:
/* Possible next states: INSIDE_OPEN_TAG_NAME,
* AFTER_CLOSE_TAG_SLASH, INSIDE_PASSTHROUGH
*/
if (*context->iter == '?' ||
*context->iter == '!')
{
/* include < in the passthrough */
const char *openangle = "<";
add_to_partial (context, openangle, openangle + 1);
context->start = context->iter;
context->balance = 1;
context->state = STATE_INSIDE_PASSTHROUGH;
}
else if (*context->iter == '/')
{
/* move after it */
advance_char (context);
context->state = STATE_AFTER_CLOSE_TAG_SLASH;
}
else if (!IS_COMMON_NAME_END_CHAR (*(context->iter)))
{
context->state = STATE_INSIDE_OPEN_TAG_NAME;
/* start of tag name */
context->start = context->iter;
}
else
{
char *error_text = xasprintf (_("invalid character after '%s'"),
"<");
emit_error (context, error_text);
free (error_text);
}
break;
/* The AFTER_CLOSE_ANGLE state is actually sort of
* broken, because it doesn't correspond to a range
* of characters in the input stream as the others do,
* and thus makes things harder to conceptualize
*/
case STATE_AFTER_CLOSE_ANGLE:
/* Possible next states: INSIDE_TEXT, STATE_START */
if (gl_list_size (context->tag_stack) == 0)
{
context->start = NULL;
context->state = STATE_START;
}
else
{
context->start = context->iter;
context->state = STATE_INSIDE_TEXT;
}
break;
case STATE_AFTER_ELISION_SLASH:
/* Possible next state: AFTER_CLOSE_ANGLE */
if (*context->iter == '>')
{
/* move after the close angle */
advance_char (context);
context->state = STATE_AFTER_CLOSE_ANGLE;
emit_end_element (context);
}
else
{
char *error_text = xasprintf (_("missing '%c'"), '>');
emit_error (context, error_text);
free (error_text);
}
break;
case STATE_INSIDE_OPEN_TAG_NAME:
/* Possible next states: BETWEEN_ATTRIBUTES */
/* if there's a partial chunk then it's the first part of the
* tag name. If there's a context->start then it's the start
* of the tag name in current_text, the partial chunk goes
* before that start though.
*/
advance_to_name_end (context);
if (context->iter == context->current_text_end)
{
/* The name hasn't necessarily ended. Merge with
* partial chunk, leave state unchanged.
*/
add_to_partial (context, context->start, context->iter);
}
else
{
/* The name has ended. Combine it with the partial chunk
* if any; push it on the stack; enter next state.
*/
add_to_partial (context, context->start, context->iter);
push_partial_as_tag (context);
context->state = STATE_BETWEEN_ATTRIBUTES;
context->start = NULL;
}
break;
case STATE_INSIDE_ATTRIBUTE_NAME:
/* Possible next states: AFTER_ATTRIBUTE_NAME */
advance_to_name_end (context);
add_to_partial (context, context->start, context->iter);
/* read the full name, if we enter the equals sign state
* then add the attribute to the list (without the value),
* otherwise store a partial chunk to be prepended later.
*/
if (context->iter != context->current_text_end)
context->state = STATE_AFTER_ATTRIBUTE_NAME;
break;
case STATE_AFTER_ATTRIBUTE_NAME:
/* Possible next states: AFTER_ATTRIBUTE_EQUALS_SIGN */
skip_spaces (context);
if (context->iter != context->current_text_end)
{
/* The name has ended. Combine it with the partial chunk
* if any; push it on the stack; enter next state.
*/
if (!name_validate (context, context->partial_chunk->buffer))
break;
add_attribute (context, context->partial_chunk);
markup_string_free (context->partial_chunk, true);
context->partial_chunk = NULL;
context->start = NULL;
if (*context->iter == '=')
{
advance_char (context);
context->state = STATE_AFTER_ATTRIBUTE_EQUALS_SIGN;
}
else
{
char *error_text = xasprintf (_("missing '%c'"), '=');
emit_error (context, error_text);
free (error_text);
}
}
break;
case STATE_BETWEEN_ATTRIBUTES:
/* Possible next states: AFTER_CLOSE_ANGLE,
* AFTER_ELISION_SLASH, INSIDE_ATTRIBUTE_NAME
*/
skip_spaces (context);
if (context->iter != context->current_text_end)
{
if (*context->iter == '/')
{
advance_char (context);
context->state = STATE_AFTER_ELISION_SLASH;
}
else if (*context->iter == '>')
{
advance_char (context);
context->state = STATE_AFTER_CLOSE_ANGLE;
}
else if (!IS_COMMON_NAME_END_CHAR (*(context->iter)))
{
context->state = STATE_INSIDE_ATTRIBUTE_NAME;
/* start of attribute name */
context->start = context->iter;
}
else
{
char *error_text = xasprintf (_("missing '%c' or '%c'"),
'>', '/');
emit_error (context, error_text);
free (error_text);
}
/* If we're done with attributes, invoke
* the start_element callback
*/
if (context->state == STATE_AFTER_ELISION_SLASH ||
context->state == STATE_AFTER_CLOSE_ANGLE)
emit_start_element (context);
}
break;
case STATE_AFTER_ATTRIBUTE_EQUALS_SIGN:
/* Possible next state: INSIDE_ATTRIBUTE_VALUE_[SQ/DQ] */
skip_spaces (context);
if (context->iter != context->current_text_end)
{
if (*context->iter == '"')
{
advance_char (context);
context->state = STATE_INSIDE_ATTRIBUTE_VALUE_DQ;
context->start = context->iter;
}
else if (*context->iter == '\'')
{
advance_char (context);
context->state = STATE_INSIDE_ATTRIBUTE_VALUE_SQ;
context->start = context->iter;
}
else
{
char *error_text = xasprintf (_("missing '%c' or '%c'"),
'\'', '"');
emit_error (context, error_text);
free (error_text);
}
}
break;
case STATE_INSIDE_ATTRIBUTE_VALUE_SQ:
case STATE_INSIDE_ATTRIBUTE_VALUE_DQ:
/* Possible next states: BETWEEN_ATTRIBUTES */
{
char delim;
if (context->state == STATE_INSIDE_ATTRIBUTE_VALUE_SQ)
{
delim = '\'';
}
else
{
delim = '"';
}
do
{
if (*context->iter == delim)
break;
}
while (advance_char (context));
}
if (context->iter == context->current_text_end)
{
/* The value hasn't necessarily ended. Merge with
* partial chunk, leave state unchanged.
*/
add_to_partial (context, context->start, context->iter);
}
else
{
bool is_ascii;
/* The value has ended at the quote mark. Combine it
* with the partial chunk if any; set it for the current
* attribute.
*/
add_to_partial (context, context->start, context->iter);
assert (context->cur_attr >= 0);
if (unescape_string_inplace (context, context->partial_chunk,
&is_ascii)
&& (is_ascii
|| text_validate (context,
context->partial_chunk->buffer,
context->partial_chunk->buflen)))
{
/* success, advance past quote and set state. */
context->attr_values[context->cur_attr] =
markup_string_free (context->partial_chunk, false);
context->partial_chunk = NULL;
advance_char (context);
context->state = STATE_BETWEEN_ATTRIBUTES;
context->start = NULL;
}
truncate_partial (context);
}
break;
case STATE_INSIDE_TEXT:
/* Possible next states: AFTER_OPEN_ANGLE */
do
{
if (*context->iter == '<')
break;
}
while (advance_char (context));
/* The text hasn't necessarily ended. Merge with
* partial chunk, leave state unchanged.
*/
add_to_partial (context, context->start, context->iter);
if (context->iter != context->current_text_end)
{
bool is_ascii;
/* The text has ended at the open angle. Call the text
* callback.
*/
if (unescape_string_inplace (context, context->partial_chunk,
&is_ascii)
&& (is_ascii
|| text_validate (context,
context->partial_chunk->buffer,
context->partial_chunk->buflen)))
{
if (context->parser->text)
(*context->parser->text) (context,
context->partial_chunk->buffer,
context->partial_chunk->buflen,
context->user_data);
/* advance past open angle and set state. */
advance_char (context);
context->state = STATE_AFTER_OPEN_ANGLE;
/* could begin a passthrough */
context->start = context->iter;
}
truncate_partial (context);
}
break;
case STATE_AFTER_CLOSE_TAG_SLASH:
/* Possible next state: INSIDE_CLOSE_TAG_NAME */
if (!IS_COMMON_NAME_END_CHAR (*(context->iter)))
{
context->state = STATE_INSIDE_CLOSE_TAG_NAME;
/* start of tag name */
context->start = context->iter;
}
else
{
char *error_text = xasprintf (_("invalid character after '%s'"),
"</");
emit_error (context, error_text);
free (error_text);
}
break;
case STATE_INSIDE_CLOSE_TAG_NAME:
/* Possible next state: AFTER_CLOSE_TAG_NAME */
advance_to_name_end (context);
add_to_partial (context, context->start, context->iter);
if (context->iter != context->current_text_end)
context->state = STATE_AFTER_CLOSE_TAG_NAME;
break;
case STATE_AFTER_CLOSE_TAG_NAME:
/* Possible next state: AFTER_CLOSE_TAG_SLASH */
skip_spaces (context);
if (context->iter != context->current_text_end)
{
markup_string_ty *close_name;
close_name = context->partial_chunk;
context->partial_chunk = NULL;
if (*context->iter != '>')
{
char *error_text =
xasprintf (_("invalid character after '%s'"),
_("a close element name"));
emit_error (context, error_text);
free (error_text);
}
else if (gl_list_size (context->tag_stack) == 0)
{
emit_error (context, _("element is closed"));
}
else if (strcmp (close_name->buffer, current_element (context))
!= 0)
{
emit_error (context, _("element is closed"));
}
else
{
advance_char (context);
context->state = STATE_AFTER_CLOSE_ANGLE;
context->start = NULL;
emit_end_element (context);
}
context->partial_chunk = close_name;
truncate_partial (context);
}
break;
case STATE_INSIDE_PASSTHROUGH:
/* Possible next state: AFTER_CLOSE_ANGLE */
do
{
if (*context->iter == '<')
context->balance++;
if (*context->iter == '>')
{
char *str;
size_t len;
context->balance--;
add_to_partial (context, context->start, context->iter);
context->start = context->iter;
str = context->partial_chunk->buffer;
len = context->partial_chunk->buflen;
if (str[1] == '?' && str[len - 1] == '?')
break;
if (strncmp (str, "<!--", 4) == 0 &&
strcmp (str + len - 2, "--") == 0)
break;
if (strncmp (str, "<![CDATA[", 9) == 0 &&
strcmp (str + len - 2, "]]") == 0)
break;
if (strncmp (str, "<!DOCTYPE", 9) == 0 &&
context->balance == 0)
break;
}
}
while (advance_char (context));
if (context->iter == context->current_text_end)
{
/* The passthrough hasn't necessarily ended. Merge with
* partial chunk, leave state unchanged.
*/
add_to_partial (context, context->start, context->iter);
}
else
{
/* The passthrough has ended at the close angle. Combine
* it with the partial chunk if any. Call the passthrough
* callback. Note that the open/close angles are
* included in the text of the passthrough.
*/
advance_char (context); /* advance past close angle */
add_to_partial (context, context->start, context->iter);
if (context->flags & MARKUP_TREAT_CDATA_AS_TEXT &&
strncmp (context->partial_chunk->buffer, "<![CDATA[", 9) == 0)
{
if (context->parser->text &&
text_validate (context,
context->partial_chunk->buffer + 9,
context->partial_chunk->buflen - 12))
(*context->parser->text) (context,
context->partial_chunk->buffer + 9,
context->partial_chunk->buflen - 12,
context->user_data);
}
else if (context->parser->passthrough &&
text_validate (context,
context->partial_chunk->buffer,
context->partial_chunk->buflen))
(*context->parser->passthrough) (context,
context->partial_chunk->buffer,
context->partial_chunk->buflen,
context->user_data);
truncate_partial (context);
context->state = STATE_AFTER_CLOSE_ANGLE;
context->start = context->iter; /* could begin text */
}
break;
case STATE_ERROR:
goto finished;
break;
default:
abort ();
break;
}
}
finished:
context->parsing = false;
return context->state != STATE_ERROR;
}
/* Signals to the parse context that all data has been fed into the
* parse context with markup_parse_context_parse.
*
* This function reports an error if the document isn't complete,
* for example if elements are still open. */
bool
markup_parse_context_end_parse (markup_parse_context_ty *context)
{
const char *location = NULL;
assert (context != NULL);
assert (!context->parsing);
assert (context->state != STATE_ERROR);
if (context->partial_chunk != NULL)
{
markup_string_free (context->partial_chunk, true);
context->partial_chunk = NULL;
}
if (context->document_empty)
{
emit_error (context, _("empty document"));
return false;
}
context->parsing = true;
switch (context->state)
{
case STATE_START:
/* Nothing to do */
break;
case STATE_AFTER_OPEN_ANGLE:
location = _("after '<'");
break;
case STATE_AFTER_CLOSE_ANGLE:
if (gl_list_size (context->tag_stack) > 0)
{
/* Error message the same as for INSIDE_TEXT */
location = _("elements still open");
}
break;
case STATE_AFTER_ELISION_SLASH:
location = _("missing '>'");
break;
case STATE_INSIDE_OPEN_TAG_NAME:
location = _("inside an element name");
break;
case STATE_INSIDE_ATTRIBUTE_NAME:
case STATE_AFTER_ATTRIBUTE_NAME:
location = _("inside an attribute name");
break;
case STATE_BETWEEN_ATTRIBUTES:
location = _("inside an open tag");
break;
case STATE_AFTER_ATTRIBUTE_EQUALS_SIGN:
location = _("after '='");
break;
case STATE_INSIDE_ATTRIBUTE_VALUE_SQ:
case STATE_INSIDE_ATTRIBUTE_VALUE_DQ:
location = _("inside an attribute value");
break;
case STATE_INSIDE_TEXT:
assert (gl_list_size (context->tag_stack) > 0);
location = _("elements still open");
break;
case STATE_AFTER_CLOSE_TAG_SLASH:
case STATE_INSIDE_CLOSE_TAG_NAME:
case STATE_AFTER_CLOSE_TAG_NAME:
location = _("inside the close tag");
break;
case STATE_INSIDE_PASSTHROUGH:
location = _("inside a comment or processing instruction");
break;
case STATE_ERROR:
default:
abort ();
break;
}
if (location != NULL)
{
char *error_text = xasprintf (_("document ended unexpectedly: %s"),
location);
emit_error (context, error_text);
free (error_text);
}
context->parsing = false;
return context->state != STATE_ERROR;
}
const char *
markup_parse_context_get_error (markup_parse_context_ty *context)
{
return context->error_text;
}
|