2023-08-24 06:24:46 -04:00
|
|
|
/* regex.c
|
|
|
|
* Copyright 2023 Anon Anonson, Ognjen 'xolatile' Milan Robovic, Emil Williams
|
|
|
|
* SPDX Identifier: GPL-3.0-only / NO WARRANTY / NO GUARANTEE */
|
|
|
|
|
2023-08-23 21:16:55 -04:00
|
|
|
#include "regex.h"
|
|
|
|
|
2023-08-23 19:59:59 -04:00
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
2023-08-28 09:42:42 -04:00
|
|
|
#include <limits.h>
|
2023-08-19 18:49:10 -04:00
|
|
|
|
2023-08-24 09:19:35 -04:00
|
|
|
// ------------------
|
2023-08-24 09:00:44 -04:00
|
|
|
// ### Char tests ###
|
2023-08-24 09:19:35 -04:00
|
|
|
// ------------------
|
2023-08-23 22:11:46 -04:00
|
|
|
static bool is_quantifier(const char c) {
|
2023-08-24 13:17:14 -04:00
|
|
|
for (const char * s = "+*?="; *s != '\00'; s++) {
|
2023-08-23 19:59:59 -04:00
|
|
|
if (*s == c) {
|
2023-08-19 18:49:10 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-08-23 22:11:46 -04:00
|
|
|
bool is_magic(const char c) {
|
|
|
|
if (is_quantifier(c)) {
|
|
|
|
return true;
|
|
|
|
}
|
2023-08-28 09:43:32 -04:00
|
|
|
for (const char * s = "\\[]."; *s != '\00'; s++) {
|
2023-08-23 22:11:46 -04:00
|
|
|
if (*s == c) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-08-23 19:59:59 -04:00
|
|
|
|
2023-08-24 09:00:44 -04:00
|
|
|
|
2023-08-24 09:19:35 -04:00
|
|
|
// ----------------------
|
2023-08-24 09:00:44 -04:00
|
|
|
// ### Internal Types ###
|
2023-08-24 09:19:35 -04:00
|
|
|
// ----------------------
|
2023-08-24 09:00:44 -04:00
|
|
|
typedef struct {
|
|
|
|
int in;
|
|
|
|
char input;
|
|
|
|
int to;
|
2023-08-24 13:17:14 -04:00
|
|
|
int width;
|
2023-08-24 09:00:44 -04:00
|
|
|
} delta_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int in;
|
|
|
|
int to;
|
|
|
|
} offshoot_t;
|
|
|
|
|
2023-08-24 15:08:31 -04:00
|
|
|
typedef struct {
|
|
|
|
bool * do_catch;
|
|
|
|
bool * is_negative;
|
|
|
|
int * state;
|
|
|
|
int * width;
|
|
|
|
char * whitelist;
|
2023-08-28 09:42:42 -04:00
|
|
|
char * blacklist;
|
2023-08-24 15:08:31 -04:00
|
|
|
regex_t * regex;
|
|
|
|
} compiler_state;
|
|
|
|
|
2023-08-24 09:00:44 -04:00
|
|
|
|
|
|
|
|
2023-08-24 09:19:35 -04:00
|
|
|
// ----------------------------------
|
2023-08-24 09:00:44 -04:00
|
|
|
// ### Regex creation/destruction ###
|
2023-08-24 09:19:35 -04:00
|
|
|
// ----------------------------------
|
2023-08-28 09:42:42 -04:00
|
|
|
static int escape_1_to_1(const char c, compiler_state * cs) {
|
|
|
|
char * target_list = (*cs->is_negative) ? cs->blacklist : cs->whitelist;
|
2023-08-24 11:21:21 -04:00
|
|
|
switch (c) {
|
2023-08-23 19:59:59 -04:00
|
|
|
case 't': {
|
2023-08-28 09:42:42 -04:00
|
|
|
strcat(target_list, "\t");
|
2023-08-23 19:59:59 -04:00
|
|
|
} return 1;
|
|
|
|
case 'n': {
|
2023-08-28 09:42:42 -04:00
|
|
|
strcat(target_list, "\n");
|
2023-08-23 19:59:59 -04:00
|
|
|
} return 1;
|
|
|
|
case 'r': {
|
2023-08-28 09:42:42 -04:00
|
|
|
strcat(target_list, "\r");
|
2023-08-23 19:59:59 -04:00
|
|
|
} return 1;
|
|
|
|
case 'b': {
|
2023-08-28 09:42:42 -04:00
|
|
|
strcat(target_list, "\b");
|
2023-08-23 19:59:59 -04:00
|
|
|
} return 1;
|
|
|
|
case '[': {
|
2023-08-28 09:42:42 -04:00
|
|
|
strcat(target_list, "[");
|
2023-08-23 19:59:59 -04:00
|
|
|
} return 1;
|
|
|
|
case ']': {
|
2023-08-28 09:42:42 -04:00
|
|
|
strcat(target_list, "]");
|
2023-08-23 19:59:59 -04:00
|
|
|
} return 1;
|
|
|
|
case '.': {
|
2023-08-28 09:42:42 -04:00
|
|
|
strcat(target_list, ".");
|
2023-08-23 19:59:59 -04:00
|
|
|
} return 1;
|
2023-08-24 13:17:14 -04:00
|
|
|
case '=': {
|
2023-08-28 09:42:42 -04:00
|
|
|
strcat(target_list, "=");
|
2023-08-24 13:17:14 -04:00
|
|
|
} return 1;
|
2023-08-23 19:59:59 -04:00
|
|
|
case '?': {
|
2023-08-28 09:42:42 -04:00
|
|
|
strcat(target_list, "?");
|
2023-08-23 19:59:59 -04:00
|
|
|
} return 1;
|
|
|
|
case '+': {
|
2023-08-28 09:42:42 -04:00
|
|
|
strcat(target_list, "+");
|
2023-08-23 19:59:59 -04:00
|
|
|
} return 1;
|
|
|
|
case '*': {
|
2023-08-28 09:42:42 -04:00
|
|
|
strcat(target_list, "*");
|
2023-08-23 19:59:59 -04:00
|
|
|
} return 1;
|
|
|
|
case '\\': {
|
2023-08-28 09:42:42 -04:00
|
|
|
strcat(target_list, "\\");
|
2023-08-23 19:59:59 -04:00
|
|
|
} return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2023-08-19 18:49:10 -04:00
|
|
|
}
|
|
|
|
|
2023-08-28 09:42:42 -04:00
|
|
|
static int escape_1_to_N(const char c, compiler_state * cs) {
|
|
|
|
char * target_list = (*cs->is_negative) ? cs->blacklist : cs->whitelist;
|
2023-08-23 19:59:59 -04:00
|
|
|
switch(c) {
|
|
|
|
case 'i': {
|
2023-08-24 11:21:21 -04:00
|
|
|
const char identifier_chars[] = "@0123456789_"
|
|
|
|
"\300\301\302\303\304"
|
2023-08-25 12:51:50 -04:00
|
|
|
"\305\306\307\310\311"
|
|
|
|
"\312\313\314\315\316"
|
|
|
|
"\317\320\321\322\323"
|
|
|
|
"\324\325\326\327\330"
|
|
|
|
"\331\332\333\334\335"
|
|
|
|
"\336\337";
|
2023-08-28 09:42:42 -04:00
|
|
|
strcpy(target_list, identifier_chars);
|
2023-08-23 19:59:59 -04:00
|
|
|
return sizeof(identifier_chars)-1;
|
|
|
|
};
|
|
|
|
case 'I': {
|
2023-08-24 11:21:21 -04:00
|
|
|
const char identifier_chars[] = "@_"
|
|
|
|
"\300\301\302\303\304"
|
2023-08-25 12:51:50 -04:00
|
|
|
"\305\306\307\310\311"
|
|
|
|
"\312\313\314\315\316"
|
|
|
|
"\317\320\321\322\323"
|
|
|
|
"\324\325\326\327\330"
|
|
|
|
"\331\332\333\334\335"
|
|
|
|
"\336\337";
|
2023-08-28 09:42:42 -04:00
|
|
|
strcpy(target_list, identifier_chars);
|
2023-08-23 19:59:59 -04:00
|
|
|
return sizeof(identifier_chars)-1;
|
2023-08-19 18:49:10 -04:00
|
|
|
};
|
2023-08-23 19:59:59 -04:00
|
|
|
case 'k': {
|
2023-08-24 11:21:21 -04:00
|
|
|
const char keyword_chars[] = "@0123456789_"
|
|
|
|
"\300\301\302\303\304"
|
2023-08-25 12:51:50 -04:00
|
|
|
"\305\306\307\310\311"
|
|
|
|
"\312\313\314\315\316"
|
|
|
|
"\317\320\321\322\323"
|
|
|
|
"\324\325\326\327\330"
|
|
|
|
"\331\332\333\334\335"
|
|
|
|
"\336\337";
|
2023-08-28 09:42:42 -04:00
|
|
|
strcpy(target_list, keyword_chars);
|
2023-08-23 19:59:59 -04:00
|
|
|
return sizeof(keyword_chars)-1;
|
2023-08-19 18:49:10 -04:00
|
|
|
};
|
2023-08-23 19:59:59 -04:00
|
|
|
case 'K': {
|
2023-08-24 11:21:21 -04:00
|
|
|
const char keyword_chars[] = "@_"
|
2023-08-25 12:51:50 -04:00
|
|
|
"\300\301\302\303\304"
|
|
|
|
"\305\306\307\310\311"
|
|
|
|
"\312\313\314\315\316"
|
|
|
|
"\317\320\321\322\323"
|
|
|
|
"\324\325\326\327\330"
|
|
|
|
"\331\332\333\334\335"
|
|
|
|
"\336\337";
|
2023-08-28 09:42:42 -04:00
|
|
|
strcpy(target_list, keyword_chars);
|
2023-08-23 19:59:59 -04:00
|
|
|
return sizeof(keyword_chars)-1;
|
2023-08-19 18:49:10 -04:00
|
|
|
};
|
2023-08-23 19:59:59 -04:00
|
|
|
case 'f': {
|
|
|
|
const char filename_chars[] = "@0123456789/.-_+,#$%~=";
|
2023-08-28 09:42:42 -04:00
|
|
|
strcpy(target_list, filename_chars);
|
2023-08-23 21:16:55 -04:00
|
|
|
return sizeof(filename_chars)-1;
|
2023-08-19 18:49:10 -04:00
|
|
|
};
|
2023-08-23 19:59:59 -04:00
|
|
|
case 'F': {
|
|
|
|
const char filename_chars[] = "@/.-_+,#$%~=";
|
2023-08-28 09:42:42 -04:00
|
|
|
strcpy(target_list, filename_chars);
|
2023-08-23 21:16:55 -04:00
|
|
|
return sizeof(filename_chars)-1;
|
2023-08-19 18:49:10 -04:00
|
|
|
};
|
2023-08-23 19:59:59 -04:00
|
|
|
case 'p': {
|
2023-08-24 11:21:21 -04:00
|
|
|
const char printable_chars[] = "@"
|
2023-08-25 12:51:50 -04:00
|
|
|
"\241\242\243\244\245"
|
|
|
|
"\246\247\250\251\252"
|
|
|
|
"\253\254\255\256\257"
|
|
|
|
"\260\261\262\263\264"
|
|
|
|
"\265\266\267\270\271"
|
|
|
|
"\272\273\274\275\276"
|
|
|
|
"\277"
|
|
|
|
"\300\301\302\303\304"
|
|
|
|
"\305\306\307\310\311"
|
|
|
|
"\312\313\314\315\316"
|
|
|
|
"\317\320\321\322\323"
|
|
|
|
"\324\325\326\327\330"
|
|
|
|
"\331\332\333\334\335"
|
|
|
|
"\336\337";
|
2023-08-28 09:42:42 -04:00
|
|
|
strcpy(target_list, printable_chars);
|
2023-08-23 19:59:59 -04:00
|
|
|
return sizeof(printable_chars)-1;
|
2023-08-19 18:49:10 -04:00
|
|
|
};
|
2023-08-23 19:59:59 -04:00
|
|
|
case 'P': {
|
2023-08-24 11:21:21 -04:00
|
|
|
const char printable_chars[] = "@"
|
2023-08-25 12:51:50 -04:00
|
|
|
"\241\242\243\244\245"
|
|
|
|
"\246\247\250\251\252"
|
|
|
|
"\253\254\255\256\257"
|
|
|
|
"\260\261\262\263\264"
|
|
|
|
"\265\266\267\270\271"
|
|
|
|
"\272\273\274\275\276"
|
|
|
|
"\277"
|
|
|
|
"\300\301\302\303\304"
|
|
|
|
"\305\306\307\310\311"
|
|
|
|
"\312\313\314\315\316"
|
|
|
|
"\317\320\321\322\323"
|
|
|
|
"\324\325\326\327\330"
|
|
|
|
"\331\332\333\334\335"
|
|
|
|
"\336\337";
|
2023-08-28 09:42:42 -04:00
|
|
|
strcpy(target_list, printable_chars);
|
2023-08-23 19:59:59 -04:00
|
|
|
return sizeof(printable_chars)-1;
|
2023-08-19 18:49:10 -04:00
|
|
|
};
|
2023-08-23 19:59:59 -04:00
|
|
|
case 's': {
|
|
|
|
const char whitespace_chars[] = " \t\v\n";
|
2023-08-28 09:42:42 -04:00
|
|
|
strcpy(target_list, whitespace_chars);
|
2023-08-23 19:59:59 -04:00
|
|
|
return sizeof(whitespace_chars)-1;
|
2023-08-19 18:49:10 -04:00
|
|
|
};
|
2023-08-23 19:59:59 -04:00
|
|
|
case 'd': {
|
|
|
|
const char digit_chars[] = "0123456789";
|
2023-08-28 09:42:42 -04:00
|
|
|
strcpy(target_list, digit_chars);
|
2023-08-23 19:59:59 -04:00
|
|
|
return sizeof(digit_chars)-1;
|
2023-08-19 18:49:10 -04:00
|
|
|
};
|
2023-08-23 19:59:59 -04:00
|
|
|
case 'x': {
|
2023-08-24 11:21:21 -04:00
|
|
|
const char hex_chars[] = "0123456789"
|
2023-08-25 12:51:50 -04:00
|
|
|
"abcdef"
|
|
|
|
"ABCDEF";
|
2023-08-28 09:42:42 -04:00
|
|
|
strcpy(target_list, hex_chars);
|
2023-08-23 19:59:59 -04:00
|
|
|
return sizeof(hex_chars)-1;
|
2023-08-19 18:49:10 -04:00
|
|
|
};
|
2023-08-23 19:59:59 -04:00
|
|
|
case 'o': {
|
|
|
|
const char oct_chars[] = "01234567";
|
2023-08-28 09:42:42 -04:00
|
|
|
strcpy(target_list, oct_chars);
|
2023-08-23 19:59:59 -04:00
|
|
|
return sizeof(oct_chars)-1;
|
2023-08-19 18:49:10 -04:00
|
|
|
};
|
2023-08-23 19:59:59 -04:00
|
|
|
case 'w': {
|
2023-08-24 11:21:21 -04:00
|
|
|
const char word_chars[] = "0123456789"
|
2023-08-25 12:51:50 -04:00
|
|
|
"abcdefghijklmnopqrstuwxyz"
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUWXYZ"
|
|
|
|
"_";
|
2023-08-28 09:42:42 -04:00
|
|
|
strcpy(target_list, word_chars);
|
2023-08-23 19:59:59 -04:00
|
|
|
return sizeof(word_chars)-1;
|
2023-08-19 18:49:10 -04:00
|
|
|
};
|
2023-08-23 19:59:59 -04:00
|
|
|
case 'h': {
|
2023-08-24 11:21:21 -04:00
|
|
|
const char very_word_chars[] = "abcdefghijklmnopqrstuwxyz"
|
2023-08-25 12:51:50 -04:00
|
|
|
"ABCDEFGHIJKLMNOPQRSTUWXYZ"
|
|
|
|
"_";
|
2023-08-28 09:42:42 -04:00
|
|
|
strcpy(target_list, very_word_chars);
|
2023-08-23 19:59:59 -04:00
|
|
|
return sizeof(very_word_chars)-1;
|
2023-08-19 18:49:10 -04:00
|
|
|
};
|
2023-08-23 19:59:59 -04:00
|
|
|
case 'a': {
|
2023-08-24 11:21:21 -04:00
|
|
|
const char alpha_chars[] = "abcdefghijklmnopqrstuwxyz"
|
2023-08-25 12:51:50 -04:00
|
|
|
"ABCDEFGHIJKLMNOPQRSTUWXYZ";
|
2023-08-28 09:42:42 -04:00
|
|
|
strcpy(target_list, alpha_chars);
|
2023-08-23 19:59:59 -04:00
|
|
|
return sizeof(alpha_chars)-1;
|
2023-08-19 18:49:10 -04:00
|
|
|
};
|
2023-08-23 19:59:59 -04:00
|
|
|
case 'l': {
|
|
|
|
const char lower_alpha_chars[] = "abcdefghijklmnopqrstuwxyz";
|
2023-08-28 09:42:42 -04:00
|
|
|
strcpy(target_list, lower_alpha_chars);
|
2023-08-23 19:59:59 -04:00
|
|
|
return sizeof(lower_alpha_chars)-1;
|
2023-08-19 18:49:10 -04:00
|
|
|
};
|
2023-08-23 19:59:59 -04:00
|
|
|
case 'u': {
|
|
|
|
const char upper_alpha_chars[] = "ABCDEFGHIJKLMNOPQRSTUWXYZ";
|
2023-08-28 09:42:42 -04:00
|
|
|
strcpy(target_list, upper_alpha_chars);
|
2023-08-23 19:59:59 -04:00
|
|
|
return sizeof(upper_alpha_chars)-1;
|
2023-08-19 18:49:10 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-08-23 19:59:59 -04:00
|
|
|
return 0;
|
2023-08-19 18:49:10 -04:00
|
|
|
}
|
|
|
|
|
2023-08-28 09:43:25 -04:00
|
|
|
static int escape_to_negative(const char c,
|
|
|
|
compiler_state * cs) {
|
|
|
|
switch (c) {
|
|
|
|
case 'D': {
|
|
|
|
const char digit_chars[] = "0123456789";
|
|
|
|
strcpy(cs->blacklist, digit_chars);
|
|
|
|
*cs->is_negative = true;
|
|
|
|
return sizeof(digit_chars)-1;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-08-24 13:17:14 -04:00
|
|
|
//static int compile_hologram(char * hologram, char * whitelist) {
|
|
|
|
// if (hologram[0] == '\\') {
|
|
|
|
// switch (hologram[1]) {
|
|
|
|
// case '<': {
|
|
|
|
// const char very_word_chars[] = "abcdefghijklmnopqrstuwxyz"
|
|
|
|
// "ABCDEFGHIJKLMNOPQRSTUWXYZ"
|
|
|
|
// "_";
|
|
|
|
// strcat(whitelist, very_word_chars);
|
|
|
|
// is_negative = true;
|
|
|
|
// HOOK_ALL(0, whitelist, 0)
|
|
|
|
// } break;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
|
2023-08-28 09:43:25 -04:00
|
|
|
static int compile_dot(compiler_state * cs) {
|
|
|
|
*cs->do_catch = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int compile_escape(const char c,
|
|
|
|
compiler_state * cs) {
|
|
|
|
|
|
|
|
return escape_1_to_1(c, cs)
|
|
|
|
|| escape_1_to_N(c, cs)
|
|
|
|
|| escape_to_negative(c, cs)
|
|
|
|
//|| compile_hologram(*s, whitelist)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int compile_range(const char * const range,
|
|
|
|
compiler_state * cs) {
|
2023-08-24 13:17:14 -04:00
|
|
|
assert((range[0] == '[') && "Not a range.");
|
2023-08-19 18:49:10 -04:00
|
|
|
|
2023-08-28 09:43:25 -04:00
|
|
|
char * target_list = (*cs->is_negative) ? cs->blacklist : cs->whitelist;
|
|
|
|
|
2023-08-23 19:59:59 -04:00
|
|
|
const char * s;
|
2023-08-24 08:32:36 -04:00
|
|
|
if (range[1] == '^') {
|
2023-08-28 09:43:25 -04:00
|
|
|
*cs->is_negative = true;
|
2023-08-24 08:32:36 -04:00
|
|
|
s = range + 2;
|
|
|
|
} else {
|
|
|
|
s = range + 1;
|
|
|
|
}
|
|
|
|
for (; *s != ']'; s++) {
|
2023-08-24 13:17:14 -04:00
|
|
|
assert((*s != '\0') && "Unclosed range.");
|
2023-08-23 19:59:59 -04:00
|
|
|
char c = *s;
|
2023-08-28 09:42:42 -04:00
|
|
|
if (c == '\\') {
|
|
|
|
s += 1;
|
|
|
|
assert(compile_escape(*s, cs) && "Unknown escape.");
|
2023-08-23 19:59:59 -04:00
|
|
|
} else if (*(s+1) == '-') {
|
|
|
|
char end = *(s+2);
|
2023-08-24 13:17:14 -04:00
|
|
|
assert((c < end) && "Endless range.");
|
2023-08-23 19:59:59 -04:00
|
|
|
for (char cc = c; cc < end+1; cc++) {
|
2023-08-28 09:42:42 -04:00
|
|
|
strncat(target_list, &cc, 1);
|
|
|
|
strncat(target_list, "\0", 1);
|
2023-08-19 18:49:10 -04:00
|
|
|
}
|
2023-08-23 19:59:59 -04:00
|
|
|
s += 2;
|
|
|
|
} else {
|
2023-08-28 09:42:42 -04:00
|
|
|
strncat(target_list, &c, 1);
|
2023-08-23 19:59:59 -04:00
|
|
|
}
|
|
|
|
}
|
2023-08-19 18:49:10 -04:00
|
|
|
|
2023-08-23 19:59:59 -04:00
|
|
|
return ((s - range) + 1);
|
|
|
|
}
|
2023-08-19 18:49:10 -04:00
|
|
|
|
2023-08-28 09:42:42 -04:00
|
|
|
void filter_blacklist(const char * const whitelist,
|
|
|
|
const char * const blacklist,
|
|
|
|
char * const filtered) {
|
|
|
|
for (char * black_pointer = blacklist; *black_pointer != '\0'; black_pointer++) {
|
|
|
|
for(char * white_pointer = blacklist; *white_pointer != '\0'; white_pointer++) {
|
|
|
|
if (*black_pointer == *white_pointer) {
|
|
|
|
goto long_continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
strncat(filtered, black_pointer, 1);
|
|
|
|
long_continue:
|
|
|
|
}
|
|
|
|
}
|
2023-08-24 08:32:36 -04:00
|
|
|
|
2023-08-28 09:42:42 -04:00
|
|
|
#define HALT_AND_CATCH_FIRE INT_MIN
|
2023-08-24 15:08:31 -04:00
|
|
|
|
2023-08-28 09:42:42 -04:00
|
|
|
void HOOK_ALL( int from,
|
|
|
|
const char * const str,
|
|
|
|
int to,
|
|
|
|
compiler_state * cs) {
|
|
|
|
|
|
|
|
int hook_to = (to == HALT_AND_CATCH_FIRE) ? -1 : ((*cs->state) + to);
|
2023-08-24 15:08:31 -04:00
|
|
|
|
2023-08-25 15:06:26 -04:00
|
|
|
|
2023-08-24 15:08:31 -04:00
|
|
|
for (const char * s = str; *s != '\0'; s++) {
|
2023-08-25 15:06:26 -04:00
|
|
|
delta_t * delta = malloc(sizeof(delta_t));
|
|
|
|
delta->in = *cs->state + from;
|
|
|
|
delta->input = *s;
|
|
|
|
delta->to = hook_to;
|
|
|
|
delta->width = *cs->width;
|
2023-08-24 15:08:31 -04:00
|
|
|
vector_push(&cs->regex->delta_table,
|
2023-08-25 15:06:26 -04:00
|
|
|
&delta);
|
2023-08-24 15:08:31 -04:00
|
|
|
}
|
|
|
|
}
|
2023-08-24 08:32:36 -04:00
|
|
|
|
2023-08-28 09:42:42 -04:00
|
|
|
void OFFSHOOT(int from,
|
|
|
|
int to,
|
|
|
|
compiler_state * cs) {
|
|
|
|
offshoot_t * offshoot = malloc(sizeof(offshoot_t));
|
|
|
|
offshoot->in = *cs->state + from;
|
|
|
|
offshoot->to = *cs->state + to;
|
|
|
|
vector_push(&cs->regex->catch_table,
|
|
|
|
&offshoot);
|
|
|
|
}
|
2023-08-24 08:32:36 -04:00
|
|
|
|
2023-08-23 19:59:59 -04:00
|
|
|
regex_t * regex_compile(const char * const pattern) {
|
2023-08-23 21:16:55 -04:00
|
|
|
regex_t * regex = (regex_t *)malloc(sizeof(regex_t));
|
2023-08-23 19:59:59 -04:00
|
|
|
regex->str = strdup(pattern);
|
2023-08-25 15:17:00 -04:00
|
|
|
vector_init(®ex->delta_table, sizeof(delta_t*), 0UL);
|
|
|
|
vector_init(®ex->catch_table, sizeof(offshoot_t*), 0UL);
|
2023-08-19 18:49:10 -04:00
|
|
|
|
2023-08-23 19:59:59 -04:00
|
|
|
int state = 0;
|
2023-08-19 18:49:10 -04:00
|
|
|
|
2023-08-23 19:59:59 -04:00
|
|
|
bool do_catch;
|
2023-08-24 08:32:36 -04:00
|
|
|
bool is_negative;
|
2023-08-24 13:17:14 -04:00
|
|
|
int width;
|
2023-08-24 15:08:31 -04:00
|
|
|
char whitelist[64];
|
2023-08-28 09:42:42 -04:00
|
|
|
char blacklist[64];
|
2023-08-24 15:08:31 -04:00
|
|
|
|
|
|
|
compiler_state cs = {
|
|
|
|
.do_catch = &do_catch,
|
|
|
|
.is_negative = &is_negative,
|
|
|
|
.state = &state,
|
|
|
|
.width = &width,
|
|
|
|
.whitelist = whitelist,
|
2023-08-28 09:42:42 -04:00
|
|
|
.blacklist = blacklist,
|
2023-08-24 15:08:31 -04:00
|
|
|
.regex = regex,
|
|
|
|
};
|
|
|
|
|
2023-08-23 19:59:59 -04:00
|
|
|
for (const char * s = pattern; *s != '\00';) {
|
2023-08-28 09:42:42 -04:00
|
|
|
// Reset the compiler
|
2023-08-23 19:59:59 -04:00
|
|
|
assert(!is_quantifier(*pattern) && "Pattern starts with quantifier.");
|
|
|
|
whitelist[0] = '\00';
|
2023-08-28 09:42:42 -04:00
|
|
|
blacklist[0] = '\00';
|
2023-08-23 19:59:59 -04:00
|
|
|
do_catch = false;
|
2023-08-25 19:16:05 -04:00
|
|
|
is_negative = false;
|
2023-08-24 13:17:14 -04:00
|
|
|
width = 1;
|
2023-08-24 08:32:36 -04:00
|
|
|
|
2023-08-28 09:42:42 -04:00
|
|
|
// Translate char
|
2023-08-23 19:59:59 -04:00
|
|
|
switch (*s) {
|
|
|
|
case '.': {
|
2023-08-28 09:42:42 -04:00
|
|
|
compile_dot(&cs);
|
2023-08-23 19:59:59 -04:00
|
|
|
} break;
|
|
|
|
case '\\': {
|
2023-08-28 09:42:42 -04:00
|
|
|
s += 1;
|
|
|
|
assert(compile_escape(*s, &cs) && "Unknown escape.");
|
2023-08-23 19:59:59 -04:00
|
|
|
} break;
|
|
|
|
case '[': {
|
2023-08-28 09:42:42 -04:00
|
|
|
s += compile_range(s, &cs) - 1;
|
2023-08-23 19:59:59 -04:00
|
|
|
} break;
|
|
|
|
default: {
|
|
|
|
whitelist[0] = *s;
|
|
|
|
whitelist[1] = '\00';
|
|
|
|
} break;
|
|
|
|
}
|
2023-08-19 18:49:10 -04:00
|
|
|
|
2023-08-28 09:42:42 -04:00
|
|
|
s += 1;
|
2023-08-19 18:49:10 -04:00
|
|
|
|
2023-08-28 09:42:42 -04:00
|
|
|
// Compile with quantifier
|
2023-08-23 19:59:59 -04:00
|
|
|
switch (*s) {
|
2023-08-24 13:17:14 -04:00
|
|
|
case '=':
|
2023-08-23 19:59:59 -04:00
|
|
|
case '?': {
|
2023-08-24 15:08:31 -04:00
|
|
|
HOOK_ALL(0, whitelist, +1, &cs);
|
2023-08-28 09:42:42 -04:00
|
|
|
if (do_catch || is_negative) {
|
|
|
|
OFFSHOOT(0, +1, &cs);
|
|
|
|
}
|
|
|
|
s += 1;
|
2023-08-23 19:59:59 -04:00
|
|
|
} break;
|
|
|
|
case '*': {
|
2023-08-24 15:08:31 -04:00
|
|
|
HOOK_ALL(0, whitelist, 0, &cs);
|
2023-08-28 09:42:42 -04:00
|
|
|
if (do_catch) {
|
|
|
|
OFFSHOOT(0, +1, &cs);
|
|
|
|
} else if (is_negative) {
|
|
|
|
OFFSHOOT(0, 0, &cs);
|
|
|
|
}
|
|
|
|
s += 1;
|
2023-08-23 19:59:59 -04:00
|
|
|
} break;
|
|
|
|
case '+': {
|
2023-08-24 15:08:31 -04:00
|
|
|
HOOK_ALL(0, whitelist, +1, &cs);
|
2023-08-28 09:42:42 -04:00
|
|
|
if (do_catch || is_negative) {
|
|
|
|
OFFSHOOT(0, +1, &cs);
|
|
|
|
}
|
2023-08-23 19:59:59 -04:00
|
|
|
state += 1;
|
2023-08-24 15:08:31 -04:00
|
|
|
HOOK_ALL(0, whitelist, 0, &cs);
|
2023-08-28 09:42:42 -04:00
|
|
|
if (do_catch || is_negative) {
|
|
|
|
OFFSHOOT(0, 0, &cs);
|
|
|
|
}
|
|
|
|
s += 1;
|
2023-08-23 19:59:59 -04:00
|
|
|
} break;
|
|
|
|
default: { // Literal
|
2023-08-24 15:08:31 -04:00
|
|
|
HOOK_ALL(0, whitelist, +1, &cs);
|
2023-08-28 09:42:42 -04:00
|
|
|
if (do_catch || is_negative) {
|
|
|
|
OFFSHOOT(0, +1, &cs);
|
|
|
|
}
|
2023-08-23 19:59:59 -04:00
|
|
|
state += 1;
|
|
|
|
} break;
|
2023-08-19 18:49:10 -04:00
|
|
|
}
|
2023-08-28 09:42:42 -04:00
|
|
|
|
|
|
|
// Compile blacklist
|
|
|
|
if (*blacklist) {
|
|
|
|
char filtered_blacklist[64];
|
|
|
|
filtered_blacklist[0] = '\0';
|
|
|
|
filter_blacklist(whitelist, blacklist, filtered_blacklist);
|
|
|
|
HOOK_ALL(0, filtered_blacklist, HALT_AND_CATCH_FIRE, &cs);
|
|
|
|
}
|
2023-08-23 19:59:59 -04:00
|
|
|
}
|
2023-08-19 18:49:10 -04:00
|
|
|
|
2023-08-23 19:59:59 -04:00
|
|
|
regex->accepting_state = state;
|
|
|
|
|
2023-08-23 21:16:55 -04:00
|
|
|
return regex;
|
2023-08-23 19:59:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int regex_free(regex_t * const regex) {
|
|
|
|
free(regex->str);
|
2023-08-23 21:16:55 -04:00
|
|
|
vector_free(®ex->delta_table);
|
|
|
|
vector_free(®ex->catch_table);
|
|
|
|
free(regex);
|
2023-08-23 19:59:59 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-24 09:19:35 -04:00
|
|
|
// -----------------
|
2023-08-24 09:00:44 -04:00
|
|
|
// ### Searching ###
|
2023-08-24 09:19:35 -04:00
|
|
|
// -----------------
|
2023-08-25 19:16:05 -04:00
|
|
|
static bool catch_(const regex_t * const regex,
|
|
|
|
int * const state) {
|
|
|
|
for (size_t i = 0; i < regex->catch_table.element_count; i++){
|
|
|
|
const offshoot_t * const offshoot = *(offshoot_t**)vector_get(®ex->catch_table, i);
|
|
|
|
if (offshoot->in == *state) {
|
|
|
|
*state = offshoot->to;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int regex_assert(const regex_t * const regex,
|
2023-08-24 09:00:44 -04:00
|
|
|
const char * const string,
|
2023-08-25 19:16:05 -04:00
|
|
|
int state,
|
2023-08-28 09:43:25 -04:00
|
|
|
int width) {
|
2023-08-23 19:59:59 -04:00
|
|
|
for (const char * s = string; *s != '\00'; s++) {
|
|
|
|
// delta
|
2023-08-24 13:17:14 -04:00
|
|
|
for (size_t i = 0; i < regex->delta_table.element_count; i++) {
|
2023-08-25 15:06:26 -04:00
|
|
|
const delta_t * const delta = *(delta_t**)vector_get(®ex->delta_table, i);
|
2023-08-23 21:16:55 -04:00
|
|
|
if ((delta->in == state)
|
2023-08-23 19:59:59 -04:00
|
|
|
&& (delta->input == *s)) {
|
2023-08-26 16:39:07 -04:00
|
|
|
int r = regex_assert(regex, s + delta->width, delta->to, width + 1);
|
|
|
|
if(r){
|
|
|
|
return r;
|
2023-08-23 19:59:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-23 21:16:55 -04:00
|
|
|
if (catch_(regex, &state)) {
|
2023-08-28 09:43:25 -04:00
|
|
|
width += 1;
|
2023-08-23 19:59:59 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-08-26 16:39:07 -04:00
|
|
|
return (state == regex->accepting_state) ? width : false;
|
2023-08-23 19:59:59 -04:00
|
|
|
}
|
|
|
|
|
2023-08-25 19:16:05 -04:00
|
|
|
return false;
|
2023-08-23 19:59:59 -04:00
|
|
|
}
|
|
|
|
|
2023-08-25 19:16:05 -04:00
|
|
|
int regex_match( regex_t * regex,
|
|
|
|
const char * const string) {
|
2023-08-23 19:59:59 -04:00
|
|
|
if (regex == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (string == NULL) {
|
|
|
|
return true;
|
2023-08-21 14:07:39 -04:00
|
|
|
}
|
2023-08-23 19:59:59 -04:00
|
|
|
|
2023-08-26 16:39:07 -04:00
|
|
|
return regex_assert(regex, string, 0, 0);
|
2023-08-19 18:49:10 -04:00
|
|
|
}
|
2023-08-25 19:16:05 -04:00
|
|
|
|
|
|
|
bool regex_search( regex_t * regex,
|
|
|
|
const char * const string) {
|
|
|
|
|
2023-08-26 16:39:07 -04:00
|
|
|
return (bool)regex_match(regex, string);
|
2023-08-25 19:16:05 -04:00
|
|
|
}
|