include/frx/frx_utf8.h

filter: Statementstmt 100% 83 / 83 Branchbran 98.0% 198 / 202 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 98.6% 281 / 285 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/frx/frx_tree.h ↑ Summary blib/lib/File/Raw/XML.pm »
1 #ifndef FRX_UTF8_H
2 #define FRX_UTF8_H
3  
4 /* UTF-8 in, scalars out, and the three character classes XML names.
5 *
6 * frx_utf8_decode reads one scalar and returns its byte length, or 0 when
7 * the bytes are not UTF-8: a truncated sequence, a bad continuation byte,
8 * an overlong form, a surrogate, or anything above U+10FFFF. The table is
9 * the one in RFC 3629 section 4, which is the whole definition of "valid".
10 *
11 * The three predicates are XML 1.0 fifth edition, sections 2.2 and 2.3,
12 * transcribed range by range from the specification text. Char excludes
13 * the C0 controls other than TAB, LF and CR, the surrogates, and U+FFFE and
14 * U+FFFF; the decoder already refuses surrogates, so the Char check after a
15 * decode is what catches the last two, which are valid UTF-8 and not XML.
16 *
17 * No SIMD. The documents are kilobytes and the cost is the signature
18 * check, not the scan.
19 *
20 * Needs nothing. */
21  
22 /* byte length of the scalar at p, 0 when not UTF-8; *cp gets the scalar */
23 static size_t
24 123040 frx_utf8_decode(const unsigned char *p, size_t n, unsigned long *cp)
25 {
26 unsigned char c;
27 123040 ▶ if (!n) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return 0; 36 123004 123040
28 123004 c = p[0];
29 123004 ▶ if (c < 0x80) { *cp = c; return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (c < 0x80) { *cp = c; return 1; } 12497 110507 123004
30 110507 ▶ if (c < 0xC2) return 0; /* continuation or overlong 2-byte lead */
Branch: 1 branch
BranchTrueFalseTotal
if (c < 0xC2) return 0; /* continuation or overlong 2-byte lead */ 35 110472 110507
31 110472 ▶ if (c < 0xE0) {
Branch: 1 branch
BranchTrueFalseTotal
if (c < 0xE0) { 6866 103606 110472
32 6866 ▶ if (n < 2 || (p[1] & 0xC0) != 0x80) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (n < 2 || (p[1] & 0xC0) != 0x80) return 0; 6844 22 6866
if (n < 2 || (p[1] & 0xC0) != 0x80) return 0; 10 6834 6844
33 6834 *cp = ((unsigned long)(c & 0x1F) << 6) | (p[1] & 0x3F);
34 6834 return 2;
35 }
36 103606 ▶ if (c < 0xF0) {
Branch: 1 branch
BranchTrueFalseTotal
if (c < 0xF0) { 103108 498 103606
37 103108 ▶ if (n < 3 || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (n < 3 || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80) return 0; 103024 84 103108
if (n < 3 || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80) return 0; 103020 4 103024
if (n < 3 || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80) return 0; 42 102978 103020
38 102978 ▶ if (c == 0xE0 && p[1] < 0xA0) return 0; /* overlong */
Branch: 2 branches
BranchTrueFalseTotal
if (c == 0xE0 && p[1] < 0xA0) return 0; /* overlong */ 5192 97786 102978
if (c == 0xE0 && p[1] < 0xA0) return 0; /* overlong */ 1 5191 5192
39 102977 ▶ if (c == 0xED && p[1] >= 0xA0) return 0; /* surrogate */
Branch: 2 branches
BranchTrueFalseTotal
if (c == 0xED && p[1] >= 0xA0) return 0; /* surrogate */ 119 102858 102977
if (c == 0xED && p[1] >= 0xA0) return 0; /* surrogate */ 24 95 119
40 102953 *cp = ((unsigned long)(c & 0x0F) << 12)
41 102953 | ((unsigned long)(p[1] & 0x3F) << 6) | (p[2] & 0x3F);
42 102953 return 3;
43 }
44 498 ▶ if (c < 0xF5) {
Branch: 1 branch
BranchTrueFalseTotal
if (c < 0xF5) { 497 1 498
45 497 ▶ if (n < 4 || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80
Branch: 3 branches
BranchTrueFalseTotal
if (n < 4 || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80 479 18 497
if (n < 4 || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80 479 0 479
if (n < 4 || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80 479 0 479
46 497 ▶ || (p[3] & 0xC0) != 0x80) return 0;
Branch: 1 branch
BranchTrueFalseTotal
|| (p[3] & 0xC0) != 0x80) return 0; 3 476 479
47 476 ▶ if (c == 0xF0 && p[1] < 0x90) return 0; /* overlong */
Branch: 2 branches
BranchTrueFalseTotal
if (c == 0xF0 && p[1] < 0x90) return 0; /* overlong */ 333 143 476
if (c == 0xF0 && p[1] < 0x90) return 0; /* overlong */ 1 332 333
48 475 ▶ if (c == 0xF4 && p[1] >= 0x90) return 0; /* above U+10FFFF */
Branch: 2 branches
BranchTrueFalseTotal
if (c == 0xF4 && p[1] >= 0x90) return 0; /* above U+10FFFF */ 127 348 475
if (c == 0xF4 && p[1] >= 0x90) return 0; /* above U+10FFFF */ 1 126 127
49 474 *cp = ((unsigned long)(c & 0x07) << 18)
50 474 | ((unsigned long)(p[1] & 0x3F) << 12)
51 474 | ((unsigned long)(p[2] & 0x3F) << 6) | (p[3] & 0x3F);
52 474 return 4;
53 }
54 1 return 0;
55 }
56  
57 /* the scalar as UTF-8 into out (at least 4 bytes); returns the length */
58 static size_t
59 478580 frx_utf8_encode(unsigned long cp, unsigned char *out)
60 {
61 478580 ▶ if (cp < 0x80) { out[0] = (unsigned char)cp; return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (cp < 0x80) { out[0] = (unsigned char)cp; return 1; } 423279 55301 478580
62 55301 ▶ if (cp < 0x800) {
Branch: 1 branch
BranchTrueFalseTotal
if (cp < 0x800) { 1864 53437 55301
63 1864 out[0] = (unsigned char)(0xC0 | (cp >> 6));
64 1864 out[1] = (unsigned char)(0x80 | (cp & 0x3F));
65 1864 return 2;
66 }
67 53437 ▶ if (cp < 0x10000) {
Branch: 1 branch
BranchTrueFalseTotal
if (cp < 0x10000) { 53167 270 53437
68 53167 out[0] = (unsigned char)(0xE0 | (cp >> 12));
69 53167 out[1] = (unsigned char)(0x80 | ((cp >> 6) & 0x3F));
70 53167 out[2] = (unsigned char)(0x80 | (cp & 0x3F));
71 53167 return 3;
72 }
73 270 out[0] = (unsigned char)(0xF0 | (cp >> 18));
74 270 out[1] = (unsigned char)(0x80 | ((cp >> 12) & 0x3F));
75 270 out[2] = (unsigned char)(0x80 | ((cp >> 6) & 0x3F));
76 270 out[3] = (unsigned char)(0x80 | (cp & 0x3F));
77 270 return 4;
78 }
79  
80 /* XML 1.0 section 2.2: Char */
81 static int
82 337543 frx_is_char(unsigned long c)
83 {
84 335660 ▶ return c == 0x9 || c == 0xA || c == 0xD
Branch: 2 branches
BranchTrueFalseTotal
return c == 0x9 || c == 0xA || c == 0xD 325073 10587 335660
return c == 0x9 || c == 0xA || c == 0xD 317951 7122 325073
85 317951 ▶ || (c >= 0x20 && c <= 0xD7FF)
Branch: 2 branches
BranchTrueFalseTotal
|| (c >= 0x20 && c <= 0xD7FF) 317917 34 317951
|| (c >= 0x20 && c <= 0xD7FF) 4636 313281 317917
86 4670 ▶ || (c >= 0xE000 && c <= 0xFFFD)
Branch: 2 branches
BranchTrueFalseTotal
|| (c >= 0xE000 && c <= 0xFFFD) 4633 37 4670
|| (c >= 0xE000 && c <= 0xFFFD) 675 3958 4633
87 673203 ▶ || (c >= 0x10000 && c <= 0x10FFFF);
Branch: 3 branches
BranchTrueFalseTotal
|| (c >= 0x10000 && c <= 0x10FFFF); 335660 1883 337543
|| (c >= 0x10000 && c <= 0x10FFFF); 656 56 712
|| (c >= 0x10000 && c <= 0x10FFFF); 650 6 656
88 }
89  
90 /* XML 1.1 second edition, section 2.2, production [2]: Char */
91 static int
92 11413 frx_is_char11(unsigned long c)
93 {
94 11412 ▶ return (c >= 0x1 && c <= 0xD7FF)
Branch: 1 branch
BranchTrueFalseTotal
return (c >= 0x1 && c <= 0xD7FF) 65 11347 11412
95 66 ▶ || (c >= 0xE000 && c <= 0xFFFD)
Branch: 2 branches
BranchTrueFalseTotal
|| (c >= 0xE000 && c <= 0xFFFD) 65 1 66
|| (c >= 0xE000 && c <= 0xFFFD) 10 55 65
96 22826 ▶ || (c >= 0x10000 && c <= 0x10FFFF);
Branch: 3 branches
BranchTrueFalseTotal
|| (c >= 0x10000 && c <= 0x10FFFF); 11412 1 11413
|| (c >= 0x10000 && c <= 0x10FFFF); 6 5 11
|| (c >= 0x10000 && c <= 0x10FFFF); 6 0 6
97 }
98  
99 /* XML 1.1 second edition, section 2.2, production [2a]: RestrictedChar.
100 * Production [1] subtracts `Char* RestrictedChar Char*` from the document,
101 * so one of these written literally anywhere is not well-formed; as a
102 * character reference it is a Char and allowed. #x0 is neither. */
103 static int
104 33043 frx_is_restricted11(unsigned long c)
105 {
106 33043 ▶ return (c >= 0x1 && c <= 0x8) || c == 0xB || c == 0xC
Branch: 3 branches
BranchTrueFalseTotal
return (c >= 0x1 && c <= 0x8) || c == 0xB || c == 0xC 33023 18 33041
return (c >= 0x1 && c <= 0x8) || c == 0xB || c == 0xC 33023 2 33025
return (c >= 0x1 && c <= 0x8) || c == 0xB || c == 0xC 33012 11 33023
107 33012 ▶ || (c >= 0xE && c <= 0x1F) || (c >= 0x7F && c <= 0x84)
Branch: 4 branches
BranchTrueFalseTotal
|| (c >= 0xE && c <= 0x1F) || (c >= 0x7F && c <= 0x84) 31731 1281 33012
|| (c >= 0xE && c <= 0x1F) || (c >= 0x7F && c <= 0x84) 31710 21 31731
|| (c >= 0xE && c <= 0x1F) || (c >= 0x7F && c <= 0x84) 370 32621 32991
|| (c >= 0xE && c <= 0x1F) || (c >= 0x7F && c <= 0x84) 352 18 370
108 66086 ▶ || (c >= 0x86 && c <= 0x9F);
Branch: 3 branches
BranchTrueFalseTotal
|| (c >= 0x86 && c <= 0x9F); 33041 2 33043
|| (c >= 0x86 && c <= 0x9F); 352 32621 32973
|| (c >= 0x86 && c <= 0x9F); 43 309 352
109 }
110  
111 /* XML 1.1 second edition, section 2.3, productions [4] and [4a]. They are
112 * the productions XML 1.0 fifth edition adopted word for word, so the
113 * name tables below serve both versions; a document's version selects
114 * nothing here, and the comment says so rather than a second copy. */
115  
116 /* XML 1.0 section 2.3: NameStartChar, without the colon - the lexer
117 * handles the QName colon rule itself */
118 static int
119 52489814 frx_is_name_start(unsigned long c)
120 {
121 52489814 ▶ return (c >= 'A' && c <= 'Z') || c == '_' || (c >= 'a' && c <= 'z')
Branch: 4 branches
BranchTrueFalseTotal
return (c >= 'A' && c <= 'Z') || c == '_' || (c >= 'a' && c <= 'z') 39010235 2169366 41179601
return (c >= 'A' && c <= 'Z') || c == '_' || (c >= 'a' && c <= 'z') 50316105 4343 50320448
return (c >= 'A' && c <= 'Z') || c == '_' || (c >= 'a' && c <= 'z') 39005860 11310245 50316105
return (c >= 'A' && c <= 'Z') || c == '_' || (c >= 'a' && c <= 'z') 12565 38993295 39005860
122 11322810 ▶ || (c >= 0xC0 && c <= 0xD6) || (c >= 0xD8 && c <= 0xF6)
Branch: 4 branches
BranchTrueFalseTotal
|| (c >= 0xC0 && c <= 0xD6) || (c >= 0xD8 && c <= 0xF6) 10101 11312709 11322810
|| (c >= 0xC0 && c <= 0xD6) || (c >= 0xD8 && c <= 0xF6) 10053 48 10101
|| (c >= 0xC0 && c <= 0xD6) || (c >= 0xD8 && c <= 0xF6) 10053 11312709 11322762
|| (c >= 0xC0 && c <= 0xD6) || (c >= 0xD8 && c <= 0xF6) 9876 177 10053
123 11322585 ▶ || (c >= 0xF8 && c <= 0x2FF) || (c >= 0x370 && c <= 0x37D)
Branch: 4 branches
BranchTrueFalseTotal
|| (c >= 0xF8 && c <= 0x2FF) || (c >= 0x370 && c <= 0x37D) 9876 11312709 11322585
|| (c >= 0xF8 && c <= 0x2FF) || (c >= 0x370 && c <= 0x37D) 8686 1190 9876
|| (c >= 0xF8 && c <= 0x2FF) || (c >= 0x370 && c <= 0x37D) 8628 11312767 11321395
|| (c >= 0xF8 && c <= 0x2FF) || (c >= 0x370 && c <= 0x37D) 8602 26 8628
124 11321369 ▶ || (c >= 0x37F && c <= 0x1FFF) || (c >= 0x200C && c <= 0x200D)
Branch: 4 branches
BranchTrueFalseTotal
|| (c >= 0x37F && c <= 0x1FFF) || (c >= 0x200C && c <= 0x200D) 8596 11312773 11321369
|| (c >= 0x37F && c <= 0x1FFF) || (c >= 0x200C && c <= 0x200D) 4491 4105 8596
|| (c >= 0x37F && c <= 0x1FFF) || (c >= 0x200C && c <= 0x200D) 4471 11312793 11317264
|| (c >= 0x37F && c <= 0x1FFF) || (c >= 0x200C && c <= 0x200D) 4459 12 4471
125 11317252 ▶ || (c >= 0x2070 && c <= 0x218F) || (c >= 0x2C00 && c <= 0x2FEF)
Branch: 4 branches
BranchTrueFalseTotal
|| (c >= 0x2070 && c <= 0x218F) || (c >= 0x2C00 && c <= 0x2FEF) 4434 11312818 11317252
|| (c >= 0x2070 && c <= 0x218F) || (c >= 0x2C00 && c <= 0x2FEF) 4345 89 4434
|| (c >= 0x2070 && c <= 0x218F) || (c >= 0x2C00 && c <= 0x2FEF) 4325 11312838 11317163
|| (c >= 0x2070 && c <= 0x218F) || (c >= 0x2C00 && c <= 0x2FEF) 4301 24 4325
126 11317139 ▶ || (c >= 0x3001 && c <= 0xD7FF) || (c >= 0xF900 && c <= 0xFDCF)
Branch: 4 branches
BranchTrueFalseTotal
|| (c >= 0x3001 && c <= 0xD7FF) || (c >= 0xF900 && c <= 0xFDCF) 4293 11312846 11317139
|| (c >= 0x3001 && c <= 0xD7FF) || (c >= 0xF900 && c <= 0xFDCF) 63 4230 4293
|| (c >= 0x3001 && c <= 0xD7FF) || (c >= 0xF900 && c <= 0xFDCF) 51 11312858 11312909
|| (c >= 0x3001 && c <= 0xD7FF) || (c >= 0xF900 && c <= 0xFDCF) 18 33 51
127 104979628 ▶ || (c >= 0xFDF0 && c <= 0xFFFD) || (c >= 0x10000 && c <= 0xEFFFF);
Branch: 5 branches
BranchTrueFalseTotal
|| (c >= 0xFDF0 && c <= 0xFFFD) || (c >= 0x10000 && c <= 0xEFFFF); 41179601 11310213 52489814
|| (c >= 0xFDF0 && c <= 0xFFFD) || (c >= 0x10000 && c <= 0xEFFFF); 14 11312862 11312876
|| (c >= 0xFDF0 && c <= 0xFFFD) || (c >= 0x10000 && c <= 0xEFFFF); 10 4 14
|| (c >= 0xFDF0 && c <= 0xFFFD) || (c >= 0x10000 && c <= 0xEFFFF); 6 11312866 11312872
|| (c >= 0xFDF0 && c <= 0xFFFD) || (c >= 0x10000 && c <= 0xEFFFF); 4 2 6
128 }
129  
130 /* XML 1.0 section 2.3: NameChar, without the colon */
131 static int
132 40672373 frx_is_name_char(unsigned long c)
133 {
134 40672373 return frx_is_name_start(c)
135 11312155 ▶ || c == '-' || c == '.' || (c >= '0' && c <= '9') || c == 0xB7
Branch: 5 branches
BranchTrueFalseTotal
|| c == '-' || c == '.' || (c >= '0' && c <= '9') || c == 0xB7 11299603 12552 11312155
|| c == '-' || c == '.' || (c >= '0' && c <= '9') || c == 0xB7 11298431 1172 11299603
|| c == '-' || c == '.' || (c >= '0' && c <= '9') || c == 0xB7 9681779 1616652 11298431
|| c == '-' || c == '.' || (c >= '0' && c <= '9') || c == 0xB7 9092788 588991 9681779
|| c == '-' || c == '.' || (c >= '0' && c <= '9') || c == 0xB7 10709424 16 10709440
136 51984528 ▶ || (c >= 0x300 && c <= 0x36F) || (c >= 0x203F && c <= 0x2040);
Branch: 5 branches
BranchTrueFalseTotal
|| (c >= 0x300 && c <= 0x36F) || (c >= 0x203F && c <= 0x2040); 11312155 29360218 40672373
|| (c >= 0x300 && c <= 0x36F) || (c >= 0x203F && c <= 0x2040); 101 10709323 10709424
|| (c >= 0x300 && c <= 0x36F) || (c >= 0x203F && c <= 0x2040); 53 48 101
|| (c >= 0x300 && c <= 0x36F) || (c >= 0x203F && c <= 0x2040); 34 10709342 10709376
|| (c >= 0x300 && c <= 0x36F) || (c >= 0x203F && c <= 0x2040); 8 26 34
137 }
138  
139 /* Namespaces in XML section 4: an NCName is a Name with no colon. What
140 * xml:id's value must be (xml:id section 4), and what a prefix is. */
141 static int
142 2892 frx_is_ncname(const char *p, size_t n)
143 {
144 2892 size_t i = 0;
145 2892 int first = 1;
146 2892 ▶ if (!n) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return 0; 1 2891 2892
147 28109 ▶ while (i < n) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < n) { 25250 2859 28109
148 unsigned long cp;
149 size_t k;
150 25250 unsigned char c = (unsigned char)p[i];
151 25250 ▶ if (c < 0x80) { cp = c; k = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (c < 0x80) { cp = c; k = 1; } 25249 1 25250
152 else {
153 1 k = frx_utf8_decode((const unsigned char *)p + i, n - i, &cp);
154 33 ▶ if (!k) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!k) return 0; 0 1 1
155 }
156 25250 ▶ if (cp == ':') return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (cp == ':') return 0; 6 25244 25250
157 25244 ▶ if (first ? !frx_is_name_start(cp) : !frx_is_name_char(cp)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (first ? !frx_is_name_start(cp) : !frx_is_name_char(cp)) return 0; 2891 22353 25244
if (first ? !frx_is_name_start(cp) : !frx_is_name_char(cp)) return 0; 26 25218 25244
158 25218 first = 0;
159 25218 i += k;
160 }
161 2859 return 1;
162 }
163  
164 /* XML 1.0 section 2.3: S */
165 static int
166 15231489 frx_is_s(unsigned char c)
167 {
168 15231489 ▶ return c == 0x20 || c == 0x9 || c == 0xA || c == 0xD;
Branch: 4 branches
BranchTrueFalseTotal
return c == 0x20 || c == 0x9 || c == 0xA || c == 0xD; 12925304 2306185 15231489
return c == 0x20 || c == 0x9 || c == 0xA || c == 0xD; 12920166 5138 12925304
return c == 0x20 || c == 0x9 || c == 0xA || c == 0xD; 12845965 74201 12920166
return c == 0x20 || c == 0x9 || c == 0xA || c == 0xD; 11322 12834643 12845965
169 }
170  
171 #endif /* FRX_UTF8_H */
« include/frx/frx_tree.h ↑ Summary blib/lib/File/Raw/XML.pm »