/* if i put this garbage on the stack instead of bss, it will segfault. */ size_t n_exprs = 0; regex_t exprs[MAX_EXPRS];
void usage(void) { fprintf(stderr, "usage: tripex [-Ef:hil:n:m:s:v] expressions\n" " -E\tuse extended regular expressions\n" " -f\tread expressions from file (use '-' for stdin)\n" " -h\tshow usage\n" " -i\tignore case\n" " -l\tkey length\n" " -n\tnumber of threads\n" " -m\tmax number of tries (per thread!)\n" " -s\tstack (logical AND) expressions before/after expr #n (negative for before)\n"); }
void appendExpr(regex_t exprs[], size_t *i, char *str, int re_flags) { /* avoiding malloc like the plague, simple and fast shitware! * if you somehow need more than MAX_EXPRS, change it & recompile. */ if (*i >= MAX_EXPRS) errx(EXIT_FAILURE, "no %s-kun! it's too many exprs, it wont fit!", getlogin()); if (regcomp(&exprs[*i], str, re_flags)) errx(EXIT_FAILURE, "failed to compile regex '%s'", str); (*i)++; }
int main(int argc, char *argv[]) { int re_flags = 0; int sflag = 0; long stack_after = 0; size_t n_procs = 1; size_t key_len = 10; size_t max_tries = 0;
int opt; while ((opt = getopt(argc, argv, "E:f:hil:m:M:n:s:v")) != -1) { switch(opt) { case 'E': re_flags |= REG_EXTENDED; break; case 'f': FILE *fp = stdin; if (strcmp(optarg, "-") != 0) fp = fopen(optarg, "r"); if (!fp) err(EXIT_FAILURE, "couldn't open regex file '%s'", optarg);
free(line); fclose(fp); break; case 'i': re_flags |= REG_ICASE; break; case 'l': key_len = atol(optarg); if (key_len < 1) errx(EXIT_FAILURE, "key length must be at least 1"); break; case 'n': n_procs = atol(optarg); if (n_procs < 1) errx(EXIT_FAILURE, "need at least one process"); break; case 'm': /* ! PER THREAD ! */ max_tries = atol(optarg); if (max_tries < 1) errx(EXIT_FAILURE, "need at least one try"); break; case 's': sflag = 1; stack_after = atol(optarg); break; case 'v': printf("tripex 1.2 by taocana\n"); return EXIT_SUCCESS; case 'h': /* FALLTHROUGH */ default: usage(); return EXIT_FAILURE; } }
argc -= optind; argv += optind;
for (size_t i = 0; i < argc; i++) appendExpr(exprs, &n_exprs, argv[i], re_flags);
if (n_exprs < 1) { usage(); errx(EXIT_FAILURE, "need at least one expression"); }
for (int i = 1; i < n_procs; i++) if (!fork()) break;
/* we must get printable ascii from a random byte, * but 256 % 94 = 68 which leaves too much skew. * quantizing into 64 chars won't skew at all, but * it only has 6 shannons of entropy per character. * an 85 character quantization only skews by a * remainder of one, which is miniscule enough and * we average 6.41 shannons per character. */ static unsigned char rngFilter[256] = "!#$%&'()*+,-./012" "3456789:;<=>?@ABC" "DEFGHIJKLMNOPQRST" "UVWXYZ^abcdefghij" "klmnopqrstuvwxyz~" "!#$%&'()*+,-./012" "3456789:;<=>?@ABC" "DEFGHIJKLMNOPQRST" "UVWXYZ^abcdefghij" "klmnopqrstuvwxyz~" "!#$%&'()*+,-./012" "3456789:;<=>?@ABC" "DEFGHIJKLMNOPQRST" "UVWXYZ^abcdefghij" "klmnopqrstuvwxyz~" "~";