summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/header.c53
-rw-r--r--src/header.h42
-rw-r--r--src/io.c24
-rw-r--r--src/io.h18
-rw-r--r--test/test_header.c38
-rw-r--r--test/test_header.h23
-rw-r--r--test/test_io.c30
-rw-r--r--test/test_io.h23
-rw-r--r--test/test_run.c46
9 files changed, 263 insertions, 34 deletions
diff --git a/src/header.c b/src/header.c
new file mode 100644
index 0000000..e039447
--- /dev/null
+++ b/src/header.c
@@ -0,0 +1,53 @@
+/* header.c -- Code for handling the archive's header section.
+
+ Copyright (C) 2017 Jakob Tsar-Fox, All Rights Reserved.
+
+ This file is part of Nekopack.
+
+ Nekopack is free software: you can redistribute it and/or modify it
+ under the terms of the GNU General Public License as published by the
+ Free Software Foundation, either version 3 of the License, or (at
+ your option) any later version.
+
+ Nekopack is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Nekopack. If not, see <http://www.gnu.org/licenses/>. */
+
+#include <stdbool.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "header.h"
+#include "io.h"
+
+static bool is_xp3(struct xp3_header *h);
+static bool is_supported(struct xp3_header *h);
+
+
+/* Reads data from the given stream into a newly allocated xp3_header
+ structure. The stream is assumed to be at the header's beginning.
+ NULL is returned if the header contains an invalid magic number, or
+ if the archive's version is not supported. */
+struct xp3_header *read_header(struct stream *s) {
+ struct xp3_header *h = malloc(sizeof(struct xp3_header));
+ if (h == NULL) return NULL;
+ stream_read(h, s, sizeof(struct xp3_header));
+ if (!is_xp3(h) || !is_supported(h)) {free(h); return NULL;}
+ return h;
+}
+
+
+/* Checks that the header contains the correct magic number. */
+static bool is_xp3(struct xp3_header *h) {
+ return !memcmp(h->magic, XP3_MAGIC, 11);
+}
+
+
+/* Checks that the archive is of XP3 version 2. */
+static bool is_supported(struct xp3_header *h) {
+ return h->version == 1;
+}
diff --git a/src/header.h b/src/header.h
new file mode 100644
index 0000000..7119f23
--- /dev/null
+++ b/src/header.h
@@ -0,0 +1,42 @@
+/* header.h -- Code for handling the archive's header section.
+
+ Copyright (C) 2017 Jakob Tsar-Fox, All Rights Reserved.
+
+ This file is part of Nekopack.
+
+ Nekopack is free software: you can redistribute it and/or modify it
+ under the terms of the GNU General Public License as published by the
+ Free Software Foundation, either version 3 of the License, or (at
+ your option) any later version.
+
+ Nekopack is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Nekopack. If not, see <http://www.gnu.org/licenses/>. */
+
+#pragma once
+
+#include <stdint.h>
+
+#include "io.h"
+
+#define XP3_MAGIC "XP3\x0d\x0a\x20\x0a\x1a\x8b\x67\x01"
+
+/* Structure representing the header section of an XP3 archive. */
+struct xp3_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. */
+};
+
+/* Reads data from the given stream into a newly allocated xp3_header
+ structure. The stream is assumed to be at the header's beginning.
+ NULL is returned if the header contains an invalid magic number, or
+ if the archive's version is not supported. */
+struct xp3_header *read_header(struct stream *s);
diff --git a/src/io.c b/src/io.c
index 2762180..1775f6b 100644
--- a/src/io.c
+++ b/src/io.c
@@ -18,19 +18,21 @@
along with Nekopack. If not, see <http://www.gnu.org/licenses/>. */
#include <stdint.h>
+#include <stdlib.h>
#include <string.h>
#include "io.h"
-/* Allocates `len` bytes of memory and returns a new stream pointing to
- it. The memory provided has not zeroed. */
+/* Allocates `len` bytes of non-zeroed memory and returns a new stream
+ structure pointing to it. */
struct stream *stream_new(size_t len) {
- stream *new = malloc(sizeof(stream));
+ struct stream *new = malloc(sizeof(struct stream));
new->len = len;
new->_start = malloc(len);
new->_cur = new->_start;
- new->_location = HEAP;
+ new->_loc = HEAP;
+ return new;
}
@@ -46,17 +48,17 @@ void stream_free(struct stream *s) {
/* Copies `n` bytes from the given stream into the memory area specified
- by `dest`. The stream is advanced appropriately. */
+ by `dest`. The stream's cursor is advanced appropriately. */
void stream_read(void *dest, struct stream *s, size_t n) {
memcpy(dest, s->_cur, n);
- (char *) s->_cur += n;
+ s->_cur += n;
}
/* Copies `n` bytes into the given stream from the memory area specified
- by `src`. The stream is advanced appropriately. */
-void stream_write(struct stream *s, void *src, size_t n) {
- if (s->cur + n > s->start + s->len) {
+ by `src`. The stream's cursor is advanced appropriately. */
+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:
@@ -64,6 +66,6 @@ void stream_write(struct stream *s, void *src, size_t n) {
s->_cur = s->_start + dist;
}
}
- memcpy(s->cur, src, n);
- s->cur += n;
+ memcpy(s->_cur, src, n);
+ s->_cur += n;
}
diff --git a/src/io.h b/src/io.h
index f07a421..0914c9f 100644
--- a/src/io.h
+++ b/src/io.h
@@ -26,32 +26,30 @@
segment. Used for freeing and unmapping purposes, if necessary. */
enum _location {
HEAP,
-}
+};
/* Structure to emulate a stream of data that maintains a current
position. The structure additionally maintains a `length` variable
containing the stream's length. */
struct stream {
size_t len; /* Length of the stream. */
- void *_start; /* Pointer to the stream's start. */
- void *_cur; /* Pointer to the current position. */
+ char *_start; /* Pointer to the stream's start. */
+ char *_cur; /* Pointer to the current position. */
enum _location _loc; /* Location of the stream in memory. */
-}
+};
-/* Allocates `len` bytes of memory and returns a new stream pointing to
- it. The memory provided has not zeroed. */
+/* Allocates `len` bytes of non-zeroed memory and returns a new stream
+ structure pointing to it. */
struct stream *stream_new(size_t len);
/* Called to free or unmap the memory chunk associated with the given
stream, as well as the stream structure itself. */
void stream_free(struct stream *s);
-
/* Copies `n` bytes from the given stream into the memory area specified
- by `dest`. The stream is advanced appropriately. */
+ by `dest`. The stream's cursor is advanced appropriately. */
void stream_read(void *dest, struct stream *s, size_t n);
-
/* Copies `n` bytes into the given stream from the memory area specified
- by `src`. The stream is advanced appropriately. */
+ by `src`. The stream's cursor is advanced appropriately. */
void stream_write(struct stream *s, void *src, size_t n);
diff --git a/test/test_header.c b/test/test_header.c
new file mode 100644
index 0000000..1ae4e19
--- /dev/null
+++ b/test/test_header.c
@@ -0,0 +1,38 @@
+/* test_header.c -- MinUnit test cases for header.c
+
+ Copyright (C) 2017 Jakob Tsar-Fox, All Rights Reserved.
+
+ This file is part of Nekopack.
+
+ Nekopack is free software: you can redistribute it and/or modify it
+ under the terms of the GNU General Public License as published by the
+ Free Software Foundation, either version 3 of the License, or (at
+ your option) any later version.
+
+ Nekopack is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Nekopack. If not, see <http://www.gnu.org/licenses/>. */
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "minunit.h"
+
+#include "header.h"
+#include "io.h"
+
+extern int tests_run;
+
+
+char *test_header_read(void) {
+ struct stream *s = stream_new(sizeof(struct xp3_header));
+ memset(s->_start, '\x00', sizeof(struct xp3_header));
+ struct xp3_header *h = read_header(s);
+ mu_assert("Header assertions failed", h == NULL);
+ stream_free(s);
+ return NULL;
+}
diff --git a/test/test_header.h b/test/test_header.h
new file mode 100644
index 0000000..83613ce
--- /dev/null
+++ b/test/test_header.h
@@ -0,0 +1,23 @@
+/* test_header.h -- MinUnit test cases for header.c
+
+ Copyright (C) 2017 Jakob Tsar-Fox, All Rights Reserved.
+
+ This file is part of Nekopack.
+
+ Nekopack is free software: you can redistribute it and/or modify it
+ under the terms of the GNU General Public License as published by the
+ Free Software Foundation, either version 3 of the License, or (at
+ your option) any later version.
+
+ Nekopack is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Nekopack. If not, see <http://www.gnu.org/licenses/>. */
+
+#pragma once
+
+char *test_header_read(void);
+
diff --git a/test/test_io.c b/test/test_io.c
index 8bf7593..fd1e483 100644
--- a/test/test_io.c
+++ b/test/test_io.c
@@ -1,4 +1,4 @@
-/* test_io.c -- MinUnit test cases for I/O related functions.
+/* test_io.c -- MinUnit test cases for io.c
Copyright (C) 2017 Jakob Tsar-Fox, All Rights Reserved.
@@ -17,36 +17,40 @@
You should have received a copy of the GNU General Public License
along with Nekopack. If not, see <http://www.gnu.org/licenses/>. */
-#include <stdio.h>
#include <string.h>
#include "minunit.h"
#include "io.h"
-int tests_run = 0;
+extern int tests_run;
-static char *test_stream_obj(void) {
+char *test_stream_obj(void) {
struct stream *s_1 = stream_new(0x10);
struct stream *s_2 = stream_new(0x20);
struct stream *s_3 = stream_new(0x40);
- mu_assert("Incorrect size for `s_1`", s_1.len == 0x10);
- mu_assert("Incorrect size for `s_2`", s_1.len == 0x20);
- mu_assert("Incorrect size for `s_3`", s_1.len == 0x40);
+ mu_assert("Incorrect size for `s_1`", s_1->len == 0x10);
+ mu_assert("Incorrect size for `s_2`", s_2->len == 0x20);
+ mu_assert("Incorrect size for `s_3`", s_3->len == 0x40);
stream_free(s_1);
stream_free(s_2);
stream_free(s_3);
+ return NULL;
}
-static char *test_stream_rw(void) {
+char *test_stream_rw(void) {
char dest[4];
- struct stream *s_1 = stream_new(0x4);
- stream_write(s_1, "\x00\x01\x02\x03", 4);
- mu_assert("Write failure", !memcmp(s_1->_start, "\x00\x01\x02\x03", 4));
- mu_assert("Cursor not incremented", s_1->cur - s_1->start == 4);
+ struct stream *s = stream_new(0x4);
+ stream_write(s, "\x00\x01\x02\x03", 4);
+ mu_assert("Write failure", !memcmp(s->_start, "\x00\x01\x02\x03", 4));
+ mu_assert("Cursor not incremented", s->_cur - s->_start == 4);
+ s->_cur = s->_start;
+
stream_read(dest, s, 4);
mu_assert("Read failure", !memcmp(dest, "\x00\x01\x02\x03", 4));
- stream_free(s_1);
+ mu_assert("Cursor not incremented", s->_cur - s->_start == 4);
+ stream_free(s);
+ return NULL;
}
diff --git a/test/test_io.h b/test/test_io.h
new file mode 100644
index 0000000..c750214
--- /dev/null
+++ b/test/test_io.h
@@ -0,0 +1,23 @@
+/* test_io.h -- MinUnit test cases for io.c
+
+ Copyright (C) 2017 Jakob Tsar-Fox, All Rights Reserved.
+
+ This file is part of Nekopack.
+
+ Nekopack is free software: you can redistribute it and/or modify it
+ under the terms of the GNU General Public License as published by the
+ Free Software Foundation, either version 3 of the License, or (at
+ your option) any later version.
+
+ Nekopack is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Nekopack. If not, see <http://www.gnu.org/licenses/>. */
+
+#pragma once
+
+char *test_stream_obj(void);
+char *test_stream_rw(void);
diff --git a/test/test_run.c b/test/test_run.c
new file mode 100644
index 0000000..8728b81
--- /dev/null
+++ b/test/test_run.c
@@ -0,0 +1,46 @@
+/* test_io.c -- MinUnit test runner.
+
+ Copyright (C) 2017 Jakob Tsar-Fox, All Rights Reserved.
+
+ This file is part of Nekopack.
+
+ Nekopack is free software: you can redistribute it and/or modify it
+ under the terms of the GNU General Public License as published by the
+ Free Software Foundation, either version 3 of the License, or (at
+ your option) any later version.
+
+ Nekopack is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Nekopack. If not, see <http://www.gnu.org/licenses/>. */
+
+#include <stdio.h>
+
+#include "minunit.h"
+
+#include "test_header.h"
+#include "test_io.h"
+
+int tests_run = 0;
+
+
+static char *run_all_tests(void) {
+ mu_run_test(test_stream_obj);
+ mu_run_test(test_stream_rw);
+ mu_run_test(test_header_read);
+ return NULL;
+}
+
+
+int main(void) {
+ char *ret = run_all_tests();
+ if (ret) {
+ printf("%s\n", ret);
+ return 1;
+ }
+ printf("Successful tests: %d\n", tests_run);
+ return 0;
+}