include/frx/frx_entity.h

filter: Statementstmt 95.6% 222 / 232 Branchbran 75.9% 237 / 312 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 84.3% 459 / 544 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/frx/frx_plugin.h ↑ Summary xs/internal.xs »
1 #ifndef FRX_ENTITY_H
2 #define FRX_ENTITY_H
3  
4 /* Entity expansion: the frame stack, the constraints of section 4.4, and
5 * the fetch of everything external through the caller's resolver.
6 *
7 * A reference to an internal entity pushes a frame (frx_lex.h explains
8 * why it is a frame and not a substitution) and the lexer reads the
9 * replacement text as if it stood where the reference was; the end of
10 * that text pops the frame and reading continues after the reference. A
11 * reference to an external entity fetches its bytes first: the resolver
12 * is handed the public identifier, the system identifier resolved
13 * against the base of the entity that declared it (section 4.2.2, RFC
14 * 3986 section 5.2) and that base, the bytes come back transcoded by
15 * frx_enc.h from their own byte order mark and text declaration (section
16 * 4.3.1, stripped here; the encoding it names must be the one detected,
17 * 4.3.3), and the text is kept in the arena for the parse. A fetch is
18 * done once per resolved identifier per parse and counted once against
19 * max_fetches; the same DTD named from three places is fetched once. The
20 * frame a fetched text makes is flat: its bytes are not in the document,
21 * so every offset inside it reports the reference. The constraints:
22 *
23 * WFC: Entity Declared an undeclared name is refused
24 * WFC: No Recursion an entity on the stack is refused
25 * WFC: Parsed Entity an unparsed entity is refused; an element
26 * opened inside a frame must close inside
27 * it, checked at the pop (frx_lex.h checks
28 * the end tag that would close an outer one)
29 * WFC: No External Entity References in an attribute value
30 * WFC: No < in Attribute Values frx_lex_attr_value, on the frame
31 * section 4.3.4 a 1.1 entity in a 1.0 document is refused
32 *
33 * and with no resolver every external reference is refused with the
34 * option named. Under standalone="yes" WFC: Entity Declared asks more: a
35 * reference in the document (not one inside the DTD's own text) must name
36 * an entity declared directly in the internal subset, not in the external
37 * subset nor inside any parameter entity, and that is a well-formedness
38 * refusal whatever validate says. The rest of section 2.9's standalone
39 * rules (a default or a type from external markup the document relies
40 * on) are validity constraints, applied under validate with the VC named.
41 *
42 * Three budgets, all counted per parse across every frame and each
43 * refused with the offset of the reference that crossed it: the depth of
44 * the stack, the total bytes pushed (fetched text included), and the
45 * ratio of bytes pushed to input bytes; and a fourth on fetches. The
46 * bytes are counted at the push, so the work done before a refusal is
47 * bounded by the budget itself, and a fetched text is bounded by
48 * max_bytes on its own before it is counted.
49 *
50 * Needs frx_lex.h, frx_dtd.h, frx_enc.h, frx_uri.h. */
51  
52 /* the frame push every kind of expansion goes through: text becomes the
53 * current input with base_off as its offset base (flat: every offset
54 * reports base_off), cur_base its base URI and ext_decl its markup rule */
55 static int
56 2179920 frx_entity_push_text(frx_lex *l, frx_entity *e, const frx_str *text, size_t ref_rel,
57 int flat, size_t base_off, const frx_str *cur_base, int ext_decl)
58 {
59 2179920 ▶ size_t ref_abs = FRX_LEX_ABS(l, ref_rel);
Branch: 1 branch
BranchTrueFalseTotal
size_t ref_abs = FRX_LEX_ABS(l, ref_rel); 1539 2178381 2179920
60 frx_frame_in *f;
61  
62 2179920 ▶ if (e && e->expanding)
Branch: 2 branches
BranchTrueFalseTotal
if (e && e->expanding) 2178897 1023 2179920
if (e && e->expanding) 9 2178888 2178897
63 9 return FRX_LEX_FAIL_ABS(l, FRX_E_ENTITY, ref_abs,
64 "an entity refers to itself, directly or through another (WFC: No Recursion)");
65 2179911 ▶ if (l->n_frames + 1 > l->max_entity_depth)
Branch: 1 branch
BranchTrueFalseTotal
if (l->n_frames + 1 > l->max_entity_depth) 3 2179908 2179911
66 3 return FRX_LEX_FAIL_ABS(l, FRX_E_EXPANSION, ref_abs,
67 "entity references nested deeper than max_entity_depth");
68 2179908 l->pushed += text->len;
69 2179908 ▶ if (l->pushed > l->max_expansion_bytes)
Branch: 1 branch
BranchTrueFalseTotal
if (l->pushed > l->max_expansion_bytes) 3 2179905 2179908
70 3 return FRX_LEX_FAIL_ABS(l, FRX_E_EXPANSION, ref_abs,
71 "entity expansion exceeds max_expansion_bytes");
72 2179905 ▶ if (l->doc_len && l->pushed / l->doc_len > (size_t)l->max_expansion_ratio)
Branch: 2 branches
BranchTrueFalseTotal
if (l->doc_len && l->pushed / l->doc_len > (size_t)l->max_expansion_ratio) 2179905 0 2179905
if (l->doc_len && l->pushed / l->doc_len > (size_t)l->max_expansion_ratio) 4 2179901 2179905
73 4 return FRX_LEX_FAIL_ABS(l, FRX_E_EXPANSION, ref_abs,
74 "entity expansion exceeds max_expansion_ratio times the input");
75  
76 2179901 ▶ if (l->n_frames == l->cap_frames) {
Branch: 1 branch
BranchTrueFalseTotal
if (l->n_frames == l->cap_frames) { 1536 2178365 2179901
77 1536 ▶ int ncap = l->cap_frames ? l->cap_frames * 2 : 8;
Branch: 1 branch
BranchTrueFalseTotal
int ncap = l->cap_frames ? l->cap_frames * 2 : 8; 9 1527 1536
78 1536 frx_frame_in *nf = (frx_frame_in *)realloc(l->frames, (size_t)ncap * sizeof *nf);
79 1536 ▶ if (!nf) return frx_lex_nomem(l);
Branch: 1 branch
BranchTrueFalseTotal
if (!nf) return frx_lex_nomem(l); 0 1536 1536
80 1536 l->frames = nf;
81 1536 l->cap_frames = ncap;
82 }
83 2179901 f = &l->frames[l->n_frames++];
84 2179901 f->in = l->in;
85 2179901 f->len = l->len;
86 2179901 f->pos = l->pos;
87 2179901 f->base = l->base;
88 2179901 f->cur_base = l->cur_base;
89 2179901 f->flat = l->flat;
90 2179901 f->ext_decl = l->ext_decl;
91 2179901 f->in_dtd = l->in_dtd;
92 2179901 f->entity = e;
93 2179901 f->seq = ++l->frame_seq;
94 2179901 f->depth_at_push = l->tag_depth;
95 2179901 f->ref_offset = ref_abs;
96  
97 2179901 l->in = (const unsigned char *)text->p;
98 2179901 l->len = text->len;
99 2179901 l->pos = 0;
100 2179901 l->base = base_off;
101 2179901 l->flat = flat;
102 2179901 l->cur_base = *cur_base;
103 2179901 l->ext_decl = ext_decl;
104 2179901 ▶ if (!e || e->is_pe) l->in_dtd = 1; /* the subset, or a parameter entity; inherited below */
Branch: 2 branches
BranchTrueFalseTotal
if (!e || e->is_pe) l->in_dtd = 1; /* the subset, or a parameter entity; inherited below */ 2178878 1023 2179901
if (!e || e->is_pe) l->in_dtd = 1; /* the subset, or a parameter entity; inherited below */ 1881 2176997 2178878
105 2179901 ▶ if (e) e->expanding = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (e) e->expanding = 1; 2178878 1023 2179901
106 2179901 return 1;
107 }
108  
109 /* the current frame is exhausted: back to the one it interrupted */
110 static int
111 2179701 frx_entity_pop(frx_lex *l)
112 {
113 2179701 frx_frame_in *f = &l->frames[l->n_frames - 1];
114 2179701 ▶ if (l->tag_depth != f->depth_at_push)
Branch: 1 branch
BranchTrueFalseTotal
if (l->tag_depth != f->depth_at_push) 4 2179697 2179701
115 4 return FRX_LEX_FAIL_ABS(l, FRX_E_ENTITY, f->ref_offset,
116 "an element opened inside an entity must close inside it (WFC: Parsed Entity)");
117 2179697 ▶ if (f->entity) f->entity->expanding = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (f->entity) f->entity->expanding = 0; 2178733 964 2179697
118 2179697 l->in = f->in;
119 2179697 l->len = f->len;
120 2179697 l->pos = f->pos;
121 2179697 l->base = f->base;
122 2179697 l->cur_base = f->cur_base;
123 2179697 l->flat = f->flat;
124 2179697 l->ext_decl = f->ext_decl;
125 2179697 l->in_dtd = f->in_dtd;
126 2179697 l->n_frames--;
127 2179697 return 1;
128 }
129  
130 /* ---- fetching --------------------------------------------------------- */
131  
132 /* A text declaration at the start of fetched text, section 4.3.1:
133 * <?xml VersionInfo? EncodingDecl S? ?>. Its byte count goes to *skip so
134 * the caller drops it; the encoding must be the one the transcoder
135 * detected; the version is reported. A pseudo-attribute scanner over the
136 * text, since the lexer's own reads through l->in. */
137 static int
138 1447 frx_entity_text_decl(frx_lex *l, const char *p, size_t n, int enc_kind, int enc_bom,
139 size_t ref_rel, size_t *skip, int *version11)
140 {
141 1447 size_t i = 5;
142 1447 int have_encoding = 0;
143 1447 *skip = 0;
144 1447 *version11 = 0;
145 1447 ▶ if (n < 6 || memcmp(p, "<?xml", 5) != 0 || !frx_is_s((unsigned char)p[5])) return 1;
Branch: 3 branches
BranchTrueFalseTotal
if (n < 6 || memcmp(p, "<?xml", 5) != 0 || !frx_is_s((unsigned char)p[5])) return 1; 1391 56 1447
if (n < 6 || memcmp(p, "<?xml", 5) != 0 || !frx_is_s((unsigned char)p[5])) return 1; 385 1006 1391
if (n < 6 || memcmp(p, "<?xml", 5) != 0 || !frx_is_s((unsigned char)p[5])) return 1; 0 385 385
146 685 for (;;) {
147 const char *name;
148 size_t nlen, vstart, vlen;
149 char q;
150 1889 ▶ while (i < n && frx_is_s((unsigned char)p[i])) i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < n && frx_is_s((unsigned char)p[i])) i++; 1889 0 1889
while (i < n && frx_is_s((unsigned char)p[i])) i++; 819 1070 1889
151 1070 ▶ if (i + 1 < n && p[i] == '?' && p[i + 1] == '>') { i += 2; break; }
Branch: 3 branches
BranchTrueFalseTotal
if (i + 1 < n && p[i] == '?' && p[i + 1] == '>') { i += 2; break; } 1070 0 1070
if (i + 1 < n && p[i] == '?' && p[i + 1] == '>') { i += 2; break; } 375 695 1070
if (i + 1 < n && p[i] == '?' && p[i + 1] == '>') { i += 2; break; } 375 0 375
152 695 name = p + i;
153 5930 ▶ while (i < n && p[i] != '=' && !frx_is_s((unsigned char)p[i]) && p[i] != '?') i++;
Branch: 4 branches
BranchTrueFalseTotal
while (i < n && p[i] != '=' && !frx_is_s((unsigned char)p[i]) && p[i] != '?') i++; 5930 0 5930
while (i < n && p[i] != '=' && !frx_is_s((unsigned char)p[i]) && p[i] != '?') i++; 5239 691 5930
while (i < n && p[i] != '=' && !frx_is_s((unsigned char)p[i]) && p[i] != '?') i++; 5235 4 5239
while (i < n && p[i] != '=' && !frx_is_s((unsigned char)p[i]) && p[i] != '?') i++; 5235 0 5235
154 695 nlen = (size_t)(p + i - name);
155 700 ▶ while (i < n && frx_is_s((unsigned char)p[i])) i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < n && frx_is_s((unsigned char)p[i])) i++; 700 0 700
while (i < n && frx_is_s((unsigned char)p[i])) i++; 5 695 700
156 695 ▶ if (i >= n || p[i] != '=')
Branch: 2 branches
BranchTrueFalseTotal
if (i >= n || p[i] != '=') 695 0 695
if (i >= n || p[i] != '=') 4 691 695
157 4 ▶ return FRX_LEX_FAIL(l, FRX_E_ENTITY, ref_rel, "malformed text declaration in an external entity");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_ENTITY, ref_rel, "malformed text declaration in an external entity"); 0 4 4
158 691 i++;
159 691 ▶ while (i < n && frx_is_s((unsigned char)p[i])) i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < n && frx_is_s((unsigned char)p[i])) i++; 691 0 691
while (i < n && frx_is_s((unsigned char)p[i])) i++; 0 691 691
160 691 ▶ if (i >= n || (p[i] != '"' && p[i] != '\''))
Branch: 3 branches
BranchTrueFalseTotal
if (i >= n || (p[i] != '"' && p[i] != '\'')) 691 0 691
if (i >= n || (p[i] != '"' && p[i] != '\'')) 213 478 691
if (i >= n || (p[i] != '"' && p[i] != '\'')) 0 213 213
161 0 ▶ return FRX_LEX_FAIL(l, FRX_E_ENTITY, ref_rel, "malformed text declaration in an external entity");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_ENTITY, ref_rel, "malformed text declaration in an external entity"); 0 0 0
162 691 q = p[i++];
163 691 vstart = i;
164 3534 ▶ while (i < n && p[i] != q) i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < n && p[i] != q) i++; 3534 0 3534
while (i < n && p[i] != q) i++; 2843 691 3534
165 691 ▶ if (i >= n) return FRX_LEX_FAIL(l, FRX_E_ENTITY, ref_rel, "unterminated text declaration in an external entity");
Branch: 2 branches
BranchTrueFalseTotal
if (i >= n) return FRX_LEX_FAIL(l, FRX_E_ENTITY, ref_rel, "unterminated text declaration in an external entity"); 0 691 691
if (i >= n) return FRX_LEX_FAIL(l, FRX_E_ENTITY, ref_rel, "unterminated text declaration in an external entity"); 0 0 0
166 691 vlen = i - vstart;
167 691 i++;
168 691 ▶ if (nlen == 7 && memcmp(name, "version", 7) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (nlen == 7 && memcmp(name, "version", 7) == 0) { 310 381 691
if (nlen == 7 && memcmp(name, "version", 7) == 0) { 310 0 310
169 310 ▶ if (have_encoding)
Branch: 1 branch
BranchTrueFalseTotal
if (have_encoding) 1 309 310
170 1 ▶ return FRX_LEX_FAIL(l, FRX_E_ENTITY, ref_rel, "in a text declaration the version precedes the encoding (section 4.3.1)");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_ENTITY, ref_rel, "in a text declaration the version precedes the encoding (section 4.3.1)"); 0 1 1
171 309 ▶ if (vlen == 3 && memcmp(p + vstart, "1.1", 3) == 0) *version11 = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (vlen == 3 && memcmp(p + vstart, "1.1", 3) == 0) *version11 = 1; 308 1 309
if (vlen == 3 && memcmp(p + vstart, "1.1", 3) == 0) *version11 = 1; 26 282 308
172 283 ▶ else if (!(vlen == 3 && memcmp(p + vstart, "1.0", 3) == 0))
Branch: 2 branches
BranchTrueFalseTotal
else if (!(vlen == 3 && memcmp(p + vstart, "1.0", 3) == 0)) 282 1 283
else if (!(vlen == 3 && memcmp(p + vstart, "1.0", 3) == 0)) 0 282 282
173 1 ▶ return FRX_LEX_FAIL(l, FRX_E_ENTITY, ref_rel, "an external entity declares an XML version other than 1.0 or 1.1");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_ENTITY, ref_rel, "an external entity declares an XML version other than 1.0 or 1.1"); 0 1 1
174 381 ▶ } else if (nlen == 8 && memcmp(name, "encoding", 8) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (nlen == 8 && memcmp(name, "encoding", 8) == 0) { 378 3 381
} else if (nlen == 8 && memcmp(name, "encoding", 8) == 0) { 378 0 378
175 378 have_encoding = 1;
176 378 ▶ if (frx_enc_kind_by_name(p + vstart, vlen) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (frx_enc_kind_by_name(p + vstart, vlen) < 0) 0 378 378
177 0 ▶ return FRX_LEX_FAIL(l, FRX_E_ENTITY, ref_rel,
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_ENTITY, ref_rel, 0 0 0
178 "an external entity declares an encoding this parser does not support (UTF-8, UTF-16, ISO-8859-1, US-ASCII)");
179 378 ▶ if (!frx_enc_declared_matches(enc_kind, p + vstart, vlen))
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_enc_declared_matches(enc_kind, p + vstart, vlen)) 1 377 378
180 1 ▶ return FRX_LEX_FAIL(l, FRX_E_ENTITY, ref_rel,
Branch: 2 branches
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_ENTITY, ref_rel, 0 1 1
return FRX_LEX_FAIL(l, FRX_E_ENTITY, ref_rel, 0 1 1
181 enc_bom ? "an external entity's encoding declaration contradicts its byte order mark"
182 : "an external entity's encoding declaration contradicts the encoding it arrived in");
183 } else {
184 3 ▶ return FRX_LEX_FAIL(l, FRX_E_ENTITY, ref_rel, "a text declaration carries only version and encoding");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_ENTITY, ref_rel, "a text declaration carries only version and encoding"); 0 3 3
185 }
186 }
187 375 ▶ if (!have_encoding)
Branch: 1 branch
BranchTrueFalseTotal
if (!have_encoding) 3 372 375
188 3 ▶ return FRX_LEX_FAIL(l, FRX_E_ENTITY, ref_rel, "a text declaration must name its encoding (section 4.3.1)");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_ENTITY, ref_rel, "a text declaration must name its encoding (section 4.3.1)"); 0 3 3
189 372 *skip = i;
190 372 return 1;
191 }
192  
193 /* Section 2.11 on input: an external parsed entity's line ends become LF
194 * before it is parsed (CR LF and CR; under XML 1.1 also NEL, CR NEL and
195 * LS), in place, so that inside its frame a CR is a character that came
196 * from a reference, as the frame rule in frx_lex.h assumes. */
197 static void
198 1434 frx_entity_normalise_eol(char *p, size_t *len, int v11)
199 {
200 1434 size_t r = 0, w = 0, n = *len;
201 294153 ▶ while (r < n) {
Branch: 1 branch
BranchTrueFalseTotal
while (r < n) { 292719 1434 294153
202 292719 unsigned char c = (unsigned char)p[r];
203 292719 ▶ if (c == '\r') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '\r') { 6009 286710 292719
204 6009 p[w++] = '\n';
205 6009 r++;
206 6009 ▶ if (r < n && p[r] == '\n') r++;
Branch: 2 branches
BranchTrueFalseTotal
if (r < n && p[r] == '\n') r++; 5999 10 6009
if (r < n && p[r] == '\n') r++; 5994 5 5999
207 15 ▶ else if (v11 && r + 1 < n && (unsigned char)p[r] == 0xC2 && (unsigned char)p[r + 1] == 0x85) r += 2;
Branch: 4 branches
BranchTrueFalseTotal
else if (v11 && r + 1 < n && (unsigned char)p[r] == 0xC2 && (unsigned char)p[r + 1] == 0x85) r += 2; 5 10 15
else if (v11 && r + 1 < n && (unsigned char)p[r] == 0xC2 && (unsigned char)p[r + 1] == 0x85) r += 2; 5 0 5
else if (v11 && r + 1 < n && (unsigned char)p[r] == 0xC2 && (unsigned char)p[r + 1] == 0x85) r += 2; 2 3 5
else if (v11 && r + 1 < n && (unsigned char)p[r] == 0xC2 && (unsigned char)p[r + 1] == 0x85) r += 2; 2 0 2
208 6009 continue;
209 }
210 286710 ▶ if (v11 && c == 0xC2 && r + 1 < n && (unsigned char)p[r + 1] == 0x85) { p[w++] = '\n'; r += 2; continue; }
Branch: 4 branches
BranchTrueFalseTotal
if (v11 && c == 0xC2 && r + 1 < n && (unsigned char)p[r + 1] == 0x85) { p[w++] = '\n'; r += 2; continue; } 4394 282316 286710
if (v11 && c == 0xC2 && r + 1 < n && (unsigned char)p[r + 1] == 0x85) { p[w++] = '\n'; r += 2; continue; } 108 4286 4394
if (v11 && c == 0xC2 && r + 1 < n && (unsigned char)p[r + 1] == 0x85) { p[w++] = '\n'; r += 2; continue; } 108 0 108
if (v11 && c == 0xC2 && r + 1 < n && (unsigned char)p[r + 1] == 0x85) { p[w++] = '\n'; r += 2; continue; } 5 103 108
211 286705 ▶ if (v11 && c == 0xE2 && r + 2 < n && (unsigned char)p[r + 1] == 0x80 && (unsigned char)p[r + 2] == 0xA8) { p[w++] = '\n'; r += 3; continue; }
Branch: 5 branches
BranchTrueFalseTotal
if (v11 && c == 0xE2 && r + 2 < n && (unsigned char)p[r + 1] == 0x80 && (unsigned char)p[r + 2] == 0xA8) { p[w++] = '\n'; r += 3; continue; } 4389 282316 286705
if (v11 && c == 0xE2 && r + 2 < n && (unsigned char)p[r + 1] == 0x80 && (unsigned char)p[r + 2] == 0xA8) { p[w++] = '\n'; r += 3; continue; } 0 4389 4389
if (v11 && c == 0xE2 && r + 2 < n && (unsigned char)p[r + 1] == 0x80 && (unsigned char)p[r + 2] == 0xA8) { p[w++] = '\n'; r += 3; continue; } 0 0 0
if (v11 && c == 0xE2 && r + 2 < n && (unsigned char)p[r + 1] == 0x80 && (unsigned char)p[r + 2] == 0xA8) { p[w++] = '\n'; r += 3; continue; } 0 0 0
if (v11 && c == 0xE2 && r + 2 < n && (unsigned char)p[r + 1] == 0x80 && (unsigned char)p[r + 2] == 0xA8) { p[w++] = '\n'; r += 3; continue; } 0 0 0
212 286705 p[w++] = (char)c;
213 286705 r++;
214 }
215 1434 p[w] = '\0';
216 1434 *len = w;
217 1434 }
218  
219 /* the bytes behind (pub, sys) resolved against base, as arena UTF-8 text
220 * with its text declaration stripped; cached per resolved identifier */
221 static int
222 1480 frx_entity_fetch(frx_lex *l, int kind, const frx_str *pub, const frx_str *sys,
223 const frx_str *base, size_t ref_rel,
224 frx_str *text, frx_str *uri, int *version11)
225 {
226 frx_buf joined;
227 frx_fetch_entry *fe;
228 frx_fetched f;
229 frx_enc enc;
230 const char *err;
231 const char *utf8;
232 size_t ulen, skip;
233 int v11;
234  
235 /* the resolved identifier: the system literal against the declaring
236 * entity's base; with no base it is used as written */
237 1480 frx_buf_init(&joined);
238 1480 ▶ if (!frx_uri_join(&joined, base->p, base->len, sys->p, sys->len, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_uri_join(&joined, base->p, base->len, sys->p, sys->len, 0)) { 0 1480 1480
239 0 frx_buf_free(&joined);
240 0 return frx_lex_nomem(l);
241 }
242 1480 ▶ *uri = frx_arena_strndup(l->arena, joined.p ? joined.p : "", joined.len);
Branch: 1 branch
BranchTrueFalseTotal
*uri = frx_arena_strndup(l->arena, joined.p ? joined.p : "", joined.len); 1480 0 1480
243 1480 frx_buf_free(&joined);
244 1480 ▶ if (!uri->p) return frx_lex_nomem(l);
Branch: 1 branch
BranchTrueFalseTotal
if (!uri->p) return frx_lex_nomem(l); 0 1480 1480
245  
246 1752 ▶ for (fe = l->fetches; fe; fe = fe->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (fe = l->fetches; fe; fe = fe->next) { 274 1478 1752
247 274 ▶ if (frx_str_eq2(&fe->uri, uri)) {
Branch: 1 branch
BranchTrueFalseTotal
if (frx_str_eq2(&fe->uri, uri)) { 2 272 274
248 2 *text = fe->text;
249 2 *version11 = fe->version11;
250 2 return 1;
251 }
252 }
253  
254 1478 ▶ if (!l->resolve)
Branch: 1 branch
BranchTrueFalseTotal
if (!l->resolve) 3 1475 1478
255 3 ▶ return FRX_LEX_FAIL(l, FRX_E_ENTITY, ref_rel,
Branch: 2 branches
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_ENTITY, ref_rel, 0 3 3
return FRX_LEX_FAIL(l, FRX_E_ENTITY, ref_rel, 0 3 3
256 kind == FRX_REF_SUBSET
257 ? "the external subset cannot be read without a resolver; the resolve option arrives with external entities"
258 : "an external entity cannot be read without a resolver; the resolve option arrives with external entities");
259 1475 ▶ if (l->n_fetches >= l->max_fetches)
Branch: 1 branch
BranchTrueFalseTotal
if (l->n_fetches >= l->max_fetches) 1 1474 1475
260 1 ▶ return FRX_LEX_FAIL(l, FRX_E_EXPANSION, ref_rel, "more external fetches than max_fetches");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_EXPANSION, ref_rel, "more external fetches than max_fetches"); 0 1 1
261 1474 l->n_fetches++;
262  
263 1474 memset(&f, 0, sizeof f);
264 1474 ▶ err = l->resolve(l->resolve_ud, pub->len ? pub->p : NULL, uri->p, base->len ? base->p : NULL, kind, &f);
Branch: 2 branches
BranchTrueFalseTotal
err = l->resolve(l->resolve_ud, pub->len ? pub->p : NULL, uri->p, base->len ? base->p : NULL, kind, &f); 1419 55 1474
err = l->resolve(l->resolve_ud, pub->len ? pub->p : NULL, uri->p, base->len ? base->p : NULL, kind, &f); 42 1432 1474
265 1474 ▶ if (err) return FRX_LEX_FAIL(l, FRX_E_ENTITY, ref_rel, err);
Branch: 2 branches
BranchTrueFalseTotal
if (err) return FRX_LEX_FAIL(l, FRX_E_ENTITY, ref_rel, err); 26 1448 1474
if (err) return FRX_LEX_FAIL(l, FRX_E_ENTITY, ref_rel, err); 0 26 26
266 1448 ▶ if (l->max_bytes && f.len > l->max_bytes) {
Branch: 2 branches
BranchTrueFalseTotal
if (l->max_bytes && f.len > l->max_bytes) { 2 1446 1448
if (l->max_bytes && f.len > l->max_bytes) { 1 1 2
267 1 ▶ if (f.release) f.release(f.ud, &f);
Branch: 1 branch
BranchTrueFalseTotal
if (f.release) f.release(f.ud, &f); 1 0 1
268 1 ▶ return FRX_LEX_FAIL(l, FRX_E_TOO_LARGE, ref_rel, "a fetched entity exceeds max_bytes");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_TOO_LARGE, ref_rel, "a fetched entity exceeds max_bytes"); 0 1 1
269 }
270  
271 /* its own encoding: byte order mark, first bytes, text declaration */
272 1447 memset(&enc, 0, sizeof enc);
273 1447 ▶ if (!frx_enc_detect(&enc, (const unsigned char *)f.bytes, f.len, NULL, &l->err)
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_enc_detect(&enc, (const unsigned char *)f.bytes, f.len, NULL, &l->err) 1447 0 1447
274 1447 ▶ || !frx_enc_run(&enc, (const unsigned char *)f.bytes, f.len, &l->err)) {
Branch: 1 branch
BranchTrueFalseTotal
|| !frx_enc_run(&enc, (const unsigned char *)f.bytes, f.len, &l->err)) { 0 1447 1447
275 0 l->failed = 1;
276 0 ▶ l->err.offset = FRX_LEX_ABS(l, ref_rel);
Branch: 1 branch
BranchTrueFalseTotal
l->err.offset = FRX_LEX_ABS(l, ref_rel); 0 0 0
277 0 l->err.enc = NULL;
278 0 frx_enc_free(&enc);
279 0 ▶ if (f.release) f.release(f.ud, &f);
Branch: 1 branch
BranchTrueFalseTotal
if (f.release) f.release(f.ud, &f); 0 0 0
280 0 return 0;
281 }
282 1447 ▶ utf8 = enc.out ? enc.out : f.bytes + enc.bom;
Branch: 1 branch
BranchTrueFalseTotal
utf8 = enc.out ? enc.out : f.bytes + enc.bom; 20 1427 1447
283 1447 ▶ ulen = enc.out ? enc.out_len : f.len - (size_t)enc.bom;
Branch: 1 branch
BranchTrueFalseTotal
ulen = enc.out ? enc.out_len : f.len - (size_t)enc.bom; 20 1427 1447
284 1447 ▶ if (!frx_entity_text_decl(l, utf8, ulen, enc.kind, enc.bom != 0, ref_rel, &skip, &v11)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_entity_text_decl(l, utf8, ulen, enc.kind, enc.bom != 0, ref_rel, &skip, &v11)) { 13 1434 1447
285 13 frx_enc_free(&enc);
286 13 ▶ if (f.release) f.release(f.ud, &f);
Branch: 1 branch
BranchTrueFalseTotal
if (f.release) f.release(f.ud, &f); 13 0 13
287 13 return 0;
288 }
289 1434 *text = frx_arena_strndup(l->arena, utf8 + skip, ulen - skip);
290 1434 frx_enc_free(&enc);
291 1434 ▶ if (f.release) f.release(f.ud, &f);
Branch: 1 branch
BranchTrueFalseTotal
if (f.release) f.release(f.ud, &f); 1430 4 1434
292 1434 ▶ if (!text->p) return frx_lex_nomem(l);
Branch: 1 branch
BranchTrueFalseTotal
if (!text->p) return frx_lex_nomem(l); 0 1434 1434
293 1434 ▶ frx_entity_normalise_eol((char *)text->p, &text->len, l->version11 || v11);
Branch: 2 branches
BranchTrueFalseTotal
frx_entity_normalise_eol((char *)text->p, &text->len, l->version11 || v11); 1370 64 1434
frx_entity_normalise_eol((char *)text->p, &text->len, l->version11 || v11); 9 1361 1370
294 1434 *version11 = v11;
295  
296 1434 fe = (frx_fetch_entry *)frx_arena_alloc(l->arena, sizeof *fe);
297 1434 ▶ if (!fe) return frx_lex_nomem(l);
Branch: 1 branch
BranchTrueFalseTotal
if (!fe) return frx_lex_nomem(l); 0 1434 1434
298 1434 fe->uri = *uri;
299 1434 fe->text = *text;
300 1434 fe->version11 = v11;
301 1434 fe->next = l->fetches;
302 1434 l->fetches = fe;
303 1434 return 1;
304 }
305  
306 /* an external entity's text, fetched on first use */
307 static int
308 449 frx_entity_ensure_fetched(frx_lex *l, frx_entity *e, size_t ref_rel)
309 {
310 449 ▶ if (e->fetched) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (e->fetched) return 1; 13 436 449
311 436 ▶ if (!frx_entity_fetch(l, FRX_REF_ENTITY, &e->public_id, &e->system_id, &e->base, ref_rel,
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_entity_fetch(l, FRX_REF_ENTITY, &e->public_id, &e->system_id, &e->base, ref_rel, 25 411 436
312 25 &e->text, &e->uri, &e->version11)) return 0;
313 411 e->fetched = 1;
314 411 return 1;
315 }
316  
317 /* the text of e with 4.4.8's space on each side, built once (a padded
318 * text is never empty, so its length says whether it exists) */
319 static const frx_str *
320 1554 frx_entity_padded(frx_lex *l, frx_entity *e)
321 {
322 1554 ▶ if (!e->padded.len) {
Branch: 1 branch
BranchTrueFalseTotal
if (!e->padded.len) { 1164 390 1554
323 1164 char *p = (char *)frx_arena_alloc(l->arena, e->text.len + 3);
324 1164 ▶ if (!p) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!p) return NULL; 0 1164 1164
325 1164 p[0] = ' ';
326 1164 memcpy(p + 1, e->text.p, e->text.len);
327 1164 p[e->text.len + 1] = ' ';
328 1164 p[e->text.len + 2] = '\0';
329 1164 e->padded.p = p;
330 1164 e->padded.len = e->text.len + 2;
331 }
332 1554 return &e->padded;
333 }
334  
335 /* push e: an internal entity's own text at its declaration's offset, an
336 * external one's fetched text as a flat frame at the reference */
337 static int
338 2178929 frx_entity_push(frx_lex *l, frx_entity *e, size_t ref_rel, int padded)
339 {
340 const frx_str *text;
341 2178929 ▶ if (e->external) {
Branch: 1 branch
BranchTrueFalseTotal
if (e->external) { 449 2178480 2178929
342 449 ▶ if (!frx_entity_ensure_fetched(l, e, ref_rel)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_entity_ensure_fetched(l, e, ref_rel)) return 0; 25 424 449
343 424 ▶ if (e->version11 && !l->version11)
Branch: 2 branches
BranchTrueFalseTotal
if (e->version11 && !l->version11) 16 408 424
if (e->version11 && !l->version11) 7 9 16
344 7 ▶ return FRX_LEX_FAIL(l, FRX_E_ENTITY, ref_rel,
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_ENTITY, ref_rel, 1 6 7
345 "an XML 1.1 entity cannot be referenced from an XML 1.0 document (section 4.3.4)");
346 417 ▶ text = padded ? frx_entity_padded(l, e) : &e->text;
Branch: 1 branch
BranchTrueFalseTotal
text = padded ? frx_entity_padded(l, e) : &e->text; 76 341 417
347 417 ▶ if (!text) return frx_lex_nomem(l);
Branch: 1 branch
BranchTrueFalseTotal
if (!text) return frx_lex_nomem(l); 0 417 417
348 417 ▶ return frx_entity_push_text(l, e, text, ref_rel, 1, FRX_LEX_ABS(l, ref_rel), &e->uri, 1);
Branch: 1 branch
BranchTrueFalseTotal
return frx_entity_push_text(l, e, text, ref_rel, 1, FRX_LEX_ABS(l, ref_rel), &e->uri, 1); 36 381 417
349 }
350 2178480 ▶ text = padded ? frx_entity_padded(l, e) : &e->text;
Branch: 1 branch
BranchTrueFalseTotal
text = padded ? frx_entity_padded(l, e) : &e->text; 1478 2177002 2178480
351 2178480 ▶ if (!text) return frx_lex_nomem(l);
Branch: 1 branch
BranchTrueFalseTotal
if (!text) return frx_lex_nomem(l); 0 2178480 2178480
352 /* the padding space precedes the literal's first byte, which follows
353 * a quote, so the base moves back one and offsets inside still map */
354 2178480 return frx_entity_push_text(l, e, text, ref_rel, e->flat,
355 2178480 ▶ padded ? e->text_offset - 1 : e->text_offset, &e->base, l->ext_decl);
Branch: 1 branch
BranchTrueFalseTotal
padded ? e->text_offset - 1 : e->text_offset, &e->base, l->ext_decl); 1478 2177002 2178480
356 }
357  
358 /* &Name; in content or an attribute value, not one of the five; pos is
359 * at the &, start is its frame-relative offset */
360 static int
361 2177149 frx_entity_reference(frx_lex *l, size_t start, int in_attr)
362 {
363 frx_str name;
364 frx_entity *e;
365 2177149 l->pos = start + 1;
366 2177149 ▶ if (!frx_lex_name(l, 0, &name)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_name(l, 0, &name)) return 0; 31 2177118 2177149
367 2177118 ▶ if (l->pos >= l->len || l->in[l->pos] != ';')
Branch: 2 branches
BranchTrueFalseTotal
if (l->pos >= l->len || l->in[l->pos] != ';') 2177107 11 2177118
if (l->pos >= l->len || l->in[l->pos] != ';') 12 2177095 2177107
368 23 ▶ return FRX_LEX_FAIL(l, FRX_E_REFERENCE, start, "malformed entity reference");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_REFERENCE, start, "malformed entity reference"); 0 23 23
369 2177095 l->pos++;
370 2177095 ▶ e = l->dtd ? frx_dtd_entity(l->dtd, &name, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
e = l->dtd ? frx_dtd_entity(l->dtd, &name, 0) : NULL; 2177092 3 2177095
371 2177095 ▶ if (!e) {
Branch: 1 branch
BranchTrueFalseTotal
if (!e) { 33 2177062 2177095
372 /* WFC: Entity Declared applies to a document with no DTD, one
373 * whose internal subset made no parameter entity reference, or one
374 * declared standalone="yes". Outside those three the processor may
375 * not have seen every declaration, so an undeclared name is NOT a
376 * well-formedness error; it is VC: Entity Declared, which only a
377 * validating parse reports. The reference then stands for nothing
378 * and contributes no characters, and its offset is kept so the
379 * validate pass can report it. Erratum E13 is the case where the
380 * parameter entity reference alone is what opens the exemption.
381 *
382 * Nothing of this reaches the strict profile, which refuses every
383 * DOCTYPE, so a caller that wants an undeclared entity to be fatal
384 * has it by staying there. */
385 33 ▶ if (l->dtd && !l->standalone
Branch: 2 branches
BranchTrueFalseTotal
if (l->dtd && !l->standalone 30 3 33
if (l->dtd && !l->standalone 28 2 30
386 28 ▶ && (l->dtd->pe_ref_seen || l->dtd->external_subset)) {
Branch: 2 branches
BranchTrueFalseTotal
&& (l->dtd->pe_ref_seen || l->dtd->external_subset)) { 12 16 28
&& (l->dtd->pe_ref_seen || l->dtd->external_subset)) { 2 10 12
387 18 ▶ if (l->n_undecl == l->cap_undecl) {
Branch: 1 branch
BranchTrueFalseTotal
if (l->n_undecl == l->cap_undecl) { 18 0 18
388 18 ▶ int ncap = l->cap_undecl ? l->cap_undecl * 2 : 8;
Branch: 1 branch
BranchTrueFalseTotal
int ncap = l->cap_undecl ? l->cap_undecl * 2 : 8; 0 18 18
389 18 size_t *nu = (size_t *)realloc(l->undecl, (size_t)ncap * sizeof *nu);
390 18 ▶ if (!nu) return FRX_LEX_FAIL(l, FRX_E_NOMEM, start, "out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (!nu) return FRX_LEX_FAIL(l, FRX_E_NOMEM, start, "out of memory"); 0 18 18
if (!nu) return FRX_LEX_FAIL(l, FRX_E_NOMEM, start, "out of memory"); 0 0 0
391 18 l->undecl = nu;
392 18 l->cap_undecl = ncap;
393 }
394 18 ▶ l->undecl[l->n_undecl++] = FRX_LEX_ABS(l, start);
Branch: 1 branch
BranchTrueFalseTotal
l->undecl[l->n_undecl++] = FRX_LEX_ABS(l, start); 4 14 18
395 18 return 1; /* included as nothing */
396 }
397 15 ▶ return FRX_LEX_FAIL(l, FRX_E_REFERENCE, start,
Branch: 2 branches
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_REFERENCE, start, 12 3 15
return FRX_LEX_FAIL(l, FRX_E_REFERENCE, start, 0 15 15
398 l->dtd ? "undeclared entity"
399 : "undeclared entity (there is no DOCTYPE, so only the five predefined exist)");
400 }
401 2177062 ▶ if (e->unparsed)
Branch: 1 branch
BranchTrueFalseTotal
if (e->unparsed) 6 2177056 2177062
402 6 ▶ return FRX_LEX_FAIL(l, FRX_E_ENTITY, start,
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_ENTITY, start, 0 6 6
403 "an unparsed entity may be named only by an ENTITY attribute (WFC: Parsed Entity)");
404 2177056 ▶ if (e->external && in_attr)
Branch: 2 branches
BranchTrueFalseTotal
if (e->external && in_attr) 363 2176693 2177056
if (e->external && in_attr) 8 355 363
405 8 ▶ return FRX_LEX_FAIL(l, FRX_E_ENTITY, start,
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_ENTITY, start, 0 8 8
406 "an attribute value must not refer to an external entity (WFC: No External Entity References)");
407 2177048 ▶ if (l->standalone && !e->direct && !l->in_dtd)
Branch: 3 branches
BranchTrueFalseTotal
if (l->standalone && !e->direct && !l->in_dtd) 69 2176979 2177048
if (l->standalone && !e->direct && !l->in_dtd) 6 63 69
if (l->standalone && !e->direct && !l->in_dtd) 5 1 6
408 5 ▶ return FRX_LEX_FAIL(l, FRX_E_ENTITY, start,
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_ENTITY, start, 0 5 5
409 "standalone=\"yes\" but the entity is declared in the external subset or a parameter entity (WFC: Entity Declared)");
410 2177043 return frx_entity_push(l, e, start, 0);
411 }
412  
413 /* %Name; in the DTD; pos is past the %. padded says it is included as a
414 * PE (4.4.8, between or inside declarations) rather than in a literal. */
415 static int
416 1913 frx_entity_pe_reference(frx_lex *l, size_t start, int padded)
417 {
418 frx_str name;
419 frx_entity *e;
420 1913 ▶ if (!frx_lex_name(l, 0, &name)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_name(l, 0, &name)) return 0; 7 1906 1913
421 1906 ▶ if (l->pos >= l->len || l->in[l->pos] != ';')
Branch: 2 branches
BranchTrueFalseTotal
if (l->pos >= l->len || l->in[l->pos] != ';') 1904 2 1906
if (l->pos >= l->len || l->in[l->pos] != ';') 6 1898 1904
422 8 ▶ return FRX_LEX_FAIL(l, FRX_E_REFERENCE, start, "malformed parameter entity reference");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_REFERENCE, start, "malformed parameter entity reference"); 0 8 8
423 1898 l->pos++;
424 1898 l->dtd->pe_ref_seen = 1;
425 1898 e = frx_dtd_entity(l->dtd, &name, 1);
426 1898 ▶ if (!e)
Branch: 1 branch
BranchTrueFalseTotal
if (!e) 12 1886 1898
427 12 ▶ return FRX_LEX_FAIL(l, FRX_E_REFERENCE, start, "undeclared parameter entity");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_REFERENCE, start, "undeclared parameter entity"); 5 7 12
428 1886 return frx_entity_push(l, e, start, padded);
429 }
430  
431 #endif /* FRX_ENTITY_H */
« include/frx/frx_plugin.h ↑ Summary xs/internal.xs »