Skip to content

Commit d05dc5d

Browse files
committed
Fixed output channel & debug writing formats
1 parent ca695dd commit d05dc5d

File tree

3 files changed

+91
-48
lines changed

3 files changed

+91
-48
lines changed

main.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ void process_stage1_thread(const std::vector<std::string>& test_words) {
229229
} else {
230230
std::cout << "Deleted:\t";
231231
for (int i = 0; i < rule_set_pair.second.size(); i++) {
232-
rule_set_pair.second[i].print(2);
232+
rule_set_pair.second[i].print();
233233
if (i != rule_set_pair.second.size() - 1) std::cout << '\t';
234234
}
235235
std::cout << std::endl;
@@ -500,7 +500,7 @@ void process_stage3_thread(std::vector<std::pair<unsigned long, std::vector<Rule
500500
std::cout << std::endl;
501501
std::cout << "Deleted:\t";
502502
for(int j = 0; j < all_rules[rule_iterator].second.size(); j++) {
503-
all_rules[rule_iterator].second[j].print();
503+
all_rules[rule_iterator].second[j].print(0);
504504
if(j != all_rules[rule_iterator].second.size()-1) std::cout << '\t';
505505
}
506506
std::cout << std::endl;
@@ -755,7 +755,7 @@ void process_stage3_thread(std::vector<std::pair<unsigned long, std::vector<Rule
755755
}
756756
}
757757

758-
void process_stage3_thread_slow(std::vector<std::pair<unsigned long, std::vector<Rule>>>& all_rules, std::vector<std::pair<unsigned long, std::vector<Rule>>>& all_compare_rules, const std::vector<std::string>& test_words, bool optimize_similar_op) {
758+
void process_stage3_thread_slow(std::vector<std::pair<unsigned long, std::vector<Rule>>>& all_rules, std::vector<std::pair<unsigned long, std::vector<Rule>>>& all_compare_rules, const std::vector<std::string>& test_words, const bool optimize_similar_op) {
759759
// todo possible rewrite to check feasibility of file memory
760760
while(!rule_queue_stage_3.empty() || is_processing) {
761761
std::unique_lock<std::mutex> lock(lock_obj);
@@ -2054,7 +2054,7 @@ int main(int argc, const char *argv[]) {
20542054
line_counter++;
20552055
}
20562056
for(int i = 0; i < rule_pairs.second.size(); i++) {
2057-
rule_pairs.second[i].print();
2057+
rule_pairs.second[i].print(0, hashcat_output);
20582058
if(i != rule_pairs.second.size()-1) {
20592059
if(hashcat_output) {
20602060
std::cout << ' '; // hashcat formatting
@@ -2140,7 +2140,7 @@ int main(int argc, const char *argv[]) {
21402140
}
21412141
if (file_line != new_plain && !new_plain.empty()) {
21422142
for (Rule &rule_item: rule_pair.second) {
2143-
rule_item.print();
2143+
rule_item.print(0, hashcat_output);
21442144
std::cout << show_rule_delimiter;
21452145
}
21462146
std::cout << new_plain << '\n';

rule.cpp

Lines changed: 85 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ void Rule::process(std::string& plaintext) {
587587
return rule_processor(plaintext);
588588
}
589589

590-
std::string Rule::print(int output_channel) { // 0 = stdout, 1 = error, 2 = return
590+
std::string Rule::print(int output_channel, bool hashcat_output) { // 0 = stdout, 1 = error, 2 = return
591591
std::string rule_1_copy = rule_value_1;
592592
std::string rule_2_copy = rule_value_2;
593593
std::string debug_string;
@@ -601,60 +601,103 @@ std::string Rule::print(int output_channel) { // 0 = stdout, 1 = error, 2 = ret
601601
if(start_pos != std::string::npos) {
602602
rule_2_copy.replace(start_pos, 1, "\\x09");
603603
}
604-
// start_pos = rule_1_copy.find(' ');
605-
// if(start_pos != std::string::npos) {
606-
// rule_1_copy.replace(start_pos, 1, "\\x20");
607-
// }
608-
// start_pos = rule_2_copy.find(' ');
609-
// if(start_pos != std::string::npos) {
610-
// rule_2_copy.replace(start_pos, 1, "\\x20");
611-
// }
612-
613-
if(Rule::rule_identify(rule) == 3) {
614-
if(rule_value_1.size() > 1) { // intentionally take rule_value_1 to not take escapes into account.
615-
start_pos = rule_1_copy.find('/');
616-
if(start_pos != std::string::npos) {
617-
rule_1_copy.replace(start_pos, 1, "\\/");
618-
}
619604

620-
start_pos = rule_2_copy.find('/');
621-
if(start_pos != std::string::npos) {
622-
rule_2_copy.replace(start_pos, 1, "\\/");
623-
}
605+
if(hashcat_output) {
606+
start_pos = rule_1_copy.find(' ');
607+
if(start_pos != std::string::npos) {
608+
rule_1_copy.replace(start_pos, 1, "\\x20");
609+
}
610+
start_pos = rule_2_copy.find(' ');
611+
if(start_pos != std::string::npos) {
612+
rule_2_copy.replace(start_pos, 1, "\\x20");
613+
}
614+
}
624615

616+
switch(Rule::rule_identify(rule)) {
617+
case 1:
625618
if (output_channel == 0) {
626-
std::cerr << rule << '/' << rule_1_copy << '/' << rule_2_copy;
619+
std::cout << rule;
627620
} else if (output_channel == 1) {
628-
std::cout << rule << '/' << rule_1_copy << '/' << rule_2_copy;
621+
std::cerr << rule;
629622
} else if (output_channel == 2) {
630623
debug_string += rule;
631-
debug_string += '/';
632-
debug_string += rule_1_copy;
633-
debug_string += '/';
634-
debug_string += rule_2_copy;
635624
}
636-
637-
} else {
625+
break;
626+
case 2:
638627
if (output_channel == 0) {
639-
std::cerr << rule << '/' << rule_1_copy << '/' << rule_2_copy;
628+
std::cout << rule << rule_1_copy;
640629
} else if (output_channel == 1) {
641-
std::cout << rule << '/' << rule_1_copy << '/' << rule_2_copy;
630+
std::cerr << rule << rule_1_copy;
642631
} else if (output_channel == 2) {
643632
debug_string += rule;
644633
debug_string += rule_1_copy;
645-
debug_string += rule_2_copy;
646634
}
647-
}
648-
} else {
649-
if (output_channel == 0) {
650-
std::cerr << rule << '/' << rule_1_copy << '/' << rule_2_copy;
651-
} else if (output_channel == 1) {
652-
std::cout << rule << '/' << rule_1_copy << '/' << rule_2_copy;
653-
} else if (output_channel == 2) {
654-
debug_string += rule;
655-
debug_string += rule_1_copy;
656-
debug_string += rule_2_copy;
657-
}
635+
break;
636+
case 3:
637+
if (rule_value_1.size() > 1) { // if the first rule is multi-byte
638+
start_pos = rule_1_copy.find('/');
639+
if (start_pos != std::string::npos) {
640+
rule_1_copy.replace(start_pos, 1, "\\/");
641+
}
642+
643+
start_pos = rule_2_copy.find('/');
644+
if (start_pos != std::string::npos) {
645+
rule_2_copy.replace(start_pos, 1, "\\/");
646+
}
647+
648+
if (output_channel == 0) {
649+
std::cout << rule << '/' << rule_1_copy << '/' << rule_2_copy;
650+
} else if (output_channel == 1) {
651+
std::cerr << rule << '/' << rule_1_copy << '/' << rule_2_copy;
652+
} else if (output_channel == 2) {
653+
debug_string += rule;
654+
debug_string += '/';
655+
debug_string += rule_1_copy;
656+
debug_string += '/';
657+
debug_string += rule_2_copy;
658+
}
659+
} else if (rule_value_2.size() > 1) { // if the second rule is multi-byte
660+
start_pos = rule_1_copy.find('/');
661+
if (start_pos != std::string::npos) {
662+
rule_1_copy.replace(start_pos, 1, "\\/");
663+
}
664+
665+
start_pos = rule_2_copy.find('/');
666+
if (start_pos != std::string::npos) {
667+
rule_2_copy.replace(start_pos, 1, "\\/");
668+
}
669+
670+
if (output_channel == 0) {
671+
if (hashcat_output) {
672+
std::cout << rule << rule_1_copy << rule_2_copy;
673+
} else {
674+
std::cout << rule << '/' << rule_1_copy << '/' << rule_2_copy;
675+
}
676+
} else if (output_channel == 1) {
677+
if (hashcat_output) {
678+
std::cerr << rule << rule_1_copy << rule_2_copy;
679+
} else {
680+
std::cerr << rule << '/' << rule_1_copy << '/' << rule_2_copy;
681+
}
682+
} else if (output_channel == 2) {
683+
debug_string += rule;
684+
if (!hashcat_output) debug_string += '/';
685+
debug_string += rule_1_copy;
686+
if (!hashcat_output) debug_string += '/';
687+
debug_string += rule_2_copy;
688+
}
689+
} else { // rule 1 is not multi-byte
690+
if (output_channel == 0) {
691+
std::cout << rule << rule_1_copy << rule_2_copy;
692+
} else if (output_channel == 1) {
693+
std::cerr << rule << rule_1_copy << rule_2_copy;
694+
} else if (output_channel == 2) {
695+
debug_string += rule;
696+
debug_string += rule_1_copy;
697+
debug_string += rule_2_copy;
698+
}
699+
}
700+
break;
658701
}
659702
return debug_string;
660703
}

rule.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Rule {
1919
bool invalid_rule;
2020

2121
Rule(char input_rule, const std::string &input_rule_value_1, const std::string &input_rule_value_2);
22-
std::string print(int output_channel=0);
22+
std::string print(int output_channel=0, bool hashcat_output=false);
2323
void process(std::string& plaintext);
2424
std::function<void(std::string&)> build_rule_processor();
2525
bool operator==(const Rule& rhs) const;

0 commit comments

Comments
 (0)