Skip to content

Commit 0881803

Browse files
committed
Modes: Colorize warnings and/or use _ONCE macros
Wordlist, Mask, Subsets and PRINCE modes: Revise logging/warnings. wordlist.c: Use shared macros for warn-once and warn + log messages
1 parent af7ad8b commit 0881803

File tree

4 files changed

+36
-57
lines changed

4 files changed

+36
-57
lines changed

src/mask.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ uint64_t mask_parent_keys;
9797
*/
9898
static char* parse_hex(char *string)
9999
{
100-
static int warned;
101100
unsigned char *s = (unsigned char*)string;
102101
unsigned char *d = s;
103102

@@ -111,8 +110,8 @@ static char* parse_hex(char *string)
111110
} else if (*s == '\\' && s[1] == 'x' &&
112111
atoi16[s[2]] != 0x7f && atoi16[s[3]] != 0x7f) {
113112
char c = (atoi16[s[2]] << 4) + atoi16[s[3]];
114-
if (!c && !warned++ && john_main_process)
115-
fprintf(stderr, "Warning: \\x00 in mask terminates the string\n");
113+
if (!c && john_main_process)
114+
WARN_ONCE(color_warning, stderr, "Warning: \\x00 in mask terminates the string\n");
116115
if (strchr("\\[]?-", c))
117116
*d++ = '\\';
118117
*d++ = c;
@@ -157,13 +156,8 @@ static char* expand_cplhdr(char *string, int *conv_err)
157156
int pidx = s[1] - '1';
158157
char *cs = options.custom_mask[pidx];
159158

160-
if (conv_err[pidx]) {
161-
if (john_main_process)
162-
fprintf(stderr,
163-
"Error: Selected internal codepage can't hold all chars of mask placeholder ?%d\n",
164-
pidx + 1);
165-
error();
166-
}
159+
if (conv_err[pidx])
160+
error_msg("Error: Selected internal codepage can't hold all chars of mask placeholder ?%d\n", pidx + 1);
167161
if (*cs == 0) {
168162
if (john_main_process)
169163
fprintf(stderr, "Error: Custom mask placeholder ?%d not defined\n", pidx + 1);
@@ -2436,12 +2430,12 @@ static void finalize_mask(int len)
24362430
format_cannot_reset = 0;
24372431
if (john_main_process) {
24382432
fprintf(stderr, "Note: Disabling internal mask due to stacked rules\n");
2439-
log_event("- Disabling internal mask due to stacked rules");
2433+
LOG_ONCE("- Disabling internal mask due to stacked rules");
24402434
}
24412435
}
24422436
#if defined(HAVE_OPENCL) || defined(HAVE_ZTEX)
24432437
else if ((mask_fmt->params.flags & FMT_MASK) && options.req_int_cand_target > 0) {
2444-
log_event("- Overriding format's target internal mask factor of %d with user requested %d",
2438+
LOG_ONCE("- Overriding format's target internal mask factor of %d with user requested %d",
24452439
mask_int_cand_target, options.req_int_cand_target);
24462440
mask_int_cand_target = options.req_int_cand_target;
24472441
}
@@ -2514,9 +2508,14 @@ static void finalize_mask(int len)
25142508
mask_tot_cand = cand * mask_int_cand.num_int_cand;
25152509

25162510
if ((john_main_process || !cfg_get_bool(SECTION_OPTIONS, SUBSECTION_MPI, "MPIAllGPUsSame", 0)) &&
2517-
mask_int_cand.num_int_cand > 1)
2518-
log_event("- Requested internal mask factor: %d, actual now %d",
2519-
mask_int_cand_target, mask_int_cand.num_int_cand);
2511+
mask_int_cand.num_int_cand > 1) {
2512+
static int old_factor = -1;
2513+
if (mask_int_cand.num_int_cand != old_factor) {
2514+
log_event("- Requested internal mask factor: %d, actual now %d",
2515+
mask_int_cand_target, mask_int_cand.num_int_cand);
2516+
old_factor = mask_int_cand.num_int_cand;
2517+
}
2518+
}
25202519
}
25212520

25222521
void mask_crk_init(struct db_main *db)
@@ -2633,6 +2632,7 @@ int do_mask_crack(const char *extern_key)
26332632
}
26342633

26352634
mask_cur_len = i;
2635+
log_event("- Mask length now %u", mask_cur_len);
26362636

26372637
if (format_cannot_reset)
26382638
save_restore(&cpu_mask_ctx, 0, RESTORE);

src/pp.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -934,16 +934,14 @@ static int get_bits(mpz_t *op)
934934
*/
935935
static MAYBE_INLINE char *check_bom(char *string)
936936
{
937-
static int warned;
938-
939937
if (((unsigned char*)string)[0] < 0xef)
940938
return string;
941939
if (!memcmp(string, "\xEF\xBB\xBF", 3))
942940
string += 3;
943941
if (options.input_enc == UTF_8 &&
944942
(!memcmp(string, "\xFE\xFF", 2) || !memcmp(string, "\xFF\xFE", 2))) {
945-
if (john_main_process && !warned++)
946-
fprintf(stderr, "Warning: UTF-16 BOM seen in wordlist.\n");
943+
if (john_main_process)
944+
WARN_ONCE(color_warning, stderr, "Warning: UTF-16 BOM seen in wordlist.\n");
947945
string += 2;
948946
}
949947
return string;

src/subsets.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,6 @@ static void remove_dupes(UTF32 *string)
227227
/* Parse \U+HHHH and \U+HHHHH notation to characters, in place. */
228228
static void parse_unicode(char *string)
229229
{
230-
static int warned;
231230
unsigned char *s = (unsigned char*)string;
232231
unsigned char *d = s;
233232

@@ -249,9 +248,9 @@ static void parse_unicode(char *string)
249248
wc[0] = (atoi16[s[3]] << 16) + (atoi16[s[4]] << 12) +
250249
(atoi16[s[5]] << 8) + (atoi16[s[6]] << 4) + atoi16[s[7]];
251250
wc[1] = 0;
252-
if (!wc[0] && !warned++ && john_main_process)
253-
fprintf(stderr,
254-
"Warning: \\U+00000 in mask terminates the string\n");
251+
if (!wc[0] && john_main_process)
252+
WARN_ONCE(color_warning, stderr,
253+
"Warning: \\U+00000 in mask terminates the string\n");
255254
if (wc[0] == '\\')
256255
*d++ = '\\';
257256

@@ -270,9 +269,9 @@ static void parse_unicode(char *string)
270269
wc[0] = (atoi16[s[3]] << 12) + (atoi16[s[4]] << 8) +
271270
(atoi16[s[5]] << 4) + atoi16[s[6]];
272271
wc[1] = 0;
273-
if (!wc[0] && !warned++ && john_main_process)
274-
fprintf(stderr,
275-
"Warning: \\U+0000 in mask terminates the string\n");
272+
if (!wc[0] && john_main_process)
273+
WARN_ONCE(color_warning, stderr,
274+
"Warning: \\U+0000 in mask terminates the string\n");
276275
if (wc[0] == '\\')
277276
*d++ = '\\';
278277

src/wordlist.c

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,7 @@ static void restore_line_number(void)
149149
if (skip_lines(rec_pos, line)) {
150150
if (ferror(word_file))
151151
pexit("fgets");
152-
fprintf_color(color_error, stderr, "fgets: Unexpected EOF\n");
153-
error();
152+
error_msg("fgets: Unexpected EOF\n");
154153
}
155154
}
156155

@@ -319,21 +318,16 @@ static MAYBE_INLINE void check_bom(char *line)
319318
return;
320319

321320
if (!memcmp(line, "\xEF\xBB\xBF", 3)) {
322-
static int warned;
323-
324321
if (options.input_enc == UTF_8)
325322
memmove(line, line + 3, strlen(line) - 2);
326-
else if (!warned++)
327-
fprintf_color(color_warning, stderr, "Warning: UTF-8 BOM seen in wordlist. You probably want --input-encoding=UTF8\n");
323+
else
324+
WARN_ONCE(color_warning, stderr,
325+
"Warning: UTF-8 BOM seen in wordlist. You probably want --input-encoding=UTF8\n");
328326
}
329327

330-
if (options.input_enc == UTF_8 && (!memcmp(line, "\xFE\xFF", 2) || !memcmp(line, "\xFF\xFE", 2))) {
331-
static int warned;
332-
333-
if (!warned++)
334-
fprintf_color(color_warning, stderr,
335-
"Warning: UTF-16 BOM seen in wordlist. File may not be read properly unless you re-encode it\n");
336-
}
328+
if (options.input_enc == UTF_8 && (!memcmp(line, "\xFE\xFF", 2) || !memcmp(line, "\xFF\xFE", 2)))
329+
WARN_ONCE(color_warning, stderr,
330+
"Warning: UTF-16 BOM seen in wordlist. File may not be read properly unless you re-encode it\n");
337331
}
338332

339333
/*
@@ -722,15 +716,12 @@ void do_wordlist_crack(struct db_main *db, const char *name, int rules)
722716
}
723717
if (i > my_size) {
724718
fprintf_color(color_error, stderr,
725-
"Error: wordlist grew "
726-
"as we read it - "
727-
"aborting\n");
719+
"Error: Wordlist grew as we read it - aborting\n");
728720
error();
729721
}
730722
}
731723
if (nWordFileLines != myWordFileLines)
732-
fprintf_color(color_warning, stderr, "Warning: wordlist changed as"
733-
" we read it\n");
724+
WARN_ONCE(color_warning, stderr, "Warning: Wordlist changed as we read it\n");
734725
log_event("- Loaded this node's share of "
735726
"wordlist %s into memory "
736727
"(%"PRIu64" bytes of %"PRId64", max_size="Zu
@@ -769,12 +760,8 @@ void do_wordlist_crack(struct db_main *db, const char *name, int rules)
769760
"fread: Unexpected EOF\n");
770761
error();
771762
}
772-
if (memchr(word_file_str, 0, (size_t)file_len)) {
773-
static int warned;
774-
775-
if (!warned++)
776-
fprintf_color(color_warning, stderr, "Warning: Wordlist contains NUL bytes, lines may be truncated.\n");
777-
}
763+
if (memchr(word_file_str, 0, (size_t)file_len))
764+
WARN_ONCE(color_warning, stderr, "Warning: Wordlist contains NUL bytes, lines may be truncated.\n");
778765
}
779766
aep = word_file_str + file_len;
780767
*aep = 0;
@@ -827,13 +814,8 @@ void do_wordlist_crack(struct db_main *db, const char *name, int rules)
827814
{
828815
char *ep, ec;
829816
if (i > nWordFileLines) {
830-
fprintf_color(color_warning, stderr, "Warning: wordlist "
831-
"contains inconsequent "
832-
"newlines, some words may be "
833-
"skipped\n");
834-
log_event("- Warning: wordlist contains"
835-
" inconsequent newlines, some"
836-
" words may be skipped");
817+
WARN_AND_LOG_ONCE(color_warning, stderr,
818+
"Warning: Wordlist contains inconsequent newlines, some words may be skipped");
837819
i--;
838820
break;
839821
}

0 commit comments

Comments
 (0)