include/frx/frx_reader.h

filter: Statementstmt 96.5% 253 / 262 Branchbran 73.1% 120 / 164 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 87.5% 373 / 426 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/frx/frx_uri.h ↑ Summary include/frx/frx_enc.h »
1 #ifndef FRX_READER_H
2 #define FRX_READER_H
3  
4 /* The pull reader: bytes in, one event at a time out, at a flat cost.
5 *
6 * A reader owns a driver (frx_parse.h) whose sink queues events, and
7 * pumps it one step at a time: next() hands out the next queued event,
8 * and when the queue is empty runs a step, which either queues one or two
9 * events (a text flush and the token that ended it), says the input ended
10 * inside a token (0: feed more), or ends the document. Every event's
11 * strings live in the reader's scratch arena and are valid until the
12 * event after next() moves past them; a caller keeps what it needs.
13 *
14 * Memory: two arenas. The DTD, the entities and the fetch cache are in
15 * the persistent one, because a record may refer to them at any point of
16 * the stream. Everything a token allocates - names, values, attribute
17 * arrays, namespace scopes - is in the scratch arena, which is marked
18 * when a token is about to be read at depth one (between records) and
19 * released when the end event of that record has been consumed, so a
20 * stream of a million records costs the scratch of one. The root's own
21 * strings and scope were allocated before the first mark and stay. The
22 * input buffer is compacted the same way: consumed bytes are dropped
23 * from its front once a token boundary is known, and the lexer's base
24 * offset carries the count so every offset reported is into the stream.
25 *
26 * Encodings: under the full profile the first bytes choose the encoding
27 * (frx_enc.h), and everything after is transcoded to UTF-8 as it
28 * arrives, one unit at a time, with up to three bytes carried between
29 * feeds: the one place a partial unit exists. Under strict the bytes are
30 * UTF-8 or refused, as everywhere.
31 *
32 * Subtrees: at a start event, frx_reader_subtree switches the driver's
33 * sink to the tree sink in copy mode over a fresh document, replays that
34 * start, and pumps until the matching end; the document it returns is
35 * independent of the reader. If the input ends inside the subtree the
36 * call returns 0 and the reader stays in capture: feed and call it
37 * again. That is what the plugin's record mode does per record.
38 *
39 * Needs frx_parse.h (and through it everything the core has), frx_enc.h. */
40  
41 /* FRX_START, FRX_END and FRX_DOCTYPE are frx_abi.h's, since a consumer
42 * reading events compares against them. */
43  
44 typedef struct frx_event {
45 int kind;
46 frx_str qname;
47 frx_str prefix;
48 frx_str local;
49 frx_str ns;
50 frx_str value; /* text, comment body, pi data */
51 frx_str entity; /* text: the entity it came from; "" for the document */
52 const frx_attr *attrs;
53 int n_attrs;
54 const frx_nsdecl *decls;
55 int n_decls;
56 const frx_scope *scope;
57 size_t offset;
58 int depth; /* elements open before this event; a start's own depth is depth + 1 */
59 int empty; /* start: <a/> */
60 const frx_doctype *doctype;
61 } frx_event;
62  
63 #define FRX_READER_QUEUE 8
64 #define FRX_READER_COMPACT (64u * 1024u)
65  
66 typedef struct frx_reader {
67 frx_opts_ex oe;
68 int profile;
69 frx_arena arena; /* persistent */
70 frx_arena scratch; /* per record */
71 frx_parse p;
72 frx_buf raw; /* fed bytes not yet transcoded (a non-UTF-8 stream) */
73 size_t raw_pos;
74 frx_buf in; /* what the lexer reads */
75 size_t dropped; /* bytes dropped from the front of in */
76 int eof_fed;
77 int enc_ready; /* the encoding has been chosen */
78 int enc_kind;
79 int enc_bom;
80 frx_event q[FRX_READER_QUEUE];
81 int qh, qn;
82 frx_event cur; /* the event last handed out */
83 int have_cur;
84 int done;
85 int failed;
86 frx_err err;
87 frx_arena_mark mark;
88 int have_mark;
89 int pending_release;
90 /* subtree capture */
91 int capturing;
92 int cap_depth; /* the captured element's own depth */
93 frx_doc *cap_doc;
94 frx_tree_sink cap_sink;
95 } frx_reader;
96  
97 #define FRX_READER_FAIL(r, c, off, what) \
98 (frx_err_set(&(r)->err, (c), (off), (what)), (r)->failed = 1, 0)
99  
100 /* ---- the queue sink ------------------------------------------------------ */
101  
102 static frx_event *
103 1782832 frx_reader_enqueue(frx_reader *r)
104 {
105 frx_event *e;
106 1782832 ▶ if (r->qn == FRX_READER_QUEUE) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (r->qn == FRX_READER_QUEUE) return NULL; 0 1782832 1782832
107 1782832 e = &r->q[(r->qh + r->qn) % FRX_READER_QUEUE];
108 1782832 r->qn++;
109 1782832 memset(e, 0, sizeof *e);
110 1782832 e->qname = e->prefix = e->local = e->ns = e->value = e->entity = FRX_EMPTY_STR;
111 1782832 return e;
112 }
113  
114 static int
115 860924 frx_reader_sink_start(frx_parse *p, void *ud, const frx_ev_start *ev)
116 {
117 860924 frx_reader *r = (frx_reader *)ud;
118 860924 frx_event *e = frx_reader_enqueue(r);
119 860924 ▶ if (!e) return FRX_PARSE_FAIL(p, FRX_E_SYNTAX, ev->offset, "reader queue overflow");
Branch: 1 branch
BranchTrueFalseTotal
if (!e) return FRX_PARSE_FAIL(p, FRX_E_SYNTAX, ev->offset, "reader queue overflow"); 0 860924 860924
120 860924 e->kind = FRX_START;
121 860924 e->qname = ev->qname;
122 860924 e->prefix = ev->prefix;
123 860924 e->local = ev->local;
124 860924 e->ns = ev->ns;
125 860924 e->attrs = ev->attrs;
126 860924 e->n_attrs = ev->n_attrs;
127 860924 e->decls = ev->decls;
128 860924 e->n_decls = ev->n_decls;
129 860924 e->scope = ev->scope;
130 860924 e->offset = ev->offset;
131 860924 e->depth = p->depth;
132 860924 e->empty = ev->empty;
133 860924 return 1;
134 }
135  
136 static int
137 100088 frx_reader_sink_end(frx_parse *p, void *ud, const frx_str *qname, size_t offset)
138 {
139 100088 frx_reader *r = (frx_reader *)ud;
140 100088 frx_event *e = frx_reader_enqueue(r);
141 100088 ▶ if (!e) return FRX_PARSE_FAIL(p, FRX_E_SYNTAX, offset, "reader queue overflow");
Branch: 1 branch
BranchTrueFalseTotal
if (!e) return FRX_PARSE_FAIL(p, FRX_E_SYNTAX, offset, "reader queue overflow"); 0 100088 100088
142 100088 e->kind = FRX_END;
143 100088 e->qname = *qname;
144 100088 frx_ns_split(&e->qname, &e->prefix, &e->local);
145 100088 e->offset = offset;
146 100088 e->depth = p->depth + 1; /* the driver has popped it; report its own depth */
147 100088 return 1;
148 }
149  
150 static int
151 820898 frx_reader_sink_text(frx_parse *p, void *ud, const char *s, size_t n,
152 const frx_span *spans, int n_spans, size_t offset, const frx_str *entity,
153 int from_ref)
154 {
155 820898 frx_reader *r = (frx_reader *)ud;
156 820898 frx_event *e = frx_reader_enqueue(r);
157 (void)spans; (void)n_spans; (void)from_ref;
158 820898 ▶ if (!e) return FRX_PARSE_FAIL(p, FRX_E_SYNTAX, offset, "reader queue overflow");
Branch: 1 branch
BranchTrueFalseTotal
if (!e) return FRX_PARSE_FAIL(p, FRX_E_SYNTAX, offset, "reader queue overflow"); 0 820898 820898
159 820898 e->kind = FRX_TEXT;
160 820898 e->value = frx_arena_strndup(&r->scratch, s, n); /* the driver's buffer is reused */
161 820898 ▶ if (!e->value.p) return frx_parse_nomem(p, offset);
Branch: 1 branch
BranchTrueFalseTotal
if (!e->value.p) return frx_parse_nomem(p, offset); 0 820898 820898
162 820898 e->entity = *entity;
163 820898 e->offset = offset;
164 820898 e->depth = p->depth;
165 820898 return 1;
166 }
167  
168 static int
169 61 frx_reader_sink_comment(frx_parse *p, void *ud, const frx_str *value, size_t offset)
170 {
171 61 frx_reader *r = (frx_reader *)ud;
172 61 frx_event *e = frx_reader_enqueue(r);
173 61 ▶ if (!e) return FRX_PARSE_FAIL(p, FRX_E_SYNTAX, offset, "reader queue overflow");
Branch: 1 branch
BranchTrueFalseTotal
if (!e) return FRX_PARSE_FAIL(p, FRX_E_SYNTAX, offset, "reader queue overflow"); 0 61 61
174 61 e->kind = FRX_COMMENT;
175 61 e->value = *value;
176 61 e->offset = offset;
177 61 e->depth = p->depth;
178 61 return 1;
179 }
180  
181 static int
182 36 frx_reader_sink_pi(frx_parse *p, void *ud, const frx_str *target, const frx_str *data, size_t offset)
183 {
184 36 frx_reader *r = (frx_reader *)ud;
185 36 frx_event *e = frx_reader_enqueue(r);
186 36 ▶ if (!e) return FRX_PARSE_FAIL(p, FRX_E_SYNTAX, offset, "reader queue overflow");
Branch: 1 branch
BranchTrueFalseTotal
if (!e) return FRX_PARSE_FAIL(p, FRX_E_SYNTAX, offset, "reader queue overflow"); 0 36 36
187 36 e->kind = FRX_PI;
188 36 e->local = *target;
189 36 e->qname = *target;
190 36 e->value = *data;
191 36 e->offset = offset;
192 36 e->depth = p->depth;
193 36 return 1;
194 }
195  
196 static int
197 825 frx_reader_sink_doctype(frx_parse *p, void *ud, const frx_doctype *dt, const frx_dtd *dtd)
198 {
199 825 frx_reader *r = (frx_reader *)ud;
200 825 frx_event *e = frx_reader_enqueue(r);
201 (void)dtd;
202 825 ▶ if (!e) return FRX_PARSE_FAIL(p, FRX_E_SYNTAX, dt->offset, "reader queue overflow");
Branch: 1 branch
BranchTrueFalseTotal
if (!e) return FRX_PARSE_FAIL(p, FRX_E_SYNTAX, dt->offset, "reader queue overflow"); 0 825 825
203 825 e->kind = FRX_DOCTYPE;
204 825 e->qname = dt->name;
205 825 e->local = dt->name;
206 825 e->doctype = dt;
207 825 e->offset = dt->offset;
208 825 e->depth = 0;
209 825 return 1;
210 }
211  
212 static const frx_sink FRX_READER_SINK = {
213 frx_reader_sink_start, frx_reader_sink_end, frx_reader_sink_text,
214 frx_reader_sink_comment, frx_reader_sink_pi, frx_reader_sink_doctype,
215 NULL
216 };
217  
218 /* ---- lifetime ------------------------------------------------------------- */
219  
220 static void
221 885 frx_reader_init(frx_reader *r, const frx_opts_ex *oe_in)
222 {
223 885 memset(r, 0, sizeof *r);
224 885 frx_opts_ex_norm(oe_in, &r->oe);
225 885 r->profile = r->oe.profile == FRX_PROFILE_FULL ? FRX_PROFILE_FULL : FRX_PROFILE_STRICT;
226 885 frx_arena_init(&r->arena);
227 885 frx_arena_init(&r->scratch);
228 885 frx_buf_init(&r->raw);
229 885 frx_buf_init(&r->in);
230 885 frx_parse_init(&r->p, &r->oe, &r->arena, &r->scratch, &FRX_READER_SINK, r);
231 885 r->p.split_entities = 1;
232 885 frx_err_set(&r->err, FRX_OK, 0, NULL);
233 885 r->cur.qname = r->cur.prefix = r->cur.local = r->cur.ns = r->cur.value = r->cur.entity = FRX_EMPTY_STR;
234 885 }
235  
236 /* A reader on the heap, for a caller that cannot see the struct: the C
237 * ABI hands out a pointer and takes it back, and nothing else needs
238 * these two. */
239 static frx_reader *
240 2 frx_reader_new(const frx_opts_ex *oe)
241 {
242 2 frx_reader *r = (frx_reader *)malloc(sizeof *r);
243 2 ▶ if (r) frx_reader_init(r, oe);
Branch: 1 branch
BranchTrueFalseTotal
if (r) frx_reader_init(r, oe); 2 0 2
244 2 return r;
245 }
246  
247 static void
248 885 frx_reader_free(frx_reader *r)
249 {
250 885 frx_parse_free(&r->p);
251 885 frx_tree_sink_free(&r->cap_sink);
252 885 ▶ if (r->cap_doc) frx_doc_free(r->cap_doc);
Branch: 1 branch
BranchTrueFalseTotal
if (r->cap_doc) frx_doc_free(r->cap_doc); 0 885 885
253 885 frx_buf_free(&r->raw);
254 885 frx_buf_free(&r->in);
255 885 frx_arena_free(&r->scratch);
256 885 frx_arena_free(&r->arena);
257 885 }
258  
259 /* the other half of frx_reader_new: release what the reader holds and
260 * then the reader itself */
261 static void
262 2 frx_reader_destroy(frx_reader *r)
263 {
264 2 ▶ if (!r) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return; 0 2 2
265 2 frx_reader_free(r);
266 2 free(r);
267 }
268  
269 /* ---- input --------------------------------------------------------------- */
270  
271 /* the lexer's view of the document frame follows the buffer */
272 static void
273 7118405 frx_reader_sync_lex(frx_reader *r)
274 {
275 7118405 frx_lex *l = &r->p.lex;
276 7118405 ▶ const unsigned char *in = (const unsigned char *)(r->in.p ? r->in.p : "");
Branch: 1 branch
BranchTrueFalseTotal
const unsigned char *in = (const unsigned char *)(r->in.p ? r->in.p : ""); 7118405 0 7118405
277 7118405 ▶ if (l->n_frames) {
Branch: 1 branch
BranchTrueFalseTotal
if (l->n_frames) { 94 7118311 7118405
278 94 l->frames[0].in = in;
279 94 l->frames[0].len = r->in.len;
280 } else {
281 7118311 l->in = in;
282 7118311 l->len = r->in.len;
283 }
284 7118405 l->doc_len = r->dropped + r->in.len;
285 7118405 ▶ l->eof = r->eof_fed && r->raw_pos >= r->raw.len;
Branch: 2 branches
BranchTrueFalseTotal
l->eof = r->eof_fed && r->raw_pos >= r->raw.len; 568 7117837 7118405
l->eof = r->eof_fed && r->raw_pos >= r->raw.len; 568 0 568
286 7118405 }
287  
288 /* the encoding from the first bytes; once, when there are enough of them
289 * or the input is complete */
290 static int
291 4248 frx_reader_choose_encoding(frx_reader *r)
292 {
293 frx_enc enc;
294 4248 ▶ if (r->enc_ready) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (r->enc_ready) return 1; 3349 899 4248
295 899 ▶ if (r->profile != FRX_PROFILE_FULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->profile != FRX_PROFILE_FULL) { 48 851 899
296 48 r->enc_kind = FRX_ENC_UTF8;
297 48 r->enc_ready = 1;
298 } else {
299 851 ▶ if (r->raw.len < 4 && !r->eof_fed) return 1; /* wait */
Branch: 2 branches
BranchTrueFalseTotal
if (r->raw.len < 4 && !r->eof_fed) return 1; /* wait */ 18 833 851
if (r->raw.len < 4 && !r->eof_fed) return 1; /* wait */ 18 0 18
300 833 memset(&enc, 0, sizeof enc);
301 833 ▶ if (!frx_enc_detect(&enc, (const unsigned char *)(r->raw.p ? r->raw.p : ""), r->raw.len, r->oe.encoding, &r->err)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!frx_enc_detect(&enc, (const unsigned char *)(r->raw.p ? r->raw.p : ""), r->raw.len, r->oe.encoding, &r->err)) { 833 0 833
if (!frx_enc_detect(&enc, (const unsigned char *)(r->raw.p ? r->raw.p : ""), r->raw.len, r->oe.encoding, &r->err)) { 0 833 833
302 0 r->failed = 1;
303 0 return 0;
304 }
305 833 r->enc_kind = enc.kind;
306 833 r->enc_bom = enc.bom;
307 833 r->raw_pos = (size_t)enc.bom;
308 833 r->enc_ready = 1;
309 833 r->p.lex.enc_kind = enc.kind;
310 833 r->p.lex.enc_bom = enc.bom != 0;
311 833 r->p.lex.enc_override = enc.override;
312 }
313 881 return 1;
314 }
315  
316 /* raw bytes become UTF-8 in `in`; a partial unit waits for the next feed */
317 static int
318 4230 frx_reader_transcode(frx_reader *r)
319 {
320 4230 ▶ const unsigned char *raw = (const unsigned char *)(r->raw.p ? r->raw.p : "");
Branch: 1 branch
BranchTrueFalseTotal
const unsigned char *raw = (const unsigned char *)(r->raw.p ? r->raw.p : ""); 4230 0 4230
321 4230 ▶ if (r->enc_kind == FRX_ENC_UTF8) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->enc_kind == FRX_ENC_UTF8) { 3954 276 4230
322 3954 ▶ if (r->raw_pos < r->raw.len) frx_buf_append_n(&r->in, (const char *)raw + r->raw_pos, r->raw.len - r->raw_pos);
Branch: 1 branch
BranchTrueFalseTotal
if (r->raw_pos < r->raw.len) frx_buf_append_n(&r->in, (const char *)raw + r->raw_pos, r->raw.len - r->raw_pos); 3945 9 3954
323 3954 r->raw_pos = r->raw.len;
324 } else {
325 548 ▶ while (r->raw_pos < r->raw.len) {
Branch: 1 branch
BranchTrueFalseTotal
while (r->raw_pos < r->raw.len) { 410 138 548
326 unsigned long cp;
327 410 const char *what = NULL;
328 unsigned char u8[4];
329 size_t n, k;
330 410 n = frx_enc_step(r->enc_kind, raw, r->raw.len, r->raw_pos, &cp, &what);
331 410 ▶ if (!n) {
Branch: 1 branch
BranchTrueFalseTotal
if (!n) { 138 272 410
332 /* a unit the feed cut: wait unless the input is complete */
333 138 ▶ if (!r->eof_fed && r->raw.len - r->raw_pos < 4) break;
Branch: 2 branches
BranchTrueFalseTotal
if (!r->eof_fed && r->raw.len - r->raw_pos < 4) break; 138 0 138
if (!r->eof_fed && r->raw.len - r->raw_pos < 4) break; 138 0 138
334 0 return FRX_READER_FAIL(r, FRX_E_ENCODING, r->raw_pos, what);
335 }
336 272 k = frx_utf8_encode(cp, u8);
337 272 frx_buf_append_n(&r->in, (const char *)u8, k);
338 272 r->raw_pos += n;
339 }
340 }
341 4230 ▶ if (r->in.failed) return FRX_READER_FAIL(r, FRX_E_NOMEM, 0, "out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (r->in.failed) return FRX_READER_FAIL(r, FRX_E_NOMEM, 0, "out of memory"); 0 4230 4230
342 /* what was transcoded is not needed again */
343 4230 ▶ if (r->raw_pos > 0 && r->raw_pos == r->raw.len) { r->raw.len = 0; r->raw_pos = 0; }
Branch: 2 branches
BranchTrueFalseTotal
if (r->raw_pos > 0 && r->raw_pos == r->raw.len) { r->raw.len = 0; r->raw_pos = 0; } 4083 147 4230
if (r->raw_pos > 0 && r->raw_pos == r->raw.len) { r->raw.len = 0; r->raw_pos = 0; } 4083 0 4083
344 147 ▶ else if (r->raw_pos > FRX_READER_COMPACT) {
Branch: 1 branch
BranchTrueFalseTotal
else if (r->raw_pos > FRX_READER_COMPACT) { 0 147 147
345 0 memmove(r->raw.p, r->raw.p + r->raw_pos, r->raw.len - r->raw_pos);
346 0 r->raw.len -= r->raw_pos;
347 0 r->raw_pos = 0;
348 }
349 4230 return 1;
350 }
351  
352 /* bytes arrive; eof says these are the last */
353 static int
354 4250 frx_reader_feed(frx_reader *r, const char *bytes, size_t len, int eof)
355 {
356 4250 ▶ if (r->failed) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (r->failed) return 0; 0 4250 4250
357 4250 ▶ if (r->eof_fed) return FRX_READER_FAIL(r, FRX_E_SYNTAX, r->dropped + r->in.len, "fed after eof");
Branch: 1 branch
BranchTrueFalseTotal
if (r->eof_fed) return FRX_READER_FAIL(r, FRX_E_SYNTAX, r->dropped + r->in.len, "fed after eof"); 2 4248 4250
358 4248 ▶ if (r->oe.base.max_bytes && r->dropped + r->in.len + r->raw.len + len > r->oe.base.max_bytes)
Branch: 2 branches
BranchTrueFalseTotal
if (r->oe.base.max_bytes && r->dropped + r->in.len + r->raw.len + len > r->oe.base.max_bytes) 0 4248 4248
if (r->oe.base.max_bytes && r->dropped + r->in.len + r->raw.len + len > r->oe.base.max_bytes) 0 0 0
359 0 return FRX_READER_FAIL(r, FRX_E_TOO_LARGE, r->oe.base.max_bytes, "input exceeds max_bytes");
360 4248 ▶ if (len) frx_buf_append_n(&r->raw, bytes, len);
Branch: 1 branch
BranchTrueFalseTotal
if (len) frx_buf_append_n(&r->raw, bytes, len); 4239 9 4248
361 4248 ▶ if (r->raw.failed) return FRX_READER_FAIL(r, FRX_E_NOMEM, 0, "out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (r->raw.failed) return FRX_READER_FAIL(r, FRX_E_NOMEM, 0, "out of memory"); 0 4248 4248
362 4248 ▶ if (eof) r->eof_fed = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (eof) r->eof_fed = 1; 55 4193 4248
363 4248 ▶ if (!frx_reader_choose_encoding(r)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_reader_choose_encoding(r)) return 0; 0 4248 4248
364 4248 ▶ if (r->enc_ready && !frx_reader_transcode(r)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (r->enc_ready && !frx_reader_transcode(r)) return 0; 4230 18 4248
if (r->enc_ready && !frx_reader_transcode(r)) return 0; 0 4230 4230
365 4248 ▶ if (r->enc_ready && !r->p.lex.arena) {
Branch: 2 branches
BranchTrueFalseTotal
if (r->enc_ready && !r->p.lex.arena) { 4230 18 4248
if (r->enc_ready && !r->p.lex.arena) { 881 3349 4230
366 /* the lexer exists from the first bytes; the prolog runs when there are enough */
367 881 frx_lex_init_stream(&r->p.lex, &r->oe.base, r->profile, &r->arena, &r->scratch,
368 881 r->enc_kind, r->enc_bom != 0, r->oe.encoding != NULL);
369 881 r->p.lex.prolog_max_bytes = 0; /* applied above, on the stream */
370 881 frx_parse_wire_lex(&r->p, &r->oe);
371 }
372 4248 ▶ if (r->p.lex.arena) frx_reader_sync_lex(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r->p.lex.arena) frx_reader_sync_lex(r); 4230 18 4248
373 4248 return 1;
374 }
375  
376 /* consumed bytes leave the buffer; only between tokens of the document frame */
377 static void
378 7109059 frx_reader_compact(frx_reader *r)
379 {
380 7109059 frx_lex *l = &r->p.lex;
381 7109059 ▶ if (l->n_frames || l->pos < FRX_READER_COMPACT || !r->in.p) return;
Branch: 3 branches
BranchTrueFalseTotal
if (l->n_frames || l->pos < FRX_READER_COMPACT || !r->in.p) return; 7108965 94 7109059
if (l->n_frames || l->pos < FRX_READER_COMPACT || !r->in.p) return; 1719 7107246 7108965
if (l->n_frames || l->pos < FRX_READER_COMPACT || !r->in.p) return; 0 1719 1719
382 1719 memmove(r->in.p, r->in.p + l->pos, r->in.len - l->pos);
383 1719 r->in.len -= l->pos;
384 1719 r->dropped += l->pos;
385 1719 l->pos = 0;
386 1719 l->base = r->dropped;
387 1719 frx_reader_sync_lex(r);
388 }
389  
390 /* ---- events -------------------------------------------------------------- */
391  
392 static int
393 7113285 frx_reader_pump(frx_reader *r)
394 {
395 int step;
396 7113285 ▶ if (!r->p.lex.arena) return FRX_STEP_MORE; /* no bytes yet */
Branch: 1 branch
BranchTrueFalseTotal
if (!r->p.lex.arena) return FRX_STEP_MORE; /* no bytes yet */ 829 7112456 7113285
397 7112456 ▶ if (r->p.depth == 1 && !r->have_mark && !r->capturing) {
Branch: 3 branches
BranchTrueFalseTotal
if (r->p.depth == 1 && !r->have_mark && !r->capturing) { 1562543 5549913 7112456
if (r->p.depth == 1 && !r->have_mark && !r->capturing) { 820845 741698 1562543
if (r->p.depth == 1 && !r->have_mark && !r->capturing) { 820845 0 820845
398 820845 frx_arena_mark_get(&r->scratch, &r->mark);
399 820845 r->have_mark = 1;
400 }
401 7112456 frx_reader_sync_lex(r);
402 7112456 step = frx_parse_step(&r->p);
403 7112456 ▶ if (step == FRX_STEP_FAIL) { r->err = r->p.err; r->failed = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (step == FRX_STEP_FAIL) { r->err = r->p.err; r->failed = 1; } 8 7112448 7112456
404 7112448 ▶ else if (step == FRX_STEP_OK) frx_reader_compact(r);
Branch: 1 branch
BranchTrueFalseTotal
else if (step == FRX_STEP_OK) frx_reader_compact(r); 7109059 3389 7112448
405 7112456 return step;
406 }
407  
408 /* the next event into *ev: 1, or 0 when more bytes are needed, or -1 at
409 * the end, or -2 while a subtree is being captured (a caller's mistake,
410 * not the stream's: the reader is not failed); r->failed with r->err on
411 * a refusal */
412 static int
413 1785817 frx_reader_next(frx_reader *r, frx_event *ev)
414 {
415 1785817 ▶ if (r->failed) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (r->failed) return 0; 0 1785817 1785817
416 1785817 ▶ if (r->capturing) return -2;
Branch: 1 branch
BranchTrueFalseTotal
if (r->capturing) return -2; 2 1785815 1785817
417 1785815 ▶ if (r->pending_release) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->pending_release) { 820812 965003 1785815
418 820812 frx_arena_release(&r->scratch, &r->mark);
419 820812 r->have_mark = 0;
420 820812 r->pending_release = 0;
421 }
422 for (;;) {
423 3569667 ▶ if (r->qn) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->qn) { 1782832 1786835 3569667
424 1782832 r->cur = r->q[r->qh];
425 1782832 r->qh = (r->qh + 1) % FRX_READER_QUEUE;
426 1782832 r->qn--;
427 1782832 r->have_cur = 1;
428 1782832 *ev = r->cur;
429 1782832 ▶ if (r->cur.kind == FRX_END && r->cur.depth <= 2 && r->have_mark) r->pending_release = 1;
Branch: 3 branches
BranchTrueFalseTotal
if (r->cur.kind == FRX_END && r->cur.depth <= 2 && r->have_mark) r->pending_release = 1; 100088 1682744 1782832
if (r->cur.kind == FRX_END && r->cur.depth <= 2 && r->have_mark) r->pending_release = 1; 60082 40006 100088
if (r->cur.kind == FRX_END && r->cur.depth <= 2 && r->have_mark) r->pending_release = 1; 60082 0 60082
430 1782832 return 1;
431 }
432 1786835 ▶ if (r->done) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (r->done) return -1; 40 1786795 1786835
433 {
434 1786795 int step = frx_reader_pump(r);
435 1786795 ▶ if (step == FRX_STEP_FAIL) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (step == FRX_STEP_FAIL) return 0; 8 1786787 1786795
436 1786787 ▶ if (step == FRX_STEP_MORE) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (step == FRX_STEP_MORE) return 0; 2935 1783852 1786787
437 1783852 ▶ if (step == FRX_STEP_EOF) {
Branch: 1 branch
BranchTrueFalseTotal
if (step == FRX_STEP_EOF) { 40 1783812 1783852
438 40 r->done = 1;
439 40 ▶ if (!frx_parse_finish(&r->p, r->dropped + r->in.len)) { r->err = r->p.err; r->failed = 1; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_parse_finish(&r->p, r->dropped + r->in.len)) { r->err = r->p.err; r->failed = 1; return 0; } 0 40 40
440 }
441 }
442 }
443 }
444  
445 /* a Document for the element of the current start event: 1 with *out,
446 * 0 when more bytes are needed (feed, call again), -2 when the current
447 * event is not a start (the reader is not failed), and r->failed on a
448 * refusal */
449 static int
450 762001 frx_reader_subtree(frx_reader *r, frx_doc **out)
451 {
452 762001 *out = NULL;
453 762001 ▶ if (r->failed) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (r->failed) return 0; 0 762001 762001
454 762001 ▶ if (!r->capturing) {
Branch: 1 branch
BranchTrueFalseTotal
if (!r->capturing) { 760758 1243 762001
455 frx_doc *d;
456 frx_ev_start ev;
457 760760 ▶ if (!r->have_cur || r->cur.kind != FRX_START) return -2;
Branch: 2 branches
BranchTrueFalseTotal
if (!r->have_cur || r->cur.kind != FRX_START) return -2; 760756 2 760758
if (!r->have_cur || r->cur.kind != FRX_START) return -2; 0 760756 760756
458 760756 d = frx_doc_new();
459 760756 ▶ if (!d) return FRX_READER_FAIL(r, FRX_E_NOMEM, r->cur.offset, "out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!d) return FRX_READER_FAIL(r, FRX_E_NOMEM, r->cur.offset, "out of memory"); 0 760756 760756
460 760756 d->profile = r->profile;
461 760756 d->max_depth = r->p.max_depth;
462 760756 d->document = frx_node_new(d, FRX_DOCUMENT, 0);
463 760756 ▶ if (!d->document) { frx_doc_free(d); return FRX_READER_FAIL(r, FRX_E_NOMEM, r->cur.offset, "out of memory"); }
Branch: 1 branch
BranchTrueFalseTotal
if (!d->document) { frx_doc_free(d); return FRX_READER_FAIL(r, FRX_E_NOMEM, r->cur.offset, "out of memory"); } 0 760756 760756
464 760756 frx_tree_sink_init(&r->cap_sink, d, 1);
465 760756 r->cap_doc = d;
466 760756 r->cap_depth = r->cur.depth + 1;
467 760756 memset(&ev, 0, sizeof ev);
468 760756 ev.qname = r->cur.qname; ev.prefix = r->cur.prefix; ev.local = r->cur.local; ev.ns = r->cur.ns;
469 760756 ev.attrs = r->cur.attrs; ev.n_attrs = r->cur.n_attrs;
470 760756 ev.decls = r->cur.decls; ev.n_decls = r->cur.n_decls;
471 760756 ev.scope = r->cur.scope; ev.offset = r->cur.offset; ev.empty = r->cur.empty;
472 760756 ▶ if (!frx_tree_sink_start(&r->p, &r->cap_sink, &ev)) { r->err = r->p.err; r->failed = 1; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_tree_sink_start(&r->p, &r->cap_sink, &ev)) { r->err = r->p.err; r->failed = 1; return 0; } 0 760756 760756
473 760756 ▶ if (r->cur.empty) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->cur.empty) { 2 760754 760756
474 2 *out = d;
475 2 r->cap_doc = NULL;
476 2 frx_tree_sink_free(&r->cap_sink);
477 2 ▶ if (r->cap_depth <= 2 && r->have_mark) r->pending_release = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (r->cap_depth <= 2 && r->have_mark) r->pending_release = 1; 2 0 2
if (r->cap_depth <= 2 && r->have_mark) r->pending_release = 1; 2 0 2
478 2 return 1;
479 }
480 760754 r->capturing = 1;
481 760754 r->p.sink = &FRX_TREE_SINK;
482 760754 r->p.ud = &r->cap_sink;
483 760754 r->p.split_entities = 0;
484 }
485 4564493 for (;;) {
486 5326490 int step = frx_reader_pump(r);
487 5326490 ▶ if (step == FRX_STEP_FAIL) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (step == FRX_STEP_FAIL) return 0; 0 5326490 5326490
488 5326490 ▶ if (step == FRX_STEP_MORE) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (step == FRX_STEP_MORE) return 0; 1243 5325247 5326490
489 5325247 ▶ if (step == FRX_STEP_EOF) {
Branch: 1 branch
BranchTrueFalseTotal
if (step == FRX_STEP_EOF) { 0 5325247 5325247
490 0 r->done = 1;
491 0 return FRX_READER_FAIL(r, FRX_E_SYNTAX, r->dropped + r->in.len, "the input ended inside the subtree");
492 }
493 5325247 ▶ if (r->p.depth < r->cap_depth) break; /* its end tag has been read */
Branch: 1 branch
BranchTrueFalseTotal
if (r->p.depth < r->cap_depth) break; /* its end tag has been read */ 760754 4564493 5325247
494 }
495 760754 r->capturing = 0;
496 760754 r->p.sink = &FRX_READER_SINK;
497 760754 r->p.ud = r;
498 760754 r->p.split_entities = 1;
499 760754 *out = r->cap_doc;
500 760754 r->cap_doc = NULL;
501 760754 frx_tree_sink_free(&r->cap_sink);
502 760754 ▶ if (r->cap_depth <= 2 && r->have_mark) r->pending_release = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (r->cap_depth <= 2 && r->have_mark) r->pending_release = 1; 760752 2 760754
if (r->cap_depth <= 2 && r->have_mark) r->pending_release = 1; 760752 0 760752
503 760754 return 1;
504 }
505  
506 #endif /* FRX_READER_H */
« include/frx/frx_uri.h ↑ Summary include/frx/frx_enc.h »