-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrules.c
1089 lines (959 loc) · 20.9 KB
/
rules.c
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
/*
* This file is part of John the Ripper password cracker,
* Copyright (c) 1996-99,2003,2005,2009,2010,2015,2016 by Solar Designer
*/
#include <stdio.h>
#include <string.h>
#include "arch.h"
#include "misc.h"
#include "params.h"
#include "common.h"
#include "memory.h"
#include "formats.h"
#include "loader.h"
#include "logger.h"
#include "rpp.h"
#include "rules.h"
/*
* Error codes.
*/
#define RULES_ERROR_NONE 0
#define RULES_ERROR_END 1
#define RULES_ERROR_UNKNOWN 2
#define RULES_ERROR_UNALLOWED 3
#define RULES_ERROR_POSITION 4
#define RULES_ERROR_CLASS 5
#define RULES_ERROR_REJECT 6
/*
* Error names.
*/
static const char * const rules_errors[] = {
NULL, /* No error */
"Unexpected end of rule",
"Unknown command",
"Unallowed command",
"Invalid position code",
"Unknown character class code",
"Unknown rule reject flag"
};
/*
* Last error code.
*/
static int rules_errno;
/*
* Configuration file line number, only set after a rules_check() call if
* rules_errno indicates an error.
*/
static int rules_line;
static int rules_max_length = 0;
static struct {
unsigned char vars[0x100];
/*
* pass == -2 initial syntax checking of rules
* pass == -1 optimization of rules (no-ops are removed)
* pass == 0 actual processing of rules
*/
int pass;
/*
* Some rule commands may temporarily double the length, and we skip a few
* machine words to avoid cache bank conflicts when copying data between the
* buffers. We need three buffers because some rule commands require separate
* input and output buffers and we also need a buffer either for leaving the
* previous mangled word intact for a subsequent comparison (in wordlist mode)
* or for switching between two input words (in "single crack" mode).
* rules_apply() tries to minimize data copying, and thus it may return a
* pointer to any of the three buffers.
*/
union {
char buffer[3][RULE_WORD_SIZE * 2 + CACHE_BANK_SHIFT];
ARCH_WORD dummy;
} aligned;
/*
* "memory" doesn't have to be static (could as well be on stack), but we keep
* it here to ensure it doesn't occasionally "overlap" with our other data in
* terms of cache tags.
*/
char memory[RULE_WORD_SIZE];
char *classes[0x100];
} CC_CACHE_ALIGN rules_data;
#define rules_pass rules_data.pass
#define rules_classes rules_data.classes
#define rules_vars rules_data.vars
#define buffer rules_data.aligned.buffer
#define memory_buffer rules_data.memory
#define CONV_SOURCE \
"`1234567890-=\\qwertyuiop[]asdfghjkl;'zxcvbnm,./" \
"~!@#$%^&*()_+|QWERTYUIOP{}ASDFGHJKL:\"ZXCVBNM<>?"
#define CONV_SHIFT \
"~!@#$%^&*()_+|QWERTYUIOP{}ASDFGHJKL:\"ZXCVBNM<>?" \
"`1234567890-=\\qwertyuiop[]asdfghjkl;'zxcvbnm,./"
#define CONV_INVERT \
"`1234567890-=\\QWERTYUIOP[]ASDFGHJKL;'ZXCVBNM,./" \
"~!@#$%^&*()_+|qwertyuiop{}asdfghjkl:\"zxcvbnm<>?"
#define CONV_VOWELS \
"`1234567890-=\\QWeRTYuioP[]aSDFGHJKL;'ZXCVBNM,./" \
"~!@#$%^&*()_+|QWeRTYuioP{}aSDFGHJKL:\"ZXCVBNM<>?"
#define CONV_RIGHT \
"1234567890-=\\\\wertyuiop[]]sdfghjkl;''xcvbnm,./\\" \
"!@#$%^&*()_+||WERTYUIOP{}}SDFGHJKL:\"\"XCVBNM<>?|"
#define CONV_LEFT \
"``1234567890-=qqwertyuiop[aasdfghjkl;zzxcvbnm,." \
"~~!@#$%^&*()_+QQWERTYUIOP{AASDFGHJKL:ZZXCVBNM<>"
#define CHARS_LOWER \
"abcdefghijklmnopqrstuvwxyz"
#define CHARS_UPPER \
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define CHARS_DIGITS \
"0123456789"
static char *conv_source = CONV_SOURCE;
static char *conv_shift, *conv_invert, *conv_vowels, *conv_right, *conv_left;
static char *conv_tolower, *conv_toupper;
#define INVALID_LENGTH 0x81
#define INFINITE_LENGTH 0xFF
#define RULE (*rule++)
#define LAST (*(rule - 1))
#define NEXT (*rule)
#define REJECT { \
if (!rules_pass) goto out_NULL; \
}
#define VALUE(value) { \
if (!((value) = RULE)) goto out_ERROR_END; \
}
#define POSITION(pos) { \
if (((pos) = rules_vars[ARCH_INDEX(RULE)]) == INVALID_LENGTH) \
goto out_ERROR_POSITION; \
}
#define CLASS_export_pos(start, true, false) { \
char value, *class; \
if ((value = RULE) == '?') { \
if (!(class = rules_classes[ARCH_INDEX(RULE)])) \
goto out_ERROR_CLASS; \
for (pos = (start); ARCH_INDEX(in[pos]); pos++) \
if (class[ARCH_INDEX(in[pos])]) { \
true; \
} else { \
false; \
} \
} else { \
if (!value) goto out_ERROR_END; \
for (pos = (start); ARCH_INDEX(in[pos]); pos++) \
if (in[pos] == value) { \
true; \
} else { \
false; \
} \
} \
}
#define CLASS(start, true, false) { \
int pos; \
CLASS_export_pos(start, true, false); \
}
#define SKIP_CLASS { \
char value; \
VALUE(value) \
if (value == '?') VALUE(value) \
}
#define CONV(conv) { \
int pos; \
for (pos = 0; (in[pos] = (conv)[ARCH_INDEX(in[pos])]); pos++); \
}
#define GET_OUT { \
out = alt; \
alt = in; \
}
static void rules_init_class(char name, char *valid)
{
char *pos, inv;
rules_classes[ARCH_INDEX(name)] =
mem_alloc_tiny(0x100, MEM_ALIGN_NONE);
memset(rules_classes[ARCH_INDEX(name)], 0, 0x100);
for (pos = valid; ARCH_INDEX(*pos); pos++)
rules_classes[ARCH_INDEX(name)][ARCH_INDEX(*pos)] = 1;
if ((name | 0x20) >= 'a' && (name | 0x20) <= 'z') {
inv = name ^ 0x20;
rules_classes[ARCH_INDEX(inv)] =
mem_alloc_tiny(0x100, MEM_ALIGN_NONE);
memset(rules_classes[ARCH_INDEX(inv)], 1, 0x100);
for (pos = valid; ARCH_INDEX(*pos); pos++)
rules_classes[ARCH_INDEX(inv)][ARCH_INDEX(*pos)] = 0;
}
}
static void rules_init_classes(void)
{
memset(rules_classes, 0, sizeof(rules_classes));
rules_init_class('?', "?");
rules_init_class('v', "aeiouAEIOU");
rules_init_class('c', "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ");
rules_init_class('w', " \t");
rules_init_class('p', ".,:;'\"?!`");
rules_init_class('s', "$%^&*()-_+=|\\<>[]{}#@/~");
rules_init_class('l', CHARS_LOWER);
rules_init_class('u', CHARS_UPPER);
rules_init_class('d', CHARS_DIGITS);
rules_init_class('a', CHARS_LOWER CHARS_UPPER);
rules_init_class('x', CHARS_LOWER CHARS_UPPER CHARS_DIGITS);
rules_init_class('Z', "");
}
static char *rules_init_conv(char *src, char *dst)
{
char *conv;
int pos;
conv = mem_alloc_tiny(0x100, MEM_ALIGN_NONE);
for (pos = 0; pos < 0x100; pos++) conv[pos] = pos;
while (*src)
conv[ARCH_INDEX(*src++)] = *dst++;
return conv;
}
static void rules_init_convs(void)
{
conv_shift = rules_init_conv(conv_source, CONV_SHIFT);
conv_invert = rules_init_conv(conv_source, CONV_INVERT);
conv_vowels = rules_init_conv(conv_source, CONV_VOWELS);
conv_right = rules_init_conv(conv_source, CONV_RIGHT);
conv_left = rules_init_conv(conv_source, CONV_LEFT);
conv_tolower = rules_init_conv(CHARS_UPPER, CHARS_LOWER);
conv_toupper = rules_init_conv(CHARS_LOWER, CHARS_UPPER);
}
static void rules_init_length(int max_length)
{
int c;
memset(rules_vars, INVALID_LENGTH, sizeof(rules_vars));
for (c = '0'; c <= '9'; c++) rules_vars[c] = c - '0';
for (c = 'A'; c <= 'Z'; c++) rules_vars[c] = c - ('A' - 10);
rules_vars['*'] = rules_max_length = max_length;
rules_vars['-'] = max_length - 1;
rules_vars['+'] = max_length + 1;
rules_vars['z'] = INFINITE_LENGTH;
}
void rules_init(int max_length)
{
rules_pass = 0;
rules_errno = RULES_ERROR_NONE;
if (max_length > RULE_WORD_SIZE - 1)
max_length = RULE_WORD_SIZE - 1;
if (max_length == rules_max_length) return;
if (!rules_max_length) {
rules_init_classes();
rules_init_convs();
}
rules_init_length(max_length);
}
char *rules_reject(char *rule, int split, char *last, struct db_main *db)
{
static char out_rule[RULE_BUFFER_SIZE];
while (RULE)
switch (LAST) {
case ':':
case ' ':
case '\t':
break;
case '-':
switch (RULE) {
case ':':
continue;
case 'c':
if (!db) continue;
if (db->format->params.flags & FMT_CASE) continue;
return NULL;
case '8':
if (!db) continue;
if (db->format->params.flags & FMT_8_BIT) continue;
return NULL;
case 's':
if (!db) continue;
if (db->options->flags & DB_SPLIT) continue;
return NULL;
case 'p':
if (split >= 0) continue;
return NULL;
case '\0':
rules_errno = RULES_ERROR_END;
return NULL;
default:
rules_errno = RULES_ERROR_REJECT;
return NULL;
}
default:
goto accept;
}
accept:
rules_pass--;
strnzcpy(out_rule, rule - 1, sizeof(out_rule));
rules_apply("", out_rule, split, last);
rules_pass++;
return out_rule;
}
char *rules_apply(char *word, char *rule, int split, char *last)
{
char *in, *alt, *memory = word;
int length;
int which;
in = buffer[0];
if (in == last)
in = buffer[2];
length = 0;
while (length < RULE_WORD_SIZE) {
if (!(in[length] = word[length]))
break;
length++;
}
/*
* This check assumes that rules_reject() has optimized the no-op rule
* (a colon) into an empty string.
*/
if (!NEXT)
goto out_OK;
if (!length) REJECT
alt = buffer[1];
if (alt == last)
alt = buffer[2];
/*
* This assumes that RULE_WORD_SIZE is small enough that length can't reach or
* exceed INVALID_LENGTH.
*/
rules_vars['l'] = length;
rules_vars['m'] = (unsigned char)length - 1;
which = 0;
while (RULE) {
if (length >= RULE_WORD_SIZE)
in[length = RULE_WORD_SIZE - 1] = 0;
switch (LAST) {
/* Crack 4.1 rules */
case ':':
case ' ':
case '\t':
if (rules_pass == -1) {
memmove(rule - 1, rule, strlen(rule) + 1);
rule--;
}
break;
case '<':
{
int pos;
POSITION(pos)
if (length >= pos) REJECT
}
break;
case '>':
{
int pos;
POSITION(pos)
if (length <= pos) REJECT
}
break;
case 'l':
CONV(conv_tolower)
break;
case 'u':
CONV(conv_toupper)
break;
case 'c':
{
int pos = 0;
if ((in[0] = conv_toupper[ARCH_INDEX(in[0])]))
while (in[++pos])
in[pos] =
conv_tolower[ARCH_INDEX(in[pos])];
in[pos] = 0;
}
break;
case 'r':
{
char *out;
GET_OUT
*(out += length) = 0;
while (*in)
*--out = *in++;
in = out;
}
break;
case 'd':
memcpy(in + length, in, length);
in[length <<= 1] = 0;
break;
case 'f':
{
int pos;
in[pos = (length <<= 1)] = 0;
{
char *p = in;
while (*p)
in[--pos] = *p++;
}
}
break;
case 'p':
if (length < 2) break;
{
int pos = length - 1;
if (strchr("sxz", in[pos]) ||
(pos > 1 && in[pos] == 'h' &&
(in[pos - 1] == 'c' || in[pos - 1] == 's')))
strcat(in, "es");
else
if (in[pos] == 'f' && in[pos - 1] != 'f')
strcpy(&in[pos], "ves");
else
if (pos > 1 &&
in[pos] == 'e' && in[pos - 1] == 'f')
strcpy(&in[pos - 1], "ves");
else
if (pos > 1 && in[pos] == 'y') {
if (strchr("aeiou", in[pos - 1]))
strcat(in, "s");
else
strcpy(&in[pos], "ies");
} else
strcat(in, "s");
}
length = strlen(in);
break;
case '$':
VALUE(in[length++])
if (NEXT == '$') {
(void)RULE;
VALUE(in[length++])
if (NEXT == '$') {
(void)RULE;
VALUE(in[length++])
}
}
in[length] = 0;
break;
case '^':
{
char *out, a, b;
GET_OUT
VALUE(a)
if (NEXT != '^') {
out[0] = a;
memcpy(&out[1], in, ++length);
in = out;
break;
}
(void)RULE;
VALUE(b)
if (NEXT != '^') {
out[0] = b;
out[1] = a;
memcpy(&out[2], in, length + 1);
length += 2;
in = out;
break;
}
(void)RULE;
VALUE(out[0])
out[1] = b;
out[2] = a;
memcpy(&out[3], in, length + 1);
length += 3;
in = out;
}
break;
case 'x':
{
int pos;
POSITION(pos)
if (pos < length) {
char *out;
GET_OUT
in += pos;
POSITION(pos)
strnzcpy(out, in, pos + 1);
length = strlen(in = out);
break;
}
POSITION(pos)
in[length = 0] = 0;
}
break;
case 'i':
{
int pos;
POSITION(pos)
if (pos < length) {
char *p = in + pos;
memmove(p + 1, p, length++ - pos);
VALUE(*p)
in[length] = 0;
break;
}
}
VALUE(in[length++])
in[length] = 0;
break;
case 'o':
{
int pos;
char value;
POSITION(pos)
VALUE(value);
if (pos < length)
in[pos] = value;
}
break;
case 's':
CLASS(0, in[pos] = NEXT, {})
{
char value;
VALUE(value)
}
break;
case '@':
length = 0;
CLASS(0, {}, in[length++] = in[pos])
in[length] = 0;
break;
case '!':
CLASS(0, REJECT, {})
break;
case '/':
{
int pos;
CLASS_export_pos(0, break, {})
rules_vars['p'] = pos;
if (in[pos]) break;
}
REJECT
break;
case '=':
{
int pos;
POSITION(pos)
if (pos >= length) {
SKIP_CLASS
REJECT
} else {
CLASS_export_pos(pos, break, REJECT)
}
}
break;
/* Crack 5.0 rules */
case '[':
{
int count = 1;
while (NEXT == '[') {
(void)RULE;
count++;
}
if ((length -= count) > 0) {
char *out;
GET_OUT
memcpy(out, &in[count], length + 1);
in = out;
break;
}
in[length = 0] = 0;
}
break;
case ']':
{
int count = 1;
while (NEXT == ']') {
(void)RULE;
count++;
}
if ((length -= count) < 0)
length = 0;
in[length] = 0;
}
break;
case 'C':
{
int pos = 0;
if ((in[0] = conv_tolower[ARCH_INDEX(in[0])]))
while (in[++pos])
in[pos] =
conv_toupper[ARCH_INDEX(in[pos])];
in[pos] = 0;
}
break;
case 't':
CONV(conv_invert)
break;
case '(':
CLASS(0, break, REJECT)
break;
case ')':
if (!length) {
SKIP_CLASS
REJECT
} else {
CLASS(length - 1, break, REJECT)
}
break;
case '\'':
{
int pos;
POSITION(pos)
if (pos < length)
in[length = pos] = 0;
}
break;
case '%':
{
int count = 0, required, pos;
POSITION(required)
CLASS_export_pos(0,
if (++count >= required) break, {})
if (count < required) REJECT
rules_vars['p'] = pos;
}
break;
/* Rules added in John */
case 'A': /* append/insert/prepend string */
{
int pos;
char term;
POSITION(pos)
VALUE(term)
if (pos >= length) { /* append */
char *start, *end, *p;
start = p = &in[pos = length];
end = &in[RULE_WORD_SIZE - 1];
do {
char c = RULE;
if (c == term)
break;
if (p < end)
*p++ = c;
if (c)
continue;
goto out_ERROR_END;
} while (1);
*p = 0;
length += p - start;
break;
}
/* insert or prepend */
{
char *out, *start, *end, *p;
GET_OUT
memcpy(out, in, pos);
start = p = &out[pos];
end = &out[RULE_WORD_SIZE - 1];
do {
char c = RULE;
if (c == term)
break;
if (p < end)
*p++ = c;
if (c)
continue;
goto out_ERROR_END;
} while (1);
strcpy(p, &in[pos]);
length += p - start;
in = out;
}
}
break;
case 'T':
{
int pos;
POSITION(pos)
in[pos] = conv_invert[ARCH_INDEX(in[pos])];
}
break;
case 'D':
{
int pos;
POSITION(pos)
if (pos < length) {
memmove(&in[pos], &in[pos + 1],
length - pos);
length--;
}
}
break;
case '{':
if (length) {
char *out;
int count = 1;
while (NEXT == '{') {
(void)RULE;
count++;
}
while (count >= length)
count -= length;
if (!count)
break;
GET_OUT
memcpy(out, &in[count], length - count);
memcpy(&out[length - count], in, count);
out[length] = 0;
in = out;
break;
}
in[0] = 0;
break;
case '}':
if (length) {
char *out;
int pos;
int count = 1;
while (NEXT == '}') {
(void)RULE;
count++;
}
while (count >= length)
count -= length;
if (!count)
break;
GET_OUT
memcpy(out, &in[pos = length - count], count);
memcpy(&out[count], in, pos);
out[length] = 0;
in = out;
break;
}
in[0] = 0;
break;
case 'S':
CONV(conv_shift);
break;
case 'V':
CONV(conv_vowels);
break;
case 'R':
CONV(conv_right);
break;
case 'L':
CONV(conv_left);
break;
case 'P':
{
int pos;
if ((pos = length - 1) < 2) break;
if (in[pos] == 'd' && in[pos - 1] == 'e') break;
if (in[pos] == 'y') in[pos] = 'i'; else
if (strchr("bgp", in[pos]) &&
!strchr("bgp", in[pos - 1])) {
in[pos + 1] = in[pos];
in[pos + 2] = 0;
}
if (in[pos] == 'e')
strcat(in, "d");
else
strcat(in, "ed");
}
length = strlen(in);
break;
case 'I':
{
int pos;
if ((pos = length - 1) < 2) break;
if (in[pos] == 'g' && in[pos - 1] == 'n' &&
in[pos - 2] == 'i') break;
if (strchr("aeiou", in[pos]))
strcpy(&in[pos], "ing");
else {
if (strchr("bgp", in[pos]) &&
!strchr("bgp", in[pos - 1])) {
in[pos + 1] = in[pos];
in[pos + 2] = 0;
}
strcat(in, "ing");
}
}
length = strlen(in);
break;
case 'M':
memcpy(memory = memory_buffer, in, length + 1);
rules_vars['m'] = (unsigned char)length - 1;
break;
case 'Q':
if (NEXT) {
if (!strcmp(memory, in))
REJECT
} else if (!strncmp(memory, in, rules_max_length))
REJECT
break;
case 'X': /* append/insert/prepend substring from memory */
{
int mpos, count, ipos, mleft;
char *inp, *mp;
POSITION(mpos)
POSITION(count)
POSITION(ipos)
mleft = (int)(unsigned char)
(rules_vars['m'] + 1) - mpos;
if (count > mleft)
count = mleft;
if (count <= 0)
break;
mp = memory + mpos;
if (ipos >= length) {
memcpy(&in[length], mp, count);
in[length += count] = 0;
break;
}
inp = in + ipos;
memmove(inp + count, inp, length - ipos);
in[length += count] = 0;
memcpy(inp, mp, count);
}
break;
case 'v': /* assign value to numeric variable */
{
char var;
unsigned char a, s;
VALUE(var)
if (var < 'a' || var > 'k')
goto out_ERROR_POSITION;
rules_vars['l'] = length;
POSITION(a)
POSITION(s)
rules_vars[ARCH_INDEX(var)] = a - s;
}
break;
/* Additional "single crack" mode rules */
case '1':
if (split < 0)
goto out_ERROR_UNALLOWED;
if (!split) REJECT
if (which)
memcpy(buffer[2], in, length + 1);
else
strnzcpy(buffer[2], &word[split],
RULE_WORD_SIZE);
length = split;
if (length > RULE_WORD_SIZE - 1)
length = RULE_WORD_SIZE - 1;
memcpy(in, word, length);
in[length] = 0;
which = 1;
break;
case '2':
if (split < 0)
goto out_ERROR_UNALLOWED;
if (!split) REJECT
if (which) {
memcpy(buffer[2], in, length + 1);
} else {
length = split;
if (length > RULE_WORD_SIZE - 1)
length = RULE_WORD_SIZE - 1;
strnzcpy(buffer[2], word, length + 1);
}
strnzcpy(in, &word[split], RULE_WORD_SIZE);
length = strlen(in);
which = 2;
break;
case '+':
switch (which) {
case 1:
strcat(in, buffer[2]);
break;
case 2:
{
char *out;
GET_OUT
strcpy(out, buffer[2]);
strcat(out, in);
in = out;
}
break;
default:
goto out_ERROR_UNALLOWED;
}
length = strlen(in);
which = 0;
break;
default:
goto out_ERROR_UNKNOWN;
}
if (!length) REJECT
}
if (which)
goto out_which;
out_OK:
in[rules_max_length] = 0;
if (last) {
if (length > rules_max_length)
length = rules_max_length;
if (length >= ARCH_SIZE - 1) {
if (*(ARCH_WORD *)in != *(ARCH_WORD *)last)
return in;
if (strcmp(&in[ARCH_SIZE - 1], &last[ARCH_SIZE - 1]))
return in;
return NULL;
}
if (last[length])
return in;
if (memcmp(in, last, length))
return in;