leptris 1.9.356.0-arm-linux → 1.9.360.0-arm-linux

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/README.adoc +13 -5
  3. data/Rakefile +1 -1
  4. data/ext/leptris/native/native.c +353 -1
  5. data/lib/leptris/version.rb +1 -1
  6. data/lib/leptris/xml/cdata.rb +3 -0
  7. data/lib/leptris/xml/comment.rb +3 -0
  8. data/lib/leptris/xml/declaration.rb +64 -0
  9. data/lib/leptris/xml/doc_type.rb +33 -1
  10. data/lib/leptris/xml/document.rb +77 -2
  11. data/lib/leptris/xml/element.rb +21 -1
  12. data/lib/leptris/xml/entity_reference.rb +3 -0
  13. data/lib/leptris/xml/native.so +0 -0
  14. data/lib/leptris/xml/node_set.rb +22 -0
  15. data/lib/leptris/xml/processing_instruction.rb +3 -0
  16. data/lib/leptris/xml/sax/parser.rb +102 -78
  17. data/lib/leptris/xml/text.rb +4 -0
  18. data/lib/leptris/xml.rb +14 -0
  19. data/lib/libleptris.so +0 -0
  20. data/scripts/gem_smoke.rb +6 -3
  21. data/vendor-src/README.md +1 -1
  22. data/vendor-src/libleptris/CHANGELOG.md +95 -0
  23. data/vendor-src/libleptris/CMakeLists.txt +1 -1
  24. data/vendor-src/libleptris/TODO.xslt-full/REMAINING.md +37 -18
  25. data/vendor-src/libleptris/cli/commands/validate.c +15 -2
  26. data/vendor-src/libleptris/src/include/leptris.h +60 -4
  27. data/vendor-src/libleptris/src/leptris/dtd/content_check.c +280 -144
  28. data/vendor-src/libleptris/src/leptris/dtd/parser.c +17 -4
  29. data/vendor-src/libleptris/src/leptris/rng/parse.c +53 -8
  30. data/vendor-src/libleptris/src/leptris/rng/validate.c +63 -21
  31. data/vendor-src/libleptris/src/leptris/xinclude/xinclude.c +304 -12
  32. data/vendor-src/libleptris/src/leptris/xpath/evaluator.c +30 -0
  33. data/vendor-src/libleptris/src/leptris/xpath/evaluator.h +11 -0
  34. data/vendor-src/libleptris/src/leptris/xpath/evaluator_axes.c +3 -3
  35. data/vendor-src/libleptris/src/leptris/xpath/evaluator_path.c +4 -20
  36. data/vendor-src/libleptris/src/leptris/xpath/parser.c +4 -1
  37. data/vendor-src/libleptris/src/leptris/xpath/vm.c +41 -11
  38. data/vendor-src/libleptris/src/leptris/xpath/xpath_compiled.c +4 -3
  39. data/vendor-src/libleptris/src/leptris/xpath/xpath_public.c +2 -1
  40. data/vendor-src/libleptris/src/leptris/xsd/compile.c +981 -38
  41. data/vendor-src/libleptris/src/leptris/xsd/content_model.c +15 -3
  42. data/vendor-src/libleptris/src/leptris/xsd/validate.c +27 -9
  43. data/vendor-src/libleptris/src/leptris/xsd/xsd_internal.h +1 -0
  44. data/vendor-src/libleptris/test/cli/test_cli.cpp +50 -14
  45. data/vendor-src/libleptris/test/dtd/test_dtd_validate.cpp +80 -4
  46. data/vendor-src/libleptris/test/rng/include-cases/attr-text-choice.rng +18 -0
  47. data/vendor-src/libleptris/test/rng/include-cases/dup-inc-mid.rng +7 -0
  48. data/vendor-src/libleptris/test/rng/include-cases/dup-inc-shared.rng +5 -0
  49. data/vendor-src/libleptris/test/rng/include-cases/dup-inc-top.rng +6 -0
  50. data/vendor-src/libleptris/test/rng/test_rng.cpp +101 -0
  51. data/vendor-src/libleptris/test/xinclude/test_xinclude.cpp +261 -0
  52. data/vendor-src/libleptris/test/xpath/test_xpath.cpp +169 -0
  53. data/vendor-src/libleptris/test/xpath/test_xpath_conformance.cpp +8 -1
  54. data/vendor-src/libleptris/test/xsd/test_xsd_compile.cpp +128 -11
  55. data/vendor-src/libleptris/vcpkg.json +1 -1
  56. metadata +7 -2
@@ -13,6 +13,7 @@
13
13
  #include "../common/port.h" /* strndup shim for MSVC's CRT */
14
14
  #include "xsd_internal.h"
15
15
  #include "xsd_internal.h"
16
+ #include "../dom/element.h" /* attribute walk for the clause checks */
16
17
 
17
18
  #define XSD_NS "http://www.w3.org/2001/XMLSchema"
18
19
 
@@ -94,8 +95,8 @@ static char* xsd_read_file(const char* path, size_t* out_len);
94
95
  static XsdAttrDecl* xsd_copy_attr_rows(const XsdAttrDecl* src);
95
96
  static void xsd_register_complex(struct leptris_xsd_schema* s,
96
97
  XsdCm* root, XsdAttrDecl* attrs,
97
- const char* name,
98
- char* any_attr_ns);
98
+ const char* name, char* any_attr_ns,
99
+ char* base_any_attr_ns);
99
100
  static XsdCm* xsd_capture_complex_model(struct leptris_xsd_schema* s,
100
101
  LeptrisElement ct,
101
102
  const char* name);
@@ -728,11 +729,17 @@ static int xsd_uint_attr(LeptrisElement e, const char* name, int* out) {
728
729
 
729
730
  /* src-include.1 (conformance lane schema-error class, #1691):
730
731
  * the included/redefined schema's targetNamespace must equal the
731
- * includer's — or be absent. Checked on the root's top-level
732
- * include/redefine instructions; the subfile is parsed once here
733
- * purely for the tns verdict (the capture pass re-reads it). */
734
- static int xsd_check_include_tns(struct leptris_xsd_schema* s,
735
- LeptrisElement root) {
732
+ * includer's — or be absent. Slice 4 (#1691): RECURSIVE — the
733
+ * violation can live one hop deeper than the root (582906-2: the
734
+ * root IMPORTS prog1/prog2 legally; prog1/prog2 INCLUDE common
735
+ * whose tns differs from theirs), so every loaded document's own
736
+ * includes are checked against that document's tns. */
737
+ static int xsd_check_include_tns_pass(struct leptris_xsd_schema* s,
738
+ LeptrisElement root,
739
+ const char* doc_tns,
740
+ const char* base_dir,
741
+ int depth) {
742
+ if (!root || depth > 8) return 0;
736
743
  for (LeptrisNodeRef n =
737
744
  leptris_node_first_child(leptris_element_as_node(root));
738
745
  n; n = leptris_node_next_sibling(n)) {
@@ -745,14 +752,21 @@ static int xsd_check_include_tns(struct leptris_xsd_schema* s,
745
752
  leptris_element_expanded_name(c, &local, &prefix, &uri);
746
753
  if (!local || !uri || strcmp(uri, XSD_NS) != 0)
747
754
  continue;
748
- if (strcmp(local, "include") != 0 && strcmp(local, "redefine") != 0)
749
- continue;
755
+ int is_include = (strcmp(local, "include") == 0 ||
756
+ strcmp(local, "redefine") == 0);
757
+ int is_import = (strcmp(local, "import") == 0);
758
+ /* The tns rule only binds include/redefine; imports may
759
+ * differ freely — but an imported document's OWN includes
760
+ * still must satisfy it (582906-2: the root imports
761
+ * prog1/prog2 legally, and their INCLUDE of common is the
762
+ * violation), so imports recurse without the verdict. */
763
+ if (!is_include && !is_import) continue;
750
764
  const char* loc =
751
765
  leptris_element_attribute(c, "schemaLocation");
752
- if (!loc || !s->base_dir)
766
+ if (!loc || !base_dir)
753
767
  continue;
754
768
  char path[4096];
755
- snprintf(path, sizeof(path), "%s/%s", s->base_dir, loc);
769
+ snprintf(path, sizeof(path), "%s/%s", base_dir, loc);
756
770
  size_t flen = 0;
757
771
  char* text = xsd_read_file(path, &flen);
758
772
  if (!text) continue;
@@ -766,37 +780,87 @@ static int xsd_check_include_tns(struct leptris_xsd_schema* s,
766
780
  : NULL;
767
781
  int err = 0;
768
782
  char msg[512];
769
- if (s->target_ns && sub_tns && s->target_ns[0] && sub_tns[0] &&
770
- strcmp(sub_tns, s->target_ns) != 0) {
771
- snprintf(msg, sizeof(msg),
772
- "src-include.1: the target namespace '%s' of the "
773
- "included/redefined schema '%s' differs from '%s' "
774
- "of the including schema",
775
- sub_tns, loc, s->target_ns);
776
- err = 1;
777
- } else if ((!s->target_ns || !s->target_ns[0]) && sub_tns &&
778
- sub_tns[0]) {
779
- snprintf(msg, sizeof(msg),
780
- "src-include.1: the target namespace '%s' of the "
781
- "included/redefined schema '%s' has to be absent, "
782
- "since the including schema has no target namespace",
783
- sub_tns, loc);
784
- err = 1;
783
+ if (is_include) {
784
+ if (doc_tns && sub_tns && doc_tns[0] && sub_tns[0] &&
785
+ strcmp(sub_tns, doc_tns) != 0) {
786
+ snprintf(msg, sizeof(msg),
787
+ "src-include.1: the target namespace '%s' of the "
788
+ "included/redefined schema '%s' differs from '%s' "
789
+ "of the including schema",
790
+ sub_tns, loc, doc_tns);
791
+ err = 1;
792
+ } else if ((!doc_tns || !doc_tns[0]) && sub_tns && sub_tns[0]) {
793
+ snprintf(msg, sizeof(msg),
794
+ "src-include.1: the target namespace '%s' of the "
795
+ "included/redefined schema '%s' has to be absent, "
796
+ "since the including schema has no target namespace",
797
+ sub_tns, loc);
798
+ err = 1;
799
+ }
800
+ }
801
+ if (!err && subroot) {
802
+ /* Recurse with the sub-document's own tns and the
803
+ * sub-file's directory as the include base. */
804
+ char sub_base[4096];
805
+ const char* slash = strrchr(path, '/');
806
+ const char* bslash = strrchr(path, '\\');
807
+ if (bslash && (!slash || bslash > slash)) slash = bslash;
808
+ if (slash) {
809
+ size_t bl = (size_t)(slash - path);
810
+ if (bl >= sizeof(sub_base)) bl = sizeof(sub_base) - 1;
811
+ memcpy(sub_base, path, bl);
812
+ sub_base[bl] = 0;
813
+ if (xsd_check_include_tns_pass(s, subroot, sub_tns,
814
+ sub_base, depth + 1)) {
815
+ err = 2;
816
+ }
817
+ }
785
818
  }
786
819
  leptris_document_free(sub);
787
- if (err) {
820
+ if (err == 1) {
788
821
  xsd_set_error(s, msg);
789
822
  return -1;
790
823
  }
824
+ if (err == 2) return -1;
791
825
  }
792
826
  return 0;
793
827
  }
794
828
 
829
+ static int xsd_check_include_tns(struct leptris_xsd_schema* s,
830
+ LeptrisElement root) {
831
+ return xsd_check_include_tns_pass(s, root, s->target_ns, s->base_dir,
832
+ 0);
833
+ }
834
+
795
835
  /* Schema-structure constraints (the conformance lane's schema-error
796
836
  * class, #1691): the xs:-vocabulary rules the oracle enforces at
797
837
  * load. First violation wins; the message names the rule. */
838
+ /* Attribute allowlists for the xs: vocabulary (conformance lane
839
+ * schema-error class, #1691 slice 3): the src-attribute.* /
840
+ * src-element.* clauses the oracle enforces at load. */
841
+ static int xsd_attr_allowed(const char* local, int toplevel,
842
+ const char* attr) {
843
+ if (strcmp(attr, "id") == 0) return 1;
844
+ if (strcmp(local, "attribute") == 0) {
845
+ if (strcmp(attr, "use") == 0 || strcmp(attr, "form") == 0)
846
+ return !toplevel;
847
+ /* ref form: ref + use only. name form: the rest. */
848
+ return 1; /* name/type/default/fixed allowed in name form */
849
+ }
850
+ if (strcmp(local, "element") == 0) {
851
+ if (strcmp(attr, "minOccurs") == 0 ||
852
+ strcmp(attr, "maxOccurs") == 0)
853
+ return !toplevel;
854
+ if (strcmp(attr, "form") == 0) return !toplevel;
855
+ return 1; /* name/ref/type/default/fixed/nillable/block/... */
856
+ }
857
+ if (strcmp(local, "annotation") == 0)
858
+ return strcmp(attr, "id") == 0;
859
+ return 1;
860
+ }
861
+
798
862
  static int xsd_check_structure(struct leptris_xsd_schema* s,
799
- LeptrisElement e) {
863
+ LeptrisElement e, int depth) {
800
864
  if (!e) return 0;
801
865
  for (LeptrisNodeRef n = leptris_node_first_child(leptris_element_as_node(e));
802
866
  n; n = leptris_node_next_sibling(n)) {
@@ -809,9 +873,27 @@ static int xsd_check_structure(struct leptris_xsd_schema* s,
809
873
  leptris_element_expanded_name(c, &local, &prefix, &uri);
810
874
  if (!local || !uri || strcmp(uri, XSD_NS) != 0) {
811
875
  /* foreign-namespace content: recurse only */
812
- if (xsd_check_structure(s, c) != 0) return -1;
876
+ if (xsd_check_structure(s, c, depth + 1) != 0) return -1;
813
877
  continue;
814
878
  }
879
+ int toplevel = (depth == 0);
880
+ /* Unknown attributes on the xs: vocabulary (annot-err: a
881
+ * 'death' attribute on xs:annotation). */
882
+ for (LeptrisAttribute a = leptris_element_get_first_attribute(c);
883
+ a; a = leptris_attr_next(a)) {
884
+ const char* an = leptris_attribute_get_name(
885
+ (LeptrisAttribute)a);
886
+ if (strncmp(an, "xmlns", 5) == 0) continue;
887
+ if (!xsd_attr_allowed(local, toplevel, an)) {
888
+ char msg[160];
889
+ snprintf(msg, sizeof(msg),
890
+ "src-%s: The attribute '%s' is not allowed on "
891
+ "xs:%s",
892
+ local, an, local);
893
+ xsd_set_error(s, msg);
894
+ return -1;
895
+ }
896
+ }
815
897
  if (strcmp(local, "attribute") == 0 ||
816
898
  strcmp(local, "element") == 0) {
817
899
  const char* nm = leptris_element_attribute(c, "name");
@@ -834,18 +916,79 @@ static int xsd_check_structure(struct leptris_xsd_schema* s,
834
916
  xsd_set_error(s, msg);
835
917
  return -1;
836
918
  }
919
+ /* name == "xmlns" reserves the namespace declaration
920
+ * name (scc-no-xmlns). */
921
+ if (strcmp(local, "attribute") == 0 && nm &&
922
+ strcmp(nm, "xmlns") == 0) {
923
+ xsd_set_error(
924
+ s,
925
+ "scc-no-xmlns: an attribute declaration must not "
926
+ "be named 'xmlns'");
927
+ return -1;
928
+ }
929
+ const char* dv = leptris_element_attribute(c, "default");
930
+ const char* fx = leptris_element_attribute(c, "fixed");
931
+ if (dv && dv[0] && fx && fx[0]) {
932
+ char msg[160];
933
+ snprintf(msg, sizeof(msg),
934
+ "src-element.1: xs:%s cannot carry both "
935
+ "'default' and 'fixed'",
936
+ local);
937
+ xsd_set_error(s, msg);
938
+ return -1;
939
+ }
940
+ }
941
+ if (strcmp(local, "element") == 0) {
942
+ /* type attribute and an inline type child are mutually
943
+ * exclusive (src-element.3 / src-attribute.4). */
944
+ const char* ty = leptris_element_attribute(c, "type");
945
+ int has_type_child = 0;
946
+ for (LeptrisNodeRef k = leptris_node_first_child(
947
+ leptris_element_as_node(c));
948
+ k; k = leptris_node_next_sibling(k)) {
949
+ if (leptris_node_get_type(k) !=
950
+ LEPTRIS_NODE_TYPE_ELEMENT)
951
+ continue;
952
+ const char* kl = NULL;
953
+ const char* kp = NULL;
954
+ const char* ku = NULL;
955
+ leptris_element_expanded_name((LeptrisElement)k, &kl,
956
+ &kp, &ku);
957
+ if (kl && ku && strcmp(ku, XSD_NS) == 0 &&
958
+ (strcmp(kl, "complexType") == 0 ||
959
+ strcmp(kl, "simpleType") == 0))
960
+ has_type_child = 1;
961
+ }
962
+ if (ty && ty[0] && has_type_child) {
963
+ xsd_set_error(
964
+ s,
965
+ "src-element.3: xs:element cannot carry both a "
966
+ "'type' attribute and an inline type child");
967
+ return -1;
968
+ }
837
969
  }
838
970
  if (strcmp(local, "element") == 0 ||
839
971
  strcmp(local, "sequence") == 0 ||
840
972
  strcmp(local, "choice") == 0 ||
841
973
  strcmp(local, "all") == 0) {
842
- int mn = -1, mx = -1;
974
+ int mn = 1, mx = 1;
843
975
  int has_mn = xsd_uint_attr(c, "minOccurs", &mn);
844
976
  int has_mx = xsd_uint_attr(c, "maxOccurs", &mx);
845
977
  const char* mx_raw =
846
978
  leptris_element_attribute(c, "maxOccurs");
847
979
  int unbounded =
848
980
  (mx_raw && strcmp(mx_raw, "unbounded") == 0);
981
+ /* Absent minOccurs defaults to 1: maxOccurs="0" alone is
982
+ * a violation (src-element.2.2's maxOccurs=0 shape). */
983
+ if (has_mn && !has_mx && !unbounded && mn > mx) {
984
+ char msg[160];
985
+ snprintf(msg, sizeof(msg),
986
+ "occurrence: xs:%s minOccurs (%d) exceeds "
987
+ "maxOccurs (%d)",
988
+ local, mn, mx);
989
+ xsd_set_error(s, msg);
990
+ return -1;
991
+ }
849
992
  if (has_mn && has_mx && !unbounded && mn > mx) {
850
993
  char msg[160];
851
994
  snprintf(msg, sizeof(msg),
@@ -856,11 +999,779 @@ static int xsd_check_structure(struct leptris_xsd_schema* s,
856
999
  return -1;
857
1000
  }
858
1001
  }
859
- if (xsd_check_structure(s, c) != 0) return -1;
1002
+ if (xsd_check_structure(s, c, depth + 1) != 0) return -1;
1003
+ }
1004
+ return 0;
1005
+ }
1006
+
1007
+ /* ---- #1691 slice 4: derivation constraints -----------------------
1008
+ *
1009
+ * The oracle (libxml2 2.9.13) enforces two derivation-ok families at
1010
+ * load that the capture pass never sees:
1011
+ * - cos-ct-derived-ok content-type match: a complexContent
1012
+ * extension must not change the content type (issue491).
1013
+ * - derivation-ok-restriction-4.1: a restricted complex type with
1014
+ * an attribute wildcard requires the base to carry a wildcard,
1015
+ * and a subset of it.
1016
+ * Document-level, before capture: only same-document global complex
1017
+ * types resolve here (the corpus shapes); imports ride the capture
1018
+ * tables and stay out of this pass. */
1019
+
1020
+ /* In-scope prefix -> uri for `e` (namespace declarations live in
1021
+ * the element's own decl list — leptris_element_attribute does not
1022
+ * answer "xmlns"/"xmlns:p"). Walks the ancestor chain. */
1023
+ static void xsd_ns_for_prefix(LeptrisElement e, const char* prefix,
1024
+ char* out, size_t outsz) {
1025
+ out[0] = 0;
1026
+ if (prefix && !prefix[0]) prefix = NULL;
1027
+ for (LeptrisElement a = e; a; a = leptris_element_get_parent(a)) {
1028
+ size_t n = leptris_element_namespace_count(a);
1029
+ for (size_t i = 0; i < n; i++) {
1030
+ const char* dp =
1031
+ leptris_element_namespace_decl_prefix(a, i);
1032
+ if (prefix) {
1033
+ if (dp && strcmp(dp, prefix) == 0) {
1034
+ const char* du =
1035
+ leptris_element_namespace_decl_uri(a, i);
1036
+ snprintf(out, outsz, "%s", du ? du : "");
1037
+ return;
1038
+ }
1039
+ } else if (!dp) {
1040
+ const char* du =
1041
+ leptris_element_namespace_decl_uri(a, i);
1042
+ snprintf(out, outsz, "%s", du ? du : "");
1043
+ return;
1044
+ }
1045
+ }
1046
+ }
1047
+ }
1048
+
1049
+ /* Split `qname` (possibly prefixed) and resolve the prefix in
1050
+ * e's scope. Unresolvable prefixes yield empty local (skip). */
1051
+ static void xsd_resolve_qname(LeptrisElement e, const char* qname,
1052
+ char* uri, size_t urisz, char* local,
1053
+ size_t localsz) {
1054
+ uri[0] = 0;
1055
+ local[0] = 0;
1056
+ if (!qname || !qname[0]) return;
1057
+ const char* colon = strchr(qname, ':');
1058
+ char prefix[64] = {0};
1059
+ if (colon) {
1060
+ size_t pl = (size_t)(colon - qname);
1061
+ if (pl >= sizeof(prefix)) pl = sizeof(prefix) - 1;
1062
+ memcpy(prefix, qname, pl);
1063
+ snprintf(local, localsz, "%s", colon + 1);
1064
+ char u[256];
1065
+ xsd_ns_for_prefix(e, prefix, u, sizeof(u));
1066
+ if (!u[0]) {
1067
+ local[0] = 0;
1068
+ return;
1069
+ }
1070
+ snprintf(uri, urisz, "%s", u);
1071
+ } else {
1072
+ char u[256];
1073
+ xsd_ns_for_prefix(e, NULL, u, sizeof(u));
1074
+ snprintf(uri, urisz, "%s", u);
1075
+ snprintf(local, localsz, "%s", qname);
1076
+ }
1077
+ }
1078
+
1079
+ static LeptrisElement xsd_find_child(LeptrisElement e, const char* local) {
1080
+ if (!e) return NULL;
1081
+ for (LeptrisNodeRef n = leptris_node_first_child(
1082
+ leptris_element_as_node(e));
1083
+ n; n = leptris_node_next_sibling(n)) {
1084
+ if (leptris_node_get_type(n) != LEPTRIS_NODE_TYPE_ELEMENT)
1085
+ continue;
1086
+ const char* l = NULL;
1087
+ const char* p = NULL;
1088
+ const char* u = NULL;
1089
+ leptris_element_expanded_name((LeptrisElement)n, &l, &p, &u);
1090
+ if (l && u && strcmp(u, XSD_NS) == 0 && strcmp(l, local) == 0)
1091
+ return (LeptrisElement)n;
1092
+ }
1093
+ return NULL;
1094
+ }
1095
+
1096
+ static LeptrisElement xsd_find_any_attribute(LeptrisElement e) {
1097
+ if (!e) return NULL;
1098
+ for (LeptrisNodeRef n = leptris_node_first_child(
1099
+ leptris_element_as_node(e));
1100
+ n; n = leptris_node_next_sibling(n)) {
1101
+ if (leptris_node_get_type(n) != LEPTRIS_NODE_TYPE_ELEMENT)
1102
+ continue;
1103
+ const char* l = NULL;
1104
+ const char* p = NULL;
1105
+ const char* u = NULL;
1106
+ leptris_element_expanded_name((LeptrisElement)n, &l, &p, &u);
1107
+ if (l && u && strcmp(u, XSD_NS) == 0) {
1108
+ if (strcmp(l, "anyAttribute") == 0) return (LeptrisElement)n;
1109
+ LeptrisElement deep = xsd_find_any_attribute((LeptrisElement)n);
1110
+ if (deep) return deep;
1111
+ }
1112
+ }
1113
+ return NULL;
1114
+ }
1115
+
1116
+ static int xsd_mixed_flag(LeptrisElement ct) {
1117
+ const char* m = leptris_element_attribute(ct, "mixed");
1118
+ return (m && (strcmp(m, "true") == 0 || strcmp(m, "1") == 0));
1119
+ }
1120
+
1121
+ /* Global complex type by {uri}local among the root's depth-0
1122
+ * declarations (globals live in the schema's target namespace). */
1123
+ static LeptrisElement xsd_find_global_ct(LeptrisElement root,
1124
+ const char* uri,
1125
+ const char* local) {
1126
+ if (!root || !uri || !local) return NULL;
1127
+ for (LeptrisNodeRef n = leptris_node_first_child(
1128
+ leptris_element_as_node(root));
1129
+ n; n = leptris_node_next_sibling(n)) {
1130
+ if (leptris_node_get_type(n) != LEPTRIS_NODE_TYPE_ELEMENT)
1131
+ continue;
1132
+ LeptrisElement c = (LeptrisElement)n;
1133
+ const char* l = NULL;
1134
+ const char* p = NULL;
1135
+ const char* u = NULL;
1136
+ leptris_element_expanded_name(c, &l, &p, &u);
1137
+ if (!l || !u || strcmp(u, XSD_NS) != 0 ||
1138
+ strcmp(l, "complexType") != 0)
1139
+ continue;
1140
+ const char* nm = leptris_element_attribute(c, "name");
1141
+ if (!nm || strcmp(nm, local) != 0) continue;
1142
+ LeptrisElement schema = leptris_element_get_parent(c);
1143
+ const char* tns = schema
1144
+ ? leptris_element_attribute(schema, "targetNamespace")
1145
+ : NULL;
1146
+ if (strcmp(uri, tns ? tns : "") == 0) return c;
1147
+ }
1148
+ return NULL;
1149
+ }
1150
+
1151
+ static int xsd_check_derivation(struct leptris_xsd_schema* s,
1152
+ LeptrisElement root) {
1153
+ if (!root) return 0;
1154
+ for (LeptrisNodeRef n = leptris_node_first_child(
1155
+ leptris_element_as_node(root));
1156
+ n; n = leptris_node_next_sibling(n)) {
1157
+ if (leptris_node_get_type(n) != LEPTRIS_NODE_TYPE_ELEMENT)
1158
+ continue;
1159
+ LeptrisElement ct = (LeptrisElement)n;
1160
+ const char* l = NULL;
1161
+ const char* p = NULL;
1162
+ const char* u = NULL;
1163
+ leptris_element_expanded_name(ct, &l, &p, &u);
1164
+ if (!l || !u || strcmp(u, XSD_NS) != 0 ||
1165
+ strcmp(l, "complexType") != 0)
1166
+ continue;
1167
+ LeptrisElement cc = xsd_find_child(ct, "complexContent");
1168
+ if (!cc) continue;
1169
+ LeptrisElement ext = xsd_find_child(cc, "extension");
1170
+ LeptrisElement res = xsd_find_child(cc, "restriction");
1171
+ LeptrisElement deriv = ext ? ext : res;
1172
+ if (!deriv) continue;
1173
+ int is_extension = (deriv == ext);
1174
+ const char* base_q = leptris_element_attribute(deriv, "base");
1175
+ if (!base_q || !base_q[0]) continue;
1176
+ char buri[256], blocal[128];
1177
+ xsd_resolve_qname(ct, base_q, buri, sizeof(buri), blocal,
1178
+ sizeof(blocal));
1179
+ if (!blocal[0]) continue;
1180
+ if (strcmp(buri, XSD_NS) == 0) continue; /* builtin base */
1181
+ LeptrisElement base = xsd_find_global_ct(root, buri, blocal);
1182
+ if (!base) continue; /* cross-document: capture handles it */
1183
+
1184
+ if (is_extension) {
1185
+ /* cos-ct-derived-ok: the content types must agree. */
1186
+ LeptrisElement cc_b = xsd_find_child(base, "complexContent");
1187
+ LeptrisElement sc_b = xsd_find_child(base, "simpleContent");
1188
+ int d_mixed = xsd_mixed_flag(ct);
1189
+ int b_mixed = cc_b ? xsd_mixed_flag(base) : 0;
1190
+ if ((sc_b && !cc_b) || (cc_b && d_mixed != b_mixed) ||
1191
+ (!cc_b && !sc_b && d_mixed)) {
1192
+ xsd_set_error(
1193
+ s,
1194
+ "derivation-ok: the content type of both, the "
1195
+ "type and its base type, must either 'mixed' or "
1196
+ "'element-only'");
1197
+ return -1;
1198
+ }
1199
+ } else {
1200
+ /* derivation-ok-restriction-4.1: attribute wildcards. */
1201
+ LeptrisElement r_any = xsd_find_any_attribute(deriv);
1202
+ if (!r_any) continue;
1203
+ LeptrisElement b_any = xsd_find_any_attribute(base);
1204
+ if (!b_any) {
1205
+ char msg[512];
1206
+ snprintf(msg, sizeof(msg),
1207
+ "derivation-ok-restriction-4.1: the complex "
1208
+ "type definition has an attribute wildcard, "
1209
+ "but the base complex type definition '%s' "
1210
+ "does not have one",
1211
+ blocal);
1212
+ xsd_set_error(s, msg);
1213
+ return -1;
1214
+ }
1215
+ const char* r_ns =
1216
+ leptris_element_attribute(r_any, "namespace");
1217
+ const char* b_ns =
1218
+ leptris_element_attribute(b_any, "namespace");
1219
+ if (!r_ns || !r_ns[0]) r_ns = "##any";
1220
+ if (!b_ns || !b_ns[0]) b_ns = "##any";
1221
+ if (strcmp(b_ns, "##any") == 0) continue;
1222
+ if (strcmp(r_ns, "##any") != 0 &&
1223
+ strcmp(r_ns, b_ns) != 0) {
1224
+ /* token-list subset: every derived token must be in
1225
+ * the base list (##other/##local compared verbatim;
1226
+ * the corpus shapes are plain lists). */
1227
+ char bcopy[256];
1228
+ snprintf(bcopy, sizeof(bcopy), "%s", b_ns);
1229
+ int subset = 1;
1230
+ char rcopy[256];
1231
+ snprintf(rcopy, sizeof(rcopy), "%s", r_ns);
1232
+ for (char* tok = strtok(rcopy, " "); tok;
1233
+ tok = strtok(NULL, " ")) {
1234
+ int found = 0;
1235
+ char* save = NULL;
1236
+ char* bt = strtok_r(bcopy, " ", &save);
1237
+ while (bt) {
1238
+ if (strcmp(bt, tok) == 0) {
1239
+ found = 1;
1240
+ break;
1241
+ }
1242
+ bt = strtok_r(NULL, " ", &save);
1243
+ }
1244
+ if (!found) {
1245
+ subset = 0;
1246
+ break;
1247
+ }
1248
+ snprintf(bcopy, sizeof(bcopy), "%s", b_ns);
1249
+ }
1250
+ if (!subset) {
1251
+ char msg[512];
1252
+ snprintf(msg, sizeof(msg),
1253
+ "derivation-ok-restriction-4.1: the "
1254
+ "attribute wildcard is not a valid "
1255
+ "subset of the wildcard in the base "
1256
+ "complex type definition '%s'",
1257
+ blocal);
1258
+ xsd_set_error(s, msg);
1259
+ return -1;
1260
+ }
1261
+ }
1262
+ }
860
1263
  }
861
1264
  return 0;
862
1265
  }
863
1266
 
1267
+ /* ---- #1691 slice 6: UPA (Unique Particle Attribution) -------------
1268
+ *
1269
+ * The oracle rejects non-deterministic content models at load
1270
+ * (deter0_0: (odd,even)* followed by odd?; any4_0: a wildcard
1271
+ * choice branch overlapping an explicit element). The check that
1272
+ * catches the real-world shapes without building a full derivative
1273
+ * machine:
1274
+ * - choice/all branches: pairwise FIRST-set overlap;
1275
+ * - sequences: a child that can repeat or be skipped may not have
1276
+ * a FIRST overlap with the union of its siblings' FIRSTs
1277
+ * ((a,a) stays deterministic; (a?,a) and (a*,a) do not).
1278
+ * FIRST entries are (name, wildcard-ns-class) pairs; a wildcard
1279
+ * overlaps a name when its namespace set covers the name's
1280
+ * namespace (the schema's targetNamespace for bare names). */
1281
+
1282
+ #define XSD_UPA_MAX 64
1283
+
1284
+ struct xsd_upa_first {
1285
+ const char* name; /* local name; NULL for a pure wildcard */
1286
+ char ns[256]; /* the name's namespace ("" unqualified) */
1287
+ char wc[256]; /* wildcard namespace spec ("" if not wc) */
1288
+ };
1289
+
1290
+ static int xsd_upa_ns_covers(const char* wc, const char* ns,
1291
+ const char* tns) {
1292
+ if (!wc || !wc[0]) return 0;
1293
+ if (strcmp(wc, "##any") == 0) return 1;
1294
+ if (strcmp(wc, "##other") == 0)
1295
+ /* any namespace OTHER than the schema's targetNamespace and
1296
+ * absent from no-namespace */
1297
+ return ns && ns[0] && (!tns || !tns[0] || strcmp(ns, tns) != 0);
1298
+ if (strcmp(wc, "##targetNamespace") == 0)
1299
+ return ns && ns[0] && tns && strcmp(ns, tns) == 0;
1300
+ if (strcmp(wc, "##local") == 0) return !ns || !ns[0];
1301
+ return ns && ns[0] && strstr(wc, ns) != NULL;
1302
+ }
1303
+
1304
+ static int xsd_upa_first_overlap(const struct xsd_upa_first* a,
1305
+ const struct xsd_upa_first* b,
1306
+ const char* tns) {
1307
+ if (a->wc[0] && b->wc[0]) {
1308
+ /* two wildcards overlap unless their sets are disjoint —
1309
+ * conservatively report any pair carrying ##any, or equal
1310
+ * specs */
1311
+ if (strcmp(a->wc, "##any") == 0 || strcmp(b->wc, "##any") == 0 ||
1312
+ strcmp(a->wc, b->wc) == 0)
1313
+ return 1;
1314
+ return 0;
1315
+ }
1316
+ if (a->wc[0])
1317
+ return b->name ? xsd_upa_ns_covers(a->wc, b->ns, tns) : 0;
1318
+ if (b->wc[0])
1319
+ return a->name ? xsd_upa_ns_covers(b->wc, a->ns, tns) : 0;
1320
+ if (!a->name || !b->name) return 0;
1321
+ if (strcmp(a->name, b->name) != 0) return 0;
1322
+ const char* ans = a->ns[0] ? a->ns : (tns ? tns : "");
1323
+ const char* bns = b->ns[0] ? b->ns : (tns ? tns : "");
1324
+ return strcmp(ans, bns) == 0;
1325
+ }
1326
+
1327
+ static int xsd_upa_nullable(LeptrisElement e, int depth);
1328
+ static int xsd_upa_first(LeptrisElement e, struct xsd_upa_first* out,
1329
+ int max, int* n, const char* tns, int depth);
1330
+
1331
+ static void xsd_upa_name_of(LeptrisElement e, const char* tns,
1332
+ struct xsd_upa_first* f) {
1333
+ memset(f, 0, sizeof(*f));
1334
+ const char* ref = leptris_element_attribute(e, "ref");
1335
+ const char* nm = leptris_element_attribute(e, "name");
1336
+ f->name = (ref && ref[0]) ? strrchr(ref, ':') ? strrchr(ref, ':') + 1
1337
+ : ref
1338
+ : (nm ? nm : NULL);
1339
+ (void)tns;
1340
+ if (f->name && strchr(f->name, ':')) {
1341
+ /* prefixed literal name — strip for the local compare */
1342
+ f->name = strrchr(f->name, ':') + 1;
1343
+ }
1344
+ }
1345
+
1346
+ static int xsd_upa_first(LeptrisElement e, struct xsd_upa_first* out,
1347
+ int max, int* n, const char* tns, int depth) {
1348
+ if (!e || depth > 24 || *n >= max) return 0;
1349
+ const char* l = NULL;
1350
+ const char* p2 = NULL;
1351
+ const char* u = NULL;
1352
+ leptris_element_expanded_name(e, &l, &p2, &u);
1353
+ if (!l || !u || strcmp(u, XSD_NS) != 0) return 0;
1354
+
1355
+ if (strcmp(l, "element") == 0) {
1356
+ struct xsd_upa_first f;
1357
+ xsd_upa_name_of(e, tns, &f);
1358
+ /* the element's namespace: the schema's targetNamespace for
1359
+ * qualified globals/locals (form=qualified is the corpus
1360
+ * shape); unqualified names carry "" */
1361
+ const char* form = leptris_element_attribute(e, "form");
1362
+ snprintf(f.ns, sizeof(f.ns), "%s",
1363
+ (form && strcmp(form, "unqualified") == 0)
1364
+ ? ""
1365
+ : (tns ? tns : ""));
1366
+ if (f.name && *n < max) out[(*n)++] = f;
1367
+ return 0;
1368
+ }
1369
+ if (strcmp(l, "any") == 0) {
1370
+ struct xsd_upa_first f;
1371
+ memset(&f, 0, sizeof(f));
1372
+ const char* ns = leptris_element_attribute(e, "namespace");
1373
+ snprintf(f.wc, sizeof(f.wc), "%s",
1374
+ (ns && ns[0]) ? ns : "##any");
1375
+ if (*n < max) out[(*n)++] = f;
1376
+ return 0;
1377
+ }
1378
+ if (strcmp(l, "sequence") == 0 || strcmp(l, "group") == 0) {
1379
+ for (LeptrisNodeRef c = leptris_node_first_child(
1380
+ leptris_element_as_node(e));
1381
+ c; c = leptris_node_next_sibling(c)) {
1382
+ if (leptris_node_get_type(c) != LEPTRIS_NODE_TYPE_ELEMENT)
1383
+ continue;
1384
+ xsd_upa_first((LeptrisElement)c, out, max, n, tns, depth + 1);
1385
+ if (!xsd_upa_nullable((LeptrisElement)c, depth + 1)) break;
1386
+ }
1387
+ return 0;
1388
+ }
1389
+ if (strcmp(l, "choice") == 0 || strcmp(l, "all") == 0) {
1390
+ for (LeptrisNodeRef c = leptris_node_first_child(
1391
+ leptris_element_as_node(e));
1392
+ c; c = leptris_node_next_sibling(c)) {
1393
+ if (leptris_node_get_type(c) != LEPTRIS_NODE_TYPE_ELEMENT)
1394
+ continue;
1395
+ xsd_upa_first((LeptrisElement)c, out, max, n, tns, depth + 1);
1396
+ }
1397
+ return 0;
1398
+ }
1399
+ /* optional wrappers (xs already models minOccurs on the particle
1400
+ * itself; xs:element with minOccurs=0 is handled by callers) */
1401
+ return 0;
1402
+ }
1403
+
1404
+ static int xsd_upa_nullable(LeptrisElement e, int depth) {
1405
+ if (!e || depth > 24) return 0;
1406
+ const char* l = NULL;
1407
+ const char* p2 = NULL;
1408
+ const char* u = NULL;
1409
+ leptris_element_expanded_name(e, &l, &p2, &u);
1410
+ if (!l || !u || strcmp(u, XSD_NS) != 0) return 0;
1411
+ int mn = 1;
1412
+ const char* mn_s = leptris_element_attribute(e, "minOccurs");
1413
+ if (mn_s && strcmp(mn_s, "0") == 0) return 1;
1414
+ if (strcmp(l, "choice") == 0) {
1415
+ for (LeptrisNodeRef c = leptris_node_first_child(
1416
+ leptris_element_as_node(e));
1417
+ c; c = leptris_node_next_sibling(c))
1418
+ if (leptris_node_get_type(c) == LEPTRIS_NODE_TYPE_ELEMENT &&
1419
+ xsd_upa_nullable((LeptrisElement)c, depth + 1))
1420
+ return 1;
1421
+ return 0;
1422
+ }
1423
+ if (strcmp(l, "sequence") == 0) {
1424
+ for (LeptrisNodeRef c = leptris_node_first_child(
1425
+ leptris_element_as_node(e));
1426
+ c; c = leptris_node_next_sibling(c)) {
1427
+ if (leptris_node_get_type(c) != LEPTRIS_NODE_TYPE_ELEMENT)
1428
+ continue;
1429
+ if (!xsd_upa_nullable((LeptrisElement)c, depth + 1))
1430
+ return 0;
1431
+ }
1432
+ return 1;
1433
+ }
1434
+ return 0;
1435
+ }
1436
+
1437
+ static int xsd_upa_check_node(struct leptris_xsd_schema* s,
1438
+ LeptrisElement e, const char* tns,
1439
+ int depth);
1440
+
1441
+ static int xsd_upa_collect_children(LeptrisElement e, LeptrisElement* kids,
1442
+ int max) {
1443
+ int n = 0;
1444
+ for (LeptrisNodeRef c = leptris_node_first_child(
1445
+ leptris_element_as_node(e));
1446
+ c && n < max; c = leptris_node_next_sibling(c)) {
1447
+ if (leptris_node_get_type(c) != LEPTRIS_NODE_TYPE_ELEMENT)
1448
+ continue;
1449
+ const char* kl = NULL;
1450
+ const char* kp = NULL;
1451
+ const char* ku = NULL;
1452
+ leptris_element_expanded_name((LeptrisElement)c, &kl, &kp, &ku);
1453
+ if (!kl || !ku || strcmp(ku, XSD_NS) != 0) continue;
1454
+ if (strcmp(kl, "annotation") == 0) continue;
1455
+ kids[n++] = (LeptrisElement)c;
1456
+ }
1457
+ return n;
1458
+ }
1459
+
1460
+ static int xsd_upa_particle_can_repeat_or_skip(LeptrisElement c) {
1461
+ const char* mn = leptris_element_attribute(c, "minOccurs");
1462
+ const char* mx = leptris_element_attribute(c, "maxOccurs");
1463
+ if (mn && strcmp(mn, "0") == 0) return 1;
1464
+ if (mx && strcmp(mx, "unbounded") == 0) return 1;
1465
+ if (mx && mx[0] && strcmp(mx, "1") != 0) {
1466
+ long v = strtol(mx, NULL, 10);
1467
+ if (v != 1) return 1;
1468
+ }
1469
+ /* an optional GROUP: its restart can collide with the sibling */
1470
+ const char* l = NULL;
1471
+ const char* p2 = NULL;
1472
+ const char* u = NULL;
1473
+ leptris_element_expanded_name(c, &l, &p2, &u);
1474
+ return 0;
1475
+ }
1476
+
1477
+ static int xsd_upa_check_node(struct leptris_xsd_schema* s,
1478
+ LeptrisElement e, const char* tns,
1479
+ int depth) {
1480
+ if (!e || depth > 24) return 0;
1481
+ const char* l = NULL;
1482
+ const char* p2 = NULL;
1483
+ const char* u = NULL;
1484
+ leptris_element_expanded_name(e, &l, &p2, &u);
1485
+ if (!l || !u || strcmp(u, XSD_NS) != 0) return 0;
1486
+ int is_choice = (strcmp(l, "choice") == 0);
1487
+ int is_all = (strcmp(l, "all") == 0);
1488
+ int is_seq = (strcmp(l, "sequence") == 0);
1489
+
1490
+ LeptrisElement kids[64];
1491
+ int nk = xsd_upa_collect_children(e, kids, 64);
1492
+
1493
+ if ((is_choice || is_all) && nk >= 2) {
1494
+ for (int i = 0; i < nk; i++) {
1495
+ struct xsd_upa_first fi[XSD_UPA_MAX];
1496
+ int ni = 0;
1497
+ xsd_upa_first(kids[i], fi, XSD_UPA_MAX, &ni, tns, depth + 1);
1498
+ for (int j = i + 1; j < nk; j++) {
1499
+ struct xsd_upa_first fj[XSD_UPA_MAX];
1500
+ int nj = 0;
1501
+ xsd_upa_first(kids[j], fj, XSD_UPA_MAX, &nj, tns,
1502
+ depth + 1);
1503
+ for (int a = 0; a < ni; a++)
1504
+ for (int b = 0; b < nj; b++) {
1505
+ if (xsd_upa_first_overlap(&fi[a], &fj[b], tns)) {
1506
+ xsd_set_error(
1507
+ s,
1508
+ "upa: the content model is not "
1509
+ "deterministic: ambiguous choice "
1510
+ "branches");
1511
+ return -1;
1512
+ }
1513
+ }
1514
+ }
1515
+ }
1516
+ }
1517
+ if (is_seq) {
1518
+ for (int i = 0; i + 1 < nk; i++) {
1519
+ if (!xsd_upa_particle_can_repeat_or_skip(kids[i])) continue;
1520
+ struct xsd_upa_first fi[XSD_UPA_MAX];
1521
+ int ni = 0;
1522
+ xsd_upa_first(kids[i], fi, XSD_UPA_MAX, &ni, tns, depth + 1);
1523
+ /* the union of every LATER sibling's FIRST — after the
1524
+ * repeating/skippable child, any of them may come next */
1525
+ struct xsd_upa_first rest[XSD_UPA_MAX];
1526
+ int nr = 0;
1527
+ for (int j = i + 1; j < nk; j++)
1528
+ xsd_upa_first(kids[j], rest, XSD_UPA_MAX, &nr, tns,
1529
+ depth + 1);
1530
+ for (int a = 0; a < ni; a++)
1531
+ for (int b = 0; b < nr; b++) {
1532
+ if (xsd_upa_first_overlap(&fi[a], &rest[b], tns)) {
1533
+ xsd_set_error(
1534
+ s,
1535
+ "upa: the content model is not "
1536
+ "deterministic: an optional or repeating "
1537
+ "particle overlaps a following particle");
1538
+ return -1;
1539
+ }
1540
+ }
1541
+ }
1542
+ }
1543
+ for (int i = 0; i < nk; i++)
1544
+ if (xsd_upa_check_node(s, kids[i], tns, depth + 1) != 0)
1545
+ return -1;
1546
+ return 0;
1547
+ }
1548
+
1549
+ static int xsd_check_upa(struct leptris_xsd_schema* s,
1550
+ LeptrisElement root) {
1551
+ if (!root) return 0;
1552
+ const char* tns = s->target_ns;
1553
+ for (LeptrisNodeRef n = leptris_node_first_child(
1554
+ leptris_element_as_node(root));
1555
+ n; n = leptris_node_next_sibling(n)) {
1556
+ if (leptris_node_get_type(n) != LEPTRIS_NODE_TYPE_ELEMENT)
1557
+ continue;
1558
+ if (xsd_upa_check_node(s, (LeptrisElement)n, tns, 0) != 0)
1559
+ return -1;
1560
+ }
1561
+ return 0;
1562
+ }
1563
+
1564
+ /* ---- #1691 slice 5: element/attribute/group ref resolution -------
1565
+ *
1566
+ * The oracle (libxml2 2.9.13) rejects a schema whose xs:element/@ref
1567
+ * (or attribute, group, attributeGroup) QName does not resolve to a
1568
+ * declaration (import1_0: the import's schemaLocation file is
1569
+ * missing, so {http://BAR}bar.B resolves to nothing). Resolution is
1570
+ * by DECLARATION, not by namespace: the available set records every
1571
+ * loaded document's root-level declarations keyed by that
1572
+ * document's effective target namespace (chameleon includes adopt
1573
+ * the includer's). A namespace-only import or a missing file
1574
+ * provides nothing. type=/base= QNames stay out of scope: the
1575
+ * oracle fails to resolve spec-valid xs: builtins (length3_0's
1576
+ * xs:non-positive-integer), so its type verdicts are not
1577
+ * byte-matchable without codifying that gap. */
1578
+
1579
+ struct xsd_decl {
1580
+ char uri[256];
1581
+ char name[128];
1582
+ };
1583
+
1584
+
1585
+
1586
+ static void xsd_decls_add(struct xsd_decl* decls, int* n, int max,
1587
+ const char* uri, const char* name) {
1588
+ if (!name || !name[0] || *n >= max) return;
1589
+ for (int i = 0; i < *n; i++) {
1590
+ if (strcmp(decls[i].name, name) == 0 &&
1591
+ strcmp(decls[i].uri, uri ? uri : "") == 0)
1592
+ return;
1593
+ }
1594
+ snprintf(decls[*n].uri, sizeof(decls[*n].uri), "%s", uri ? uri : "");
1595
+ snprintf(decls[*n].name, sizeof(decls[*n].name), "%s", name);
1596
+ (*n)++;
1597
+ }
1598
+
1599
+ static int xsd_decls_contain(struct xsd_decl* decls, int n,
1600
+ const char* uri, const char* name) {
1601
+ if (!name || !name[0]) return 0;
1602
+ for (int i = 0; i < n; i++) {
1603
+ if (strcmp(decls[i].name, name) == 0 &&
1604
+ strcmp(decls[i].uri, uri ? uri : "") == 0)
1605
+ return 1;
1606
+ }
1607
+ return 0;
1608
+ }
1609
+
1610
+ static void xsd_dirname_into(const char* path, char* out, size_t outsz) {
1611
+ const char* slash = strrchr(path, '/');
1612
+ const char* bslash = strrchr(path, '\\');
1613
+ if (bslash && (!slash || bslash > slash)) slash = bslash;
1614
+ if (!slash) {
1615
+ out[0] = 0;
1616
+ return;
1617
+ }
1618
+ size_t bl = (size_t)(slash - path);
1619
+ if (bl >= outsz) bl = outsz - 1;
1620
+ memcpy(out, path, bl);
1621
+ out[bl] = 0;
1622
+ }
1623
+
1624
+ /* claimed[] tracks the namespaces an import already served — the
1625
+ * oracle serves the FIRST import whose schemaLocation loads per
1626
+ * namespace and skips the rest ("already imported with the schema
1627
+ * located at ..."), so later docs' declarations must not
1628
+ * resolve. */
1629
+ static void xsd_collect_decls_doc(LeptrisElement root, const char* eff_tns,
1630
+ const char* base_dir,
1631
+ struct xsd_decl* decls, int* n, int max,
1632
+ char (*claimed)[256], int* nclaimed,
1633
+ int cmax, int depth) {
1634
+ if (!root || depth > 8 || *n >= max) return;
1635
+ for (LeptrisNodeRef k = leptris_node_first_child(
1636
+ leptris_element_as_node(root));
1637
+ k; k = leptris_node_next_sibling(k)) {
1638
+ if (leptris_node_get_type(k) != LEPTRIS_NODE_TYPE_ELEMENT)
1639
+ continue;
1640
+ const char* kl = NULL;
1641
+ const char* kp = NULL;
1642
+ const char* ku = NULL;
1643
+ leptris_element_expanded_name((LeptrisElement)k, &kl, &kp, &ku);
1644
+ if (!kl || !ku || strcmp(ku, XSD_NS) != 0) continue;
1645
+ if (strcmp(kl, "element") == 0 || strcmp(kl, "attribute") == 0 ||
1646
+ strcmp(kl, "group") == 0 ||
1647
+ strcmp(kl, "attributeGroup") == 0) {
1648
+ const char* nm = leptris_element_attribute(
1649
+ (LeptrisElement)k, "name");
1650
+ xsd_decls_add(decls, n, max, eff_tns, nm);
1651
+ }
1652
+ }
1653
+ /* cn, not n: the count pointer `n` is live across this loop —
1654
+ * shadowing it passed the loop node as the decls count and the
1655
+ * decls_add increment wrote node memory (#1717). */
1656
+ for (LeptrisNodeRef cn = leptris_node_first_child(
1657
+ leptris_element_as_node(root));
1658
+ cn; cn = leptris_node_next_sibling(cn)) {
1659
+ if (leptris_node_get_type(cn) != LEPTRIS_NODE_TYPE_ELEMENT)
1660
+ continue;
1661
+ LeptrisElement c = (LeptrisElement)cn;
1662
+ const char* l = NULL;
1663
+ const char* p = NULL;
1664
+ const char* u = NULL;
1665
+ leptris_element_expanded_name(c, &l, &p, &u);
1666
+ if (!l || !u || strcmp(u, XSD_NS) != 0)
1667
+ continue;
1668
+ int is_inc = (strcmp(l, "include") == 0 ||
1669
+ strcmp(l, "redefine") == 0);
1670
+ int is_imp = (strcmp(l, "import") == 0);
1671
+ if (!is_inc && !is_imp) continue;
1672
+ const char* loc = leptris_element_attribute(c, "schemaLocation");
1673
+ if (!loc || !base_dir) continue; /* provides nothing */
1674
+ /* The import-namespace claim: the first LOADABLE import
1675
+ * per namespace wins; later ones are skipped. */
1676
+ char imp_ns[256];
1677
+ imp_ns[0] = 0;
1678
+ if (is_imp) {
1679
+ const char* ns = leptris_element_attribute(c, "namespace");
1680
+ snprintf(imp_ns, sizeof(imp_ns), "%s", ns ? ns : "");
1681
+ int seen = 0;
1682
+ for (int ci = 0; ci < *nclaimed; ci++)
1683
+ if (strcmp(claimed[ci], imp_ns) == 0) seen = 1;
1684
+ if (seen) continue; /* skipped import: provides nothing */
1685
+ }
1686
+ char path[4096];
1687
+ snprintf(path, sizeof(path), "%s/%s", base_dir, loc);
1688
+ size_t flen = 0;
1689
+ char* text = xsd_read_file(path, &flen);
1690
+ if (!text) continue; /* missing file: provides nothing */
1691
+ LeptrisDocument sub = leptris_parse_string(text, flen, NULL);
1692
+ free(text);
1693
+ if (!sub) continue;
1694
+ LeptrisElement subroot = leptris_document_root(sub);
1695
+ if (is_imp && *nclaimed < cmax) {
1696
+ int seen = 0;
1697
+ for (int ci = 0; ci < *nclaimed; ci++)
1698
+ if (strcmp(claimed[ci], imp_ns) == 0) seen = 1;
1699
+ if (!seen)
1700
+ snprintf(claimed[(*nclaimed)++], 256, "%s", imp_ns);
1701
+ }
1702
+ if (subroot) {
1703
+ const char* sub_tns = leptris_element_attribute(
1704
+ subroot, "targetNamespace");
1705
+ const char* eff = is_inc ? (sub_tns ? sub_tns : eff_tns)
1706
+ : sub_tns;
1707
+ char sub_base[4096];
1708
+ xsd_dirname_into(path, sub_base, sizeof(sub_base));
1709
+ xsd_collect_decls_doc(subroot, eff, sub_base, decls, n, max,
1710
+ claimed, nclaimed, cmax, depth + 1);
1711
+ }
1712
+ leptris_document_free(sub);
1713
+ }
1714
+ }
1715
+
1716
+ static int xsd_check_refs_walk(LeptrisElement root,
1717
+ struct leptris_xsd_schema* s,
1718
+ struct xsd_decl* decls, int ndecls) {
1719
+ if (!root) return 0;
1720
+ for (LeptrisNodeRef n = leptris_node_first_child(
1721
+ leptris_element_as_node(root));
1722
+ n; n = leptris_node_next_sibling(n)) {
1723
+ if (leptris_node_get_type(n) != LEPTRIS_NODE_TYPE_ELEMENT)
1724
+ continue;
1725
+ LeptrisElement c = (LeptrisElement)n;
1726
+ const char* l = NULL;
1727
+ const char* p = NULL;
1728
+ const char* u = NULL;
1729
+ leptris_element_expanded_name(c, &l, &p, &u);
1730
+ if (!l || !u || strcmp(u, XSD_NS) != 0)
1731
+ continue;
1732
+ int is_ref_holder =
1733
+ (strcmp(l, "element") == 0 || strcmp(l, "attribute") == 0 ||
1734
+ strcmp(l, "group") == 0 || strcmp(l, "attributeGroup") == 0);
1735
+ if (!is_ref_holder) {
1736
+ if (xsd_check_refs_walk(c, s, decls, ndecls) != 0) return -1;
1737
+ continue;
1738
+ }
1739
+ const char* ref = leptris_element_attribute(c, "ref");
1740
+ if (!ref || !ref[0]) {
1741
+ if (xsd_check_refs_walk(c, s, decls, ndecls) != 0) return -1;
1742
+ continue;
1743
+ }
1744
+ char ruri[256], rlocal[128];
1745
+ xsd_resolve_qname(c, ref, ruri, sizeof(ruri), rlocal,
1746
+ sizeof(rlocal));
1747
+ if (!xsd_decls_contain(decls, ndecls, ruri, rlocal)) {
1748
+ char msg[512];
1749
+ snprintf(msg, sizeof(msg),
1750
+ "src-element.2: The QName value '{%s}%s' does not "
1751
+ "resolve to a(n) %s declaration",
1752
+ ruri, rlocal,
1753
+ strcmp(l, "attribute") == 0 ? "attribute"
1754
+ : "element");
1755
+ xsd_set_error(s, msg);
1756
+ return -1;
1757
+ }
1758
+ if (xsd_check_refs_walk(c, s, decls, ndecls) != 0) return -1;
1759
+ }
1760
+ return 0;
1761
+ }
1762
+
1763
+ static int xsd_check_refs(struct leptris_xsd_schema* s,
1764
+ LeptrisElement root) {
1765
+ if (!root) return 0;
1766
+ struct xsd_decl decls[512];
1767
+ int ndecls = 0;
1768
+ char claimed[32][256];
1769
+ int nclaimed = 0;
1770
+ xsd_collect_decls_doc(root, s->target_ns, s->base_dir, decls,
1771
+ &ndecls, 512, claimed, &nclaimed, 32, 0);
1772
+ return xsd_check_refs_walk(root, s, decls, ndecls);
1773
+ }
1774
+
864
1775
  static LeptrisXsdSchema xsd_compile_impl(const char* xsd_text,
865
1776
  size_t len,
866
1777
  const char* base_dir,
@@ -917,7 +1828,24 @@ static LeptrisXsdSchema xsd_compile_impl(const char* xsd_text,
917
1828
  * required/allowed attributes on the xs: vocabulary and
918
1829
  * occurrence ordering, checked at the document level exactly
919
1830
  * where the oracle raises them — before capture. */
920
- if (xsd_check_structure(s, root) != 0)
1831
+ if (xsd_check_structure(s, root, 0) != 0)
1832
+ goto fail;
1833
+
1834
+ /* Derivation constraints (#1691 slice 4): content-type match on
1835
+ * extension (cos-ct-derived-ok) and the attribute-wildcard
1836
+ * subset rules on restriction (derivation-ok-restriction-4-1). */
1837
+ if (xsd_check_derivation(s, root) != 0)
1838
+ goto fail;
1839
+
1840
+ /* Ref resolution (#1691 slice 5): element/attribute/group
1841
+ * ref QNames must resolve to a declaration in an available
1842
+ * document. */
1843
+ if (xsd_check_refs(s, root) != 0)
1844
+ goto fail;
1845
+
1846
+ /* UPA (#1691 slice 6): choice-branch FIRST overlap and the
1847
+ * optional/repeating-sequence adjacency rule. */
1848
+ if (xsd_check_upa(s, root) != 0)
921
1849
  goto fail;
922
1850
 
923
1851
  /* Schema Component Constraint: xsi: Not Allowed — the schema's
@@ -1015,6 +1943,10 @@ static void xsd_resolve_extension_bases(struct leptris_xsd_schema* s) {
1015
1943
  tail_a->next = mine->attrs;
1016
1944
  mine->attrs = prepend;
1017
1945
  }
1946
+ /* the base's attribute wildcard joins the union */
1947
+ if (bt->any_attr_ns && !mine->any_attr_base_ns)
1948
+ mine->any_attr_base_ns =
1949
+ xsd_strdup(bt->any_attr_ns);
1018
1950
  } else if (prepend) {
1019
1951
  XsdTypeAttrs* row =
1020
1952
  (XsdTypeAttrs*)calloc(1, sizeof(*row));
@@ -1398,6 +2330,7 @@ LEPTRIS_API void leptris_xsd_free(LeptrisXsdSchema schema) {
1398
2330
  }
1399
2331
  free(ta->type_name);
1400
2332
  free(ta->any_attr_ns);
2333
+ free(ta->any_attr_base_ns);
1401
2334
  free(ta);
1402
2335
  ta = tn;
1403
2336
  }
@@ -1499,8 +2432,8 @@ static XsdCm* xsd_cm_copy_list(const XsdCm* head) {
1499
2432
 
1500
2433
  static void xsd_register_complex(struct leptris_xsd_schema* s,
1501
2434
  XsdCm* root, XsdAttrDecl* attrs,
1502
- const char* name,
1503
- char* any_attr_ns) {
2435
+ const char* name, char* any_attr_ns,
2436
+ char* base_any_attr_ns) {
1504
2437
  if (!root) return;
1505
2438
  root->name = xsd_strdup(name);
1506
2439
  /* leptris-ruby#440: the DECLARING file's targetNamespace — the
@@ -1510,18 +2443,20 @@ static void xsd_register_complex(struct leptris_xsd_schema* s,
1510
2443
  if (s->target_ns) root->ns = xsd_strdup(s->target_ns);
1511
2444
  root->next = s->complex_types;
1512
2445
  s->complex_types = root;
1513
- if (attrs || any_attr_ns) {
2446
+ if (attrs || any_attr_ns || base_any_attr_ns) {
1514
2447
  XsdTypeAttrs* ta = (XsdTypeAttrs*)calloc(1, sizeof(*ta));
1515
2448
  if (ta) {
1516
2449
  ta->type_name = xsd_strdup(name);
1517
2450
  if (root->ns) ta->ns = xsd_strdup(root->ns);
1518
2451
  ta->attrs = attrs;
1519
2452
  ta->any_attr_ns = any_attr_ns;
2453
+ ta->any_attr_base_ns = base_any_attr_ns;
1520
2454
  ta->next = s->type_attrs;
1521
2455
  s->type_attrs = ta;
1522
2456
  }
1523
- } else if (any_attr_ns) {
2457
+ } else {
1524
2458
  free(any_attr_ns);
2459
+ free(base_any_attr_ns);
1525
2460
  }
1526
2461
  }
1527
2462
 
@@ -1620,6 +2555,7 @@ static XsdCm* xsd_capture_complex_model(struct leptris_xsd_schema* s,
1620
2555
  if (!root) return NULL;
1621
2556
  XsdAttrDecl* attrs = NULL;
1622
2557
  char* any_attr_ns = NULL;
2558
+ char* base_any_attr_ns = NULL;
1623
2559
 
1624
2560
  for (LeptrisNodeRef n =
1625
2561
  leptris_node_first_child(leptris_element_as_node(ct));
@@ -1688,6 +2624,12 @@ static XsdCm* xsd_capture_complex_model(struct leptris_xsd_schema* s,
1688
2624
  s, base);
1689
2625
  attrs = xsd_copy_attr_rows(bt ? bt->attrs
1690
2626
  : NULL);
2627
+ /* the spec's attribute-wildcard union: the
2628
+ * derived type also allows what the base's
2629
+ * xs:anyAttribute allowed (#1691) */
2630
+ if (bt && bt->any_attr_ns && !base_any_attr_ns)
2631
+ base_any_attr_ns =
2632
+ xsd_strdup(bt->any_attr_ns);
1691
2633
  }
1692
2634
  XsdAttrDecl* own_attrs =
1693
2635
  xsd_capture_attr_rows(s, dq, 0);
@@ -1845,7 +2787,8 @@ static XsdCm* xsd_capture_complex_model(struct leptris_xsd_schema* s,
1845
2787
  xsd_cm_free(root->open_any);
1846
2788
  root->open_any = NULL;
1847
2789
  }
1848
- xsd_register_complex(s, root, attrs, name, any_attr_ns);
2790
+ xsd_register_complex(s, root, attrs, name, any_attr_ns,
2791
+ base_any_attr_ns);
1849
2792
  return root;
1850
2793
  }
1851
2794