Compare commits

..

No commits in common. "be27cccf45940bdb369a500ca8db49b3421c0741" and "dccbb407985bbe7c01a89165812940fade8e3233" have entirely different histories.

4 changed files with 51 additions and 124 deletions

View file

@ -1,17 +1,14 @@
#include <ctype.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"
#include "debug.h"
#define PMSG(t, x) fprintf(stderr, "%s:%d:%d: " t ": " x "\n", path, line, col)
#define PMSG(t, x) fprintf(stderr, t ":%s:%d: " x "\n", path, line)
#define BUF_STORE(name, len) { \
if (i >= len) { \
PMSG("error", name " is too long"); \
#define BUF_STORE(name) { \
if (i >= 10) { \
PMSG("error", name " is too big"); \
err = 1; \
} else { \
ibuf[i] = ch; \
@ -19,13 +16,11 @@
} \
}
static int parse_conf_opts(struct conf_opts_t *, char *);
config_t *
parse_config(char *path)
{
FILE *fd;
int state, line, ws, comment, err, i, col;
int res, state, line, ws, comment, err, i;
#define ST_FILENAME 0
#define ST_EXT 1
#define ST_START 2
@ -34,7 +29,7 @@ parse_config(char *path)
#define ST_SKIP_B 5
#define ST_OPTS 6
char ch;
char ibuf[32];
char ibuf[11];
config_t *conf, *cur;
fd = fopen(path, "r");
@ -43,66 +38,52 @@ parse_config(char *path)
return NULL;
}
conf = calloc(1, sizeof(config_t));
conf = malloc(sizeof(config_t));
if (conf == NULL) {
perror("couldn't allocate config");
return NULL;
}
memset(conf, 0, sizeof(config_t));
state = ST_FILENAME;
line = col = 1;
line = 1;
ws = comment = err = 0;
i = 0;
cur = conf;
while ((ch = getc(fd)) != EOF) {
switch (ch) {
case '\t':
case ' ':
if (comment) {
if (comment)
continue;
}
/* only run on the first whitespace */
if (!ws) {
DPRINTF(("i = %d\n", i));
i = 0;
ws = 1;
state++;
DPRINTF(("%s:%d:%d: switching to state %d\n", path, line, col, state));
switch (state-1) {
case ST_FILENAME:
cur->filename[i] = 0;
break;
case ST_EXT:
cur->ext[i] = 0;
break;
/* convert offsets to uint32_t (or close enough) */
switch (state) {
case ST_START:
cur->start = strtoul(ibuf, NULL, 10);
cur->start = strtoul(ibuf, ibuf+strlen(ibuf), 10);
break;
case ST_END:
cur->end = strtoul(ibuf, NULL, 10);
cur->end = strtoul(ibuf, ibuf+strlen(ibuf), 10);
break;
case ST_SKIP_A:
cur->skip_a = strtoul(ibuf, NULL, 10);
cur->skip_a = strtoul(ibuf, ibuf+strlen(ibuf), 10);
break;
case ST_SKIP_B:
cur->skip_b = strtoul(ibuf, NULL, 10);
cur->skip_b = strtoul(ibuf, ibuf+strlen(ibuf), 10);
break;
/* TODO: probably handle ST_OPTS here? */
}
i = 0;
memset(ibuf, 0, 32);
memset(ibuf, 0, 11);
}
break;
case '\r': /* dos line end */
(void)getc(fd);
/* FALLTHROUGH */
case '\n':
/*
* Only start a new struct if we didn't have a comment at the
* beginning of a line.
*/
if (state != ST_FILENAME || !comment) {
cur->next = calloc(1, sizeof(config_t));
cur->next = malloc(sizeof(config_t));
if (cur == NULL) {
perror("couldn't allocate config");
err = 1;
@ -110,33 +91,22 @@ parse_config(char *path)
}
if (state != ST_OPTS) {
PMSG("error", "line ends early");
err = 1;
break;
} else {
DPRINTF(("filename: %s.%s\n", cur->filename, cur->ext));
if (parse_conf_opts(&cur->opts, ibuf) != 0) {
PMSG("error", "unable to parse opts (see above)");
err = 1;
}
}
cur = cur->next;
PMSG("warn", "line ends early")
}
/* reset state for next line */
state = ST_FILENAME;
ws = col = i = 0;
comment = 0;
cur = cur->next;
memset(cur, 0, sizeof(config_t));
ws = i = comment = 0;
line++;
break;
case '#':
comment = 1;
break;
default:
if (comment) {
if (comment)
continue;
}
ws = 0;
@ -147,7 +117,7 @@ parse_config(char *path)
PMSG("error", "filename must be less than eight characters");
err = 1;
} else {
cur->filename[i] = toupper(ch);
cur->filename[i] = ch;
i++;
}
break;
@ -156,25 +126,20 @@ parse_config(char *path)
PMSG("error", "ext must be less than three characters");
err = 1;
} else {
cur->ext[i] = toupper(ch);
cur->ext[i] = ch;
i++;
}
break;
case ST_START:
BUF_STORE("start byte", 10);
break;
BUF_STORE("start byte")
case ST_END:
BUF_STORE("end byte", 10);
break;
BUF_STORE("end byte")
case ST_SKIP_A:
BUF_STORE("first byte skip", 10);
break;
BUF_STORE("first byte skip")
case ST_SKIP_B:
BUF_STORE("second byte skip", 10);
break;
BUF_STORE("second byte skip")
case ST_OPTS:
BUF_STORE("option string", 31);
break;
/* TODO */
default:
PMSG("error", "too many columns");
err = 1;
@ -183,8 +148,6 @@ parse_config(char *path)
if (err)
break;
col++;
}
@ -197,42 +160,6 @@ parse_config(char *path)
return conf;
}
#define STORE_VAL(name, var, lim) { \
val = strtoul(cur+4, NULL, 10); \
if (val > lim) { \
fprintf(stderr, "err: " name " value out of bounds\n"); \
return 1; \
} else { \
var = val; \
} \
}
/*
* Parses the options column of the config, ex.
* "xor=10,add=5,shl=2"
*/
static int
parse_conf_opts(struct conf_opts_t *opts, char *parse)
{
char *cur, *last;
unsigned int val;
for ((cur = strtok_r(parse, ",", &last)); cur;
(cur = strtok_r(NULL, ",", &last))) {
if (!strncmp(cur, "xor=", 4)) {
STORE_VAL("xor", opts->bit_xor, 0xff);
} else if (!strncmp(cur, "add=", 4)) {
STORE_VAL("add", opts->add, 0xff);
} else if (!strncmp(cur, "shr=", 4)) {
STORE_VAL("shr", opts->shr, 8);
} else if (!strncmp(cur, "shl=", 4)) {
STORE_VAL("shl", opts->shl, 8);
}
}
return 0;
}
void
free_config(config_t *conf)
{

View file

@ -2,7 +2,7 @@
#define _DEBUG_H
#ifdef DEBUG
#define DPRINTF(x) printf x
#define DPRINTF(x) printf(x)
#else
#define DPRINTF(x)
#endif

View file

@ -35,13 +35,13 @@ open_image(const char *path, int flags, dosfs_t *fsd)
}
if (fsd->ib.jmp[0] != 0xEB || fsd->ib.jmp[2] != 0x90) {
DPRINTF(("hm... doesn't look like a FAT image?\n"));
DPRINTF("hm... doesn't look like a FAT image?\n");
close(ifd);
return EOPNOTSUPP;
}
if (fsd->ib.sect_ct == 0 || fsd->ib.fat_size == 0) {
DPRINTF(("sect_ct or fat_size == 0, likely fat>32 (unsupported)\n"));
DPRINTF("sect_ct or fat_size == 0, likely fat>32 (unsupported)\n");
close(ifd);
return EOPNOTSUPP;
}
@ -57,7 +57,7 @@ open_image(const char *path, int flags, dosfs_t *fsd)
} else if (fsd->data_size < FAT16_MAX_CLUSTERS) {
fsd->fs_ver = 16;
} else {
DPRINTF(("data_size >= FAT12_MAX_CLUSTERS, likely fat>32 (unsupported)\n"));
DPRINTF("data_size >= FAT12_MAX_CLUSTERS, likely fat>32 (unsupported)\n");
close(ifd);
return EOPNOTSUPP;
}
@ -93,7 +93,7 @@ dos_listdir(dosfs_t *fsd, unsigned int offset)
start = malloc(sizeof(dosfile_t));
if (start == NULL) {
DPRINTF(("malloc failed\n"));
DPRINTF("malloc failed\n");
return NULL;
}
@ -107,14 +107,14 @@ dos_listdir(dosfs_t *fsd, unsigned int offset)
res = reallocarr(&dirs, root_ents, sizeof(struct dos_dirent));
if (res == -1) {
DPRINTF(("reallocarr failed\n"));
DPRINTF("reallocarr failed\n");
return NULL;
}
res = pread(fsd->ifd, dirs, (root_ents * sizeof(struct dos_dirent)),
offset);
if (res == -1) {
DPRINTF(("pread failed\n"));
DPRINTF("pread failed\n");
return NULL;
}
@ -128,7 +128,7 @@ dos_listdir(dosfs_t *fsd, unsigned int offset)
if (i > 0) {
cur->next = malloc(sizeof(dosfile_t));
if (cur->next == NULL) {
DPRINTF(("inner malloc failed!?\n"));
DPRINTF("inner malloc failed!?\n");
res = -1;
break;
}
@ -199,7 +199,7 @@ read_fat_chain(dosfs_t *fsd, unsigned int first, int *length)
len++;
res = reallocarr(&chain, len, sizeof(unsigned int));
if (res == -1) {
DPRINTF(("reallocarr failed\n"));
DPRINTF("reallocarr failed\n");
free(chain);
return NULL;
}
@ -211,7 +211,7 @@ read_fat_chain(dosfs_t *fsd, unsigned int first, int *length)
/* XXX: endianness */
res = pread(fsd->ifd, &next, 2, byte_off);
if (res == -1) {
DPRINTF(("pread failed\n"));
DPRINTF("pread failed\n");
free(chain);
return NULL;
}
@ -243,12 +243,12 @@ read_fat_chain(dosfs_t *fsd, unsigned int first, int *length)
* offset, i.e. one that can be used with lseek()/pread().
*/
int
get_byte_offset(dosfs_t *fsd, dosfile_t *file, unsigned int f_offset)
get_byte_offset(dosfs_t *fsd, dosfile_t *file, int f_offset)
{
int coff, csz;
if (f_offset > file->ent.size) {
DPRINTF(("offset requested is out of bounds!\n"));
DPRINTF("offset requested is out of bounds!\n");
return -EINVAL;
}
@ -259,7 +259,7 @@ get_byte_offset(dosfs_t *fsd, dosfile_t *file, unsigned int f_offset)
coff = f_offset / csz;
if (coff > file->fc_len) {
DPRINTF(("offset is within filesize, but not enough clusters??\n"));
DPRINTF("offset is within filesize, but not enough clusters??\n");
return -EINVAL;
}

View file

@ -80,6 +80,6 @@ void close_image(dosfs_t *);
dosfile_t *dos_listdir(dosfs_t *, unsigned int);
void dos_freedir(dosfile_t *);
int get_byte_offset(dosfs_t *, dosfile_t *, unsigned int);
int get_byte_offset(dosfs_t *, dosfile_t *, int);
#endif /* !_FAT12_H */