| 1 |
|
| #ifndef FRX_ENC_H |
| 2 |
|
| #define FRX_ENC_H |
| 3 |
|
| |
| 4 |
|
| /* The transcoder in front of the lexer, full profile only. |
| 5 |
|
| * |
| 6 |
|
| * XML 1.0 section 4.3.3 says every conforming processor accepts UTF-16, |
| 7 |
|
| * and Latin-1 and US-ASCII are what the rest of the world declares. The |
| 8 |
|
| * lexer stays a UTF-8 lexer: this step turns the caller's bytes into one |
| 9 |
|
| * malloc'd UTF-8 buffer owned by the parse, and the only thing it shares |
| 10 |
|
| * with the lexer is the offset map, so a refusal in a UTF-16 document |
| 11 |
|
| * still names the byte the user can find with a hex editor. Anything |
| 12 |
|
| * beyond these four is decision G's territory: the caller transcodes on |
| 13 |
|
| * its side and says `encoding => 'UTF-8'`. |
| 14 |
|
| * |
| 15 |
|
| * Detection is XML 1.0 appendix F, in this order: a byte order mark; the |
| 16 |
|
| * first four bytes for the `<?xm` pattern in each width; else UTF-8. A |
| 17 |
|
| * UTF-8-compatible start is then told apart by peeking at the encoding |
| 18 |
|
| * declaration's name, which is ASCII in every encoding this accepts, so |
| 19 |
|
| * a document declaring iso-8859-1 is transcoded from it. The lexer |
| 20 |
|
| * afterwards checks the declaration it parses against what was detected |
| 21 |
|
| * (frx_lex.h): a BOM contradicted by the declaration is fatal, and so is |
| 22 |
|
| * a UTF-16 document declaring anything but UTF-16. UCS-4 and EBCDIC are |
| 23 |
|
| * detected and refused by name. |
| 24 |
|
| * |
| 25 |
|
| * The output is sized once from the bounds: UTF-16 to UTF-8 is at most |
| 26 |
|
| * 1.5x, Latin-1 at most 2x, so max_bytes is checked on the input and the |
| 27 |
|
| * output cannot exceed twice it. |
| 28 |
|
| * |
| 29 |
|
| * The map is one checkpoint per FRX_ENC_STEP input bytes recording the |
| 30 |
|
| * matching output offset; mapping an output offset back re-walks from the |
| 31 |
|
| * nearest checkpoint. The error path pays; the parse path does not. |
| 32 |
|
| * |
| 33 |
|
| * Under strict none of this runs: the 0.01 checks in frx_lex.h refuse |
| 34 |
|
| * UTF-16 by BOM or first pair and every declared non-UTF-8 encoding. |
| 35 |
|
| * |
| 36 |
|
| * Needs frx_err.h, frx_utf8.h. */ |
| 37 |
|
| |
| 38 |
|
| enum { |
| 39 |
|
| FRX_ENC_UTF8 = 0, |
| 40 |
|
| FRX_ENC_UTF16LE, |
| 41 |
|
| FRX_ENC_UTF16BE, |
| 42 |
|
| FRX_ENC_LATIN1, |
| 43 |
|
| FRX_ENC_ASCII, |
| 44 |
|
| FRX_ENC_UNSUPPORTED /* detected, refused by name */ |
| 45 |
|
| }; |
| 46 |
|
| |
| 47 |
|
| #define FRX_ENC_STEP 4096 |
| 48 |
|
| |
| 49 |
|
| typedef struct frx_enc { |
| 50 |
|
| int kind; /* FRX_ENC_* */ |
| 51 |
|
| int bom; /* bytes of byte order mark consumed from the input */ |
| 52 |
|
| int override; /* the caller named the encoding; the declaration is not checked */ |
| 53 |
|
| char *out; /* malloc'd UTF-8; NULL when the input is used as it is */ |
| 54 |
|
| size_t out_len; |
| 55 |
|
| size_t *map; /* map[i] = output offset at input offset bom + i * FRX_ENC_STEP */ |
| 56 |
|
| size_t n_map; |
| 57 |
|
| } frx_enc; |
| 58 |
|
| |
| 59 |
|
| static const char * |
| 60 |
1749 |
| frx_enc_name(int kind) |
| 61 |
|
| { |
| 62 |
1749 |
| switch (kind) { |
| 63 |
1272 |
| case FRX_ENC_UTF8: return "UTF-8"; |
| 64 |
458 |
| case FRX_ENC_UTF16LE: return "UTF-16LE"; |
| 65 |
4 |
| case FRX_ENC_UTF16BE: return "UTF-16BE"; |
| 66 |
7 |
| case FRX_ENC_LATIN1: return "ISO-8859-1"; |
| 67 |
8 |
| case FRX_ENC_ASCII: return "US-ASCII"; |
| 68 |
0 |
| default: return "unsupported"; |
| 69 |
|
| } |
| 70 |
|
| } |
| 71 |
|
| |
| 72 |
|
| static int |
| 73 |
13291 |
| frx_enc_ieq(const char *v, size_t n, const char *lit) |
| 74 |
|
| { |
| 75 |
|
| size_t i; |
| 76 |
13291 |
▶ | if (n != strlen(lit)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n != strlen(lit)) return 0; |
3610 |
9681 |
13291 |
|
| 77 |
61564 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
51996 |
9568 |
61564 |
|
| 78 |
51996 |
| unsigned char a = (unsigned char)v[i], b = (unsigned char)lit[i]; |
| 79 |
51996 |
▶ | if (a >= 'A' && a <= 'Z') a = (unsigned char)(a - 'A' + 'a'); |
Branch: 2 branches
| Branch | True | False | Total |
| if (a >= 'A' && a <= 'Z') a = (unsigned char)(a - 'A' + 'a'); |
30638 |
21358 |
51996 |
| if (a >= 'A' && a <= 'Z') a = (unsigned char)(a - 'A' + 'a'); |
29172 |
1466 |
30638 |
|
| 80 |
51996 |
▶ | if (a != b) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (a != b) return 0; |
113 |
51883 |
51996 |
|
| 81 |
|
| } |
| 82 |
9568 |
| return 1; |
| 83 |
|
| } |
| 84 |
|
| |
| 85 |
|
| /* an encoding name, case-insensitively, to a kind; -1 when not one of ours. |
| 86 |
|
| * The names are the IANA ones and their common aliases. */ |
| 87 |
|
| static int |
| 88 |
9196 |
| frx_enc_kind_by_name(const char *v, size_t n) |
| 89 |
|
| { |
| 90 |
9196 |
▶ | if (frx_enc_ieq(v, n, "utf-8") || frx_enc_ieq(v, n, "utf8")) return FRX_ENC_UTF8; |
Branch: 2 branches
| Branch | True | False | Total |
| if (frx_enc_ieq(v, n, "utf-8") || frx_enc_ieq(v, n, "utf8")) return FRX_ENC_UTF8; |
1165 |
8031 |
9196 |
| if (frx_enc_ieq(v, n, "utf-8") || frx_enc_ieq(v, n, "utf8")) return FRX_ENC_UTF8; |
6 |
1159 |
1165 |
|
| 91 |
1159 |
▶ | if (frx_enc_ieq(v, n, "utf-16le")) return FRX_ENC_UTF16LE; |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_enc_ieq(v, n, "utf-16le")) return FRX_ENC_UTF16LE; |
844 |
315 |
1159 |
|
| 92 |
315 |
▶ | if (frx_enc_ieq(v, n, "utf-16be")) return FRX_ENC_UTF16BE; |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_enc_ieq(v, n, "utf-16be")) return FRX_ENC_UTF16BE; |
4 |
311 |
315 |
|
| 93 |
311 |
▶ | if (frx_enc_ieq(v, n, "utf-16")) return FRX_ENC_UTF16LE; /* the endianness comes from the BOM */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_enc_ieq(v, n, "utf-16")) return FRX_ENC_UTF16LE; /* the endianness comes from the BOM */ |
58 |
253 |
311 |
|
| 94 |
253 |
▶ | if (frx_enc_ieq(v, n, "iso-8859-1") || frx_enc_ieq(v, n, "iso_8859-1") |
Branch: 2 branches
| Branch | True | False | Total |
| if (frx_enc_ieq(v, n, "iso-8859-1") || frx_enc_ieq(v, n, "iso_8859-1") |
79 |
174 |
253 |
| if (frx_enc_ieq(v, n, "iso-8859-1") || frx_enc_ieq(v, n, "iso_8859-1") |
79 |
0 |
79 |
|
| 95 |
253 |
▶ | || frx_enc_ieq(v, n, "latin1") || frx_enc_ieq(v, n, "l1")) return FRX_ENC_LATIN1; |
Branch: 2 branches
| Branch | True | False | Total |
| || frx_enc_ieq(v, n, "latin1") || frx_enc_ieq(v, n, "l1")) return FRX_ENC_LATIN1; |
75 |
4 |
79 |
| || frx_enc_ieq(v, n, "latin1") || frx_enc_ieq(v, n, "l1")) return FRX_ENC_LATIN1; |
0 |
75 |
75 |
|
| 96 |
75 |
▶ | if (frx_enc_ieq(v, n, "us-ascii") || frx_enc_ieq(v, n, "ascii") |
Branch: 2 branches
| Branch | True | False | Total |
| if (frx_enc_ieq(v, n, "us-ascii") || frx_enc_ieq(v, n, "ascii") |
64 |
11 |
75 |
| if (frx_enc_ieq(v, n, "us-ascii") || frx_enc_ieq(v, n, "ascii") |
58 |
6 |
64 |
|
| 97 |
75 |
▶ | || frx_enc_ieq(v, n, "ansi_x3.4-1968")) return FRX_ENC_ASCII; |
Branch: 1 branch
| Branch | True | False | Total |
| || frx_enc_ieq(v, n, "ansi_x3.4-1968")) return FRX_ENC_ASCII; |
0 |
58 |
58 |
|
| 98 |
58 |
| return -1; |
| 99 |
|
| } |
| 100 |
|
| |
| 101 |
|
| /* is the declared name consistent with what was detected? "utf-16" is |
| 102 |
|
| * consistent with either endianness; "utf-16le" only with LE. */ |
| 103 |
|
| static int |
| 104 |
3186 |
| frx_enc_declared_matches(int kind, const char *v, size_t n) |
| 105 |
|
| { |
| 106 |
3186 |
| int d = frx_enc_kind_by_name(v, n); |
| 107 |
3186 |
▶ | if (d < 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (d < 0) return 0; |
0 |
3186 |
3186 |
|
| 108 |
3186 |
▶ | if (d == kind) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (d == kind) return 1; |
3173 |
13 |
3186 |
|
| 109 |
13 |
▶ | if (frx_enc_ieq(v, n, "utf-16") && (kind == FRX_ENC_UTF16LE || kind == FRX_ENC_UTF16BE)) return 1; |
Branch: 3 branches
| Branch | True | False | Total |
| if (frx_enc_ieq(v, n, "utf-16") && (kind == FRX_ENC_UTF16LE || kind == FRX_ENC_UTF16BE)) return 1; |
7 |
6 |
13 |
| if (frx_enc_ieq(v, n, "utf-16") && (kind == FRX_ENC_UTF16LE || kind == FRX_ENC_UTF16BE)) return 1; |
7 |
0 |
7 |
| if (frx_enc_ieq(v, n, "utf-16") && (kind == FRX_ENC_UTF16LE || kind == FRX_ENC_UTF16BE)) return 1; |
5 |
2 |
7 |
|
| 110 |
8 |
| return 0; |
| 111 |
|
| } |
| 112 |
|
| |
| 113 |
|
| /* the encoding declaration's value, peeked from the first line of an |
| 114 |
|
| * ASCII-compatible input: `<?xml ... encoding = "name"`; 0 when absent */ |
| 115 |
|
| static int |
| 116 |
7712 |
| frx_enc_peek_declaration(const unsigned char *in, size_t len, const char **v, size_t *n) |
| 117 |
|
| { |
| 118 |
7712 |
| size_t i = 0, limit = len < 256 ? len : 256; |
| 119 |
7712 |
▶ | if (len < 6 || memcmp(in, "<?xml", 5) != 0 || !frx_is_s(in[5])) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (len < 6 || memcmp(in, "<?xml", 5) != 0 || !frx_is_s(in[5])) return 0; |
7631 |
81 |
7712 |
| if (len < 6 || memcmp(in, "<?xml", 5) != 0 || !frx_is_s(in[5])) return 0; |
4492 |
3139 |
7631 |
| if (len < 6 || memcmp(in, "<?xml", 5) != 0 || !frx_is_s(in[5])) return 0; |
2 |
4490 |
4492 |
|
| 120 |
71163 |
▶ | for (i = 5; i + 8 < limit; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 5; i + 8 < limit; i++) { |
71132 |
31 |
71163 |
|
| 121 |
71132 |
▶ | if (in[i] == '?' && in[i + 1] == '>') return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (in[i] == '?' && in[i + 1] == '>') return 0; |
1667 |
69465 |
71132 |
| if (in[i] == '?' && in[i + 1] == '>') return 0; |
1666 |
1 |
1667 |
|
| 122 |
69466 |
▶ | if (memcmp(in + i, "encoding", 8) == 0 && frx_is_s(in[i - 1])) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (memcmp(in + i, "encoding", 8) == 0 && frx_is_s(in[i - 1])) { |
2801 |
66665 |
69466 |
| if (memcmp(in + i, "encoding", 8) == 0 && frx_is_s(in[i - 1])) { |
2793 |
8 |
2801 |
|
| 123 |
2793 |
| size_t j = i + 8; |
| 124 |
|
| unsigned char q; |
| 125 |
|
| size_t start; |
| 126 |
2794 |
▶ | while (j < limit && frx_is_s(in[j])) j++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (j < limit && frx_is_s(in[j])) j++; |
2794 |
0 |
2794 |
| while (j < limit && frx_is_s(in[j])) j++; |
1 |
2793 |
2794 |
|
| 127 |
2793 |
▶ | if (j >= limit || in[j] != '=') return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (j >= limit || in[j] != '=') return 0; |
2793 |
0 |
2793 |
| if (j >= limit || in[j] != '=') return 0; |
2 |
2791 |
2793 |
|
| 128 |
2791 |
| j++; |
| 129 |
2792 |
▶ | while (j < limit && frx_is_s(in[j])) j++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (j < limit && frx_is_s(in[j])) j++; |
2791 |
1 |
2792 |
| while (j < limit && frx_is_s(in[j])) j++; |
1 |
2790 |
2791 |
|
| 130 |
2791 |
▶ | if (j >= limit) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (j >= limit) return 0; |
1 |
2790 |
2791 |
|
| 131 |
2790 |
| q = in[j]; |
| 132 |
2790 |
▶ | if (q != '"' && q != '\'') return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (q != '"' && q != '\'') return 0; |
199 |
2591 |
2790 |
| if (q != '"' && q != '\'') return 0; |
2 |
197 |
199 |
|
| 133 |
2788 |
| start = ++j; |
| 134 |
17071 |
▶ | while (j < limit && in[j] != q) j++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (j < limit && in[j] != q) j++; |
17065 |
6 |
17071 |
| while (j < limit && in[j] != q) j++; |
14283 |
2782 |
17065 |
|
| 135 |
2788 |
▶ | if (j >= limit) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (j >= limit) return 0; |
6 |
2782 |
2788 |
|
| 136 |
2782 |
| *v = (const char *)in + start; |
| 137 |
2782 |
| *n = j - start; |
| 138 |
2782 |
| return 1; |
| 139 |
|
| } |
| 140 |
|
| } |
| 141 |
31 |
| return 0; |
| 142 |
|
| } |
| 143 |
|
| |
| 144 |
|
| /* Appendix F. Fills e->kind and e->bom; a name the caller supplied wins |
| 145 |
|
| * over everything. 0 on a refusal with err set (an unsupported family or |
| 146 |
|
| * an unknown override). */ |
| 147 |
|
| static int |
| 148 |
8239 |
| frx_enc_detect(frx_enc *e, const unsigned char *in, size_t len, const char *override, frx_err *err) |
| 149 |
|
| { |
| 150 |
|
| const char *v; |
| 151 |
|
| size_t n; |
| 152 |
|
| |
| 153 |
8239 |
| e->kind = FRX_ENC_UTF8; |
| 154 |
8239 |
| e->bom = 0; |
| 155 |
8239 |
| e->override = 0; |
| 156 |
|
| |
| 157 |
|
| /* a BOM is consumed whatever else is decided */ |
| 158 |
8239 |
▶ | if (len >= 3 && in[0] == 0xEF && in[1] == 0xBB && in[2] == 0xBF) { e->kind = FRX_ENC_UTF8; e->bom = 3; } |
Branch: 4 branches
| Branch | True | False | Total |
| if (len >= 3 && in[0] == 0xEF && in[1] == 0xBB && in[2] == 0xBF) { e->kind = FRX_ENC_UTF8; e->bom = 3; } |
8191 |
48 |
8239 |
| if (len >= 3 && in[0] == 0xEF && in[1] == 0xBB && in[2] == 0xBF) { e->kind = FRX_ENC_UTF8; e->bom = 3; } |
9 |
8182 |
8191 |
| if (len >= 3 && in[0] == 0xEF && in[1] == 0xBB && in[2] == 0xBF) { e->kind = FRX_ENC_UTF8; e->bom = 3; } |
9 |
0 |
9 |
| if (len >= 3 && in[0] == 0xEF && in[1] == 0xBB && in[2] == 0xBF) { e->kind = FRX_ENC_UTF8; e->bom = 3; } |
9 |
0 |
9 |
|
| 159 |
8230 |
▶ | else if (len >= 4 && in[0] == 0 && in[1] == 0 && in[2] == 0xFE && in[3] == 0xFF) { e->kind = FRX_ENC_UNSUPPORTED; } |
Branch: 5 branches
| Branch | True | False | Total |
| else if (len >= 4 && in[0] == 0 && in[1] == 0 && in[2] == 0xFE && in[3] == 0xFF) { e->kind = FRX_ENC_UNSUPPORTED; } |
8177 |
53 |
8230 |
| else if (len >= 4 && in[0] == 0 && in[1] == 0 && in[2] == 0xFE && in[3] == 0xFF) { e->kind = FRX_ENC_UNSUPPORTED; } |
5 |
8172 |
8177 |
| else if (len >= 4 && in[0] == 0 && in[1] == 0 && in[2] == 0xFE && in[3] == 0xFF) { e->kind = FRX_ENC_UNSUPPORTED; } |
2 |
3 |
5 |
| else if (len >= 4 && in[0] == 0 && in[1] == 0 && in[2] == 0xFE && in[3] == 0xFF) { e->kind = FRX_ENC_UNSUPPORTED; } |
1 |
1 |
2 |
| else if (len >= 4 && in[0] == 0 && in[1] == 0 && in[2] == 0xFE && in[3] == 0xFF) { e->kind = FRX_ENC_UNSUPPORTED; } |
1 |
0 |
1 |
|
| 160 |
8229 |
▶ | else if (len >= 4 && in[0] == 0xFF && in[1] == 0xFE && in[2] == 0 && in[3] == 0) { e->kind = FRX_ENC_UNSUPPORTED; } |
Branch: 5 branches
| Branch | True | False | Total |
| else if (len >= 4 && in[0] == 0xFF && in[1] == 0xFE && in[2] == 0 && in[3] == 0) { e->kind = FRX_ENC_UNSUPPORTED; } |
8176 |
53 |
8229 |
| else if (len >= 4 && in[0] == 0xFF && in[1] == 0xFE && in[2] == 0 && in[3] == 0) { e->kind = FRX_ENC_UNSUPPORTED; } |
491 |
7685 |
8176 |
| else if (len >= 4 && in[0] == 0xFF && in[1] == 0xFE && in[2] == 0 && in[3] == 0) { e->kind = FRX_ENC_UNSUPPORTED; } |
491 |
0 |
491 |
| else if (len >= 4 && in[0] == 0xFF && in[1] == 0xFE && in[2] == 0 && in[3] == 0) { e->kind = FRX_ENC_UNSUPPORTED; } |
0 |
491 |
491 |
| else if (len >= 4 && in[0] == 0xFF && in[1] == 0xFE && in[2] == 0 && in[3] == 0) { e->kind = FRX_ENC_UNSUPPORTED; } |
0 |
0 |
0 |
|
| 161 |
8229 |
▶ | else if (len >= 2 && in[0] == 0xFE && in[1] == 0xFF) { e->kind = FRX_ENC_UTF16BE; e->bom = 2; } |
Branch: 3 branches
| Branch | True | False | Total |
| else if (len >= 2 && in[0] == 0xFE && in[1] == 0xFF) { e->kind = FRX_ENC_UTF16BE; e->bom = 2; } |
8186 |
43 |
8229 |
| else if (len >= 2 && in[0] == 0xFE && in[1] == 0xFF) { e->kind = FRX_ENC_UTF16BE; e->bom = 2; } |
15 |
8171 |
8186 |
| else if (len >= 2 && in[0] == 0xFE && in[1] == 0xFF) { e->kind = FRX_ENC_UTF16BE; e->bom = 2; } |
15 |
0 |
15 |
|
| 162 |
8214 |
▶ | else if (len >= 2 && in[0] == 0xFF && in[1] == 0xFE) { e->kind = FRX_ENC_UTF16LE; e->bom = 2; } |
Branch: 3 branches
| Branch | True | False | Total |
| else if (len >= 2 && in[0] == 0xFF && in[1] == 0xFE) { e->kind = FRX_ENC_UTF16LE; e->bom = 2; } |
8171 |
43 |
8214 |
| else if (len >= 2 && in[0] == 0xFF && in[1] == 0xFE) { e->kind = FRX_ENC_UTF16LE; e->bom = 2; } |
491 |
7680 |
8171 |
| else if (len >= 2 && in[0] == 0xFF && in[1] == 0xFE) { e->kind = FRX_ENC_UTF16LE; e->bom = 2; } |
491 |
0 |
491 |
|
| 163 |
|
| |
| 164 |
8239 |
▶ | if (override) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (override) { |
8 |
8231 |
8239 |
|
| 165 |
8 |
| int k = frx_enc_kind_by_name(override, strlen(override)); |
| 166 |
8 |
▶ | if (k < 0) return (frx_err_set(err, FRX_E_ENCODING, 0, "the encoding named by the caller is not one this parser supports"), 0); |
Branch: 1 branch
| Branch | True | False | Total |
| if (k < 0) return (frx_err_set(err, FRX_E_ENCODING, 0, "the encoding named by the caller is not one this parser supports"), 0); |
1 |
7 |
8 |
|
| 167 |
7 |
▶ | if (frx_enc_ieq(override, strlen(override), "utf-16") && e->bom == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (frx_enc_ieq(override, strlen(override), "utf-16") && e->bom == 0) |
2 |
5 |
7 |
| if (frx_enc_ieq(override, strlen(override), "utf-16") && e->bom == 0) |
1 |
1 |
2 |
|
| 168 |
1 |
| k = FRX_ENC_UTF16BE; /* RFC 2781: no BOM, big-endian */ |
| 169 |
7 |
▶ | if (e->kind == FRX_ENC_UNSUPPORTED) e->kind = k; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->kind == FRX_ENC_UNSUPPORTED) e->kind = k; |
0 |
7 |
7 |
|
| 170 |
7 |
▶ | else if (e->bom && k != e->kind && !(e->kind == FRX_ENC_UTF16LE && frx_enc_ieq(override, strlen(override), "utf-16"))) |
Branch: 4 branches
| Branch | True | False | Total |
| else if (e->bom && k != e->kind && !(e->kind == FRX_ENC_UTF16LE && frx_enc_ieq(override, strlen(override), "utf-16"))) |
2 |
5 |
7 |
| else if (e->bom && k != e->kind && !(e->kind == FRX_ENC_UTF16LE && frx_enc_ieq(override, strlen(override), "utf-16"))) |
1 |
1 |
2 |
| else if (e->bom && k != e->kind && !(e->kind == FRX_ENC_UTF16LE && frx_enc_ieq(override, strlen(override), "utf-16"))) |
1 |
0 |
1 |
| else if (e->bom && k != e->kind && !(e->kind == FRX_ENC_UTF16LE && frx_enc_ieq(override, strlen(override), "utf-16"))) |
1 |
0 |
1 |
|
| 171 |
1 |
| return (frx_err_set(err, FRX_E_ENCODING, 0, "the encoding named by the caller contradicts the byte order mark"), 0); |
| 172 |
6 |
| else e->kind = k; |
| 173 |
6 |
| e->override = 1; |
| 174 |
6 |
| return 1; |
| 175 |
|
| } |
| 176 |
|
| |
| 177 |
8231 |
▶ | if (e->kind == FRX_ENC_UNSUPPORTED) |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->kind == FRX_ENC_UNSUPPORTED) |
1 |
8230 |
8231 |
|
| 178 |
1 |
| return (frx_err_set(err, FRX_E_ENCODING, 0, "UCS-4 is not supported; transcode to UTF-8 or UTF-16 first"), 0); |
| 179 |
8230 |
▶ | if (e->bom) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->bom) return 1; |
513 |
7717 |
8230 |
|
| 180 |
|
| |
| 181 |
|
| /* no BOM: the first four bytes */ |
| 182 |
7717 |
▶ | if (len >= 4) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (len >= 4) { |
7664 |
53 |
7717 |
|
| 183 |
7664 |
▶ | if (in[0] == 0 && in[1] == '<' && in[2] == 0 && in[3] == '?') { e->kind = FRX_ENC_UTF16BE; return 1; } |
Branch: 4 branches
| Branch | True | False | Total |
| if (in[0] == 0 && in[1] == '<' && in[2] == 0 && in[3] == '?') { e->kind = FRX_ENC_UTF16BE; return 1; } |
3 |
7661 |
7664 |
| if (in[0] == 0 && in[1] == '<' && in[2] == 0 && in[3] == '?') { e->kind = FRX_ENC_UTF16BE; return 1; } |
2 |
1 |
3 |
| if (in[0] == 0 && in[1] == '<' && in[2] == 0 && in[3] == '?') { e->kind = FRX_ENC_UTF16BE; return 1; } |
1 |
1 |
2 |
| if (in[0] == 0 && in[1] == '<' && in[2] == 0 && in[3] == '?') { e->kind = FRX_ENC_UTF16BE; return 1; } |
1 |
0 |
1 |
|
| 184 |
7663 |
▶ | if (in[0] == '<' && in[1] == 0 && in[2] == '?' && in[3] == 0) { e->kind = FRX_ENC_UTF16LE; return 1; } |
Branch: 4 branches
| Branch | True | False | Total |
| if (in[0] == '<' && in[1] == 0 && in[2] == '?' && in[3] == 0) { e->kind = FRX_ENC_UTF16LE; return 1; } |
7381 |
282 |
7663 |
| if (in[0] == '<' && in[1] == 0 && in[2] == '?' && in[3] == 0) { e->kind = FRX_ENC_UTF16LE; return 1; } |
2 |
7379 |
7381 |
| if (in[0] == '<' && in[1] == 0 && in[2] == '?' && in[3] == 0) { e->kind = FRX_ENC_UTF16LE; return 1; } |
2 |
0 |
2 |
| if (in[0] == '<' && in[1] == 0 && in[2] == '?' && in[3] == 0) { e->kind = FRX_ENC_UTF16LE; return 1; } |
2 |
0 |
2 |
|
| 185 |
7661 |
▶ | if (in[0] == 0 && in[1] == 0 && in[2] == 0 && in[3] == '<') |
Branch: 4 branches
| Branch | True | False | Total |
| if (in[0] == 0 && in[1] == 0 && in[2] == 0 && in[3] == '<') |
2 |
7659 |
7661 |
| if (in[0] == 0 && in[1] == 0 && in[2] == 0 && in[3] == '<') |
1 |
1 |
2 |
| if (in[0] == 0 && in[1] == 0 && in[2] == 0 && in[3] == '<') |
1 |
0 |
1 |
| if (in[0] == 0 && in[1] == 0 && in[2] == 0 && in[3] == '<') |
1 |
0 |
1 |
|
| 186 |
1 |
| return (frx_err_set(err, FRX_E_ENCODING, 0, "UCS-4 is not supported; transcode to UTF-8 or UTF-16 first"), 0); |
| 187 |
7660 |
▶ | if (in[0] == '<' && in[1] == 0 && in[2] == 0 && in[3] == 0) |
Branch: 4 branches
| Branch | True | False | Total |
| if (in[0] == '<' && in[1] == 0 && in[2] == 0 && in[3] == 0) |
7379 |
281 |
7660 |
| if (in[0] == '<' && in[1] == 0 && in[2] == 0 && in[3] == 0) |
0 |
7379 |
7379 |
| if (in[0] == '<' && in[1] == 0 && in[2] == 0 && in[3] == 0) |
0 |
0 |
0 |
| if (in[0] == '<' && in[1] == 0 && in[2] == 0 && in[3] == 0) |
0 |
0 |
0 |
|
| 188 |
0 |
| return (frx_err_set(err, FRX_E_ENCODING, 0, "UCS-4 is not supported; transcode to UTF-8 or UTF-16 first"), 0); |
| 189 |
7660 |
▶ | if (in[0] == 0x4C && in[1] == 0x6F && in[2] == 0xA7 && in[3] == 0x94) |
Branch: 4 branches
| Branch | True | False | Total |
| if (in[0] == 0x4C && in[1] == 0x6F && in[2] == 0xA7 && in[3] == 0x94) |
1 |
7659 |
7660 |
| if (in[0] == 0x4C && in[1] == 0x6F && in[2] == 0xA7 && in[3] == 0x94) |
1 |
0 |
1 |
| if (in[0] == 0x4C && in[1] == 0x6F && in[2] == 0xA7 && in[3] == 0x94) |
1 |
0 |
1 |
| if (in[0] == 0x4C && in[1] == 0x6F && in[2] == 0xA7 && in[3] == 0x94) |
1 |
0 |
1 |
|
| 190 |
1 |
| return (frx_err_set(err, FRX_E_ENCODING, 0, "EBCDIC is not supported; transcode to UTF-8 first"), 0); |
| 191 |
|
| } |
| 192 |
|
| /* UTF-8 compatible: a declaration may name Latin-1 or ASCII */ |
| 193 |
7712 |
▶ | if (frx_enc_peek_declaration(in, len, &v, &n)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_enc_peek_declaration(in, len, &v, &n)) { |
2782 |
4930 |
7712 |
|
| 194 |
2782 |
| int k = frx_enc_kind_by_name(v, n); |
| 195 |
2782 |
▶ | if (k == FRX_ENC_LATIN1 || k == FRX_ENC_ASCII) e->kind = k; |
Branch: 2 branches
| Branch | True | False | Total |
| if (k == FRX_ENC_LATIN1 || k == FRX_ENC_ASCII) e->kind = k; |
2726 |
56 |
2782 |
| if (k == FRX_ENC_LATIN1 || k == FRX_ENC_ASCII) e->kind = k; |
7 |
2719 |
2726 |
|
| 196 |
|
| /* anything else is the lexer's to judge against UTF-8 */ |
| 197 |
|
| } |
| 198 |
7712 |
| return 1; |
| 199 |
|
| } |
| 200 |
|
| |
| 201 |
|
| /* one scalar from the input at pos under kind; returns bytes consumed, 0 |
| 202 |
|
| * on a malformed unit with *what set */ |
| 203 |
|
| static size_t |
| 204 |
449971 |
| frx_enc_step(int kind, const unsigned char *in, size_t len, size_t pos, |
| 205 |
|
| unsigned long *cp, const char **what) |
| 206 |
|
| { |
| 207 |
449971 |
| switch (kind) { |
| 208 |
434113 |
| case FRX_ENC_UTF16LE: |
| 209 |
|
| case FRX_ENC_UTF16BE: { |
| 210 |
|
| unsigned u, lo; |
| 211 |
434113 |
▶ | if (len - pos < 2) { *what = "an odd trailing byte in UTF-16 input"; return 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (len - pos < 2) { *what = "an odd trailing byte in UTF-16 input"; return 0; } |
136 |
433977 |
434113 |
|
| 212 |
704079 |
| u = kind == FRX_ENC_UTF16LE ? (unsigned)(in[pos] | (in[pos + 1] << 8)) |
| 213 |
433977 |
▶ | : (unsigned)((in[pos] << 8) | in[pos + 1]); |
Branch: 1 branch
| Branch | True | False | Total |
| : (unsigned)((in[pos] << 8) | in[pos + 1]); |
270102 |
163875 |
433977 |
|
| 214 |
433977 |
▶ | if (u >= 0xDC00 && u <= 0xDFFF) { *what = "an unpaired low surrogate in UTF-16 input"; return 0; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (u >= 0xDC00 && u <= 0xDFFF) { *what = "an unpaired low surrogate in UTF-16 input"; return 0; } |
2593 |
431384 |
433977 |
| if (u >= 0xDC00 && u <= 0xDFFF) { *what = "an unpaired low surrogate in UTF-16 input"; return 0; } |
1 |
2592 |
2593 |
|
| 215 |
433976 |
▶ | if (u >= 0xD800 && u <= 0xDBFF) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (u >= 0xD800 && u <= 0xDBFF) { |
2617 |
431359 |
433976 |
| if (u >= 0xD800 && u <= 0xDBFF) { |
25 |
2592 |
2617 |
|
| 216 |
25 |
▶ | if (len - pos < 4) { *what = "a high surrogate with nothing after it in UTF-16 input"; return 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (len - pos < 4) { *what = "a high surrogate with nothing after it in UTF-16 input"; return 0; } |
4 |
21 |
25 |
|
| 217 |
42 |
| lo = kind == FRX_ENC_UTF16LE ? (unsigned)(in[pos + 2] | (in[pos + 3] << 8)) |
| 218 |
21 |
▶ | : (unsigned)((in[pos + 2] << 8) | in[pos + 3]); |
Branch: 1 branch
| Branch | True | False | Total |
| : (unsigned)((in[pos + 2] << 8) | in[pos + 3]); |
21 |
0 |
21 |
|
| 219 |
21 |
▶ | if (lo < 0xDC00 || lo > 0xDFFF) { *what = "a high surrogate not followed by a low surrogate in UTF-16 input"; return 0; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (lo < 0xDC00 || lo > 0xDFFF) { *what = "a high surrogate not followed by a low surrogate in UTF-16 input"; return 0; } |
20 |
1 |
21 |
| if (lo < 0xDC00 || lo > 0xDFFF) { *what = "a high surrogate not followed by a low surrogate in UTF-16 input"; return 0; } |
0 |
20 |
20 |
|
| 220 |
20 |
| *cp = 0x10000 + (((unsigned long)u - 0xD800) << 10) + (lo - 0xDC00); |
| 221 |
20 |
| return 4; |
| 222 |
|
| } |
| 223 |
433951 |
| *cp = u; |
| 224 |
433951 |
| return 2; |
| 225 |
|
| } |
| 226 |
14989 |
| case FRX_ENC_LATIN1: |
| 227 |
14989 |
| *cp = in[pos]; |
| 228 |
14989 |
| return 1; |
| 229 |
869 |
| case FRX_ENC_ASCII: |
| 230 |
869 |
▶ | if (in[pos] >= 0x80) { *what = "a byte outside US-ASCII in input declared US-ASCII"; return 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (in[pos] >= 0x80) { *what = "a byte outside US-ASCII in input declared US-ASCII"; return 0; } |
1 |
868 |
869 |
|
| 231 |
868 |
| *cp = in[pos]; |
| 232 |
868 |
| return 1; |
| 233 |
0 |
| default: |
| 234 |
0 |
| *cp = in[pos]; |
| 235 |
0 |
| return 1; |
| 236 |
|
| } |
| 237 |
|
| } |
| 238 |
|
| |
| 239 |
|
| /* Transcode the input after the BOM into e->out. For UTF-8 nothing is |
| 240 |
|
| * allocated: out stays NULL and the caller uses the input past the BOM. |
| 241 |
|
| * 0 on refusal with err set at the input offset. */ |
| 242 |
|
| static int |
| 243 |
7401 |
| frx_enc_run(frx_enc *e, const unsigned char *in, size_t len, frx_err *err) |
| 244 |
|
| { |
| 245 |
7401 |
| size_t pos, o = 0, cap, next_cp; |
| 246 |
7401 |
| const char *what = NULL; |
| 247 |
|
| |
| 248 |
7401 |
| e->out = NULL; e->out_len = 0; e->map = NULL; e->n_map = 0; |
| 249 |
7401 |
▶ | if (e->kind == FRX_ENC_UTF8) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->kind == FRX_ENC_UTF8) return 1; |
6830 |
571 |
7401 |
|
| 250 |
|
| |
| 251 |
571 |
| cap = (len - (size_t)e->bom) * 2 + 4; |
| 252 |
571 |
| e->out = (char *)malloc(cap); |
| 253 |
571 |
| e->n_map = (len - (size_t)e->bom) / FRX_ENC_STEP + 2; |
| 254 |
571 |
| e->map = (size_t *)malloc(e->n_map * sizeof *e->map); |
| 255 |
571 |
▶ | if (!e->out || !e->map) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!e->out || !e->map) { |
571 |
0 |
571 |
| if (!e->out || !e->map) { |
0 |
571 |
571 |
|
| 256 |
0 |
| free(e->out); free(e->map); e->out = NULL; e->map = NULL; |
| 257 |
0 |
| frx_err_set(err, FRX_E_NOMEM, 0, "out of memory"); |
| 258 |
0 |
| return 0; |
| 259 |
|
| } |
| 260 |
571 |
| next_cp = 0; |
| 261 |
448014 |
▶ | for (pos = (size_t)e->bom; pos < len; ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (pos = (size_t)e->bom; pos < len; ) { |
447448 |
566 |
448014 |
|
| 262 |
|
| unsigned long cp; |
| 263 |
|
| size_t n; |
| 264 |
447448 |
▶ | if (pos - (size_t)e->bom >= next_cp) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pos - (size_t)e->bom >= next_cp) { |
729 |
446719 |
447448 |
|
| 265 |
729 |
| e->map[next_cp / FRX_ENC_STEP] = o; |
| 266 |
729 |
| next_cp += FRX_ENC_STEP; |
| 267 |
|
| } |
| 268 |
447448 |
| n = frx_enc_step(e->kind, in, len, pos, &cp, &what); |
| 269 |
447448 |
▶ | if (!n) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!n) { |
5 |
447443 |
447448 |
|
| 270 |
5 |
| frx_err_set(err, FRX_E_ENCODING, pos, what); |
| 271 |
5 |
| err->enc = frx_enc_name(e->kind); |
| 272 |
5 |
| free(e->out); free(e->map); e->out = NULL; e->map = NULL; |
| 273 |
5 |
| return 0; |
| 274 |
|
| } |
| 275 |
447443 |
| o += frx_utf8_encode(cp, (unsigned char *)e->out + o); |
| 276 |
447443 |
| pos += n; |
| 277 |
|
| } |
| 278 |
566 |
| e->out[o] = '\0'; |
| 279 |
566 |
| e->out_len = o; |
| 280 |
|
| /* a final checkpoint so the map is never walked past its end */ |
| 281 |
566 |
| e->map[next_cp / FRX_ENC_STEP] = o; |
| 282 |
566 |
| return 1; |
| 283 |
|
| } |
| 284 |
|
| |
| 285 |
|
| /* the input offset that produced output offset out_off, for a message */ |
| 286 |
|
| static size_t |
| 287 |
50 |
| frx_enc_input_offset(const frx_enc *e, const unsigned char *in, size_t len, size_t out_off) |
| 288 |
|
| { |
| 289 |
|
| size_t i, pos, o; |
| 290 |
50 |
▶ | if (!e->out) return out_off + (size_t)e->bom; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!e->out) return out_off + (size_t)e->bom; |
3 |
47 |
50 |
|
| 291 |
|
| /* the last checkpoint at or before out_off */ |
| 292 |
47 |
| i = 0; |
| 293 |
49 |
▶ | while (i + 1 < e->n_map && (i + 1) * FRX_ENC_STEP + (size_t)e->bom < len && e->map[i + 1] <= out_off) i++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (i + 1 < e->n_map && (i + 1) * FRX_ENC_STEP + (size_t)e->bom < len && e->map[i + 1] <= out_off) i++; |
49 |
0 |
49 |
| while (i + 1 < e->n_map && (i + 1) * FRX_ENC_STEP + (size_t)e->bom < len && e->map[i + 1] <= out_off) i++; |
2 |
47 |
49 |
| while (i + 1 < e->n_map && (i + 1) * FRX_ENC_STEP + (size_t)e->bom < len && e->map[i + 1] <= out_off) i++; |
2 |
0 |
2 |
|
| 294 |
47 |
| pos = (size_t)e->bom + i * FRX_ENC_STEP; |
| 295 |
47 |
| o = e->map[i]; |
| 296 |
2160 |
▶ | while (pos < len && o < out_off) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (pos < len && o < out_off) { |
2160 |
0 |
2160 |
| while (pos < len && o < out_off) { |
2113 |
47 |
2160 |
|
| 297 |
|
| unsigned long cp; |
| 298 |
|
| const char *what; |
| 299 |
|
| unsigned char tmp[4]; |
| 300 |
2113 |
| size_t n = frx_enc_step(e->kind, in, len, pos, &cp, &what); |
| 301 |
2113 |
▶ | if (!n) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!n) break; |
0 |
2113 |
2113 |
|
| 302 |
2113 |
| o += frx_utf8_encode(cp, tmp); |
| 303 |
2113 |
| pos += n; |
| 304 |
|
| } |
| 305 |
47 |
| return pos; |
| 306 |
|
| } |
| 307 |
|
| |
| 308 |
|
| static void |
| 309 |
29638 |
| frx_enc_free(frx_enc *e) |
| 310 |
|
| { |
| 311 |
29638 |
| free(e->out); |
| 312 |
29638 |
| free(e->map); |
| 313 |
29638 |
| e->out = NULL; |
| 314 |
29638 |
| e->map = NULL; |
| 315 |
29638 |
| } |
| 316 |
|
| |
| 317 |
|
| #endif /* FRX_ENC_H */ |