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
@@ -259,8 +259,21 @@ static cli_result_t validate_execute(int argc, char** argv) {
259
259
  printf("rng: valid\n");
260
260
  } else {
261
261
  printf("rng: invalid\n");
262
- const char* e = leptris_rng_error(rng);
263
- if (e && e[0]) printf(" %s\n", e);
262
+ /* the full report: position AND attribute diagnostics ride
263
+ * separate records (#1704 — Jing reports both) */
264
+ const LeptrisRngErrorRecord* rep = NULL;
265
+ size_t n = leptris_rng_error_report(rng, &rep);
266
+ for (size_t i = 0; i < n; i++) {
267
+ if (rep[i].line > 0)
268
+ printf(" %u:%u: error: %s\n", rep[i].line,
269
+ rep[i].column, rep[i].message);
270
+ else
271
+ printf(" error: %s\n", rep[i].message);
272
+ }
273
+ if (n == 0) {
274
+ const char* e = leptris_rng_error(rng);
275
+ if (e && e[0]) printf(" %s\n", e);
276
+ }
264
277
  invalid = 1;
265
278
  }
266
279
  leptris_rng_free(rng);
@@ -3074,7 +3074,10 @@ LEPTRIS_API const char* leptris_status_string(LeptrisStatus status);
3074
3074
  * Evaluate XPath expression
3075
3075
  *
3076
3076
  * @param doc Document (required)
3077
- * @param context Context element (NULL = document root)
3077
+ * @param context Context element (NULL = the document node —
3078
+ * relative paths then resolve against the document's
3079
+ * children, i.e. the root element matches by name, matching
3080
+ * libxml2/Nokogiri document-level query semantics)
3078
3081
  * @param expression XPath expression string
3079
3082
  * @return XPath result or NULL on error
3080
3083
  *
@@ -3133,7 +3136,7 @@ LEPTRIS_API LeptrisXPathResult leptris_xpath_eval(
3133
3136
  * leptris_xpath_eval).
3134
3137
  *
3135
3138
  * @param doc Document to evaluate against
3136
- * @param context Context element (NULL = document root)
3139
+ * @param context Context element (NULL = the document node)
3137
3140
  * @param expression Expression text
3138
3141
  * @param version LEPTRIS_XPATH_10 or LEPTRIS_XPATH_31
3139
3142
  * @param status Optional status out-param
@@ -3689,7 +3692,7 @@ LEPTRIS_API LeptrisXPathResult leptris_xpath_eval_with_vars(
3689
3692
  * root (e.g. relative paths like ".//item[@id = $x]").
3690
3693
  *
3691
3694
  * @param doc Document
3692
- * @param context Context element (NULL = document root)
3695
+ * @param context Context element (NULL = the document node)
3693
3696
  * @param expression XPath expression
3694
3697
  * @param variables Variable set (may be NULL)
3695
3698
  * @return XPath result (caller frees with leptris_xpath_result_free)
@@ -3771,7 +3774,7 @@ LEPTRIS_API LeptrisXPathNsSet leptris_xpath_ns_set_new_from_pairs(
3771
3774
  * declared it with).
3772
3775
  *
3773
3776
  * @param doc Document
3774
- * @param context Context element (NULL = document root)
3777
+ * @param context Context element (NULL = the document node)
3775
3778
  * @param expression XPath expression
3776
3779
  * @param ns Namespace bindings (may be NULL; then behavior equals
3777
3780
  * leptris_xpath_eval_with_vars_context)
@@ -3946,6 +3949,59 @@ LEPTRIS_API LeptrisStatus leptris_document_set_allocators(
3946
3949
  */
3947
3950
  LEPTRIS_API LeptrisStatus leptris_xinclude_process(LeptrisDocument doc, const char* base_url);
3948
3951
 
3952
+ /**
3953
+ * Resource fetch hook for xi:include (TODO.assemble 04).
3954
+ *
3955
+ * Return 1 and fill out_content/out_len to serve the resource yourself;
3956
+ * the engine takes ownership of the buffer and releases it with
3957
+ * free() — allocate it with malloc() (or an allocator compatible
3958
+ * with it).
3959
+ * Return 0 to fall through to the default file loader. Return -1 to
3960
+ * force a resource error (the xi:fallback applies).
3961
+ *
3962
+ * The uri is the resolved include target (no fragment). It is never a
3963
+ * network scheme while no_network is set (the default).
3964
+ */
3965
+ typedef int (*leptris_xinclude_resolver_fn)(const char* uri, void* user,
3966
+ char** out_content,
3967
+ size_t* out_len);
3968
+
3969
+ /**
3970
+ * Per-call options for XInclude processing (TODO.assemble 03/04).
3971
+ * Zero-initialize for the defaults: no resolver, no catalog,
3972
+ * no_network = 1 (remote schemes refuse with a resource error).
3973
+ *
3974
+ * Memory: the strings and the resolver are borrowed for the duration
3975
+ * of the call; nothing here is freed by the engine.
3976
+ */
3977
+ typedef struct leptris_xinclude_opts {
3978
+ leptris_xinclude_resolver_fn resolver; /**< NULL = file loader only */
3979
+ void* resolver_user; /**< passed to the resolver */
3980
+ const char* catalog_path; /**< XML catalog file or NULL */
3981
+ int no_network; /**< default 1 (SSRF posture) */
3982
+ } leptris_xinclude_opts;
3983
+
3984
+ /**
3985
+ * Process XInclude with explicit resolver policy.
3986
+ *
3987
+ * Extends leptris_xinclude_process with:
3988
+ * - xml:base inheritance: relative hrefs resolve against the
3989
+ * nearest xml:base ancestor (RFC 3986 merge), and nested
3990
+ * includes inside an included document resolve against that
3991
+ * document's own location;
3992
+ * - an optional resolver hook (see leptris_xinclude_resolver_fn);
3993
+ * - an optional XML Catalog file (<uri name= uri=> exact mappings);
3994
+ * catalog lookups rewrite the URI before the resolver/loader runs;
3995
+ * - the network policy: with no_network (the default), hrefs whose
3996
+ * resolved URI carries http/https/ftp(s)/file-remote schemes are
3997
+ * resource errors (xi:fallback applies).
3998
+ *
3999
+ * Memory: opts is borrowed for the call; pass NULL for the defaults.
4000
+ */
4001
+ LEPTRIS_API LeptrisStatus leptris_xinclude_process_opts(
4002
+ LeptrisDocument doc, const char* base_url,
4003
+ const leptris_xinclude_opts* opts);
4004
+
3949
4005
  /**
3950
4006
  * Check if element is an XInclude include element
3951
4007
  *
@@ -107,170 +107,292 @@ static int cm_peek_token(CMLexer* lx, CMTokenType* type, char name_buf[64]) {
107
107
  return 0;
108
108
  }
109
109
 
110
- /* Forward decls for the recursive matcher. */
111
- static int match_seq(CMLexer* lx, const char** child_names, size_t* child_idx,
112
- size_t child_count, const char* model_name,
113
- char* out_msg, size_t msg_size);
110
+ /* ---- the matcher (TODO.hygiene/02 closeout) --------------------------
111
+ *
112
+ * A backtracking recursive-descent evaluator over the model with the
113
+ * child list as input stream. State = (lexer position, child index);
114
+ * every trial saves/restores both, so choices and repetitions reject
115
+ * a branch without consuming children:
116
+ *
117
+ * expr := sequence ('|' sequence)* — first match wins
118
+ * sequence := term (',' term)*
119
+ * term := ( NAME | '(' expr ')' ) occurrence?
120
+ * occurrence := '' | '?' | '*' | '+'
121
+ *
122
+ * Mixed models (`(#PCDATA | a | b)*`) are validated separately: the
123
+ * alternation names form a SET, every element child must belong to
124
+ * it, text is always allowed; `(#PCDATA)` without `*` allows no
125
+ * element children at all (XML 1.0 §3.2.2, the mixed grammar). */
126
+
127
+ typedef struct {
128
+ const char** names; /* element children only */
129
+ size_t count;
130
+ size_t idx;
131
+ CMLexer lx;
132
+ const char* model_name;
133
+ char* out_msg;
134
+ size_t msg_size;
135
+ } CMState;
136
+
137
+ static int cm_match_expr(CMState* st);
138
+
139
+ /* Match ONE instance of the group whose '(' sits at group_start.
140
+ * On success the lexer lands just past the group's ')'; on failure
141
+ * the state is restored. */
142
+ static int cm_try_group_once(CMState* st, const char* group_start) {
143
+ CMLexer save_lx = st->lx;
144
+ size_t save_idx = st->idx;
145
+
146
+ st->lx.src = group_start + 1; /* past '(' */
147
+ if (!cm_match_expr(st)) goto fail;
148
+ cm_skip_ws(&st->lx);
149
+ if (st->lx.src >= st->lx.end || *st->lx.src != ')') {
150
+ if (st->out_msg && st->msg_size)
151
+ snprintf(st->out_msg, st->msg_size,
152
+ "Content model of '%s' expected ')'",
153
+ st->model_name);
154
+ goto fail;
155
+ }
156
+ st->lx.src++;
157
+ return 1;
158
+ fail:
159
+ st->lx = save_lx;
160
+ st->idx = save_idx;
161
+ return 0;
162
+ }
163
+
164
+ /* term := ( NAME | '(' expr ')' ) occurrence? */
165
+ static int cm_match_term(CMState* st) {
166
+ cm_skip_ws(&st->lx);
167
+ int c = cm_peek(&st->lx);
168
+ if (c < 0) return 0;
169
+
170
+ if (c == '(') {
171
+ const char* group_start = st->lx.src;
172
+ /* occurrence needs the token AFTER ')': peek the balanced
173
+ * close by trying once first — the occurrence layer below
174
+ * re-enters via cm_try_group_once, so this match consumes
175
+ * exactly one instance. */
176
+ if (!cm_try_group_once(st, group_start)) return 0;
177
+ cm_skip_ws(&st->lx);
178
+ char occurrence = 1;
179
+ if (st->lx.src < st->lx.end) {
180
+ if (*st->lx.src == '?') { st->lx.src++; occurrence = '?'; }
181
+ else if (*st->lx.src == '*') { st->lx.src++; occurrence = '*'; }
182
+ else if (*st->lx.src == '+') { st->lx.src++; occurrence = '+'; }
183
+ }
184
+ if (occurrence == 1 || occurrence == '?') return 1;
185
+ /* '*' / '+': trial further instances while they match and
186
+ * make progress (a zero-width body terminates the loop) */
187
+ for (;;) {
188
+ CMLexer save_lx = st->lx;
189
+ size_t save_idx = st->idx;
190
+ if (!cm_try_group_once(st, group_start)) {
191
+ st->lx = save_lx; /* the trailing occurrence token? no —
192
+ try_group_once restores to before
193
+ the retry; keep the successful
194
+ state from before this trial */
195
+ st->lx = save_lx;
196
+ st->idx = save_idx;
197
+ return 1;
198
+ }
199
+ if (st->idx == save_idx) {
200
+ /* zero-width instance: stop, state stays at the last
201
+ * successful position (already equal) */
202
+ return 1;
203
+ }
204
+ }
205
+ }
206
+
207
+ if (c == '#') {
208
+ /* #PCDATA handled by the mixed path; a bare one here is a
209
+ * malformed model for element-only matching */
210
+ if (st->out_msg && st->msg_size)
211
+ snprintf(st->out_msg, st->msg_size,
212
+ "Content model of '%s' mixes #PCDATA with element "
213
+ "content", st->model_name);
214
+ return 0;
215
+ }
114
216
 
115
- /* Match a single particle (NAME?occurrence) against one or more
116
- * children, advancing *child_idx as needed. Returns 1 on success,
117
- * 0 on mismatch. */
118
- static int match_particle(CMLexer* lx, const char** child_names,
119
- size_t* child_idx, size_t child_count,
120
- int allow_empty, const char* model_name,
121
- char* out_msg, size_t msg_size) {
122
- CMTokenType tok;
123
217
  char name[64];
124
- if (!cm_peek_token(lx, &tok, name) || tok != CMT_NAME) {
125
- snprintf(out_msg, msg_size,
126
- "Content model of '%s' expected a name", model_name);
218
+ CMTokenType tok;
219
+ if (!cm_peek_token(&st->lx, &tok, name) || tok != CMT_NAME) {
220
+ if (st->out_msg && st->msg_size)
221
+ snprintf(st->out_msg, st->msg_size,
222
+ "Content model of '%s' expected a name",
223
+ st->model_name);
127
224
  return 0;
128
225
  }
129
- cm_skip_ws(lx);
130
- /* Consume the name token by advancing past it. */
131
- while (lx->src < lx->end && !isspace((unsigned char)*lx->src) &&
132
- *lx->src != '(' && *lx->src != ')' && *lx->src != ',' &&
133
- *lx->src != '|' && *lx->src != '?' && *lx->src != '*' &&
134
- *lx->src != '+') {
135
- lx->src++;
226
+ while (st->lx.src < st->lx.end &&
227
+ !isspace((unsigned char)*st->lx.src) &&
228
+ *st->lx.src != '(' && *st->lx.src != ')' &&
229
+ *st->lx.src != ',' && *st->lx.src != '|' &&
230
+ *st->lx.src != '?' && *st->lx.src != '*' &&
231
+ *st->lx.src != '+') {
232
+ st->lx.src++;
136
233
  }
137
- /* Optional occurrence modifier. */
138
- cm_skip_ws(lx);
139
- int occurrence = 1; /* default: exactly one */
140
- if (lx->src < lx->end) {
141
- if (*lx->src == '?') { lx->src++; occurrence = '?'; }
142
- else if (*lx->src == '*') { lx->src++; occurrence = '*'; }
143
- else if (*lx->src == '+') { lx->src++; occurrence = '+'; }
234
+ cm_skip_ws(&st->lx);
235
+ char occurrence = 1;
236
+ if (st->lx.src < st->lx.end) {
237
+ if (*st->lx.src == '?') { st->lx.src++; occurrence = '?'; }
238
+ else if (*st->lx.src == '*') { st->lx.src++; occurrence = '*'; }
239
+ else if (*st->lx.src == '+') { st->lx.src++; occurrence = '+'; }
144
240
  }
145
241
 
146
- int matched_any = 0;
147
- /* * and + consume EVERY run of matching children — a single
148
- * compare left the run's tail unconsumed and failed valid
149
- * documents like <r><item/><item/></r> against (item+). */
150
- while (*child_idx < child_count &&
151
- strcmp(child_names[*child_idx], name) == 0) {
152
- (*child_idx)++;
153
- matched_any = 1;
242
+ size_t matched = 0;
243
+ while (st->idx < st->count &&
244
+ strcmp(st->names[st->idx], name) == 0) {
245
+ st->idx++;
246
+ matched++;
154
247
  if (occurrence == 1 || occurrence == '?') break;
155
248
  }
156
-
157
249
  switch (occurrence) {
158
250
  case '?':
159
251
  case '*':
160
- return 1; /* optional / zero-or-more — match even if missing */
252
+ return 1;
161
253
  case '+':
162
- if (matched_any) return 1;
163
- snprintf(out_msg, msg_size,
164
- "Element '%s' content model requires one or more '%s'",
165
- model_name, name);
254
+ if (matched) return 1;
255
+ if (st->out_msg && st->msg_size)
256
+ snprintf(st->out_msg, st->msg_size,
257
+ "Element '%s' content model requires one or "
258
+ "more '%s'", st->model_name, name);
166
259
  return 0;
167
- default: /* exactly one */
168
- if (matched_any) return 1;
169
- if (allow_empty && *child_idx >= child_count) {
170
- /* The next child would have been missing, but the
171
- * caller is OK with empty if optional follows. */
172
- return 1;
173
- }
174
- snprintf(out_msg, msg_size,
175
- "Element '%s' content model requires '%s'",
176
- model_name, name);
260
+ default:
261
+ if (matched) return 1;
262
+ if (st->out_msg && st->msg_size)
263
+ snprintf(st->out_msg, st->msg_size,
264
+ "Element '%s' content model requires '%s'",
265
+ st->model_name, name);
177
266
  return 0;
178
267
  }
179
268
  }
180
269
 
181
- /* Match a parenthesized group: ( seq | choice )?occurrence. */
182
- static int match_group(CMLexer* lx, const char** child_names,
183
- size_t* child_idx, size_t child_count,
184
- int allow_empty, const char* model_name,
185
- char* out_msg, size_t msg_size) {
186
- /* Consume '(' */
187
- if (lx->src >= lx->end || *lx->src != '(') {
188
- snprintf(out_msg, msg_size,
189
- "Content model of '%s' expected '('", model_name);
190
- return 0;
270
+ /* sequence := term (',' term)* */
271
+ static int cm_match_sequence(CMState* st) {
272
+ if (!cm_match_term(st)) return 0;
273
+ for (;;) {
274
+ cm_skip_ws(&st->lx);
275
+ if (st->lx.src >= st->lx.end || *st->lx.src != ',') return 1;
276
+ st->lx.src++;
277
+ if (!cm_match_term(st)) return 0;
191
278
  }
192
- lx->src++;
193
- cm_skip_ws(lx);
279
+ }
194
280
 
195
- /* Parse sequence/choice. We support both via recursive descent
196
- * over particles separated by `,` or `|`. */
197
- /* First term */
198
- if (!match_particle(lx, child_names, child_idx, child_count,
199
- allow_empty, model_name, out_msg, msg_size)) {
200
- return 0;
281
+ /* Lexically walk past one unmatched alternative (balanced parens,
282
+ * stopping at the depth-0 '|' or ')'). */
283
+ static void cm_skip_alternative(CMLexer* lx) {
284
+ int depth = 0;
285
+ while (lx->src < lx->end) {
286
+ char c = *lx->src;
287
+ if (c == '(') depth++;
288
+ else if (c == ')') {
289
+ if (depth == 0) return;
290
+ depth--;
291
+ } else if (c == '|' && depth == 0) return;
292
+ lx->src++;
293
+ }
294
+ }
295
+
296
+ /* expr := sequence ('|' sequence)* — first match wins; failed
297
+ * alternatives restore the state, and after a SUCCESS the remaining
298
+ * alternatives are skipped lexically so the enclosing group's ')'
299
+ * check lands on the real close. */
300
+ static int cm_match_expr(CMState* st) {
301
+ CMLexer save_lx = st->lx;
302
+ size_t save_idx = st->idx;
303
+ if (cm_match_sequence(st)) {
304
+ for (;;) {
305
+ cm_skip_ws(&st->lx);
306
+ if (st->lx.src >= st->lx.end || *st->lx.src != '|') break;
307
+ st->lx.src++;
308
+ cm_skip_alternative(&st->lx);
309
+ }
310
+ return 1;
201
311
  }
202
- /* Following terms (sequence or choice) */
203
312
  for (;;) {
204
- cm_skip_ws(lx);
205
- if (lx->src >= lx->end) break;
206
- if (*lx->src == ')') break;
207
- if (*lx->src == ',') {
208
- lx->src++;
209
- cm_skip_ws(lx);
210
- if (!match_particle(lx, child_names, child_idx, child_count,
211
- 0, model_name, out_msg, msg_size)) {
212
- return 0;
213
- }
214
- } else if (*lx->src == '|') {
215
- /* Choice: try to match the current alternative; if it
216
- * doesn't match the current child, backtrack to before
217
- * THIS particle and try the alternative. For chained `|`,
218
- * the matcher already accepted one of the earlier alts. */
219
- lx->src++;
220
- cm_skip_ws(lx);
221
- /* Save BEFORE the current particle (we already consumed
222
- * it, so we need to track from where we entered this
223
- * iteration). Since match_particle always advances on
224
- * success even for `?`/`*`, we instead check: if the
225
- * child at the current position did NOT match the
226
- * previous particle, roll back by re-running the alt. */
227
- CMLexer save = *lx;
228
- size_t save_idx = *child_idx;
229
- int alt_ok = match_particle(lx, child_names, child_idx,
230
- child_count, allow_empty,
231
- model_name, out_msg, msg_size);
232
- if (!alt_ok) {
233
- /* Alternative didn't match — try skipping it (treat
234
- * as optional). This happens when the previous
235
- * alternative matched. */
236
- *lx = save;
237
- *child_idx = save_idx;
238
- while (lx->src < lx->end && *lx->src != ')') lx->src++;
313
+ cm_skip_ws(&st->lx);
314
+ if (st->lx.src >= st->lx.end || *st->lx.src != '|') {
315
+ st->lx = save_lx;
316
+ st->idx = save_idx;
317
+ return 0;
318
+ }
319
+ st->lx.src++;
320
+ if (cm_match_sequence(st)) {
321
+ for (;;) {
322
+ cm_skip_ws(&st->lx);
323
+ if (st->lx.src >= st->lx.end || *st->lx.src != '|') break;
324
+ st->lx.src++;
325
+ cm_skip_alternative(&st->lx);
239
326
  }
240
- } else {
241
- break;
327
+ return 1;
242
328
  }
329
+ /* this alternative failed — restore and try the next one */
243
330
  }
244
- /* Consume ')' */
245
- if (lx->src >= lx->end || *lx->src != ')') {
246
- snprintf(out_msg, msg_size,
247
- "Content model of '%s' expected ')'", model_name);
331
+ }
332
+
333
+ /* Mixed model: `(#PCDATA)` or `(#PCDATA|n1|n2...)*`. */
334
+ static int cm_match_mixed(const char* model, CMState* st) {
335
+ /* collect the allowed element names between '(' and ')' */
336
+ struct { const char* s; size_t len; } names[64];
337
+ size_t n = 0;
338
+ const char* p = strchr(model, '(');
339
+ const char* close = strrchr(model, ')');
340
+ if (!p || !close || close < p) {
341
+ if (st->out_msg && st->msg_size)
342
+ snprintf(st->out_msg, st->msg_size,
343
+ "Content model of '%s' is malformed",
344
+ st->model_name);
248
345
  return 0;
249
346
  }
250
- lx->src++;
251
- /* Optional outer-group occurrence modifier. */
252
- cm_skip_ws(lx);
253
- if (lx->src < lx->end) {
254
- if (*lx->src == '?') { lx->src++; return 1; } /* outer (a|b)? — allowed */
255
- if (*lx->src == '*') { lx->src++; return 1; } /* outer (a|b)* */
256
- if (*lx->src == '+') { lx->src++; return 1; } /* outer (a|b)+ — at least one alt */
347
+ int star = 0;
348
+ {
349
+ const char* q = close + 1;
350
+ while (*q && isspace((unsigned char)*q)) q++;
351
+ star = (*q == '*');
257
352
  }
258
- return 1;
259
- }
260
-
261
- /* Top-level: parse the model and match against children. */
262
- static int match_seq(CMLexer* lx, const char** child_names, size_t* child_idx,
263
- size_t child_count, const char* model_name,
264
- char* out_msg, size_t msg_size) {
265
- /* If model starts with '(', parse a group. */
266
- cm_skip_ws(lx);
267
- if (lx->src < lx->end && *lx->src == '(') {
268
- return match_group(lx, child_names, child_idx, child_count,
269
- 0, model_name, out_msg, msg_size);
353
+ p++;
354
+ while (p < close) {
355
+ while (p < close && (isspace((unsigned char)*p) || *p == '|')) p++;
356
+ if (p >= close) break;
357
+ const char* s = p;
358
+ while (p < close && *p != '|' && !isspace((unsigned char)*p)) p++;
359
+ size_t len = (size_t)(p - s);
360
+ if (len == 7 && strncmp(s, "#PCDATA", 7) == 0) continue;
361
+ if (n < 64) {
362
+ /* borrowed (ptr,len): model text is not NUL-terminated
363
+ * at the name boundary */
364
+ names[n].s = s;
365
+ names[n].len = len;
366
+ n++;
367
+ }
368
+ }
369
+ for (size_t i = 0; i < st->count; i++) {
370
+ int allowed = 0;
371
+ for (size_t j = 0; j < n; j++) {
372
+ if (strlen(st->names[i]) == names[j].len &&
373
+ strncmp(st->names[i], names[j].s, names[j].len) == 0) {
374
+ allowed = 1;
375
+ break;
376
+ }
377
+ }
378
+ if (!allowed) {
379
+ if (st->out_msg && st->msg_size)
380
+ snprintf(st->out_msg, st->msg_size,
381
+ "Element '%s' child '%s' is not allowed by the "
382
+ "mixed content model", st->model_name,
383
+ st->names[i]);
384
+ return 0;
385
+ }
270
386
  }
271
- /* Single particle (NAME?occurrence). */
272
- return match_particle(lx, child_names, child_idx, child_count,
273
- 0, model_name, out_msg, msg_size);
387
+ if (!star && st->count > 0) {
388
+ if (st->out_msg && st->msg_size)
389
+ snprintf(st->out_msg, st->msg_size,
390
+ "Element '%s' mixed content model allows no element "
391
+ "children", st->model_name);
392
+ return 0;
393
+ }
394
+ st->idx = st->count;
395
+ return 1;
274
396
  }
275
397
 
276
398
  /* Public entry point: match actual children against a content model
@@ -282,17 +404,31 @@ int leptris_content_model_match(const char* model, const char* elem_name,
282
404
  if (out_msg && msg_size) snprintf(out_msg, msg_size, "NULL model or children");
283
405
  return 0;
284
406
  }
285
- CMLexer lx = { .src = model, .end = model + strlen(model) };
286
- size_t child_idx = 0;
287
- int rc = match_seq(&lx, child_names, &child_idx, child_count,
288
- elem_name, out_msg, msg_size);
289
- if (rc != 1) return 0;
290
- /* All children must be consumed. */
291
- if (child_idx != child_count) {
407
+ CMState st = {0};
408
+ st.names = child_names;
409
+ st.count = child_count;
410
+ st.idx = 0;
411
+ st.lx = (CMLexer){ .src = model, .end = model + strlen(model) };
412
+ st.model_name = elem_name;
413
+ st.out_msg = out_msg;
414
+ st.msg_size = msg_size;
415
+
416
+ if (strstr(model, "#PCDATA")) return cm_match_mixed(model, &st);
417
+
418
+ if (!cm_match_expr(&st)) {
419
+ if (out_msg && msg_size && out_msg[0] == 0) {
420
+ snprintf(out_msg, msg_size,
421
+ "Element '%s' does not match its content model",
422
+ elem_name);
423
+ }
424
+ return 0;
425
+ }
426
+ if (st.idx != st.count) {
292
427
  if (out_msg && msg_size) {
293
428
  snprintf(out_msg, msg_size,
294
- "Element '%s' has %zu child(ren) but content model accepts %zu",
295
- elem_name, child_count, child_idx);
429
+ "Element '%s' has %zu child(ren) but the content "
430
+ "model accepts %zu",
431
+ elem_name, child_count, st.idx);
296
432
  }
297
433
  return 0;
298
434
  }
@@ -251,7 +251,6 @@ static DTDElementDecl* dtd_parse_element(DTDParser* p) {
251
251
  depth--;
252
252
  dtd_advance(p);
253
253
  if (depth == 0) { closed = 1; break; }
254
- dtd_advance(p);
255
254
  continue;
256
255
  }
257
256
  dtd_advance(p);
@@ -260,6 +259,15 @@ static DTDElementDecl* dtd_parse_element(DTDParser* p) {
260
259
  * malformed declaration — the NULL-on-error contract. */
261
260
  if (!closed) return NULL;
262
261
 
262
+ /* The occurrence modifier rides the group: `(a|b)*` stored
263
+ * WITHOUT the * made every mixed/repeated model validate as
264
+ * required-once (#1709-follow-up probe). */
265
+ if (!dtd_at_end(p) &&
266
+ (dtd_peek(p) == '*' || dtd_peek(p) == '+' ||
267
+ dtd_peek(p) == '?')) {
268
+ dtd_advance(p);
269
+ }
270
+
263
271
  size_t len = p->pos - start;
264
272
  elem->content_model = (char*)leptris_pool_alloc(p->pool, len + 1);
265
273
  if (elem->content_model) {
@@ -583,9 +591,14 @@ static void dtd_parse_into(LeptrisDTD* dtd, const char* dtd_content,
583
591
  * content models, ATTLIST lists — legal in external subsets,
584
592
  * accepted leniently everywhere). ENTITY declarations are
585
593
  * excluded: their VALUES may legitimately contain '%'. */
586
- if (dtd_match(&parser, "<!ELEMENT") ||
587
- dtd_match(&parser, "<!ATTLIST") ||
588
- dtd_match(&parser, "<!NOTATION")) {
594
+ /* PEEK (do not consume): the substitution path re-parses the
595
+ * FULL declaration text — capturing decl_start after dtd_match
596
+ * would cut off the keyword and the reparse would register a
597
+ * garbage model (mixed models via %pe; validated as if
598
+ * #PCDATA were required). */
599
+ if (strncmp(parser.pos, "<!ELEMENT", 9) == 0 ||
600
+ strncmp(parser.pos, "<!ATTLIST", 9) == 0 ||
601
+ strncmp(parser.pos, "<!NOTATION", 10) == 0) {
589
602
  const char* decl_start = parser.pos;
590
603
  const char* scan = parser.pos;
591
604
  while (scan < parser.end && *scan != '>') scan++;