-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphp_grep_upstream.c
More file actions
649 lines (548 loc) · 15.8 KB
/
Copy pathphp_grep_upstream.c
File metadata and controls
649 lines (548 loc) · 15.8 KB
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
#include "php.h"
#include "php_grep_upstream.h"
#include "vendor/grep/config.h"
#include "vendor/grep/gnulib/lib/dfa.h"
#include "vendor/grep/lib/idx.h"
#include "vendor/grep/lib/kwset.h"
#include "vendor/grep/lib/localeinfo.h"
#include <regex.h>
#include <ctype.h>
#include <string.h>
#include <wchar.h>
/* Minimal subset of GNU grep globals needed by the matcher objects. */
bool match_icase = false;
bool match_words = false;
bool match_lines = false;
char eolbyte = '\n';
struct localeinfo localeinfo;
/* GNU grep matcher entrypoints provided by upstream objects. */
extern void wordinit(void);
extern void *Fcompile(char *pattern, idx_t size, reg_syntax_t ignored, bool exact);
extern void *GEAcompile(char *pattern, idx_t size, reg_syntax_t syntax_bits, bool exact);
extern ptrdiff_t Fexecute(void *vcp, char const *buf, idx_t size, idx_t *match_size,
char const *start_ptr);
extern ptrdiff_t EGexecute(void *vcp, char const *buf, idx_t size, idx_t *match_size,
char const *start_ptr);
/*
* GNU grep's kwsearch object is private to kwsearch.c. We mirror it here only
* so the extension can destroy compiled fixed-string programs correctly.
*/
struct kwsearch
{
kwset_t kwset;
idx_t words;
char *pattern;
idx_t size;
void *re;
};
struct dfa_comp
{
kwset_t kwset;
struct dfa *dfa;
struct re_pattern_buffer *patterns;
idx_t pcount;
struct re_registers regs;
idx_t kwset_exact_matches;
bool begline;
};
static bool php_grep_upstream_initialized = false;
typedef struct _php_grep_upstream_rewrite {
char *buffer;
size_t len;
size_t capacity;
} php_grep_upstream_rewrite;
static bool
php_grep_upstream_is_word_byte(unsigned char c)
{
return isalnum(c) || c == '_';
}
static bool
php_grep_upstream_match_boundary_ok(char const *subject, size_t line_len, size_t start, size_t length)
{
unsigned char before;
unsigned char after;
if (!match_words) {
return true;
}
before = start > 0 ? (unsigned char) subject[start - 1] : '\0';
after = start + length < line_len ? (unsigned char) subject[start + length] : '\0';
return (start == 0 || !php_grep_upstream_is_word_byte(before))
&& (start + length == line_len || !php_grep_upstream_is_word_byte(after));
}
static bool
php_grep_upstream_rewrite_reserve(php_grep_upstream_rewrite *rewrite, size_t additional)
{
size_t needed;
size_t new_capacity;
char *new_buffer;
if (rewrite->capacity - rewrite->len > additional) {
return true;
}
needed = rewrite->len + additional + 1;
new_capacity = rewrite->capacity == 0 ? 64 : rewrite->capacity;
while (new_capacity < needed) {
new_capacity *= 2;
}
new_buffer = rewrite->buffer == NULL ? malloc(new_capacity) : realloc(rewrite->buffer, new_capacity);
if (new_buffer == NULL) {
free(rewrite->buffer);
rewrite->buffer = NULL;
rewrite->len = 0;
rewrite->capacity = 0;
php_error_docref(NULL, E_ERROR, "failed to allocate GNU grep pattern rewrite buffer");
return false;
}
rewrite->buffer = new_buffer;
rewrite->capacity = new_capacity;
return true;
}
static bool
php_grep_upstream_rewrite_append_literal(php_grep_upstream_rewrite *rewrite, char ch)
{
if (!php_grep_upstream_rewrite_reserve(rewrite, 1)) {
return false;
}
rewrite->buffer[rewrite->len++] = ch;
return true;
}
static bool
php_grep_upstream_rewrite_append_string(php_grep_upstream_rewrite *rewrite, char const *value)
{
size_t value_len = strlen(value);
if (!php_grep_upstream_rewrite_reserve(rewrite, value_len)) {
return false;
}
memcpy(rewrite->buffer + rewrite->len, value, value_len);
rewrite->len += value_len;
return true;
}
static char *
php_grep_upstream_translate_regex_pattern(char const *pattern, size_t pattern_len, size_t *translated_len)
{
php_grep_upstream_rewrite rewrite = {0};
size_t i;
for (i = 0; i < pattern_len; i++) {
unsigned char ch = (unsigned char) pattern[i];
if (ch != '\\' || i + 1 >= pattern_len) {
if (!php_grep_upstream_rewrite_append_literal(&rewrite, (char) ch)) {
return NULL;
}
continue;
}
i++;
switch ((unsigned char) pattern[i]) {
case 'd':
if (!php_grep_upstream_rewrite_append_string(&rewrite, "[[:digit:]]")) {
return NULL;
}
break;
case 'D':
if (!php_grep_upstream_rewrite_append_string(&rewrite, "[^[:digit:]]")) {
return NULL;
}
break;
case 's':
if (!php_grep_upstream_rewrite_append_string(&rewrite, "[[:space:]]")) {
return NULL;
}
break;
case 'S':
if (!php_grep_upstream_rewrite_append_string(&rewrite, "[^[:space:]]")) {
return NULL;
}
break;
case 'w':
if (!php_grep_upstream_rewrite_append_string(&rewrite, "[[:alnum:]_]")) {
return NULL;
}
break;
case 'W':
if (!php_grep_upstream_rewrite_append_string(&rewrite, "[^[:alnum:]_]")) {
return NULL;
}
break;
case 'h':
if (!php_grep_upstream_rewrite_append_string(&rewrite, "[[:blank:]]")) {
return NULL;
}
break;
case 'H':
if (!php_grep_upstream_rewrite_append_string(&rewrite, "[^[:blank:]]")) {
return NULL;
}
break;
default:
if (!php_grep_upstream_rewrite_append_literal(&rewrite, '\\')
|| !php_grep_upstream_rewrite_append_literal(&rewrite, pattern[i])) {
return NULL;
}
break;
}
}
if (!php_grep_upstream_rewrite_reserve(&rewrite, 0)) {
return NULL;
}
rewrite.buffer[rewrite.len] = '\0';
*translated_len = rewrite.len;
return rewrite.buffer;
}
static bool
php_grep_upstream_match_regex_line(void *compiled_pattern, char const *subject, size_t subject_len)
{
struct dfa_comp *dc = compiled_pattern;
size_t line_len = subject_len;
idx_t i;
if (line_len > 0 && subject[line_len - 1] == '\n') {
line_len--;
}
for (i = 0; i < dc->pcount; i++) {
regoff_t start;
idx_t match_len;
dc->patterns[i].not_eol = 0;
dc->patterns[i].newline_anchor = eolbyte == '\n';
start = re_search(&dc->patterns[i], subject, line_len, 0, line_len, &dc->regs);
if (start < 0) {
continue;
}
match_len = dc->regs.end[0] - start;
if (start == 0 && match_len == line_len) {
return true;
}
}
return false;
}
bool
php_grep_upstream_match_line_regexp(int mode, void *compiled_pattern, char const *subject, size_t subject_len)
{
if (mode == PHP_GREP_UPSTREAM_MODE_FIXED) {
struct kwsearch *kwsearch = compiled_pattern;
char const *pattern = kwsearch->pattern;
size_t line_len = subject_len;
char const *cursor = pattern;
char const *limit = pattern + kwsearch->size;
if (line_len > 0 && subject[line_len - 1] == '\n') {
line_len--;
}
while (cursor <= limit) {
char const *sep = memchr(cursor, '\n', (size_t) (limit - cursor));
size_t pattern_len = sep != NULL ? (size_t) (sep - cursor) : (size_t) (limit - cursor);
if (pattern_len == line_len && memcmp(cursor, subject, line_len) == 0) {
return true;
}
if (sep == NULL) {
break;
}
cursor = sep + 1;
}
return false;
}
if (mode == PHP_GREP_UPSTREAM_MODE_BASIC || mode == PHP_GREP_UPSTREAM_MODE_EXTENDED) {
return php_grep_upstream_match_regex_line(compiled_pattern, subject, subject_len);
}
php_error_docref(NULL, E_ERROR, "unsupported GNU grep mode %d", mode);
return false;
}
static bool
php_grep_upstream_fixed_match_at(char const *subject, size_t line_len, size_t start, char const *needle, size_t needle_len)
{
if (needle_len == 0 || start + needle_len > line_len) {
return false;
}
if (match_icase) {
return zend_binary_strncasecmp(subject + start, needle_len, needle, needle_len, needle_len) == 0;
}
return memcmp(subject + start, needle, needle_len) == 0;
}
static bool
php_grep_upstream_foreach_fixed_match(void *compiled_pattern, char const *subject, size_t subject_len, php_grep_upstream_match_visitor visitor, void *context)
{
struct kwsearch *kwsearch = compiled_pattern;
char const *pattern = kwsearch->pattern;
char const *cursor = pattern;
char const *limit = pattern + kwsearch->size;
size_t line_len = subject_len;
size_t search_from = 0;
if (line_len > 0 && subject[line_len - 1] == '\n') {
line_len--;
}
while (search_from < line_len) {
size_t best_start = SIZE_MAX;
size_t best_len = 0;
cursor = pattern;
while (cursor <= limit) {
char const *sep = memchr(cursor, '\n', (size_t) (limit - cursor));
size_t needle_len = sep != NULL ? (size_t) (sep - cursor) : (size_t) (limit - cursor);
size_t pos;
if (needle_len == 0) {
if (sep == NULL) {
break;
}
cursor = sep + 1;
continue;
}
for (pos = search_from; pos + needle_len <= line_len; pos++) {
if (!php_grep_upstream_fixed_match_at(subject, line_len, pos, cursor, needle_len)) {
continue;
}
if (!php_grep_upstream_match_boundary_ok(subject, line_len, pos, needle_len)) {
continue;
}
if (best_start == SIZE_MAX || pos < best_start || (pos == best_start && needle_len > best_len)) {
best_start = pos;
best_len = needle_len;
}
break;
}
if (sep == NULL) {
break;
}
cursor = sep + 1;
}
if (best_start == SIZE_MAX || best_len == 0) {
return true;
}
if (!visitor(best_start, best_len, context)) {
return false;
}
search_from = best_start + best_len;
}
return true;
}
static bool
php_grep_upstream_foreach_regex_match(void *compiled_pattern, char const *subject, size_t subject_len, php_grep_upstream_match_visitor visitor, void *context)
{
struct dfa_comp *dc = compiled_pattern;
size_t line_len = subject_len;
size_t cursor = 0;
if (line_len > 0 && subject[line_len - 1] == '\n') {
line_len--;
}
while (cursor < line_len) {
regoff_t best_start = -1;
regoff_t best_end = -1;
idx_t i;
for (i = 0; i < dc->pcount; i++) {
regoff_t start;
regoff_t end;
dc->patterns[i].not_eol = 0;
dc->patterns[i].newline_anchor = eolbyte == '\n';
start = re_search(&dc->patterns[i], subject, line_len, cursor, line_len - cursor, &dc->regs);
if (start < 0) {
continue;
}
end = dc->regs.end[0];
if (end <= start) {
continue;
}
if (!php_grep_upstream_match_boundary_ok(subject, line_len, (size_t) start, (size_t) (end - start))) {
continue;
}
if (best_start < 0 || start < best_start || (start == best_start && end > best_end)) {
best_start = start;
best_end = end;
}
}
if (best_start < 0 || best_end <= best_start) {
return true;
}
if (!visitor((size_t) best_start, (size_t) (best_end - best_start), context)) {
return false;
}
cursor = (size_t) best_end;
}
return true;
}
void
php_grep_upstream_set_options(bool ignore_case, bool word_regexp, bool line_regexp)
{
match_icase = ignore_case;
match_words = word_regexp;
match_lines = line_regexp;
eolbyte = '\n';
}
void
php_grep_upstream_init(void)
{
if (php_grep_upstream_initialized) {
return;
}
init_localeinfo(&localeinfo);
wordinit();
php_grep_upstream_initialized = true;
}
static char *
php_grep_upstream_prepare_pattern(char const *pattern, size_t pattern_len)
{
char *buffer;
buffer = malloc(pattern_len + 1);
if (buffer == NULL) {
php_error_docref(NULL, E_ERROR, "failed to allocate GNU grep pattern buffer");
return NULL;
}
memcpy(buffer, pattern, pattern_len);
buffer[pattern_len] = '\n';
return buffer;
}
static char *
php_grep_upstream_prepare_regex_pattern(char const *pattern, size_t pattern_len, size_t *prepared_len)
{
char *translated;
char *buffer;
size_t translated_len;
translated = php_grep_upstream_translate_regex_pattern(pattern, pattern_len, &translated_len);
if (translated == NULL) {
return NULL;
}
buffer = malloc(translated_len + 1);
if (buffer == NULL) {
free(translated);
php_error_docref(NULL, E_ERROR, "failed to allocate GNU grep regex pattern buffer");
return NULL;
}
memcpy(buffer, translated, translated_len);
buffer[translated_len] = '\n';
*prepared_len = translated_len;
free(translated);
return buffer;
}
void *
php_grep_upstream_compile(int mode, char const *pattern, size_t pattern_len)
{
char *buffer;
size_t prepared_len = pattern_len;
php_grep_upstream_init();
switch (mode) {
case PHP_GREP_UPSTREAM_MODE_FIXED:
buffer = php_grep_upstream_prepare_pattern(pattern, pattern_len);
if (buffer == NULL) {
return NULL;
}
return Fcompile(buffer, (idx_t) pattern_len, 0, false);
case PHP_GREP_UPSTREAM_MODE_BASIC:
buffer = php_grep_upstream_prepare_regex_pattern(pattern, pattern_len, &prepared_len);
if (buffer == NULL) {
return NULL;
}
return GEAcompile(buffer, (idx_t) prepared_len, RE_SYNTAX_GREP, true);
case PHP_GREP_UPSTREAM_MODE_EXTENDED:
buffer = php_grep_upstream_prepare_regex_pattern(pattern, pattern_len, &prepared_len);
if (buffer == NULL) {
return NULL;
}
return GEAcompile(buffer, (idx_t) prepared_len, RE_SYNTAX_EGREP, true);
default:
php_error_docref(NULL, E_ERROR, "unsupported GNU grep mode %d", mode);
return NULL;
}
}
bool
php_grep_upstream_match(int mode, void *compiled_pattern, char const *subject, size_t subject_len)
{
idx_t match_size = 0;
ptrdiff_t offset;
char *line_buffer = NULL;
char const *match_subject = subject;
size_t match_len = subject_len;
if ((mode == PHP_GREP_UPSTREAM_MODE_BASIC || mode == PHP_GREP_UPSTREAM_MODE_EXTENDED)
&& (subject_len == 0 || subject[subject_len - 1] != '\n')) {
line_buffer = malloc(subject_len + 1);
if (line_buffer == NULL) {
php_error_docref(NULL, E_ERROR, "failed to allocate GNU grep subject buffer");
return false;
}
memcpy(line_buffer, subject, subject_len);
line_buffer[subject_len] = '\n';
match_subject = line_buffer;
match_len = subject_len + 1;
}
switch (mode) {
case PHP_GREP_UPSTREAM_MODE_FIXED:
offset = Fexecute(compiled_pattern, match_subject, (idx_t) match_len, &match_size, NULL);
break;
case PHP_GREP_UPSTREAM_MODE_BASIC:
case PHP_GREP_UPSTREAM_MODE_EXTENDED:
offset = EGexecute(compiled_pattern, match_subject, (idx_t) match_len, &match_size, NULL);
break;
default:
free(line_buffer);
php_error_docref(NULL, E_ERROR, "unsupported GNU grep mode %d", mode);
return false;
}
free(line_buffer);
return offset >= 0;
}
bool
php_grep_upstream_foreach_match(int mode, void *compiled_pattern, char const *subject, size_t subject_len, php_grep_upstream_match_visitor visitor, void *context)
{
switch (mode) {
case PHP_GREP_UPSTREAM_MODE_FIXED:
return php_grep_upstream_foreach_fixed_match(compiled_pattern, subject, subject_len, visitor, context);
case PHP_GREP_UPSTREAM_MODE_BASIC:
case PHP_GREP_UPSTREAM_MODE_EXTENDED:
return php_grep_upstream_foreach_regex_match(compiled_pattern, subject, subject_len, visitor, context);
default:
php_error_docref(NULL, E_ERROR, "unsupported GNU grep mode %d", mode);
return false;
}
}
static void
php_grep_upstream_free_fixed(void *compiled_pattern)
{
struct kwsearch *kwsearch = compiled_pattern;
if (kwsearch == NULL) {
return;
}
kwsfree(kwsearch->kwset);
free(kwsearch->pattern);
free(kwsearch);
}
static void
php_grep_upstream_free_regex(void *compiled_pattern)
{
struct dfa_comp *dc = compiled_pattern;
if (dc == NULL) {
return;
}
/*
* GNU grep's regex state ownership is trickier than the fixed-string path.
* Keep teardown disabled for now so the extension remains stable while
* we continue wiring matcher slices and add coverage around lifetime rules.
*/
(void) dc;
}
void
php_grep_upstream_free(int mode, void *compiled_pattern)
{
switch (mode) {
case PHP_GREP_UPSTREAM_MODE_FIXED:
php_grep_upstream_free_fixed(compiled_pattern);
return;
case PHP_GREP_UPSTREAM_MODE_BASIC:
case PHP_GREP_UPSTREAM_MODE_EXTENDED:
php_grep_upstream_free_regex(compiled_pattern);
return;
default:
php_error_docref(NULL, E_ERROR, "unsupported GNU grep mode %d", mode);
return;
}
}
void
fgrep_to_grep_pattern(char **keys_p, idx_t *len_p)
{
(void) keys_p;
(void) len_p;
php_error_docref(NULL, E_ERROR, "GNU grep fixed-string regex fallback is not implemented yet");
}
char const *
pattern_file_name(idx_t lineno, idx_t *new_lineno)
{
if (new_lineno != NULL) {
*new_lineno = lineno + 1;
}
return "";
}
char const *
input_filename(void)
{
return "<php-grep>";
}