-
Notifications
You must be signed in to change notification settings - Fork 2
/
tokens_match.h
462 lines (451 loc) · 11.5 KB
/
tokens_match.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
* Copyright © 2021 - 2023 Eero Häkkinen <Eero+pam-ssh-auth-info@Häkkinen.fi>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <stdbool.h>
#include <string.h>
#include "pattern.h"
struct tokens_match_config {
bool allow_prefix_match;
struct {
struct tokens_match_config_set {
size_t len;
char const *ptr;
} pattern_separator, token_separator;
} sets;
};
static bool
in_tokens_match_config_set(
struct tokens_match_config_set const *const set,
char const ch
) {
return memchr(set->ptr, ch, set->len) != NULL;
}
static bool
is_pattern_separator(
struct tokens_match_config const *const config,
char const ch
) {
return in_tokens_match_config_set(&config->sets.pattern_separator, ch);
}
static bool
is_token_separator(
struct tokens_match_config const *const config,
char const ch
) {
return in_tokens_match_config_set(&config->sets.token_separator, ch);
}
static bool
is_end_of_token(
struct tokens_match_config const *const config,
char const *const tokens,
char const *const tokens_end
) {
assert(tokens <= tokens_end);
return tokens >= tokens_end || is_token_separator(config, *tokens);
}
/* Check if the tokens match the pattern.
*
* Any character byte that appears in a pattern, other than the extended
* patterns and the special pattern characters described below and the pattern
* separator characters, matches itself.
*
* The pattern separator character bytes match themselves and token
* separator character bytes.
*
* The special pattern characters:
*
* * Matches any number of (including zero) token character bytes but not
* a token separator.
* ? Matches any token character byte but not a token separator.
* [ A pair of brackets introduces a character byte class.
* A character byte class ([...]) matches any token character byte in
* the class. A complemented character byte class ([!...]) matches any
* token character byte not in the class. Neither matches a token
* separator. A class may contain character bytes ([abcde]) and character
* byte ranges ([a-e]).
* \ Preserves the literal meaning of the following character byte.
*
* The extended patterns:
*
* ?(pattern|pattern|...) Matches zero or one occurence of the given
* patterns.
* Does not match a token separator.
* *(pattern|pattern|...) Matches zero or more occurences of the given
* patterns.
* Does not match a token separator.
* +(pattern|pattern|...) Matches one or more occurences of the given
* patterns.
* Does not match a token separator.
* @(pattern|pattern|...) Matches one of the given patterns.
* Does not match a token separator.
* !(pattern|pattern|...) Matches anything except one occurence of the given
* patterns.
* Does not match a token separator.
*/
static bool
tokens_match(
struct tokens_match_config const *const config,
char const *tokens,
char const *const tokens_end,
char const *pattern,
char const *const pattern_end,
unsigned const recursion_limit
);
static bool
token_match_pattern_list(
char const *const token,
char const *const token_end,
char const *const pattern_list,
char const *const pattern_list_end,
unsigned const recursion_limit
) {
/* The token does not contain separators.
* Therefore, a zero config is enough.
*/
static struct tokens_match_config const config = {
false,
{{0, ""}, {0, ""}}
};
char const *pattern = pattern_list;
for (;;) {
char const *pattern_end = find_in_pattern(
pattern,
pattern_list_end,
'|',
pattern_list_end
);
if (tokens_match(
&config,
token,
token_end,
pattern,
pattern_end,
recursion_limit
))
return true;
if (pattern_end == pattern_list_end)
break;
pattern = pattern_end + 1;
}
return false;
}
static bool
tokens_match_extended_pattern(
struct tokens_match_config const *const config,
char const *tokens,
char const *const tokens_end,
struct extended_pattern_info const *const info,
char const *const pattern,
char const *const pattern_end,
unsigned const recursion_limit,
unsigned count
) {
char const *tokens_tail;
if (info->count.max == 0) { /* !(...) */
/* Try to split the tokens to a head and a tail so that
* 1) the tokens head is a token or a token prefix
* (does not contain a token separator),
* 2) the tokens head does not match any of the patterns in
* the extended pattern and
* 3) the tokens tail matches the rest of the pattern.
*/
for (tokens_tail = tokens;; ++tokens_tail) {
if (
!token_match_pattern_list(
tokens,
tokens_tail,
info->begin,
info->end,
recursion_limit
) &&
tokens_match(
config,
tokens_tail,
tokens_end,
pattern,
pattern_end,
recursion_limit
)
)
return true;
if (is_end_of_token(config, tokens_tail, tokens_end))
return false;
}
}
/* If there are not enough occurences or
* if the tokens do not match the rest of the pattern,
* try to split the tokens to a head and a tail so that
* 1) the tokens head is a token or a token prefix
* (does not contain a token separator),
* 2) the tokens head matches at least one of the patterns in
* the extended pattern and
* 3) the tokens tail matches the extended pattern with increased
* occurence count.
*/
for (tokens_tail = tokens;;) {
size_t const match_len = (size_t)(tokens_tail - tokens);
if (tokens_tail == tokens) {
if (
count >= info->count.min &&
tokens_match(
config,
tokens,
tokens_end,
pattern,
pattern_end,
recursion_limit
)
)
/* There are enough occurences and
* the tokens match the rest of the pattern.
*/
return true;
if (count >= info->count.max)
/* No more occurences can be found.
*/
return false;
}
if (match_len > info->match_len.max)
/* No more matches can be found.
*/
return false;
if (
match_len >= info->match_len.min &&
(match_len || count < info->count.min) &&
token_match_pattern_list(
tokens,
tokens_tail,
info->begin,
info->end,
recursion_limit
)
) {
/* The tokens head matches the pattern list and
* either the tokens head is not empty or the occurence
* count must be increased.
*/
if (
match_len == info->match_len.max ||
is_end_of_token(
config,
tokens_tail,
tokens_end
)
) {
/* Tail call optimization.
*/
++count;
tokens = tokens_tail;
continue;
}
if (
recursion_limit &&
tokens_match_extended_pattern(
config,
tokens_tail,
tokens_end,
info,
pattern,
pattern_end,
recursion_limit - 1,
count + 1
)
)
/* The tokens tail matches the extended pattern
* with increased occurence count.
*/
return true;
}
if (is_end_of_token(config, tokens_tail, tokens_end))
return false;
++tokens_tail;
}
}
static bool
tokens_match(
struct tokens_match_config const *const config,
char const *tokens,
char const *const tokens_end,
char const *pattern,
char const *const pattern_end,
unsigned const recursion_limit
) {
for (; pattern < pattern_end; ++pattern) {
struct character_class_info character_class;
struct extended_pattern_info extended_pattern;
if (is_extended_pattern(
pattern,
pattern_end,
&extended_pattern
)) {
if (!recursion_limit)
return false;
pattern = extended_pattern.end + 1;
return tokens_match_extended_pattern(
config,
tokens,
tokens_end,
&extended_pattern,
pattern,
pattern_end,
recursion_limit - 1,
0
);
}
switch (pattern[0]) {
case '*':
/* An asterisk matches any number of (including zero)
* token character bytes but not a token separator.
*/
while (pattern < pattern_end && pattern[0] == '*')
++pattern;
if (pattern == pattern_end) {
assert(tokens <= tokens_end);
if (config->allow_prefix_match)
/* An asterisk matches remaining token
* character bytes to the end of
* the token and an empty pattern
* matches there.
*/
return true;
for (; tokens < tokens_end; ++tokens) {
if (is_token_separator(
config,
*tokens
))
return false;
}
return true;
}
if (!recursion_limit)
return false;
for (;; ++tokens) {
if (tokens_match(
config,
tokens,
tokens_end,
pattern,
pattern_end,
recursion_limit - 1
))
return true;
if (is_end_of_token(
config,
tokens,
tokens_end
))
return false;
}
assert(false);
case '?':
/* A question mark matches any token character byte but
* not a token separator.
*/
if (is_end_of_token(config, tokens, tokens_end))
return false;
++tokens;
continue;
case '[':
if (!is_character_class(
pattern,
pattern_end,
&character_class
))
/* An opening bracket ([) without a matching
* closing bracket (]) is not a character byte
* class.
*/
break;
/* A character byte class ([...]) matches any token
* character byte in the class.
* A complemented character byte class ([!...]) matches
* any token character byte not in the class.
* Neither matches a token separator.
*/
if (is_end_of_token(config, tokens, tokens_end))
return false;
pattern = character_class.begin;
do {
if (pattern[1] == '-' && pattern[2] != ']') {
/* A character byte range.
*/
if (
*tokens >= pattern[0] &&
*tokens <= pattern[2]
)
break;
pattern += 3;
}
else {
/* A character byte.
*/
if (*tokens == pattern[0])
break;
++pattern;
}
} while (pattern < character_class.end);
assert(pattern <= character_class.end);
if (
character_class.negation
? pattern < character_class.end
: pattern >= character_class.end
)
return false;
pattern = character_class.end;
++tokens;
continue;
case '\\':
/* A backslash preserves the literal meaning of
* the following character byte.
*/
if (pattern_end - pattern >= 2)
++pattern;
break;
default:
if (is_pattern_separator(config, pattern[0])) {
/* A pattern separator character byte matches
* itself or a token separator character byte.
*/
assert(tokens <= tokens_end);
if (tokens >= tokens_end)
return false;
if (!(
*tokens == pattern[0] ||
is_token_separator(config, *tokens)
))
return false;
++tokens;
continue;
}
}
assert(tokens <= tokens_end);
if (tokens >= tokens_end)
return false;
if (*tokens != pattern[0])
return false;
++tokens;
}
/* The end of the pattern.
*/
assert(tokens <= tokens_end);
if (tokens >= tokens_end)
return true;
if (config->allow_prefix_match && is_token_separator(config, *tokens))
return true;
return false;
}