forked from eehakkin/pam-ssh-auth-info
-
Notifications
You must be signed in to change notification settings - Fork 0
/
line_tokens_match.h
354 lines (347 loc) · 9.12 KB
/
line_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
/*
* Copyright © 2021 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 version 3
* as published by the Free Software Foundation.
*
* 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"
/* Check if the initial tokens on the first line matches the pattern.
*
* Any character byte that appears in a pattern, other than the extended
* patterns and the special pattern characters described below, matches
* itself.
*
* The special pattern characters:
*
* * Matches any number of (including zero) token character bytes but not
* a token separator (space).
* = Matches equal-sign (=) or a token separator (space).
* ? Matches any token character byte but not a token separator (space).
* [ 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
* (space). 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 (space).
* *(pattern|pattern|...) Matches zero or more occurences of the given
* patterns.
* Does not match a token separator (space).
* +(pattern|pattern|...) Matches one or more occurences of the given
* patterns.
* Does not match a token separator (space).
* @(pattern|pattern|...) Matches one of the given patterns.
* Does not match a token separator (space).
* !(pattern|pattern|...) Matches anything except one of the given
* patterns or a token separator (space).
*/
static bool
initial_line_tokens_match(
char const *line,
char const *line_end,
char const *prefix_pattern,
char const *const prefix_pattern_end
);
static bool
initial_line_tokens_match_pattern_list(
char const *line,
char const *line_end,
char const *prefix_pattern_list,
char const *const prefix_pattern_list_end
) {
char const *prefix_pattern = prefix_pattern_list;
for (;;) {
char const *prefix_pattern_end = find_in_pattern(
prefix_pattern,
prefix_pattern_list_end,
'|'
);
if (!prefix_pattern_end)
prefix_pattern_end = prefix_pattern_list_end;
if (initial_line_tokens_match(
line,
line_end,
prefix_pattern,
prefix_pattern_end
))
return true;
if (prefix_pattern_end == prefix_pattern_list_end)
break;
prefix_pattern = prefix_pattern_end + 1;
}
return false;
}
static bool
initial_line_tokens_match_extended_pattern(
char const *const line,
char const *const line_end,
struct extended_pattern_info const *const info,
char const *const prefix_pattern_end,
unsigned const count
) {
char const *line_suffix = line;
if (info->max == 0) { /* !(...) */
for (;; ++line_suffix) {
if (
!initial_line_tokens_match_pattern_list(
line,
line_suffix,
info->list,
info->list_end
) &&
initial_line_tokens_match(
line_suffix,
line_end,
info->list_end + 1,
prefix_pattern_end
)
)
return true;
if (line_suffix >= line_end || *line_suffix == ' ')
/* The end of a line or the end of a token.
*/
return false;
}
}
else {
if (
count >= info->min &&
initial_line_tokens_match(
line,
line_end,
info->list_end + 1,
prefix_pattern_end
)
)
return true;
if (count >= info->max)
/* No more occurences can be found.
*/
return false;
if (count >= info->min) {
/* Enough occurences have been found
* thus skip empty occurences as they would not change
* anything.
*/
if (line_suffix >= line_end || *line_suffix == ' ')
/* The end of a line or the end of a token.
*/
return false;
++line_suffix;
}
for (;; ++line_suffix) {
if (
initial_line_tokens_match_pattern_list(
line,
line_suffix,
info->list,
info->list_end
) &&
initial_line_tokens_match_extended_pattern(
line_suffix,
line_end,
info,
prefix_pattern_end,
count + 1
)
)
return true;
if (line_suffix >= line_end || *line_suffix == ' ')
/* The end of a line or the end of a token.
*/
return false;
}
}
}
static bool
initial_line_tokens_match(
char const *line,
char const *line_end,
char const *prefix_pattern,
char const *const prefix_pattern_end
) {
for (; prefix_pattern < prefix_pattern_end; ++prefix_pattern) {
struct character_class_info character_class;
struct extended_pattern_info extended_pattern;
if (line < line_end && *line == ' ') {
/* A token separator must be matched explicitly.
*/
if (
prefix_pattern_end - prefix_pattern >= 2 &&
prefix_pattern[0] == '\\' &&
prefix_pattern[1] == ' '
)
++prefix_pattern;
if (
prefix_pattern[0] == ' ' ||
prefix_pattern[0] == '='
) {
++line;
continue;
}
/* The end of a token without an explicitly matched
* token separator is the end of the initial tokens.
*/
line_end = line;
}
if (is_extended_pattern(
prefix_pattern,
prefix_pattern_end,
&extended_pattern
)) {
return initial_line_tokens_match_extended_pattern(
line,
line_end,
&extended_pattern,
prefix_pattern_end,
0
);
}
if (prefix_pattern[0] == '*') {
/* An asterisk matches any number of (including zero)
* token character bytes but not a token separator
* (space).
*/
while (
prefix_pattern < prefix_pattern_end &&
prefix_pattern[0] == '*'
)
++prefix_pattern;
for (;; ++line) {
if (initial_line_tokens_match(
line,
line_end,
prefix_pattern,
prefix_pattern_end
))
return true;
if (line >= line_end || *line == ' ')
break;
}
/* The end of a line or the end of a token without
* an explicitly matched token separator (space)
* but not the end of the pattern.
*/
assert(prefix_pattern < prefix_pattern_end);
return false;
}
if (line >= line_end) {
/* The end of a line but not the end of the pattern.
*/
assert(prefix_pattern < prefix_pattern_end);
return false;
}
switch (prefix_pattern[0]) {
case '?':
/* A question mark matches any token character byte but
* not a token separator (space).
*/
assert(line < line_end);
assert(*line != ' ');
++line;
continue;
case '[':
if (!is_character_class(
prefix_pattern,
prefix_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 (space).
*/
assert(line < line_end);
assert(*line != ' ');
prefix_pattern = character_class.begin;
do {
if (
prefix_pattern[1] == '-' &&
prefix_pattern[2] != ']'
) {
/* A character byte range.
*/
if (
*line >= prefix_pattern[0] &&
*line <= prefix_pattern[2]
)
break;
prefix_pattern += 3;
}
else {
/* A character byte.
*/
if (*line == prefix_pattern[0])
break;
++prefix_pattern;
}
} while (prefix_pattern < character_class.end);
assert(prefix_pattern <= character_class.end);
if (
character_class.negation
? prefix_pattern < character_class.end
: prefix_pattern >= character_class.end
)
return false;
prefix_pattern = character_class.end;
++line;
continue;
case '\\':
/* A backslash preserves the literal meaning of
* the following character byte.
*/
assert(line < line_end);
assert(*line != ' ');
if (prefix_pattern_end - prefix_pattern >= 2)
++prefix_pattern;
break;
default:
break;
}
if (*line != prefix_pattern[0])
return false;
++line;
}
/* The end of the pattern.
* Accept at the end of a line and at the end of a token.
*/
assert(line <= line_end);
return line >= line_end || *line == ' ';
}
static bool
initial_first_line_tokens_match(
char const *lines,
char const *prefix_pattern
) {
return initial_line_tokens_match(
lines,
lines + strcspn(lines, "\n"),
prefix_pattern,
prefix_pattern + strlen(prefix_pattern)
);
}