-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.cpp
2185 lines (2009 loc) · 103 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <vector>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <thread>
#include <queue>
#include <mutex>
#include <set>
#include <condition_variable>
#include "rule.h"
inline bool file_exists (const std::string& file_name) {
std::ifstream infile(file_name);
return infile.good();
}
bool replace(std::string& str, const std::string& from, const std::string& to) {
size_t start_pos = str.find(from);
if(start_pos == std::string::npos)
return false;
str.replace(start_pos, from.length(), to);
return true;
}
std::vector<std::string> split(const std::string& input_string, char delimiter) {
std::vector<std::string> internal;
std::stringstream ss(input_string); // Turn the string into a stream.
std::string tok;
while(getline(ss, tok, delimiter)) {
internal.push_back(tok);
}
return internal;
}
void debug_file_log(const std::string& debug_file_name, const std::string& message) {
std::ofstream debugFile(debug_file_name, std::ios::app);
if (!debugFile.is_open()) {
std::cerr << "Error opening debug file for appending!" << std::endl;
return;
}
debugFile << message;
// Close the file
debugFile.close();
}
bool sort_lineorder_rules(const std::pair<unsigned long ,std::vector<Rule>> &a, const std::pair<unsigned long ,std::vector<Rule>> &b)
{
return (a.first < b.first);
}
static void show_usage() {
std::cerr << "Usage: RuleProcessorY [option(s)] > results.rule\n"
<< "Options:\n"
<< "\t-h,--help\t\t\tShow this help message\n"
<< "\t-w,--wordlist WORDLIST_FILE\tSpecify the input wordlist path\n"
<< "\t-r,--rules RULE_FILE\t\tSpecify the input rules path\n"
<< "\t--delimiter DELIMITER\t\tSpecify delimiter to use. Default: \\t, Default hashcat: \" \"\n\n"
<< "\t--hashcat-input\t\t\tUse hashcat rule format for input rules\n"
<< "\t--hashcat-output\t\tUse hashcat rule format for the output of rules\n\n"
<< "\t--optimize-no-op\t\tRemove rules that perform no operation \"$1 ]\"\n"
<< "\t--optimize-same-op\t\tRemove rules that perform the same operation \"$1 $1 ]\" => \"$1\"\n"
<< "\t--optimize-similar-op\t\tRemove one of the rules that performs a similar operation \"$1 ^1\" and \"^1 $1\"\n"
<< "\t--optimize-all\t\t\tAll the optimizations!\n"
<< "\t--optimize-compare COMPARE_FILE\tRemove rules from RULE_FILE found in COMPARE_FILE (like similar-op)\n"
<< "\t--optimize-debug [DEBUG_FILE]\tShow the modified rules in STDOUT if output file is not specified\n"
<< "\t--optimize-slow\t\t\tDo not use memory to store data (not recommended)\n"
<< "\t--optimized-words\t\tLose cracks, but remove more rules. !USE_WITH_CAUTION!\n"
<< "\t--show-rule [DELIMITER]\t\tPrint out the rule prior to the plain. Delimiter optional (not \\0 or -). Default: \\t\n\n"
<< "\t--format-rules\t\tPerform no actions, only format rules.\n"
<< "Version: 1.3.1\n\n"
<< std::endl;
}
std::vector<std::thread> threads;
std::queue<std::vector<std::pair<unsigned long, std::vector<Rule>>>> rule_queue;
std::queue<std::vector<long long>> rule_queue_stage_3;
std::vector<std::pair<unsigned long, std::vector<Rule>>> queue_buffer;
std::mutex lock_obj;
std::mutex result_rule_mutex;
std::condition_variable condition_var;
std::vector<std::pair<unsigned long, std::vector<Rule>>> good_rule_objects;
std::vector<std::pair<unsigned long, std::vector<Rule>>> bad_rule_objects;
std::vector<std::pair<unsigned long, std::string>> ordered_comments;
int improvement_counter_level_2 = 0;
int duplicates_removed_level_3_compare = 0;
int duplicates_removed_level_3 = 0;
int redundant_removed = 0;
bool is_processing{true};
bool optimize_debug{false};
std::string optimize_debug_file;
std::vector<std::string> invalid_lines;
// Convert from Hashcat to TSV format (for RuleProcessorY)
std::string convert_from_hashcat(unsigned long line_counter, std::string rule) {
// sets of each rule width
std::set<char> single_wide = { ':', 'l', 'u', 'c', 'C', 't', 'r', 'd', 'f', '{', '}', '[', ']', 'k', 'K', 'q','E' };
std::set<char> double_wide = { 'T', 'p', 'D', 'Z', 'z', '$', '^', '<', '>', '_', '\'', '!', '/', '@' ,'-', '+', 'y', 'Y', 'L', 'R', '.', ',', 'e' };
std::set<char> triple_wide = { 's', 'x', 'O', 'o', 'i', '*', '3' };
std::string formatted_rule;
int offset;
char baseRule;
formatted_rule = "";
for (offset = 0; offset < rule.length();) {
// get rule identifier
baseRule = rule[offset];
// skip if it's the space separator
if (baseRule == ' ')
offset += 1;
// check if the rule is 1 character wide
else if (single_wide.count(baseRule)) {
formatted_rule += rule.substr(offset, 1) + '\t';
offset += 1;
}
// check if the rule is 2 characters wide
else if (double_wide.count(baseRule)) {
// check for hex notation
if (rule.substr(offset + 1, 2) == "\\x") {
formatted_rule += rule.substr(offset, 5) + '\t';
offset += 5;
}
else {
formatted_rule += rule.substr(offset, 2) + '\t';
offset += 2;
}
}
// check if the rule is 3 characters wide
else if (triple_wide.count(baseRule)) {
// check for hex notation
if (rule.length() >= (offset+9) && (rule.substr(offset + 1, 2) == "\\x" && rule.substr(offset + 5, 2) == "\\x")) {
formatted_rule += rule.substr(offset, 9) + '\t';
offset += 9;
}
else if (rule.length() >= (offset+6) && (rule.substr(offset + 1, 2) == "\\x" || rule.substr(offset + 2, 2) == "\\x")) {
formatted_rule += rule.substr(offset, 6) + '\t';
offset += 6;
}
else {
formatted_rule += rule.substr(offset, 3) + '\t';
offset += 3;
}
}
// ignore if the line is a comment
else if (baseRule == '#')
offset = 254;
// error if the baseRule is unknown
else {
std::cerr << "Unknown rule function [" << line_counter << "]: \"" << baseRule << "\"" << std::endl;
offset = 254;
return "!!!INVALID_RULE!!!";
}
}
// Remove last \t
if (!formatted_rule.empty() && formatted_rule.back() == '\t') {
formatted_rule.pop_back();
}
return formatted_rule;
}
void process_stage1_thread(const std::vector<std::string>& test_words) {
while(!rule_queue.empty() || is_processing) {
std::unique_lock<std::mutex> lock(lock_obj);
condition_var.wait(lock, [&] {
return !(rule_queue.empty() && is_processing);
});
if (rule_queue.empty()) {
lock.unlock();
continue;
}
std::vector<std::pair<unsigned long, std::vector<Rule>>> rule_buffer = rule_queue.front();
rule_queue.pop();
lock.unlock();
for(auto& rule_set_pair : rule_buffer) {
bool changes_made = false;
for (const std::string &test_word: test_words) {
if (rule_set_pair.second[0].rule == ':') {
changes_made = true;
continue;
}
std::string new_plain{test_word};
for (Rule &rule_item: rule_set_pair.second) {
rule_item.process(new_plain);
}
if (test_word != new_plain && !new_plain.empty()) {
changes_made = true;
}
}
for (Rule &rule_item: rule_set_pair.second) {
if (rule_item.rule == 's' && rule_item.rule_value_1 != rule_item.rule_value_2) {
changes_made = true;
}
}
if (changes_made) {
std::unique_lock<std::mutex> new_lock(result_rule_mutex); // Lock
good_rule_objects.emplace_back(rule_set_pair);
new_lock.unlock();
continue;
}
// else
redundant_removed++;
if (optimize_debug) { // Debug values
std::unique_lock<std::mutex> new_lock(result_rule_mutex); // Lock
if(!optimize_debug_file.empty()) {
std::string debug_file_output_buffer = "Deleted:\t";
for (int i = 0; i < rule_set_pair.second.size(); i++) {
debug_file_output_buffer.append(rule_set_pair.second[i].print(2));
if (i != rule_set_pair.second.size() - 1) debug_file_output_buffer += '\t';
}
debug_file_output_buffer += '\n';
debug_file_log(optimize_debug_file, debug_file_output_buffer);
} else {
std::cerr << "Deleted:\t";
for (int i = 0; i < rule_set_pair.second.size(); i++) {
rule_set_pair.second[i].print(1);
if (i != rule_set_pair.second.size() - 1) std::cerr << '\t';
}
std::cerr << std::endl;
}
new_lock.unlock();
}
}
}
}
void process_stage2_thread(const std::vector<std::string>& test_words) {
// todo investigate why threading occasionally hangs / pause and does not close correctly. Causing a deadlock
while(!rule_queue.empty() || is_processing) {
std::unique_lock<std::mutex> lock(lock_obj);
condition_var.wait(lock, [&] {
return !(rule_queue.empty() && is_processing);
});
if(rule_queue.empty()) {
lock.unlock();
continue;
}
std::vector<std::pair<unsigned long, std::vector<Rule>>> rule_buffer = rule_queue.front();
rule_queue.pop();
lock.unlock();
for(auto& rule_set_pair : rule_buffer) {
// Create PowerSet
double pow_set_size = pow(2, rule_set_pair.second.size());
int counter, j;
bool found_new = false;
// Get original rule output to compare against powerset output
std::vector<std::string> original_rule_output;
for (const std::string &test_word: test_words) {
std::string new_plain{test_word};
for (Rule &rule_item: rule_set_pair.second) {
rule_item.process(new_plain);
}
if (test_word != new_plain && !new_plain.empty()) {
original_rule_output.push_back(std::move(new_plain));
}
}
// End fetching original rule output
for (counter = 0; counter < pow_set_size; counter++) {
std::vector<Rule> rule_power_set_item;
for (j = 0; j < rule_set_pair.second.size(); j++) {
if (counter & (1 << j))
rule_power_set_item.emplace_back(rule_set_pair.second[j]);
}
if(rule_power_set_item.size() > 1 && rule_power_set_item.size() < rule_set_pair.second.size()) {
// start testing
std::vector<std::string> powerset_item_output;
for (const std::string &test_word: test_words) {
std::string new_plain{test_word};
for (Rule &rule_item: rule_power_set_item) {
rule_item.process(new_plain);
}
if (test_word != new_plain && !new_plain.empty()) {
powerset_item_output.push_back(std::move(new_plain));
}
}
if (original_rule_output == powerset_item_output) {
// Do not modify if the rule_value_1 is greater than 1 char due to test-set not covering those cases.
bool modify = true;
for (const Rule &rule_item: rule_set_pair.second) {
if (Rule::rule_identify(rule_item.rule) == 3 && rule_item.rule_value_1.size() > 1) {
modify = false;
}
}
if (modify) {
found_new = true;
std::unique_lock<std::mutex> new_lock(result_rule_mutex); // Lock
if(optimize_debug && !optimize_debug_file.empty()) {
std::string debug_file_output_buffer = "Before:\t";
for (Rule rule: rule_set_pair.second) {
debug_file_output_buffer.append(rule.print(2));
debug_file_output_buffer += '\t';
}
debug_file_output_buffer += '\n';
debug_file_output_buffer += "After:\t";
for (Rule rule: rule_power_set_item) {
debug_file_output_buffer.append(rule.print(2));
debug_file_output_buffer += '\t';
}
debug_file_output_buffer += '\n';
debug_file_log(optimize_debug_file, debug_file_output_buffer);
} else if(optimize_debug) {
std::cerr << "Before:\t";
for (Rule rule: rule_set_pair.second) {
rule.print(1);
std::cerr << '\t';
}
std::cerr << std::endl;
std::cerr << "After:\t";
for (Rule rule: rule_power_set_item) {
rule.print(1);
std::cerr << '\t';
}
std::cerr << std::endl;
}
// Add improved / optimized rule
std::pair<unsigned long, std::vector<Rule>> improved_pair (rule_set_pair.first, std::move(rule_power_set_item));
good_rule_objects.push_back(improved_pair);
improvement_counter_level_2++;
new_lock.unlock(); // Unlock
break;
}
}
}
}
if(!found_new) {
// keep old rule
std::unique_lock<std::mutex> new_lock(result_rule_mutex); // Lock
good_rule_objects.push_back(std::move(rule_set_pair));
new_lock.unlock(); // Unlock
}
// end of buffer
}
//finalize
}
}
long double get_rule_performance(const Rule& rule) {
// obtained by testing on a single NVIDIA 3070v1
// perl -e 'print "D4\n" x 200000' > rule (rule to test)
// ./hashcat --potfile-disable -m900 afe04867ec7a3845145579a95f72e000 -O D:\Wordlists\rockyou.txt -r rule -n 64 -u 256 -T 512 --backend-vector-width 1 --force
// Take the GH/s value, one decimal place rounded to nearest quarter
switch(rule.rule) {
case ':':
return 25;
case 'l':
return 16.5;
case 'u':
return 16.75;
case 'c':
return 16;
case 'C':
return 15.75;
case 't':
return 16;
case 'T':
return 19.5;
case 'r':
return 16.25;
case 'd':
return 18;
case 'p': // pA
return 21.5;
case 'f':
return 14.5;
case '{':
return 20;
case '}':
return 19.5;
case '$': // $a
return 22.75;
case '^': // ^a
return 21.0;
case '[':
return 22.75;
case ']':
return 21;
case 'D':
return 21.75;
case 'x': // x46
return 19.75;
case 'O': // O31
return 20.5;
case 'i': // i4c
return 19;
case 'o': // o5e
return 19.5;
case '\'': // 5'
return 20;
case 's':
return 11.5;
case '@': // this one can go from 10-15 GH/s quite easily by choosing e or x respectively.
return 10.25;
case 'z':
return 18.75;
case 'Z':
return 9.75;
case 'q':
return 18.5;
case 'k':
return 20.5;
case 'K':
return 19.75;
case '*':
return 19.5;
case 'L': // L4
return 19.75;
case 'R': // R4
return 19.75;
case '+': // +4
return 19.75;
case '-': // -4
return 19.75;
case '.': // .4
return 10;
case ',': // ,4
return 10;
case 'y': // y4
return 17.75;
case 'Y': // Y4
return 18;
case 'E':
return 11.5;
case 'e': // e-
return 11;
case '3': // 30-
return 14.5;
}
return 15; // default a bit in the middle (lower end)
}
void process_stage3_thread(std::vector<std::pair<unsigned long, std::vector<Rule>>>& all_rules, const std::vector<std::vector<std::string>>& all_rules_output, const std::vector<std::vector<std::string>>& all_compare_rules_output, bool optimize_similar_op) {
while(!rule_queue_stage_3.empty() || is_processing) {
std::unique_lock<std::mutex> lock(lock_obj);
condition_var.wait(lock, [&] {
return !(rule_queue_stage_3.empty() && is_processing);
});
if (rule_queue_stage_3.empty()) {
lock.unlock();
continue;
}
std::vector<long long> buffer = rule_queue_stage_3.front();
rule_queue_stage_3.pop();
lock.unlock();
for (long long rule_iterator: buffer) { // Enumerate the buffer
std::pair<unsigned long, std::vector<Rule>> &rule_set_pair = all_rules[rule_iterator];
if(all_rules.size() < 2) { // Skip if too small
std::unique_lock<std::mutex> new_lock(result_rule_mutex); // Lock
good_rule_objects.push_back(std::move(rule_set_pair));
new_lock.unlock();
continue;
}
// Get rule set output from every other rule set
bool matches_none = true;
if(!all_compare_rules_output.empty()) { // if comparing to another rule-set
bool is_bad = false;
for (const auto & i : all_compare_rules_output) {
// Compare output from ruleset with comparison ruleset and if matches, do not save rule (i.e. delete it)
if (all_rules_output[rule_iterator] == i) {
// if good_rule_objects contains rule_set then skip
matches_none = false;
duplicates_removed_level_3_compare++;
std::unique_lock<std::mutex> new_lock(result_rule_mutex); // Lock
if(optimize_debug && !optimize_debug_file.empty()) {
std::string debug_file_output_buffer = "\nDeleted:\t";
for(int j = 0; j < all_rules[rule_iterator].second.size(); j++) {
debug_file_output_buffer.append(all_rules[rule_iterator].second[j].print(2));
if(j != all_rules[rule_iterator].second.size()-1) debug_file_output_buffer += '\t';
}
debug_file_output_buffer += '\n';
debug_file_log(optimize_debug_file, debug_file_output_buffer);
}
else if(optimize_debug) {
std::cerr << std::endl;
std::cerr << "Deleted:\t";
for(int j = 0; j < all_rules[rule_iterator].second.size(); j++) {
all_rules[rule_iterator].second[j].print(1);
if(j != all_rules[rule_iterator].second.size()-1) std::cerr << '\t';
}
std::cerr << std::endl;
}
bad_rule_objects.push_back(std::move(rule_set_pair));
new_lock.unlock();
}
}
if(!optimize_similar_op) { // skip if not intending to optimize the main file too.
if (matches_none) {
std::unique_lock<std::mutex> good_lock(result_rule_mutex);
good_rule_objects.emplace_back(rule_set_pair);
good_lock.unlock();
}
continue;
}
}
// Comparing to itself, if no comparison rule is set
for (size_t i = 0; i < all_rules.size(); i++) {
// Compare output from ruleset with comparison ruleset
if (all_rules_output[rule_iterator] == all_rules_output[i] && i != rule_iterator) {
// if good_rule_objects contains rule_set -> skip
std::pair<unsigned long, std::vector<Rule>> &rule_set_comparison_pair = all_rules[i];
matches_none = false;
bool rule_set_is_good = false;
std::unique_lock<std::mutex> good_lock(result_rule_mutex);
if (std::find(good_rule_objects.begin(), good_rule_objects.end(), rule_set_pair) != good_rule_objects.end()) { // if original rule is good, investigate. Comparison might need to be replaced or removed.
rule_set_is_good = true;
}
if (std::find(bad_rule_objects.begin(), bad_rule_objects.end(), rule_set_pair) != bad_rule_objects.end()) { // if original rule is bad, a good one already exists -> skip
good_lock.unlock();
continue;
}
if (std::find(bad_rule_objects.begin(), bad_rule_objects.end(), rule_set_comparison_pair) != bad_rule_objects.end()) { // if comparison is bad, a good one already exists -> skip
good_lock.unlock();
continue;
}
if (std::find(good_rule_objects.begin(), good_rule_objects.end(), rule_set_comparison_pair) != good_rule_objects.end()) { // if comparison is good -> skip. We only need to logically go through one condition
good_lock.unlock();
continue;
}
duplicates_removed_level_3++;
// save smaller size
if (rule_set_pair.second.size() < rule_set_comparison_pair.second.size()) {
// Set rule_set_pair line number to be the lowest of the two since they're identical outputs.
rule_set_pair.first = (rule_set_pair.first < rule_set_comparison_pair.first) ? rule_set_pair.first : rule_set_comparison_pair.first;
if (rule_set_is_good) { // if the rule_set is not already part of the good_rules, add it
bad_rule_objects.emplace_back(rule_set_comparison_pair);
} else {
good_rule_objects.emplace_back(rule_set_pair);
bad_rule_objects.emplace_back(rule_set_comparison_pair);
}
if(optimize_debug && !optimize_debug_file.empty()) {
std::string debug_file_output_buffer;
if(!rule_set_is_good) debug_file_output_buffer = "Kept:\t\t";
if(rule_set_is_good) debug_file_output_buffer = "Exists:\t\t";
for (int j = 0; j < rule_set_pair.second.size(); j++) {
debug_file_output_buffer.append(rule_set_pair.second[j].print(2));
if (j != rule_set_pair.second.size() - 1) debug_file_output_buffer += '\t';
}
debug_file_output_buffer += "\nDeleted:\t";
for (int j = 0; j < rule_set_comparison_pair.second.size(); j++) {
debug_file_output_buffer.append(rule_set_comparison_pair.second[j].print(2));
if (j != rule_set_comparison_pair.second.size() - 1) debug_file_output_buffer += '\t';
}
debug_file_output_buffer += '\n';
debug_file_log(optimize_debug_file, debug_file_output_buffer);
}
else if (optimize_debug) {
if(!rule_set_is_good) std::cerr << "Kept:\t\t";
if(rule_set_is_good) std::cerr << "Exists:\t\t";
for (int j = 0; j < rule_set_pair.second.size(); j++) {
rule_set_pair.second[j].print(1);
if (j != rule_set_pair.second.size() - 1) std::cerr << '\t';
}
std::cerr << std::endl;
std::cerr << "Deleted:\t";
for (int j = 0; j < rule_set_comparison_pair.second.size(); j++) {
rule_set_comparison_pair.second[j].print(1);
if (j != rule_set_comparison_pair.second.size() - 1) std::cerr << '\t';
}
std::cerr << std::endl;
}
good_lock.unlock();
continue;
}
// Don't save the larger rule
if(rule_set_pair.second.size() > rule_set_comparison_pair.second.size()) {
// Set rule_set_comparison_pair line number to be the lowest of the two since they're identical outputs.
rule_set_comparison_pair.first = (rule_set_comparison_pair.first < rule_set_pair.first) ? rule_set_comparison_pair.first : rule_set_pair.first;
if (rule_set_is_good) { // rule set is already good
// Remove rule_set_pair from the list.
good_rule_objects.erase(std::remove(good_rule_objects.begin(), good_rule_objects.end(), rule_set_pair), good_rule_objects.end());
// Add comparison pair to good objects instead
good_rule_objects.emplace_back(rule_set_comparison_pair);
bad_rule_objects.emplace_back(rule_set_pair);
} else { // rule set is not good
good_rule_objects.emplace_back(rule_set_comparison_pair);
bad_rule_objects.emplace_back(rule_set_pair);
}
if(optimize_debug && !optimize_debug_file.empty()) {
std::string debug_file_output_buffer;
if(!rule_set_is_good) debug_file_output_buffer = "Kept:\t\t";
if(rule_set_is_good) debug_file_output_buffer = "Exists:\t\t";
for (int j = 0; j < rule_set_comparison_pair.second.size(); j++) {
debug_file_output_buffer.append(rule_set_comparison_pair.second[j].print(2));
if (j != rule_set_comparison_pair.second.size() - 1) debug_file_output_buffer += '\t';
}
debug_file_output_buffer += "\nDeleted:\t";
for (int j = 0; j < rule_set_pair.second.size(); j++) {
debug_file_output_buffer.append(rule_set_pair.second[j].print(2));
if (j != rule_set_pair.second.size() - 1) debug_file_output_buffer += '\t';
}
debug_file_output_buffer += '\n';
debug_file_log(optimize_debug_file, debug_file_output_buffer);
}
else if(optimize_debug) {
if(!rule_set_is_good) std::cerr << "Kept:\t\t";
if(rule_set_is_good) std::cerr << "Exists:\t\t";
for (int j = 0; j < rule_set_comparison_pair.second.size(); j++) {
rule_set_comparison_pair.second[j].print(1);
if (j != rule_set_comparison_pair.second.size() - 1) std::cerr << '\t';
}
std::cerr << std::endl;
std::cerr << "Deleted:\t";
for (int j = 0; j < rule_set_pair.second.size(); j++) {
rule_set_pair.second[j].print(1);
if (j != rule_set_pair.second.size() - 1) std::cerr << '\t';
}
std::cerr << std::endl;
}
good_lock.unlock();
continue;
}
// Compare complexity and choose faster one.
if(rule_set_pair.second.size() == rule_set_comparison_pair.second.size()) { // if they're the same length, judge what is better efficiency
long double rule_performance = 0;
long double rule_comparison_performance = 0;
for(const auto& rule_item : rule_set_pair.second) {
rule_performance += get_rule_performance(rule_item);
}
for(const auto& rule_item : rule_set_comparison_pair.second) {
rule_comparison_performance += get_rule_performance(rule_item);
}
if(rule_performance >= rule_comparison_performance) {
rule_set_pair.first = (rule_set_pair.first < rule_set_comparison_pair.first) ? rule_set_pair.first : rule_set_comparison_pair.first;
if(rule_set_is_good) {
bad_rule_objects.emplace_back(rule_set_comparison_pair);
} else {
// Set rule_set_pair line number to be the lowest of the two since they're identical outputs.
good_rule_objects.emplace_back(rule_set_pair);
bad_rule_objects.emplace_back(rule_set_comparison_pair);
}
if(optimize_debug && !optimize_debug_file.empty()) {
std::string debug_file_output_buffer;
if(!rule_set_is_good) debug_file_output_buffer = "Kept:\t\t";
if(rule_set_is_good) debug_file_output_buffer = "Exists:\t\t";
for (int j = 0; j < rule_set_pair.second.size(); j++) {
debug_file_output_buffer.append(rule_set_pair.second[j].print(2));
if (j != rule_set_pair.second.size() - 1) debug_file_output_buffer += '\t';
}
debug_file_output_buffer += "\nDeleted:\t";
for (int j = 0; j < rule_set_comparison_pair.second.size(); j++) {
debug_file_output_buffer.append(rule_set_comparison_pair.second[j].print(2));
if (j != rule_set_comparison_pair.second.size() - 1) debug_file_output_buffer += '\t';
}
debug_file_output_buffer += '\n';
debug_file_log(optimize_debug_file, debug_file_output_buffer);
}
else if(optimize_debug) {
if(!rule_set_is_good) std::cerr << "Kept:\t\t";
if(rule_set_is_good) std::cerr << "Exists:\t\t";
for (int j = 0; j < rule_set_pair.second.size(); j++) {
rule_set_pair.second[j].print(1);
if (j != rule_set_pair.second.size() - 1) std::cerr << '\t';
}
std::cerr << std::endl;
std::cerr << "Deleted:\t";
for (int j = 0; j < rule_set_comparison_pair.second.size(); j++) {
rule_set_comparison_pair.second[j].print(1);
if (j != rule_set_comparison_pair.second.size() - 1) std::cerr << '\t';
}
std::cerr << std::endl;
}
} else {
rule_set_comparison_pair.first = (rule_set_comparison_pair.first < rule_set_pair.first) ? rule_set_comparison_pair.first : rule_set_pair.first;
if(rule_set_is_good) {
// Set rule_set_comparison_pair line number to be the lowest of the two since they're identical outputs.
good_rule_objects.erase(std::remove(good_rule_objects.begin(), good_rule_objects.end(), rule_set_pair), good_rule_objects.end());
good_rule_objects.emplace_back(rule_set_comparison_pair);
bad_rule_objects.emplace_back(rule_set_pair);
} else {
good_rule_objects.emplace_back(rule_set_comparison_pair);
bad_rule_objects.emplace_back(rule_set_pair);
}
if(optimize_debug && !optimize_debug_file.empty()) {
std::string debug_file_output_buffer = "Kept:\t\t";
for (int j = 0; j < rule_set_comparison_pair.second.size(); j++) {
debug_file_output_buffer.append(rule_set_comparison_pair.second[j].print(2));
if (j != rule_set_comparison_pair.second.size() - 1) debug_file_output_buffer += '\t';
}
debug_file_output_buffer += "\nDeleted:\t";
for (int j = 0; j < rule_set_pair.second.size(); j++) {
debug_file_output_buffer.append(rule_set_pair.second[j].print(2));
if (j != rule_set_pair.second.size() - 1) debug_file_output_buffer += '\t';
}
debug_file_output_buffer += '\n';
debug_file_log(optimize_debug_file, debug_file_output_buffer);
}
else if(optimize_debug) {
std::cerr << "Kept:\t\t";
for (int j = 0; j < rule_set_comparison_pair.second.size(); j++) {
rule_set_comparison_pair.second[j].print(1);
if (j != rule_set_comparison_pair.second.size() - 1) std::cerr << '\t';
}
std::cerr << std::endl;
std::cerr << "Deleted:\t";
for (int j = 0; j < rule_set_pair.second.size(); j++) {
rule_set_pair.second[j].print(1);
if (j != rule_set_pair.second.size() - 1) std::cerr << '\t';
}
std::cerr << std::endl;
}
}
good_lock.unlock();
continue;
}
}
}
// default behaviour if nothing matches and it's unique
if (matches_none) {
std::unique_lock<std::mutex> new_lock(result_rule_mutex); // Lock
good_rule_objects.emplace_back(rule_set_pair);
new_lock.unlock();
}
}
}
}
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) {
// todo possible rewrite to check feasibility of file memory
while(!rule_queue_stage_3.empty() || is_processing) {
std::unique_lock<std::mutex> lock(lock_obj);
condition_var.wait(lock, [&] {
return !(rule_queue_stage_3.empty() && is_processing);
});
if (rule_queue_stage_3.empty()) {
lock.unlock();
continue;
}
std::vector<long long> buffer = rule_queue_stage_3.front();
rule_queue_stage_3.pop();
lock.unlock();
for (long long rule_iterator: buffer) {
std::pair<unsigned long, std::vector<Rule>> &rule_set_pair = all_rules[rule_iterator];
if(all_rules.size() < 2) { // Skip if too small
std::unique_lock<std::mutex> new_lock(result_rule_mutex); // Lock
good_rule_objects.emplace_back(rule_set_pair);
new_lock.unlock();
continue;
}
std::vector<std::string> rule_set_output;
rule_set_output.reserve(test_words.size());
for (const std::string &test_word: test_words) {
std::string new_plain{test_word};
for (Rule &rule_item: rule_set_pair.second) {
rule_item.process(new_plain);
}
if (test_word != new_plain && !new_plain.empty()) {
rule_set_output.push_back(std::move(new_plain));
}
}
// Get rule set output from every other rule set
bool matches_none = true;
if(!all_compare_rules.empty()) { // if comparing to another rule-set
for (size_t i = 0; i < all_compare_rules.size(); i++) {
std::vector<std::string> compare_rule_set_output;
compare_rule_set_output.reserve(test_words.size());
std::vector<Rule> &rule_set_comparison = all_compare_rules[i].second;
// Get the computed words
for (const std::string &test_word: test_words) {
std::string new_plain{test_word};
for (Rule &rule_item: rule_set_comparison) {
rule_item.process(new_plain);
}
if (test_word != new_plain && !new_plain.empty()) {
compare_rule_set_output.push_back(std::move(new_plain));
}
}
// Compare output from ruleset with comparison ruleset and if matches, do not save rule (i.e. delete it)
if (rule_set_output == compare_rule_set_output) {
// if good_rule_objects contains rule_set -> skip
matches_none = false;
duplicates_removed_level_3_compare++;
std::unique_lock<std::mutex> new_lock(result_rule_mutex); // Lock
if(optimize_debug && !optimize_debug_file.empty()) {
std::string debug_file_output_buffer = "Deleted:\t\t";
for (int j = 0; j < all_rules[i].second.size(); j++) {
debug_file_output_buffer.append(all_rules[i].second[j].print(2));
if (j != all_rules[i].second.size() - 1) debug_file_output_buffer += '\t';
}
debug_file_output_buffer += '\n';
debug_file_log(optimize_debug_file, debug_file_output_buffer);
}
else if(optimize_debug) {
std::cerr << "Deleted:\t";
for(int j = 0; j < all_rules[i].second.size(); j++) {
all_rules[i].second[j].print(1);
if(j != all_rules[i].second.size()-1) std::cerr << '\t';
}
std::cerr << std::endl;
}
bad_rule_objects.emplace_back(rule_set_pair);
new_lock.unlock();
}
}
if(!optimize_similar_op) { // skip if not intending to optimize the main file too.
if (matches_none) {
std::unique_lock<std::mutex> good_lock(result_rule_mutex);
good_rule_objects.emplace_back(rule_set_pair);
good_lock.unlock();
}
continue;
}
}
// Compare rules to itself (O(x^2))
for (size_t i = 0; i < all_rules.size(); i++) {
std::unique_lock<std::mutex> good_lock(result_rule_mutex);
std::pair<unsigned long, std::vector<Rule>> &rule_set_comparison_pair = all_rules[i];
if (std::find(bad_rule_objects.begin(), bad_rule_objects.end(), rule_set_pair) != bad_rule_objects.end()) { // if original rule is bad, a good one already exists -> skip
good_lock.unlock();
continue;
}
if (std::find(bad_rule_objects.begin(), bad_rule_objects.end(), rule_set_comparison_pair) != bad_rule_objects.end()) { // if comparison is bad, a good one already exists -> skip
good_lock.unlock();
continue;
}
if (std::find(good_rule_objects.begin(), good_rule_objects.end(), rule_set_comparison_pair) != good_rule_objects.end()) { // if comparison is good -> skip. We only need to logically go through one condition
good_lock.unlock();
continue;
}
std::vector<std::string> compare_rule_set_output;
compare_rule_set_output.reserve(test_words.size());
for (const std::string &test_word: test_words) {
std::string new_plain{test_word};
for (Rule &rule_item: rule_set_comparison_pair.second) {
rule_item.process(new_plain);
}
if (test_word != new_plain && !new_plain.empty()) {
compare_rule_set_output.push_back(std::move(new_plain));
}
}
// Compare output from ruleset with comparison ruleset
if (rule_set_output == compare_rule_set_output && i != rule_iterator) {
// if the rule output is the same, but the rule is different
matches_none = false;
bool rule_set_is_good = false;
// if rule_set_pair is in good_rule_objects or bad_rule_objects -> skip
if (std::find(good_rule_objects.begin(), good_rule_objects.end(), rule_set_pair) != good_rule_objects.end()) { // if original rule is good, investigate. Comparison might need to be replaced or removed.
rule_set_is_good = true;
}
duplicates_removed_level_3++;
// save the smaller rule
if(rule_set_pair.second.size() < rule_set_comparison_pair.second.size()) { // if one rule is smaller than the other, take it
rule_set_pair.first = (rule_set_pair.first < rule_set_comparison_pair.first) ? rule_set_pair.first : rule_set_comparison_pair.first;
if (rule_set_is_good) { // if the rule_set is not already part of the good_rules, add it
bad_rule_objects.emplace_back(rule_set_comparison_pair);
} else {
good_rule_objects.emplace_back(rule_set_pair);
bad_rule_objects.emplace_back(rule_set_comparison_pair);
}
good_lock.unlock();
if(optimize_debug && !optimize_debug_file.empty()) {
std::string debug_file_output_buffer;
if(!rule_set_is_good) debug_file_output_buffer = "Kept:\t\t";
if(rule_set_is_good) debug_file_output_buffer = "Exists:\t\t";
for (int j = 0; j < rule_set_pair.second.size(); j++) {
debug_file_output_buffer.append(rule_set_pair.second[j].print(2));
if (j != rule_set_pair.second.size() - 1) debug_file_output_buffer += '\t';
}
debug_file_output_buffer += "\nDeleted:\t";
for (int j = 0; j < all_rules[i].second.size(); j++) {
debug_file_output_buffer.append(all_rules[i].second[j].print(2));
if (j != all_rules[i].second.size() - 1) debug_file_output_buffer += '\t';
}
debug_file_output_buffer += '\n';
debug_file_log(optimize_debug_file, debug_file_output_buffer);
}
else if(optimize_debug) {
if(!rule_set_is_good) std::cerr << "Kept:\t\t";
if(rule_set_is_good) std::cerr << "Exists:\t";
for (int j = 0; j < rule_set_pair.second.size(); j++) {
rule_set_pair.second[j].print(1);
if (j != rule_set_pair.second.size() - 1) std::cerr << '\t';
}
std::cerr << std::endl;
std::cerr << "Deleted:\t";
for(int j = 0; j < all_rules[i].second.size(); j++) {
all_rules[i].second[j].print(1);
if(j != all_rules[i].second.size()-1) std::cerr << '\t';
}
std::cerr << std::endl;
// }
}
continue;
}
// Don't save the larger rule
if(rule_set_pair.second.size() > rule_set_comparison_pair.second.size()) {
rule_set_comparison_pair.first = (rule_set_comparison_pair.first < rule_set_pair.first) ? rule_set_comparison_pair.first : rule_set_pair.first;
if (rule_set_is_good) { // rule set is already good
// Remove rule_set_pair from the list.
good_rule_objects.erase(std::remove(good_rule_objects.begin(), good_rule_objects.end(), rule_set_pair), good_rule_objects.end());
// Add comparison pair to good objects instead
good_rule_objects.emplace_back(rule_set_comparison_pair);
bad_rule_objects.emplace_back(rule_set_pair);
} else { // rule set is not good
good_rule_objects.emplace_back(rule_set_comparison_pair);
bad_rule_objects.emplace_back(rule_set_pair);
}
good_lock.unlock();
if(optimize_debug && !optimize_debug_file.empty()) {
std::string debug_file_output_buffer;
if(rule_set_is_good) { // rule set is already good
debug_file_output_buffer = "Exists:\t";
for (int j = 0; j < rule_set_pair.second.size(); j++) {
debug_file_output_buffer.append(rule_set_pair.second[j].print(2));
if (j != rule_set_pair.second.size() - 1) debug_file_output_buffer += '\t';
}
debug_file_output_buffer += "\tDeleted:\t";
for (int j = 0; j < rule_set_comparison_pair.second.size(); j++) {
debug_file_output_buffer.append(rule_set_comparison_pair.second[j].print(2));
if (j != rule_set_comparison_pair.second.size() - 1) debug_file_output_buffer += '\t';
}
debug_file_output_buffer += '\n';
debug_file_log(optimize_debug_file, debug_file_output_buffer);
} else {
debug_file_output_buffer = "Kept:\t";
for (int j = 0; j < rule_set_comparison_pair.second.size(); j++) {
debug_file_output_buffer.append(rule_set_comparison_pair.second[j].print(2));
if (j != rule_set_comparison_pair.second.size() - 1) debug_file_output_buffer += '\t';
}
debug_file_output_buffer += "\nDeleted:\t";
for (int j = 0; j < rule_set_pair.second.size(); j++) {
debug_file_output_buffer.append(rule_set_pair.second[j].print(2));
if (j != rule_set_pair.second.size() - 1) debug_file_output_buffer += '\t';
}
debug_file_output_buffer += '\n';
}
}
else if(optimize_debug) {
if(rule_set_is_good) { // rule set is already good
std::cerr << "Exists:\t";
for (int j = 0; j < rule_set_pair.second.size(); j++) {
rule_set_pair.second[j].print(1);
if (j != rule_set_pair.second.size() - 1) std::cerr << '\t';
}
std::cerr << std::endl;
std::cerr << "Deleted:\t";
for (int j = 0; j < rule_set_comparison_pair.second.size(); j++) {
rule_set_comparison_pair.second[j].print(1);
if (j != rule_set_comparison_pair.second.size() - 1) std::cerr << '\t';
}
std::cerr << std::endl;
} else {
std::cerr << "Kept:\t";