diff options
| author | jakob <jakob@memeware.net> | 2017-04-09 19:39:46 -0400 |
|---|---|---|
| committer | jakob <jakob@memeware.net> | 2017-04-09 19:39:46 -0400 |
| commit | 411a40b5c077ca69e34c7016428eb9d856f8fe73 (patch) | |
| tree | cff2ee6b0f6e4144a0041c53adfc8c542088781c | |
| parent | 8a1fcff16e9ba8674c72011da7da5ad1bc133168 (diff) | |
Automated style checking and fixed unit tests.
| -rw-r--r-- | .astylerc | 4 | ||||
| -rw-r--r-- | Makefile | 5 | ||||
| -rw-r--r-- | src/cli.c | 42 | ||||
| -rw-r--r-- | src/cli.h | 12 | ||||
| -rw-r--r-- | src/crypto.c | 48 | ||||
| -rw-r--r-- | src/crypto.h | 8 | ||||
| -rw-r--r-- | src/encoding.c | 2 | ||||
| -rw-r--r-- | src/header.h | 12 | ||||
| -rw-r--r-- | src/io.c | 28 | ||||
| -rw-r--r-- | src/main.c | 42 | ||||
| -rw-r--r-- | src/table.c | 50 | ||||
| -rw-r--r-- | test/test_cli.c | 2 |
12 files changed, 131 insertions, 124 deletions
diff --git a/.astylerc b/.astylerc new file mode 100644 index 0000000..319a751 --- /dev/null +++ b/.astylerc @@ -0,0 +1,4 @@ +--style=java \ + --indent-preproc-block \ + --pad-oper --pad-comma --pad-header --unpad-paren \ + --align-pointer=name @@ -50,4 +50,7 @@ test: bin/test clean: rm -f bin/* obj/* -.PHONY: clean test +style: + astyle -n --recursive "src/*.c" "src/*.h" "test/*.c" "test/*.h" + +.PHONY: clean test style @@ -79,27 +79,27 @@ struct params parse_args(int argc, char **argv) { count++; cur = getopt_long(argc, argv, "hVvelqo:g:", long_opts, &opt_index); switch (cur) { - case 'h': - p.mode = HELP; - return p; - case 'V': - p.mode = VERSION; - return p; - case 'v': - p.verbose = true; - break; - case 'o': - count++; - parse_output_path(optarg, &p); - break; - case 'g': - count++; - parse_game_id(optarg, &p); - case 'e': - p.mode = EXTRACT; - break; - case 'l': - p.mode = LIST; + case 'h': + p.mode = HELP; + return p; + case 'V': + p.mode = VERSION; + return p; + case 'v': + p.verbose = true; + break; + case 'o': + count++; + parse_output_path(optarg, &p); + break; + case 'g': + count++; + parse_game_id(optarg, &p); + case 'e': + p.mode = EXTRACT; + break; + case 'l': + p.mode = LIST; } } while (cur >= 0); @@ -33,12 +33,12 @@ enum { /* Structure for storing options set from the command-line. */ struct params { - int mode; /* Current mode of operation. */ - int game; /* Which encryption keys to use. */ - bool verbose; /* Whether or not to output progress messages. */ - int vararg_index; /* Start index of paths in argv. */ - char *out; /* Path to extract files to. */ - size_t out_len; /* Length of the output path string. */ + 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. */ + int mode; /* Current mode of operation. */ + int game; /* Which encryption keys to use. */ + int vararg_index; /* Start index of paths in argv. */ }; /* Returns a params structure parsed from `argv`. */ diff --git a/src/crypto.c b/src/crypto.c index 92c4032..5a91f39 100644 --- a/src/crypto.c +++ b/src/crypto.c @@ -28,30 +28,30 @@ struct game_key get_key(int game) { struct game_key k = {0}; switch (game) { - case NEKOPARA_VOLUME_0: - k.master = 0x1548e29c; - k.fallback_initial = 0x9c; - k.fallback_primary = 0xd7; - k.uses_initial = true; - break; - case NEKOPARA_VOLUME_0_STEAM: - k.master = 0x44528b87; - k.fallback_initial = 0x87; - k.fallback_primary = 0x23; - k.uses_initial = true; - break; - case NEKOPARA_VOLUME_1: - k.master = 0x1548e29c; - k.fallback_initial = 0x00; - k.fallback_primary = 0xd7; - k.uses_initial = false; - break; - case NEKOPARA_VOLUME_1_STEAM: - k.master = 0x44528b87; - k.fallback_initial = 0x00; - k.fallback_primary = 0x23; - k.uses_initial = false; - break; + case NEKOPARA_VOLUME_0: + k.master = 0x1548e29c; + k.fallback_initial = 0x9c; + k.fallback_primary = 0xd7; + k.uses_initial = true; + break; + case NEKOPARA_VOLUME_0_STEAM: + k.master = 0x44528b87; + k.fallback_initial = 0x87; + k.fallback_primary = 0x23; + k.uses_initial = true; + break; + case NEKOPARA_VOLUME_1: + k.master = 0x1548e29c; + k.fallback_initial = 0x00; + k.fallback_primary = 0xd7; + k.uses_initial = false; + break; + case NEKOPARA_VOLUME_1_STEAM: + k.master = 0x44528b87; + k.fallback_initial = 0x00; + k.fallback_primary = 0x23; + k.uses_initial = false; + break; } return k; } diff --git a/src/crypto.h b/src/crypto.h index e80531d..c5417c7 100644 --- a/src/crypto.h +++ b/src/crypto.h @@ -33,10 +33,10 @@ enum { /* Structure containing game-specific encryption information. */ struct game_key { - bool uses_initial; /* Whether or not to use an initial key. */ - uint32_t master; /* Master key specific to the game. */ - uint8_t fallback_initial; /* Fallback if initial key is 0. */ - uint8_t fallback_primary; /* Fallback if primary key is 0. */ + bool uses_initial; /* Whether or not to use an initial key. */ + uint32_t master; /* Master key specific to the game. */ + uint8_t fallback_initial; /* Fallback if initial key is 0. */ + uint8_t fallback_primary; /* Fallback if primary key is 0. */ }; /* Returns the game_key structure for a given game identifier, as diff --git a/src/encoding.c b/src/encoding.c index d50e503..23d38dc 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.h b/src/header.h index 19f7fcd..8ccf930 100644 --- a/src/header.h +++ b/src/header.h @@ -27,12 +27,12 @@ /* Structure representing the header section of an XP3 archive. */ struct header { - char magic[11]; /* Identifier for the archive. */ - uint64_t info_offset; /* Offset to `table_size`. */ - uint32_t version; /* Raw value containing the archive version. */ - uint64_t table_size; /* The size of the table section. (?) */ - uint8_t flags; /* A flags variable for the archive. (?) */ - uint64_t table_offset; /* Offset to the archive table. */ + char magic[11]; /* Identifier for the archive. */ + uint64_t info_offset; /* Offset to `table_size`. */ + uint32_t version; /* Raw value containing the archive version. */ + uint64_t table_size; /* The size of the table section. (?) */ + uint8_t flags; /* A flags variable for the archive. (?) */ + uint64_t table_offset; /* Offset to the archive table. */ }; /* Reads data from the given stream into a newly allocated header @@ -67,8 +67,8 @@ struct stream *stream_from_file(char *path) { stream, as well as the stream structure itself. */ void stream_free(struct stream *s) { switch (s->_loc) { - case HEAP: - free(s->_start); + case HEAP: + free(s->_start); } free(s); } @@ -94,10 +94,10 @@ void stream_write(struct stream *s, void *src, size_t n) { if (s->_cur + n > s->_start + s->len) { ptrdiff_t dist = (uintptr_t) s->_cur - (uintptr_t) s->_start; switch(s->_loc) { - case HEAP: - s->len *= 2; - s->_start = realloc(s->_start, s->len); - s->_cur = s->_start + dist; + case HEAP: + s->len *= 2; + s->_start = realloc(s->_start, s->len); + s->_cur = s->_start + dist; } } memcpy(s->_cur, src, n); @@ -125,14 +125,14 @@ size_t stream_tell(struct stream *s) { end-of-file, respectively. */ void stream_seek(struct stream *s, size_t pos, int whence) { switch (whence) { - case SEEK_SET: - s->_cur = s->_start + pos; - break; - case SEEK_CUR: - s->_cur += pos; - break; - case SEEK_END: - s->_cur = s->_start + s->len - pos; + case SEEK_SET: + s->_cur = s->_start + pos; + break; + case SEEK_CUR: + s->_cur += pos; + break; + case SEEK_END: + s->_cur = s->_start + s->len - pos; } } @@ -177,16 +177,16 @@ static void map_entries(char *path, struct params p) { struct header *h = read_header(archive); stream_seek(archive, h->table_offset, SEEK_SET); - struct stream *table = load_table(archive); - struct table_entry *root = read_table(table); + struct stream *table = load_table(archive); + struct table_entry *root = read_table(table); for (struct table_entry *cur = root->next; cur != NULL; cur = cur->next) { switch (p.mode) { - case LIST: - list(cur); - break; - case EXTRACT: - extract(archive, cur, p); + case LIST: + list(cur); + break; + case EXTRACT: + extract(archive, cur, p); } } @@ -200,20 +200,20 @@ static void map_entries(char *path, struct params p) { int main(int argc, char **argv) { struct params p = parse_args(argc, argv); switch (p.mode) { - case USAGE: - print_usage(argv[0]); - params_free(p); - return EXIT_FAILURE; - case VERSION: - print_version(); - break; - case HELP: - print_help(); - break; - case LIST: - case EXTRACT: - for (int i = p.vararg_index; i < argc; i++) - map_entries(argv[i], p); + case USAGE: + print_usage(argv[0]); + params_free(p); + return EXIT_FAILURE; + case VERSION: + print_version(); + break; + case HELP: + print_help(); + break; + case LIST: + case EXTRACT: + for (int i = p.vararg_index; i < argc; i++) + map_entries(argv[i], p); } params_free(p); return EXIT_SUCCESS; diff --git a/src/table.c b/src/table.c index 5a0b262..7998295 100644 --- a/src/table.c +++ b/src/table.c @@ -80,16 +80,16 @@ struct table_entry *read_table(struct stream *s) { stream_read(&size, s, sizeof(uint64_t)); switch (magic) { - case ELIF_MAGIC: - case HNFN_MAGIC: - case NEKO_MAGIC: - read_elif(s, root); - break; - case FILE_MAGIC: - read_file(s, root); - break; - default: - ended = 1; + case ELIF_MAGIC: + case HNFN_MAGIC: + case NEKO_MAGIC: + read_elif(s, root); + break; + case FILE_MAGIC: + read_file(s, root); + break; + default: + ended = 1; } } while (!ended); @@ -112,21 +112,21 @@ void read_file(struct stream *s, struct table_entry *root) { stream_read(&size, s, sizeof(uint64_t)); switch (magic) { - case ADLR_MAGIC: - read_adlr(s, tmp); - break; - case SEGM_MAGIC: - read_segm(s, tmp, size / 28); - break; - case INFO_MAGIC: - stream_seek(s, size, SEEK_CUR); - break; - case TIME_MAGIC: - read_time(s, tmp); - break; - default: - ended = true; - stream_seek(s, -sizeof(uint32_t) - sizeof(uint64_t), SEEK_CUR); + case ADLR_MAGIC: + read_adlr(s, tmp); + break; + case SEGM_MAGIC: + read_segm(s, tmp, size / 28); + break; + case INFO_MAGIC: + stream_seek(s, size, SEEK_CUR); + break; + case TIME_MAGIC: + read_time(s, tmp); + break; + default: + ended = true; + stream_seek(s, -sizeof(uint32_t) - sizeof(uint64_t), SEEK_CUR); } } while (!ended); diff --git a/test/test_cli.c b/test/test_cli.c index c4b42f4..5a5f0db 100644 --- a/test/test_cli.c +++ b/test/test_cli.c @@ -26,7 +26,7 @@ char *test_out_path(void) { char *argv[4] = {"nekopack", "-o", "/tmp", "a.xp3"}; - struct params p = parse_args(3, argv); + struct params p = parse_args(4, argv); mu_assert("Trailing slash not appended to path", !strcmp(p.out, "/tmp/")); return NULL; } |