-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
pp.c
2499 lines (1973 loc) · 57.1 KB
/
pp.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
/*
* Implementation in John the Ripper Copyright (c) 2015, magnum
* This software is hereby released to the general public under
* the following terms: Redistribution and use in source and binary
* forms, with or without modification, are permitted.
*
* The MIT License (MIT)
* Copyright (c) 2015 Jens Steube
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#if AC_BUILT
#include "autoconfig.h"
#else
#include <sys/mman.h>
#define _GNU_SOURCE 1
#define _FILE_OFFSET_BITS 64
#define __USE_MINGW_ANSI_STDIO 1
#ifdef __SIZEOF_INT128__
#define HAVE___INT128 1
#endif
#endif
#if HAVE_LIBGMP || HAVE_INT128 || HAVE___INT128 || HAVE___INT128_T
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#if !AC_BUILT
#include <string.h>
#ifndef _MSC_VER
#include <strings.h>
#endif
#else
#if STRING_WITH_STRINGS
#include <string.h>
#include <strings.h>
#elif HAVE_STRING_H
#include <string.h>
#elif HAVE_STRINGS_H
#include <strings.h>
#endif
#endif
#include <fcntl.h>
#include <time.h>
#include <errno.h>
#ifndef JTR_MODE
#include <getopt.h>
#endif
#include <ctype.h>
#include <signal.h>
#if HAVE_INT128 || HAVE___INT128 || HAVE___INT128_T
#include "mpz_int128.h"
#define REALGMP "int128"
#else
#define REALGMP "GMP"
#if HAVE_GMP_GMP_H
#include <gmp/gmp.h>
#else
#include <gmp.h>
#endif
#endif
/**
* Name........: princeprocessor (pp)
* Description.: Standalone password candidate generator using the PRINCE algorithm
* Version.....: 0.22
* Authors.....: Jens Steube <jens.steube@gmail.com>
* Steve Thomas (Sc00bz)
* magnum <john.magnum@hushmail.com>
* License.....: MIT
*/
#ifdef JTR_MODE
#include "os.h"
#if (!AC_BUILT || HAVE_UNISTD_H) && !_MSC_VER
#include <unistd.h>
#endif
#include "arch.h"
#include "jumbo.h"
#include "misc.h"
#include "config.h"
#include "math.h"
#include "params.h"
#include "common.h"
#include "path.h"
#include "signals.h"
#include "mem_map.h"
#include "memory.h"
#include "loader.h"
#include "logger.h"
#include "status.h"
#include "recovery.h"
#include "options.h"
#include "external.h"
#include "cracker.h"
#include "suppressor.h"
#include "john.h"
#include "unicode.h"
#include "prince.h"
#include "rpp.h"
#include "rules.h"
#include "mask.h"
#include "regex.h"
#define _STR_VALUE(arg) #arg
#define STR_MACRO(n) _STR_VALUE(n)
int prince_elem_cnt_min;
int prince_elem_cnt_max;
int prince_wl_max;
char *prince_skip_str;
char *prince_limit_str;
static double progress;
static char *mem_map, *map_pos, *map_end;
#if HAVE_REXGEN
static char *regex_alpha;
static int regex_case;
static char *regex;
#endif
#else
#undef MIN
#undef MAX
#endif
#define IN_LEN_MIN 1
#define IN_LEN_MAX 32
#define OUT_LEN_MAX 32 /* Limited by (u32)(1 << pw_len - 1) */
#define PW_MIN 1
#define PW_MAX 16
#define ELEM_CNT_MIN 1
#define ELEM_CNT_MAX 8
#define WL_DIST_LEN 0
#define WL_MAX 10000000
#define CASE_PERMUTE 0
#define DUPE_CHECK 1
#define SAVE_POS 1
#define SAVE_FILE "pp.save"
#define VERSION_BIN 22
#define ALLOC_NEW_ELEMS 0x40000
#define ALLOC_NEW_CHAINS 0x10
#define ALLOC_NEW_DUPES 0x100000
#define ENTRY_END_HASH 0xFFFFFFFF
#ifndef JTR_MODE
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
#endif
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef struct
{
int len;
u64 cnt;
} pw_order_t;
typedef struct
{
u8 *buf;
} elem_t;
typedef struct
{
u8 *buf;
int cnt;
mpz_t ks_cnt;
mpz_t ks_pos;
} chain_t;
typedef struct
{
u32 next;
char *element;
} uniq_data_t;
typedef struct
{
u32 index;
u32 alloc;
u32 *hash;
u32 hash_mask;
uniq_data_t *data;
} uniq_t;
typedef struct
{
elem_t *elems_buf;
u64 elems_cnt;
u64 elems_alloc;
chain_t *chains_buf;
int chains_cnt;
int chains_pos;
int chains_alloc;
u64 cur_chain_ks_poses[OUT_LEN_MAX];
uniq_t *uniq;
} db_entry_t;
#ifndef JTR_MODE
typedef struct
{
FILE *fp;
char buf[BUFSIZ];
int len;
} out_t;
#endif
/**
* Default word-length distribution, calculated out of first 1,000,000 entries of rockyou.txt
*/
#define DEF_WORDLEN_DIST_CNT 25
static u64 DEF_WORDLEN_DIST[DEF_WORDLEN_DIST_CNT] =
{
0,
15,
56,
350,
3315,
43721,
276252,
201748,
226412,
119885,
75075,
26323,
13373,
6353,
3540,
1877,
972,
311,
151,
81,
66,
21,
16,
13,
13
};
#ifndef JTR_MODE
/* Losely based on rockyou-with-dupes */
static const u32 DEF_HASH_LOG_SIZE[33] =
{ 0,
8, 12, 16, 20, 24, 24, 24, 24,
24, 24, 23, 22, 21, 20, 19, 18,
17, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16
};
static const char *USAGE_MINI[] =
{
"Usage: %s [options] [<] wordlist",
"",
"Try --help for more help.",
NULL
};
static const char *USAGE_BIG[] =
{
"Usage: %s [options] [<] wordlist",
"",
"* Startup:",
"",
" -V, --version Print version",
" -h, --help Print help",
"",
"* Misc:",
"",
" --keyspace Calculate number of combinations",
"",
"* Optimization:",
"",
" --pw-min=NUM Print candidate if length is greater than NUM",
" --pw-max=NUM Print candidate if length is smaller than NUM",
" --elem-cnt-min=NUM Minimum number of elements per chain",
" --elem-cnt-max=NUM Maximum number of elements per chain",
" --wl-dist-len Calculate output length distribution from wordlist",
" --wl-max=NUM Load only NUM words from input wordlist or use 0 to disable",
" -c, --dupe-check-disable Disable dupes check for faster initial load",
" --save-pos-disable Save the position for later resume with -s",
"",
"* Resources:",
"",
" -s, --skip=NUM Skip NUM passwords from start (for distributed)",
" -l, --limit=NUM Limit output to NUM passwords (for distributed)",
"",
"* Files:",
"",
" -o, --output-file=FILE Output-file",
"",
"* Amplifier:",
"",
" --case-permute For each word in the wordlist that begins with a letter",
" generate a word with the opposite case of the first letter",
"",
NULL
};
static void *mem_alloc (const size_t size)
{
void *res = malloc (size);
if (res == NULL)
{
fprintf (stderr, "malloc: %s\n", strerror (ENOMEM));
exit (-1);
}
return res;
}
static void *malloc_tiny (const size_t size)
{
#ifdef DEBUG
#define MEM_ALLOC_SIZE 0 /* It's hard to debug BOF with tiny alloc */
#else
#define MEM_ALLOC_SIZE 0x10000
#endif
if (size > MEM_ALLOC_SIZE)
{
// we can't handle it here
return mem_alloc (size);
}
static char *buffer = NULL;
static size_t bufree = 0;
if (size > bufree)
{
buffer = mem_alloc (MEM_ALLOC_SIZE);
bufree = MEM_ALLOC_SIZE;
}
char *p = buffer;
buffer += size;
bufree -= size;
return p;
}
static void usage_mini_print (const char *progname)
{
int i;
for (i = 0; USAGE_MINI[i] != NULL; i++)
{
printf (USAGE_MINI[i], progname);
#ifdef OSX
putchar ('\n');
#endif
#ifdef LINUX
putchar ('\n');
#endif
#ifdef WINDOWS
putchar ('\r');
putchar ('\n');
#endif
}
}
static void usage_big_print (const char *progname)
{
int i;
for (i = 0; USAGE_BIG[i] != NULL; i++)
{
printf (USAGE_BIG[i], progname);
#ifdef OSX
putchar ('\n');
#endif
#ifdef LINUX
putchar ('\n');
#endif
#ifdef WINDOWS
putchar ('\r');
putchar ('\n');
#endif
}
}
#else
#define malloc_tiny(size) mem_alloc_tiny(size, MEM_ALIGN_NONE)
#endif
static void check_realloc_elems (db_entry_t *db_entry)
{
if (db_entry->elems_cnt == db_entry->elems_alloc)
{
const u64 elems_alloc = db_entry->elems_alloc;
const u64 elems_alloc_new = elems_alloc + ALLOC_NEW_ELEMS;
db_entry->elems_buf = (elem_t *) realloc (db_entry->elems_buf, elems_alloc_new * sizeof (elem_t));
if (db_entry->elems_buf == NULL)
{
#ifdef JTR_MODE
fprintf (stderr, "Out of memory trying to allocate "Zu" bytes\n", (size_t) elems_alloc_new * sizeof (elem_t));
#else
fprintf (stderr, "Out of memory trying to allocate %zu bytes\n", (size_t) elems_alloc_new * sizeof (elem_t));
#endif
#ifndef JTR_MODE
exit (-1);
#else
error();
#endif
}
memset (&db_entry->elems_buf[elems_alloc], 0, ALLOC_NEW_ELEMS * sizeof (elem_t));
db_entry->elems_alloc = elems_alloc_new;
}
}
static void check_realloc_chains (db_entry_t *db_entry)
{
if (db_entry->chains_cnt == db_entry->chains_alloc)
{
const u64 chains_alloc = db_entry->chains_alloc;
const u64 chains_alloc_new = chains_alloc + ALLOC_NEW_CHAINS;
db_entry->chains_buf = (chain_t *) realloc (db_entry->chains_buf, chains_alloc_new * sizeof (chain_t));
if (db_entry->chains_buf == NULL)
{
#ifdef JTR_MODE
fprintf (stderr, "Out of memory trying to allocate "Zu" bytes\n", (size_t) chains_alloc_new * sizeof (chain_t));
#else
fprintf (stderr, "Out of memory trying to allocate %zu bytes\n", (size_t) chains_alloc_new * sizeof (chain_t));
#endif
#ifndef JTR_MODE
exit (-1);
#else
error();
#endif
}
memset (&db_entry->chains_buf[chains_alloc], 0, ALLOC_NEW_CHAINS * sizeof (chain_t));
db_entry->chains_alloc = chains_alloc_new;
}
}
static int in_superchop (char *buf)
{
int len = strlen (buf);
while (len)
{
if (buf[len - 1] == '\n')
{
len--;
continue;
}
if (buf[len - 1] == '\r')
{
len--;
continue;
}
break;
}
buf[len] = 0;
return len;
}
#ifndef JTR_MODE
static void out_flush (out_t *out)
{
const size_t n = fwrite (out->buf, 1, out->len, out->fp);
if (n != (size_t) out->len)
{
const int err = ferror (out->fp);
if (err == EPIPE)
{
// out->fp is probably closed
exit (0);
}
exit (-1);
}
out->len = 0;
}
static void out_push (out_t *out, const char *pw_buf, const int pw_len)
{
memcpy (out->buf + out->len, pw_buf, pw_len);
out->len += pw_len;
if (out->len >= BUFSIZ - 100)
{
out_flush (out);
}
}
#endif
static int sort_by_cnt (const void *p1, const void *p2)
{
const pw_order_t *o1 = (const pw_order_t *) p1;
const pw_order_t *o2 = (const pw_order_t *) p2;
// Descending order
if (o1->cnt > o2->cnt) return -1;
if (o1->cnt < o2->cnt) return 1;
return 0;
}
static int sort_by_ks (const void *p1, const void *p2)
{
const chain_t *f1 = (const chain_t *) p1;
const chain_t *f2 = (const chain_t *) p2;
return mpz_cmp (f1->ks_cnt, f2->ks_cnt);
}
static int chain_valid_with_db (const chain_t *chain_buf, const db_entry_t *db_entries)
{
const u8 *buf = chain_buf->buf;
const int cnt = chain_buf->cnt;
for (int idx = 0; idx < cnt; idx++)
{
const u8 db_key = buf[idx];
const db_entry_t *db_entry = &db_entries[db_key];
if (db_entry->elems_cnt == 0) return 0;
}
return 1;
}
static int chain_valid_with_cnt_min (const chain_t *chain_buf, const int elem_cnt_min)
{
const int cnt = chain_buf->cnt;
if (cnt < elem_cnt_min) return 0;
return 1;
}
static int chain_valid_with_cnt_max (const chain_t *chain_buf, const int elem_cnt_max)
{
const int cnt = chain_buf->cnt;
if (cnt > elem_cnt_max) return 0;
return 1;
}
static void chain_ks (const chain_t *chain_buf, const db_entry_t *db_entries, mpz_t *ks_cnt)
{
const u8 *buf = chain_buf->buf;
const int cnt = chain_buf->cnt;
mpz_set_si (*ks_cnt, 1);
for (int idx = 0; idx < cnt; idx++)
{
const u8 db_key = buf[idx];
const db_entry_t *db_entry = &db_entries[db_key];
const u64 elems_cnt = db_entry->elems_cnt;
mpz_mul_ui (*ks_cnt, *ks_cnt, elems_cnt);
}
}
static void set_chain_ks_poses (const chain_t *chain_buf, const db_entry_t *db_entries, mpz_t *tmp, u64 cur_chain_ks_poses[OUT_LEN_MAX])
{
const u8 *buf = chain_buf->buf;
const int cnt = chain_buf->cnt;
for (int idx = 0; idx < cnt; idx++)
{
const u8 db_key = buf[idx];
const db_entry_t *db_entry = &db_entries[db_key];
const u64 elems_cnt = db_entry->elems_cnt;
cur_chain_ks_poses[idx] = mpz_fdiv_ui (*tmp, elems_cnt);
mpz_div_ui (*tmp, *tmp, elems_cnt);
}
}
static void chain_set_pwbuf_init (const chain_t *chain_buf, const db_entry_t *db_entries, const u64 cur_chain_ks_poses[OUT_LEN_MAX], char *pw_buf)
{
const u8 *buf = chain_buf->buf;
const u32 cnt = chain_buf->cnt;
for (u32 idx = 0; idx < cnt; idx++)
{
const u8 db_key = buf[idx];
const db_entry_t *db_entry = &db_entries[db_key];
const u64 elems_idx = cur_chain_ks_poses[idx];
memcpy (pw_buf, db_entry->elems_buf[elems_idx].buf, db_key);
pw_buf += db_key;
}
}
static void chain_set_pwbuf_increment (const chain_t *chain_buf, const db_entry_t *db_entries, u64 cur_chain_ks_poses[OUT_LEN_MAX], char *pw_buf)
{
const u8 *buf = chain_buf->buf;
const int cnt = chain_buf->cnt;
for (int idx = 0; idx < cnt; idx++)
{
const u8 db_key = buf[idx];
const db_entry_t *db_entry = &db_entries[db_key];
const u64 elems_cnt = db_entry->elems_cnt;
cur_chain_ks_poses[idx]++;
const u64 elems_idx = cur_chain_ks_poses[idx];
if (elems_idx < elems_cnt)
{
memcpy (pw_buf, db_entry->elems_buf[elems_idx].buf, db_key);
break;
}
cur_chain_ks_poses[idx] = 0;
memcpy (pw_buf, db_entry->elems_buf[0].buf, db_key);
pw_buf += db_key;
}
}
static void chain_gen_with_idx (chain_t *chain_buf, const int len1, const int chains_idx)
{
chain_buf->cnt = 0;
u8 db_key = 1;
for (int chains_shr = 0; chains_shr < len1; chains_shr++)
{
if ((chains_idx >> chains_shr) & 1)
{
chain_buf->buf[chain_buf->cnt] = db_key;
chain_buf->cnt++;
db_key = 1;
}
else
{
db_key++;
}
}
chain_buf->buf[chain_buf->cnt] = db_key;
chain_buf->cnt++;
}
static char *add_elem (db_entry_t *db_entry, char *input_buf, int input_len)
{
check_realloc_elems (db_entry);
elem_t *elem_buf = &db_entry->elems_buf[db_entry->elems_cnt];
#ifndef JTR_MODE
elem_buf->buf = malloc_tiny (input_len);
memcpy (elem_buf->buf, input_buf, input_len);
#else
if (mem_map && options.input_enc == options.target_enc)
{
elem_buf->buf = (u8*)input_buf;
}
else
{
elem_buf->buf = malloc_tiny (input_len);
memcpy (elem_buf->buf, input_buf, input_len);
}
#endif
db_entry->elems_cnt++;
return (char *) elem_buf->buf;
}
static u32 input_hash (char *input_buf, int input_len, const int hash_mask)
{
u32 h = 0;
for (int i = 0; i < input_len; i++)
{
h = (h * 33) + input_buf[i];
}
return h & hash_mask;
}
static void add_uniq (db_entry_t *db_entry, char *input_buf, int input_len)
{
uniq_t *uniq = db_entry->uniq;
const u32 h = input_hash (input_buf, input_len, uniq->hash_mask);
u32 cur = uniq->hash[h];
u32 prev = cur;
while (cur != ENTRY_END_HASH)
{
if (memcmp (input_buf, uniq->data[cur].element, input_len) == 0) return;
prev = cur;
cur = uniq->data[cur].next;
}
const u32 index = uniq->index;
if (prev == ENTRY_END_HASH)
{
uniq->hash[h] = index;
}
else
{
uniq->data[prev].next = index;
}
if (index == uniq->alloc)
{
uniq->alloc += ALLOC_NEW_DUPES;
uniq->data = realloc (uniq->data, uniq->alloc * sizeof (uniq_data_t));
if (uniq->data == NULL)
{
#ifdef JTR_MODE
fprintf (stderr, "Out of memory trying to allocate "Zu" bytes\n", (size_t) uniq->alloc * sizeof (uniq_data_t));
error();
#else
fprintf (stderr, "Out of memory trying to allocate %zu bytes\n", (size_t) uniq->alloc * sizeof (uniq_data_t));
exit (-1);
#endif
}
}
uniq->data[index].element = add_elem (db_entry, input_buf, input_len);
uniq->data[index].next = ENTRY_END_HASH;
uniq->index++;
}
mpz_t save;
#ifndef JTR_MODE
static void catch_int (int signum)
{
FILE *fp = fopen (SAVE_FILE, "w");
if (fp == NULL) fp = stderr;
mpz_out_str (fp, 10, save);
fprintf (fp, "\n");
fclose (fp);
exit (signum==0?0:signum);
}
int main (int argc, char *argv[])
#else
static mpf_t count;
static mpz_t rec_pos;
static mpz_t hybrid_rec_pos;
static int rec_pos_destroyed;
static int rule_count;
static struct list_main *rule_list;
static void save_state(FILE *file)
{
mpz_t half; mpz_init(half);
mpz_fdiv_r_2exp(half, rec_pos, 64); // lower 64 bits
fprintf(file, "%"PRIu64"\n", (uint64_t)mpz_get_ui(half));
mpz_fdiv_q_2exp(half, rec_pos, 64); // upper 64 bits
fprintf(file, "%"PRIu64"\n", (uint64_t)mpz_get_ui(half));
}
static int restore_state(FILE *file)
{
uint64_t temp;
mpz_t hi;
if (fscanf(file, "%"PRIu64"\n", &temp) != 1)
return 1;
mpz_set_ui(rec_pos, temp);
if (fscanf(file, "%"PRIu64"\n", &temp) != 1)
return 1;
mpz_init_set_ui(hi, temp);
mpz_mul_2exp(hi, hi, 64); // hi = temp << 64
mpz_add(rec_pos, rec_pos, hi);
mpz_clear(hi);
return 0;
}
static void fix_state(void)
{
if (mpz_cmp_ui(hybrid_rec_pos, 0)) {
mpz_set(rec_pos, hybrid_rec_pos);
mpz_set_ui(hybrid_rec_pos, 0);
} else {
mpz_set(rec_pos, save);
}
}
void pp_hybrid_fix_state(void)
{
mpz_set(hybrid_rec_pos, save);
}
static double get_progress(void)
{
mpf_t fpos, perc;
if (rec_pos_destroyed)
return progress;
mpf_init(fpos); mpf_init(perc);
mpf_set_z(fpos, rec_pos);
if (mpf_sgn(count))
mpf_div(perc, fpos, count);
progress = 100.0 * mpf_get_d(perc);
mpf_clear(fpos); mpf_clear(perc);
return progress;
}
static int get_bits(mpz_t *op)
{
mpz_t half; mpz_init(half);
u64 h;
int b;
mpz_fdiv_q_2exp(half, *op, 64);
h = mpz_get_ui(half);
if (h) b = 64;
else
{
mpz_fdiv_r_2exp(half, *op, 64);
h = mpz_get_ui(half);
b = 0;
}
while (h >>= 1) b++;
return b;
}
/*
* There should be legislation against adding a BOM to UTF-8, not to
* mention calling UTF-16 a "text file".
*/
static MAYBE_INLINE char *check_bom(char *string)
{
static int warned;
if (((unsigned char*)string)[0] < 0xef)
return string;
if (!memcmp(string, "\xEF\xBB\xBF", 3))
string += 3;
if (options.input_enc == UTF_8 &&
(!memcmp(string, "\xFE\xFF", 2) || !memcmp(string, "\xFF\xFE", 2))) {
if (john_main_process && !warned++)
fprintf(stderr, "Warning: UTF-16 BOM seen in wordlist.\n");
string += 2;
}
return string;
}
/* Sort-of fgets() but for a memory-mapped file. Updates len, returns pointer to string */
static MAYBE_INLINE char *mgets(int *len)
{
char *pos = map_pos;
char *end = MIN(map_end, pos + BUFSIZ);
if (map_pos >= map_end)
return NULL;
while (map_pos < end && *map_pos != '\n' && *map_pos != '\r')
map_pos++;
*len = map_pos - pos;
while (map_pos < end && (*map_pos == '\n' || *map_pos == '\r'))
map_pos++;
return pos;
}
void do_prince_crack(struct db_main *db, const char *wordlist, int rules)
#endif
{
mpz_t pw_ks_pos[OUT_LEN_MAX + 1];
mpz_t pw_ks_cnt[OUT_LEN_MAX + 1];
mpz_t iter_max; mpz_init_set_si (iter_max, 0);
mpz_t total_ks_cnt; mpz_init_set_si (total_ks_cnt, 0);
mpz_t total_ks_pos; mpz_init_set_si (total_ks_pos, 0);
mpz_t total_ks_left; mpz_init_set_si (total_ks_left, 0);
mpz_t skip; mpz_init_set_si (skip, 0);
mpz_t limit; mpz_init_set_si (limit, 0);
mpz_t tmp; mpz_init_set_si (tmp, 0);