File size: 41,379 Bytes
6baed57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*
 * error.c: module displaying/handling XML parser errors
 *
 * See Copyright for the status of this software.
 *
 * Author: Daniel Veillard
 */

#define IN_LIBXML
#include "libxml.h"

#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <libxml/parser.h>
#include <libxml/xmlerror.h>
#include <libxml/xmlmemory.h>

#include "private/error.h"
#include "private/globals.h"
#include "private/parser.h"
#include "private/string.h"

/**
 * This currently comprises
 *
 * - OOM errors
 * - assertion failures
 * - invalid argument errors
 * - I/O errors
 * - unexpected errors from external libraries
 *
 * @param level  error level
 * @param code  error code
 * @returns true if an error is catastrophic.
 */
int
xmlIsCatastrophicError(int level, int code) {
    int fatal = 0;

    if (level != XML_ERR_FATAL)
        return(0);

    switch (code) {
        case XML_ERR_NO_MEMORY:
        /* case XML_ERR_RESOURCE_LIMIT: */
        case XML_ERR_SYSTEM:
        case XML_ERR_ARGUMENT:
        case XML_ERR_INTERNAL_ERROR:
            fatal = 1;
            break;
        default:
            if ((code >= 1500) && (code <= 1599))
                fatal = 1;
            break;
    }

    return(fatal);
}

/************************************************************************
 *									*
 *			Error struct					*
 *									*
 ************************************************************************/

static int
xmlVSetError(xmlError *err,
             void *ctxt, xmlNodePtr node,
             int domain, int code, xmlErrorLevel level,
             const char *file, int line,
             const char *str1, const char *str2, const char *str3,
             int int1, int col,
             const char *fmt, va_list ap)
{
    char *message = NULL;
    char *fileCopy = NULL;
    char *str1Copy = NULL;
    char *str2Copy = NULL;
    char *str3Copy = NULL;

    if (code == XML_ERR_OK) {
        xmlResetError(err);
        return(0);
    }

    /*
     * Formatting the message
     */
    if (fmt == NULL) {
        message = xmlMemStrdup("No error message provided");
    } else {
        xmlChar *tmp;
        int res;

        res = xmlStrVASPrintf(&tmp, MAX_ERR_MSG_SIZE, fmt, ap);
        if (res < 0)
            goto err_memory;
        message = (char *) tmp;
    }
    if (message == NULL)
        goto err_memory;

    if (file != NULL) {
        fileCopy = (char *) xmlStrdup((const xmlChar *) file);
        if (fileCopy == NULL)
            goto err_memory;
    }
    if (str1 != NULL) {
        str1Copy = (char *) xmlStrdup((const xmlChar *) str1);
        if (str1Copy == NULL)
            goto err_memory;
    }
    if (str2 != NULL) {
        str2Copy = (char *) xmlStrdup((const xmlChar *) str2);
        if (str2Copy == NULL)
            goto err_memory;
    }
    if (str3 != NULL) {
        str3Copy = (char *) xmlStrdup((const xmlChar *) str3);
        if (str3Copy == NULL)
            goto err_memory;
    }

    xmlResetError(err);

    err->domain = domain;
    err->code = code;
    err->message = message;
    err->level = level;
    err->file = fileCopy;
    err->line = line;
    err->str1 = str1Copy;
    err->str2 = str2Copy;
    err->str3 = str3Copy;
    err->int1 = int1;
    err->int2 = col;
    err->node = node;
    err->ctxt = ctxt;

    return(0);

err_memory:
    xmlFree(message);
    xmlFree(fileCopy);
    xmlFree(str1Copy);
    xmlFree(str2Copy);
    xmlFree(str3Copy);
    return(-1);
}

static int LIBXML_ATTR_FORMAT(14,15)
xmlSetError(xmlError *err,
            void *ctxt, xmlNodePtr node,
            int domain, int code, xmlErrorLevel level,
            const char *file, int line,
            const char *str1, const char *str2, const char *str3,
            int int1, int col,
            const char *fmt, ...)
{
    va_list ap;
    int res;

    va_start(ap, fmt);
    res = xmlVSetError(err, ctxt, node, domain, code, level, file, line,
                       str1, str2, str3, int1, col, fmt, ap);
    va_end(ap);

    return(res);
}

static int
xmlVUpdateError(xmlError *err,
                void *ctxt, xmlNodePtr node,
                int domain, int code, xmlErrorLevel level,
                const char *file, int line,
                const char *str1, const char *str2, const char *str3,
                int int1, int col,
                const char *fmt, va_list ap)
{
    int res;

    /*
     * Find first element parent.
     */
    if (node != NULL) {
        int i;

        for (i = 0; i < 10; i++) {
            if ((node->type == XML_ELEMENT_NODE) ||
                (node->parent == NULL))
                break;
            node = node->parent;
        }
    }

    /*
     * Get file and line from node.
     */
    if (node != NULL) {
        if ((file == NULL) && (node->doc != NULL))
            file = (const char *) node->doc->URL;

        if (line == 0) {
            if (node->type == XML_ELEMENT_NODE)
                line = node->line;
            if ((line == 0) || (line == 65535))
                line = xmlGetLineNo(node);
        }
    }

    res = xmlVSetError(err, ctxt, node, domain, code, level, file, line,
                       str1, str2, str3, int1, col, fmt, ap);

    return(res);
}

/************************************************************************
 *									*
 *			Handling of out of context errors		*
 *									*
 ************************************************************************/

/**
 * Default handler for out-of-context error messages.
 *
 * @param ctx  user data (unused)
 * @param msg  printf-like format string
 * @param ...  arguments to format
 */
void
xmlGenericErrorDefaultFunc(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...) {
    va_list args;

    if (xmlGenericErrorContext == NULL)
	xmlGenericErrorContext = (void *) stderr;

    va_start(args, msg);
    vfprintf((FILE *)xmlGenericErrorContext, msg, args);
    va_end(args);
}

/**
 * Set the thread-local "generic" handler and context for error
 * messages.
 *
 * @deprecated See #xmlSetStructuredErrorFunc for alternatives.
 *
 * If you only want to disable parser errors being printed to
 * stderr, use xmlParserOption XML_PARSE_NOERROR.
 *
 * The generic error handler will only receive fragments of
 * error messages which should be concatenated or printed to a
 * stream.
 *
 * If `handler` is NULL, use the built-in default handler which prints
 * to stderr.
 *
 * Since this is a thread-local setting, it's a good idea to reset
 * the error handler to its default value after collecting the
 * errors you're interested in. To get the original values, you
 * have to access xmlGenericError and xmlGenericErrorContext
 * directly, making this function kind of useless.
 *
 * For multi-threaded applications, this must be set separately for
 * each thread.
 *
 * @param ctx  the new error handling context
 * @param handler  the new handler function
 */
void
xmlSetGenericErrorFunc(void *ctx, xmlGenericErrorFunc handler) {
    xmlGenericErrorContext = ctx;
    if (handler != NULL)
	xmlGenericError = handler;
    else
	xmlGenericError = xmlGenericErrorDefaultFunc;
}

/**
 * Set the thread-local "structured" handler and context for error
 * messages.
 *
 * @deprecated Use a per-context error handler.
 *
 * If you only want to disable parser errors being printed to
 * stderr, use xmlParserOption XML_PARSE_NOERROR.
 *
 * It's recommended to use the per-context error handlers instead:
 *
 * - #xmlCtxtSetErrorHandler (since 2.13.0)
 * - #xmlTextReaderSetStructuredErrorHandler
 * - #xmlXPathSetErrorHandler (since 2.13.0)
 * - #xmlXIncludeSetErrorHandler (since 2.13.0)
 * - #xmlSchemaSetParserStructuredErrors
 * - #xmlSchemaSetValidStructuredErrors
 * - #xmlRelaxNGSetParserStructuredErrors
 * - #xmlRelaxNGSetValidStructuredErrors
 *
 * If `handler` is NULL, the error handler is deactivated.
 *
 * The structured error handler takes precedence over "generic"
 * handlers, even per-context generic handlers.
 *
 * Since this is a thread-local setting, it's a good idea to reset
 * the error handler to its default value after collecting the
 * errors you're interested in. To get the original values, you
 * have to access xmlStructuredError and xmlStructuredErrorContext
 * directly, making this function kind of useless.
 *
 * For multi-threaded applications, this must be set separately for
 * each thread.
 *
 * @param ctx  the new error handling context
 * @param handler  the new handler function
 */
void
xmlSetStructuredErrorFunc(void *ctx, xmlStructuredErrorFunc handler) {
    xmlStructuredErrorContext = ctx;
    xmlStructuredError = handler;
}

/************************************************************************
 *									*
 *			Handling of parsing errors			*
 *									*
 ************************************************************************/

/**
 * Displays the associated file and line information for the
 * current input.
 *
 * @deprecated Use #xmlFormatError.
 *
 * @param input  an xmlParserInput input
 */

void
xmlParserPrintFileInfo(struct _xmlParserInput *input) {
    if (input != NULL) {
	if (input->filename)
	    xmlGenericError(xmlGenericErrorContext,
		    "%s:%d: ", input->filename,
		    input->line);
	else
	    xmlGenericError(xmlGenericErrorContext,
		    "Entity: line %d: ", input->line);
    }
}

/**
 * Displays current context within the input content for
 * error reporting.
 *
 * @param input  an xmlParserInput input
 * @param channel  output callback
 * @param data  user data for output callback
 */

static void
xmlParserPrintFileContextInternal(xmlParserInputPtr input ,
		xmlGenericErrorFunc channel, void *data ) {
    const xmlChar *start;
    int n, col;
    xmlChar content[81]; /* space for 80 chars + line terminator */

    if ((input == NULL) || (input->cur == NULL))
        return;

    n = sizeof(content) - 1;
    xmlParserInputGetWindow(input, &start, &n, &col);

    memcpy(content, start, n);
    content[n] = 0;
    /* print out the selected text */
    channel(data ,"%s\n", content);
    /* create blank line with problem pointer */
    for (n = 0; n < col; n++) {
	if (content[n] != '\t')
	    content[n] = ' ';
    }
    content[n++] = '^';
    content[n] = 0;
    channel(data ,"%s\n", content);
}

/**
 * Displays current context within the input content for
 * error reporting.
 *
 * @deprecated Use #xmlFormatError.
 *
 * @param input  an xmlParserInput input
 */
void
xmlParserPrintFileContext(struct _xmlParserInput *input) {
   xmlParserPrintFileContextInternal(input, xmlGenericError,
                                     xmlGenericErrorContext);
}

/**
 * Report a formatted error to a printf-like callback.
 *
 * This can result in a verbose multi-line report including additional
 * information from the parser context.
 *
 * @since 2.13.0
 * @param err  the error
 * @param channel  callback
 * @param data  user data for callback
 */
void
xmlFormatError(const xmlError *err, xmlGenericErrorFunc channel, void *data)
{
    const char *message;
    const char *file;
    int line;
    int code;
    int domain;
    const xmlChar *name = NULL;
    xmlNodePtr node;
    xmlErrorLevel level;
    xmlParserCtxtPtr ctxt = NULL;
    xmlParserInputPtr input = NULL;
    xmlParserInputPtr cur = NULL;

    if ((err == NULL) || (channel == NULL))
        return;

    message = err->message;
    file = err->file;
    line = err->line;
    code = err->code;
    domain = err->domain;
    level = err->level;
    node = err->node;

    if (code == XML_ERR_OK)
        return;

    if ((domain == XML_FROM_PARSER) || (domain == XML_FROM_HTML) ||
        (domain == XML_FROM_DTD) || (domain == XML_FROM_NAMESPACE) ||
	(domain == XML_FROM_IO) || (domain == XML_FROM_VALID)) {
	ctxt = err->ctxt;
    }

    if ((node != NULL) && (node->type == XML_ELEMENT_NODE) &&
        (domain != XML_FROM_SCHEMASV))
        name = node->name;

    /*
     * Maintain the compatibility with the legacy error handling
     */
    if ((ctxt != NULL) && (ctxt->input != NULL)) {
        input = ctxt->input;
        if ((input->filename == NULL) &&
            (ctxt->inputNr > 1)) {
            cur = input;
            input = ctxt->inputTab[ctxt->inputNr - 2];
        }
        if (input->filename)
            channel(data, "%s:%d: ", input->filename, input->line);
        else if ((line != 0) && (domain == XML_FROM_PARSER))
            channel(data, "Entity: line %d: ", input->line);
    } else {
        if (file != NULL)
            channel(data, "%s:%d: ", file, line);
        else if ((line != 0) &&
	         ((domain == XML_FROM_PARSER) || (domain == XML_FROM_SCHEMASV)||
		  (domain == XML_FROM_SCHEMASP)||(domain == XML_FROM_DTD) ||
		  (domain == XML_FROM_RELAXNGP)||(domain == XML_FROM_RELAXNGV)))
            channel(data, "Entity: line %d: ", line);
    }
    if (name != NULL) {
        channel(data, "element %s: ", name);
    }
    switch (domain) {
        case XML_FROM_PARSER:
            channel(data, "parser ");
            break;
        case XML_FROM_NAMESPACE:
            channel(data, "namespace ");
            break;
        case XML_FROM_DTD:
        case XML_FROM_VALID:
            channel(data, "validity ");
            break;
        case XML_FROM_HTML:
            channel(data, "HTML parser ");
            break;
        case XML_FROM_MEMORY:
            channel(data, "memory ");
            break;
        case XML_FROM_OUTPUT:
            channel(data, "output ");
            break;
        case XML_FROM_IO:
            channel(data, "I/O ");
            break;
        case XML_FROM_XINCLUDE:
            channel(data, "XInclude ");
            break;
        case XML_FROM_XPATH:
            channel(data, "XPath ");
            break;
        case XML_FROM_XPOINTER:
            channel(data, "parser ");
            break;
        case XML_FROM_REGEXP:
            channel(data, "regexp ");
            break;
        case XML_FROM_MODULE:
            channel(data, "module ");
            break;
        case XML_FROM_SCHEMASV:
            channel(data, "Schemas validity ");
            break;
        case XML_FROM_SCHEMASP:
            channel(data, "Schemas parser ");
            break;
        case XML_FROM_RELAXNGP:
            channel(data, "Relax-NG parser ");
            break;
        case XML_FROM_RELAXNGV:
            channel(data, "Relax-NG validity ");
            break;
        case XML_FROM_CATALOG:
            channel(data, "Catalog ");
            break;
        case XML_FROM_C14N:
            channel(data, "C14N ");
            break;
        case XML_FROM_XSLT:
            channel(data, "XSLT ");
            break;
        case XML_FROM_I18N:
            channel(data, "encoding ");
            break;
        case XML_FROM_SCHEMATRONV:
            channel(data, "schematron ");
            break;
        case XML_FROM_BUFFER:
            channel(data, "internal buffer ");
            break;
        case XML_FROM_URI:
            channel(data, "URI ");
            break;
        default:
            break;
    }
    switch (level) {
        case XML_ERR_NONE:
            channel(data, ": ");
            break;
        case XML_ERR_WARNING:
            channel(data, "warning : ");
            break;
        case XML_ERR_ERROR:
            channel(data, "error : ");
            break;
        case XML_ERR_FATAL:
            channel(data, "error : ");
            break;
    }
    if (message != NULL) {
        int len;
	len = xmlStrlen((const xmlChar *) message);
	if ((len > 0) && (message[len - 1] != '\n'))
	    channel(data, "%s\n", message);
	else
	    channel(data, "%s", message);
    } else {
        channel(data, "%s\n", "No error message provided");
    }

    if (ctxt != NULL) {
        if ((input != NULL) &&
            ((input->buf == NULL) || (input->buf->encoder == NULL)) &&
            (code == XML_ERR_INVALID_ENCODING) &&
            (input->cur < input->end)) {
            int i;

            channel(data, "Bytes:");
            for (i = 0; i < 4; i++) {
                if (input->cur + i >= input->end)
                    break;
                channel(data, " 0x%02X", input->cur[i]);
            }
            channel(data, "\n");
        }

        xmlParserPrintFileContextInternal(input, channel, data);

        if (cur != NULL) {
            if (cur->filename)
                channel(data, "%s:%d: \n", cur->filename, cur->line);
            else if ((line != 0) &&
                     ((domain == XML_FROM_PARSER) ||
                      (domain == XML_FROM_SCHEMASV) ||
                      (domain == XML_FROM_SCHEMASP) ||
                      (domain == XML_FROM_DTD) ||
                      (domain == XML_FROM_RELAXNGP) ||
                      (domain == XML_FROM_RELAXNGV)))
                channel(data, "Entity: line %d: \n", cur->line);
            xmlParserPrintFileContextInternal(cur, channel, data);
        }
    }
    if ((domain == XML_FROM_XPATH) && (err->str1 != NULL) &&
        (err->int1 < 100) &&
	(err->int1 < xmlStrlen((const xmlChar *)err->str1))) {
	xmlChar buf[150];
	int i;

	channel(data, "%s\n", err->str1);
	for (i=0;i < err->int1;i++)
	     buf[i] = ' ';
	buf[i++] = '^';
	buf[i] = 0;
	channel(data, "%s\n", buf);
    }
}

/**
 * Update the global and optional error structure, then forward the
 * error to an error handler.
 *
 * This function doesn't make memory allocations which are likely
 * to fail after an OOM error.
 *
 * @param schannel  the structured callback channel
 * @param channel  the old callback channel
 * @param data  the callback data
 * @param domain  the domain for the error
 * @param error  optional error struct to be filled
 */
void
xmlRaiseMemoryError(xmlStructuredErrorFunc schannel, xmlGenericErrorFunc channel,
                    void *data, int domain, xmlError *error)
{
    xmlError *lastError = xmlGetLastErrorInternal();

    xmlResetLastError();
    lastError->domain = domain;
    lastError->code = XML_ERR_NO_MEMORY;
    lastError->level = XML_ERR_FATAL;

    if (error != NULL) {
        xmlResetError(error);
        error->domain = domain;
        error->code = XML_ERR_NO_MEMORY;
        error->level = XML_ERR_FATAL;
    }

    if (schannel != NULL) {
        schannel(data, lastError);
    } else if (xmlStructuredError != NULL) {
        xmlStructuredError(xmlStructuredErrorContext, lastError);
    } else if (channel != NULL) {
        channel(data, "libxml2: out of memory\n");
    }
}

/**
 * Update the appropriate global or contextual error structure,
 * then forward the error message down the parser or generic
 * error callback handler
 *
 * @param schannel  the structured callback channel
 * @param channel  the old callback channel
 * @param data  the callback data
 * @param ctx  the parser context or NULL
 * @param node  the current node or NULL
 * @param domain  the domain for the error
 * @param code  the code for the error
 * @param level  the xmlErrorLevel for the error
 * @param file  the file source of the error (or NULL)
 * @param line  the line of the error or 0 if N/A
 * @param str1  extra string info
 * @param str2  extra string info
 * @param str3  extra string info
 * @param int1  extra int info
 * @param col  column number of the error or 0 if N/A
 * @param msg  the message to display/transmit
 * @param ap  extra parameters for the message display
 * @returns 0 on success, -1 if a memory allocation failed.
 */
int
xmlVRaiseError(xmlStructuredErrorFunc schannel,
               xmlGenericErrorFunc channel, void *data, void *ctx,
               xmlNode *node, int domain, int code, xmlErrorLevel level,
               const char *file, int line, const char *str1,
               const char *str2, const char *str3, int int1, int col,
               const char *msg, va_list ap)
{
    xmlParserCtxtPtr ctxt = NULL;
    /* xmlLastError is a macro retrieving the per-thread global. */
    xmlErrorPtr lastError = xmlGetLastErrorInternal();
    xmlErrorPtr to = lastError;

    if (code == XML_ERR_OK)
        return(0);
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
    if (code == XML_ERR_INTERNAL_ERROR)
        xmlAbort("Unexpected internal error: %s\n", msg);
#endif
    if ((xmlGetWarningsDefaultValue == 0) && (level == XML_ERR_WARNING))
        return(0);

    if ((domain == XML_FROM_PARSER) || (domain == XML_FROM_HTML) ||
        (domain == XML_FROM_DTD) || (domain == XML_FROM_NAMESPACE) ||
	(domain == XML_FROM_IO) || (domain == XML_FROM_VALID)) {
	ctxt = (xmlParserCtxtPtr) ctx;

        if (ctxt != NULL)
            to = &ctxt->lastError;
    }

    if (xmlVUpdateError(to, ctxt, node, domain, code, level, file, line,
                        str1, str2, str3, int1, col, msg, ap))
        return(-1);

    if (to != lastError) {
        if (xmlCopyError(to, lastError) < 0)
            return(-1);
    }

    if (schannel != NULL) {
	schannel(data, to);
    } else if (xmlStructuredError != NULL) {
        xmlStructuredError(xmlStructuredErrorContext, to);
    } else if (channel != NULL) {
        /* Don't invoke legacy error handlers */
        if ((channel == xmlGenericErrorDefaultFunc) ||
            (channel == xmlParserError) ||
            (channel == xmlParserWarning) ||
            (channel == xmlParserValidityError) ||
            (channel == xmlParserValidityWarning))
            xmlFormatError(to, xmlGenericError, xmlGenericErrorContext);
        else
	    channel(data, "%s", to->message);
    }

    return(0);
}

/**
 * Update the appropriate global or contextual error structure,
 * then forward the error message down the parser or generic
 * error callback handler
 *
 * @param schannel  the structured callback channel
 * @param channel  the old callback channel
 * @param data  the callback data
 * @param ctx  the parser context or NULL
 * @param node  the node or NULL
 * @param domain  the domain for the error
 * @param code  the code for the error
 * @param level  the xmlErrorLevel for the error
 * @param file  the file source of the error (or NULL)
 * @param line  the line of the error or 0 if N/A
 * @param str1  extra string info
 * @param str2  extra string info
 * @param str3  extra string info
 * @param int1  extra int info
 * @param col  column number of the error or 0 if N/A
 * @param msg  printf-like format string
 * @param ...  arguments to format
 * @returns 0 on success, -1 if a memory allocation failed.
 */
int
xmlRaiseError(xmlStructuredErrorFunc schannel,
              xmlGenericErrorFunc channel, void *data, void *ctx,
              xmlNode *node, int domain, int code, xmlErrorLevel level,
              const char *file, int line, const char *str1,
              const char *str2, const char *str3, int int1, int col,
              const char *msg, ...)
{
    va_list ap;
    int res;

    va_start(ap, msg);
    res = xmlVRaiseError(schannel, channel, data, ctx, node, domain, code,
                         level, file, line, str1, str2, str3, int1, col, msg,
                         ap);
    va_end(ap);

    return(res);
}

static void
xmlVFormatLegacyError(void *ctx, const char *level,
                      const char *fmt, va_list ap) {
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
    xmlParserInputPtr input = NULL;
    xmlParserInputPtr cur = NULL;
    xmlChar *str = NULL;

    if (ctxt != NULL) {
	input = ctxt->input;
	if ((input != NULL) && (input->filename == NULL) &&
	    (ctxt->inputNr > 1)) {
	    cur = input;
	    input = ctxt->inputTab[ctxt->inputNr - 2];
	}
	xmlParserPrintFileInfo(input);
    }

    xmlGenericError(xmlGenericErrorContext, "%s: ", level);

    xmlStrVASPrintf(&str, MAX_ERR_MSG_SIZE, fmt, ap);
    if (str != NULL) {
        xmlGenericError(xmlGenericErrorContext, "%s", (char *) str);
	xmlFree(str);
    }

    if (ctxt != NULL) {
	xmlParserPrintFileContext(input);
	if (cur != NULL) {
	    xmlParserPrintFileInfo(cur);
	    xmlGenericError(xmlGenericErrorContext, "\n");
	    xmlParserPrintFileContext(cur);
	}
    }
}

/**
 * This is the default SAX error handler, but it will never be
 * called. If it isn't replaced by the user, errors will be
 * handled by #xmlFormatError.
 *
 * @deprecated Do not call directly.
 *
 * Format an error message with additional detail from the
 * parser context and print to generic error handler.
 *
 * @param ctx  an XML parser context
 * @param msg  printf-like format string
 * @param ...  arguments to format
 */
void
xmlParserError(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...)
{
    va_list ap;

    va_start(ap, msg);
    xmlVFormatLegacyError(ctx, "error", msg, ap);
    va_end(ap);
}

/**
 * This is the default SAX warning handler, but it will never be
 * called. If it isn't replaced by the user, warnings will be
 * handled by #xmlFormatError.
 *
 * @deprecated Do not call directly.
 *
 * Format an warning message with additional detail from the
 * parser context and print to generic error handler.
 *
 * @param ctx  an XML parser context
 * @param msg  printf-like format string
 * @param ...  arguments to format
 */
void
xmlParserWarning(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...)
{
    va_list ap;

    va_start(ap, msg);
    xmlVFormatLegacyError(ctx, "warning", msg, ap);
    va_end(ap);
}

/**
 * This is the default validity error handler, but it will never be
 * called. If it isn't replaced by the user, errors will be
 * handled by #xmlFormatError.
 *
 * @deprecated Do not call directly.
 *
 * Format an error message with additional detail from the
 * parser context and print to generic error handler.
 *
 * @param ctx  an XML parser context
 * @param msg  printf-like format string
 * @param ...  arguments to format
 */
void
xmlParserValidityError(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...)
{
    va_list ap;

    va_start(ap, msg);
    xmlVFormatLegacyError(ctx, "validity error", msg, ap);
    va_end(ap);
}

/**
 * This is the default validity warning handler, but it will never
 * be called. If it isn't replaced by the user, warnings will be
 * handled by #xmlFormatError.
 *
 * @deprecated Do not call directly.
 *
 * Format an warning message with additional detail from the
 * parser context and print to generic error handler.
 *
 * @param ctx  an XML parser context
 * @param msg  printf-like format string
 * @param ...  arguments to format
 */
void
xmlParserValidityWarning(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...)
{
    va_list ap;

    va_start(ap, msg);
    xmlVFormatLegacyError(ctx, "validity warning", msg, ap);
    va_end(ap);
}


/************************************************************************
 *									*
 *			Extended Error Handling				*
 *									*
 ************************************************************************/

/**
 * Get the last error raised in this thread.
 *
 * @returns a pointer to the error
 */
const xmlError *
xmlGetLastError(void)
{
    const xmlError *error = xmlGetLastErrorInternal();

    if (error->code == XML_ERR_OK)
        return(NULL);
    return(error);
}

/**
 * Reset the error to success.
 *
 * @param err  pointer to the error
 */
void
xmlResetError(xmlError *err)
{
    if (err == NULL)
        return;
    if (err->code == XML_ERR_OK)
        return;
    if (err->message != NULL)
        xmlFree(err->message);
    if (err->file != NULL)
        xmlFree(err->file);
    if (err->str1 != NULL)
        xmlFree(err->str1);
    if (err->str2 != NULL)
        xmlFree(err->str2);
    if (err->str3 != NULL)
        xmlFree(err->str3);
    memset(err, 0, sizeof(xmlError));
    err->code = XML_ERR_OK;
}

/**
 * Reset the last error to success.
 */
void
xmlResetLastError(void)
{
    xmlError *error = xmlGetLastErrorInternal();

    if (error->code != XML_ERR_OK)
        xmlResetError(error);
}

/**
 * Copy an error.
 *
 * @param from  a source error
 * @param to  a target error
 * @returns 0 in case of success and -1 in case of error.
 */
int
xmlCopyError(const xmlError *from, xmlError *to) {
    const char *fmt = NULL;

    if ((from == NULL) || (to == NULL))
        return(-1);

    if (from->message != NULL)
        fmt = "%s";

    return(xmlSetError(to, from->ctxt, from->node,
                       from->domain, from->code, from->level,
                       from->file, from->line,
                       from->str1, from->str2, from->str3,
                       from->int1, from->int2,
                       fmt, from->message));
}

/**
 * @param code  an xmlParserErrors code
 * @returns an error message for a code.
 */
const char *
xmlErrString(xmlParserErrors code) {
    const char *errmsg;

    switch (code) {
        case XML_ERR_INVALID_HEX_CHARREF:
            errmsg = "CharRef: invalid hexadecimal value";
            break;
        case XML_ERR_INVALID_DEC_CHARREF:
            errmsg = "CharRef: invalid decimal value";
            break;
        case XML_ERR_INVALID_CHARREF:
            errmsg = "CharRef: invalid value";
            break;
        case XML_ERR_INTERNAL_ERROR:
            errmsg = "internal error";
            break;
        case XML_ERR_PEREF_AT_EOF:
            errmsg = "PEReference at end of document";
            break;
        case XML_ERR_PEREF_IN_PROLOG:
            errmsg = "PEReference in prolog";
            break;
        case XML_ERR_PEREF_IN_EPILOG:
            errmsg = "PEReference in epilog";
            break;
        case XML_ERR_PEREF_NO_NAME:
            errmsg = "PEReference: no name";
            break;
        case XML_ERR_PEREF_SEMICOL_MISSING:
            errmsg = "PEReference: expecting ';'";
            break;
        case XML_ERR_ENTITY_LOOP:
            errmsg = "Detected an entity reference loop";
            break;
        case XML_ERR_ENTITY_NOT_STARTED:
            errmsg = "EntityValue: \" or ' expected";
            break;
        case XML_ERR_ENTITY_PE_INTERNAL:
            errmsg = "PEReferences forbidden in internal subset";
            break;
        case XML_ERR_ENTITY_NOT_FINISHED:
            errmsg = "EntityValue: \" or ' expected";
            break;
        case XML_ERR_ATTRIBUTE_NOT_STARTED:
            errmsg = "AttValue: \" or ' expected";
            break;
        case XML_ERR_LT_IN_ATTRIBUTE:
            errmsg = "Unescaped '<' not allowed in attributes values";
            break;
        case XML_ERR_LITERAL_NOT_STARTED:
            errmsg = "SystemLiteral \" or ' expected";
            break;
        case XML_ERR_LITERAL_NOT_FINISHED:
            errmsg = "Unfinished System or Public ID \" or ' expected";
            break;
        case XML_ERR_MISPLACED_CDATA_END:
            errmsg = "Sequence ']]>' not allowed in content";
            break;
        case XML_ERR_URI_REQUIRED:
            errmsg = "SYSTEM or PUBLIC, the URI is missing";
            break;
        case XML_ERR_PUBID_REQUIRED:
            errmsg = "PUBLIC, the Public Identifier is missing";
            break;
        case XML_ERR_HYPHEN_IN_COMMENT:
            errmsg = "Comment must not contain '--' (double-hyphen)";
            break;
        case XML_ERR_PI_NOT_STARTED:
            errmsg = "xmlParsePI : no target name";
            break;
        case XML_ERR_RESERVED_XML_NAME:
            errmsg = "Invalid PI name";
            break;
        case XML_ERR_NOTATION_NOT_STARTED:
            errmsg = "NOTATION: Name expected here";
            break;
        case XML_ERR_NOTATION_NOT_FINISHED:
            errmsg = "'>' required to close NOTATION declaration";
            break;
        case XML_ERR_VALUE_REQUIRED:
            errmsg = "Entity value required";
            break;
        case XML_ERR_URI_FRAGMENT:
            errmsg = "Fragment not allowed";
            break;
        case XML_ERR_ATTLIST_NOT_STARTED:
            errmsg = "'(' required to start ATTLIST enumeration";
            break;
        case XML_ERR_NMTOKEN_REQUIRED:
            errmsg = "NmToken expected in ATTLIST enumeration";
            break;
        case XML_ERR_ATTLIST_NOT_FINISHED:
            errmsg = "')' required to finish ATTLIST enumeration";
            break;
        case XML_ERR_MIXED_NOT_STARTED:
            errmsg = "MixedContentDecl : '|' or ')*' expected";
            break;
        case XML_ERR_PCDATA_REQUIRED:
            errmsg = "MixedContentDecl : '#PCDATA' expected";
            break;
        case XML_ERR_ELEMCONTENT_NOT_STARTED:
            errmsg = "ContentDecl : Name or '(' expected";
            break;
        case XML_ERR_ELEMCONTENT_NOT_FINISHED:
            errmsg = "ContentDecl : ',' '|' or ')' expected";
            break;
        case XML_ERR_PEREF_IN_INT_SUBSET:
            errmsg =
                "PEReference: forbidden within markup decl in internal subset";
            break;
        case XML_ERR_GT_REQUIRED:
            errmsg = "expected '>'";
            break;
        case XML_ERR_CONDSEC_INVALID:
            errmsg = "XML conditional section '[' expected";
            break;
        case XML_ERR_INT_SUBSET_NOT_FINISHED:
            errmsg = "Content error in the internal subset";
            break;
        case XML_ERR_EXT_SUBSET_NOT_FINISHED:
            errmsg = "Content error in the external subset";
            break;
        case XML_ERR_CONDSEC_INVALID_KEYWORD:
            errmsg =
                "conditional section INCLUDE or IGNORE keyword expected";
            break;
        case XML_ERR_CONDSEC_NOT_FINISHED:
            errmsg = "XML conditional section not closed";
            break;
        case XML_ERR_XMLDECL_NOT_STARTED:
            errmsg = "Text declaration '<?xml' required";
            break;
        case XML_ERR_XMLDECL_NOT_FINISHED:
            errmsg = "parsing XML declaration: '?>' expected";
            break;
        case XML_ERR_EXT_ENTITY_STANDALONE:
            errmsg = "external parsed entities cannot be standalone";
            break;
        case XML_ERR_ENTITYREF_SEMICOL_MISSING:
            errmsg = "EntityRef: expecting ';'";
            break;
        case XML_ERR_DOCTYPE_NOT_FINISHED:
            errmsg = "DOCTYPE improperly terminated";
            break;
        case XML_ERR_LTSLASH_REQUIRED:
            errmsg = "EndTag: '</' not found";
            break;
        case XML_ERR_EQUAL_REQUIRED:
            errmsg = "expected '='";
            break;
        case XML_ERR_STRING_NOT_CLOSED:
            errmsg = "String not closed expecting \" or '";
            break;
        case XML_ERR_STRING_NOT_STARTED:
            errmsg = "String not started expecting ' or \"";
            break;
        case XML_ERR_ENCODING_NAME:
            errmsg = "Invalid XML encoding name";
            break;
        case XML_ERR_STANDALONE_VALUE:
            errmsg = "standalone accepts only 'yes' or 'no'";
            break;
        case XML_ERR_DOCUMENT_EMPTY:
            errmsg = "Document is empty";
            break;
        case XML_ERR_DOCUMENT_END:
            errmsg = "Extra content at the end of the document";
            break;
        case XML_ERR_NOT_WELL_BALANCED:
            errmsg = "chunk is not well balanced";
            break;
        case XML_ERR_EXTRA_CONTENT:
            errmsg = "extra content at the end of well balanced chunk";
            break;
        case XML_ERR_VERSION_MISSING:
            errmsg = "Malformed declaration expecting version";
            break;
        case XML_ERR_NAME_TOO_LONG:
            errmsg = "Name too long";
            break;
        case XML_ERR_INVALID_ENCODING:
            errmsg = "Invalid bytes in character encoding";
            break;
        case XML_ERR_RESOURCE_LIMIT:
            errmsg = "Resource limit exceeded";
            break;
        case XML_ERR_ARGUMENT:
            errmsg = "Invalid argument";
            break;
        case XML_ERR_SYSTEM:
            errmsg = "Out of system resources";
            break;
        case XML_ERR_REDECL_PREDEF_ENTITY:
            errmsg = "Invalid redeclaration of predefined entity";
            break;
        case XML_ERR_UNSUPPORTED_ENCODING:
            errmsg = "Unsupported encoding";
            break;
        case XML_ERR_INVALID_CHAR:
            errmsg = "Invalid character";
            break;

        case XML_IO_UNKNOWN:
            errmsg = "Unknown IO error"; break;
        case XML_IO_EACCES:
            errmsg = "Permission denied"; break;
        case XML_IO_EAGAIN:
            errmsg = "Resource temporarily unavailable"; break;
        case XML_IO_EBADF:
            errmsg = "Bad file descriptor"; break;
        case XML_IO_EBADMSG:
            errmsg = "Bad message"; break;
        case XML_IO_EBUSY:
            errmsg = "Resource busy"; break;
        case XML_IO_ECANCELED:
            errmsg = "Operation canceled"; break;
        case XML_IO_ECHILD:
            errmsg = "No child processes"; break;
        case XML_IO_EDEADLK:
            errmsg = "Resource deadlock avoided"; break;
        case XML_IO_EDOM:
            errmsg = "Domain error"; break;
        case XML_IO_EEXIST:
            errmsg = "File exists"; break;
        case XML_IO_EFAULT:
            errmsg = "Bad address"; break;
        case XML_IO_EFBIG:
            errmsg = "File too large"; break;
        case XML_IO_EINPROGRESS:
            errmsg = "Operation in progress"; break;
        case XML_IO_EINTR:
            errmsg = "Interrupted function call"; break;
        case XML_IO_EINVAL:
            errmsg = "Invalid argument"; break;
        case XML_IO_EIO:
            errmsg = "Input/output error"; break;
        case XML_IO_EISDIR:
            errmsg = "Is a directory"; break;
        case XML_IO_EMFILE:
            errmsg = "Too many open files"; break;
        case XML_IO_EMLINK:
            errmsg = "Too many links"; break;
        case XML_IO_EMSGSIZE:
            errmsg = "Inappropriate message buffer length"; break;
        case XML_IO_ENAMETOOLONG:
            errmsg = "Filename too long"; break;
        case XML_IO_ENFILE:
            errmsg = "Too many open files in system"; break;
        case XML_IO_ENODEV:
            errmsg = "No such device"; break;
        case XML_IO_ENOENT:
            errmsg = "No such file or directory"; break;
        case XML_IO_ENOEXEC:
            errmsg = "Exec format error"; break;
        case XML_IO_ENOLCK:
            errmsg = "No locks available"; break;
        case XML_IO_ENOMEM:
            errmsg = "Not enough space"; break;
        case XML_IO_ENOSPC:
            errmsg = "No space left on device"; break;
        case XML_IO_ENOSYS:
            errmsg = "Function not implemented"; break;
        case XML_IO_ENOTDIR:
            errmsg = "Not a directory"; break;
        case XML_IO_ENOTEMPTY:
            errmsg = "Directory not empty"; break;
        case XML_IO_ENOTSUP:
            errmsg = "Not supported"; break;
        case XML_IO_ENOTTY:
            errmsg = "Inappropriate I/O control operation"; break;
        case XML_IO_ENXIO:
            errmsg = "No such device or address"; break;
        case XML_IO_EPERM:
            errmsg = "Operation not permitted"; break;
        case XML_IO_EPIPE:
            errmsg = "Broken pipe"; break;
        case XML_IO_ERANGE:
            errmsg = "Result too large"; break;
        case XML_IO_EROFS:
            errmsg = "Read-only file system"; break;
        case XML_IO_ESPIPE:
            errmsg = "Invalid seek"; break;
        case XML_IO_ESRCH:
            errmsg = "No such process"; break;
        case XML_IO_ETIMEDOUT:
            errmsg = "Operation timed out"; break;
        case XML_IO_EXDEV:
            errmsg = "Improper link"; break;
        case XML_IO_NETWORK_ATTEMPT:
            errmsg = "Attempt to load network entity"; break;
        case XML_IO_ENCODER:
            errmsg = "encoder error"; break;
        case XML_IO_FLUSH:
            errmsg = "flush error"; break;
        case XML_IO_WRITE:
            errmsg = "write error"; break;
        case XML_IO_NO_INPUT:
            errmsg = "no input"; break;
        case XML_IO_BUFFER_FULL:
            errmsg = "buffer full"; break;
        case XML_IO_LOAD_ERROR:
            errmsg = "loading error"; break;
        case XML_IO_ENOTSOCK:
            errmsg = "not a socket"; break;
        case XML_IO_EISCONN:
            errmsg = "already connected"; break;
        case XML_IO_ECONNREFUSED:
            errmsg = "connection refused"; break;
        case XML_IO_ENETUNREACH:
            errmsg = "unreachable network"; break;
        case XML_IO_EADDRINUSE:
            errmsg = "address in use"; break;
        case XML_IO_EALREADY:
            errmsg = "already in use"; break;
        case XML_IO_EAFNOSUPPORT:
            errmsg = "unknown address family"; break;
        case XML_IO_UNSUPPORTED_PROTOCOL:
            errmsg = "unsupported protocol"; break;

        default:
            errmsg = "Unregistered error message";
    }

    return(errmsg);
}

/**
 * Prints to stderr.
 *
 * @param fmt  printf-like format string
 * @param ap  arguments
 */
void
xmlVPrintErrorMessage(const char *fmt, va_list ap) {
    vfprintf(stderr, fmt, ap);
}

/**
 * Prints to stderr.
 *
 * @param fmt  printf-like format string
 * @param ...  arguments
 */
void
xmlPrintErrorMessage(const char *fmt, ...) {
    va_list ap;

    va_start(ap, fmt);
    xmlVPrintErrorMessage(fmt, ap);
    va_end(ap);
}

/**
 * Print message to stderr and abort.
 *
 * @param fmt  printf-like format string
 * @param ...  arguments
 */
void
xmlAbort(const char *fmt, ...) {
    va_list ap;

    va_start(ap, fmt);
    xmlVPrintErrorMessage(fmt, ap);
    va_end(ap);

    abort();
}