Introduce simple regex utils

This commit is contained in:
kormax committed 2026-03-24 20:21:51 +02:00
1 parent 7bedde39a7
commit e44e1f1cbb
4 files changed
+178

No files matched your search

+87
View File
@@ -36,6 +36,7 @@
#include "cliparser.h"
#include "generator.h" // generate nuid
#include "iso14b.h" // defines for ETU conversions
#include "util.h" // regex utility
static int CmdHelp(const char *Cmd);
@@ -1168,6 +1169,91 @@ static int CmdAnalyseUnits(const char *Cmd) {
return PM3_SUCCESS;
}
static int CmdAnalyseRegex(const char *Cmd) {
CLIParserContext *ctx;
CLIParserInit(&ctx, "analyse regex",
"Regex utility (subset: ^ $ . * with \\\\ escape)",
"analyse regex --pattern '^A000' --text A000000476D0000111\n"
"analyse regex --pattern '.*500A416E64726F6964506179.*9000$' --text 6F8150500A416E64726F69645061799000 --insensitive\n"
"analyse regex --test"
);
void *argtable[] = {
arg_param_begin,
arg_str0("p", "pattern", "<str>", "regex pattern"),
arg_str0("d", "text", "<str>", "text to match"),
arg_lit0("i", "insensitive", "case-insensitive match"),
arg_lit0("t", "test", "run self tests"),
arg_param_end
};
CLIExecWithReturn(ctx, Cmd, argtable, true);
struct arg_str *arg_pattern = arg_get_str(ctx, 1);
struct arg_str *arg_text = arg_get_str(ctx, 2);
bool insensitive = arg_get_lit(ctx, 3);
bool selftest = arg_get_lit(ctx, 4);
if (selftest) {
CLIParserFree(ctx);
typedef struct {
const char *pattern;
const char *text;
bool case_insensitive;
bool expect_match;
} regex_test_case_t;
const regex_test_case_t tests[] = {
{.pattern = "^A000", .text = "A000000476D0000111", .case_insensitive = false, .expect_match = true},
{.pattern = "9000$", .text = "6F009000", .case_insensitive = false, .expect_match = true},
{.pattern = ".*500A416E64726F6964506179.*9000$", .text = "6F8150500A416E64726F69645061799000", .case_insensitive = true, .expect_match = true},
{.pattern = "^a0.*$", .text = "A0000000", .case_insensitive = true, .expect_match = true},
{.pattern = "^a0.*$", .text = "B0000000", .case_insensitive = true, .expect_match = false},
{.pattern = "A\\*B", .text = "ZZA*BZZ", .case_insensitive = false, .expect_match = true},
{.pattern = "A+B", .text = "AAAB", .case_insensitive = false, .expect_match = false},
{.pattern = "*ABC", .text = "ABC", .case_insensitive = false, .expect_match = false},
{.pattern = "ABC\\", .text = "ABC", .case_insensitive = false, .expect_match = false},
};
bool all_ok = true;
for (size_t i = 0; i < ARRAYLEN(tests); i++) {
bool matched = tests[i].case_insensitive
? str_regex_match_case_insensitive(tests[i].pattern, tests[i].text)
: str_regex_match(tests[i].pattern, tests[i].text);
bool ok = (matched == tests[i].expect_match);
PrintAndLogEx(ok ? SUCCESS : FAILED, "%zu. pattern=`%s` valid=%s match=%s ( %s )",
i + 1,
tests[i].pattern,
"true",
matched ? "true" : "false",
ok ? _GREEN_("ok") : _RED_("fail"));
if (!ok) {
all_ok = false;
}
}
PrintAndLogEx(all_ok ? SUCCESS : FAILED, "Tests ( %s )", all_ok ? _GREEN_("ok") : _RED_("fail"));
return all_ok ? PM3_SUCCESS : PM3_ESOFT;
}
if (arg_pattern->count == 0 || arg_text->count == 0) {
CLIParserFree(ctx);
PrintAndLogEx(ERR, "pattern and text are required unless --test is used");
return PM3_EINVARG;
}
const char *pattern = arg_pattern->sval[0];
const char *text = arg_text->sval[0];
bool matched = insensitive
? str_regex_match_case_insensitive(pattern, text)
: str_regex_match(pattern, text);
CLIParserFree(ctx);
PrintAndLogEx(matched ? SUCCESS : INFO, "Regex match: %s", matched ? _GREEN_("true") : _YELLOW_("false"));
return PM3_SUCCESS;
}
static command_t CommandTable[] = {
{"help", CmdHelp, AlwaysAvailable, "This help"},
{"lrc", CmdAnalyseLRC, AlwaysAvailable, "Generate final byte for XOR LRC"},
@@ -1180,6 +1266,7 @@ static command_t CommandTable[] = {
{"demodbuff", CmdAnalyseDemodBuffer, AlwaysAvailable, "Load binary string to DemodBuffer"},
{"freq", CmdAnalyseFreq, AlwaysAvailable, "Calc wave lengths"},
{"foo", CmdAnalyseFoo, AlwaysAvailable, "muxer"},
{"regex", CmdAnalyseRegex, AlwaysAvailable, "Regex utility (subset: ^ $ . * with \\\\ escape)"},
{"units", CmdAnalyseUnits, AlwaysAvailable, "convert ETU <> US <> SSP_CLK (3.39MHz)"},
{NULL, NULL, NULL, NULL}
};