-
Notifications
You must be signed in to change notification settings - Fork 0
/
nomos_regex.c
708 lines (643 loc) · 18.3 KB
/
nomos_regex.c
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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
/***************************************************************
Copyright (C) 2006-2011 Hewlett-Packard Development Company, L.P.
Copyright (C) 2014, Siemens AG
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 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 General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
***************************************************************/
//#define DEBUG_TNG
#ifndef DEBUG_TNG
#define CALL_IF_DEBUG_MODE(x)
#else
#define CALL_IF_DEBUG_MODE(x) x
#endif
#include "nomos_regex.h"
#include "nomos_gap.h"
#include "nomos_utils.h"
regex_t regc[NFOOTPRINTS];
/**
* \file
* \brief search using regex functions
*
* Functions for dealing with the regex clib functions. Performs the
* regex searchs on the data.
*/
/** Buffer to hold regex error */
static char regexErrbuf[myBUFSIZ];
regex_t idx_regc[NFOOTPRINTS];
/**
* \brief Log an error caused by regex
*
* Calls regerror()
* \param ret Error code
* \param regc Compiled regex
* \param regex Regex string
*/
void regexError(int ret, regex_t *regc, char *regex)
{
#ifdef PROC_TRACE
traceFunc("== regexError(%d, %p, %s)\n", ret, regc, regex);
#endif /* PROC_TRACE */
(void) regerror(ret, regc, regexErrbuf, sizeof(regexErrbuf));
Msg("regex = \"%s\"\n", regex);
LOG_FATAL("regcomp failure: %s", regexErrbuf)
Bail(-__LINE__);
}
/**
* \brief Check if a string ends with given suffix
* \param s String to check
* \param suffix Suffix to find
* \return 1 if suffix found, 0 otherwise
*/
int endsIn(char *s, char *suffix)
{
int slen = (int) strlen(s);
int sufflen = (int) strlen(suffix);
/*
* compare trailing chars in a string with a constant (should be faster
* than calling regcomp() and regexec()!)
*/
#ifdef PROC_TRACE
traceFunc("== endsIn(%s, %s)\n", s, suffix);
#endif /* PROC_TRACE */
if (strncasecmp(s + slen - sufflen, suffix, (size_t) sufflen) == 0)
{
return (1);
}
return (0);
}
/**
* \brief Check if a line exists in a file
* \param pathname File location
* \param regex Regex to check
* \return True if regex satisfied, false otherwise
*/
int lineInFile(char *pathname, char *regex)
{
char buf[myBUFSIZ];
#ifdef PROC_TRACE
traceFunc("== lineInFile(%s, \"%s\")\n", pathname, regex);
#endif /* PROC_TRACE */
(void) sprintf(buf, "^%s$", regex);
return (textInFile(pathname, buf, REG_NEWLINE));
}
/**
* \brief Check if a regex passes in a file
* \param pathname File location
* \param regex Regex to check
* \param flags Additional regex flags
* \return True if regex satisfied, false otherwise
*/
int textInFile(char *pathname, char *regex, int flags)
{
char *textp;
int ret;
#ifdef PROC_TRACE
traceFunc("== textInFile(%s, \"%s\", 0x%x)\n", pathname, regex, flags);
#endif /* PROC_TRACE */
if ((pathname == NULL_STR ) || (regex == NULL_STR ))
{
#ifdef QA_CHECKS
if (pathname == NULL_STR)
{
Assert(NO, "textInFile: NULL pathname");
}
if (regex == NULL_STR)
{
Assert(NO, "textInFile: NULL regex");
}
#endif /* QA_CHECKS */
return (0);
}
if ((textp = mmapFile(pathname)) == NULL_STR)
{
return (0);
}
ret = strGrep(regex, textp, flags);
munmapFile(textp);
return (ret);
}
/**
* \brief General-purpose grep function, used for one-time-only searches.
*
* @return -1 on regex-compile failure, 1 if regex search fails, and 0 if
* regex search is successful.
*/
int strGrep(char *regex, char *data, int flags)
{
regex_t regc;
int ret;
#ifdef PHRASE_DEBUG
int i;
#endif /* PHRASE_DEBUG */
#if defined(PROC_TRACE) || defined(PHRASE_DEBUG)
traceFunc("== strGrep(\"%s\", %p, 0x%x)\n", regex, data, flags);
#endif /* PROC_TRACE || PHRASE_DEBUG */
if (data == NULL_STR || regex == NULL_STR)
{
return (0);
}
/* DO NOT, repeat DO NOT add REG_EXTENDED as a default flag! */
if ((ret = regcomp(®c, regex, flags)) != 0)
{
regexError(ret, ®c, regex);
regfree(®c);
return (-1); /* <0 indicates compile failure */
}
/*
* regexec() returns 1 on failure and 0 on success - make sure we call
* regfree after the regexec call, else after a million or so regex
* searches we'll have lost a LOT of memory. :)
*/
ret = regexec(®c, data, 1, &cur.regm, 0);
regfree(®c);
if (ret)
{
return (0); /* >0 indicates search failure */
}
#ifdef QA_CHECKS
if (cur.regm.rm_so == cur.regm.rm_eo)
{
Assert(NO, "start/end offsets are identical in strGrep()");
}
#endif /* QA_CHECKS */
#ifdef PHRASE_DEBUG
printf("strGrep MATCH(%s) @ %d! = {", regex, cur.regm.rm_so);
for (i = cur.regm.rm_so; i < cur.regm.rm_eo; i++)
{
printf("%c", data[i]);
}
printf("}\n");
#endif /* PHRASE_DEBUG */
if (gl.flags & FL_SAVEBASE)
{
cur.matchBase = data;
}
return (1);
}
/**
* \brief compile a regex, and perform the search (on data?)
*
* @param index number of licence/regex we are looking for (given in STRINGS.in)
* @param data the data to search
* @param flags regcomp cflags
*
* @return -1 on regex-compile failure, 1 if regex search fails, and 0 if
* regex search is successful.
*/
int idxGrep(int index, char *data, int flags)
{
return idxGrep_base(index, data, flags, 0);
}
/**
* \brief compile a regex, perform the search and record findings
*
* If OPTS_NO_HIGHLIGHTINFO is set, do not record. If not set, record the findings
* \param index number of licence/regex we are looking for (given in STRINGS.in)
* \param data the data to search
* \param flags regcomp cflags
* \return -1 on regex-compile failure, 1 if regex search fails, and 0 if
* regex search is successful.
*/
int idxGrep_recordPosition(int index, char *data, int flags)
{
if( optionIsSet(OPTS_NO_HIGHLIGHTINFO) ) {
return idxGrep_base(index, data, flags, 0);
}
else {
return idxGrep_base(index, data, flags, 1);
}
}
/**
* \brief compile a regex, perform the search and record findings
*
* If OPTS_NO_HIGHLIGHTINFO is set, do not record. If not set, record the
* doctored findings
* \param index number of licence/regex we are looking for (given in STRINGS.in)
* \param data the data to search
* \param flags regcomp cflags
* \return -1 on regex-compile failure, 1 if regex search fails, and 0 if
* regex search is successful.
*/
int idxGrep_recordPositionDoctored(int index, char *data, int flags)
{
if( optionIsSet(OPTS_NO_HIGHLIGHTINFO) ) {
return idxGrep_base(index, data, flags, 0);
}
else {
return idxGrep_base(index, data, flags, 2);
}
}
/**
* \brief compile a regex, perform the search and record index
*
* If OPTS_NO_HIGHLIGHTINFO is set, do not record. If not set, record the
* finding index
* \param index number of licence/regex we are looking for (given in STRINGS.in)
* \param data the data to search
* \param flags regcomp cflags
* \return -1 on regex-compile failure, 1 if regex search fails, and 0 if
* regex search is successful.
*/
int idxGrep_recordIndex(int index, char *data, int flags)
{
if( optionIsSet(OPTS_NO_HIGHLIGHTINFO) ) {
return idxGrep_base(index, data, flags, 0);
}
else {
return idxGrep_base(index, data, flags, 3);
}
}
/**
* \brief Perform a regex match on a given data and return only first match
* \param isPlain Do a plain match?
* \param data The string to perform search on
* \param regex The regex string
* \param rp Regex buffer
* \param[out] regmatch Regex matches
* \return
*/
int matchOnce(int isPlain, char *data, char* regex, regex_t *rp,
regmatch_t* regmatch)
{
if(isPlain) {
return !strNbuf_noGlobals(data, regex, regmatch , 0 , cur.matchBase );
}
return regexec(rp, data, 1, regmatch, 0);
}
/**
* \brief Store a single regex match to array
* \param[in] currentRegMatch Match to store
* \param[in] lastmatch Index of last match
* \param[in,out] allmatches Array of all matches
* \param[in,out] tmpData
* \param[in] data
* \return New lastmatch index
*/
int storeOneMatch(regmatch_t currentRegMatch, int lastmatch, GArray* allmatches,
char** tmpData, char* data)
{
regmatch_t storeRegMatch;
storeRegMatch.rm_so = currentRegMatch.rm_so + lastmatch;
storeRegMatch.rm_eo = currentRegMatch.rm_eo + lastmatch;
g_array_append_val(allmatches, storeRegMatch);
lastmatch += currentRegMatch.rm_eo;
*tmpData = data + lastmatch;
return lastmatch;
}
/**
* \brief compile a regex, and perform the search (on data?)
*
* @param index number of licence/regex we are looking for (given in STRINGS.in)
* @param data the data to search
* @param flags regcomp cflags
* @param mode Flag to control recording of findings (0:No, 1: Yes, 2:Yes doctored buffer)
*
* @return -1 on regex-compile failure, 1 if regex search fails, and 0 if
* regex search is successful.
*/
int idxGrep_base(int index, char *data, int flags, int mode)
{
int i;
int ret;
int show = flags & FL_SHOWMATCH;
licText_t *ltp = licText + index;
/**
* \todo is idx_regc needed? Here we set the pointer to our array and later
* we fill it, but we never reuse the regex_t
*/
regex_t *rp = idx_regc + index;
CALL_IF_DEBUG_MODE(printf(" %i %i \"", index, ltp->plain);)
#if defined(PROC_TRACE) || defined(PHRASE_DEBUG)
traceFunc("== idxGrep(%d, %p, 0x%x)\n... regex \"%s\"\n", index, data,
flags, _REGEX(index));
#endif /* PROC_TRACE || PHRASE_DEBUG */
if (index > NFOOTPRINTS)
{
LOG_FATAL("idxGrep: index %d out of range", index)
Bail(-__LINE__);
}
if (data == NULL_STR)
{
#ifdef PHRASE_DEBUG
printf("idxGrep: NULL pointer to file data!\n");
#endif /* PHRASE_DEBUG */
return (0);
}
if (ltp->plain )
{
ret = strNbuf(data, ltp->regex);
if(ret == 0) return (ret);
}
else {
if ((ret = regcomp(rp, ltp->regex, flags)))
{
fprintf(stderr, "Compile failed, regex #%d\n", index);
regexError(ret, rp, ltp->regex);
regfree(rp);
printf("Compile error \n");
return (-1); /* <0 indicates compile failure */
}
if (regexec(rp, data, 1, &cur.regm, 0))
{
regfree(rp);
return (0);
}
else ret =1;
#ifdef QA_CHECKS
if (cur.regm.rm_so == cur.regm.rm_eo)
{
regfree(rp);
Assert(NO, "start/end offsets are identical in idxGrep(%d)",
index);
}
#endif /* QA_CHECKS */
/* Set up a global match-length variable? */
if (show)
{
#ifdef DEBUG
printf("REGEX(%d) \"%s\"\n", index, ltp->regex);
#endif /* DEBUG */
printf("MATCH @ %d! = {", cur.regm.rm_so);
for (i = cur.regm.rm_so; i < cur.regm.rm_eo; i++)
{
printf("%c", data[i]);
}
printf("}\n");
}
if (gl.flags & FL_SAVEBASE)
{
cur.matchBase = data;
}
}
//! Now we have a match
if (mode == 3 ) {
recordIndex(cur.indexList, index);
}
else if (mode==1 || mode == 2)
{
CALL_IF_DEBUG_MODE(printf("MATCH!\n");)
//! All sanity checks have passed and we have at least one match
CALL_IF_DEBUG_MODE(printf("%s", data);)
GArray* allmatches = g_array_new(FALSE, FALSE, sizeof(regmatch_t));
regmatch_t currentRegMatch;
int lastmatch = 0;
char* tmpData = data;
lastmatch = storeOneMatch(cur.regm, lastmatch, allmatches, &tmpData, data);
while (!matchOnce(ltp->plain,tmpData, ltp->regex, rp, ¤tRegMatch ) )
{
lastmatch = storeOneMatch(currentRegMatch, lastmatch, allmatches, &tmpData, data);
}
if(index >= _KW_first && index <= _KW_last ) {
rememberWhatWeFound(cur.keywordPositions, allmatches, index, mode);
}
else if (cur.currentLicenceIndex > -1 ) {
rememberWhatWeFound( getLicenceAndMatchPositions(cur.theMatches, cur.currentLicenceIndex )->matchPositions , allmatches, index, mode);
}
g_array_free(allmatches, 1);
CALL_IF_DEBUG_MODE(printf("Bye!\n");)
}
if (!ltp->plain ) regfree(rp);
return (1);
}
/**
* \brief Add a given index to index list
* \param[in,out] indexList List to add index to
* \param[in] index Index to be appended
*/
void recordIndex(GArray* indexList, int index){
g_array_append_val(indexList, index);
}
/**
* \brief Get offset from doctored buffer
* \param posInDoctoredBuffer
* \return new offset
* \sa uncollapsePosition()
*/
static int getOffset(int posInDoctoredBuffer)
{
return uncollapsePosition(posInDoctoredBuffer, cur.docBufferPositionsAndOffsets);
}
/**
* \brief From a given array, get regex match from a given index
* \param in Array to get regex match from
* \param index The index in array required
* \return Regex match @index
*/
regmatch_t* getRegmatch_t(GArray* in, int index)
{
return & g_array_index(in, regmatch_t, index);
}
/**
* \brief Store regex matches in highlight array
* \param[in,out] highlight The array holding the regex matches
* \param[in] regmatch_tArray Array of regex matches to store
* \param[in] index Index of license (from STRINGS.in)
* \param[in] mode Mode to store (1=>get the byte position|2=>get the offset)
*/
void rememberWhatWeFound(GArray* highlight, GArray* regmatch_tArray, int index,
int mode)
{
if (mode != 1 && mode != 2)
{
FOSSY_EXIT("This mode is not supported\n", 8);
return;
}
int i = 0;
int nmatches = regmatch_tArray->len;
int alreadyFound = highlight->len;
g_array_set_size(highlight, alreadyFound + nmatches);
for (i = 0; i < nmatches; ++i)
{
regmatch_t* theRegmatch = getRegmatch_t(regmatch_tArray, i);
if (theRegmatch->rm_eo == -1 || theRegmatch->rm_so == -1)
{
FOSSY_EXIT("Found match at negative position... this should not happen\n", 9);
return;
}
MatchPositionAndType* ourMatchv = getMatchfromHighlightInfo(highlight, i + alreadyFound);
ourMatchv->start = (mode == 1) ? theRegmatch->rm_so : getOffset(theRegmatch->rm_so);
ourMatchv->end = (mode == 1) ? theRegmatch->rm_eo : getOffset(theRegmatch->rm_eo);
ourMatchv->index = index;
CALL_IF_DEBUG_MODE(printf("here: %i - %i \n", ourMatchv->start, ourMatchv->end);)
}
CALL_IF_DEBUG_MODE(printf(" We go and now we know %d ", highlight->len);)
}
#define _XC(q) ((char) xascii[q])
/**
* \brief Check if a string exists in buffer (case insensitive)
* \param data Haystack
* \param str Needle
* \return 1 on success, 0 otherwise
* \sa strNbuf_noGlobals()
*/
int strNbuf(char *data, char *str){
return strNbuf_noGlobals(data, str, &(cur.regm), gl.flags & FL_SAVEBASE , cur.matchBase );
}
/**
* \brief This is our own internal, case-insensitive version of strstr().
*
* No open-source code was consulted/used in the construction of this function.
*/
int strNbuf_noGlobals(char *data, char *str, regmatch_t* matchPos, int doSave,
char* saveData)
{
static int firstFlag = 1;
static char xascii[128];
int i;
int alph = 0;
int save = 0;
char *bufp;
char *pattp;
char *mark;
char x;
char firstx = 0;
#if defined(PROC_TRACE) || defined(PHRASE_DEBUG)
traceFunc("== strNbuf(%p, %p)\n", data, str);
#endif /* PROC_TRACE || PHRASE_DEBUG */
if (firstFlag)
{
firstFlag = 0;
/*
* 32 characters separate 'A' (65) and 'a' (97), contiguous up to 'Z'.
* Therefore, 'Z' == 90, 'a' == 97, and 'z' == 122
*/
for (i = 0; i < sizeof(xascii); i++)
{
if ((i >= 65) && (i <= 90))
{ /* isupper */
xascii[i] = i + 32; /* -> tolower */
}
else if ((i >= 97) && (i <= 122))
{ /* islower */
xascii[i] = i - 32; /* -> toupper */
}
else
{
/* *foo = tolower((char)i); */
xascii[i] = (char) /*i*/0;
}
}
#ifdef STRSTR_DEBUG
/*
* Dump the table (debugging purposes only)
*/
for (i = 0; i < sizeof (xascii); i++)
{
if (xascii[i])
{
printf(" %c%c ", (unsigned) i, xascii[i]);
}
else
{
printf("\\%03d ", (int) xascii[i]);
}
if (i & 16 == 15)
{
printf("\n");
}
}
#endif /* STRSTR_DEBUG */
}
#ifdef STRSTR_DEBUG
printf("DATA \"%s\"\nPATT \"%s\"\n", data, str);
#endif /* STRSTR_DEBUG */
if (data == NULL_STR || str == NULL_STR)
{
return (0);
}
alph = isalpha(*str);
if (alph)
{
firstx = xascii[(int) *str];
#ifdef STRSTR_DEBUG
printf("NOTE: first char (%c) is Alphabetic - alternate is (%c)\n",
*str, firstx);
#endif /* STRSTR_DEBUG */
#ifdef QA_CHECKS
if (firstx == NULL_CHAR)
{
LOG_FATAL("Unexpected initialization")
Bail(-__LINE__);
}
#endif /* QA_CHECKS */
}
for (bufp = data; /* *pattp && */*bufp; bufp = mark)
{
#ifdef STRSTR_DEBUG
printf("\nDEBUG: start, buffer = \"%s\"\n", bufp);
#endif /* STRSTR_DEBUG */
pattp = str;
/*
* Locate the first character of our target-pattern in the buffer...
*/
while (*bufp)
{
#ifdef STRSTR_DEBUG
printf("... findfirst, *bufp is '%c' == [%c%c]?\n",
*bufp, *str, alph ? firstx : *str);
#endif /* STRSTR_DEBUG */
if (*bufp == *pattp)
{
break;
}
if (alph && (*bufp == firstx))
{
break;
}
bufp++;
}
if (*bufp == NULL_CHAR)
{
return (0);
}
save = bufp - data;
mark = ++bufp; /* could optimize this in loop below */
#ifdef STRSTR_DEBUG
printf("GOT IT, at offset %d (*mark now is '%c')\n",
bufp - data - 1, *mark);
#endif /* STRSTR_DEBUG */
/* optimizeMark = 1; */
for (++pattp; *bufp && *pattp; bufp++, pattp++)
{
#ifdef STRSTR_DEBUG
printf("STRING-COMPARE: %c == %c ??\n", *bufp, *pattp);
#endif /* STRSTR_DEBUG */
if (*bufp == *pattp)
{
continue;
}
#ifdef STRSTR_DEBUG
printf("... or perhaps: %c == %c ??\n", *bufp,
xascii[*pattp]);
#endif /* STRSTR_DEBUG */
if (((x = xascii[(int) *pattp])) && (*bufp == x))
{
continue;
}
break;
}
if (*pattp == NULL_CHAR)
{
matchPos->rm_so = save;
matchPos->rm_eo = save + strlen(str);
if (doSave)
{
saveData = data;
}
return (1); /* end of pattern == success */
}
if (*bufp == NULL_CHAR)
{
return (0); /* end of buffer == success */
}
}
return (0);
}