Length of the well-formed UTF-8 sequence at s[0, n), 1 to 4; 0 when there * is none: a stray continuation byte, an overlong form, a surrogate, a code * point past U+10FFFF, a sequence cut short. */
| 142 | * is none: a stray continuation byte, an overlong form, a surrogate, a code |
| 143 | * point past U+10FFFF, a sequence cut short. */ |
| 144 | static size_t cs_utf8_len(const unsigned char *s, size_t n) { |
| 145 | if (n == 0) { |
| 146 | return 0; |
| 147 | } |
| 148 | unsigned char c = s[0]; |
| 149 | if (c < 0x80) { |
| 150 | return SKIP_ONE; |
| 151 | } |
| 152 | size_t len = 0; |
| 153 | if (c >= 0xC2 && c <= 0xDF) { |
| 154 | len = PAIR_LEN; |
| 155 | } else if (c >= 0xE0 && c <= 0xEF) { |
| 156 | len = 3; |
| 157 | } else if (c >= 0xF0 && c <= 0xF4) { |
| 158 | len = 4; |
| 159 | } |
| 160 | if (len == 0 || n < len) { |
| 161 | return 0; |
| 162 | } |
| 163 | unsigned char c1 = s[1]; |
| 164 | if ((c == 0xE0 && c1 < 0xA0) || (c == 0xED && c1 > 0x9F) || (c == 0xF0 && c1 < 0x90) || |
| 165 | (c == 0xF4 && c1 > 0x8F)) { |
| 166 | return 0; /* overlong, surrogate, or past U+10FFFF */ |
| 167 | } |
| 168 | for (size_t k = SKIP_ONE; k < len; k++) { |
| 169 | if ((s[k] & CS_UTF8_TAIL_MASK) != CS_UTF8_TAIL) { |
| 170 | return 0; |
| 171 | } |
| 172 | } |
| 173 | return len; |
| 174 | } |
| 175 | |
| 176 | /* True when s[0, n) is well-formed UTF-8. */ |
| 177 | static bool cs_utf8_ok(const char *s, size_t n) { |
no outgoing calls
no test coverage detected