-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
177 lines (146 loc) · 3.98 KB
/
main.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
/*
bf_new
-------
2016, 2017, [email protected]
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "parser.h"
#include "signal.h"
//#define COUNT (1000000 *5) // enables timing info
#ifdef COUNT
#include <time.h>
clock_t startm, stopm;
#define START if((startm = clock()) == -1){ printf("Error calling clock"); exit(1); }
#define STOP if((stopm = clock()) == -1){ printf("Error calling clock"); exit(1); }
#define PRINTTIME printf( "%6.3f seconds", ((double)stopm - startm) /CLOCKS_PER_SEC);
#endif
static void change(ctx * const ctx, s8 *i)
{
u8 *p, *d;
while(*i >= 0)
{
p = ctx->word + *i;
d = ctx->idx[(u8)*i];
//DPRINTF("change word[%d] : %c -> @%p : %d items\n", *i, *p, d, d[0]);
//scan(&d[1], &d[0], DUMP, NULL); // report
if(*p == *(d + d[0])) // if p is the last in charset
{
*p = d[1]; // change to first one
*i -= 1;
}
else
{
s8 r = scan(&d[1], &d[0], FIND, p); // find p in charset
//DPRINTF("find %#2x: r=%d -> %c\n", *p, r, *(d + r + 1));
*p = *(d + r + 1); // change to next one
break;
}
}
p = d = NULL;
}
int main(int argc, char **argv)
{
DPRINTF("[I] DEBUG build\n");
/* working context init */
ctx job;
job.mode = CHAR;
job.word = NULL;
job.idx = NULL;
job.numw = 0;
job.wlen = 0;
job.out_m = DRY_RUN; // default
job.work = parse_opt(argc, argv, &job);
DPRINTF("parse_opt()\tret:%d\n", job.work);
if(job.work)
{
cleanup(&job); exit(EXIT_FAILURE);
}
job.work = parse_file(&job);
DPRINTF("parse_file()\tret:%d\n", job.work);
if(job.work)
{
fprintf(stderr, "[E] Please recheck and pass a valid config file with -c\n");
cleanup(&job); exit(EXIT_FAILURE);
}
u8 *p = NULL;
if(1) // for verbose
{
#ifdef DEBUG
DPRINTF("Report from main, mode %u\n", job.mode);
for(u8 i = 0; i < job.wlen; i++)
{
p = job.idx[i];
DPRINTF("idx %2d/%.2d @%p:\t%d items\n", i, job.wlen, p, p[0]);
scan(&p[1], &p[0], HEXDUMP, NULL);
}
DPRINTF("%zub %zub\n", sizeof(ctx), sizeof(void*));
DPRINTF("[I] Config passed, report data matrix:\n");
#endif
}
/* report data matrix */
if(job.out_m == DRY_RUN)
{
if(job.numw) fprintf(stderr, "[I] Requested %u words!\n", job.numw);
dump_matrix(&job);
// in this case word is moved into the last one!
cleanup(&job); exit(0);
}
/* catch signals */
setup_signals(&job);
/* main process starts here */
p = job.word;
s8 n = job.wlen -1;
u32 c = 1;
while(1) // break it to exit(DONE)
{
#ifdef COUNT
if(c == 1) START;
if(c %COUNT == 0) // output only every COUNT attempt
#endif
{
switch(job.out_m)
{
case BIN: bin2stdout(&job); break; /* bin to STDOUT, mode based */
default: scan(p, &job.wlen, PRINT, NULL); break; /* standard output, mode based */
}
#ifdef COUNT // print timing info
{
STOP;
PRINTTIME;
printf(" [%.2f/sec]", COUNT /(((double)stopm - startm) /CLOCKS_PER_SEC));
START;
}
#endif
switch(job.out_m)
{
case WORDLIST: printf("\n"); break; /* one-per-line output */
case QUIET: printf("\r"); break; /* on-same-line output */
}
}
if(job.numw && job.numw == c) break;
if(job.work == DUMP) {
DPRINTF("\nReceived SIGUSR1\n"); dump_matrix(&job); // -USR1 output trigger
}
else if(job.work == INTR) {
DPRINTF("\nReceived SIGINT\n"); break;
}
change(&job, &n);
if(n < 0) break; // after that, we start increase word lenght!
/*
compute which one have to change and eventually continue
something like n = find(word);
*/
n = job.wlen -1; // reset n to rightmost one
c++; // and keep count
}
fflush(stdout), job.work = DONE;
#ifndef DEBUG
if(job.out_m == QUIET)
#endif
fprintf(stderr, "\nForged [%u] combinations\n", c);
cleanup(&job);
p = NULL;
return 0;
}