-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrulify.cpp
1444 lines (1310 loc) · 50.3 KB
/
rulify.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
/*
Heavy speed focused optimizations will be made to this rulify including
a/ Removal of incompatible hashcat/jtr/isp rules
b/ Early escape detection in rules
c/ Attempt to remove/minimize all calls to string functions eg strlen, strcpy instead use memory based functions
d/ Optimize len tracking through strict len tracking within rules to prevent unnecessary strlen calls
e/ Optimize fast skipping by jumping the pointer along the rule instead of moving along each rule call
f/ Since no strlen calls will be invoked the NULL terminator will be applied at the final step as we don't need it
*/
#define MAXLINE '100'
#include <stdio.h>
#include <string.h>
#include <stdlib.h> //Random
#include <ctype.h> //To lower
#include "rulify.h"
//Rules ripped from hashcat/ppro/jtr
#define RULE_OP_MANGLE_NOOP ':' // does nothing
#define RULE_OP_MANGLE_LREST 'l' // lower case all chars
#define RULE_OP_MANGLE_UREST 'u' // upper case all chars
#define RULE_OP_MANGLE_LREST_UFIRST 'c' // lower case all chars, upper case 1st
#define RULE_OP_MANGLE_UREST_LFIRST 'C' // upper case all chars, lower case 1st
#define RULE_OP_MANGLE_TREST 't' // switch the case of each char
#define RULE_OP_MANGLE_TOGGLE_AT 'T' // switch the case of each char on pos N
#define RULE_OP_MANGLE_REVERSE 'r' // reverse word
#define RULE_OP_MANGLE_DUPEWORD 'd' // append word to itself
#define RULE_OP_MANGLE_DUPEWORD_TIMES 'p' // append word to itself N times
#define RULE_OP_MANGLE_REFLECT 'f' // reflect word (append reversed word)
#define RULE_OP_MANGLE_ROTATE_LEFT '{' // rotate the word left. ex: hello -> elloh
#define RULE_OP_MANGLE_ROTATE_RIGHT '}' // rotate the word right. ex: hello -> ohell
#define RULE_OP_MANGLE_APPEND '$' // append char X
#define RULE_OP_MANGLE_PREPEND '^' // prepend char X
#define RULE_OP_MANGLE_DELETE_FIRST '[' // delete first char of word
#define RULE_OP_MANGLE_DELETE_LAST ']' // delete last char of word
#define RULE_OP_MANGLE_DELETE_AT 'D' // delete char of word at pos N
#define RULE_OP_MANGLE_EXTRACT 'x' // extract X chars of word at pos N
#define RULE_OP_MANGLE_OMIT 'O' // OMIT X chars of word at pos N
#define RULE_OP_MANGLE_INSERT 'i' // insert char X at pos N
#define RULE_OP_MANGLE_OVERSTRIKE 'o' // overwrite with char X at pos N
#define RULE_OP_MANGLE_TRUNCATE_AT '\''// cut the word at pos N
#define RULE_OP_MANGLE_REPLACE 's' // replace all chars X with char Y
#define RULE_OP_MANGLE_PURGECHAR '@' // purge all instances of char X
#define RULE_OP_MANGLE_DUPECHAR_FIRST 'z' // prepend first char of word to itself N times. ex: hello -> hhello
#define RULE_OP_MANGLE_DUPECHAR_LAST 'Z' // append last char of word to itself N times. ex: hello -> helloo
#define RULE_OP_MANGLE_DUPECHAR_ALL 'q' // duplicate all chars. ex: hello -> hheelllloo
#define RULE_OP_MANGLE_EXTRACT_MEMORY 'X' // insert substring delimited by N and M into current word at position I
#define RULE_OP_MANGLE_APPEND_MEMORY '4' // insert the word saved by 'M' at the end of current word
#define RULE_OP_MANGLE_PREPEND_MEMORY '6' // insert the word saved by 'M' at the beginning of current word
#define RULE_OP_MEMORIZE 'M' // memorize the current word
//End standard rules
//Additional rules not found in ppro/hm/hashcat/jtr
#define RULE_OP_REPLACE_SINGLE_LEFT 'S' // replace a single instance of X with Y from the left SXY
#define RULE_OP_REPLACE_SINGLE_RIGHT 'W' // replace a single instance of X with Y from the right RXY
//End additional rules
//Extra memory functions
#define RULE_MEM_TOGGLE '0' // Toggle memory mode (rules will be applied to memory, make sure it is untoggled)
#define RULE_MEM_CUT_BLOCK 'v' // move a block from pos X to Y into memory
#define RULE_MEM_COPY_BLOCK 'm' // copy a block from pos X to Y into memory (can use X mode instead)
#define RULE_MEM_INSERT 'I' // Inserts memory into line at pos X (can use X mode instead)
#define RULE_MEM_OVERWRITE 'P' // Overwrites line with memory at pos X
//End extra memory functions
//Extra Rules from hashcat
#define RULE_OP_SWAPFRONT 'k' //Swap first two characters
#define RULE_OP_SWAPBACK 'K' //Swap last two characters
#define RULE_OP_SWAPCHARS '*' //Swaps character X with Y
#define RULE_OP_BITWISEL 'L' //Bitwise rotate left
#define RULE_OP_BITWISER 'R' //Birwise rotate right
#define RULE_OP_CLONEFORWARD '.' //Replaces character @ N with value @ N plus 1
#define RULE_OP_CLONEBACKWARD ',' //Replaces character @ N with value @ N minus 1
#define RULE_OP_ASCIIUP '+' //Increment character @ N by 1 ascii value
#define RULE_OP_ASCIIDOWN '-' //Decrement character @ N by 1 ascii value
#define RULE_OP_CLONEBLOCKF 'y' //Duplicates first block of N characters
#define RULE_OP_CLONEBLOCKR 'Y' //Duplicates last block N characters
#define RULE_OP_TITLE 'E' //Lower case the whole line, then upper case the first letter and every letter after a space
//End Extra Rules
//Hashcat Rejection rules (can be used with logic IF)
#define RULE_GATE_LESS '<' //Reject plains of length greater than N
#define RULE_GATE_GREATER '>' //Reject plains of length less than N
#define RULE_GATE_CONTAIN '!' //Reject plains which contain char X
#define RULE_GATE_NOT_CONTAIN '/' //Reject plains which do not contain char X
#define RULE_GATE_FIRSTCHAR '(' //Start with char (X thi
#define RULE_GATE_LASTCHAR ')' //Ends with char )X
#define RULE_GATE_EQUALSCHAR_AT '=' //Reject plains which do not have char X at position N
#define RULE_GATE_CONTAINS_NUM '%' //Reject plains which do not have char X at position N
#define RULE_GATE_MEM_CONTAINS 'Q' //Reject plains where the memory saved matches current word
char mapstring[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; //Used to map the chars to positions
char singleR[] = "LR:lucCtrdf[]{}qM46Q~E0\""; //Rules with single char
char DoubleR[] = "Tp$^DzZ@\\<>!/()IO"; //Rules with double char
char TripleR[] = "ios=mvSW"; //Rules with three chars
char QuadR[] = "XF"; //Rules with quadruple char
int RuleJump[100];
int LongJump = 0;
//Logical operators
char logicOPs[] = "<>!/()=Q";
int isLogical[127] = {0};
int RuleOPs[127] = {0};
char toggleMap[BUFSIZ];
int charMap[256]; //Holds the ASCII rep of luds for those sets
//Used to generate the correct positional maps to map the chars into positions
int posMap[127];
//Self explanatory
char numbers[] = "0123456789";
char symbols[] = "!@#$%^&*()-_+=~`[]{}|\\:;\"'<>,.?/ ]";
char lower[] = "abcdefghijklmnopqrstuvwxyz";
char upper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char all[] = "!@#$%^&*()-_+=~`[]{}|\\:;\"'<>,.?/ ]0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char uspecial[] = "ÀÁÂÄÃÅÆÇÐÈÉÊËÌÍÎÏÑÒÓÔÖÕØŒŠÙÛÚÜÝŸŽÞ";
char lspecial[] = "àáâäãåæçÐèéêëìíîïñòóôöõøœšùûúüýÿžþ";
int randomize(int min, int max){
return min + rand() / (RAND_MAX / (max - min + 1) + 1);
}
int remSpace(char * buffer, int pos)
{
if (buffer[pos+1] == 32)
{
strcpy(buffer+pos+1,buffer+pos+2);
return 1;
}
return 0;
}
//Routine which gets the pointer to the read rule
int validateRule(char * rule_buff)
{
int rule_len = strlen(rule_buff); //Holds the length (chars) of the rule
int u = 0; //Variable we use to process loops
int skip = 0; //Denotes whether a skip is needing as some rules contain more than 1 character (2,3,4)
int validMap[BUFSIZ]; //Holds the mapping of position to RulePos
int mem_mode = 0;
int rand_mode = 0;
//Initialize the map (position to > RulePos [0-Z]) to zero
for (u = 0; u< BUFSIZ; u++)
{
validMap[u] = 0;
}
//Flag the positions which we know will be valid to one [0-Z] 62 positions
for (u = 0; u< sizeof(mapstring); u++)
{
validMap[(int)mapstring[u]] = 1;
}
//Start looping through the characters in the rule and process
for (u = 0; u<rule_len; u++)
{
if (skip !=0 )
{
skip--; //If a skip is issues, simply decrease the skip counter and do not process (this is needed since some rules contain more than a single char)
continue;
}
if (rule_buff[u] == ' ') //The rule processor ignores space characters (treats them as blank so just skip them)
{
memmove(rule_buff+u,rule_buff+u+1,rule_len-u);
rule_len--;
u--;
continue;
}
if (rule_buff[u] == 34) //The rule processor ignores space characters (treats them as blank so just skip them)
{
continue;
}
if (rule_buff[u] == '`') //The rule processor ignores space characters (treats them as blank so just skip them)
{
continue;
}
if (rule_buff[u] == ';') //The rule processor ignores space characters (treats them as blank so just skip them)
{
if (rand_mode !=1)
{
return 0;
}
rand_mode = !rand_mode;
continue;
}
else if ( rule_buff[u] == RULE_OP_MANGLE_EXTRACT_MEMORY)
{
skip = 3;
if (rule_len-(u+1) < 3)
{
return 0;
}
int val = rule_buff[u+1] - '0';
if (val == 0 || validMap[rule_buff[u+1]] == 0)
{
return 0;
}
if (validMap[rule_buff[u+2]] == 0 || validMap[rule_buff[u+3]] == 0)
{
return 0;
}
}
else if ( rule_buff[u] == RULE_MEM_COPY_BLOCK || rule_buff[u] == RULE_MEM_CUT_BLOCK)
{
skip = 2;
if (rule_len-(u+1) < 2)
{
return 0;
}
if (validMap[rule_buff[u+1]] == 0)
{
return 0;
}
int val = rule_buff[u+2] - '0';
if (val == 0 || validMap[rule_buff[u+2]] == 0)
{
return 0;
}
}
else if ( rule_buff[u] == RULE_OP_MANGLE_EXTRACT)
{
skip = 2;
if (rule_len-(u+1) < 2)
{
return 0;
}
if (validMap[rule_buff[u+1]] == 0)
{
return 0;
}
int val = rule_buff[u+2] - '0';
if (val == 0 || validMap[rule_buff[u+2]] == 0)
{
return 0;
}
}
else if (rule_buff[u] == RULE_MEM_INSERT || rule_buff[u] == RULE_MEM_OVERWRITE || rule_buff[u] == RULE_OP_MANGLE_TOGGLE_AT || rule_buff[u] == RULE_OP_MANGLE_DELETE_AT)
{
skip = 1;
if (rule_len-(u+1) < 1)
{
return 0;
}
if (validMap[rule_buff[u+1]] == 0)
{
return 0;
}
}
else if (rule_buff[u] == RULE_OP_MANGLE_DUPEWORD_TIMES
|| rule_buff[u] == RULE_OP_MANGLE_TRUNCATE_AT || rule_buff[u] == RULE_OP_CLONEBACKWARD || rule_buff[u] == RULE_OP_CLONEFORWARD
|| rule_buff[u] == RULE_OP_ASCIIUP || rule_buff[u] == RULE_OP_ASCIIDOWN || rule_buff[u] == RULE_OP_CLONEBLOCKF || rule_buff[u] == RULE_OP_CLONEBLOCKR
|| rule_buff[u] == RULE_GATE_LESS || rule_buff[u] == RULE_GATE_GREATER || rule_buff[u] == RULE_OP_BITWISEL|| rule_buff[u] == RULE_OP_BITWISER)
{
skip = 1;
if (rule_len-(u+1) < 1)
{
return 0;
}
if (validMap[rule_buff[u+1]] == 0)
{
return 0;
}
if (rule_buff[u] == RULE_OP_CLONEBACKWARD) //Cannot be 0 since we can't go backwards
{
if (rule_buff[u+1] == 0)
{
return 0;
}
}
}
else if( rule_buff[u] == RULE_OP_MANGLE_INSERT || rule_buff[u] == RULE_OP_MANGLE_OVERSTRIKE || rule_buff[u] == RULE_GATE_EQUALSCHAR_AT )
{
skip = 2;
if (rule_len-(u+1) < 2)
{
return 0;
}
if (validMap[rule_buff[u+1]] == 0)
{
return 0;
}
}
else if (rule_buff[u] == RULE_OP_MANGLE_DUPECHAR_FIRST || rule_buff[u] == RULE_OP_MANGLE_DUPECHAR_LAST)
{
skip = 1;
if (rule_len-(u+1) < 1)
{
return 0;
}
int val = rule_buff[u+1] - '0';
if (val == 0 || validMap[rule_buff[u+1]] == 0)
{
return 0;
}
}
else if (rule_buff[u] == RULE_OP_MANGLE_REPLACE || rule_buff[u] == RULE_OP_REPLACE_SINGLE_LEFT || rule_buff[u] == RULE_OP_REPLACE_SINGLE_RIGHT)
{
skip = 2;
if (rule_len-(u+1) < 2)
{
return 0;
}
if (rule_buff[u+1] - rule_buff[u+2]==0)
{
return 0;
}
}
else if (rule_buff[u] == RULE_OP_MANGLE_APPEND || rule_buff[u] == RULE_OP_MANGLE_PREPEND || rule_buff[u] == RULE_OP_MANGLE_PURGECHAR
|| rule_buff[u] == RULE_GATE_FIRSTCHAR || rule_buff[u] == RULE_GATE_LASTCHAR || rule_buff[u] == RULE_GATE_CONTAIN
|| rule_buff[u] == RULE_GATE_NOT_CONTAIN)
{
skip = 1;
if (rule_len-(u+1) < 1)
{
return 0;
}
}
else if (rule_buff[u] == RULE_OP_SWAPCHARS)
{
skip = 2;
if (rule_len-(u+1) < 2)
{
return 0;
}
if (validMap[rule_buff[u+1]] == 0)
{
return 0;
}
if (validMap[rule_buff[u+2]] == 0)
{
return 0;
}
}
else if (rule_buff[u] == RULE_OP_MANGLE_NOOP || rule_buff[u] == RULE_OP_MANGLE_LREST || rule_buff[u] == RULE_OP_MANGLE_UREST
|| rule_buff[u] == RULE_OP_MANGLE_LREST_UFIRST || rule_buff[u] == RULE_OP_MANGLE_UREST_LFIRST
||rule_buff[u] == RULE_OP_MANGLE_TREST || rule_buff[u] == RULE_OP_MANGLE_REVERSE
|| rule_buff[u] == RULE_OP_MANGLE_DUPEWORD || rule_buff[u] == RULE_OP_MANGLE_REFLECT
|| rule_buff[u] == RULE_OP_MANGLE_ROTATE_LEFT || rule_buff[u] == RULE_OP_MANGLE_ROTATE_RIGHT
|| rule_buff[u] == RULE_OP_MANGLE_DELETE_FIRST || rule_buff[u] == RULE_OP_MANGLE_DELETE_LAST
|| rule_buff[u] == RULE_OP_MANGLE_EXTRACT_MEMORY || rule_buff[u] == RULE_OP_MANGLE_APPEND_MEMORY
|| rule_buff[u] == RULE_OP_MANGLE_PREPEND_MEMORY || rule_buff[u] == RULE_OP_MEMORIZE
|| rule_buff[u] == RULE_OP_SWAPFRONT || rule_buff[u] == RULE_OP_SWAPBACK || rule_buff[u] == RULE_GATE_MEM_CONTAINS
|| rule_buff[u] == RULE_OP_TITLE || rule_buff[u] == RULE_OP_MANGLE_DUPECHAR_ALL )
{}
else if ( rule_buff[u] == RULE_MEM_TOGGLE)
{
mem_mode = !mem_mode;
}
else //If nothing matched then fail the verification
{
return 0;
}
}
if (mem_mode == 1 || rand_mode == 1) //Ensure the user actually untoggled the mem_editor or closed the random function
{
return 0;
}
return 1;
}
void initMaps()
{
int i = 0;
//Map the logical operators
for (i = 0; i<sizeof(logicOPs); i++)
{
isLogical[logicOPs[i]] = 1; //Mark the logical Operators
}
for (i = 0; i<sizeof(singleR); i++)
{
RuleOPs[singleR[i]] = 1; //Single
}
for (i = 0; i<sizeof(DoubleR); i++)
{
RuleOPs[DoubleR[i]] = 2; //Double
}
for (i = 0; i<sizeof(TripleR); i++)
{
RuleOPs[TripleR[i]] = 3; //Triple
}
for (i = 0; i<sizeof(QuadR); i++)
{
RuleOPs[QuadR[i]] = 4; //Quad
}
//End Mapping Operations
for (i = 0; i<62; i++)
{
posMap[mapstring[i]] = i;
}
for (i = 0; i<sizeof(lower)-1; i++)
{
charMap[lower[i]] = 108;
}
for (i = 0; i<sizeof(upper)-1; i++)
{
charMap[upper[i]] = 117;
}
for (i = 0; i<sizeof(numbers)-1; i++)
{
charMap[numbers[i]] = 100;
}
for (i = 0; i<sizeof(symbols)-1; i++)
{
charMap[symbols[i]] = 115;
}
//Initialize the full map
for (i = 0; i<BUFSIZ;i++)
{
toggleMap[i] = i;
}
//Map the reverse toggles
for (i = 0; i< sizeof(lower) ;i++)
{
toggleMap[lower[i]] = upper[i];
toggleMap[upper[i]] = lower[i];
}
for (i = 0; i<sizeof(uspecial); i++)
{
toggleMap[uspecial[i]] = lspecial[i];
toggleMap[lspecial[i]] = uspecial[i];
}
}
int skipCalc(char** RuleMap, int ruleNum, int offset)
{
int calc = 0;
while (1)
{
if (RuleMap[ruleNum][offset]==34) break;
calc+= RuleOPs[RuleMap[ruleNum][offset]];
offset += RuleOPs[RuleMap[ruleNum][offset]];
}
return calc;
}
int markRules(char** RuleMap, int ruleNum, int offset)
{
int qt_counter = 0;
int qt_flag = 0;
int initial_offset = offset; //Holds the value we started at so we can calculate the actual offset rather than the position
while (1)
{
if (RuleMap[ruleNum][offset]==59)
{
LongJump = offset;
break;
}
if (RuleMap[ruleNum][offset]==34)
{
qt_flag = !qt_flag;
if (qt_flag)
{
qt_counter ++;
RuleJump[qt_counter] = (offset-initial_offset)+2;
}
else
{
offset ++;
continue;
}
}
offset += RuleOPs[RuleMap[ruleNum][offset]];
}
return qt_counter;
}
int RunRule(rule_struct * ruleProp, char * inString, size_t inStringLen, char * outString, unsigned long ruleNum)
{
char rule_temp[BUFSIZ]; //Used as a temporary storage for some functions
char rule_mem[BUFSIZ]; //Used to memorize the line
char line_toggle[BUFSIZ]; //Backup for memory toggling
int skipRule = 0;
long line_len = 0;
long mem_len =0;
int mem_mode = 0;;
skipRule = 0;
unsigned long i = 0;
int x , k;
{
memcpy(outString,inString,inStringLen);
line_len = inStringLen;
i =0 ;
while(i< ruleProp[0].RuleLen[ruleNum])//Optimizaion here, we can pre-cache strlen
{
if (line_len == 0)
break;
if (skipRule == 1)
break;
switch (ruleProp[0].RuleMap[ruleNum][i])
{
case RULE_OP_MANGLE_APPEND:
{
while(ruleProp[0].RuleMap[ruleNum][i] == RULE_OP_MANGLE_APPEND)
{
outString[line_len] = (ruleProp[0].RuleMap[ruleNum][i+1]);
line_len ++;
i +=2;
}
break;
}
case RULE_OP_MANGLE_TOGGLE_AT:
{
if (posMap[ruleProp[0].RuleMap[ruleNum][i+1]] > line_len)
{ i+=2;
continue;}
int pos = 0;
pos = posMap[ruleProp[0].RuleMap[ruleNum][i+1]];
outString[pos] = toggleMap[outString[pos]];
i +=2;
break;
}
case RULE_MEM_COPY_BLOCK:
{
int ilen = 0;
int start = 0;
start = (ruleProp[0].RuleMap[ruleNum][i+1]-'0');
ilen = (ruleProp[0].RuleMap[ruleNum][i+2]-'0');
if (start+ilen > line_len) {
i+=3;
continue;}
memcpy(rule_mem,outString+start,ilen);
mem_len = ilen;
i +=3;
break;
}
case RULE_MEM_CUT_BLOCK:
{
int ilen = 0;
int start = 0;
start = (ruleProp[0].RuleMap[ruleNum][i+1]-'0');
ilen = (ruleProp[0].RuleMap[ruleNum][i+2]-'0');
if (start+ilen > line_len) { i+=3;
continue;}
memcpy(rule_mem,outString+start,ilen);
mem_len = ilen;
memcpy(outString+start,outString+start+ilen,line_len-start-ilen);
line_len -= ilen;
i +=3;
break;
}
case RULE_MEM_INSERT://optimizae here please
{
if (posMap[ruleProp[0].RuleMap[ruleNum][i+1]] > line_len)
{ i+=2;
continue;}
int value = 0;
value = posMap[ruleProp[0].RuleMap[ruleNum][i+1]];
memmove(outString+value+mem_len,outString+value,line_len-value);
memcpy(outString+value,rule_mem,mem_len);
line_len +=mem_len;
i+=2;
break;
}
case RULE_MEM_OVERWRITE:
{
if ( posMap[ruleProp[0].RuleMap[ruleNum][i+1]] > line_len)
{ i+=2;
continue;}
int value= 0;
value = posMap[ruleProp[0].RuleMap[ruleNum][i+1]];
memcpy(outString+value,rule_mem,mem_len);
line_len += value;
i +=2;
break;
}
case RULE_OP_MANGLE_TREST:
{
int b = 0;
for (b= 0; b<line_len; b++)
{
outString[b] = toggleMap[outString[b] ];
}
i++;
break;
}
case RULE_OP_MANGLE_REPLACE:
{
char match = ruleProp[0].RuleMap[ruleNum][i+1];
char write = ruleProp[0].RuleMap[ruleNum][i+2];
int b = 0;
for (b = 0;b<line_len; b++)
{
if (outString[b] == match)
{
outString[b] = write;
}
}
i +=3;
break;
}
case RULE_OP_REPLACE_SINGLE_LEFT:
{
char match = 0;
char write = 0;
match = ruleProp[0].RuleMap[ruleNum][i+1];
write = ruleProp[0].RuleMap[ruleNum][i+2];
int b = 0;
for (b = 0;b<line_len; b++)
{
if (outString[b] == match)
{
outString[b] = write;
break;
}
}
i+=3;
break;
}
case RULE_OP_REPLACE_SINGLE_RIGHT:
{
char match = 0;
char write = 0;
match = ruleProp[0].RuleMap[ruleNum][i+1];
write = ruleProp[0].RuleMap[ruleNum][i+2];
int b = 0;
for (b = line_len-1;b>-1; b--)
{
if (outString[b] == match)
{
outString[b] = write;
break;
}
}
i+=3;
break;
}
case RULE_OP_MANGLE_EXTRACT:
{
//Updated to reflect changes
int ilen = posMap[ruleProp[0].RuleMap[ruleNum][i+2]];
int start = posMap[ruleProp[0].RuleMap[ruleNum][i+1]];
if (start > line_len)
{
i+=3;
break;
}
if (start+ilen > line_len) //Do not use absolute, fall back to maximum
{
ilen = line_len-start;
}
memmove(outString,outString+start,ilen);
line_len = ilen;
i +=3;
break;
}
case RULE_OP_MANGLE_OMIT:
{
int ilen = posMap[ruleProp[0].RuleMap[ruleNum][i+2]];
int start = posMap[ruleProp[0].RuleMap[ruleNum][i+1]];
if (start+ilen > line_len) { i+=3;
continue;}
memmove(outString,outString+start+ilen,line_len-start-ilen);
line_len -= ilen;
i +=3;
break;
}
case RULE_OP_MANGLE_PURGECHAR:
{
//skip =1;
int b = 0;
char needle = ruleProp[0].RuleMap[ruleNum][i+1];
for (b = 0; b<line_len; b++)
{
if (outString[b] == needle)
{
memmove(outString+b,outString+b+1,line_len-1);
line_len--;
}
}
i+=2;
break;
}
case RULE_OP_MANGLE_EXTRACT_MEMORY:
{
//skip = 3;
int pos = ruleProp[0].RuleMap[ruleNum][i+3] - '0';
int ilen = ruleProp[0].RuleMap[ruleNum][i+2] - '0';
int start = ruleProp[0].RuleMap[ruleNum][i+1] - '0';
if (pos > line_len) { i+=3;
continue;}
if (start + ilen >strlen(rule_mem)) { i+=3;
continue;}
memcpy(rule_temp,outString,line_len);
memcpy(outString+pos+ilen,rule_temp+pos,line_len-pos);
memcpy(outString+pos,rule_mem+start,ilen);
line_len += ilen;
i+=4;
break;
}
case RULE_OP_MANGLE_APPEND_MEMORY:
{
memcpy(outString+line_len,rule_mem,mem_len);
line_len += mem_len;
i++;
break;
}
case RULE_OP_MANGLE_PREPEND_MEMORY:
{
memcpy(rule_temp,outString,line_len);
memcpy(outString+mem_len,rule_temp,line_len);
memcpy(outString,rule_mem,mem_len);
line_len += mem_len;
i++;
break;
}
case RULE_OP_MEMORIZE:
{
memcpy(rule_mem,outString,line_len);
mem_len = line_len;
i++;
break;
}
case RULE_OP_MANGLE_ROTATE_LEFT:
{
int b = 0;
k = 1;
while (ruleProp[0].RuleMap[ruleNum][i+1] == RULE_OP_MANGLE_ROTATE_LEFT )
{
k++; i++;
}
if (line_len ==1) //Early exit if there is 1 char to rotate
{
i++;
break;
}
if (k > line_len) //Number of rotates is greater than line_len, lets calculate the offset using remainder method
{
x = k % line_len;
}
else
{
x = k;
}
if (x == 0)
{
i++;
break;
}
for (k = 0;k<x;k++) //Move the first section to the end of the string
{
outString[line_len+k] = outString[k];
}
for (k = x;k<line_len+x;k++) //Shift everything back to starting spot
{
outString[b] = outString[k];
b++;
}
i++;
break;
}
case RULE_OP_MANGLE_ROTATE_RIGHT:
{
k = 1;
while (ruleProp[0].RuleMap[ruleNum][i+1] == RULE_OP_MANGLE_ROTATE_RIGHT )
{
k++; i++;
}
if (line_len ==1) //Early exit if there is 1 char to rotate
{
i++;
break;
}
if (k >= line_len) //Number of rotates is greater than line_len, lets calculate the offset using remainder method
{
x = k % line_len;
}
else
{
x = k;
}
if (x == 0)
{
i++;
break;
}
for (k = 0;k<line_len-x;k++) //Move what is needed to the end
{
outString[line_len+k] = outString[k];
}
for (k = 0; k<line_len; k++)
{
outString[k] = outString[line_len-x+k];
}
i++;
break;
}
case RULE_OP_MANGLE_PREPEND:
{
k = line_len-1;
while ( k !=-1)
{
outString[k+1] = outString[k];
k--;
}
outString[0]= ruleProp[0].RuleMap[ruleNum][i+1];
line_len ++;
i+=2;
break;
}
case RULE_OP_MANGLE_LREST:
{
int b = 0;
for (b = 0; b<line_len; b++)
{
outString[b] = tolower(outString[b]);
}
i++;
break;
}
case RULE_OP_MANGLE_DUPECHAR_ALL:
{
if ((line_len * 2 ) > BUFSIZ)
{
i++;
break;
}
memcpy(rule_temp,outString,line_len);
int b = 0;
int count = 0;
for (b = 0; b<line_len*2; b+=2)
{
outString[b] = rule_temp[count];
outString[b+1] = rule_temp[count];
count ++;
}
line_len = line_len *2;
i++;
break;
}
case RULE_OP_MANGLE_UREST:
{
int b = 0;
for (b = 0; b<line_len; b++)
{
if (outString[b] > '`' && outString[b] < '{')
{
outString[b] = outString[b] -0x20;
}
}
i++;
break;
}
case RULE_OP_TITLE:
{
int b = 0;
outString[0] = toupper(
outString[0]);
for (b = 1; b<line_len; b++)
{
if (outString[b-1]== 32)
{
outString[b] = toupper(outString[b]);
}
else
{
outString[b] = tolower(outString[b]);
}
}
i++;
break;
}
case RULE_OP_MANGLE_LREST_UFIRST:
{
int b = 0;
if (outString[0] > '`' && outString[0] < '{')
{
outString[0] = outString[0] -0x20;
}
for (b = 1; b<line_len; b++)
{
if (outString[b] > '@' && outString[b] < '[')
{
outString[b] = outString[b] +0x20;
}
}
i++;
break;
}
case RULE_OP_MANGLE_UREST_LFIRST:
{
int b = 0;
if (outString[0] > '@' && outString[0] < '[')
{
outString[0] = outString[0] +0x20;
}
for (b = 1; b<line_len; b++)
{
if (outString[b] > '`' && outString[b] < '{')
{
outString[b] = outString[b] -0x20;
}
}
i++;
break;
}
case RULE_OP_MANGLE_DELETE_LAST:
{
k = 1;
while (ruleProp[0].RuleMap[ruleNum][i+1] == RULE_OP_MANGLE_DELETE_LAST)
{
k++; i++;
}
if (k>= line_len)
{
line_len = 0;
break;
}
line_len -=k;
i++;
break;
}
case RULE_OP_MANGLE_DELETE_FIRST:
{
k = 1;
while (ruleProp[0].RuleMap[ruleNum][i+1] == RULE_OP_MANGLE_DELETE_FIRST )
{
k++; i++;
}