Debian Codegraph @

src/scanner-utils.h

raw
1 /*
2 * Copyright © 2012 Ran Benita <[email protected]>
3 * SPDX-License-Identifier: MIT
4 */
5 #pragma once
6
7 #include "config.h"
8
9 #include <stdbool.h>
10 #include <stddef.h>
11 #include <stdint.h>
12 #include <string.h>
13
14 #include "context.h"
15 #include "darray.h"
16 #include "messages-codes.h"
17 #include "utils.h"
18 #include "utils-numbers.h"
19 #include "utf8.h"
20
21 /* Point to some substring in the file; used to avoid copying. */
22 struct sval {
23 const char *start;
24 size_t len;
25 };
26 typedef darray(struct sval) darray_sval;
27
28 static inline bool
29 svaleq(struct sval s1, struct sval s2)
30 {
31 return s1.len == s2.len && memcmp(s1.start, s2.start, s1.len) == 0;
32 }
33
34 static inline bool
35 isvaleq(struct sval s1, struct sval s2)
36 {
37 return s1.len == s2.len && istrncmp(s1.start, s2.start, s1.len) == 0;
38 }
39
40 static inline bool
41 svaleq_prefix(struct sval s1, struct sval s2)
42 {
43 return s1.len <= s2.len && memcmp(s1.start, s2.start, s1.len) == 0;
44 }
45
46 #define SVAL(start, len) (struct sval){(start), len}
47 #define SVAL_LIT(literal) SVAL(literal, sizeof(literal) - 1)
48 #define SVAL_INIT(literal) { literal, sizeof(literal) - 1 }
49
50 /* A line:column location in the input string (1-based). */
51 struct scanner_loc {
52 size_t line, column;
53 };
54
55 struct scanner {
56 const char *s;
57 size_t pos;
58 size_t len;
59 /*
60 * Internal buffer.
61 * Since this is used to handle paths that are possibly absolute, in theory
62 * we should size it to PATH_MAX. However it is very unlikely to reach such
63 * long paths in our context.
64 */
65 char buf[1024];
66 size_t buf_pos;
67 /* The position of the start of the current token. */
68 size_t token_pos;
69 /* Cached values used by scanner_token_location. */
70 size_t cached_pos;
71 struct scanner_loc cached_loc;
72 const char *file_name;
73 struct xkb_context *ctx;
74 void *priv;
75 };
76
77 /* Compute the line:column location for the current token (slow). */
78 struct scanner_loc
79 scanner_token_location(struct scanner *s);
80
81 #define scanner_log_with_code(scanner, level, verbosity, log_msg_id, fmt, ...) do { \
82 struct scanner_loc loc = scanner_token_location((scanner)); \
83 xkb_log_with_code((scanner)->ctx, (level), verbosity, log_msg_id, \
84 "%s:%zu:%zu: " fmt "\n", \
85 (scanner)->file_name, \
86 loc.line, \
87 loc.column, ##__VA_ARGS__); \
88 } while(0)
89
90 #define scanner_err(scanner, id, fmt, ...) \
91 scanner_log_with_code(scanner, XKB_LOG_LEVEL_ERROR, \
92 XKB_LOG_VERBOSITY_MINIMAL, id, fmt, ##__VA_ARGS__)
93
94 #define scanner_warn(scanner, id, fmt, ...) \
95 scanner_log_with_code(scanner, XKB_LOG_LEVEL_WARNING, \
96 XKB_LOG_VERBOSITY_MINIMAL, id, fmt, ##__VA_ARGS__)
97
98 #define scanner_vrb(scanner, verbosity, id, fmt, ...) \
99 scanner_log_with_code(scanner, XKB_LOG_LEVEL_WARNING, verbosity, id, \
100 fmt, ##__VA_ARGS__)
101
102 static inline void
103 scanner_init(struct scanner *s, struct xkb_context *ctx,
104 const char *string, size_t len, const char *file_name,
105 void *priv)
106 {
107 s->s = string;
108 s->len = len;
109 s->pos = 0;
110 s->token_pos = 0;
111 s->cached_pos = 0;
112 s->cached_loc.line = s->cached_loc.column = 1;
113 s->file_name = file_name;
114 s->ctx = ctx;
115 s->priv = priv;
116 }
117
118 static inline char
119 scanner_peek(struct scanner *s)
120 {
121 if (unlikely(s->pos >= s->len))
122 return '\0';
123 return s->s[s->pos];
124 }
125
126 static inline bool
127 scanner_eof(struct scanner *s)
128 {
129 return s->pos >= s->len;
130 }
131
132 static inline bool
133 scanner_eol(struct scanner *s)
134 {
135 return scanner_peek(s) == '\n';
136 }
137
138 static inline void
139 scanner_skip_to_eol(struct scanner *s)
140 {
141 const char *nl = memchr(s->s + s->pos, '\n', s->len - s->pos);
142 const size_t new_pos = nl ? (size_t) (nl - s->s) : s->len;
143 s->pos = new_pos;
144 }
145
146 static inline char
147 scanner_next(struct scanner *s)
148 {
149 if (unlikely(scanner_eof(s)))
150 return '\0';
151 return s->s[s->pos++];
152 }
153
154 static inline bool
155 scanner_chr(struct scanner *s, char ch)
156 {
157 if (likely(scanner_peek(s) != ch))
158 return false;
159 s->pos++;
160 return true;
161 }
162
163 static inline bool
164 scanner_str(struct scanner *s, const char *string, size_t len)
165 {
166 if (s->len - s->pos < len)
167 return false;
168 if (memcmp(s->s + s->pos, string, len) != 0)
169 return false;
170 s->pos += len;
171 return true;
172 }
173
174 #define scanner_lit(s, literal) scanner_str(s, literal, sizeof(literal) - 1)
175
176 static inline bool
177 scanner_buf_append(struct scanner *s, char ch)
178 {
179 if (s->buf_pos + 1 >= sizeof(s->buf))
180 return false;
181 s->buf[s->buf_pos++] = ch;
182 return true;
183 }
184
185 static inline bool
186 scanner_buf_appends(struct scanner *s, const char *str)
187 {
188 int ret;
189 ret = snprintf(s->buf + s->buf_pos, sizeof(s->buf) - s->buf_pos, "%s", str);
190 if (ret < 0 || (size_t) ret >= sizeof(s->buf) - s->buf_pos)
191 return false;
192 s->buf_pos += ret;
193 return true;
194 }
195
196 static inline bool
197 scanner_buf_appends_code_point(struct scanner *s, uint32_t c)
198 {
199 /* Up to 4 bytes + NULL */
200 if (s->buf_pos + 5 <= sizeof(s->buf)) {
201 int count = utf32_to_utf8(c, s->buf + s->buf_pos);
202 if (count == 0) {
203 /* Handle encoding failure with U+FFFD REPLACEMENT CHARACTER */
204 count = utf32_to_utf8(0xfffd, s->buf + s->buf_pos);
205 }
206 if (count == 0)
207 return false;
208 /* `count` counts the NULL byte */
209 s->buf_pos += count - 1;
210 return true;
211 } else {
212 return false;
213 }
214 }
215
216 static inline bool
217 scanner_oct(struct scanner *s, uint8_t *out)
218 {
219 uint8_t i = 0;
220 uint8_t c = 0;
221 for (; scanner_peek(s) >= '0' && scanner_peek(s) <= '7' && i < 4; i++) {
222 /* Test overflow */
223 if (c < 040) {
224 c = c * 8 + scanner_next(s) - '0';
225 } else {
226 /* Consume valid digit, but mark result as invalid */
227 scanner_next(s);
228 *out = c;
229 return false;
230 }
231 }
232 *out = c;
233 return i > 0;
234 }
235
236 static inline bool
237 scanner_hex(struct scanner *s, uint8_t *out)
238 {
239 uint8_t i;
240 for (i = 0, *out = 0; is_xdigit(scanner_peek(s)) && i < 2; i++) {
241 const char c = scanner_next(s);
242 const char offset = (char) (c >= '0' && c <= '9' ? '0' :
243 c >= 'a' && c <= 'f' ? 'a' - 10
244 : 'A' - 10);
245 *out = *out * 16 + c - offset;
246 }
247 return i > 0;
248 }
249
250 static inline int
251 scanner_dec_int64(struct scanner *s, int64_t *out)
252 {
253 uint64_t val = 0;
254 const int count =
255 parse_dec_to_uint64_t(s->s + s->pos, s->len - s->pos, &val);
256 if (count > 0) {
257 /*
258 * NOTE: Since the sign is handled as a token, we parse only *positive*
259 * values here. So that means that *we cannot parse INT64_MIN*,
260 * because abs(INT64_MIN) > INT64_MAX. But we use 64-bit integers
261 * only to avoid under/overflow in expressions: we do not use
262 * 64-bit integers in the keymap, only up to 32 bits. So we expect
263 * to parse only 32 bits integers in realistic keymap files and
264 * the limitation should not be an issue.
265 */
266 if (val > INT64_MAX)
267 return -1;
268 s->pos += count;
269 *out = (int64_t) val;
270 }
271 return count;
272 }
273
274 static inline int
275 scanner_hex_int64(struct scanner *s, int64_t *out)
276 {
277 uint64_t val = 0;
278 const int count =
279 parse_hex_to_uint64_t(s->s + s->pos, s->len - s->pos, &val);
280 if (count > 0) {
281 /* See comment in `scanner_dec_int64()` above */
282 if (val > INT64_MAX)
283 return -1;
284 s->pos += count;
285 *out = (int64_t) val;
286 }
287 return count;
288 }
289
290 /** Parser for the {NNNN} part of a Unicode escape sequences */
291 static inline bool
292 scanner_unicode_code_point(struct scanner *s, uint32_t *out)
293 {
294 if (!scanner_chr(s, '{'))
295 return false;
296
297 uint32_t cp = 0;
298 const int count =
299 parse_hex_to_uint32_t(s->s + s->pos, s->len - s->pos, &cp);
300 if (count > 0)
301 s->pos += count;
302
303 /* Try to consume everything within the string until the next `}` */
304 const size_t last_valid = s->pos;
305 while (!scanner_eof(s) && !scanner_eol(s) && scanner_peek(s) != '"' &&
306 scanner_peek(s) != '}') {
307 scanner_next(s);
308 }
309
310 if (scanner_chr(s, '}')) {
311 /* End of the escape sequence; code point may be invalid */
312 *out = cp;
313 return (count > 0 && s->pos == last_valid + 1 && cp <= 0x10ffff);
314 }
315
316 /* No closing `}` within the string: rollback to last valid position */
317 s->pos = last_valid;
318 return false;
319 }
320
321 /* Basic detection of wrong character encoding based on the first bytes */
322 static inline bool
323 scanner_check_supported_char_encoding(struct scanner *scanner)
324 {
325 /* Skip UTF-8 encoded BOM (U+FEFF)
326 * See: https://www.unicode.org/faq/utf_bom.html#bom5 */
327 if (scanner_str(scanner, "\xef\xbb\xbf", 3) || scanner->len < 2) {
328 /* Assume UTF-8 encoding or trivial short input */
329 return true;
330 }
331
332 /* Early detection of wrong file encoding, e.g. UTF-16 or UTF-32 */
333 if (scanner->s[0] == '\0' || scanner->s[1] == '\0') {
334 scanner_err(scanner, XKB_ERROR_INVALID_FILE_ENCODING,
335 "unexpected NULL character.");
336 return false;
337 }
338 /* Enforce the first character to be ASCII.
339 See the note before the use of this function, that explains the relevant
340 parts of the grammars of rules, keymap components and Compose. */
341 if (!is_ascii(scanner->s[0])) {
342 scanner_err(scanner, XKB_ERROR_INVALID_FILE_ENCODING,
343 "unexpected non-ASCII character.");
344 return false;
345 }
346
347 return true;
348 }