From caf1f6a478d217b8449fda7a31df4f327fa637b4 Mon Sep 17 00:00:00 2001 From: Gabriele Gristina Date: Fri, 1 Nov 2024 18:27:51 +0100 Subject: [PATCH] Improve Purge rule backwards compatibility and simplify code --- OpenCL/inc_rp.cl | 19 +++++++---- OpenCL/inc_rp.h | 14 ++------ OpenCL/inc_rp_optimized.cl | 19 +++++++---- OpenCL/inc_rp_optimized.h | 14 ++------ include/types.h | 7 +--- src/rp.c | 65 ++++++++------------------------------ src/rp_cpu.c | 7 ++++ 7 files changed, 51 insertions(+), 94 deletions(-) diff --git a/OpenCL/inc_rp.cl b/OpenCL/inc_rp.cl index 8837ab119..fc368f43c 100644 --- a/OpenCL/inc_rp.cl +++ b/OpenCL/inc_rp.cl @@ -713,6 +713,18 @@ DECLSPEC int mangle_purgeclass_s (MAYBE_UNUSED const u8 p0, MAYBE_UNUSED const u return (out_len); } +DECLSPEC int mangle_purgeclass (MAYBE_UNUSED const u8 p0, MAYBE_UNUSED const u8 p1, PRIVATE_AS u8 *buf, const int len) +{ + if (p0 == 'l') return mangle_purgeclass_l (p0, p1, buf, len); + else if (p0 == 'u') return mangle_purgeclass_u (p0, p1, buf, len); + else if (p0 == 'd') return mangle_purgeclass_d (p0, p1, buf, len); + else if (p0 == 'h') return mangle_purgeclass_lh (p0, p1, buf, len); + else if (p0 == 'H') return mangle_purgeclass_uh (p0, p1, buf, len); + else if (p0 == 's') return mangle_purgeclass_s (p0, p1, buf, len); + + return len; +} + DECLSPEC int mangle_dupechar_first (MAYBE_UNUSED const u8 p0, MAYBE_UNUSED const u8 p1, PRIVATE_AS u8 *buf, const int len) { const int out_len = len + p0; @@ -943,12 +955,7 @@ DECLSPEC int apply_rule (const u32 name, MAYBE_UNUSED const u8 p0, MAYBE_UNUSED case RULE_OP_MANGLE_TRUNCATE_AT: out_len = mangle_truncate_at (p0, p1, (PRIVATE_AS u8 *) buf, out_len); break; case RULE_OP_MANGLE_REPLACE: out_len = mangle_replace (p0, p1, (PRIVATE_AS u8 *) buf, out_len); break; case RULE_OP_MANGLE_PURGECHAR: out_len = mangle_purgechar (p0, p1, (PRIVATE_AS u8 *) buf, out_len); break; - case RULE_OP_MANGLE_PURGECLASS_L: out_len = mangle_purgeclass_l (p0, p1, (PRIVATE_AS u8 *) buf, out_len); break; - case RULE_OP_MANGLE_PURGECLASS_U: out_len = mangle_purgeclass_u (p0, p1, (PRIVATE_AS u8 *) buf, out_len); break; - case RULE_OP_MANGLE_PURGECLASS_D: out_len = mangle_purgeclass_d (p0, p1, (PRIVATE_AS u8 *) buf, out_len); break; - case RULE_OP_MANGLE_PURGECLASS_LH: out_len = mangle_purgeclass_lh (p0, p1, (PRIVATE_AS u8 *) buf, out_len); break; - case RULE_OP_MANGLE_PURGECLASS_UH: out_len = mangle_purgeclass_uh (p0, p1, (PRIVATE_AS u8 *) buf, out_len); break; - case RULE_OP_MANGLE_PURGECLASS_S: out_len = mangle_purgeclass_s (p0, p1, (PRIVATE_AS u8 *) buf, out_len); break; + case RULE_OP_MANGLE_PURGECLASS: out_len = mangle_purgeclass (p0, p1, (PRIVATE_AS u8 *) buf, out_len); break; case RULE_OP_MANGLE_DUPECHAR_FIRST: out_len = mangle_dupechar_first (p0, p1, (PRIVATE_AS u8 *) buf, out_len); break; case RULE_OP_MANGLE_DUPECHAR_LAST: out_len = mangle_dupechar_last (p0, p1, (PRIVATE_AS u8 *) buf, out_len); break; case RULE_OP_MANGLE_DUPECHAR_ALL: out_len = mangle_dupechar_all (p0, p1, (PRIVATE_AS u8 *) buf, out_len); break; diff --git a/OpenCL/inc_rp.h b/OpenCL/inc_rp.h index acfda62b1..6811b44c4 100644 --- a/OpenCL/inc_rp.h +++ b/OpenCL/inc_rp.h @@ -40,12 +40,7 @@ #define RULE_OP_MANGLE_TRUNCATE_AT '\'' #define RULE_OP_MANGLE_REPLACE 's' #define RULE_OP_MANGLE_PURGECHAR '@' -#define RULE_OP_MANGLE_PURGECLASS_L 0x01 -#define RULE_OP_MANGLE_PURGECLASS_U 0x02 -#define RULE_OP_MANGLE_PURGECLASS_D 0x03 -#define RULE_OP_MANGLE_PURGECLASS_LH 0x04 -#define RULE_OP_MANGLE_PURGECLASS_UH 0x05 -#define RULE_OP_MANGLE_PURGECLASS_S 0x06 +#define RULE_OP_MANGLE_PURGECLASS 0x01 #define RULE_OP_MANGLE_TOGGLECASE_REC 'a' #define RULE_OP_MANGLE_DUPECHAR_FIRST 'z' #define RULE_OP_MANGLE_DUPECHAR_LAST 'Z' @@ -109,12 +104,7 @@ DECLSPEC int mangle_overstrike (MAYBE_UNUSED const u8 p0, MAYBE_UNUSED const u8 DECLSPEC int mangle_truncate_at (MAYBE_UNUSED const u8 p0, MAYBE_UNUSED const u8 p1, PRIVATE_AS u8 *buf, const int len); DECLSPEC int mangle_replace (MAYBE_UNUSED const u8 p0, MAYBE_UNUSED const u8 p1, PRIVATE_AS u8 *buf, const int len); DECLSPEC int mangle_purgechar (MAYBE_UNUSED const u8 p0, MAYBE_UNUSED const u8 p1, PRIVATE_AS u8 *buf, const int len); -DECLSPEC int mangle_purgeclass_l (MAYBE_UNUSED const u8 p0, MAYBE_UNUSED const u8 p1, PRIVATE_AS u8 *buf, const int len); -DECLSPEC int mangle_purgeclass_u (MAYBE_UNUSED const u8 p0, MAYBE_UNUSED const u8 p1, PRIVATE_AS u8 *buf, const int len); -DECLSPEC int mangle_purgeclass_d (MAYBE_UNUSED const u8 p0, MAYBE_UNUSED const u8 p1, PRIVATE_AS u8 *buf, const int len); -DECLSPEC int mangle_purgeclass_lh (MAYBE_UNUSED const u8 p0, MAYBE_UNUSED const u8 p1, PRIVATE_AS u8 *buf, const int len); -DECLSPEC int mangle_purgeclass_uh (MAYBE_UNUSED const u8 p0, MAYBE_UNUSED const u8 p1, PRIVATE_AS u8 *buf, const int len); -DECLSPEC int mangle_purgeclass_s (MAYBE_UNUSED const u8 p0, MAYBE_UNUSED const u8 p1, PRIVATE_AS u8 *buf, const int len); +DECLSPEC int mangle_purgeclass (MAYBE_UNUSED const u8 p0, MAYBE_UNUSED const u8 p1, PRIVATE_AS u8 *buf, const int len); DECLSPEC int mangle_dupechar_first (MAYBE_UNUSED const u8 p0, MAYBE_UNUSED const u8 p1, PRIVATE_AS u8 *buf, const int len); DECLSPEC int mangle_dupechar_last (MAYBE_UNUSED const u8 p0, MAYBE_UNUSED const u8 p1, PRIVATE_AS u8 *buf, const int len); DECLSPEC int mangle_dupechar_all (MAYBE_UNUSED const u8 p0, MAYBE_UNUSED const u8 p1, PRIVATE_AS u8 *buf, const int len); diff --git a/OpenCL/inc_rp_optimized.cl b/OpenCL/inc_rp_optimized.cl index 094716773..5982ff9b0 100644 --- a/OpenCL/inc_rp_optimized.cl +++ b/OpenCL/inc_rp_optimized.cl @@ -2114,6 +2114,18 @@ DECLSPEC HC_INLINE_RP u32 rule_op_mangle_purgeclass_s (MAYBE_UNUSED const u32 p0 return out_len; } +DECLSPEC HC_INLINE_RP u32 rule_op_mangle_purgeclass (MAYBE_UNUSED const u32 p0, MAYBE_UNUSED const u32 p1, MAYBE_UNUSED PRIVATE_AS u32 *buf0, MAYBE_UNUSED PRIVATE_AS u32 *buf1, const u32 in_len) +{ + if ((u8)p0 == 'l') return rule_op_mangle_purgeclass_l (p0, p1, buf0, buf1, in_len); + else if ((u8)p0 == 'u') return rule_op_mangle_purgeclass_u (p0, p1, buf0, buf1, in_len); + else if ((u8)p0 == 'd') return rule_op_mangle_purgeclass_d (p0, p1, buf0, buf1, in_len); + else if ((u8)p0 == 'h') return rule_op_mangle_purgeclass_lh (p0, p1, buf0, buf1, in_len); + else if ((u8)p0 == 'H') return rule_op_mangle_purgeclass_uh (p0, p1, buf0, buf1, in_len); + else if ((u8)p0 == 's') return rule_op_mangle_purgeclass_s (p0, p1, buf0, buf1, in_len); + + return in_len; +} + DECLSPEC HC_INLINE_RP u32 rule_op_mangle_dupechar_first (MAYBE_UNUSED const u32 p0, MAYBE_UNUSED const u32 p1, MAYBE_UNUSED PRIVATE_AS u32 *buf0, MAYBE_UNUSED PRIVATE_AS u32 *buf1, const u32 in_len) { if ( in_len == 0) return in_len; @@ -2670,12 +2682,7 @@ DECLSPEC u32 apply_rule_optimized (const u32 name, const u32 p0, const u32 p1, P case RULE_OP_MANGLE_TRUNCATE_AT: out_len = rule_op_mangle_truncate_at (p0, p1, buf0, buf1, out_len); break; case RULE_OP_MANGLE_REPLACE: out_len = rule_op_mangle_replace (p0, p1, buf0, buf1, out_len); break; case RULE_OP_MANGLE_PURGECHAR: out_len = rule_op_mangle_purgechar (p0, p1, buf0, buf1, out_len); break; - case RULE_OP_MANGLE_PURGECLASS_L: out_len = rule_op_mangle_purgeclass_l (p0, p1, buf0, buf1, out_len); break; - case RULE_OP_MANGLE_PURGECLASS_U: out_len = rule_op_mangle_purgeclass_u (p0, p1, buf0, buf1, out_len); break; - case RULE_OP_MANGLE_PURGECLASS_D: out_len = rule_op_mangle_purgeclass_d (p0, p1, buf0, buf1, out_len); break; - case RULE_OP_MANGLE_PURGECLASS_LH: out_len = rule_op_mangle_purgeclass_lh (p0, p1, buf0, buf1, out_len); break; - case RULE_OP_MANGLE_PURGECLASS_UH: out_len = rule_op_mangle_purgeclass_uh (p0, p1, buf0, buf1, out_len); break; - case RULE_OP_MANGLE_PURGECLASS_S: out_len = rule_op_mangle_purgeclass_s (p0, p1, buf0, buf1, out_len); break; + case RULE_OP_MANGLE_PURGECLASS: out_len = rule_op_mangle_purgeclass (p0, p1, buf0, buf1, out_len); break; //case RULE_OP_MANGLE_TOGGLECASE_REC: out_len = rule_op_mangle_togglecase_rec (p0, p1, buf0, buf1, out_len); break; case RULE_OP_MANGLE_DUPECHAR_FIRST: out_len = rule_op_mangle_dupechar_first (p0, p1, buf0, buf1, out_len); break; case RULE_OP_MANGLE_DUPECHAR_LAST: out_len = rule_op_mangle_dupechar_last (p0, p1, buf0, buf1, out_len); break; diff --git a/OpenCL/inc_rp_optimized.h b/OpenCL/inc_rp_optimized.h index 5b0b7ff99..bbed93fa9 100644 --- a/OpenCL/inc_rp_optimized.h +++ b/OpenCL/inc_rp_optimized.h @@ -46,12 +46,7 @@ #define RULE_OP_MANGLE_TRUNCATE_AT '\'' #define RULE_OP_MANGLE_REPLACE 's' #define RULE_OP_MANGLE_PURGECHAR '@' -#define RULE_OP_MANGLE_PURGECLASS_L 0x01 -#define RULE_OP_MANGLE_PURGECLASS_U 0x02 -#define RULE_OP_MANGLE_PURGECLASS_D 0x03 -#define RULE_OP_MANGLE_PURGECLASS_LH 0x04 -#define RULE_OP_MANGLE_PURGECLASS_UH 0x05 -#define RULE_OP_MANGLE_PURGECLASS_S 0x06 +#define RULE_OP_MANGLE_PURGECLASS 0x01 #define RULE_OP_MANGLE_TOGGLECASE_REC 'a' #define RULE_OP_MANGLE_DUPECHAR_FIRST 'z' #define RULE_OP_MANGLE_DUPECHAR_LAST 'Z' @@ -119,12 +114,7 @@ DECLSPEC u32 search_on_register (const u32 in, const u32 p0); DECLSPEC u32 replace_on_register (const u32 in, const u32 r, const u32 p1); DECLSPEC HC_INLINE_RP u32 rule_op_mangle_replace (MAYBE_UNUSED const u32 p0, MAYBE_UNUSED const u32 p1, MAYBE_UNUSED PRIVATE_AS u32 *buf0, MAYBE_UNUSED PRIVATE_AS u32 *buf1, const u32 in_len); DECLSPEC HC_INLINE_RP u32 rule_op_mangle_purgechar (MAYBE_UNUSED const u32 p0, MAYBE_UNUSED const u32 p1, MAYBE_UNUSED PRIVATE_AS u32 *buf0, MAYBE_UNUSED PRIVATE_AS u32 *buf1, const u32 in_len); -DECLSPEC HC_INLINE_RP u32 rule_op_mangle_purgeclass_l (MAYBE_UNUSED const u32 p0, MAYBE_UNUSED const u32 p1, MAYBE_UNUSED PRIVATE_AS u32 *buf0, MAYBE_UNUSED PRIVATE_AS u32 *buf1, const u32 in_len); -DECLSPEC HC_INLINE_RP u32 rule_op_mangle_purgeclass_u (MAYBE_UNUSED const u32 p0, MAYBE_UNUSED const u32 p1, MAYBE_UNUSED PRIVATE_AS u32 *buf0, MAYBE_UNUSED PRIVATE_AS u32 *buf1, const u32 in_len); -DECLSPEC HC_INLINE_RP u32 rule_op_mangle_purgeclass_d (MAYBE_UNUSED const u32 p0, MAYBE_UNUSED const u32 p1, MAYBE_UNUSED PRIVATE_AS u32 *buf0, MAYBE_UNUSED PRIVATE_AS u32 *buf1, const u32 in_len); -DECLSPEC HC_INLINE_RP u32 rule_op_mangle_purgeclass_lh (MAYBE_UNUSED const u32 p0, MAYBE_UNUSED const u32 p1, MAYBE_UNUSED PRIVATE_AS u32 *buf0, MAYBE_UNUSED PRIVATE_AS u32 *buf1, const u32 in_len); -DECLSPEC HC_INLINE_RP u32 rule_op_mangle_purgeclass_uh (MAYBE_UNUSED const u32 p0, MAYBE_UNUSED const u32 p1, MAYBE_UNUSED PRIVATE_AS u32 *buf0, MAYBE_UNUSED PRIVATE_AS u32 *buf1, const u32 in_len); -DECLSPEC HC_INLINE_RP u32 rule_op_mangle_purgeclass_s (MAYBE_UNUSED const u32 p0, MAYBE_UNUSED const u32 p1, MAYBE_UNUSED PRIVATE_AS u32 *buf0, MAYBE_UNUSED PRIVATE_AS u32 *buf1, const u32 in_len); +DECLSPEC HC_INLINE_RP u32 rule_op_mangle_purgeclass (MAYBE_UNUSED const u32 p0, MAYBE_UNUSED const u32 p1, MAYBE_UNUSED PRIVATE_AS u32 *buf0, MAYBE_UNUSED PRIVATE_AS u32 *buf1, const u32 in_len); DECLSPEC HC_INLINE_RP u32 rule_op_mangle_dupechar_first (MAYBE_UNUSED const u32 p0, MAYBE_UNUSED const u32 p1, MAYBE_UNUSED PRIVATE_AS u32 *buf0, MAYBE_UNUSED PRIVATE_AS u32 *buf1, const u32 in_len); DECLSPEC HC_INLINE_RP u32 rule_op_mangle_dupechar_last (MAYBE_UNUSED const u32 p0, MAYBE_UNUSED const u32 p1, MAYBE_UNUSED PRIVATE_AS u32 *buf0, MAYBE_UNUSED PRIVATE_AS u32 *buf1, const u32 in_len); DECLSPEC HC_INLINE_RP u32 rule_op_mangle_dupechar_all (MAYBE_UNUSED const u32 p0, MAYBE_UNUSED const u32 p1, MAYBE_UNUSED PRIVATE_AS u32 *buf0, MAYBE_UNUSED PRIVATE_AS u32 *buf1, const u32 in_len); diff --git a/include/types.h b/include/types.h index e0e42c37b..ba10d7651 100644 --- a/include/types.h +++ b/include/types.h @@ -331,12 +331,7 @@ typedef enum rule_functions RULE_OP_MANGLE_TRUNCATE_AT = '\'', RULE_OP_MANGLE_REPLACE = 's', RULE_OP_MANGLE_PURGECHAR = '@', - RULE_OP_MANGLE_PURGECLASS_L = 0x01, - RULE_OP_MANGLE_PURGECLASS_U = 0x02, - RULE_OP_MANGLE_PURGECLASS_D = 0x03, - RULE_OP_MANGLE_PURGECLASS_LH = 0x04, - RULE_OP_MANGLE_PURGECLASS_UH = 0x05, - RULE_OP_MANGLE_PURGECLASS_S = 0x06, + RULE_OP_MANGLE_PURGECLASS = 0x01, RULE_OP_MANGLE_TOGGLECASE_REC = 'a', RULE_OP_MANGLE_DUPECHAR_FIRST = 'z', RULE_OP_MANGLE_DUPECHAR_LAST = 'Z', diff --git a/src/rp.c b/src/rp.c index 5a5ccb409..da35b9da7 100644 --- a/src/rp.c +++ b/src/rp.c @@ -390,42 +390,28 @@ int cpu_rule_to_kernel_rule (char *rule_buf, u32 rule_len, kernel_rule_t *rule) case RULE_OP_MANGLE_PURGECHAR: if ((rule_pos + 1) < rule_len && rule_buf[rule_pos+1] == '?') { - if ((rule_pos + 2) == rule_len) return -1; + if ((rule_pos + 2) == rule_len) + { + SET_NAME (rule, rule_buf[rule_pos]); + SET_P0 (rule, rule_buf[rule_pos]); + break; + } switch (rule_buf[rule_pos+2]) { + case ' ': case '?': SET_NAME (rule, rule_buf[rule_pos]); SET_P0 (rule, rule_buf[rule_pos]); INCR_POS; break; case 'l': - SET_NAME (rule, RULE_OP_MANGLE_PURGECLASS_L); - INCR_POS; - INCR_POS; - break; case 'u': - SET_NAME (rule, RULE_OP_MANGLE_PURGECLASS_U); - INCR_POS; - INCR_POS; - break; case 'd': - SET_NAME (rule, RULE_OP_MANGLE_PURGECLASS_D); - INCR_POS; - INCR_POS; - break; case 'h': - SET_NAME (rule, RULE_OP_MANGLE_PURGECLASS_LH); - INCR_POS; - INCR_POS; - break; case 'H': - SET_NAME (rule, RULE_OP_MANGLE_PURGECLASS_UH); - INCR_POS; - INCR_POS; - break; case 's': - SET_NAME (rule, RULE_OP_MANGLE_PURGECLASS_S); - INCR_POS; + SET_NAME (rule, RULE_OP_MANGLE_PURGECLASS); + SET_P0 (rule, rule_buf[rule_pos+1]); INCR_POS; break; default : @@ -665,38 +651,13 @@ int kernel_rule_to_cpu_rule (char *rule_buf, kernel_rule_t *rule) case RULE_OP_MANGLE_PURGECHAR: rule_buf[rule_pos] = rule_cmd; GET_P0 (rule); - if (rule_buf[rule_pos] == '?') rule_buf[++rule_pos] = '?'; + if (rule_buf[rule_pos] == '?') rule_buf[++rule_pos] = '?'; // force @?? break; - case RULE_OP_MANGLE_PURGECLASS_L: + case RULE_OP_MANGLE_PURGECLASS: rule_buf[rule_pos++] = RULE_OP_MANGLE_PURGECHAR; - rule_buf[rule_pos++] = '?'; - rule_buf[rule_pos] = 'l'; - break; - case RULE_OP_MANGLE_PURGECLASS_U: - rule_buf[rule_pos++] = RULE_OP_MANGLE_PURGECHAR; - rule_buf[rule_pos++] = '?'; - rule_buf[rule_pos] = 'u'; - break; - case RULE_OP_MANGLE_PURGECLASS_D: - rule_buf[rule_pos++] = RULE_OP_MANGLE_PURGECHAR; - rule_buf[rule_pos++] = '?'; - rule_buf[rule_pos] = 'd'; - break; - case RULE_OP_MANGLE_PURGECLASS_LH: - rule_buf[rule_pos++] = RULE_OP_MANGLE_PURGECHAR; - rule_buf[rule_pos++] = '?'; - rule_buf[rule_pos] = 'h'; - break; - case RULE_OP_MANGLE_PURGECLASS_UH: - rule_buf[rule_pos++] = RULE_OP_MANGLE_PURGECHAR; - rule_buf[rule_pos++] = '?'; - rule_buf[rule_pos] = 'H'; - break; - case RULE_OP_MANGLE_PURGECLASS_S: - rule_buf[rule_pos++] = RULE_OP_MANGLE_PURGECHAR; - rule_buf[rule_pos++] = '?'; - rule_buf[rule_pos] = 's'; + rule_buf[rule_pos] = '?'; + GET_P0 (rule); break; case RULE_OP_MANGLE_TOGGLECASE_REC: diff --git a/src/rp_cpu.c b/src/rp_cpu.c index de836fd10..4c8a45893 100644 --- a/src/rp_cpu.c +++ b/src/rp_cpu.c @@ -809,9 +809,16 @@ int _old_apply_rule (const char *rule, int rule_len, char in[RP_PASSWORD_SIZE], NEXT_RULEPOS (rule_pos); if (rule_new[rule_pos] == '?') { + if ((rule_pos + 1) == rule_len_new) + { + out_len = mangle_purgechar (out, out_len, rule_new[rule_pos]); + break; + } + NEXT_RULEPOS (rule_pos); switch (rule_new[rule_pos]) { + case ' ': out_len = mangle_purgechar (out, out_len, rule_new[rule_pos-1]); break; case '?': out_len = mangle_purgechar (out, out_len, rule_new[rule_pos]); break; case 'l': out_len = mangle_purgeclass_l (out, out_len); break; case 'u': out_len = mangle_purgeclass_u (out, out_len); break;