diff options
Diffstat (limited to 'src/main.c')
| -rw-r--r-- | src/main.c | 45 |
1 files changed, 43 insertions, 2 deletions
@@ -17,6 +17,7 @@ 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> @@ -34,6 +35,9 @@ #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; @@ -243,7 +247,7 @@ static void create_archive(char **paths, int argc, struct params p) { /* TODO: Separate into own function. */ table_size += 20 + strlen(cur->filename) * 2; - table_size += 28 * cur->segment_count + 56; + table_size += 28 * cur->segment_count + 60; cur->segments = malloc(sizeof(struct segment *)); if (cur->segments == NULL) { @@ -278,7 +282,7 @@ static void create_archive(char **paths, int argc, struct params p) { return; } - h->table_size = data->len; // This isn't right <:^) + h->table_size = table_size; h->table_offset = 40 + data->len; dump_header(fp, h); @@ -296,6 +300,38 @@ 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) { @@ -318,6 +354,11 @@ 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); } |