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. frx_reader is opaque in
43 * frx_abi.h and defined here: the typedef is written once, there, since
44 * repeating it is a C11 feature and a C89 compiler refuses it. */
45  
46 typedef struct frx_event {
47 int kind;
48 frx_str qname;
49 frx_str prefix;
50 frx_str local;
51 frx_str ns;
52 frx_str value; /* text, comment body, pi data */
53 frx_str entity; /* text: the entity it came from; "" for the document */
54 const frx_attr *attrs;
55 int n_attrs;
56 const frx_nsdecl *decls;
57 int n_decls;
58 const frx_scope *scope;
59 size_t offset;
60 int depth; /* elements open before this event; a start's own depth is depth + 1 */
61 int empty; /* start: <a/> */
62 const frx_doctype *doctype;
63 } frx_event;
64  
65 #define FRX_READER_QUEUE 8
66 #define FRX_READER_COMPACT (64u * 1024u)
67  
68 struct frx_reader {
69 frx_opts_ex oe;
70 int profile;
71 frx_arena arena; /* persistent */
72 frx_arena scratch; /* per record */
73 frx_parse p;
74 frx_buf raw; /* fed bytes not yet transcoded (a non-UTF-8 stream) */
75 size_t raw_pos;
76 frx_buf in; /* what the lexer reads */
77 size_t dropped; /* bytes dropped from the front of in */
78 int eof_fed;
79 int enc_ready; /* the encoding has been chosen */
80 int enc_kind;
81 int enc_bom;
82 frx_event q[FRX_READER_QUEUE];
83 int qh, qn;
84 frx_event cur; /* the event last handed out */
85 int have_cur;
86 int done;
87 int failed;
88 frx_err err;
89 frx_arena_mark mark;
90 int have_mark;
91 int pending_release;
92 /* subtree capture */
93 int capturing;
94 int cap_depth; /* the captured element's own depth */
95 frx_doc *cap_doc;
96 frx_tree_sink cap_sink;
97 };
98  
99 #define FRX_READER_FAIL(r, c, off, what) \
100 (frx_err_set(&(r)->err, (c), (off), (what)), (r)->failed = 1, 0)
101  
102 /* ---- the queue sink ------------------------------------------------------ */
103  
104 static frx_event *
105 1782832 frx_reader_enqueue(frx_reader *r)
106 {
107 frx_event *e;
108 1782832 ▶ if (r->qn == FRX_READER_QUEUE) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (r->qn == FRX_READER_QUEUE) return NULL; 0 1782832 1782832
109 1782832 e = &r->q[(r->qh + r->qn) % FRX_READER_QUEUE];
110 1782832 r->qn++;
111 1782832 memset(e, 0, sizeof *e);
112 1782832 e->qname = e->prefix = e->local = e->ns = e->value = e->entity = FRX_EMPTY_STR;
113 1782832 return e;
114 }
115  
116 static int
117 860924 frx_reader_sink_start(frx_parse *p, void *ud, const frx_ev_start *ev)
118 {
119 860924 frx_reader *r = (frx_reader *)ud;
120 860924 frx_event *e = frx_reader_enqueue(r);
121 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
122 860924 e->kind = FRX_START;
123 860924 e->qname = ev->qname;
124 860924 e->prefix = ev->prefix;
125 860924 e->local = ev->local;
126 860924 e->ns = ev->ns;
127 860924 e->attrs = ev->attrs;
128 860924 e->n_attrs = ev->n_attrs;
129 860924 e->decls = ev->decls;
130 860924 e->n_decls = ev->n_decls;
131 860924 e->scope = ev->scope;
132 860924 e->offset = ev->offset;
133 860924 e->depth = p->depth;
134 860924 e->empty = ev->empty;
135 860924 return 1;
136 }
137  
138 static int
139 100088 frx_reader_sink_end(frx_parse *p, void *ud, const frx_str *qname, size_t offset)
140 {
141 100088 frx_reader *r = (frx_reader *)ud;
142 100088 frx_event *e = frx_reader_enqueue(r);
143 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
144 100088 e->kind = FRX_END;
145 100088 e->qname = *qname;
146 100088 frx_ns_split(&e->qname, &e->prefix, &e->local);
147 100088 e->offset = offset;
148 100088 e->depth = p->depth + 1; /* the driver has popped it; report its own depth */
149 100088 return 1;
150 }
151  
152 static int
153 820898 frx_reader_sink_text(frx_parse *p, void *ud, const char *s, size_t n,
154 const frx_span *spans, int n_spans, size_t offset, const frx_str *entity,
155 int from_ref)
156 {
157 820898 frx_reader *r = (frx_reader *)ud;
158 820898 frx_event *e = frx_reader_enqueue(r);
159 (void)spans; (void)n_spans; (void)from_ref;
160 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
161 820898 e->kind = FRX_TEXT;
162 820898 e->value = frx_arena_strndup(&r->scratch, s, n); /* the driver's buffer is reused */
163 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
164 820898 e->entity = *entity;
165 820898 e->offset = offset;
166 820898 e->depth = p->depth;
167 820898 return 1;
168 }
169  
170 static int
171 61 frx_reader_sink_comment(frx_parse *p, void *ud, const frx_str *value, size_t offset)
172 {
173 61 frx_reader *r = (frx_reader *)ud;
174 61 frx_event *e = frx_reader_enqueue(r);
175 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
176 61 e->kind = FRX_COMMENT;
177 61 e->value = *value;
178 61 e->offset = offset;
179 61 e->depth = p->depth;
180 61 return 1;
181 }
182  
183 static int
184 36 frx_reader_sink_pi(frx_parse *p, void *ud, const frx_str *target, const frx_str *data, size_t offset)
185 {
186 36 frx_reader *r = (frx_reader *)ud;
187 36 frx_event *e = frx_reader_enqueue(r);
188 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
189 36 e->kind = FRX_PI;
190 36 e->local = *target;
191 36 e->qname = *target;
192 36 e->value = *data;
193 36 e->offset = offset;
194 36 e->depth = p->depth;
195 36 return 1;
196 }
197  
198 static int
199 825 frx_reader_sink_doctype(frx_parse *p, void *ud, const frx_doctype *dt, const frx_dtd *dtd)
200 {
201 825 frx_reader *r = (frx_reader *)ud;
202 825 frx_event *e = frx_reader_enqueue(r);
203 (void)dtd;
204 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
205 825 e->kind = FRX_DOCTYPE;
206 825 e->qname = dt->name;
207 825 e->local = dt->name;
208 825 e->doctype = dt;
209 825 e->offset = dt->offset;
210 825 e->depth = 0;
211 825 return 1;
212 }
213  
214 static const frx_sink FRX_READER_SINK = {
215 frx_reader_sink_start, frx_reader_sink_end, frx_reader_sink_text,
216 frx_reader_sink_comment, frx_reader_sink_pi, frx_reader_sink_doctype,
217 NULL
218 };
219  
220 /* ---- lifetime ------------------------------------------------------------- */
221  
222 static void
223 885 frx_reader_init(frx_reader *r, const frx_opts_ex *oe_in)
224 {
225 885 memset(r, 0, sizeof *r);
226 885 frx_opts_ex_norm(oe_in, &r->oe);
227 885 r->profile = r->oe.profile == FRX_PROFILE_FULL ? FRX_PROFILE_FULL : FRX_PROFILE_STRICT;
228 885 frx_arena_init(&r->arena);
229 885 frx_arena_init(&r->scratch);
230 885 frx_buf_init(&r->raw);
231 885 frx_buf_init(&r->in);
232 885 frx_parse_init(&r->p, &r->oe, &r->arena, &r->scratch, &FRX_READER_SINK, r);
233 885 r->p.split_entities = 1;
234 885 frx_err_set(&r->err, FRX_OK, 0, NULL);
235 885 r->cur.qname = r->cur.prefix = r->cur.local = r->cur.ns = r->cur.value = r->cur.entity = FRX_EMPTY_STR;
236 885 }
237  
238 /* A reader on the heap, for a caller that cannot see the struct: the C
239 * ABI hands out a pointer and takes it back, and nothing else needs
240 * these two. */
241 static frx_reader *
242 2 frx_reader_new(const frx_opts_ex *oe)
243 {
244 2 frx_reader *r = (frx_reader *)malloc(sizeof *r);
245 2 ▶ if (r) frx_reader_init(r, oe);
Branch: 1 branch
BranchTrueFalseTotal
if (r) frx_reader_init(r, oe); 2 0 2
246 2 return r;
247 }
248  
249 static void
250 885 frx_reader_free(frx_reader *r)
251 {
252 885 frx_parse_free(&r->p);
253 885 frx_tree_sink_free(&r->cap_sink);
254 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
255 885 frx_buf_free(&r->raw);
256 885 frx_buf_free(&r->in);
257 885 frx_arena_free(&r->scratch);
258 885 frx_arena_free(&r->arena);
259 885 }
260  
261 /* the other half of frx_reader_new: release what the reader holds and
262 * then the reader itself */
263 static void
264 2 frx_reader_destroy(frx_reader *r)
265 {
266 2 ▶ if (!r) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return; 0 2 2
267 2 frx_reader_free(r);
268 2 free(r);
269 }
270  
271 /* ---- input --------------------------------------------------------------- */
272  
273 /* the lexer's view of the document frame follows the buffer */
274 static void
275 7118411 frx_reader_sync_lex(frx_reader *r)
276 {
277 7118411 frx_lex *l = &r->p.lex;
278 7118411 ▶ 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 : ""); 7118411 0 7118411
279 7118411 ▶ if (l->n_frames) {
Branch: 1 branch
BranchTrueFalseTotal
if (l->n_frames) { 94 7118317 7118411
280 94 l->frames[0].in = in;
281 94 l->frames[0].len = r->in.len;
282 } else {
283 7118317 l->in = in;
284 7118317 l->len = r->in.len;
285 }
286 7118411 l->doc_len = r->dropped + r->in.len;
287 7118411 ▶ 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 7117843 7118411
l->eof = r->eof_fed && r->raw_pos >= r->raw.len; 568 0 568
288 7118411 }
289  
290 /* the encoding from the first bytes; once, when there are enough of them
291 * or the input is complete */
292 static int
293 4251 frx_reader_choose_encoding(frx_reader *r)
294 {
295 frx_enc enc;
296 4251 ▶ if (r->enc_ready) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (r->enc_ready) return 1; 3352 899 4251
297 899 ▶ if (r->profile != FRX_PROFILE_FULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->profile != FRX_PROFILE_FULL) { 48 851 899
298 48 r->enc_kind = FRX_ENC_UTF8;
299 48 r->enc_ready = 1;
300 } else {
301 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
302 833 memset(&enc, 0, sizeof enc);
303 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
304 0 r->failed = 1;
305 0 return 0;
306 }
307 833 r->enc_kind = enc.kind;
308 833 r->enc_bom = enc.bom;
309 833 r->raw_pos = (size_t)enc.bom;
310 833 r->enc_ready = 1;
311 833 r->p.lex.enc_kind = enc.kind;
312 833 r->p.lex.enc_bom = enc.bom != 0;
313 833 r->p.lex.enc_override = enc.override;
314 }
315 881 return 1;
316 }
317  
318 /* raw bytes become UTF-8 in `in`; a partial unit waits for the next feed */
319 static int
320 4233 frx_reader_transcode(frx_reader *r)
321 {
322 4233 ▶ 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 : ""); 4233 0 4233
323 4233 ▶ if (r->enc_kind == FRX_ENC_UTF8) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->enc_kind == FRX_ENC_UTF8) { 3957 276 4233
324 3957 ▶ 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); 3948 9 3957
325 3957 r->raw_pos = r->raw.len;
326 } else {
327 548 ▶ while (r->raw_pos < r->raw.len) {
Branch: 1 branch
BranchTrueFalseTotal
while (r->raw_pos < r->raw.len) { 410 138 548
328 unsigned long cp;
329 410 const char *what = NULL;
330 unsigned char u8[4];
331 size_t n, k;
332 410 n = frx_enc_step(r->enc_kind, raw, r->raw.len, r->raw_pos, &cp, &what);
333 410 ▶ if (!n) {
Branch: 1 branch
BranchTrueFalseTotal
if (!n) { 138 272 410
334 /* a unit the feed cut: wait unless the input is complete */
335 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
336 0 return FRX_READER_FAIL(r, FRX_E_ENCODING, r->raw_pos, what);
337 }
338 272 k = frx_utf8_encode(cp, u8);
339 272 frx_buf_append_n(&r->in, (const char *)u8, k);
340 272 r->raw_pos += n;
341 }
342 }
343 4233 ▶ 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 4233 4233
344 /* what was transcoded is not needed again */
345 4233 ▶ 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; } 4086 147 4233
if (r->raw_pos > 0 && r->raw_pos == r->raw.len) { r->raw.len = 0; r->raw_pos = 0; } 4086 0 4086
346 147 ▶ else if (r->raw_pos > FRX_READER_COMPACT) {
Branch: 1 branch
BranchTrueFalseTotal
else if (r->raw_pos > FRX_READER_COMPACT) { 0 147 147
347 0 memmove(r->raw.p, r->raw.p + r->raw_pos, r->raw.len - r->raw_pos);
348 0 r->raw.len -= r->raw_pos;
349 0 r->raw_pos = 0;
350 }
351 4233 return 1;
352 }
353  
354 /* bytes arrive; eof says these are the last */
355 static int
356 4253 frx_reader_feed(frx_reader *r, const char *bytes, size_t len, int eof)
357 {
358 4253 ▶ if (r->failed) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (r->failed) return 0; 0 4253 4253
359 4253 ▶ 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 4251 4253
360 4251 ▶ 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 4251 4251
if (r->oe.base.max_bytes && r->dropped + r->in.len + r->raw.len + len > r->oe.base.max_bytes) 0 0 0
361 0 return FRX_READER_FAIL(r, FRX_E_TOO_LARGE, r->oe.base.max_bytes, "input exceeds max_bytes");
362 4251 ▶ if (len) frx_buf_append_n(&r->raw, bytes, len);
Branch: 1 branch
BranchTrueFalseTotal
if (len) frx_buf_append_n(&r->raw, bytes, len); 4242 9 4251
363 4251 ▶ 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 4251 4251
364 4251 ▶ if (eof) r->eof_fed = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (eof) r->eof_fed = 1; 55 4196 4251
365 4251 ▶ if (!frx_reader_choose_encoding(r)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_reader_choose_encoding(r)) return 0; 0 4251 4251
366 4251 ▶ if (r->enc_ready && !frx_reader_transcode(r)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (r->enc_ready && !frx_reader_transcode(r)) return 0; 4233 18 4251
if (r->enc_ready && !frx_reader_transcode(r)) return 0; 0 4233 4233
367 4251 ▶ if (r->enc_ready && !r->p.lex.arena) {
Branch: 2 branches
BranchTrueFalseTotal
if (r->enc_ready && !r->p.lex.arena) { 4233 18 4251
if (r->enc_ready && !r->p.lex.arena) { 881 3352 4233
368 /* the lexer exists from the first bytes; the prolog runs when there are enough */
369 881 frx_lex_init_stream(&r->p.lex, &r->oe.base, r->profile, &r->arena, &r->scratch,
370 881 r->enc_kind, r->enc_bom != 0, r->oe.encoding != NULL);
371 881 r->p.lex.prolog_max_bytes = 0; /* applied above, on the stream */
372 881 frx_parse_wire_lex(&r->p, &r->oe);
373 }
374 4251 ▶ if (r->p.lex.arena) frx_reader_sync_lex(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r->p.lex.arena) frx_reader_sync_lex(r); 4233 18 4251
375 4251 return 1;
376 }
377  
378 /* consumed bytes leave the buffer; only between tokens of the document frame */
379 static void
380 7109059 frx_reader_compact(frx_reader *r)
381 {
382 7109059 frx_lex *l = &r->p.lex;
383 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
384 1719 memmove(r->in.p, r->in.p + l->pos, r->in.len - l->pos);
385 1719 r->in.len -= l->pos;
386 1719 r->dropped += l->pos;
387 1719 l->pos = 0;
388 1719 l->base = r->dropped;
389 1719 frx_reader_sync_lex(r);
390 }
391  
392 /* ---- events -------------------------------------------------------------- */
393  
394 static int
395 7113288 frx_reader_pump(frx_reader *r)
396 {
397 int step;
398 7113288 ▶ 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 7112459 7113288
399 7112459 ▶ if (r->p.depth == 1 && !r->have_mark && !r->capturing) {
Branch: 3 branches
BranchTrueFalseTotal
if (r->p.depth == 1 && !r->have_mark && !r->capturing) { 1562551 5549908 7112459
if (r->p.depth == 1 && !r->have_mark && !r->capturing) { 820845 741706 1562551
if (r->p.depth == 1 && !r->have_mark && !r->capturing) { 820845 0 820845
400 820845 frx_arena_mark_get(&r->scratch, &r->mark);
401 820845 r->have_mark = 1;
402 }
403 7112459 frx_reader_sync_lex(r);
404 7112459 step = frx_parse_step(&r->p);
405 7112459 ▶ 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 7112451 7112459
406 7112451 ▶ 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 3392 7112451
407 7112459 return step;
408 }
409  
410 /* the next event into *ev: 1, or 0 when more bytes are needed, or -1 at
411 * the end, or -2 while a subtree is being captured (a caller's mistake,
412 * not the stream's: the reader is not failed); r->failed with r->err on
413 * a refusal */
414 static int
415 1785820 frx_reader_next(frx_reader *r, frx_event *ev)
416 {
417 1785820 ▶ if (r->failed) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (r->failed) return 0; 0 1785820 1785820
418 1785820 ▶ if (r->capturing) return -2;
Branch: 1 branch
BranchTrueFalseTotal
if (r->capturing) return -2; 2 1785818 1785820
419 1785818 ▶ if (r->pending_release) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->pending_release) { 820812 965006 1785818
420 820812 frx_arena_release(&r->scratch, &r->mark);
421 820812 r->have_mark = 0;
422 820812 r->pending_release = 0;
423 }
424 for (;;) {
425 3569670 ▶ if (r->qn) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->qn) { 1782832 1786838 3569670
426 1782832 r->cur = r->q[r->qh];
427 1782832 r->qh = (r->qh + 1) % FRX_READER_QUEUE;
428 1782832 r->qn--;
429 1782832 r->have_cur = 1;
430 1782832 *ev = r->cur;
431 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
432 1782832 return 1;
433 }
434 1786838 ▶ if (r->done) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (r->done) return -1; 40 1786798 1786838
435 {
436 1786798 int step = frx_reader_pump(r);
437 1786798 ▶ if (step == FRX_STEP_FAIL) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (step == FRX_STEP_FAIL) return 0; 8 1786790 1786798
438 1786790 ▶ if (step == FRX_STEP_MORE) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (step == FRX_STEP_MORE) return 0; 2938 1783852 1786790
439 1783852 ▶ if (step == FRX_STEP_EOF) {
Branch: 1 branch
BranchTrueFalseTotal
if (step == FRX_STEP_EOF) { 40 1783812 1783852
440 40 r->done = 1;
441 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
442 }
443 }
444 }
445 }
446  
447 /* a Document for the element of the current start event: 1 with *out,
448 * 0 when more bytes are needed (feed, call again), -2 when the current
449 * event is not a start (the reader is not failed), and r->failed on a
450 * refusal */
451 static int
452 762001 frx_reader_subtree(frx_reader *r, frx_doc **out)
453 {
454 762001 *out = NULL;
455 762001 ▶ if (r->failed) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (r->failed) return 0; 0 762001 762001
456 762001 ▶ if (!r->capturing) {
Branch: 1 branch
BranchTrueFalseTotal
if (!r->capturing) { 760758 1243 762001
457 frx_doc *d;
458 frx_ev_start ev;
459 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
460 760756 d = frx_doc_new();
461 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
462 760756 d->profile = r->profile;
463 760756 d->max_depth = r->p.max_depth;
464 760756 d->document = frx_node_new(d, FRX_DOCUMENT, 0);
465 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
466 760756 frx_tree_sink_init(&r->cap_sink, d, 1);
467 760756 r->cap_doc = d;
468 760756 r->cap_depth = r->cur.depth + 1;
469 760756 memset(&ev, 0, sizeof ev);
470 760756 ev.qname = r->cur.qname; ev.prefix = r->cur.prefix; ev.local = r->cur.local; ev.ns = r->cur.ns;
471 760756 ev.attrs = r->cur.attrs; ev.n_attrs = r->cur.n_attrs;
472 760756 ev.decls = r->cur.decls; ev.n_decls = r->cur.n_decls;
473 760756 ev.scope = r->cur.scope; ev.offset = r->cur.offset; ev.empty = r->cur.empty;
474 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
475 760756 ▶ if (r->cur.empty) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->cur.empty) { 2 760754 760756
476 2 *out = d;
477 2 r->cap_doc = NULL;
478 2 frx_tree_sink_free(&r->cap_sink);
479 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
480 2 return 1;
481 }
482 760754 r->capturing = 1;
483 760754 r->p.sink = &FRX_TREE_SINK;
484 760754 r->p.ud = &r->cap_sink;
485 760754 r->p.split_entities = 0;
486 }
487 4564493 for (;;) {
488 5326490 int step = frx_reader_pump(r);
489 5326490 ▶ if (step == FRX_STEP_FAIL) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (step == FRX_STEP_FAIL) return 0; 0 5326490 5326490
490 5326490 ▶ if (step == FRX_STEP_MORE) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (step == FRX_STEP_MORE) return 0; 1243 5325247 5326490
491 5325247 ▶ if (step == FRX_STEP_EOF) {
Branch: 1 branch
BranchTrueFalseTotal
if (step == FRX_STEP_EOF) { 0 5325247 5325247
492 0 r->done = 1;
493 0 return FRX_READER_FAIL(r, FRX_E_SYNTAX, r->dropped + r->in.len, "the input ended inside the subtree");
494 }
495 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
496 }
497 760754 r->capturing = 0;
498 760754 r->p.sink = &FRX_READER_SINK;
499 760754 r->p.ud = r;
500 760754 r->p.split_entities = 1;
501 760754 *out = r->cap_doc;
502 760754 r->cap_doc = NULL;
503 760754 frx_tree_sink_free(&r->cap_sink);
504 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
505 760754 return 1;
506 }
507  
508 #endif /* FRX_READER_H */
« include/frx/frx_uri.h ↑ Summary include/frx/frx_enc.h »