diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/header.c | 53 | ||||
| -rw-r--r-- | src/header.h | 42 | ||||
| -rw-r--r-- | src/io.c | 24 | ||||
| -rw-r--r-- | src/io.h | 18 |
4 files changed, 116 insertions, 21 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); @@ -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; } @@ -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); |