Compare commits

...

6 commits

Author SHA1 Message Date
snow flurry be27cccf45 config: fix some glaring errors...
... like not properly initializing the config struct. Sigh.
2020-11-16 20:56:25 -08:00
snow flurry 6e9a68711d config: "line ends early" is now a fatal error 2020-11-16 20:31:42 -08:00
snow flurry 50f3806f15 dosfs: get_byte_offset requires an unsigned offset
No reason for it to be negative, and gcc -Wextra complains when
it's signed.
2020-11-16 20:30:41 -08:00
snow flurry c8494b86bd config: multiple parser fixes
Still need to confirm it actually parses, but we're getting there?
2020-11-16 19:46:51 -08:00
snow flurry e7b8a47e49 debug: remove some parentheses 2020-11-16 19:42:27 -08:00
snow flurry c35c6f0389 config: it compiles???
broken though
2020-11-16 19:25:07 -08:00
4 changed files with 124 additions and 51 deletions

View file

@ -1,14 +1,17 @@
#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, t ":%s:%d: " x "\n", path, line)
#define PMSG(t, x) fprintf(stderr, "%s:%d:%d: " t ": " x "\n", path, line, col)
#define BUF_STORE(name) { \
if (i >= 10) { \
PMSG("error", name " is too big"); \
#define BUF_STORE(name, len) { \
if (i >= len) { \
PMSG("error", name " is too long"); \
err = 1; \
} else { \
ibuf[i] = ch; \
@ -16,11 +19,13 @@
} \
}
static int parse_conf_opts(struct conf_opts_t *, char *);
config_t *
parse_config(char *path)
{
FILE *fd;
int res, state, line, ws, comment, err, i;
int state, line, ws, comment, err, i, col;
#define ST_FILENAME 0
#define ST_EXT 1
#define ST_START 2
@ -29,7 +34,7 @@ parse_config(char *path)
#define ST_SKIP_B 5
#define ST_OPTS 6
char ch;
char ibuf[11];
char ibuf[32];
config_t *conf, *cur;
fd = fopen(path, "r");
@ -38,75 +43,100 @@ parse_config(char *path)
return NULL;
}
conf = malloc(sizeof(config_t));
conf = calloc(1, sizeof(config_t));
if (conf == NULL) {
perror("couldn't allocate config");
return NULL;
}
memset(conf, 0, sizeof(config_t));
state = ST_FILENAME;
line = 1;
line = col = 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) {
i = 0;
DPRINTF(("i = %d\n", i));
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, ibuf+strlen(ibuf), 10);
cur->start = strtoul(ibuf, NULL, 10);
break;
case ST_END:
cur->end = strtoul(ibuf, ibuf+strlen(ibuf), 10);
cur->end = strtoul(ibuf, NULL, 10);
break;
case ST_SKIP_A:
cur->skip_a = strtoul(ibuf, ibuf+strlen(ibuf), 10);
cur->skip_a = strtoul(ibuf, NULL, 10);
break;
case ST_SKIP_B:
cur->skip_b = strtoul(ibuf, ibuf+strlen(ibuf), 10);
cur->skip_b = strtoul(ibuf, NULL, 10);
break;
/* TODO: probably handle ST_OPTS here? */
}
memset(ibuf, 0, 11);
i = 0;
memset(ibuf, 0, 32);
}
break;
case '\r': /* dos line end */
(void)getc(fd);
/* FALLTHROUGH */
case '\n':
cur->next = malloc(sizeof(config_t));
if (cur == NULL) {
perror("couldn't allocate config");
err = 1;
break;
}
/*
* 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));
if (cur == NULL) {
perror("couldn't allocate config");
err = 1;
break;
}
if (state != ST_OPTS) {
PMSG("warn", "line ends early")
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;
}
/* reset state for next line */
state = ST_FILENAME;
cur = cur->next;
memset(cur, 0, sizeof(config_t));
ws = i = comment = 0;
ws = col = i = 0;
comment = 0;
line++;
break;
case '#':
comment = 1;
break;
default:
if (comment)
if (comment) {
continue;
}
ws = 0;
@ -117,7 +147,7 @@ parse_config(char *path)
PMSG("error", "filename must be less than eight characters");
err = 1;
} else {
cur->filename[i] = ch;
cur->filename[i] = toupper(ch);
i++;
}
break;
@ -126,20 +156,25 @@ parse_config(char *path)
PMSG("error", "ext must be less than three characters");
err = 1;
} else {
cur->ext[i] = ch;
cur->ext[i] = toupper(ch);
i++;
}
break;
case ST_START:
BUF_STORE("start byte")
BUF_STORE("start byte", 10);
break;
case ST_END:
BUF_STORE("end byte")
BUF_STORE("end byte", 10);
break;
case ST_SKIP_A:
BUF_STORE("first byte skip")
BUF_STORE("first byte skip", 10);
break;
case ST_SKIP_B:
BUF_STORE("second byte skip")
BUF_STORE("second byte skip", 10);
break;
case ST_OPTS:
/* TODO */
BUF_STORE("option string", 31);
break;
default:
PMSG("error", "too many columns");
err = 1;
@ -148,6 +183,8 @@ parse_config(char *path)
if (err)
break;
col++;
}
@ -160,6 +197,42 @@ 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, int f_offset)
get_byte_offset(dosfs_t *fsd, dosfile_t *file, unsigned 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, 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 *, int);
int get_byte_offset(dosfs_t *, dosfile_t *, unsigned int);
#endif /* !_FAT12_H */