exml 0.7.3-deprecated

Pure Rust XML library based on libxml2
Documentation
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
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
//! Provide methods and data structures for Canonical XML and Exclusive XML Canonicalization.
//!
//! This module is based on `libxml/c14n.h`, `c14n.c`, and so on in `libxml2-v2.11.8`.  
//! Please refer to original libxml2 documents also.

// Copyright of the original code is the following.
// --------
// Summary: Provide Canonical XML and Exclusive XML Canonicalization
// Description: the c14n modules provides a
//
// "Canonical XML" implementation
// http://www.w3.org/TR/xml-c14n
//
// and an
//
// "Exclusive XML Canonicalization" implementation
// http://www.w3.org/TR/xml-exc-c14n
//
// Copy: See Copyright for the status of this software.
//
// Author: Aleksey Sanin <aleksey@aleksey.com>
// --------
// "Canonical XML" implementation
// http://www.w3.org/TR/xml-c14n
//
// "Exclusive XML Canonicalization" implementation
// http://www.w3.org/TR/xml-exc-c14n
//
// See Copyright for the status of this software.
//
// Author: Aleksey Sanin <aleksey@aleksey.com>

use std::{cmp::Ordering, mem::take, ptr::null_mut, rc::Rc};

use crate::{
    error::{__xml_raise_error, XmlErrorDomain, XmlErrorLevel, XmlParserErrors},
    io::{XmlOutputBuffer, write_quoted},
    list::XmlList,
    tree::{
        NodeCommon, XML_XML_NAMESPACE, XmlAttr, XmlAttrPtr, XmlDocPtr, XmlElementType,
        XmlGenericNodePtr, XmlNode, XmlNodePtr, XmlNs, XmlNsPtr, xml_free_prop_list,
        xml_new_ns_prop,
    },
    uri::{XmlURI, build_uri},
    xpath::XmlNodeSet,
};

// Predefined values for C14N modes
#[doc(alias = "xmlC14NMode")]
#[repr(C)]
pub enum XmlC14NMode {
    XmlC14N1_0 = 0,          /* Original C14N 1.0 spec */
    XmlC14NExclusive1_0 = 1, /* Exclusive C14N 1.0 spec */
    XmlC14N1_1 = 2,          /* C14N 1.1 spec */
}

#[repr(C)]
pub enum XmlC14NPosition {
    XmlC14NBeforeDocumentElement = 0,
    XmlC14NInsideDocumentElement = 1,
    XmlC14NAfterDocumentElement = 2,
}

#[repr(C)]
#[derive(Default)]
pub struct XmlC14NVisibleNsStack {
    ns_cur_end: usize,         /* number of nodes in the set */
    ns_prev_start: usize,      /* the beginning of the stack for previous visible node */
    ns_prev_end: usize,        /* the end of the stack for previous visible node */
    ns_tab: Vec<XmlNsPtr>,     /* array of ns in no particular order */
    node_tab: Vec<XmlNodePtr>, /* array of nodes in no particular order */
}

impl XmlC14NVisibleNsStack {
    #[doc(alias = "xmlC14NVisibleNsStackSave")]
    fn save(&self, state: &mut XmlC14NVisibleNsStack) {
        state.ns_cur_end = self.ns_cur_end;
        state.ns_prev_start = self.ns_prev_start;
        state.ns_prev_end = self.ns_prev_end;
    }

    /// Check whether `ns` was already rendered or not.  
    /// Return `true` if already rendered, otherwise return `false`.
    ///
    /// Please refer to the document of `xmlC14NVisibleNsStackFind` for original libxml2.
    #[doc(alias = "xmlC14NVisibleNsStackFind")]
    fn find(&self, ns: &XmlNs) -> bool {
        // if the default namespace xmlns="" is not defined yet then we do not want to print it out
        let prefix = ns.prefix();
        let prefix = prefix.as_deref().unwrap_or("");
        let href = ns.href();
        let href = href.as_deref().unwrap_or("");

        let has_empty_ns =
            xml_c14n_str_equal(Some(prefix), None) && xml_c14n_str_equal(Some(href), None);

        let start = if has_empty_ns { 0 } else { self.ns_prev_start };
        for &ns1 in self.ns_tab[start..self.ns_cur_end].iter().rev() {
            if xml_c14n_str_equal(Some(prefix), ns1.prefix().as_deref()) {
                return xml_c14n_str_equal(Some(href), ns1.href().as_deref());
            }
        }
        has_empty_ns
    }

    #[doc(alias = "xmlC14NVisibleNsStackAdd")]
    fn add(&mut self, ns: XmlNsPtr, node: XmlNodePtr) {
        if self.ns_cur_end == self.ns_tab.len() {
            self.ns_tab.push(ns);
            self.node_tab.push(node);
        } else {
            self.ns_tab[self.ns_cur_end] = ns;
            self.node_tab[self.ns_cur_end] = node;
        }
        self.ns_cur_end += 1;
    }

    #[doc(alias = "xmlC14NVisibleNsStackShift")]
    fn shift(&mut self) {
        self.ns_prev_start = self.ns_prev_end;
        self.ns_prev_end = self.ns_cur_end;
    }

    #[doc(alias = "xmlC14NVisibleNsStackRestore")]
    fn restore(&mut self, state: &XmlC14NVisibleNsStack) {
        self.ns_cur_end = state.ns_cur_end;
        self.ns_prev_start = state.ns_prev_start;
        self.ns_prev_end = state.ns_prev_end;
    }
}

/// Signature for a C14N callback on visible nodes
///
/// Returns 1 if the node should be included
#[doc(alias = "xmlC14NIsVisibleCallback")]
pub type XmlC14NIsVisibleCallback<T> =
    fn(user_data: &T, node: Option<XmlGenericNodePtr>, parent: Option<XmlGenericNodePtr>) -> i32;

#[repr(C)]
pub struct XmlC14NCtx<'a, T> {
    // input parameters
    doc: XmlDocPtr,
    is_visible_callback: Option<XmlC14NIsVisibleCallback<T>>,
    user_data: T,
    with_comments: bool,
    buf: XmlOutputBuffer<'a>,

    // position in the XML document
    pos: XmlC14NPosition,
    parent_is_doc: bool,
    ns_rendered: Box<XmlC14NVisibleNsStack>,

    // C14N mode
    mode: XmlC14NMode,

    // exclusive canonicalization
    inclusive_ns_prefixes: Option<Vec<String>>,

    // error number
    error: XmlParserErrors,
}

impl<T> XmlC14NCtx<'_, T> {
    fn is_visible(
        &self,
        node: Option<XmlGenericNodePtr>,
        parent: Option<XmlGenericNodePtr>,
    ) -> bool {
        if let Some(callback) = self.is_visible_callback {
            callback(&self.user_data, node, parent) != 0
        } else {
            true
        }
    }

    fn is_exclusive(&self) -> bool {
        matches!(self.mode, XmlC14NMode::XmlC14NExclusive1_0)
    }

    /// Checks that current element node has no relative namespaces defined
    ///
    /// Returns 0 if the node has no relative namespaces or -1 otherwise.
    #[doc(alias = "xmlC14NCheckForRelativeNamespaces")]
    fn check_for_relative_namespaces(&self, cur: &XmlNode) -> i32 {
        if !matches!(cur.element_type(), XmlElementType::XmlElementNode) {
            xml_c14n_err_param("checking for relative namespaces");
            return -1;
        }

        let mut ns = cur.ns_def;
        while let Some(now) = ns {
            let href = now.href().unwrap();
            if !href.is_empty() {
                let Some(uri) = XmlURI::parse(&href) else {
                    xml_c14n_err_internal("parsing namespace uri");
                    return -1;
                };
                let scheme = uri.scheme.as_deref().unwrap();
                if scheme.is_empty() {
                    xml_c14n_err_relative_namespace(scheme);
                    return -1;
                }
            }
            ns = now.next;
        }
        0
    }

    /// Prints the given namespace to the output buffer from C14N context.
    ///
    /// Returns 1 on success or 0 on fail.
    #[doc(alias = "xmlC14NPrintNamespaces")]
    fn print_namespaces(&mut self, ns: &XmlNs) -> i32 {
        if let Some(prefix) = ns.prefix() {
            self.buf.write_str(" xmlns:").ok();
            self.buf.write_str(&prefix).ok();
            self.buf.write_str("=").ok();
        } else {
            self.buf.write_str(" xmlns=").ok();
        }
        if let Some(href) = ns.href.as_deref() {
            write_quoted(&mut self.buf, href).ok();
        } else {
            self.buf.write_str("\"\"").ok();
        }
        1
    }

    /// Prints out canonical namespace axis of the current node to the
    /// buffer from C14N context as follows
    ///
    /// Canonical XML v 1.0 (<http://www.w3.org/TR/xml-c14n>)
    ///
    /// # Namespace Axis
    /// Consider a list L containing only namespace nodes in the
    /// axis and in the node-set in lexicographic order (ascending).  
    /// To begin processing L, if the first node is not the default namespace node
    /// (a node with no namespace URI and no local name), then generate a space followed
    /// by xmlns="" if and only if the following conditions are met:
    /// - the element E that owns the axis is in the node-set
    /// - The nearest ancestor element of E in the node-set has a default namespace node
    ///   in the node-set (default namespace nodes always have non-empty values in XPath)
    ///   
    /// The latter condition eliminates unnecessary occurrences of xmlns="" in
    /// the canonical form since an element only receives an xmlns="" if its
    /// default namespace is empty and if it has an immediate parent in the
    /// canonical form that has a non-empty default namespace.  
    /// To finish processing L, simply process every namespace node in L,
    /// except omit namespace node with local name xml, which defines the xml prefix,
    /// if its string value is `http://www.w3.org/XML/1998/namespace`.
    ///
    /// Exclusive XML Canonicalization v 1.0 (<http://www.w3.org/TR/xml-exc-c14n>)
    /// Canonical XML applied to a document subset requires the search of the
    /// ancestor nodes of each orphan element node for attributes in the xml
    /// namespace, such as xml:lang and xml:space.  
    /// These are copied into the element node except if a declaration of the same attribute is already
    /// in the attribute axis of the element (whether or not it is included in the document subset).  
    /// This search and copying are omitted from the Exclusive XML Canonicalization method.
    ///
    /// Returns 0 on success or -1 on fail.
    #[doc(alias = "xmlC14NProcessNamespacesAxis")]
    fn process_namespaces_axis(&mut self, mut cur: XmlNodePtr, visible: bool) -> i32 {
        let mut has_empty_ns = false;

        if !matches!(cur.element_type(), XmlElementType::XmlElementNode) {
            xml_c14n_err_param("processing namespaces axis (c14n)");
            return -1;
        }

        // Create a sorted list to store element namespaces
        let mut list = XmlList::new(None, Rc::new(|ns1, ns2| xml_c14n_ns_compare(*ns1, *ns2)));

        // check all namespaces
        let mut n = Some(cur);
        while let Some(node) = n {
            let mut ns = node.ns_def;
            while let Some(now) = ns {
                let prefix = now.prefix();
                let cur_doc = cur.doc;
                let tmp = cur.search_ns(cur_doc, prefix.as_deref());

                if tmp == Some(now)
                    && !xml_c14n_is_xml_ns(now)
                    && self.is_visible(Some(now.into()), Some(cur.into()))
                {
                    let already_rendered = (*self.ns_rendered).find(&now);
                    if visible {
                        (*self.ns_rendered).add(now, cur);
                    }
                    if !already_rendered {
                        list.insert_lower_bound(now);
                    }
                    if now.prefix().map_or(0, |pre| pre.len()) == 0 {
                        has_empty_ns = true;
                    }
                }
                ns = now.next;
            }
            n = node.parent().and_then(|p| XmlNodePtr::try_from(p).ok());
        }

        // if the first node is not the default namespace node (a node with no
        //  namespace URI and no local name), then generate a space followed by
        //  xmlns="" if and only if the following conditions are met:
        //   - the element E that owns the axis is in the node-set
        //   - the nearest ancestor element of E in the node-set has a default
        //      namespace node in the node-set (default namespace nodes always
        //      have non-empty values in XPath)
        if visible && !has_empty_ns && !(*self.ns_rendered).find(&XmlNs::default()) {
            self.print_namespaces(&XmlNs::default());
        }

        // print out all elements from list
        list.walk(|data| self.print_namespaces(data) != 0);

        0
    }

    /// Prints out canonical attribute axis of the current node to the
    /// buffer from C14N context as follows
    ///
    /// Canonical XML v 1.0 (<http://www.w3.org/TR/xml-c14n>)
    ///
    /// # Attribute Axis
    /// In lexicographic order (ascending), process each node that
    /// is in the element's attribute axis and in the node-set.
    ///
    /// The processing of an element node E MUST be modified slightly
    /// when an XPath node-set is given as input and the element's
    /// parent is omitted from the node-set.
    ///
    /// Exclusive XML Canonicalization v 1.0 (<http://www.w3.org/TR/xml-exc-c14n>)
    ///
    /// Canonical XML applied to a document subset requires the search of the
    /// ancestor nodes of each orphan element node for attributes in the xml
    /// namespace, such as xml:lang and xml:space. These are copied into the
    /// element node except if a declaration of the same attribute is already
    /// in the attribute axis of the element (whether or not it is included in
    /// the document subset). This search and copying are omitted from the
    /// Exclusive XML Canonicalization method.
    ///
    /// Returns 0 on success or -1 on fail.
    #[doc(alias = "xmlC14NProcessAttrsAxis")]
    fn process_attrs_axis(&mut self, cur: XmlNodePtr, parent_visible: bool) -> i32 {
        let mut attrs_to_delete = None::<XmlAttrPtr>;

        if !matches!(cur.element_type(), XmlElementType::XmlElementNode) {
            xml_c14n_err_param("processing attributes axis");
            return -1;
        }

        // Create a sorted list to store element attributes
        let mut list = XmlList::new(
            None,
            Rc::new(|&attr1, &attr2| xml_c14n_attrs_compare(attr1, attr2)),
        );
        match self.mode {
            XmlC14NMode::XmlC14N1_0 => {
                // The processing of an element node E MUST be modified slightly when an XPath node-set is
                // given as input and the element's parent is omitted from the node-set. The method for processing
                // the attribute axis of an element E in the node-set is enhanced. All element nodes along E's
                // ancestor axis are examined for nearest occurrences of attributes in the xml namespace, such
                // as xml:lang and xml:space (whether or not they are in the node-set). From this list of attributes,
                // remove any that are in E's attribute axis (whether or not they are in the node-set). Then,
                // lexicographically merge this attribute list with the nodes of E's attribute axis that are in
                // the node-set. The result of visiting the attribute axis is computed by processing the attribute
                // nodes in this merged attribute list.

                // Add all visible attributes from current node.
                let mut attr = cur.properties;
                while let Some(now) = attr {
                    // check that attribute is visible
                    if self.is_visible(Some(now.into()), Some(cur.into())) {
                        list.insert_lower_bound(now);
                    }
                    attr = now.next;
                }

                // Handle xml attributes
                if parent_visible
                    && cur.parent().is_some()
                    && !self.is_visible(cur.parent(), cur.parent().and_then(|cur| cur.parent()))
                {
                    // If XPath node-set is not specified then the parent is always visible!
                    let mut tmp = cur
                        .parent()
                        .and_then(|node| XmlNodePtr::try_from(node).ok());
                    while let Some(cur_node) = tmp {
                        let mut attr = cur_node.properties;
                        while let Some(now) = attr {
                            if xml_c14n_is_xml_attr(now) && list.search(&now).is_none() {
                                list.insert_lower_bound(now);
                            }
                            attr = now.next;
                        }
                        tmp = cur_node.parent().and_then(|p| XmlNodePtr::try_from(p).ok());
                    }
                }
            }
            XmlC14NMode::XmlC14NExclusive1_0 => {
                // attributes in the XML namespace, such as xml:lang and xml:space
                // are not imported into orphan nodes of the document subset

                // Add all visible attributes from current node.
                let mut attr = cur.properties;
                while let Some(now) = attr {
                    // check that attribute is visible
                    if self.is_visible(Some(now.into()), Some(cur.into())) {
                        list.insert_lower_bound(now);
                    }
                    attr = now.next;
                }
            }
            XmlC14NMode::XmlC14N1_1 => {
                // The processing of an element node E MUST be modified slightly when an XPath node-set is
                // given as input and some of the element's ancestors are omitted from the node-set.
                //
                // Simple inheritable attributes are attributes that have a value that requires at most a simple
                // redeclaration. This redeclaration is done by supplying a new value in the child axis. The
                // redeclaration of a simple inheritable attribute A contained in one of E's ancestors is done
                // by supplying a value to an attribute Ae inside E with the same name. Simple inheritable attributes
                // are xml:lang and xml:space.
                //
                // The method for processing the attribute axis of an element E in the node-set is hence enhanced.
                // All element nodes along E's ancestor axis are examined for the nearest occurrences of simple
                // inheritable attributes in the xml namespace, such as xml:lang and xml:space (whether or not they
                // are in the node-set). From this list of attributes, any simple inheritable attributes that are
                // already in E's attribute axis (whether or not they are in the node-set) are removed. Then,
                // lexicographically merge this attribute list with the nodes of E's attribute axis that are in
                // the node-set. The result of visiting the attribute axis is computed by processing the attribute
                // nodes in this merged attribute list.
                //
                // The xml:id attribute is not a simple inheritable attribute and no processing of these attributes is
                // performed.
                //
                // The xml:base attribute is not a simple inheritable attribute and requires special processing beyond
                // a simple redeclaration.
                //
                // Attributes in the XML namespace other than xml:base, xml:id, xml:lang, and xml:space MUST be processed
                // as ordinary attributes.

                // special processing for 1.1 spec
                let mut xml_base_attr = None;
                let mut xml_lang_attr = None;
                let mut xml_space_attr = None;

                // Add all visible attributes from current node.
                let mut attr = cur.properties;
                while let Some(now) = attr {
                    // special processing for XML attribute kiks in only when we have invisible parents
                    if !parent_visible || !xml_c14n_is_xml_attr(now) {
                        // check that attribute is visible
                        if self.is_visible(Some(now.into()), Some(cur.into())) {
                            list.insert_lower_bound(now);
                        }
                    } else {
                        let mut matched: i32 = 0;

                        // check for simple inheritance attributes
                        if matched == 0
                            && xml_lang_attr.is_none()
                            && now.name().as_deref() == Some("lang")
                        {
                            xml_lang_attr = attr;
                            matched = 1;
                        }
                        if matched == 0
                            && xml_space_attr.is_none()
                            && now.name().as_deref() == Some("space")
                        {
                            xml_space_attr = attr;
                            matched = 1;
                        }

                        // check for base attr
                        if matched == 0
                            && xml_base_attr.is_none()
                            && now.name().as_deref() == Some("base")
                        {
                            xml_base_attr = attr;
                            matched = 1;
                        }

                        // otherwise, it is a normal attribute, so just check if it is visible
                        if matched == 0 && self.is_visible(Some(now.into()), Some(cur.into())) {
                            list.insert_lower_bound(now);
                        }
                    }

                    // move to the next one
                    attr = now.next;
                }

                // special processing for XML attribute kiks in only when we have invisible parents
                if parent_visible {
                    // simple inheritance attributes - copy
                    if xml_lang_attr.is_none() {
                        xml_lang_attr =
                            self.find_hidden_parent_attr(cur.parent(), "lang", XML_XML_NAMESPACE);
                    }
                    if let Some(attr) = xml_lang_attr {
                        list.insert_lower_bound(attr);
                    }
                    if xml_space_attr.is_none() {
                        xml_space_attr =
                            self.find_hidden_parent_attr(cur.parent(), "space", XML_XML_NAMESPACE);
                    }
                    if let Some(attr) = xml_space_attr {
                        list.insert_lower_bound(attr);
                    }

                    // base uri attribute - fix up
                    if xml_base_attr.is_none() {
                        // if we don't have base uri attribute, check if we have a "hidden" one above
                        xml_base_attr =
                            self.find_hidden_parent_attr(cur.parent(), "base", XML_XML_NAMESPACE);
                    }
                    if let Some(attr) = xml_base_attr {
                        xml_base_attr = self.fixup_base_attr(&attr);
                        if let Some(mut attr) = xml_base_attr {
                            list.insert_lower_bound(attr);

                            // note that we MUST delete returned attr node ourselves!
                            attr.next = attrs_to_delete;
                            attrs_to_delete = Some(attr);
                        }
                    }
                }
            }
        }

        // print out all elements from list
        list.walk(|data| self.print_attrs(*data));

        // Cleanup
        unsafe {
            // # Safety
            // If `attrs_to_delete` is `Some`, it is newly created in `Self::fixup_base_attr`.
            // This is not leaked to the out of this function, so this operation is safe.
            xml_free_prop_list(attrs_to_delete);
        }
        0
    }

    /// Canonical XML v 1.0 (<http://www.w3.org/TR/xml-c14n>)
    ///
    /// # Element Nodes
    /// If the element is not in the node-set, then the result is obtained
    /// by processing the namespace axis, then the attribute axis, then
    /// processing the child nodes of the element that are in the node-set
    /// (in document order). If the element is in the node-set, then the result
    /// is an open angle bracket (<), the element QName, the result of
    /// processing the namespace axis, the result of processing the attribute
    /// axis, a close angle bracket (>), the result of processing the child
    /// nodes of the element that are in the node-set (in document order), an
    /// open angle bracket, a forward slash (/), the element QName, and a close
    /// angle bracket.
    ///
    /// Returns non-negative value on success or negative value on fail
    #[doc(alias = "xmlC14NProcessElementNode")]
    fn process_element_node(&mut self, cur: XmlNodePtr, visible: bool) -> i32 {
        let mut parent_is_doc = false;

        if !matches!(cur.element_type(), XmlElementType::XmlElementNode) {
            xml_c14n_err_param("processing element node");
            return -1;
        }

        // Check relative relative namespaces:
        // implementations of XML canonicalization MUST report an operation
        // failure on documents containing relative namespace URIs.
        if self.check_for_relative_namespaces(&cur) < 0 {
            xml_c14n_err_internal("checking for relative namespaces");
            return -1;
        }

        let mut state: XmlC14NVisibleNsStack = XmlC14NVisibleNsStack::default();
        // Save ns_rendered stack position
        self.ns_rendered.save(&mut state);

        if visible {
            if self.parent_is_doc {
                // save this flag into the stack
                parent_is_doc = self.parent_is_doc;
                self.parent_is_doc = false;
                self.pos = XmlC14NPosition::XmlC14NInsideDocumentElement;
            }
            self.buf.write_str("<").ok();

            if let Some(prefix) = cur.ns.as_deref().and_then(|ns| ns.prefix()) {
                self.buf.write_str(&prefix).ok();
                self.buf.write_str(":").ok();
            }

            self.buf.write_str(&cur.name).ok();
        }

        let ret = if !self.is_exclusive() {
            self.process_namespaces_axis(cur, visible)
        } else {
            self.exc_c14n_process_namespaces_axis(cur, visible)
        };
        if ret < 0 {
            xml_c14n_err_internal("processing namespaces axis");
            return -1;
        }
        // todo: shouldn't this go to "visible only"?
        if visible {
            (*self.ns_rendered).shift();
        }

        let ret = self.process_attrs_axis(cur, visible);
        if ret < 0 {
            xml_c14n_err_internal("processing attributes axis");
            return -1;
        }

        if visible {
            self.buf.write_str(">").ok();
        }
        if let Some(children) = cur.children() {
            let ret = self.process_node_list(Some(children));
            if ret < 0 {
                xml_c14n_err_internal("processing childrens list");
                return -1;
            }
        }
        if visible {
            self.buf.write_str("</").ok();
            if let Some(prefix) = cur.ns.as_deref().and_then(|ns| ns.prefix()) {
                self.buf.write_str(&prefix).ok();
                self.buf.write_str(":").ok();
            }

            self.buf.write_str(&cur.name).ok();
            self.buf.write_str(">").ok();
            if parent_is_doc {
                // restore this flag from the stack for next node
                self.parent_is_doc = parent_is_doc;
                self.pos = XmlC14NPosition::XmlC14NAfterDocumentElement;
            }
        }

        // Restore ns_rendered stack position
        (*self.ns_rendered).restore(&state);
        0
    }

    /// Processes all nodes in the row starting from cur.
    ///
    /// Returns non-negative value on success or negative value on fail
    #[doc(alias = "xmlC14NProcessNodeList")]
    fn process_node_list(&mut self, mut cur: Option<XmlGenericNodePtr>) -> i32 {
        let mut ret = 0;
        while let Some(now) = cur {
            ret = self.process_node(now);
            cur = now.next();
            if ret < 0 {
                break;
            }
        }
        ret
    }

    /// Processes the given node
    ///
    /// Returns non-negative value on success or negative value on fail
    #[doc(alias = "xmlC14NProcessNode")]
    fn process_node(&mut self, cur: XmlGenericNodePtr) -> i32 {
        let mut ret: i32 = 0;

        let visible = self.is_visible(Some(cur), cur.parent());
        match cur.element_type() {
            XmlElementType::XmlElementNode => {
                let cur = XmlNodePtr::try_from(cur).unwrap();
                ret = self.process_element_node(cur, visible);
            }
            XmlElementType::XmlCDATASectionNode | XmlElementType::XmlTextNode => {
                // Text Nodes
                // the string value, except all ampersands are replaced
                // by &amp;, all open angle brackets (<) are replaced by &lt;, all closing
                // angle brackets (>) are replaced by &gt;, and all #xD characters are
                // replaced by &#xD;.

                let cur = XmlNodePtr::try_from(cur).unwrap();
                // cdata sections are processed as text nodes
                // todo: verify that cdata sections are included in XPath nodes set
                if let Some(content) = cur.content.as_deref().filter(|_| visible) {
                    let buffer = normalize_text(content);
                    self.buf.write_str(&buffer).ok();
                }
            }
            XmlElementType::XmlPINode => {
                // Processing Instruction (PI) Nodes-
                // The opening PI symbol (<?), the PI target name of the node,
                // a leading space and the string value if it is not empty, and
                // the closing PI symbol (?>). If the string value is empty,
                // then the leading space is not added. Also, a trailing #xA is
                // rendered after the closing PI symbol for PI children of the
                // root node with a lesser document order than the document
                // element, and a leading #xA is rendered before the opening PI
                // symbol of PI children of the root node with a greater document
                // order than the document element.
                if visible {
                    if matches!(self.pos, XmlC14NPosition::XmlC14NAfterDocumentElement) {
                        self.buf.write_str("\x0A<?").ok();
                    } else {
                        self.buf.write_str("<?").ok();
                    }

                    self.buf.write_str(&cur.name().unwrap()).ok();
                    let cur = XmlNodePtr::try_from(cur).unwrap();
                    if let Some(content) = cur.content.as_deref().filter(|cont| !cont.is_empty()) {
                        self.buf.write_str(" ").ok();
                        // todo: do we need to normalize pi?
                        let buffer = normalize_pi(content);
                        self.buf.write_str(&buffer).ok();
                    }

                    if matches!(self.pos, XmlC14NPosition::XmlC14NBeforeDocumentElement) {
                        self.buf.write_str("?>\x0A").ok();
                    } else {
                        self.buf.write_str("?>").ok();
                    }
                }
            }
            XmlElementType::XmlCommentNode => {
                // Comment Nodes
                // Nothing if generating canonical XML without  comments. For
                // canonical XML with comments, generate the opening comment
                // symbol (<!--), the string value of the node, and the
                // closing comment symbol (-->). Also, a trailing #xA is rendered
                // after the closing comment symbol for comment children of the
                // root node with a lesser document order than the document
                // element, and a leading #xA is rendered before the opening
                // comment symbol of comment children of the root node with a
                // greater document order than the document element. (Comment
                // children of the root node represent comments outside of the
                // top-level document element and outside of the document type
                // declaration).
                if visible && self.with_comments {
                    if matches!(self.pos, XmlC14NPosition::XmlC14NAfterDocumentElement) {
                        self.buf.write_str("\x0A<!--").ok();
                    } else {
                        self.buf.write_str("<!--").ok();
                    }

                    let cur = XmlNodePtr::try_from(cur).unwrap();
                    if let Some(content) = cur.content.as_deref() {
                        // todo: do we need to normalize comment?
                        let buffer = normalize_comment(content);
                        self.buf.write_str(&buffer).ok();
                    }

                    if matches!(self.pos, XmlC14NPosition::XmlC14NBeforeDocumentElement) {
                        self.buf.write_str("-->\x0A").ok();
                    } else {
                        self.buf.write_str("-->").ok();
                    }
                }
            }
            XmlElementType::XmlDocumentNode | XmlElementType::XmlDocumentFragNode => {
                // should be processed as document?
                if let Some(children) = cur.children() {
                    self.pos = XmlC14NPosition::XmlC14NBeforeDocumentElement;
                    self.parent_is_doc = true;
                    ret = self.process_node_list(Some(children));
                }
            }
            #[cfg(feature = "html")]
            XmlElementType::XmlHTMLDocumentNode => {
                // should be processed as document?
                if let Some(children) = cur.children() {
                    self.pos = XmlC14NPosition::XmlC14NBeforeDocumentElement;
                    self.parent_is_doc = true;
                    ret = self.process_node_list(Some(children));
                }
            }

            XmlElementType::XmlAttributeNode => {
                xml_c14n_err_invalid_node("XML_ATTRIBUTE_NODE", "processing node");
                return -1;
            }
            XmlElementType::XmlNamespaceDecl => {
                xml_c14n_err_invalid_node("XML_NAMESPACE_DECL", "processing node");
                return -1;
            }
            XmlElementType::XmlEntityRefNode => {
                xml_c14n_err_invalid_node("XML_ENTITY_REF_NODE", "processing node");
                return -1;
            }
            XmlElementType::XmlEntityNode => {
                xml_c14n_err_invalid_node("XML_ENTITY_NODE", "processing node");
                return -1;
            }

            XmlElementType::XmlDocumentTypeNode
            | XmlElementType::XmlNotationNode
            | XmlElementType::XmlDTDNode
            | XmlElementType::XmlElementDecl
            | XmlElementType::XmlAttributeDecl
            | XmlElementType::XmlEntityDecl => {
                // should be ignored according to "W3C Canonical XML"
            }
            #[cfg(feature = "xinclude")]
            XmlElementType::XmlXIncludeStart | XmlElementType::XmlXIncludeEnd => {
                // should be ignored according to "W3C Canonical XML"
            }
            _ => {
                xml_c14n_err_unknown_node(cur.element_type() as i32, "processing node");
                return -1;
            }
        }

        ret
    }

    /// Prints out exclusive canonical namespace axis of the current node
    /// to the buffer from C14N context as follows
    ///
    /// Exclusive XML Canonicalization
    /// <http://www.w3.org/TR/xml-exc-c14n>
    ///
    /// If the element node is in the XPath subset then output the node in
    /// accordance with Canonical XML except for namespace nodes which are
    /// rendered as follows:
    ///
    /// 1. Render each namespace node iff:
    ///    * it is visibly utilized by the immediate parent element or one of
    ///      its attributes, or is present in InclusiveNamespaces PrefixList, and
    ///    * its prefix and value do not appear in ns_rendered. ns_rendered is
    ///      obtained by popping the state stack in order to obtain a list of
    ///      prefixes and their values which have already been rendered by
    ///      an output ancestor of the namespace node's parent element.
    /// 2. Append the rendered namespace node to the list ns_rendered of namespace
    ///    nodes rendered by output ancestors. Push ns_rendered on state stack and
    ///    recurse.
    /// 3. After the recursion returns, pop thestate stack.
    ///
    /// Returns 0 on success or -1 on fail.
    #[doc(alias = "xmlExcC14NProcessNamespacesAxis")]
    fn exc_c14n_process_namespaces_axis(&mut self, mut cur: XmlNodePtr, visible: bool) -> i32 {
        let mut has_empty_ns = false;
        let mut has_visibly_utilized_empty_ns = false;
        let mut has_empty_ns_in_inclusive_list = false;

        if !matches!(cur.element_type(), XmlElementType::XmlElementNode) {
            xml_c14n_err_param("processing namespaces axis (exc c14n)");
            return -1;
        }

        if !self.is_exclusive() {
            xml_c14n_err_param("processing namespaces axis (exc c14n)");
            return -1;
        }

        // Create a sorted list to store element namespaces
        let mut list = XmlList::new(None, Rc::new(|ns1, ns2| xml_c14n_ns_compare(*ns1, *ns2)));

        // process inclusive namespaces:
        // All namespace nodes appearing on inclusive ns list are
        // handled as provided in Canonical XML
        if let Some(inclusive_ns_prefixes) = self.inclusive_ns_prefixes.as_deref() {
            for prefix in inclusive_ns_prefixes {
                // Special values for namespace with empty prefix
                let prefix = if prefix == "#default" || prefix.is_empty() {
                    has_empty_ns_in_inclusive_list = true;
                    None
                } else {
                    Some(prefix.as_str())
                };

                let cur_doc = cur.doc;
                let ns = cur.search_ns(cur_doc, prefix);
                if let Some(ns) = ns.filter(|&ns| {
                    !xml_c14n_is_xml_ns(ns) && self.is_visible(Some(ns.into()), Some(cur.into()))
                }) {
                    let already_rendered = (*self.ns_rendered).find(&ns);
                    if visible {
                        // TODO: replace `cur` to `Rc<XmlNode>`
                        (*self.ns_rendered).add(ns, cur);
                    }
                    if !already_rendered {
                        list.insert_lower_bound(ns);
                    }
                    if (*ns).prefix().map_or(0, |pre| pre.len()) == 0 {
                        has_empty_ns = true;
                    }
                }
            }
        }

        // add node namespace
        let ns = if cur.ns.is_some() {
            cur.ns
        } else {
            let cur_doc = cur.doc;
            has_visibly_utilized_empty_ns = true;
            cur.search_ns(cur_doc, None)
        };
        if let Some(ns) = ns.filter(|&ns| !xml_c14n_is_xml_ns(ns)) {
            if visible
                && self.is_visible(Some(ns.into()), Some(cur.into()))
                && !self.exc_c14n_visible_ns_stack_find(&self.ns_rendered, &ns)
            {
                list.insert_lower_bound(ns);
            }
            if visible {
                // TODO: replace `cur` to `Rc<XmlNode>`
                (*self.ns_rendered).add(ns, cur);
            }
            if (*ns).prefix().map_or(0, |pre| pre.len()) == 0 {
                has_empty_ns = true;
            }
        }

        // add attributes
        let mut attr = cur.properties;
        while let Some(cur_attr) = attr {
            // we need to check that attribute is visible and has non
            // default namespace (XML Namespaces: "default namespaces
            // do not apply directly to attributes")
            if let Some(attr_ns) = cur_attr.ns.filter(|&ns| {
                !xml_c14n_is_xml_ns(ns) && self.is_visible(Some(cur_attr.into()), Some(cur.into()))
            }) {
                let already_rendered =
                    self.exc_c14n_visible_ns_stack_find(&self.ns_rendered, &attr_ns);
                (*self.ns_rendered).add(attr_ns, cur);
                if !already_rendered && visible {
                    list.insert_lower_bound(attr_ns);
                }
                if attr_ns.prefix().map_or(0, |pre| pre.len()) == 0 {
                    has_empty_ns = true;
                }
            } else if cur_attr.ns.is_some_and(|ns| {
                ns.prefix().map_or(0, |pre| pre.len()) == 0
                    && ns.href.as_deref().unwrap().is_empty()
            }) {
                has_visibly_utilized_empty_ns = true;
            }
            attr = cur_attr.next;
        }

        // Process xmlns=""
        if visible
            && has_visibly_utilized_empty_ns
            && !has_empty_ns
            && !has_empty_ns_in_inclusive_list
        {
            let already_rendered =
                self.exc_c14n_visible_ns_stack_find(&self.ns_rendered, &XmlNs::default());
            if !already_rendered {
                self.print_namespaces(&XmlNs::default());
            }
        } else if visible
            && !has_empty_ns
            && has_empty_ns_in_inclusive_list
            && !(*self.ns_rendered).find(&XmlNs::default())
        {
            self.print_namespaces(&XmlNs::default());
        }

        // print out all elements from list
        list.walk(|data| self.print_namespaces(data) != 0);

        // Cleanup
        0
    }

    #[doc(alias = "xmlC14NVisibleNsStackFind")]
    fn exc_c14n_visible_ns_stack_find(&self, cur: &XmlC14NVisibleNsStack, ns: &XmlNs) -> bool {
        // if the default namespace xmlns="" is not defined yet then we do not want to print it out
        let prefix = ns.prefix();
        let prefix = prefix.as_deref().unwrap_or("");
        let href = ns.href();
        let href = href.as_deref().unwrap_or("");
        let has_empty_ns =
            xml_c14n_str_equal(Some(prefix), None) && xml_c14n_str_equal(Some(href), None);

        for (i, &ns1) in cur.ns_tab[..cur.ns_cur_end].iter().enumerate().rev() {
            if xml_c14n_str_equal(Some(prefix), ns1.prefix().as_deref()) {
                if xml_c14n_str_equal(Some(href), ns1.href().as_deref()) {
                    let node = cur.node_tab[i];
                    return self.is_visible(Some(ns1.into()), Some(node.into()));
                } else {
                    return false;
                }
            }
        }
        has_empty_ns
    }

    /// Finds an attribute in a hidden parent node.
    ///
    /// Returns a pointer to the attribute node (if found) or NULL otherwise.
    #[doc(alias = "xmlC14NFindHiddenParentAttr")]
    fn find_hidden_parent_attr(
        &self,
        mut cur: Option<XmlGenericNodePtr>,
        name: &str,
        ns: &str,
    ) -> Option<XmlAttrPtr> {
        while let Some(now) = cur.filter(|&now| !self.is_visible(Some(now), now.parent())) {
            if let Ok(now) = XmlNodePtr::try_from(now) {
                if let Some(res) = now.has_ns_prop(name, Some(ns)) {
                    // Is this `unwrap` OK ????
                    return Some(res.unwrap());
                }
            }

            cur = now.parent();
        }

        None
    }

    /// Fixes up the xml:base attribute
    ///
    /// Returns the newly created attribute or NULL
    #[doc(alias = "xmlC14NFixupBaseAttr")]
    fn fixup_base_attr(&mut self, xml_base_attr: &XmlAttr) -> Option<XmlAttrPtr> {
        let Some(parent) = xml_base_attr.parent() else {
            xml_c14n_err_param("processing xml:base attribute");
            return None;
        };

        // start from current value
        let Some(mut res) = xml_base_attr
            .children()
            .and_then(|c| c.get_string(Some(self.doc), 1))
        else {
            xml_c14n_err_internal("processing xml:base attribute - can't get attr value");
            return None;
        };

        // go up the stack until we find a node that we rendered already
        let mut cur = parent.parent();
        while let Some(cur_node) = cur.filter(|&cur| !self.is_visible(Some(cur), cur.parent())) {
            if let Ok(cur) = XmlNodePtr::try_from(cur_node) {
                if let Some(attr) = cur.has_ns_prop("base", Some(XML_XML_NAMESPACE)) {
                    // get attr value
                    let Some(mut tmp_str) = (match attr {
                        Ok(attr) => attr
                            .children()
                            .and_then(|c| c.get_string(Some(self.doc), 1)),
                        Err(attr) => attr
                            .children()
                            .and_then(|c| c.get_string(Some(self.doc), 1)),
                    }) else {
                        xml_c14n_err_internal(
                            "processing xml:base attribute - can't get attr value",
                        );
                        return None;
                    };

                    // we need to add '/' if our current base uri ends with '..' or '.'
                    // to ensure that we are forced to go "up" all the time
                    let tmp_str_len = tmp_str.len();
                    if tmp_str_len > 1 && tmp_str.as_bytes()[tmp_str_len - 2] == b'.' {
                        tmp_str.push('/');
                    }

                    // build uri
                    let Some(tmp_str2) = build_uri(&res, &tmp_str) else {
                        xml_c14n_err_internal(
                            "processing xml:base attribute - can't construct uri",
                        );
                        return None;
                    };

                    res = tmp_str2;
                }
            }

            // next
            cur = cur_node.parent();
        }

        // check if result uri is empty or not
        if res.is_empty() {
            return None;
        }

        // create and return the new attribute node
        let Some(attr) = xml_new_ns_prop(None, xml_base_attr.ns, "base", Some(&res)) else {
            xml_c14n_err_internal("processing xml:base attribute - can't construct attribute");
            return None;
        };

        // done
        Some(attr)
    }

    /// Prints out canonical attribute urrent node to the
    /// buffer from C14N context as follows
    ///
    /// Canonical XML v 1.0 (<http://www.w3.org/TR/xml-c14n>)
    ///
    /// Returns 1 on success or 0 on fail.
    #[doc(alias = "xmlC14NPrintAttrs")]
    fn print_attrs(&mut self, attr: XmlAttrPtr) -> bool {
        self.buf.write_str(" ").ok();
        if let Some(prefix) = attr
            .ns
            .as_deref()
            .and_then(|ns| ns.prefix())
            .filter(|p| !p.is_empty())
        {
            self.buf.write_str(&prefix).ok();
            self.buf.write_str(":").ok();
        }

        self.buf.write_str(&attr.name().unwrap()).ok();
        self.buf.write_str("=\"").ok();

        // todo: should we log an error if value==NULL ?
        if let Some(value) = attr
            .children()
            .and_then(|c| c.get_string(Some(self.doc), 1))
        {
            let buffer = normalize_attr(&value);
            self.buf.write_str(&buffer).ok();
        }
        self.buf.write_str("\"").ok();
        true
    }
}

pub type XmlC14NCtxPtr<'a, T> = *mut XmlC14NCtx<'a, T>;
#[repr(C)]
pub enum XmlC14NNormalizationMode {
    XmlC14NNormalizeAttr = 0,
    XmlC14NNormalizeComment = 1,
    XmlC14NNormalizePI = 2,
    XmlC14NNormalizeText = 3,
}

fn xml_c14n_is_node_in_nodeset(
    nodes: &Option<&mut XmlNodeSet>,
    node: Option<XmlGenericNodePtr>,
    parent: Option<XmlGenericNodePtr>,
) -> i32 {
    if let (Some(nodes), Some(node)) = (nodes, node) {
        if let Ok(node) = XmlNsPtr::try_from(node) {
            let mut ns = XmlNs {
                href: node.href.clone(),
                prefix: node.prefix.clone(),
                ..*node
            };
            ns.node = ns.next.map(|ns| ns.into());

            // this is a libxml hack! check xpath.c for details
            if let Some(parent) =
                parent.filter(|p| p.element_type() == XmlElementType::XmlAttributeNode)
            {
                ns.node = parent.parent();
                // ns.next = parent.parent().map_or(null_mut(), |p| p.as_ptr()) as *mut XmlNs;
            } else if let Some(parent) = parent {
                ns.node = Some(parent);
                // ns.next = parent.as_ptr() as *mut XmlNs;
            }

            // If the input is an XPath node-set, then the node-set must explicitly
            // contain every node to be rendered to the canonical form.
            return nodes.contains(Some(unsafe {
                // # Safety
                // `ns` is exactly `XmlNs` and it is not leaked to the out of this function.
                // Therefore, this operation is safe.
                XmlNsPtr::from_raw(&raw mut ns).unwrap().unwrap().into()
            })) as i32;
        } else {
            return nodes.contains(Some(node)) as i32;
        }
    }
    1
}

/// Dumps the canonized image of given XML document into the provided buffer.  
/// For details see "Canonical XML" (<http://www.w3.org/TR/xml-c14n>) or
/// "Exclusive XML Canonicalization" (<http://www.w3.org/TR/xml-exc-c14n>)
///
/// Returns non-negative value on success or a negative value on fail
#[doc(alias = "xmlC14NDocSaveTo")]
pub fn xml_c14n_doc_save_to<'a>(
    doc: XmlDocPtr,
    nodes: Option<&'a mut XmlNodeSet>,
    mode: XmlC14NMode,
    inclusive_ns_prefixes: Option<Vec<String>>,
    with_comments: bool,
    buf: XmlOutputBuffer<'a>,
) -> Result<
    XmlC14NCtx<'a, Option<&'a mut XmlNodeSet>>,
    Option<XmlC14NCtx<'a, Option<&'a mut XmlNodeSet>>>,
> {
    xml_c14n_execute(
        doc,
        xml_c14n_is_node_in_nodeset,
        nodes,
        mode,
        inclusive_ns_prefixes,
        with_comments,
        buf,
    )
}

/// Handle a redefinition of param error
#[doc(alias = "xmlC14NErrParam")]
fn xml_c14n_err_param(extra: &str) {
    __xml_raise_error!(
        None,
        None,
        None,
        null_mut(),
        None,
        XmlErrorDomain::XmlFromC14N,
        XmlParserErrors::XmlErrInternalError,
        XmlErrorLevel::XmlErrError,
        None,
        0,
        Some(extra.to_owned().into()),
        None,
        None,
        0,
        0,
        "Invalid parameter : {}\n",
        extra
    );
}

/// Handle a redefinition of memory error
#[doc(alias = "xmlC14NErrMemory")]
fn xml_c14n_err_memory(extra: &str) {
    __xml_raise_error!(
        None,
        None,
        None,
        null_mut(),
        None,
        XmlErrorDomain::XmlFromC14N,
        XmlParserErrors::XmlErrNoMemory,
        XmlErrorLevel::XmlErrError,
        None,
        0,
        Some(extra.to_owned().into()),
        None,
        None,
        0,
        0,
        "Memory allocation failed : {}\n",
        extra
    );
}

/// Handle a redefinition of internal error
#[doc(alias = "xmlC14NErrInternal")]
fn xml_c14n_err_internal(extra: &str) {
    __xml_raise_error!(
        None,
        None,
        None,
        null_mut(),
        None,
        XmlErrorDomain::XmlFromC14N,
        XmlParserErrors::XmlErrInternalError,
        XmlErrorLevel::XmlErrError,
        None,
        0,
        Some(extra.to_owned().into()),
        None,
        None,
        0,
        0,
        "Internal error : {}\n",
        extra
    );
}

/// Dumps the canonized image of given XML document into memory.  
/// For details see "Canonical XML" (<http://www.w3.org/TR/xml-c14n>) or
/// "Exclusive XML Canonicalization" (<http://www.w3.org/TR/xml-exc-c14n>)
///
/// Returns the number of bytes written on success or a negative value on fail
#[doc(alias = "xmlC14NDocDumpMemory")]
pub fn xml_c14n_doc_dump_memory(
    doc: XmlDocPtr,
    nodes: Option<&mut XmlNodeSet>,
    mode: XmlC14NMode,
    inclusive_ns_prefixes: Option<Vec<String>>,
    with_comments: bool,
    doc_txt_ptr: &mut String,
) -> i32 {
    doc_txt_ptr.clear();

    // create memory buffer with UTF8 (default) encoding
    let Some(buf) = XmlOutputBuffer::from_wrapped_encoder(None) else {
        xml_c14n_err_memory("creating output buffer");
        return -1;
    };

    // canonize document and write to buffer
    match xml_c14n_doc_save_to(doc, nodes, mode, inclusive_ns_prefixes, with_comments, buf) {
        Ok(mut ctx) => {
            let ret = ctx.buf.buffer.len() as i32;
            if ret >= 0 {
                let text = String::from_utf8(take(&mut ctx.buf.buffer));
                match text {
                    Ok(text) => *doc_txt_ptr = text,
                    Err(_) => {
                        xml_c14n_err_memory("copying canonicalized document");
                        return -1;
                    }
                }
            }

            ret
        }
        Err(ctx) => {
            xml_c14n_err_internal("saving doc to output buffer");
            if let Some(mut ctx) = ctx {
                ctx.buf.flush();
            }
            -1
        }
    }
}

/// Dumps the canonized image of given XML document into the file.  
/// For details see "Canonical XML" (<http://www.w3.org/TR/xml-c14n>) or
/// "Exclusive XML Canonicalization" (<http://www.w3.org/TR/xml-exc-c14n>)
///
/// Returns the number of bytes written success or a negative value on fail
#[doc(alias = "xmlC14NDocSave")]
pub fn xml_c14n_doc_save(
    doc: XmlDocPtr,
    nodes: Option<&mut XmlNodeSet>,
    mode: XmlC14NMode,
    inclusive_ns_prefixes: Option<Vec<String>>,
    with_comments: bool,
    filename: &str,
) -> i32 {
    // save the content to a temp buffer, use default UTF8 encoding.
    let Some(buf) = XmlOutputBuffer::from_uri(filename, None) else {
        xml_c14n_err_internal("creating temporary filename");
        return -1;
    };

    // canonize document and write to buffer
    match xml_c14n_doc_save_to(doc, nodes, mode, inclusive_ns_prefixes, with_comments, buf) {
        Ok(mut ctx) => {
            // get the numbers of bytes written
            let is_ok = ctx.buf.error.is_ok();
            if is_ok {
                ctx.buf.flush();
                ctx.buf.written
            } else {
                -1
            }
        }
        Err(ctx) => {
            xml_c14n_err_internal("canonize document to buffer");
            if let Some(mut ctx) = ctx {
                ctx.buf.flush();
            }
            -1
        }
    }
}

/// Handle a redefinition of attribute error
#[doc(alias = "xmlC14NErr")]
fn xml_c14n_err<T>(
    mut ctxt: Option<&mut XmlC14NCtx<'_, T>>,
    node: Option<XmlGenericNodePtr>,
    error: XmlParserErrors,
    msg: &str,
) {
    if let Some(ctxt) = ctxt.as_mut() {
        ctxt.error = error;
    }
    let ctxt = ctxt.map_or(null_mut(), |ctxt| ctxt as *mut XmlC14NCtx<'_, T>);
    __xml_raise_error!(
        None,
        None,
        None,
        ctxt as _,
        node,
        XmlErrorDomain::XmlFromC14N,
        error,
        XmlErrorLevel::XmlErrError,
        None,
        0,
        None,
        None,
        None,
        0,
        0,
        Some(msg),
    );
}

/// Creates new C14N context object to store C14N parameters.
///
/// Returns pointer to newly created object (success) or NULL (fail)
#[doc(alias = "xmlC14NNewCtx")]
fn xml_c14n_new_ctx<T>(
    doc: XmlDocPtr,
    is_visible_callback: Option<XmlC14NIsVisibleCallback<T>>,
    user_data: T,
    mode: XmlC14NMode,
    inclusive_ns_prefixes: Option<Vec<String>>,
    with_comments: bool,
    buf: XmlOutputBuffer<'_>,
) -> Option<XmlC14NCtx<'_, T>> {
    // Validate the encoding output buffer encoding
    if buf.encoder.is_some() {
        xml_c14n_err::<T>(
            None,
            Some(doc.into()),
            XmlParserErrors::XmlC14NRequiresUtf8,
            "xmlC14NNewCtx: output buffer encoder != NULL but C14N requires UTF8 output\n",
        );
        return None;
    }

    // initialize C14N context
    let mut context = XmlC14NCtx {
        doc,
        with_comments,
        is_visible_callback,
        user_data,
        buf,
        parent_is_doc: true,
        pos: XmlC14NPosition::XmlC14NBeforeDocumentElement,
        ns_rendered: Box::new(XmlC14NVisibleNsStack::default()),
        mode: XmlC14NMode::XmlC14N1_0,
        inclusive_ns_prefixes: None,
        error: XmlParserErrors::default(),
    };

    // Set "mode" flag and remember list of inclusive prefixes for exclusive c14n
    context.mode = mode;
    if context.is_exclusive() {
        context.inclusive_ns_prefixes = inclusive_ns_prefixes;
    }
    Some(context)
}

/// Handle a redefinition of relative namespace error
#[doc(alias = "xmlC14NErrRelativeNamespace")]
fn xml_c14n_err_relative_namespace(ns_uri: &str) {
    __xml_raise_error!(
        None,
        None,
        None,
        null_mut(),
        None,
        XmlErrorDomain::XmlFromC14N,
        XmlParserErrors::XmlC14NRelativeNamespace,
        XmlErrorLevel::XmlErrError,
        None,
        0,
        None,
        None,
        None,
        0,
        0,
        "Relative namespace UR is invalid here : {}\n",
        ns_uri
    );
}

/// Compares the namespaces by names (prefixes).
///
/// Returns -1 if ns1 < ns2, 0 if ns1 == ns2 or 1 if ns1 > ns2.
#[doc(alias = "xmlC14NNsCompare")]
fn xml_c14n_ns_compare(ns1: XmlNsPtr, ns2: XmlNsPtr) -> Ordering {
    if ns1 == ns2 {
        return Ordering::Equal;
    }
    match (ns1.prefix(), ns2.prefix()) {
        (Some(p1), Some(p2)) => p1.cmp(&p2),
        (Some(_), None) => Ordering::Greater,
        (None, Some(_)) => Ordering::Less,
        (None, None) => Ordering::Equal,
    }
}

/// Check whether `ns` is a default 'xml:' namespace with `href="http://www.w3.org/XML/1998/namespace"`.  
/// Return `true` if so, otherwise return `false`.
///
/// Please refer to the document of `xmlC14NIsXmlNs` for original libxml2.
/* todo: make it a define? */
#[doc(alias = "xmlC14NIsXmlNs")]
fn xml_c14n_is_xml_ns(ns: XmlNsPtr) -> bool {
    ns.prefix().as_deref() == Some("xml") && ns.href().as_deref() == Some(XML_XML_NAMESPACE)
}

#[doc(alias = "xmlC14NStrEqual")]
fn xml_c14n_str_equal(str1: Option<&str>, str2: Option<&str>) -> bool {
    match (str1, str2) {
        (Some(str1), Some(str2)) => str1 == str2,
        (Some(s), None) | (None, Some(s)) => s.is_empty(),
        (None, None) => true,
    }
}

// const XML_NAMESPACES_DEFAULT: usize = 16;

/// Prints the given attribute to the output buffer from C14N context.
///
/// Returns -1 if attr1 < attr2, 0 if attr1 == attr2 or 1 if attr1 > attr2.
#[doc(alias = "xmlC14NAttrsCompare")]
fn xml_c14n_attrs_compare(attr1: XmlAttrPtr, attr2: XmlAttrPtr) -> Ordering {
    // Simple cases
    if attr1 == attr2 {
        return Ordering::Equal;
    }
    if attr1.ns == attr2.ns {
        return attr1.name().cmp(&attr2.name());
    }
    // Attributes in the default namespace are first
    // because the default namespace is not applied to
    // unqualified attributes
    let Some(attr1_ns) = attr1.ns else {
        return Ordering::Less;
    };
    let Some(attr2_ns) = attr2.ns else {
        return Ordering::Greater;
    };
    if attr1_ns.prefix().is_none() {
        return Ordering::Less;
    }
    if attr2_ns.prefix().is_none() {
        return Ordering::Greater;
    }
    match attr1_ns.href().cmp(&attr2_ns.href()) {
        Ordering::Equal => attr1.name().cmp(&attr2.name()),
        diff => diff,
    }
}

/// Checks whether `attr` is a default "xml:" namespace with `href="http://www.w3.org/XML/1998/namespace"`.  
/// Return `true` if so, otherwise return false.
/* todo: make it a define? */
#[doc(alias = "xmlC14NIsXmlAttr")]
fn xml_c14n_is_xml_attr(attr: XmlAttrPtr) -> bool {
    let ns = attr.ns;
    ns.is_some_and(xml_c14n_is_xml_ns)
}

/// Converts a string to a canonical (normalized) format.  
/// The code is stolen from xmlEncodeEntitiesReentrant().  
/// Added normalization of \x09, \x0a, \x0A and the `mode` parameter
///
/// Returns a normalized string (caller is responsible for calling xmlFree())
/// or NULL if an error occurs
#[doc(alias = "xmlC11NNormalizeString")]
fn normalize_string(input: &str, mode: XmlC14NNormalizationMode) -> String {
    // allocate an translation buffer.
    let mut out = String::new();

    for cur in input.chars() {
        if cur == '<'
            && matches!(
                mode,
                XmlC14NNormalizationMode::XmlC14NNormalizeAttr
                    | XmlC14NNormalizationMode::XmlC14NNormalizeText
            )
        {
            out.push_str("&lt;");
        } else if cur == '>' && matches!(mode, XmlC14NNormalizationMode::XmlC14NNormalizeText) {
            out.push_str("&gt;");
        } else if cur == '&'
            && matches!(
                mode,
                XmlC14NNormalizationMode::XmlC14NNormalizeAttr
                    | XmlC14NNormalizationMode::XmlC14NNormalizeText
            )
        {
            out.push_str("&amp;");
        } else if cur == '"' && matches!(mode, XmlC14NNormalizationMode::XmlC14NNormalizeAttr) {
            out.push_str("&quot;");
        } else if cur == '\x09' && matches!(mode, XmlC14NNormalizationMode::XmlC14NNormalizeAttr) {
            out.push_str("&#x9;");
        } else if cur == '\x0A' && matches!(mode, XmlC14NNormalizationMode::XmlC14NNormalizeAttr) {
            out.push_str("&#xA;");
        } else if cur == '\x0D'
            && matches!(
                mode,
                XmlC14NNormalizationMode::XmlC14NNormalizeAttr
                    | XmlC14NNormalizationMode::XmlC14NNormalizeText
                    | XmlC14NNormalizationMode::XmlC14NNormalizeComment
                    | XmlC14NNormalizationMode::XmlC14NNormalizePI
            )
        {
            out.push_str("&#xD;");
        } else {
            // Works because on UTF-8, all extended sequences cannot
            // result in bytes in the ASCII range.
            out.push(cur);
        }
    }
    out
}

#[doc(alias = "xmlC11NNormalizeAttr")]
fn normalize_attr(a: &str) -> String {
    normalize_string(a, XmlC14NNormalizationMode::XmlC14NNormalizeAttr)
}

#[doc(alias = "xmlC11NNormalizeText")]
fn normalize_text(a: &str) -> String {
    normalize_string(a, XmlC14NNormalizationMode::XmlC14NNormalizeText)
}

#[doc(alias = "xmlC11NNormalizeComment")]
fn normalize_comment(a: &str) -> String {
    normalize_string(a, XmlC14NNormalizationMode::XmlC14NNormalizeComment)
}

#[doc(alias = "xmlC11NNormalizePI")]
fn normalize_pi(a: &str) -> String {
    normalize_string(a, XmlC14NNormalizationMode::XmlC14NNormalizePI)
}

/// Handle a redefinition of invalid node error
#[doc(alias = "xmlC14NErrInvalidNode")]
fn xml_c14n_err_invalid_node(node_type: &str, extra: &str) {
    __xml_raise_error!(
        None,
        None,
        None,
        null_mut(),
        None,
        XmlErrorDomain::XmlFromC14N,
        XmlParserErrors::XmlC14NInvalidNode,
        XmlErrorLevel::XmlErrError,
        None,
        0,
        Some(extra.to_owned().into()),
        None,
        None,
        0,
        0,
        "Node {} is invalid here : {}\n",
        node_type,
        extra
    );
}

/// Handle a redefinition of unknown node error
#[doc(alias = "xmlC14NErrUnknownNode")]
fn xml_c14n_err_unknown_node(node_type: i32, extra: &str) {
    __xml_raise_error!(
        None,
        None,
        None,
        null_mut(),
        None,
        XmlErrorDomain::XmlFromC14N,
        XmlParserErrors::XmlC14NUnknowNode,
        XmlErrorLevel::XmlErrError,
        None,
        0,
        Some(extra.to_owned().into()),
        None,
        None,
        0,
        0,
        "Unknown node type {} found : {}\n",
        node_type,
        extra
    );
}

/// Dumps the canonized image of given XML document into the provided buffer.
/// For details see "Canonical XML" (<http://www.w3.org/TR/xml-c14n>) or
/// "Exclusive XML Canonicalization" (<http://www.w3.org/TR/xml-exc-c14n>)
#[doc(alias = "xmlC14NExecute")]
pub fn xml_c14n_execute<T>(
    doc: XmlDocPtr,
    is_visible_callback: XmlC14NIsVisibleCallback<T>,
    user_data: T,
    mode: XmlC14NMode,
    inclusive_ns_prefixes: Option<Vec<String>>,
    with_comments: bool,
    buf: XmlOutputBuffer<'_>,
) -> Result<XmlC14NCtx<'_, T>, Option<XmlC14NCtx<'_, T>>> {
    //  Validate the encoding output buffer encoding
    if buf.encoder.is_some() {
        xml_c14n_err::<T>(
            None,
            Some(doc.into()),
            XmlParserErrors::XmlC14NRequiresUtf8,
            "xmlC14NExecute: output buffer encoder != NULL but C14N requires UTF8 output\n",
        );
        return Err(None);
    }

    let children = doc.children;
    // currently, `xml_c14n_new_ctx` checks only output buffer encoding.
    // It is already checked at this point, so buffer creation does not fail.
    let mut ctx = xml_c14n_new_ctx(
        doc,
        Some(is_visible_callback),
        user_data,
        mode,
        inclusive_ns_prefixes,
        with_comments,
        buf,
    )
    .unwrap();

    // Root Node
    // The root node is the parent of the top-level document element. The
    // result of processing each of its child nodes that is in the node-set
    // in document order. The root node does not generate a byte order mark,
    // XML declaration, nor anything from within the document type
    // declaration.
    if let Some(children) = children {
        let ret = ctx.process_node_list(Some(children));
        if ret < 0 {
            xml_c14n_err_internal("processing docs children list");
            return Err(Some(ctx));
        }
    }

    // Flush buffer to get number of bytes written
    let ret = ctx.buf.flush();
    if ret < 0 {
        xml_c14n_err_internal("flushing output buffer");
        return Err(Some(ctx));
    }

    Ok(ctx)
}