Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.2.9 devel #11

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 28 additions & 40 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "parser.h"
#include "signal.h"


//#define COUNT (1000000 *5) // enables timing info


Expand All @@ -19,7 +18,7 @@
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);
#define PTIME printf( "%6.3f seconds", ((double)stopm - startm) /CLOCKS_PER_SEC);
#endif

static void change(ctx * const ctx, s8 *i)
Expand Down Expand Up @@ -65,44 +64,37 @@ int main(int argc, char **argv)
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)
for(u8 i = 0; i < 2; i++) // do two checks
{
fprintf(stderr, "[E] Please recheck and pass a valid config file with -c\n");
cleanup(&job); exit(EXIT_FAILURE);
switch(i)
{
case 0: job.work = parse_opt(argc, argv, &job); break;
case 1: job.work = parse_file(&job); break;
}
if(job.work) { 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++)
{
#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
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!
dump_matrix(&job); // in this case word is moved into the last one!

cleanup(&job); exit(0);
}

Expand All @@ -121,26 +113,22 @@ int main(int argc, char **argv)
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 */
}
if(job.out_m == BIN)
bin2stdout(&job); /* bin to STDOUT, mode based */
else
scan(p, &job.wlen, PRINT, stdout); /* 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.out_m == WORDLIST) printf("\n"); /* one-per-line output */
else
if(job.out_m == QUIET) printf("\r"); /* on-same-line output */
}

if(job.numw && job.numw == c) break;
Expand All @@ -156,7 +144,7 @@ int main(int argc, char **argv)

if(n < 0) break; // after that, we start increase word lenght!
/*
compute which one have to change and eventually continue
Compute which one have to change and eventually continue
something like n = find(word);
*/
n = job.wlen -1; // reset n to rightmost one
Expand Down
35 changes: 23 additions & 12 deletions parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static u8 *_x_to_u8_buffer(const char *hex)
}


// wrapper to bin write to STDOUT
// Wrapper to bin write to STDOUT
void bin2stdout(ctx * const p)
{
ssize_t n = write(STDOUT_FILENO, p->word, p->wlen);
Expand All @@ -57,7 +57,7 @@ void bin2stdout(ctx * const p)
}


// actually we resume on parse_file()
// Actually we resume on parse_file()
static u8 resume(ctx * const p)
{
u8 res = 0;
Expand Down Expand Up @@ -92,7 +92,7 @@ static u8 resume(ctx * const p)
}


// actually we save on cleanup()
// Actually we save on cleanup()
static size_t save(ctx * const p)
{
FILE *fp = fopen(FILESAVE, "w");
Expand All @@ -106,7 +106,7 @@ static size_t save(ctx * const p)
}


// wrapper to release allocated ctx memory
// Wrapper to safely release allocated ctx memory
void cleanup(ctx * const p)
{
/* save last generated, to resume on next run
Expand Down Expand Up @@ -243,7 +243,12 @@ s8 parse_opt(int argc, char **argv, ctx *ctx)
return 0;
}


/*
Scan modes:
PRINT and HEXDUMP can switch STDOUT/STDERR via *dst
MARK_ONE and MARK_ALL prints to STDOUT
IS_HEX, FIND and COUNT does not prints but returns values
*/
s8 scan(const u8 *item, const u8 *l, const u8 smode, const u8 *dst)
{
const u8 *p = item;
Expand All @@ -254,8 +259,8 @@ s8 scan(const u8 *item, const u8 *l, const u8 smode, const u8 *dst)
switch(smode)
{
case PRINT:
if(opmode == CHAR) printf("%c", *p);
else printf("%.2x", *p);
if(opmode == CHAR) fprintf(dst, "%c", *p);
else fprintf(dst, "%.2x", *p);
break;

case IS_HEX: if(!isxdigit(*p)) return i;
Expand Down Expand Up @@ -297,13 +302,19 @@ s8 scan(const u8 *item, const u8 *l, const u8 smode, const u8 *dst)
}


/* Marked matrix dumper
used just on DRY_RUN, or triggered by -USR1 signal */
/*
Marked matrix dumper
used just on DRY_RUN, or triggered by -USR1 signal
This can be used by bash redirection, for example:
$ ./bf -c test/test_3.HEX -x -q 2>.bf.stderr
then:
$ cat .bf.stderr
*/
void dump_matrix(ctx * const p)
{
fflush(stderr);
fflush(stdout);

scan(p->word, &p->wlen, PRINT, NULL); puts(""); // report current
scan(p->word, &p->wlen, PRINT, stderr); fprintf(stderr, "\n"); // report current

// setup a bounder line
size_t max = sizeof(char) * p->wlen *3;
Expand Down Expand Up @@ -348,7 +359,7 @@ void dump_matrix(ctx * const p)
{
//DPRINTF("%p %p %p\n", p, p->word, &p->word);
free(p->word), p->word = t2;
scan(p->word, &p->wlen, PRINT, NULL); puts(""); // report last
scan(p->word, &p->wlen, PRINT, stderr); fprintf(stderr, "\n"); // report last
}
else free(t2), t2 = NULL;

Expand Down
2 changes: 1 addition & 1 deletion parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
bf_new definitions
*/

#define VERSION "0.2.8"
#define VERSION "0.2.9-dev"
#define FILESAVE ".bf.save"

#define MAX_ELEM (256 /8)
Expand Down
12 changes: 4 additions & 8 deletions signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@ static ctx *p = NULL;
}*/


static void sig_handler(int signo) // use p to access data
static void sig_handler(int signo) // use p to access whole ctx data
{
// uses -USR1
if(signo == SIGUSR1) p->work = DUMP; // dump the ctx, marking current item in charsets

// grab Ctrl-c
if(signo == SIGINT ) p->work = INTR;
}

Expand All @@ -33,9 +30,8 @@ void setup_signals(ctx *ctx)
{
p = ctx; // address p

if(ctx->out_m == QUIET) fprintf(stderr, "send 'kill -USR1 %d' from another terminal to dump\n", getpid());

if(signal(SIGUSR1, sig_handler) == SIG_ERR) fprintf(stderr, "\ncan't catch SIGUSR1\n");
if(ctx->out_m == QUIET) fprintf(stderr, "Send 'kill -USR1 %d' from another terminal to dump\n", getpid());

if(signal(SIGINT, sig_handler) == SIG_ERR) fprintf(stderr, "\ncan't catch SIGINT\n");
if(signal(SIGUSR1, sig_handler) == SIG_ERR) fprintf(stderr, "\nCan't catch SIGUSR1\n");
if(signal(SIGINT, sig_handler) == SIG_ERR) fprintf(stderr, "\nCan't catch SIGINT\n");
}