Line data Source code
1 : /*
2 : MIT License
3 :
4 : Copyright (c) 2026 - A bunch of nerds
5 :
6 : Permission is hereby granted, free of charge, to any person obtaining a copy
7 : of this software and associated documentation files (the "Software"), to deal
8 : in the Software without restriction, including without limitation the rights
9 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 : copies of the Software, and to permit persons to whom the Software is
11 : furnished to do so, subject to the following conditions:
12 :
13 : The above copyright notice and this permission notice shall be included in all
14 : copies or substantial portions of the Software.
15 :
16 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 : SOFTWARE.
23 : */
24 :
25 : #pragma once
26 :
27 : // INTERNAL ONLY. The UTF-8 well-formedness check that cjson and cyaml share,
28 : // so the two modules can never disagree about which bytes count as UTF-8.
29 : // Extend this file instead of writing a check of your own. Every function is
30 : // static inline and nothing here is installed or exported.
31 : #ifndef CCOL_CUTF8_H
32 : #define CCOL_CUTF8_H
33 :
34 : // C23 has bool, true and false as keywords.
35 : #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 202311L
36 : #include <stdbool.h>
37 : #endif
38 : #include <stddef.h>
39 : #include <stdio.h>
40 :
41 : // Steps over one UTF-8 sequence at s, where n > 0 bytes are readable; every
42 : // other function here is built on it. For a well-formed sequence it returns its
43 : // length (1 to 4) and sets *ok. For an ill-formed one it clears *ok and returns
44 : // the length of the maximal subpart (at least 1), the unit that Unicode
45 : // replaces with one U+FFFD. The lead byte table (trail count plus the range of
46 : // the first continuation byte) refuses stray continuation bytes, overlong
47 : // forms, surrogates and anything above U+10FFFF without decoding the value.
48 15700 : static inline size_t ccol_utf8_step(const unsigned char *s, size_t n,
49 : bool *ok) {
50 15700 : unsigned char b0 = s[0];
51 15700 : *ok = true;
52 15700 : if (b0 < 0x80) return 1;
53 :
54 15700 : size_t trail;
55 15700 : unsigned char lo, hi;
56 15700 : if (b0 >= 0xC2 && b0 <= 0xDF) {
57 : trail = 1, lo = 0x80, hi = 0xBF;
58 14024 : } else if (b0 == 0xE0) {
59 : trail = 2, lo = 0xA0, hi = 0xBF;
60 13963 : } else if (b0 == 0xED) {
61 : trail = 2, lo = 0x80, hi = 0x9F;
62 13834 : } else if (b0 >= 0xE1 && b0 <= 0xEF) {
63 : trail = 2, lo = 0x80, hi = 0xBF;
64 12189 : } else if (b0 == 0xF0) {
65 : trail = 3, lo = 0x90, hi = 0xBF;
66 11360 : } else if (b0 == 0xF4) {
67 : trail = 3, lo = 0x80, hi = 0x8F;
68 11288 : } else if (b0 >= 0xF1 && b0 <= 0xF3) {
69 : trail = 3, lo = 0x80, hi = 0xBF;
70 : } else {
71 11150 : *ok = false;
72 11150 : return 1;
73 : }
74 :
75 4550 : if (n < 2 || s[1] < lo || s[1] > hi) {
76 3641 : *ok = false;
77 3641 : return 1;
78 : }
79 1168 : for (size_t i = 2; i <= trail; i++) {
80 739 : if (n <= i || s[i] < 0x80 || s[i] > 0xBF) {
81 480 : *ok = false;
82 480 : return i;
83 : }
84 : }
85 429 : return trail + 1;
86 : }
87 :
88 : // Tells whether the n-byte buffer s is well-formed UTF-8, for the API calls of
89 : // cjson and cyaml that refuse a C string that isn't.
90 1567646 : static inline bool ccol_utf8_is_valid(const char *s, size_t n) {
91 1567646 : const unsigned char *p = (const unsigned char *)s;
92 1567646 : size_t i = 0;
93 9403281 : while (i < n) {
94 7835764 : if (p[i] < 0x80) {
95 7835516 : i++;
96 7835516 : continue;
97 : }
98 248 : bool ok;
99 248 : size_t step = ccol_utf8_step(p + i, n - i, &ok);
100 248 : if (!ok) return false;
101 119 : i += step;
102 : }
103 : return true;
104 : }
105 :
106 : // Gives the offset of the first byte of s that starts an ill-formed sequence,
107 : // or n when the whole buffer is well-formed, so a parser can report where its
108 : // input goes wrong. ASCII costs one comparison per byte.
109 1038 : static inline size_t ccol_utf8_first_ill_formed(const char *s, size_t n) {
110 1038 : const unsigned char *p = (const unsigned char *)s;
111 1038 : size_t i = 0;
112 1059 : while (i < n) {
113 1055 : if (p[i] < 0x80) {
114 14 : i++;
115 14 : continue;
116 : }
117 1041 : bool ok;
118 1041 : size_t step = ccol_utf8_step(p + i, n - i, &ok);
119 1041 : if (!ok) return i;
120 7 : i += step;
121 : }
122 : return n;
123 : }
124 :
125 : // Names, for a parse error, what is wrong with the ill-formed sequence at p[at]
126 : // (at < len). It returns a static phrase and writes the bytes that show the
127 : // defect into detail (detail_size bytes), each as 0xNN, so the message is
128 : // printable ASCII whatever the input holds. A lead byte whose maximal subpart
129 : // is 1 while the next byte is a continuation byte has a first continuation byte
130 : // outside its narrow range: A surrogate, a value above U+10FFFF, or an overlong
131 : // form.
132 5392 : static inline const char *ccol_utf8_describe_ill_formed(const unsigned char *p,
133 : size_t len, size_t at,
134 : char *detail,
135 : size_t detail_size) {
136 5392 : unsigned char c = p[at];
137 5392 : unsigned char c1 = at + 1 < len ? p[at + 1] : 0;
138 5392 : bool ok;
139 5392 : size_t step = ccol_utf8_step(p + at, len - at, &ok);
140 5392 : if (c <= 0xBF) {
141 3512 : snprintf(detail, detail_size, "0x%02x", c);
142 3512 : return "continuation byte with no lead byte";
143 : }
144 1880 : if (c <= 0xC1) {
145 35 : snprintf(detail, detail_size, "lead byte 0x%02x", c);
146 35 : return "overlong encoding";
147 : }
148 1845 : if (c >= 0xF5) {
149 206 : snprintf(detail, detail_size, "0x%02x", c);
150 206 : return "byte that never appears in UTF-8";
151 : }
152 1639 : if (at + step >= len) {
153 5 : snprintf(detail, detail_size, "lead byte 0x%02x", c);
154 5 : return "sequence truncated by the end of the input";
155 : }
156 1634 : if (step == 1 && c1 >= 0x80 && c1 <= 0xBF) {
157 60 : snprintf(detail, detail_size, "bytes 0x%02x 0x%02x", c, c1);
158 60 : return (c == 0xED) ? "encoded surrogate code point"
159 60 : : (c == 0xF4) ? "code point above U+10FFFF"
160 : : "overlong encoding";
161 : }
162 1574 : snprintf(detail, detail_size,
163 : "lead byte 0x%02x, then 0x%02x where a continuation byte is "
164 : "expected",
165 1574 : c, p[at + step]);
166 1574 : return "truncated sequence";
167 : }
168 :
169 : #endif
|