| 1 |
|
| #ifndef FRX_WRITE_H |
| 2 |
|
| #define FRX_WRITE_H |
| 3 |
|
| |
| 4 |
|
| /* The general serialiser: the tree back to markup, the way a caller |
| 5 |
|
| * asks for it. |
| 6 |
|
| * |
| 7 |
|
| * A second walker over the same tree and the same frx_buf as |
| 8 |
|
| * frx_c14n.h, sharing its escapers by call for the default case and |
| 9 |
|
| * nothing else, so that no option here can move a canonical byte. What |
| 10 |
|
| * it offers is what the canonical form fixes: a declaration or none, an |
| 11 |
|
| * output encoding, indentation, the short empty tag, the quote, the |
| 12 |
|
| * escaping of more than the minimum, the recorded DOCTYPE and CDATA |
| 13 |
|
| * sections back where they were. |
| 14 |
|
| * |
| 15 |
|
| * Everything is written as UTF-8 first and transcoded at the end. Under |
| 16 |
|
| * an encoding that cannot hold a character (ISO-8859-1 above U+00FF, |
| 17 |
|
| * US-ASCII above U+007F) the character becomes a hexadecimal reference |
| 18 |
|
| * in text and attribute values, and is a refusal in a comment, a |
| 19 |
|
| * processing instruction or a name, where a reference cannot stand. An |
| 20 |
|
| * XML 1.1 document's restricted characters are written as references |
| 21 |
|
| * for the same reason. Every string written is checked as UTF-8 and as |
| 22 |
|
| * Char under the document's version, and a comment's `--`, a PI's `?>` |
| 23 |
|
| * and a target of `xml` are refused, so an edited tree cannot |
| 24 |
|
| * write a document this parser would refuse. |
| 25 |
|
| * |
| 26 |
|
| * Indentation never touches text: an element holding any text that is |
| 27 |
|
| * not whitespace, or named in `preserve`, or under xml:space="preserve", |
| 28 |
|
| * is written as it is; an element holding whitespace-only text is left |
| 29 |
|
| * alone too unless `drop_ws` says to drop that text, which is the one |
| 30 |
|
| * lossy option and is off by default. A text node's value is already |
| 31 |
|
| * normalised (frx_lex.h) and is never normalised again: a carriage |
| 32 |
|
| * return that arrived as a reference leaves as one. |
| 33 |
|
| * |
| 34 |
|
| * Entity references were resolved at parse and are not regenerated: the |
| 35 |
|
| * text is the replacement text, and the recorded internal subset is |
| 36 |
|
| * written back as it was, so a written document that declares an entity |
| 37 |
|
| * it no longer uses is still well-formed and equivalent. A node written |
| 38 |
|
| * on its own carries the namespace bindings in scope at it, so it parses |
| 39 |
|
| * on its own. |
| 40 |
|
| * |
| 41 |
|
| * Nothing recurses (rule 2): an explicit stack of open elements, the end |
| 42 |
|
| * tags written on the way back up. |
| 43 |
|
| * |
| 44 |
|
| * Needs frx_abi.h, frx_buf.h, frx_utf8.h, frx_enc.h, frx_tree.h, |
| 45 |
|
| * frx_ns.h, frx_c14n.h. */ |
| 46 |
|
| |
| 47 |
|
| /* frx_write_opts is frx_abi.h's: the table hands it to a consumer |
| 48 |
|
| * directly, so its shape is public. */ |
| 49 |
|
| |
| 50 |
|
| static void |
| 51 |
1739 |
| frx_write_opts_init(frx_write_opts *o) |
| 52 |
|
| { |
| 53 |
1739 |
| memset(o, 0, sizeof *o); |
| 54 |
1739 |
| o->declaration = 1; |
| 55 |
1739 |
| o->encoding = FRX_ENC_UTF8; |
| 56 |
1739 |
| o->encoding_attr = 1; |
| 57 |
1739 |
| o->empty_short = 1; |
| 58 |
1739 |
| o->quote = '"'; |
| 59 |
1739 |
| o->doctype = 1; |
| 60 |
1739 |
| } |
| 61 |
|
| |
| 62 |
|
| typedef struct frx_writer { |
| 63 |
|
| frx_buf *out; /* UTF-8; the caller's buffer or a stage */ |
| 64 |
|
| const frx_write_opts *o; |
| 65 |
|
| int version11; |
| 66 |
|
| unsigned long limit; /* the largest code point the encoding holds */ |
| 67 |
|
| frx_err *err; |
| 68 |
|
| int failed; |
| 69 |
|
| } frx_writer; |
| 70 |
|
| |
| 71 |
|
| typedef struct frx_write_frame { |
| 72 |
|
| const frx_node *node; |
| 73 |
|
| int indented; |
| 74 |
|
| int preserve; |
| 75 |
|
| } frx_write_frame; |
| 76 |
|
| |
| 77 |
|
| #define FRX_WRITE_FAIL(w, off, what) \ |
| 78 |
|
| (frx_err_set((w)->err, FRX_E_SYNTAX, (off), (what)), (w)->failed = 1, 0) |
| 79 |
|
| |
| 80 |
|
| static int |
| 81 |
209540 |
| frx_write_is_char(const frx_writer *w, unsigned long cp) |
| 82 |
|
| { |
| 83 |
209540 |
▶ | return w->version11 ? frx_is_char11(cp) : frx_is_char(cp); |
Branch: 1 branch
| Branch | True | False | Total |
| return w->version11 ? frx_is_char11(cp) : frx_is_char(cp); |
10850 |
198690 |
209540 |
|
| 84 |
|
| } |
| 85 |
|
| |
| 86 |
|
| /* a character only a reference can carry: beyond the encoding, a control |
| 87 |
|
| * the version admits by reference alone, or under XML 1.1 a NEL or LS, |
| 88 |
|
| * which written literally would be a line end on the way back in */ |
| 89 |
|
| static int |
| 90 |
209479 |
| frx_write_needs_ref(const frx_writer *w, unsigned long cp) |
| 91 |
|
| { |
| 92 |
209479 |
▶ | if (cp > w->limit) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (cp > w->limit) return 1; |
9 |
209470 |
209479 |
|
| 93 |
209470 |
▶ | if (w->version11) return frx_is_restricted11(cp) || cp == 0x85 || cp == 0x2028; |
Branch: 4 branches
| Branch | True | False | Total |
| if (w->version11) return frx_is_restricted11(cp) || cp == 0x85 || cp == 0x2028; |
10846 |
198624 |
209470 |
| if (w->version11) return frx_is_restricted11(cp) || cp == 0x85 || cp == 0x2028; |
10832 |
14 |
10846 |
| if (w->version11) return frx_is_restricted11(cp) || cp == 0x85 || cp == 0x2028; |
10832 |
0 |
10832 |
| if (w->version11) return frx_is_restricted11(cp) || cp == 0x85 || cp == 0x2028; |
0 |
10832 |
10832 |
|
| 94 |
198624 |
| return 0; |
| 95 |
|
| } |
| 96 |
|
| |
| 97 |
|
| static void |
| 98 |
22 |
| frx_write_ref(frx_buf *o, unsigned long cp) |
| 99 |
|
| { |
| 100 |
|
| static const char hex[] = "0123456789ABCDEF"; |
| 101 |
|
| char tmp[8]; |
| 102 |
22 |
| int n = 0; |
| 103 |
22 |
| frx_buf_append(o, "&#x"); |
| 104 |
43 |
▶ | do { tmp[n++] = hex[cp & 15]; cp >>= 4; } while (cp); |
Branch: 1 branch
| Branch | True | False | Total |
| do { tmp[n++] = hex[cp & 15]; cp >>= 4; } while (cp); |
21 |
22 |
43 |
|
| 105 |
65 |
▶ | while (n) frx_buf_append_ch(o, tmp[--n]); |
Branch: 1 branch
| Branch | True | False | Total |
| while (n) frx_buf_append_ch(o, tmp[--n]); |
43 |
22 |
65 |
|
| 106 |
22 |
| frx_buf_append_ch(o, ';'); |
| 107 |
22 |
| } |
| 108 |
|
| |
| 109 |
|
| /* one code point at p[i]; 0 on bad UTF-8 */ |
| 110 |
|
| static size_t |
| 111 |
410989 |
| frx_write_cp(const char *p, size_t n, size_t i, unsigned long *cp) |
| 112 |
|
| { |
| 113 |
410989 |
| unsigned char c = (unsigned char)p[i]; |
| 114 |
410989 |
▶ | if (c < 0x80) { *cp = c; return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (c < 0x80) { *cp = c; return 1; } |
406304 |
4685 |
410989 |
|
| 115 |
4685 |
| return frx_utf8_decode((const unsigned char *)p + i, n - i, cp); |
| 116 |
|
| } |
| 117 |
|
| |
| 118 |
|
| /* text or an attribute value: escaped, with references for what the |
| 119 |
|
| * encoding or the version cannot hold literally */ |
| 120 |
|
| static int |
| 121 |
3830 |
| frx_write_escape(frx_writer *w, const char *p, size_t n, int in_attr, size_t off) |
| 122 |
|
| { |
| 123 |
3830 |
| const frx_write_opts *o = w->o; |
| 124 |
3830 |
| size_t i = 0; |
| 125 |
3830 |
▶ | int plain = w->limit == 0x10FFFF && !w->version11 && o->quote == '"' && !o->escape_all; |
Branch: 4 branches
| Branch | True | False | Total |
| int plain = w->limit == 0x10FFFF && !w->version11 && o->quote == '"' && !o->escape_all; |
3822 |
8 |
3830 |
| int plain = w->limit == 0x10FFFF && !w->version11 && o->quote == '"' && !o->escape_all; |
3764 |
58 |
3822 |
| int plain = w->limit == 0x10FFFF && !w->version11 && o->quote == '"' && !o->escape_all; |
2804 |
960 |
3764 |
| int plain = w->limit == 0x10FFFF && !w->version11 && o->quote == '"' && !o->escape_all; |
2801 |
3 |
2804 |
|
| 126 |
|
| |
| 127 |
3830 |
▶ | if (plain) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (plain) { |
2801 |
1029 |
3830 |
|
| 128 |
|
| /* the default case is the canonical escaping, by call */ |
| 129 |
|
| size_t k; |
| 130 |
|
| unsigned long cp; |
| 131 |
36970 |
▶ | for (k = 0; k < n; k += frx_write_cp(p, n, k, &cp)) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < n; k += frx_write_cp(p, n, k, &cp)) { |
34169 |
2801 |
36970 |
|
| 132 |
34169 |
| size_t m = frx_write_cp(p, n, k, &cp); |
| 133 |
34169 |
▶ | if (!m) return FRX_WRITE_FAIL(w, off, "not UTF-8"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!m) return FRX_WRITE_FAIL(w, off, "not UTF-8"); |
0 |
34169 |
34169 |
|
| 134 |
34169 |
▶ | if (!frx_is_char(cp)) return FRX_WRITE_FAIL(w, off, "not an XML character"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_is_char(cp)) return FRX_WRITE_FAIL(w, off, "not an XML character"); |
0 |
34169 |
34169 |
|
| 135 |
|
| } |
| 136 |
2801 |
▶ | if (in_attr) frx_c14n_esc_attr(w->out, p, n); |
Branch: 1 branch
| Branch | True | False | Total |
| if (in_attr) frx_c14n_esc_attr(w->out, p, n); |
1282 |
1519 |
2801 |
|
| 137 |
1519 |
| else frx_c14n_esc_text(w->out, p, n); |
| 138 |
2801 |
| return 1; |
| 139 |
|
| } |
| 140 |
12697 |
▶ | while (i < n) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (i < n) { |
11668 |
1029 |
12697 |
|
| 141 |
|
| unsigned long cp; |
| 142 |
11668 |
| size_t k = frx_write_cp(p, n, i, &cp); |
| 143 |
11668 |
| const char *rep = NULL; |
| 144 |
11668 |
▶ | if (!k) return FRX_WRITE_FAIL(w, off, "not UTF-8"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!k) return FRX_WRITE_FAIL(w, off, "not UTF-8"); |
0 |
11668 |
11668 |
|
| 145 |
11668 |
▶ | if (!frx_write_is_char(w, cp)) return FRX_WRITE_FAIL(w, off, "not an XML character"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_write_is_char(w, cp)) return FRX_WRITE_FAIL(w, off, "not an XML character"); |
0 |
11668 |
11668 |
|
| 146 |
11668 |
| switch (cp) { |
| 147 |
13 |
| case '&': rep = "&"; break; |
| 148 |
8 |
| case '<': rep = "<"; break; |
| 149 |
8 |
| case '>': rep = ">"; break; |
| 150 |
13 |
| case '\r': rep = "
"; break; |
| 151 |
32 |
▶ | case '"': if ((in_attr && o->quote == '"') || (!in_attr && o->escape_all)) rep = """; break; |
Branch: 4 branches
| Branch | True | False | Total |
| case '"': if ((in_attr && o->quote == '"') || (!in_attr && o->escape_all)) rep = """; break; |
19 |
13 |
32 |
| case '"': if ((in_attr && o->quote == '"') || (!in_attr && o->escape_all)) rep = """; break; |
17 |
2 |
19 |
| case '"': if ((in_attr && o->quote == '"') || (!in_attr && o->escape_all)) rep = """; break; |
13 |
17 |
30 |
| case '"': if ((in_attr && o->quote == '"') || (!in_attr && o->escape_all)) rep = """; break; |
2 |
11 |
13 |
|
| 152 |
14 |
▶ | case '\'': if ((in_attr && o->quote == '\'') || (!in_attr && o->escape_all)) rep = "'"; break; |
Branch: 4 branches
| Branch | True | False | Total |
| case '\'': if ((in_attr && o->quote == '\'') || (!in_attr && o->escape_all)) rep = "'"; break; |
8 |
6 |
14 |
| case '\'': if ((in_attr && o->quote == '\'') || (!in_attr && o->escape_all)) rep = "'"; break; |
1 |
7 |
8 |
| case '\'': if ((in_attr && o->quote == '\'') || (!in_attr && o->escape_all)) rep = "'"; break; |
6 |
1 |
7 |
| case '\'': if ((in_attr && o->quote == '\'') || (!in_attr && o->escape_all)) rep = "'"; break; |
2 |
4 |
6 |
|
| 153 |
10 |
▶ | case '\t': if (in_attr) rep = "	"; break; |
Branch: 1 branch
| Branch | True | False | Total |
| case '\t': if (in_attr) rep = "	"; break; |
3 |
7 |
10 |
|
| 154 |
548 |
▶ | case '\n': if (in_attr) rep = "
"; break; |
Branch: 1 branch
| Branch | True | False | Total |
| case '\n': if (in_attr) rep = "
"; break; |
3 |
545 |
548 |
|
| 155 |
11022 |
| default: break; |
| 156 |
|
| } |
| 157 |
11668 |
▶ | if (rep) frx_buf_append(w->out, rep); |
Branch: 1 branch
| Branch | True | False | Total |
| if (rep) frx_buf_append(w->out, rep); |
61 |
11607 |
11668 |
|
| 158 |
11607 |
▶ | else if (frx_write_needs_ref(w, cp)) frx_write_ref(w->out, cp); |
Branch: 1 branch
| Branch | True | False | Total |
| else if (frx_write_needs_ref(w, cp)) frx_write_ref(w->out, cp); |
22 |
11585 |
11607 |
|
| 159 |
11585 |
| else frx_buf_append_n(w->out, p + i, k); |
| 160 |
11668 |
| i += k; |
| 161 |
|
| } |
| 162 |
1029 |
| return 1; |
| 163 |
|
| } |
| 164 |
|
| |
| 165 |
|
| /* a comment, a PI target or data, a name: nothing here can be a |
| 166 |
|
| * reference, so every character must be writable as it is */ |
| 167 |
|
| static int |
| 168 |
2280 |
| frx_write_check(frx_writer *w, const char *p, size_t n, size_t off) |
| 169 |
|
| { |
| 170 |
2280 |
| size_t i = 0; |
| 171 |
198470 |
▶ | while (i < n) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (i < n) { |
196191 |
2279 |
198470 |
|
| 172 |
|
| unsigned long cp; |
| 173 |
196191 |
| size_t k = frx_write_cp(p, n, i, &cp); |
| 174 |
196192 |
▶ | if (!k) return FRX_WRITE_FAIL(w, off, "not UTF-8"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!k) return FRX_WRITE_FAIL(w, off, "not UTF-8"); |
0 |
196191 |
196191 |
|
| 175 |
196191 |
▶ | if (!frx_write_is_char(w, cp)) return FRX_WRITE_FAIL(w, off, "not an XML character"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_write_is_char(w, cp)) return FRX_WRITE_FAIL(w, off, "not an XML character"); |
0 |
196191 |
196191 |
|
| 176 |
196191 |
▶ | if (frx_write_needs_ref(w, cp)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_write_needs_ref(w, cp)) |
1 |
196190 |
196191 |
|
| 177 |
1 |
| return FRX_WRITE_FAIL(w, off, "a character that cannot be written in the output encoding stands where no reference can"); |
| 178 |
196190 |
| i += k; |
| 179 |
|
| } |
| 180 |
2279 |
| return 1; |
| 181 |
|
| } |
| 182 |
|
| |
| 183 |
|
| static int |
| 184 |
7620 |
| frx_write_name(frx_writer *w, const frx_str *s, size_t off) |
| 185 |
|
| { |
| 186 |
7620 |
| size_t i = 0; |
| 187 |
7620 |
| int first = 1; |
| 188 |
7620 |
▶ | if (!s->len) return FRX_WRITE_FAIL(w, off, "an empty name"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s->len) return FRX_WRITE_FAIL(w, off, "an empty name"); |
0 |
7620 |
7620 |
|
| 189 |
36677 |
▶ | while (i < s->len) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (i < s->len) { |
29057 |
7620 |
36677 |
|
| 190 |
|
| unsigned long cp; |
| 191 |
29057 |
| size_t k = frx_write_cp(s->p, s->len, i, &cp); |
| 192 |
29057 |
▶ | if (!k) return FRX_WRITE_FAIL(w, off, "a name that is not UTF-8"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!k) return FRX_WRITE_FAIL(w, off, "a name that is not UTF-8"); |
0 |
29057 |
29057 |
|
| 193 |
29057 |
▶ | if (cp > w->limit) return FRX_WRITE_FAIL(w, off, "a name that cannot be written in the output encoding"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cp > w->limit) return FRX_WRITE_FAIL(w, off, "a name that cannot be written in the output encoding"); |
0 |
29057 |
29057 |
|
| 194 |
29057 |
▶ | if (cp != ':' && (first ? !frx_is_name_start(cp) : !frx_is_name_char(cp))) |
Branch: 3 branches
| Branch | True | False | Total |
| if (cp != ':' && (first ? !frx_is_name_start(cp) : !frx_is_name_char(cp))) |
28721 |
336 |
29057 |
| if (cp != ':' && (first ? !frx_is_name_start(cp) : !frx_is_name_char(cp))) |
7620 |
21101 |
28721 |
| if (cp != ':' && (first ? !frx_is_name_start(cp) : !frx_is_name_char(cp))) |
0 |
28721 |
28721 |
|
| 195 |
0 |
| return FRX_WRITE_FAIL(w, off, "a name that is not a name"); |
| 196 |
29057 |
| first = 0; |
| 197 |
29057 |
| i += k; |
| 198 |
|
| } |
| 199 |
7620 |
| return 1; |
| 200 |
|
| } |
| 201 |
|
| |
| 202 |
|
| static void |
| 203 |
636 |
| frx_write_indent(frx_writer *w, int level) |
| 204 |
|
| { |
| 205 |
636 |
| int n = level * w->o->indent; |
| 206 |
636 |
| frx_buf_append_ch(w->out, '\n'); |
| 207 |
9475 |
▶ | while (n-- > 0) frx_buf_append_ch(w->out, ' '); |
Branch: 1 branch
| Branch | True | False | Total |
| while (n-- > 0) frx_buf_append_ch(w->out, ' '); |
8839 |
636 |
9475 |
|
| 208 |
636 |
| } |
| 209 |
|
| |
| 210 |
|
| static int |
| 211 |
1688 |
| frx_write_quoted(frx_writer *w, const frx_str *v, size_t off) |
| 212 |
|
| { |
| 213 |
1688 |
| frx_buf_append_ch(w->out, (char)w->o->quote); |
| 214 |
1688 |
▶ | if (!frx_write_escape(w, v->p, v->len, 1, off)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_write_escape(w, v->p, v->len, 1, off)) return 0; |
0 |
1688 |
1688 |
|
| 215 |
1688 |
| frx_buf_append_ch(w->out, (char)w->o->quote); |
| 216 |
1688 |
| return 1; |
| 217 |
|
| } |
| 218 |
|
| |
| 219 |
|
| static int |
| 220 |
194 |
| frx_write_decl(frx_writer *w, const frx_str *prefix, const frx_str *uri, size_t off) |
| 221 |
|
| { |
| 222 |
194 |
▶ | if (prefix->len && !frx_write_name(w, prefix, off)) return 0; /* a prefix is a name */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (prefix->len && !frx_write_name(w, prefix, off)) return 0; /* a prefix is a name */ |
130 |
64 |
194 |
| if (prefix->len && !frx_write_name(w, prefix, off)) return 0; /* a prefix is a name */ |
0 |
130 |
130 |
|
| 223 |
194 |
| frx_buf_append(w->out, " xmlns"); |
| 224 |
194 |
▶ | if (prefix->len) { frx_buf_append_ch(w->out, ':'); frx_buf_append_n(w->out, prefix->p, prefix->len); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (prefix->len) { frx_buf_append_ch(w->out, ':'); frx_buf_append_n(w->out, prefix->p, prefix->len); } |
130 |
64 |
194 |
|
| 225 |
194 |
| frx_buf_append_ch(w->out, '='); |
| 226 |
194 |
| return frx_write_quoted(w, uri, off); |
| 227 |
|
| } |
| 228 |
|
| |
| 229 |
|
| /* is prefix among the first n of the list? */ |
| 230 |
|
| static int |
| 231 |
10 |
| frx_write_prefix_seen(const frx_str *list, int n, const frx_str *prefix) |
| 232 |
|
| { |
| 233 |
|
| int i; |
| 234 |
12 |
▶ | for (i = 0; i < n; i++) if (frx_str_eq2(&list[i], prefix)) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; i < n; i++) if (frx_str_eq2(&list[i], prefix)) return 1; |
0 |
2 |
2 |
| for (i = 0; i < n; i++) if (frx_str_eq2(&list[i], prefix)) return 1; |
2 |
10 |
12 |
|
| 235 |
10 |
| return 0; |
| 236 |
|
| } |
| 237 |
|
| |
| 238 |
|
| /* <name decls attrs> or <name decls attrs/>; inherit says the bindings in |
| 239 |
|
| * scope but declared above the element are written too, which is what |
| 240 |
|
| * makes a node written on its own parse on its own */ |
| 241 |
|
| static int |
| 242 |
4219 |
| frx_write_start(frx_writer *w, const frx_node *n, int inherit, int empty) |
| 243 |
|
| { |
| 244 |
|
| int i; |
| 245 |
4219 |
▶ | if (!frx_write_name(w, &n->qname, n->offset)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_write_name(w, &n->qname, n->offset)) return 0; |
0 |
4219 |
4219 |
|
| 246 |
4219 |
| frx_buf_append_ch(w->out, '<'); |
| 247 |
4219 |
| frx_buf_append_n(w->out, n->qname.p, n->qname.len); |
| 248 |
4403 |
▶ | for (i = 0; i < n->n_decls; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n->n_decls; i++) |
184 |
4219 |
4403 |
|
| 249 |
184 |
▶ | if (!frx_write_decl(w, &n->decls[i].prefix, &n->decls[i].uri, n->offset)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_write_decl(w, &n->decls[i].prefix, &n->decls[i].uri, n->offset)) return 0; |
0 |
184 |
184 |
|
| 250 |
4219 |
▶ | if (inherit && n->scope) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (inherit && n->scope) { |
24 |
4195 |
4219 |
| if (inherit && n->scope) { |
17 |
7 |
24 |
|
| 251 |
|
| /* nearest binding of each prefix wins, as lookup walks it */ |
| 252 |
|
| const frx_scope *s; |
| 253 |
17 |
| frx_str *seen = NULL; |
| 254 |
17 |
| int n_seen = 0, cap = 0; |
| 255 |
41 |
▶ | for (s = n->scope; s; s = s->up) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (s = n->scope; s; s = s->up) { |
24 |
17 |
41 |
|
| 256 |
55 |
▶ | for (i = 0; i < s->n_decls; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < s->n_decls; i++) { |
31 |
24 |
55 |
|
| 257 |
31 |
| const frx_nsdecl *d = &s->decls[i]; |
| 258 |
31 |
| int own = 0, j; |
| 259 |
64 |
▶ | for (j = 0; j < n->n_decls; j++) if (frx_str_eq2(&n->decls[j].prefix, &d->prefix)) own = 1; |
Branch: 2 branches
| Branch | True | False | Total |
| for (j = 0; j < n->n_decls; j++) if (frx_str_eq2(&n->decls[j].prefix, &d->prefix)) own = 1; |
21 |
12 |
33 |
| for (j = 0; j < n->n_decls; j++) if (frx_str_eq2(&n->decls[j].prefix, &d->prefix)) own = 1; |
33 |
31 |
64 |
|
| 260 |
31 |
▶ | if (own || frx_write_prefix_seen(seen, n_seen, &d->prefix)) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (own || frx_write_prefix_seen(seen, n_seen, &d->prefix)) continue; |
10 |
21 |
31 |
| if (own || frx_write_prefix_seen(seen, n_seen, &d->prefix)) continue; |
0 |
10 |
10 |
|
| 261 |
10 |
▶ | if (n_seen == cap) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n_seen == cap) { |
8 |
2 |
10 |
|
| 262 |
8 |
▶ | int ncap = cap ? cap * 2 : 8; |
Branch: 1 branch
| Branch | True | False | Total |
| int ncap = cap ? cap * 2 : 8; |
0 |
8 |
8 |
|
| 263 |
8 |
| frx_str *ns = (frx_str *)realloc(seen, (size_t)ncap * sizeof *ns); |
| 264 |
8 |
▶ | if (!ns) { free(seen); return FRX_WRITE_FAIL(w, n->offset, "out of memory"); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ns) { free(seen); return FRX_WRITE_FAIL(w, n->offset, "out of memory"); } |
0 |
8 |
8 |
|
| 265 |
8 |
| seen = ns; cap = ncap; |
| 266 |
|
| } |
| 267 |
10 |
| seen[n_seen++] = d->prefix; |
| 268 |
10 |
▶ | if (!d->prefix.len && !d->uri.len) continue; /* the default unbound: nothing to say */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (!d->prefix.len && !d->uri.len) continue; /* the default unbound: nothing to say */ |
6 |
4 |
10 |
| if (!d->prefix.len && !d->uri.len) continue; /* the default unbound: nothing to say */ |
0 |
6 |
6 |
|
| 269 |
10 |
▶ | if (!frx_write_decl(w, &d->prefix, &d->uri, n->offset)) { free(seen); return 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_write_decl(w, &d->prefix, &d->uri, n->offset)) { free(seen); return 0; } |
0 |
10 |
10 |
|
| 270 |
|
| } |
| 271 |
|
| } |
| 272 |
17 |
| free(seen); |
| 273 |
|
| } |
| 274 |
5713 |
▶ | for (i = 0; i < n->n_attrs; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n->n_attrs; i++) { |
1494 |
4219 |
5713 |
|
| 275 |
1494 |
| const frx_attr *a = &n->attrs[i]; |
| 276 |
1494 |
▶ | if (!frx_write_name(w, &a->qname, a->offset)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_write_name(w, &a->qname, a->offset)) return 0; |
0 |
1494 |
1494 |
|
| 277 |
1494 |
| frx_buf_append_ch(w->out, ' '); |
| 278 |
1494 |
| frx_buf_append_n(w->out, a->qname.p, a->qname.len); |
| 279 |
1494 |
| frx_buf_append_ch(w->out, '='); |
| 280 |
1494 |
▶ | if (!frx_write_quoted(w, &a->value, a->offset)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_write_quoted(w, &a->value, a->offset)) return 0; |
0 |
1494 |
1494 |
|
| 281 |
|
| } |
| 282 |
4219 |
▶ | if (empty && w->o->empty_short) frx_buf_append(w->out, "/>"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (empty && w->o->empty_short) frx_buf_append(w->out, "/>"); |
2252 |
1967 |
4219 |
| if (empty && w->o->empty_short) frx_buf_append(w->out, "/>"); |
1700 |
552 |
2252 |
|
| 283 |
2519 |
| else frx_buf_append_ch(w->out, '>'); |
| 284 |
4219 |
| return 1; |
| 285 |
|
| } |
| 286 |
|
| |
| 287 |
|
| static void |
| 288 |
2518 |
| frx_write_end(frx_writer *w, const frx_node *n) |
| 289 |
|
| { |
| 290 |
2518 |
| frx_buf_append(w->out, "</"); |
| 291 |
2518 |
| frx_buf_append_n(w->out, n->qname.p, n->qname.len); |
| 292 |
2518 |
| frx_buf_append_ch(w->out, '>'); |
| 293 |
2518 |
| } |
| 294 |
|
| |
| 295 |
|
| /* one CDATA section over p; a ]]> inside is split into two sections */ |
| 296 |
|
| static void |
| 297 |
80 |
| frx_write_cdata(frx_writer *w, const char *p, size_t n) |
| 298 |
|
| { |
| 299 |
80 |
| size_t i, run = 0; |
| 300 |
80 |
| frx_buf_append(w->out, "<![CDATA["); |
| 301 |
1616 |
▶ | for (i = 0; i + 2 < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i + 2 < n; i++) { |
1536 |
80 |
1616 |
|
| 302 |
1536 |
▶ | if (p[i] == ']' && p[i + 1] == ']' && p[i + 2] == '>') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (p[i] == ']' && p[i + 1] == ']' && p[i + 2] == '>') { |
4 |
1532 |
1536 |
| if (p[i] == ']' && p[i + 1] == ']' && p[i + 2] == '>') { |
0 |
4 |
4 |
| if (p[i] == ']' && p[i + 1] == ']' && p[i + 2] == '>') { |
0 |
0 |
0 |
|
| 303 |
0 |
| frx_buf_append_n(w->out, p + run, i + 2 - run); |
| 304 |
0 |
| frx_buf_append(w->out, "]]><![CDATA["); |
| 305 |
0 |
| run = i + 2; |
| 306 |
0 |
| i += 1; |
| 307 |
|
| } |
| 308 |
|
| } |
| 309 |
80 |
| frx_buf_append_n(w->out, p + run, n - run); |
| 310 |
80 |
| frx_buf_append(w->out, "]]>"); |
| 311 |
80 |
| } |
| 312 |
|
| |
| 313 |
|
| /* can this span be a CDATA section: every character literal under the |
| 314 |
|
| * encoding and the version? */ |
| 315 |
|
| static int |
| 316 |
80 |
| frx_write_span_literal(frx_writer *w, const char *p, size_t n) |
| 317 |
|
| { |
| 318 |
80 |
| size_t i = 0; |
| 319 |
1761 |
▶ | while (i < n) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (i < n) { |
1681 |
80 |
1761 |
|
| 320 |
|
| unsigned long cp; |
| 321 |
1681 |
| size_t k = frx_write_cp(p, n, i, &cp); |
| 322 |
1681 |
▶ | if (!k || !frx_write_is_char(w, cp) || frx_write_needs_ref(w, cp)) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!k || !frx_write_is_char(w, cp) || frx_write_needs_ref(w, cp)) return 0; |
1681 |
0 |
1681 |
| if (!k || !frx_write_is_char(w, cp) || frx_write_needs_ref(w, cp)) return 0; |
1681 |
0 |
1681 |
| if (!k || !frx_write_is_char(w, cp) || frx_write_needs_ref(w, cp)) return 0; |
0 |
1681 |
1681 |
|
| 323 |
1681 |
| i += k; |
| 324 |
|
| } |
| 325 |
80 |
| return 1; |
| 326 |
|
| } |
| 327 |
|
| |
| 328 |
|
| /* a text node: its CDATA spans as sections, the rest escaped */ |
| 329 |
|
| static int |
| 330 |
2147 |
| frx_write_text(frx_writer *w, const frx_node *n) |
| 331 |
|
| { |
| 332 |
2147 |
| size_t at = 0; |
| 333 |
|
| int i; |
| 334 |
2227 |
▶ | for (i = 0; i < n->n_cdata_spans; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n->n_cdata_spans; i++) { |
80 |
2147 |
2227 |
|
| 335 |
80 |
| const frx_span *sp = &n->cdata_spans[i]; |
| 336 |
80 |
▶ | if (sp->offset > at && !frx_write_escape(w, n->value.p + at, sp->offset - at, 0, n->offset)) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (sp->offset > at && !frx_write_escape(w, n->value.p + at, sp->offset - at, 0, n->offset)) return 0; |
40 |
40 |
80 |
| if (sp->offset > at && !frx_write_escape(w, n->value.p + at, sp->offset - at, 0, n->offset)) return 0; |
0 |
40 |
40 |
|
| 337 |
80 |
▶ | if (frx_write_span_literal(w, n->value.p + sp->offset, sp->len)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_write_span_literal(w, n->value.p + sp->offset, sp->len)) |
80 |
0 |
80 |
|
| 338 |
80 |
| frx_write_cdata(w, n->value.p + sp->offset, sp->len); |
| 339 |
0 |
▶ | else if (!frx_write_escape(w, n->value.p + sp->offset, sp->len, 0, n->offset)) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (!frx_write_escape(w, n->value.p + sp->offset, sp->len, 0, n->offset)) |
0 |
0 |
0 |
|
| 340 |
0 |
| return 0; /* a reference has to go in: text, not CDATA */ |
| 341 |
80 |
| at = sp->offset + sp->len; |
| 342 |
|
| } |
| 343 |
2147 |
▶ | if (n->value.len > at && !frx_write_escape(w, n->value.p + at, n->value.len - at, 0, n->offset)) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (n->value.len > at && !frx_write_escape(w, n->value.p + at, n->value.len - at, 0, n->offset)) return 0; |
2102 |
45 |
2147 |
| if (n->value.len > at && !frx_write_escape(w, n->value.p + at, n->value.len - at, 0, n->offset)) return 0; |
0 |
2102 |
2102 |
|
| 344 |
2147 |
| return 1; |
| 345 |
|
| } |
| 346 |
|
| |
| 347 |
|
| static int |
| 348 |
687 |
| frx_write_comment(frx_writer *w, const frx_node *n) |
| 349 |
|
| { |
| 350 |
687 |
| const char *p = n->value.p; |
| 351 |
|
| size_t i; |
| 352 |
687 |
▶ | if (!frx_write_check(w, p, n->value.len, n->offset)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_write_check(w, p, n->value.len, n->offset)) return 0; |
1 |
686 |
687 |
|
| 353 |
35220 |
▶ | for (i = 0; i + 1 < n->value.len; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i + 1 < n->value.len; i++) |
34534 |
686 |
35220 |
|
| 354 |
34534 |
▶ | if (p[i] == '-' && p[i + 1] == '-') return FRX_WRITE_FAIL(w, n->offset, "-- inside a comment"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (p[i] == '-' && p[i + 1] == '-') return FRX_WRITE_FAIL(w, n->offset, "-- inside a comment"); |
88 |
34446 |
34534 |
| if (p[i] == '-' && p[i + 1] == '-') return FRX_WRITE_FAIL(w, n->offset, "-- inside a comment"); |
0 |
88 |
88 |
|
| 355 |
686 |
▶ | if (n->value.len && p[n->value.len - 1] == '-') return FRX_WRITE_FAIL(w, n->offset, "a comment ending in -"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (n->value.len && p[n->value.len - 1] == '-') return FRX_WRITE_FAIL(w, n->offset, "a comment ending in -"); |
682 |
4 |
686 |
| if (n->value.len && p[n->value.len - 1] == '-') return FRX_WRITE_FAIL(w, n->offset, "a comment ending in -"); |
0 |
682 |
682 |
|
| 356 |
686 |
| frx_buf_append(w->out, "<!--"); |
| 357 |
686 |
| frx_buf_append_n(w->out, p, n->value.len); |
| 358 |
686 |
| frx_buf_append(w->out, "-->"); |
| 359 |
686 |
| return 1; |
| 360 |
|
| } |
| 361 |
|
| |
| 362 |
|
| static int |
| 363 |
139 |
| frx_write_pi(frx_writer *w, const frx_node *n) |
| 364 |
|
| { |
| 365 |
|
| size_t i; |
| 366 |
139 |
▶ | if (!frx_write_name(w, &n->local, n->offset)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_write_name(w, &n->local, n->offset)) return 0; |
0 |
139 |
139 |
|
| 367 |
139 |
▶ | if (frx_lex_target_is_xml(&n->local)) return FRX_WRITE_FAIL(w, n->offset, "a processing instruction target of xml"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_lex_target_is_xml(&n->local)) return FRX_WRITE_FAIL(w, n->offset, "a processing instruction target of xml"); |
0 |
139 |
139 |
|
| 368 |
139 |
▶ | if (!frx_write_check(w, n->value.p, n->value.len, n->offset)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_write_check(w, n->value.p, n->value.len, n->offset)) return 0; |
0 |
139 |
139 |
|
| 369 |
1477 |
▶ | for (i = 0; i + 1 < n->value.len; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i + 1 < n->value.len; i++) |
1338 |
139 |
1477 |
|
| 370 |
1338 |
▶ | if (n->value.p[i] == '?' && n->value.p[i + 1] == '>') return FRX_WRITE_FAIL(w, n->offset, "?> inside a processing instruction"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (n->value.p[i] == '?' && n->value.p[i + 1] == '>') return FRX_WRITE_FAIL(w, n->offset, "?> inside a processing instruction"); |
8 |
1330 |
1338 |
| if (n->value.p[i] == '?' && n->value.p[i + 1] == '>') return FRX_WRITE_FAIL(w, n->offset, "?> inside a processing instruction"); |
0 |
8 |
8 |
|
| 371 |
139 |
| frx_buf_append(w->out, "<?"); |
| 372 |
139 |
| frx_buf_append_n(w->out, n->local.p, n->local.len); |
| 373 |
139 |
▶ | if (n->value.len) { frx_buf_append_ch(w->out, ' '); frx_buf_append_n(w->out, n->value.p, n->value.len); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (n->value.len) { frx_buf_append_ch(w->out, ' '); frx_buf_append_n(w->out, n->value.p, n->value.len); } |
96 |
43 |
139 |
|
| 374 |
139 |
| frx_buf_append(w->out, "?>"); |
| 375 |
139 |
| return 1; |
| 376 |
|
| } |
| 377 |
|
| |
| 378 |
|
| static int |
| 379 |
798 |
| frx_write_ws_only(const frx_node *n) |
| 380 |
|
| { |
| 381 |
798 |
| return frx_str_is_s(&n->value); |
| 382 |
|
| } |
| 383 |
|
| |
| 384 |
|
| /* may this element's children be laid out on their own lines? */ |
| 385 |
|
| static int |
| 386 |
4219 |
| frx_write_indentable(const frx_writer *w, const frx_node *n, int preserve) |
| 387 |
|
| { |
| 388 |
|
| const frx_node *c; |
| 389 |
4219 |
| int any = 0; |
| 390 |
4219 |
▶ | if (!w->o->indent || preserve) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!w->o->indent || preserve) return 0; |
1048 |
3171 |
4219 |
| if (!w->o->indent || preserve) return 0; |
9 |
1039 |
1048 |
|
| 391 |
1932 |
▶ | for (c = n->first_child; c; c = c->next) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (c = n->first_child; c; c = c->next) { |
1160 |
772 |
1932 |
|
| 392 |
1160 |
▶ | if (c->kind == FRX_TEXT) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c->kind == FRX_TEXT) { |
560 |
600 |
1160 |
|
| 393 |
560 |
▶ | if (!frx_write_ws_only(c) || !w->o->drop_ws) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!frx_write_ws_only(c) || !w->o->drop_ws) return 0; |
294 |
266 |
560 |
| if (!frx_write_ws_only(c) || !w->o->drop_ws) return 0; |
1 |
293 |
294 |
|
| 394 |
|
| } else { |
| 395 |
600 |
| any = 1; |
| 396 |
|
| } |
| 397 |
|
| } |
| 398 |
772 |
| return any; |
| 399 |
|
| } |
| 400 |
|
| |
| 401 |
|
| static int |
| 402 |
4219 |
| frx_write_preserved(const frx_writer *w, const frx_node *n, int inherited) |
| 403 |
|
| { |
| 404 |
4219 |
| int i, pres = inherited; |
| 405 |
5713 |
▶ | for (i = 0; i < n->n_attrs; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n->n_attrs; i++) { |
1494 |
4219 |
5713 |
|
| 406 |
1494 |
| const frx_attr *a = &n->attrs[i]; |
| 407 |
1494 |
▶ | if (frx_str_eq(&a->ns, FRX_XML_NS, sizeof FRX_XML_NS - 1) && frx_str_eq(&a->local, "space", 5)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (frx_str_eq(&a->ns, FRX_XML_NS, sizeof FRX_XML_NS - 1) && frx_str_eq(&a->local, "space", 5)) |
89 |
1405 |
1494 |
| if (frx_str_eq(&a->ns, FRX_XML_NS, sizeof FRX_XML_NS - 1) && frx_str_eq(&a->local, "space", 5)) |
14 |
75 |
89 |
|
| 408 |
14 |
| pres = frx_str_eq(&a->value, "preserve", 8); |
| 409 |
|
| } |
| 410 |
4228 |
▶ | for (i = 0; i < w->o->n_preserve; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < w->o->n_preserve; i++) |
9 |
4219 |
4228 |
|
| 411 |
9 |
▶ | if (frx_str_eq(&n->local, w->o->preserve[i], strlen(w->o->preserve[i]))) pres = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_str_eq(&n->local, w->o->preserve[i], strlen(w->o->preserve[i]))) pres = 1; |
1 |
8 |
9 |
|
| 412 |
4219 |
| return pres; |
| 413 |
|
| } |
| 414 |
|
| |
| 415 |
|
| /* the subtree under apex, or every top-level node of a document apex */ |
| 416 |
|
| static int |
| 417 |
1735 |
| frx_write_walk(frx_writer *w, const frx_node *apex) |
| 418 |
|
| { |
| 419 |
1735 |
| frx_write_frame *stack = NULL; |
| 420 |
1735 |
| int sp = 0, cap = 0, level = 0; |
| 421 |
1735 |
| int top_level = apex->kind == FRX_DOCUMENT; |
| 422 |
1735 |
| int first_top = 1; |
| 423 |
1735 |
▶ | const frx_node *n = top_level ? apex->first_child : apex; |
Branch: 1 branch
| Branch | True | False | Total |
| const frx_node *n = top_level ? apex->first_child : apex; |
1710 |
25 |
1735 |
|
| 424 |
|
| |
| 425 |
7431 |
▶ | while (n) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (n) { |
7430 |
1 |
7431 |
|
| 426 |
7430 |
▶ | int parent_indented = sp ? stack[sp - 1].indented : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| int parent_indented = sp ? stack[sp - 1].indented : 0; |
4966 |
2464 |
7430 |
|
| 427 |
7430 |
▶ | int parent_preserve = sp ? stack[sp - 1].preserve : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| int parent_preserve = sp ? stack[sp - 1].preserve : 0; |
4966 |
2464 |
7430 |
|
| 428 |
7430 |
| int ok = 1; |
| 429 |
|
| |
| 430 |
7430 |
▶ | if (!sp && top_level) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!sp && top_level) { |
2464 |
4966 |
7430 |
| if (!sp && top_level) { |
2439 |
25 |
2464 |
|
| 431 |
2439 |
▶ | if (!first_top) frx_buf_append_ch(w->out, '\n'); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!first_top) frx_buf_append_ch(w->out, '\n'); |
730 |
1709 |
2439 |
|
| 432 |
2439 |
| first_top = 0; |
| 433 |
4991 |
▶ | } else if (parent_indented && n->kind != FRX_TEXT) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (parent_indented && n->kind != FRX_TEXT) { |
676 |
4315 |
4991 |
| } else if (parent_indented && n->kind != FRX_TEXT) { |
438 |
238 |
676 |
|
| 434 |
438 |
| frx_write_indent(w, level); |
| 435 |
|
| } |
| 436 |
|
| |
| 437 |
7430 |
| switch (n->kind) { |
| 438 |
4219 |
| case FRX_ELEMENT: { |
| 439 |
4219 |
| int pres = frx_write_preserved(w, n, parent_preserve); |
| 440 |
4219 |
| int ind = frx_write_indentable(w, n, pres); |
| 441 |
4219 |
▶ | ok = frx_write_start(w, n, n == apex && !top_level, n->first_child == NULL); |
Branch: 2 branches
| Branch | True | False | Total |
| ok = frx_write_start(w, n, n == apex && !top_level, n->first_child == NULL); |
24 |
4195 |
4219 |
| ok = frx_write_start(w, n, n == apex && !top_level, n->first_child == NULL); |
24 |
0 |
24 |
|
| 442 |
4219 |
▶ | if (ok && n->first_child) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (ok && n->first_child) { |
4219 |
0 |
4219 |
| if (ok && n->first_child) { |
1967 |
2252 |
4219 |
|
| 443 |
1967 |
▶ | if (sp == cap) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sp == cap) { |
919 |
1048 |
1967 |
|
| 444 |
919 |
▶ | int ncap = cap ? cap * 2 : 32; |
Branch: 1 branch
| Branch | True | False | Total |
| int ncap = cap ? cap * 2 : 32; |
4 |
915 |
919 |
|
| 445 |
919 |
| frx_write_frame *ns = (frx_write_frame *)realloc(stack, (size_t)ncap * sizeof *ns); |
| 446 |
919 |
▶ | if (!ns) { free(stack); return FRX_WRITE_FAIL(w, n->offset, "out of memory"); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ns) { free(stack); return FRX_WRITE_FAIL(w, n->offset, "out of memory"); } |
0 |
919 |
919 |
|
| 447 |
919 |
| stack = ns; cap = ncap; |
| 448 |
|
| } |
| 449 |
1967 |
| stack[sp].node = n; stack[sp].indented = ind; stack[sp].preserve = pres; sp++; |
| 450 |
1967 |
| level++; |
| 451 |
1967 |
| n = n->first_child; |
| 452 |
1967 |
| continue; |
| 453 |
|
| } |
| 454 |
2252 |
▶ | if (ok && !n->first_child && !w->o->empty_short) frx_write_end(w, n); |
Branch: 3 branches
| Branch | True | False | Total |
| if (ok && !n->first_child && !w->o->empty_short) frx_write_end(w, n); |
2252 |
0 |
2252 |
| if (ok && !n->first_child && !w->o->empty_short) frx_write_end(w, n); |
2252 |
0 |
2252 |
| if (ok && !n->first_child && !w->o->empty_short) frx_write_end(w, n); |
552 |
1700 |
2252 |
|
| 455 |
2252 |
| break; |
| 456 |
|
| } |
| 457 |
2385 |
| case FRX_TEXT: |
| 458 |
2385 |
▶ | if (!(parent_indented && w->o->drop_ws && frx_write_ws_only(n))) ok = frx_write_text(w, n); |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(parent_indented && w->o->drop_ws && frx_write_ws_only(n))) ok = frx_write_text(w, n); |
238 |
2147 |
2385 |
| if (!(parent_indented && w->o->drop_ws && frx_write_ws_only(n))) ok = frx_write_text(w, n); |
238 |
0 |
238 |
| if (!(parent_indented && w->o->drop_ws && frx_write_ws_only(n))) ok = frx_write_text(w, n); |
0 |
238 |
238 |
|
| 459 |
2385 |
| break; |
| 460 |
687 |
| case FRX_COMMENT: ok = frx_write_comment(w, n); break; |
| 461 |
139 |
| case FRX_PI: ok = frx_write_pi(w, n); break; |
| 462 |
0 |
| default: ok = FRX_WRITE_FAIL(w, n->offset, "a node of no kind"); break; |
| 463 |
|
| } |
| 464 |
5463 |
▶ | if (!ok) { free(stack); return 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ok) { free(stack); return 0; } |
5462 |
1 |
5463 |
|
| 465 |
|
| |
| 466 |
|
| /* the next node: a sibling, or up with the end tag */ |
| 467 |
|
| for (;;) { |
| 468 |
7428 |
▶ | if (n == apex) { free(stack); return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == apex) { free(stack); return 1; } |
25 |
7403 |
7428 |
|
| 469 |
7403 |
▶ | if (n->next) { n = n->next; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (n->next) { n = n->next; break; } |
3729 |
3674 |
7403 |
|
| 470 |
3674 |
▶ | if (!sp) { free(stack); return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sp) { free(stack); return 1; } |
1708 |
1966 |
3674 |
|
| 471 |
1966 |
| sp--; level--; |
| 472 |
1966 |
| n = stack[sp].node; |
| 473 |
1966 |
▶ | if (stack[sp].indented) frx_write_indent(w, level); |
Branch: 1 branch
| Branch | True | False | Total |
| if (stack[sp].indented) frx_write_indent(w, level); |
198 |
1768 |
1966 |
|
| 474 |
1966 |
| frx_write_end(w, n); |
| 475 |
|
| } |
| 476 |
|
| } |
| 477 |
1 |
| free(stack); |
| 478 |
1 |
| return 1; |
| 479 |
|
| } |
| 480 |
|
| |
| 481 |
|
| /* the staged UTF-8 in the requested encoding */ |
| 482 |
|
| static int |
| 483 |
425 |
| frx_write_transcode(const frx_buf *u8, int enc, frx_buf *out) |
| 484 |
|
| { |
| 485 |
425 |
| size_t i = 0; |
| 486 |
425 |
▶ | const char *p = u8->p ? u8->p : ""; |
Branch: 1 branch
| Branch | True | False | Total |
| const char *p = u8->p ? u8->p : ""; |
425 |
0 |
425 |
|
| 487 |
425 |
| out->len = 0; |
| 488 |
425 |
▶ | if (enc == FRX_ENC_UTF16LE) { frx_buf_append_ch(out, (char)0xFF); frx_buf_append_ch(out, (char)0xFE); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (enc == FRX_ENC_UTF16LE) { frx_buf_append_ch(out, (char)0xFF); frx_buf_append_ch(out, (char)0xFE); } |
420 |
5 |
425 |
|
| 489 |
425 |
▶ | if (enc == FRX_ENC_UTF16BE) { frx_buf_append_ch(out, (char)0xFE); frx_buf_append_ch(out, (char)0xFF); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (enc == FRX_ENC_UTF16BE) { frx_buf_append_ch(out, (char)0xFE); frx_buf_append_ch(out, (char)0xFF); } |
1 |
424 |
425 |
|
| 490 |
104479 |
▶ | while (i < u8->len) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (i < u8->len) { |
104054 |
425 |
104479 |
|
| 491 |
|
| unsigned long cp; |
| 492 |
104054 |
| size_t k = frx_write_cp(p, u8->len, i, &cp); |
| 493 |
104054 |
▶ | if (!k) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!k) return 0; |
0 |
104054 |
104054 |
|
| 494 |
207823 |
▶ | if (enc == FRX_ENC_UTF16LE || enc == FRX_ENC_UTF16BE) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (enc == FRX_ENC_UTF16LE || enc == FRX_ENC_UTF16BE) { |
346 |
103708 |
104054 |
| if (enc == FRX_ENC_UTF16LE || enc == FRX_ENC_UTF16BE) { |
61 |
285 |
346 |
|
| 495 |
|
| unsigned units[2]; |
| 496 |
103769 |
| int n = 1, j; |
| 497 |
103769 |
▶ | if (cp >= 0x10000) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cp >= 0x10000) { |
17 |
103752 |
103769 |
|
| 498 |
17 |
| cp -= 0x10000; |
| 499 |
17 |
| units[0] = 0xD800 + (unsigned)(cp >> 10); |
| 500 |
17 |
| units[1] = 0xDC00 + (unsigned)(cp & 0x3FF); |
| 501 |
17 |
| n = 2; |
| 502 |
|
| } else { |
| 503 |
103752 |
| units[0] = (unsigned)cp; |
| 504 |
|
| } |
| 505 |
207555 |
▶ | for (j = 0; j < n; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < n; j++) { |
103786 |
103769 |
207555 |
|
| 506 |
103786 |
| unsigned char hi = (unsigned char)(units[j] >> 8), lo = (unsigned char)(units[j] & 0xFF); |
| 507 |
103786 |
▶ | if (enc == FRX_ENC_UTF16LE) { frx_buf_append_ch(out, (char)lo); frx_buf_append_ch(out, (char)hi); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (enc == FRX_ENC_UTF16LE) { frx_buf_append_ch(out, (char)lo); frx_buf_append_ch(out, (char)hi); } |
103724 |
62 |
103786 |
|
| 508 |
62 |
| else { frx_buf_append_ch(out, (char)hi); frx_buf_append_ch(out, (char)lo); } |
| 509 |
|
| } |
| 510 |
|
| } else { |
| 511 |
285 |
| frx_buf_append_ch(out, (char)cp); /* every character is under the limit by construction */ |
| 512 |
|
| } |
| 513 |
104054 |
| i += k; |
| 514 |
|
| } |
| 515 |
425 |
| return !out->failed; |
| 516 |
|
| } |
| 517 |
|
| |
| 518 |
|
| /* the document (apex = its document node) or the subtree at apex, as |
| 519 |
|
| * markup in out; 0 with *err on refusal */ |
| 520 |
|
| static int |
| 521 |
1735 |
| frx_write(const frx_node *apex, const frx_doc *d, const frx_write_opts *o, frx_buf *out, frx_err *err) |
| 522 |
|
| { |
| 523 |
|
| frx_writer w; |
| 524 |
|
| frx_buf stage; |
| 525 |
1735 |
| int direct = o->encoding == FRX_ENC_UTF8; |
| 526 |
|
| int ok; |
| 527 |
|
| |
| 528 |
1735 |
| frx_buf_init(&stage); |
| 529 |
1735 |
| out->len = 0; |
| 530 |
1735 |
▶ | w.out = direct ? out : &stage; |
Branch: 1 branch
| Branch | True | False | Total |
| w.out = direct ? out : &stage; |
426 |
1309 |
1735 |
|
| 531 |
1735 |
| w.o = o; |
| 532 |
1735 |
| w.version11 = frx_str_eq(&d->version, "1.1", 3); |
| 533 |
1735 |
▶ | w.limit = o->encoding == FRX_ENC_LATIN1 ? 0xFF : o->encoding == FRX_ENC_ASCII ? 0x7F : 0x10FFFF; |
Branch: 2 branches
| Branch | True | False | Total |
| w.limit = o->encoding == FRX_ENC_LATIN1 ? 0xFF : o->encoding == FRX_ENC_ASCII ? 0x7F : 0x10FFFF; |
1734 |
1 |
1735 |
| w.limit = o->encoding == FRX_ENC_LATIN1 ? 0xFF : o->encoding == FRX_ENC_ASCII ? 0x7F : 0x10FFFF; |
4 |
1730 |
1734 |
|
| 534 |
1735 |
| w.err = err; |
| 535 |
1735 |
| w.failed = 0; |
| 536 |
1735 |
| frx_err_set(err, FRX_OK, 0, NULL); |
| 537 |
|
| |
| 538 |
1735 |
▶ | if (o->declaration) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (o->declaration) { |
1698 |
37 |
1735 |
|
| 539 |
1698 |
| frx_buf_append(w.out, "<?xml version=\""); |
| 540 |
1698 |
| frx_buf_append_n(w.out, d->version.p, d->version.len); |
| 541 |
1698 |
| frx_buf_append_ch(w.out, '"'); |
| 542 |
1698 |
▶ | if (o->encoding_attr) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (o->encoding_attr) { |
1697 |
1 |
1698 |
|
| 543 |
1697 |
| frx_buf_append(w.out, " encoding=\""); |
| 544 |
1697 |
| frx_buf_append(w.out, frx_enc_name(o->encoding)); |
| 545 |
1697 |
| frx_buf_append_ch(w.out, '"'); |
| 546 |
|
| } |
| 547 |
1698 |
▶ | if (d->standalone) frx_buf_append(w.out, " standalone=\"yes\""); |
Branch: 1 branch
| Branch | True | False | Total |
| if (d->standalone) frx_buf_append(w.out, " standalone=\"yes\""); |
51 |
1647 |
1698 |
|
| 548 |
1698 |
| frx_buf_append(w.out, "?>\n"); |
| 549 |
|
| } |
| 550 |
1735 |
▶ | if (apex->kind == FRX_DOCUMENT && o->doctype && d->doctype) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (apex->kind == FRX_DOCUMENT && o->doctype && d->doctype) { |
1710 |
25 |
1735 |
| if (apex->kind == FRX_DOCUMENT && o->doctype && d->doctype) { |
1709 |
1 |
1710 |
| if (apex->kind == FRX_DOCUMENT && o->doctype && d->doctype) { |
1638 |
71 |
1709 |
|
| 551 |
1638 |
| const frx_doctype *dt = d->doctype; |
| 552 |
1638 |
▶ | if (!frx_write_name(&w, &dt->name, dt->offset)) { frx_buf_free(&stage); return 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_write_name(&w, &dt->name, dt->offset)) { frx_buf_free(&stage); return 0; } |
0 |
1638 |
1638 |
|
| 553 |
1638 |
| frx_buf_append(w.out, "<!DOCTYPE "); |
| 554 |
1638 |
| frx_buf_append_n(w.out, dt->name.p, dt->name.len); |
| 555 |
1638 |
▶ | if (dt->public_id.len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (dt->public_id.len) { |
21 |
1617 |
1638 |
|
| 556 |
21 |
| frx_buf_append(w.out, " PUBLIC \""); |
| 557 |
21 |
| frx_buf_append_n(w.out, dt->public_id.p, dt->public_id.len); |
| 558 |
21 |
| frx_buf_append(w.out, "\" \""); |
| 559 |
21 |
| frx_buf_append_n(w.out, dt->system_id.p, dt->system_id.len); |
| 560 |
21 |
| frx_buf_append_ch(w.out, '"'); |
| 561 |
1617 |
▶ | } else if (dt->system_id.len) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (dt->system_id.len) { |
332 |
1285 |
1617 |
|
| 562 |
332 |
| frx_buf_append(w.out, " SYSTEM \""); |
| 563 |
332 |
| frx_buf_append_n(w.out, dt->system_id.p, dt->system_id.len); |
| 564 |
332 |
| frx_buf_append_ch(w.out, '"'); |
| 565 |
|
| } |
| 566 |
1638 |
▶ | if (dt->has_subset) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (dt->has_subset) { |
1454 |
184 |
1638 |
|
| 567 |
1454 |
▶ | if (!frx_write_check(&w, dt->subset.p, dt->subset.len, dt->offset)) { frx_buf_free(&stage); return 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_write_check(&w, dt->subset.p, dt->subset.len, dt->offset)) { frx_buf_free(&stage); return 0; } |
0 |
1454 |
1454 |
|
| 568 |
1454 |
| frx_buf_append(w.out, " ["); |
| 569 |
1454 |
| frx_buf_append_n(w.out, dt->subset.p, dt->subset.len); |
| 570 |
1454 |
| frx_buf_append_ch(w.out, ']'); |
| 571 |
|
| } |
| 572 |
1638 |
| frx_buf_append(w.out, ">\n"); |
| 573 |
|
| } |
| 574 |
|
| |
| 575 |
1735 |
| ok = frx_write_walk(&w, apex); |
| 576 |
1735 |
▶ | if (ok && w.out->failed) ok = FRX_WRITE_FAIL(&w, 0, "out of memory"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (ok && w.out->failed) ok = FRX_WRITE_FAIL(&w, 0, "out of memory"); |
1734 |
1 |
1735 |
| if (ok && w.out->failed) ok = FRX_WRITE_FAIL(&w, 0, "out of memory"); |
0 |
1734 |
1734 |
|
| 577 |
1735 |
▶ | if (ok && !direct) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (ok && !direct) { |
1734 |
1 |
1735 |
| if (ok && !direct) { |
425 |
1309 |
1734 |
|
| 578 |
425 |
▶ | if (!frx_write_transcode(&stage, o->encoding, out)) ok = FRX_WRITE_FAIL(&w, 0, "out of memory"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_write_transcode(&stage, o->encoding, out)) ok = FRX_WRITE_FAIL(&w, 0, "out of memory"); |
0 |
425 |
425 |
|
| 579 |
|
| } |
| 580 |
1735 |
| frx_buf_free(&stage); |
| 581 |
1735 |
| return ok; |
| 582 |
|
| } |
| 583 |
|
| |
| 584 |
|
| #endif /* FRX_WRITE_H */ |