diff options
| -rw-r--r-- | Makefile | 8 | ||||
| -rw-r--r-- | src/cli.c | 10 | ||||
| -rw-r--r-- | src/cli.h | 2 | ||||
| -rw-r--r-- | src/compress.c | 2 | ||||
| -rw-r--r-- | src/encoding.c | 2 | ||||
| -rw-r--r-- | src/header.c | 29 | ||||
| -rw-r--r-- | test/test_cli.c | 24 | ||||
| -rw-r--r-- | test/test_cli.h | 2 | ||||
| -rw-r--r-- | test/test_run.c | 2 |
9 files changed, 55 insertions, 26 deletions
@@ -30,19 +30,19 @@ all: $(BINDIR)/nekopack $(BINDIR)/nekopack: $(OBJECTS) $(OBJDIR)/main.o @mkdir -p $(BINDIR) - $(LD) $(LDFLAGS) $(OBJECTS) $(OBJDIR)/main.o -o bin/nekopack + @$(LD) $(LDFLAGS) $(OBJECTS) $(OBJDIR)/main.o -o bin/nekopack $(BINDIR)/test: $(OBJECTS) $(TEST_OBJECTS) @mkdir -p $(BINDIR) - $(LD) $(LDFLAGS) $(OBJECTS) $(TEST_OBJECTS) -o $(BINDIR)/test + @$(LD) $(LDFLAGS) $(OBJECTS) $(TEST_OBJECTS) -o $(BINDIR)/test $(OBJDIR)/%.o: $(SRCDIR)/%.c @mkdir -p $(OBJDIR) - $(CC) $(CFLAGS) -c -o $@ $(SRCDIR)/$*.c + @$(CC) $(CFLAGS) -c -o $@ $(SRCDIR)/$*.c $(OBJDIR)/%.o: $(TSTDIR)/%.c @mkdir -p $(OBJDIR) - $(CC) $(CFLAGS) -c -o $@ -I $(SRCDIR) $(TSTDIR)/$*.c + @$(CC) $(CFLAGS) -c -o $@ -I $(SRCDIR) $(TSTDIR)/$*.c test: bin/test @bin/test @@ -33,8 +33,8 @@ static void parse_output_path(const char *optarg, struct params *p) { p->out = malloc(p->out_len + 2); strcpy(p->out, optarg); if (p->out[p->out_len - 1] != '/') { - p->out[p->out_len] = '/'; - p->out_len += 1; + p->out[p->out_len] = '/'; + p->out_len += 1; } } @@ -55,6 +55,12 @@ static void parse_game_id(const char *optarg, struct params *p) { /* Returns a params structure parsed from `argv`. */ struct params parse_args(int argc, char **argv) { + /* getopt maintains external state which needs to be reset each time + `parse_args` is called, otherwise strange things will occur. */ + optind = 1; + opterr = 1; + optopt = 63; + struct params p = {0}; if (argc < 2) { @@ -34,8 +34,8 @@ enum { /* Structure for storing options set from the command-line. */ struct params { bool verbose; /* Whether or not to output progress messages. */ - char *out; /* Path to extract files to. */ size_t out_len; /* Length of the output path string. */ + char *out; /* Path to extract files to. */ int mode; /* Current mode of operation. */ int game; /* Which encryption keys to use. */ int vararg_index; /* Start index of paths in argv. */ diff --git a/src/compress.c b/src/compress.c index f9fa8ff..578441b 100644 --- a/src/compress.c +++ b/src/compress.c @@ -43,7 +43,7 @@ struct stream *stream_inflate(struct stream *s, size_t len, strm.next_in = (Bytef *) s->_cur; do { strm.avail_out = decompressed_len; - strm.next_out = (Bytef *) n->_cur; + strm.next_out = (Bytef *) n->_cur; ret = inflate(&strm, Z_NO_FLUSH); if (ret == Z_STREAM_ERROR) { stream_free(n); diff --git a/src/encoding.c b/src/encoding.c index 23d38dc..e7af310 100644 --- a/src/encoding.c +++ b/src/encoding.c @@ -22,7 +22,7 @@ /* Wrapper for iconv, using the conversion specified by `conv`. */ static void convert(char *in_buf, char *out_buf, size_t len, iconv_t conv) { - size_t in_size = len, out_size = len; + size_t in_size = len, out_size = len; char *in_start = in_buf, *out_start = out_buf; iconv(conv, &in_start, &in_size, &out_start, &out_size); } diff --git a/src/header.c b/src/header.c index 6389817..82a181c 100644 --- a/src/header.c +++ b/src/header.c @@ -24,8 +24,18 @@ #include "header.h" #include "io.h" -static bool is_xp3(struct header *h); -static bool is_supported(struct header *h); + +/* Checks that the header contains the correct magic number. */ +static bool is_xp3(struct header *h) { + return !memcmp(h->magic, XP3_MAGIC, 11); +} + + +/* Checks that the archive's version is supported, and that it is marked + as compatible with the KiriKiriZ engine. */ +static bool is_supported(struct header *h) { + return h->version == 1 && h->flags & 0x80; +} /* Reads from the given stream into a newly allocated header structure. @@ -36,7 +46,7 @@ struct header *read_header(struct stream *s) { if (h == NULL) return NULL; /* The header structure can't be read into directly because of - potential alignment issues. */ + alignment issues. */ stream_read(h->magic, s, 11); stream_read(&h->info_offset, s, sizeof(uint64_t)); stream_read(&h->version, s, sizeof(uint32_t)); @@ -50,16 +60,3 @@ struct header *read_header(struct stream *s) { } return h; } - - -/* Checks that the header contains the correct magic number. */ -static bool is_xp3(struct header *h) { - return !memcmp(h->magic, XP3_MAGIC, 11); -} - - -/* Checks that the archive's version is supported, and that it is marked - as compatible with the KiriKiriZ engine. */ -static bool is_supported(struct header *h) { - return h->version == 1 && h->flags & 0x80; -} diff --git a/test/test_cli.c b/test/test_cli.c index 5a5f0db..00cc84e 100644 --- a/test/test_cli.c +++ b/test/test_cli.c @@ -22,11 +22,33 @@ #include "minunit.h" #include "cli.h" +#include "crypto.h" + +extern int tests_run; char *test_out_path(void) { - char *argv[4] = {"nekopack", "-o", "/tmp", "a.xp3"}; + char *argv[] = {"nekopack", "-o", "/tmp", "a.xp3"}; struct params p = parse_args(4, argv); mu_assert("Trailing slash not appended to path", !strcmp(p.out, "/tmp/")); + params_free(p); + return NULL; +} + + +char *test_vararg_index(void) { + char *argv[] = {"nekopack", "-l", "a.xp3"}; + struct params p = parse_args(3, argv); + mu_assert("Invalid vararg index", !strcmp("a.xp3", argv[p.vararg_index])); + params_free(p); + return NULL; +} + + +char *test_game_id(void) { + char *argv[] = {"nekopack", "-g", "nekopara_volume_1", "a.xp3"}; + struct params p = parse_args(4, argv); + mu_assert("Incorrect game ID", p.game == NEKOPARA_VOLUME_1); + params_free(p); return NULL; } diff --git a/test/test_cli.h b/test/test_cli.h index e580236..f16c50b 100644 --- a/test/test_cli.h +++ b/test/test_cli.h @@ -20,3 +20,5 @@ #pragma once char *test_out_path(void); +char *test_vararg_index(void); +char *test_game_id(void); diff --git a/test/test_run.c b/test/test_run.c index 04099d4..b63bcb5 100644 --- a/test/test_run.c +++ b/test/test_run.c @@ -33,6 +33,8 @@ int tests_run = 0; static char *run_all_tests(void) { mu_run_test(test_out_path); + mu_run_test(test_vararg_index); + mu_run_test(test_game_id); mu_run_test(test_derive_initial); mu_run_test(test_derive_primary); mu_run_test(test_stream_obj); |