Skip to content

Commit

Permalink
Mask mode: Fix incorrect use to CTYPE macros, so ?W will case-
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
magnumripper committed Oct 24, 2015
1 parent 39f6259 commit 416bede
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
14 changes: 7 additions & 7 deletions src/mask.c
Expand Up @@ -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]);
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion src/unicode.c
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
12 changes: 10 additions & 2 deletions src/unicode.h
Expand Up @@ -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);
Expand Down

3 comments on commit 416bede

@magnumripper
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Performance actually got better too 😎

@jfoug
Copy link
Collaborator

@jfoug jfoug commented on 416bede Oct 24, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how the old-school ctype macros were written. they are MUCH faster than the new crap, but only worked for one CP (ASCII at the time).

Are there other places where we need to convert from C ctype.h usages to enc_*() macros ?

@magnumripper
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The others I found are fine, they just handle things like command line options. No wait, dyna compiler had some... I thought they were for things like MD5 -> md5 but come to think of it: In case they are for things like lc($p) we should really use the enc_tolower stuff (btw we have other functions that do a whole string in one call).

Please sign in to comment.