From 416bede7f778b56239f5b6bf41519fa9a54d1332 Mon Sep 17 00:00:00 2001 From: magnum Date: Sat, 24 Oct 2015 10:56:38 +0200 Subject: [PATCH] Mask mode: Fix incorrect use to CTYPE macros, so ?W will case- toggle according to our active encoding - not just ASCII. Closes #1847, as other uses of ctype macros was audited too and no other needed fixing. --- src/mask.c | 14 +++++++------- src/unicode.c | 11 ++++++++++- src/unicode.h | 12 ++++++++++-- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/mask.c b/src/mask.c index 36ef20d815..bd7fb56a94 100644 --- a/src/mask.c +++ b/src/mask.c @@ -2145,21 +2145,21 @@ int do_mask_crack(const char *extern_key) i = 0; while(template_key_offsets[i] != -1) { int offset = template_key_offsets[i] & 0xffff; - unsigned char is_lower = (template_key_offsets[i++] >> 16) - == 'w'; + unsigned char toggle = (template_key_offsets[i++] >> 16) == 'W'; int cpy_len = max_keylen - offset; + cpy_len = cpy_len > key_len ? key_len : cpy_len; - if (is_lower) - memcpy(template_key + offset, extern_key, cpy_len); + if (!toggle) + memcpy(template_key + offset, extern_key, cpy_len); else { int z; for (z = 0; z < cpy_len; ++z) { - if (islower(ARCH_INDEX(extern_key[z]))) + if (enc_islower(extern_key[z])) template_key[offset + z] = - toupper(ARCH_INDEX(extern_key[z])); + enc_toupper(extern_key[z]); else template_key[offset + z] = - tolower(ARCH_INDEX(extern_key[z])); + enc_tolower(extern_key[z]); } } } diff --git a/src/unicode.c b/src/unicode.c index 30921a06be..27332a85fe 100644 --- a/src/unicode.c +++ b/src/unicode.c @@ -49,7 +49,9 @@ UTF16 CP_to_Unicode[0x100]; static UTF8 CP_from_Unicode[0x10000]; UTF8 CP_up[0x100]; +UTF8 CP_ups[0x100]; UTF8 CP_down[0x100]; +UTF8 CP_lows[0x100]; #ifndef UNICODE_NO_OPTIONS static int UnicodeType = -1; @@ -1044,7 +1046,7 @@ void initUnicode(int type) { #ifndef UNICODE_NO_OPTIONS - unsigned i; + unsigned i, j; unsigned char *cpU, *cpL, *Sep, *Letter; unsigned char *pos; int encoding; @@ -1546,6 +1548,13 @@ void initUnicode(int type) CP_down[0x91] = 0x91; } #endif + j = 0; + for (i = 0; i < 256; i++) { + if (CP_up[i] != CP_down[i]) { + CP_ups[j] = CP_up[i]; + CP_lows[j++] = CP_down[i]; + } + } return; } diff --git a/src/unicode.h b/src/unicode.h index 4e504a440c..6211793188 100644 --- a/src/unicode.h +++ b/src/unicode.h @@ -246,13 +246,21 @@ extern char *enc_strupper(char *s); extern UTF16 CP_to_Unicode[0x100]; /* Used by various formats uc/lc */ -extern UTF8 CP_up[0x100]; -extern UTF8 CP_down[0x100]; +extern UTF8 CP_up[0x100]; /* upper-case lookup table */ +extern UTF8 CP_ups[0x100]; /* all upper-case letters */ +extern UTF8 CP_down[0x100]; /* lower-case lookup table */ +extern UTF8 CP_lows[0x100]; /* all lower-case letters */ /* Used by single.c and loader.c */ extern UTF8 CP_isLetter[0x100]; extern UTF8 CP_isSeparator[0x100]; +/* These are encoding-aware but not LC_CTYPE */ +#define enc_islower(c) (pers_opts.internal_cp == ASCII ? (c > 'a' && c < 'z') : (strchr((char*)CP_lows, ARCH_INDEX(c)) != NULL)) +#define enc_isupper(c) (pers_opts.internal_cp == ASCII ? (c > 'A' && c < 'Z') : ((strchr((char*)CP_ups, ARCH_INDEX(c)) != NULL)) +#define enc_tolower(c) (char)CP_down[ARCH_INDEX(c)] +#define enc_toupper(c) (char)CP_up[ARCH_INDEX(c)] + /* Conversion between encoding names and integer id */ extern int cp_name2id(char *encoding); extern char *cp_id2name(int encoding);