| 1 |
|
| /* |
| 2 |
|
| * Copyright (c) 2026 Christian Hansen <chansen@cpan.org> |
| 3 |
|
| * <https://github.com/chansen/c-utf8> |
| 4 |
|
| * |
| 5 |
|
| * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 |
|
| * of this software and associated documentation files (the "Software"), to deal |
| 7 |
|
| * in the Software without restriction, including without limitation the rights |
| 8 |
|
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 |
|
| * copies of the Software, and to permit persons to whom the Software is |
| 10 |
|
| * furnished to do so, subject to the following conditions: |
| 11 |
|
| * |
| 12 |
|
| * The above copyright notice and this permission notice shall be included in all |
| 13 |
|
| * copies or substantial portions of the Software. |
| 14 |
|
| * |
| 15 |
|
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 |
|
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 |
|
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 |
|
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 |
|
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 |
|
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 |
|
| * SOFTWARE. |
| 22 |
|
| */ |
| 23 |
|
| #ifndef UTF8_VALID_STREAM_H |
| 24 |
|
| #define UTF8_VALID_STREAM_H |
| 25 |
|
| #include <stddef.h> |
| 26 |
|
| #include <stdbool.h> |
| 27 |
|
| |
| 28 |
|
| #if defined(UTF8_DFA32_H) && defined(UTF8_DFA64_H) |
| 29 |
|
| # error "utf8_dfa32.h and utf8_dfa64.h are mutually exclusive" |
| 30 |
|
| #elif !defined(UTF8_DFA32_H) && !defined(UTF8_DFA64_H) |
| 31 |
|
| # error "include utf8_dfa32.h or utf8_dfa64.h before utf8_valid_stream.h" |
| 32 |
|
| #endif |
| 33 |
|
| |
| 34 |
|
| #ifdef __cplusplus |
| 35 |
|
| extern "C" { |
| 36 |
|
| #endif |
| 37 |
|
| |
| 38 |
|
| #ifndef UTF8_VALID_STREAM_PROBE_WINDOW_SIZE |
| 39 |
|
| # define UTF8_VALID_STREAM_PROBE_WINDOW_SIZE 256 |
| 40 |
|
| #endif |
| 41 |
|
| |
| 42 |
|
| /* |
| 43 |
|
| * utf8_valid_stream_status_t -- outcome of a streaming validation step. |
| 44 |
|
| * |
| 45 |
|
| * UTF8_VALID_STREAM_OK src fully consumed, no errors. |
| 46 |
|
| * UTF8_VALID_STREAM_PARTIAL src fully consumed, ends in the middle of a sequence. |
| 47 |
|
| * UTF8_VALID_STREAM_ILLFORMED stopped at an ill-formed sequence. |
| 48 |
|
| * UTF8_VALID_STREAM_TRUNCATED eof is true and src ends in the middle of a sequence. |
| 49 |
|
| */ |
| 50 |
|
| |
| 51 |
|
| typedef enum { |
| 52 |
|
| UTF8_VALID_STREAM_OK, |
| 53 |
|
| UTF8_VALID_STREAM_PARTIAL, |
| 54 |
|
| UTF8_VALID_STREAM_ILLFORMED, |
| 55 |
|
| UTF8_VALID_STREAM_TRUNCATED, |
| 56 |
|
| } utf8_valid_stream_status_t; |
| 57 |
|
| |
| 58 |
|
| /* |
| 59 |
|
| * utf8_valid_stream_result_t -- result of a streaming validation step. |
| 60 |
|
| * |
| 61 |
|
| * status: outcome of the operation (see utf8_valid_stream_status_t). |
| 62 |
|
| * consumed: bytes read from src. |
| 63 |
|
| * pending: bytes of an incomplete trailing sequence on PARTIAL, else 0. |
| 64 |
|
| * advance: bytes to skip on ILLFORMED or TRUNCATED, else 0. |
| 65 |
|
| * Resume at src[consumed + advance]. |
| 66 |
|
| * carried: bytes from a previous chunk that belong to the same subpart. |
| 67 |
|
| */ |
| 68 |
|
| typedef struct { |
| 69 |
|
| utf8_valid_stream_status_t status; |
| 70 |
|
| size_t consumed; |
| 71 |
|
| size_t pending; |
| 72 |
|
| size_t advance; |
| 73 |
|
| size_t carried; |
| 74 |
|
| } utf8_valid_stream_result_t; |
| 75 |
|
| |
| 76 |
|
| typedef struct { |
| 77 |
|
| utf8_dfa_state_t state; |
| 78 |
|
| size_t carried; |
| 79 |
|
| size_t probe_window; |
| 80 |
|
| bool probe_ascii; |
| 81 |
|
| } utf8_valid_stream_t; |
| 82 |
|
| |
| 83 |
|
| static inline void |
| 84 |
|
| utf8_valid_stream_set_window(utf8_valid_stream_t *s, size_t window) { |
| 85 |
|
| s->probe_window = window; |
| 86 |
|
| } |
| 87 |
|
| |
| 88 |
|
| static inline void |
| 89 |
|
| utf8_valid_stream_set_ascii(utf8_valid_stream_t *s, bool ascii) { |
| 90 |
|
| s->probe_ascii = ascii; |
| 91 |
|
| } |
| 92 |
|
| |
| 93 |
|
| static inline void |
| 94 |
310851 |
| utf8_valid_stream_init(utf8_valid_stream_t *s) { |
| 95 |
310851 |
| s->state = UTF8_DFA_ACCEPT; |
| 96 |
310851 |
| s->carried = 0; |
| 97 |
310851 |
| s->probe_window = UTF8_VALID_STREAM_PROBE_WINDOW_SIZE; |
| 98 |
|
| #ifdef UTF8_VALID_STREAM_PROBE_ASCII |
| 99 |
|
| s->probe_ascii = true; |
| 100 |
|
| #else |
| 101 |
310851 |
| s->probe_ascii = false; |
| 102 |
|
| #endif |
| 103 |
310851 |
| } |
| 104 |
|
| |
| 105 |
|
| static inline size_t |
| 106 |
35075 |
| utf8_valid_stream_probe_boundary(const uint8_t *bytes, |
| 107 |
|
| size_t len, |
| 108 |
|
| size_t probe_window) { |
| 109 |
35075 |
▶ | size_t probe = len > probe_window ? probe_window : len - 1; |
Branch: 1 branch
| Branch | True | False | Total |
| size_t probe = len > probe_window ? probe_window : len - 1; |
8214 |
26861 |
35075 |
|
| 110 |
|
| |
| 111 |
|
| // Back up to a definite UTF-8 boundary: |
| 112 |
|
| // the start of the final sequence in the probe window. |
| 113 |
86298 |
▶ | while (probe > 0 && (bytes[probe] & 0xC0) == 0x80) |
Branch: 2 branches
| Branch | True | False | Total |
| while (probe > 0 && (bytes[probe] & 0xC0) == 0x80) |
86298 |
0 |
86298 |
| while (probe > 0 && (bytes[probe] & 0xC0) == 0x80) |
51223 |
35075 |
86298 |
|
| 114 |
51223 |
| probe--; |
| 115 |
|
| |
| 116 |
35075 |
| return probe; |
| 117 |
|
| } |
| 118 |
|
| |
| 119 |
|
| static inline bool |
| 120 |
35075 |
| utf8_valid_stream_probe_run(const uint8_t *bytes, |
| 121 |
|
| size_t len, |
| 122 |
|
| bool ascii) { |
| 123 |
|
| utf8_dfa_state_t state; |
| 124 |
|
| |
| 125 |
35075 |
▶ | if (len < 64) |
Branch: 1 branch
| Branch | True | False | Total |
| if (len < 64) |
9 |
35066 |
35075 |
|
| 126 |
9 |
| state = utf8_dfa_run(UTF8_DFA_ACCEPT, bytes, len); |
| 127 |
35066 |
▶ | else if (ascii) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (ascii) |
0 |
35066 |
35066 |
|
| 128 |
0 |
| state = utf8_dfa_run_ascii(UTF8_DFA_ACCEPT, bytes, len); |
| 129 |
|
| else |
| 130 |
35066 |
| state = utf8_dfa_run_dual(UTF8_DFA_ACCEPT, bytes, len); |
| 131 |
|
| |
| 132 |
35075 |
| return state == UTF8_DFA_ACCEPT; |
| 133 |
|
| } |
| 134 |
|
| |
| 135 |
|
| /* |
| 136 |
|
| * utf8_valid_stream_check -- validate the next chunk of a UTF-8 stream. |
| 137 |
|
| * |
| 138 |
|
| * src[0..len) is the next chunk. eof should be true only for the final chunk. |
| 139 |
|
| * The DFA state is carried in s across calls. |
| 140 |
|
| * |
| 141 |
|
| * Returns a utf8_valid_stream_result_t describing the outcome: |
| 142 |
|
| * |
| 143 |
|
| * status: |
| 144 |
|
| * UTF8_VALID_STREAM_OK src fully consumed, no errors. |
| 145 |
|
| * UTF8_VALID_STREAM_PARTIAL src fully consumed, ends in the middle of a sequence. |
| 146 |
|
| * UTF8_VALID_STREAM_ILLFORMED stopped at an ill-formed sequence. |
| 147 |
|
| * UTF8_VALID_STREAM_TRUNCATED eof is true and src ends in the middle of a sequence. |
| 148 |
|
| * |
| 149 |
|
| * consumed: bytes read from src. |
| 150 |
|
| * pending: bytes of an incomplete trailing sequence on PARTIAL, else 0. |
| 151 |
|
| * advance: bytes to skip on ILLFORMED or TRUNCATED, else 0. |
| 152 |
|
| * Resume at src[consumed + advance]. |
| 153 |
|
| * carried: bytes from a previous chunk that belong to the same subpart. |
| 154 |
|
| * |
| 155 |
|
| * On ILLFORMED or TRUNCATED: |
| 156 |
|
| * |
| 157 |
|
| * subpart length = carried + advance |
| 158 |
|
| * subpart start = src + consumed - carried (chunk-relative) |
| 159 |
|
| * |
| 160 |
|
| * and the stream state is reset to UTF8_DFA_ACCEPT. |
| 161 |
|
| */ |
| 162 |
|
| static inline utf8_valid_stream_result_t |
| 163 |
442306 |
| utf8_valid_stream_check(utf8_valid_stream_t* s, |
| 164 |
|
| const char* src, |
| 165 |
|
| size_t len, |
| 166 |
|
| bool eof) { |
| 167 |
442306 |
| const uint8_t* bytes = (const uint8_t*)src; |
| 168 |
442306 |
| utf8_dfa_state_t state = s->state; |
| 169 |
442306 |
| size_t carried = s->carried; |
| 170 |
442306 |
| size_t consumed = 0; |
| 171 |
442306 |
| size_t chunk_bytes = 0; |
| 172 |
442306 |
| size_t pos = 0; |
| 173 |
442306 |
| bool probe_run = s->probe_window >= 64; |
| 174 |
|
| |
| 175 |
3124071 |
▶ | while (pos < len) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (pos < len) { |
2790040 |
334031 |
3124071 |
|
| 176 |
2790040 |
| state = utf8_dfa_step(state, bytes[pos++]); |
| 177 |
2790040 |
| chunk_bytes++; |
| 178 |
|
| |
| 179 |
2790040 |
▶ | if (state == UTF8_DFA_ACCEPT) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (state == UTF8_DFA_ACCEPT) { |
2005555 |
784485 |
2790040 |
|
| 180 |
2005555 |
▶ | if (probe_run && len - pos >= 64) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (probe_run && len - pos >= 64) { |
1877862 |
127693 |
2005555 |
| if (probe_run && len - pos >= 64) { |
34973 |
1842889 |
1877862 |
|
| 181 |
|
| do { |
| 182 |
35075 |
| size_t probe = utf8_valid_stream_probe_boundary(bytes + pos, len - pos, s->probe_window); |
| 183 |
35075 |
▶ | if (probe == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (probe == 0) |
0 |
35075 |
35075 |
|
| 184 |
0 |
| probe_run = false; |
| 185 |
|
| else |
| 186 |
35075 |
| probe_run = utf8_valid_stream_probe_run(bytes + pos, probe, s->probe_ascii); |
| 187 |
35075 |
▶ | if (!probe_run) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!probe_run) |
33884 |
1191 |
35075 |
|
| 188 |
33884 |
| break; |
| 189 |
1191 |
| pos += probe; |
| 190 |
1191 |
▶ | } while (len - pos >= 64); |
Branch: 1 branch
| Branch | True | False | Total |
| } while (len - pos >= 64); |
102 |
1089 |
1191 |
|
| 191 |
|
| } |
| 192 |
2005555 |
| consumed = pos; |
| 193 |
2005555 |
| chunk_bytes = 0; |
| 194 |
2005555 |
| carried = 0; |
| 195 |
|
| } |
| 196 |
784485 |
▶ | else if (state == UTF8_DFA_REJECT) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (state == UTF8_DFA_REJECT) { |
108275 |
676210 |
784485 |
|
| 197 |
108275 |
| size_t total = carried + chunk_bytes; |
| 198 |
108275 |
▶ | size_t advance = total > 1 ? chunk_bytes - 1 : 1; |
Branch: 1 branch
| Branch | True | False | Total |
| size_t advance = total > 1 ? chunk_bytes - 1 : 1; |
2926 |
105349 |
108275 |
|
| 199 |
108275 |
| s->state = UTF8_DFA_ACCEPT; |
| 200 |
108275 |
| s->carried = 0; |
| 201 |
108275 |
| return (utf8_valid_stream_result_t){ |
| 202 |
|
| .status = UTF8_VALID_STREAM_ILLFORMED, |
| 203 |
108275 |
▶ | .consumed = carried ? 0 : consumed, |
Branch: 1 branch
| Branch | True | False | Total |
| .consumed = carried ? 0 : consumed, |
108043 |
232 |
108275 |
|
| 204 |
|
| .pending = 0, |
| 205 |
|
| .advance = advance, |
| 206 |
|
| .carried = carried, |
| 207 |
|
| }; |
| 208 |
|
| } |
| 209 |
|
| } |
| 210 |
|
| |
| 211 |
334031 |
▶ | if (state == UTF8_DFA_ACCEPT) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (state == UTF8_DFA_ACCEPT) { |
250009 |
84022 |
334031 |
|
| 212 |
250009 |
| s->state = UTF8_DFA_ACCEPT; |
| 213 |
250009 |
| s->carried = 0; |
| 214 |
250009 |
| return (utf8_valid_stream_result_t){ |
| 215 |
|
| .status = UTF8_VALID_STREAM_OK, |
| 216 |
|
| .consumed = len, |
| 217 |
|
| .pending = 0, |
| 218 |
|
| .advance = 0, |
| 219 |
|
| .carried = 0, |
| 220 |
|
| }; |
| 221 |
|
| } |
| 222 |
|
| |
| 223 |
84022 |
▶ | if (eof) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eof) { |
286 |
83736 |
84022 |
|
| 224 |
286 |
| s->state = UTF8_DFA_ACCEPT; |
| 225 |
286 |
| s->carried = 0; |
| 226 |
286 |
| return (utf8_valid_stream_result_t){ |
| 227 |
|
| .status = UTF8_VALID_STREAM_TRUNCATED, |
| 228 |
286 |
▶ | .consumed = carried ? 0 : consumed, |
Branch: 1 branch
| Branch | True | False | Total |
| .consumed = carried ? 0 : consumed, |
0 |
286 |
286 |
|
| 229 |
|
| .pending = 0, |
| 230 |
|
| .advance = chunk_bytes, |
| 231 |
|
| .carried = carried, |
| 232 |
|
| }; |
| 233 |
|
| } |
| 234 |
|
| |
| 235 |
83736 |
| s->state = state; |
| 236 |
83736 |
| s->carried = carried + chunk_bytes; |
| 237 |
83736 |
| return (utf8_valid_stream_result_t){ |
| 238 |
|
| .status = UTF8_VALID_STREAM_PARTIAL, |
| 239 |
|
| .consumed = consumed, |
| 240 |
83736 |
| .pending = carried + chunk_bytes, |
| 241 |
|
| .advance = 0, |
| 242 |
|
| .carried = 0, |
| 243 |
|
| }; |
| 244 |
|
| } |
| 245 |
|
| |
| 246 |
|
| #ifdef __cplusplus |
| 247 |
|
| } |
| 248 |
|
| #endif |
| 249 |
|
| #endif |