libhl/source/regex.c

377 lines
9.3 KiB
C
Raw Normal View History

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-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-23 19:59:59 -04:00
for (const char * s = "+*?"; *s != '\00'; s++) {
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;
}
for (const char * s = "\\[]"; *s != '\00'; s++) {
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;
} delta_t;
typedef struct {
int in;
int to;
} offshoot_t;
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-23 22:11:46 -04:00
static int escape_1_to_1(const char c, char * whitelist) {
2023-08-23 19:59:59 -04:00
switch(c) {
case 't': {
strcat(whitelist, "\t");
} return 1;
case 'n': {
strcat(whitelist, "\n");
} return 1;
case 'r': {
strcat(whitelist, "\r");
} return 1;
case 'b': {
strcat(whitelist, "\b");
} return 1;
case '[': {
strcat(whitelist, "[");
} return 1;
case ']': {
strcat(whitelist, "]");
} return 1;
case '.': {
strcat(whitelist, ".");
} return 1;
case '?': {
strcat(whitelist, "?");
} return 1;
case '+': {
strcat(whitelist, "+");
} return 1;
case '*': {
strcat(whitelist, "*");
} return 1;
case '\\': {
strcat(whitelist, "\\");
} return 1;
}
return 0;
2023-08-19 18:49:10 -04:00
}
2023-08-23 22:11:46 -04:00
static int escape_1_to_N(const char c, char * whitelist) {
2023-08-23 19:59:59 -04:00
switch(c) {
case 'i': {
const char identifier_chars[] = "@0123456789_\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";
strcpy(whitelist, identifier_chars);
return sizeof(identifier_chars)-1;
};
case 'I': {
const char identifier_chars[] = "@_\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";
strcpy(whitelist, identifier_chars);
return sizeof(identifier_chars)-1;
2023-08-19 18:49:10 -04:00
};
2023-08-23 19:59:59 -04:00
case 'k': {
const char keyword_chars[] = "@0123456789_\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";
strcpy(whitelist, keyword_chars);
return sizeof(keyword_chars)-1;
2023-08-19 18:49:10 -04:00
};
2023-08-23 19:59:59 -04:00
case 'K': {
const char keyword_chars[] = "@_\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";
strcpy(whitelist, keyword_chars);
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-23 21:16:55 -04:00
strcpy(whitelist, filename_chars);
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-23 21:16:55 -04:00
strcpy(whitelist, filename_chars);
return sizeof(filename_chars)-1;
2023-08-19 18:49:10 -04:00
};
2023-08-23 19:59:59 -04:00
case 'p': {
const char printable_chars[] = "@\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";
strcpy(whitelist, printable_chars);
return sizeof(printable_chars)-1;
2023-08-19 18:49:10 -04:00
};
2023-08-23 19:59:59 -04:00
case 'P': {
const char printable_chars[] = "@\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";
strcpy(whitelist, printable_chars);
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";
strcpy(whitelist, whitespace_chars);
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";
strcpy(whitelist, digit_chars);
return sizeof(digit_chars)-1;
2023-08-19 18:49:10 -04:00
};
2023-08-23 19:59:59 -04:00
case 'x': {
const char hex_chars[] = "0123456789abcdefABCDEF";
strcpy(whitelist, hex_chars);
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";
strcpy(whitelist, oct_chars);
return sizeof(oct_chars)-1;
2023-08-19 18:49:10 -04:00
};
2023-08-23 19:59:59 -04:00
case 'w': {
const char word_chars[] = "0123456789abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ_";
strcpy(whitelist, word_chars);
return sizeof(word_chars)-1;
2023-08-19 18:49:10 -04:00
};
2023-08-23 19:59:59 -04:00
case 'h': {
const char very_word_chars[] = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ_";
strcpy(whitelist, very_word_chars);
return sizeof(very_word_chars)-1;
2023-08-19 18:49:10 -04:00
};
2023-08-23 19:59:59 -04:00
case 'a': {
const char alpha_chars[] = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ";
strcpy(whitelist, alpha_chars);
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";
strcpy(whitelist, lower_alpha_chars);
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";
strcpy(whitelist, upper_alpha_chars);
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-23 22:11:46 -04:00
static int compile_range(const char * const range,
char * whitelist,
bool * is_negative) {
2023-08-23 19:59:59 -04:00
assert(range[0] == '[' && "Not a range.");
2023-08-19 18:49:10 -04:00
2023-08-23 19:59:59 -04:00
int r = 0;
const char * s;
if (range[1] == '^') {
*is_negative = true;
s = range + 2;
} else {
s = range + 1;
}
for (; *s != ']'; s++) {
2023-08-23 19:59:59 -04:00
assert(*s != '\00' && "Unclosed range.");
char c = *s;
if (escape_1_to_1(c, whitelist)
|| escape_1_to_N(c, whitelist)) {
;
} else if (*(s+1) == '-') {
char end = *(s+2);
assert(c < end && "Endless range.");
for (char cc = c; cc < end+1; cc++) {
strncat(whitelist, &cc, 1);
strncat(whitelist, "\00", 1);
2023-08-19 18:49:10 -04:00
}
2023-08-23 19:59:59 -04:00
s += 2;
} else {
++r;
strncat(whitelist, &c, 1);
strncat(whitelist, "\00", 1);
}
}
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-24 09:00:44 -04:00
static bool catch_(const regex_t * const regex,
int * const state) {
for (int i = 0; i < regex->catch_table.element_size; i++){
const offshoot_t * const offshoot = (vector_get(&regex->catch_table, i));
if (offshoot->in == *state) {
*state = offshoot->to;
return true;
}
}
return false;
}
#define HALT_AND_CATCH_FIRE -1
#define HOOK_ALL(from, str, to) do { \
int hook_to = (is_negative) ? -1 : state + to; \
for (char * s = str; *s != '\00'; s++) { \
vector_push(&regex->delta_table, \
&(delta_t){state + from, *s, hook_to} \
); \
} \
if (do_catch) { \
vector_push(&regex->catch_table, \
&(offshoot_t){state + from, hook_to} \
); \
} \
} while (0)
#define EAT(n) do { \
s += n; \
} while (0)
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-23 21:16:55 -04:00
vector_init(&regex->delta_table, sizeof(delta_t), 32);
vector_init(&regex->catch_table, sizeof(offshoot_t), 16);
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
char whitelist[64];
bool do_catch;
bool is_negative;
2023-08-23 19:59:59 -04:00
for (const char * s = pattern; *s != '\00';) {
// Get token
assert(!is_quantifier(*pattern) && "Pattern starts with quantifier.");
whitelist[0] = '\00';
do_catch = false;
2023-08-23 19:59:59 -04:00
switch (*s) {
case '.': {
do_catch = true;
} break;
case '\\': {
EAT(1);
if(escape_1_to_1(*s, whitelist)
|| escape_1_to_N(*s, whitelist)){
;
} else {
assert(!"Unknown escape.");
2023-08-19 18:49:10 -04:00
}
2023-08-23 19:59:59 -04:00
} break;
case '[': {
EAT(compile_range(s, whitelist, &is_negative)-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-23 19:59:59 -04:00
EAT(1);
2023-08-19 18:49:10 -04:00
2023-08-24 09:00:44 -04:00
// Get quantifier
2023-08-23 19:59:59 -04:00
switch (*s) {
case '?': {
HOOK_ALL(0, whitelist, +1);
EAT(1);
} break;
case '*': {
HOOK_ALL(0, whitelist, 0);
EAT(1);
} break;
case '+': {
HOOK_ALL(0, whitelist, +1);
state += 1;
HOOK_ALL(0, whitelist, 0);
EAT(1);
} break;
default: { // Literal
HOOK_ALL(0, whitelist, +1);
state += 1;
} break;
2023-08-19 18:49:10 -04:00
}
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(&regex->delta_table);
vector_free(&regex->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-24 09:00:44 -04:00
static bool regex_assert(const regex_t * const regex,
const char * const string,
int state) {
2023-08-19 18:49:10 -04:00
2023-08-23 19:59:59 -04:00
for (const char * s = string; *s != '\00'; s++) {
// delta
for (int i = 0; i < regex->delta_table.element_count; i++) {
2023-08-23 21:16:55 -04:00
const delta_t * const delta = (delta_t *)(vector_get(&regex->delta_table, i));
if ((delta->in == state)
2023-08-23 19:59:59 -04:00
&& (delta->input == *s)) {
if(regex_assert(regex, s+1, delta->to)){
return true;
}
}
}
2023-08-23 21:16:55 -04:00
if (catch_(regex, &state)) {
2023-08-23 19:59:59 -04:00
continue;
}
return false;
}
return (state == regex->accepting_state);
}
bool regex_search( regex_t * regex,
const char * const string) {
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
return regex_assert(regex, string, 0);
2023-08-19 18:49:10 -04:00
}