fk98/src/main.c

176 lines
4 KiB
C

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "config.h"
#include "corrupt.h"
#include "dosfs.h"
static void usage(void) __dead;
static int list_img(char *);
static int do_corrupt(char *, config_t *, uint32_t);
int
main(int argc, char *argv[])
{
int ch, list_mode, res, preseed;
uint32_t seed;
char *cfg_path, *ep;
config_t *conf;
setprogname(argv[0]);
list_mode = 0;
seed = 0;
preseed = 0;
cfg_path = NULL;
while ((ch = getopt(argc, argv, "lc:s:")) != -1) {
switch (ch) {
case 'l':
list_mode = 1;
break;
case 'c':
cfg_path = optarg;
break;
case 's':
errno = 0;
if (optarg[0] == '0' && optarg[1] == 'x') {
// strtoul hex
optarg += 2;
seed = strtoul(optarg, &ep, 16);
} else {
seed = strtoul(optarg, &ep, 10);
}
if (errno) {
perror("couldn't parse seed parameter");
return EXIT_FAILURE;
} else if (optarg == ep || *ep != 0) {
fprintf(stderr, "defined seed is malformed\n");
return EXIT_FAILURE;
}
preseed = 1;
break;
case '?':
default:
usage();
/* NOTREACHED */
}
}
argc -= optind;
argv += optind;
if (argc != 1) {
usage();
/* NOTREACHED */
}
if (list_mode) {
return list_img(argv[0]);
}
if (!preseed) {
seed = time(NULL) & 0xffffffff;
}
printf("using seed 0x%x\n", seed);
if (cfg_path != NULL) {
conf = parse_config(cfg_path);
if (conf == NULL) {
fprintf(stderr, "failed to parse config\n");
return EXIT_FAILURE;
}
} else {
fprintf(stderr, "no default config implemented\n");
return EXIT_FAILURE;
}
res = do_corrupt(argv[0], conf, seed);
free_config(conf);
return res;
}
static int
list_img(char *path)
{
dosfs_t img = { 0 };
dosfile_t *rootdir, *cur;
int res;
res = open_image(path, O_RDONLY, &img);
if (res != 0) {
fprintf(stderr, "err: couldn't open image: %s\n", strerror(res));
return EXIT_FAILURE;
}
rootdir = dos_listdir(&img, img.root_loc);
if (rootdir == NULL) {
perror("couldn't read root dir");
close_image(&img);
return EXIT_FAILURE;
}
printf("OEM ID: %.8s\nDirectory listing:\n", img.ib.oem_id);
for (cur = rootdir; cur->next != NULL; cur = cur->next) {
printf(" %s.%s\n", cur->fname, cur->fext);
}
dos_freedir(rootdir);
rootdir = cur = NULL;
close_image(&img);
return EXIT_SUCCESS;
}
static int
do_corrupt(char *path, config_t *conf, uint32_t seed)
{
dosfs_t img = { 0 };
dosfile_t *rootdir, *cur;
config_t *match;
int res;
res = open_image(path, O_RDWR, &img);
if (res != 0) {
fprintf(stderr, "err: couldn't open image: %s\n", strerror(res));
return EXIT_FAILURE;
}
rootdir = dos_listdir(&img, img.root_loc);
if (rootdir == NULL) {
perror("couldn't read root dir");
close_image(&img);
return EXIT_FAILURE;
}
for (cur = rootdir; cur->next != NULL; cur = cur->next) {
match = match_rule(conf, cur);
if (match != NULL) {
printf("Corrupting %s.%s... (x%u a%u l%u r%u)\n",
cur->fname, cur->fext, match->opts.bit_xor,
match->opts.add, match->opts.shl, match->opts.shr);
res = corrupt_file(&img, cur, match, &seed);
if (res != 0) {
fprintf(stderr, "Corrupting file failed...\n");
}
}
}
close_image(&img);
return 0;
}
static void
usage(void)
{
fprintf(stderr, "usage: %s [-l] [-s seed] [-c config] image\n", getprogname());
exit(EXIT_FAILURE);
}