summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjakob <jakob@memeware.net>2017-06-06 15:40:40 -0400
committerjakob <jakob@memeware.net>2017-06-06 15:40:40 -0400
commitcfd5fb6e6fb47e084e66e1e4e2d6a99f19be661e (patch)
tree5fcfe91127c67f6dbb94a81848e8c1b9e6ddea7d /src
parent17b658ceadd08d693f23448c276958e2eea5970e (diff)
Cleaned up
Diffstat (limited to 'src')
-rw-r--r--src/cli.c18
-rw-r--r--src/cli.h1
-rw-r--r--src/compress.c16
-rw-r--r--src/compress.h8
-rw-r--r--src/header.c32
-rw-r--r--src/main.c44
6 files changed, 41 insertions, 78 deletions
diff --git a/src/cli.c b/src/cli.c
index 7f2703e..da6d3cc 100644
--- a/src/cli.c
+++ b/src/cli.c
@@ -26,15 +26,20 @@
#include "cli.h"
#include "crypto.h"
-#define NEKOPACK_VERSION "2.1.0b1"
+#define NEKOPACK_VERSION "2.1.0"
-/* Copies `optarg` into the out field of `p` and ensures that it ends in
- a trailing path delimiter. */
+/* Copies `optarg` into the out member of `p` and ensures that it has a
+ trailing path delimiter. */
static void parse_output_path(const char *optarg, struct params *p) {
p->out_len = strlen(optarg);
p->out = malloc(p->out_len + 2);
+
+ if (p->out == NULL)
+ return;
+
strcpy(p->out, optarg);
+
if (p->out[p->out_len - 1] != '/') {
p->out[p->out_len] = '/';
p->out_len += 1;
@@ -78,7 +83,6 @@ struct params parse_args(int argc, char **argv) {
{"extract", no_argument, NULL, 'e'},
{"list", no_argument, NULL, 'l'},
{"create", no_argument, NULL, 'c'},
- {"debug", no_argument, NULL, 'd'},
{"output", no_argument, NULL, 'o'},
{"game", no_argument, NULL, 'g'},
{NULL, 0, NULL, 0}
@@ -86,7 +90,7 @@ struct params parse_args(int argc, char **argv) {
do {
count++;
- cur = getopt_long(argc, argv, "hVvelcdqo:g:", long_opts, &opt_index);
+ cur = getopt_long(argc, argv, "hVvelcqo:g:", long_opts, &opt_index);
switch (cur) {
case 'h':
p.mode = HELP;
@@ -120,10 +124,6 @@ struct params parse_args(int argc, char **argv) {
case 'c':
p.mode = CREATE;
- break;
-
- case 'd':
- p.mode = DEBUG;
}
} while (cur >= 0);
diff --git a/src/cli.h b/src/cli.h
index 00e33fa..dfc8ef4 100644
--- a/src/cli.h
+++ b/src/cli.h
@@ -30,7 +30,6 @@ enum {
LIST,
EXTRACT,
CREATE,
- DEBUG,
};
/* Structure for storing options set from the command-line. */
diff --git a/src/compress.c b/src/compress.c
index e61780d..4df5ef7 100644
--- a/src/compress.c
+++ b/src/compress.c
@@ -26,7 +26,10 @@
#define LEVEL -1
-/* Inflates `s` into a newly allocated stream structure. */
+/* Inflates `len` bytes from the current position of `s` to a new stream
+ structure of size `decompressed_len` bytes. Stream inflation will not
+ work if `decompressed_len` does not represent the actual size of the
+ original data. */
struct stream *stream_inflate(struct stream *s, size_t len,
size_t decompressed_len) {
z_stream strm;
@@ -40,17 +43,17 @@ struct stream *stream_inflate(struct stream *s, size_t len,
return NULL;
int ret;
- struct stream *n = stream_new(decompressed_len);
+ struct stream *new = stream_new(decompressed_len);
do {
strm.avail_in = len;
strm.next_in = (Bytef *) s->_cur;
do {
strm.avail_out = decompressed_len;
- strm.next_out = (Bytef *) n->_cur;
+ strm.next_out = (Bytef *) new->_cur;
ret = inflate(&strm, Z_NO_FLUSH);
if (ret == Z_STREAM_ERROR) {
- stream_free(n);
+ stream_free(new);
inflateEnd(&strm);
return NULL;
}
@@ -58,11 +61,12 @@ struct stream *stream_inflate(struct stream *s, size_t len,
} while (ret != Z_STREAM_END);
inflateEnd(&strm);
- return n;
+ return new;
}
-/* Deflates `s` into a newly allocated stream structure. */
+/* Deflates `len` bytes from the current position of `s` to a new stream
+ structure, where the `len` member represents the decompressed size. */
struct stream *stream_deflate(struct stream *s, size_t len) {
struct stream *new = stream_new(len);
if (new == NULL)
diff --git a/src/compress.h b/src/compress.h
index cfb2487..e885231 100644
--- a/src/compress.h
+++ b/src/compress.h
@@ -21,9 +21,13 @@
#include "io.h"
-/* Inflates `s` into a newly allocated stream structure. */
+/* Inflates `len` bytes from the current position of `s` to a new stream
+ structure of size `decompressed_len` bytes. Stream inflation will not
+ work if `decompressed_len` does not represent the actual size of the
+ original data. */
struct stream *stream_inflate(struct stream *s, size_t len,
size_t decompressed_len);
-/* Deflates `s` into a newly allocated stream structure. */
+/* Deflates `len` bytes from the current position of `s` to a new stream
+ structure, where the `len` member represents the decompressed size. */
struct stream *stream_deflate(struct stream *s, size_t len);
diff --git a/src/header.c b/src/header.c
index 86f5e25..11dc654 100644
--- a/src/header.c
+++ b/src/header.c
@@ -39,6 +39,22 @@ static bool is_supported(struct header *h) {
}
+/* Generates an XP3 header structure readable by Nekopara. */
+struct header *create_header(void) {
+ struct header *h = malloc(sizeof(struct header));
+ if (h == NULL)
+ return NULL;
+
+ memcpy(h->magic, XP3_MAGIC, 11);
+ h->info_offset = 0x17;
+ h->version = 1;
+ h->flags = 0x80;
+ h->table_size = 0;
+ h->table_offset = 0;
+ return h;
+}
+
+
/* Reads from the given stream into a newly allocated header structure.
NULL is returned if the header contains an invalid magic number, or
if the archive's version is not supported. */
@@ -63,22 +79,6 @@ struct header *read_header(struct stream *s) {
}
-/* Generates an XP3 header structure readable by Nekopara. */
-struct header *create_header(void) {
- struct header *h = malloc(sizeof(struct header));
- if (h == NULL)
- return NULL;
-
- memcpy(h->magic, XP3_MAGIC, 11);
- h->info_offset = 0x17;
- h->version = 1;
- h->flags = 0x80;
- h->table_size = 0;
- h->table_offset = 0;
- return h;
-}
-
-
/* Dumps the XP3 header specified by `h` into `fp`. */
void dump_header(FILE *fp, struct header *h) {
fwrite(h->magic, 11, 1, fp);
diff --git a/src/main.c b/src/main.c
index ac21bb4..d50ee4c 100644
--- a/src/main.c
+++ b/src/main.c
@@ -17,7 +17,6 @@
You should have received a copy of the GNU General Public License
along with Nekopack. If not, see <http://www.gnu.org/licenses/>. */
-#include <inttypes.h> /* Only used for debug mode. */
#include <errno.h>
#include <sys/stat.h>
#include <stdint.h>
@@ -35,9 +34,6 @@
#define EXIT_FAILURE 1
#define EXIT_SUCCESS 0
-#define ANSI_TITLE "\x1b[36m\x1b[4m"
-#define ANSI_END "\x1b[0m"
-
static struct stream *load_table(struct stream *s) {
uint8_t compressed;
@@ -245,7 +241,6 @@ static void create_archive(char **paths, int argc, struct params p) {
return;
}
- /* TODO: Separate into own function. */
table_size += 20 + strlen(cur->filename) * 2;
table_size += 28 * cur->segment_count + 60;
@@ -286,11 +281,9 @@ static void create_archive(char **paths, int argc, struct params p) {
h->table_offset = 40 + data->len;
dump_header(fp, h);
- /* FIXME: Formatting and this initial seek might not be necessary. */
stream_seek(data, 0, SEEK_SET);
stream_dump(fp, data, data->len);
- /* FIXME: May be an incompatible value. Also, move this shitty hack. */
uint8_t compressed = 1;
fwrite(&compressed, sizeof(uint8_t), 1, fp);
fwrite(&table_compressed->len, sizeof(uint64_t), 1, fp);
@@ -300,38 +293,6 @@ static void create_archive(char **paths, int argc, struct params p) {
}
-/* The name is not particularly fitting to its function. */
-static void display_table(char *path, struct params p) {
- /* Replicated code. Could this be in map_entries? */
- struct stream *archive = stream_from_file(path);
- if (archive == NULL) {
- if (errno == ENOENT) {
- perror(path);
- } else {
- fprintf(stderr, "Error allocating memory.\n");
- }
- return;
- }
-
- struct header *h = read_header(archive);
- if (h == NULL) {
- fprintf(stderr, "File is not an XP3 archive.\n");
- return;
- }
-
- printf(ANSI_TITLE "Archive Header\n" ANSI_END);
- printf("Magic: ");
- for (int i = 0; i < 11; i++)
- printf("%x", h->magic[i]);
- printf("\n");
- printf("Info Offset: 0x%"PRIx64"\n", h->info_offset);
- printf("Version: 0x%"PRIx32"\n", h->version);
- printf("Flags: 0x%"PRIx8"\n", h->flags);
- printf("Table Size: 0x%"PRIx64"\n", h->table_size);
- printf("Table Offset: 0x%"PRIx64"\n", h->table_offset);
-}
-
-
int main(int argc, char **argv) {
struct params p = parse_args(argc, argv);
switch (p.mode) {
@@ -354,11 +315,6 @@ int main(int argc, char **argv) {
map_entries(argv[i], p);
break;
- case DEBUG:
- for (int i = p.vararg_index; i < argc; i++)
- display_table(argv[i], p);
- break;
-
case CREATE:
create_archive(argv + p.vararg_index, argc, p);
}