utf8_valid.h

filter: Statementstmt 62.3% 38 / 61 Branchbran 37.0% 20 / 54 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 50.4% 58 / 115 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary XS.xs »
1 /*
2 * Copyright (c) 2017 Christian Hansen <chansen@cpan.org>
3 * <https://github.com/chansen/c-utf8-valid>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice, this
10 * list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26 #ifndef UTF8_VALID_H
27 #define UTF8_VALID_H
28 #include <stddef.h>
29 #include <string.h>
30  
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34  
35 size_t
36 1 utf8_maximal_subpart(const char *src, size_t len) {
37 1 const unsigned char *cur = (const unsigned char *)src;
38 U32 v;
39  
40 1 ▶ if (len < 2)
Branch: 1 branch
BranchTrueFalseTotal
if (len < 2) 0 1 1
41 0 return len;
42  
43 1 v = (cur[0] << 8) | cur[1];
44 1 ▶ if ((v & 0xC0C0) != 0xC080)
Branch: 1 branch
BranchTrueFalseTotal
if ((v & 0xC0C0) != 0xC080) 1 0 1
45 1 return 1;
46  
47 0 ▶ if ((v & 0x2000) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v & 0x2000) == 0) { 0 0 0
48 0 ▶ if ((v & 0x1E00) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((v & 0x1E00) == 0) 0 0 0
49 0 return 1;
50 0 return 2;
51 }
52 0 ▶ else if ((v & 0x1000) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if ((v & 0x1000) == 0) { 0 0 0
53 0 ▶ if ((v & 0x0F20) == 0 ||
Branch: 1 branch
BranchTrueFalseTotal
if ((v & 0x0F20) == 0 || 0 0 0
54 0 ▶ (v & 0x0F20) == 0x0D20)
Branch: 1 branch
BranchTrueFalseTotal
(v & 0x0F20) == 0x0D20) 0 0 0
55 0 return 1;
56 0 ▶ if (len < 3 || (cur[2] & 0xC0) != 0x80)
Branch: 2 branches
BranchTrueFalseTotal
if (len < 3 || (cur[2] & 0xC0) != 0x80) 0 0 0
if (len < 3 || (cur[2] & 0xC0) != 0x80) 0 0 0
57 0 return 2;
58 0 return 3;
59 }
60 else {
61 0 ▶ if ((v & 0x0730) == 0 ||
Branch: 2 branches
BranchTrueFalseTotal
if ((v & 0x0730) == 0 || 0 0 0
if ((v & 0x0730) == 0 || 0 0 0
62 (v > 0xF48F))
63 0 return 1;
64 0 ▶ if (len < 3 || (cur[2] & 0xC0) != 0x80)
Branch: 2 branches
BranchTrueFalseTotal
if (len < 3 || (cur[2] & 0xC0) != 0x80) 0 0 0
if (len < 3 || (cur[2] & 0xC0) != 0x80) 0 0 0
65 0 return 2;
66 0 ▶ if (len < 4 || (cur[3] & 0xC0) != 0x80)
Branch: 2 branches
BranchTrueFalseTotal
if (len < 4 || (cur[3] & 0xC0) != 0x80) 0 0 0
if (len < 4 || (cur[3] & 0xC0) != 0x80) 0 0 0
67 0 return 3;
68 0 return 4;
69 }
70 }
71  
72 bool
73 11 utf8_check(const char *src, size_t len, size_t *cursor) {
74 11 const unsigned char *cur = (const unsigned char *)src;
75 11 const unsigned char *end = cur + len;
76 const unsigned char *p;
77 unsigned char buf[4];
78 U32 v;
79  
80 11 for (p = cur;;) {
81 37 ▶ if (cur >= end - 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (cur >= end - 3) { 18 19 37
82 18 ▶ if (cur == end)
Branch: 1 branch
BranchTrueFalseTotal
if (cur == end) 8 10 18
83 8 break;
84 10 memset(buf, 0, 4);
85 10 memcpy(buf, cur, end - cur);
86 10 p = (const unsigned char *)buf;
87 }
88  
89 29 v = *p++;
90 29 ▶ if ((v & 0x80) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v & 0x80) == 0) { 18 11 29
91 18 cur += 1;
92 18 continue;
93 }
94  
95 11 v = (v << 8) | *p++;
96 11 ▶ if ((v & 0xE0C0) == 0xC080 &&
Branch: 1 branch
BranchTrueFalseTotal
if ((v & 0xE0C0) == 0xC080 && 6 5 11
97 6 ▶ (v & 0x1E00) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
(v & 0x1E00) != 0) { 6 0 6
98 6 cur += 2;
99 6 continue;
100 }
101  
102 5 v = (v << 8) | *p++;
103 5 ▶ if ((v & 0xF0C0C0) == 0xE08080 &&
Branch: 1 branch
BranchTrueFalseTotal
if ((v & 0xF0C0C0) == 0xE08080 && 1 4 5
104 1 ▶ (v & 0x0F2000) != 0 &&
Branch: 1 branch
BranchTrueFalseTotal
(v & 0x0F2000) != 0 && 1 0 1
105 1 ▶ (v & 0x0F2000) != 0x0D2000) {
Branch: 1 branch
BranchTrueFalseTotal
(v & 0x0F2000) != 0x0D2000) { 0 1 1
106 0 cur += 3;
107 0 continue;
108 }
109  
110 5 v = (v << 8) | *p++;
111 5 ▶ if ((v & 0xF8C0C0C0) == 0xF0808080 &&
Branch: 1 branch
BranchTrueFalseTotal
if ((v & 0xF8C0C0C0) == 0xF0808080 && 2 3 5
112 2 ▶ (v & 0x07300000) != 0 &&
Branch: 2 branches
BranchTrueFalseTotal
(v & 0x07300000) != 0 && 2 0 2
(v & 0x07300000) != 0 && 2 0 2
113 (v < 0xF4908080)) {
114 2 cur += 4;
115 2 continue;
116 }
117  
118 3 break;
119 }
120  
121 11 ▶ if (cursor)
Branch: 1 branch
BranchTrueFalseTotal
if (cursor) 11 0 11
122 11 *cursor = (const char *)cur - src;
123  
124 11 return cur == end;
125 }
126  
127 bool
128 0 utf8_valid(const char *src, size_t len) {
129 0 return utf8_check(src, len, NULL);
130 }
131  
132 #ifdef __cplusplus
133 }
134 #endif
135 #endif
136  
↑ Summary XS.xs »