summaryrefslogtreecommitdiff
path: root/src/main.c
blob: efda3bca1ff913994e0030ede98c180ef19b4d35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* This file is part of Nekopack.

   Copyright (C) 2017 Jakob Tsar-Fox, All Rights Reserved.

   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 <errno.h>
#include <inttypes.h> // Needed for debugging at this point.
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "cli.h"
#include "extract.h"

#define XP3_MAGIC "XP3\x0d\x0a\x20\x0a\x1a\x8b\x67\x01"
#define XP3_TABLE_OFFSET 11
#define XP3_VERSION_OFFSET 19

int is_xp3_archive(FILE *archive);
int get_archive_version(FILE *archive);
uint64_t get_table_offset(FILE *archive, uint8_t archive_version);

/* Global instance of the command-line configuration structure. */
struct configuration arguments;


int main(int argc, char *argv[]) {
    arguments = parse_args(argc, argv);

    FILE *archive = fopen(arguments.archive_path, "rb");
    if (archive == NULL) {
        perror(arguments.archive_path);
        exit(EXIT_FAILURE);
    } else if (!is_xp3_archive(archive)) {
        fprintf(stderr, "File is not an XP3 archive.\n");
        fclose(archive);
        exit(EXIT_FAILURE);
    }

    int archive_version = get_archive_version(archive);
    uint64_t table_offset = get_table_offset(archive, archive_version);
    extract(archive, table_offset);
    fclose(archive);
    return 0;
}


/* Simple check of the archive's magic number to decide
   whether or not it represents a valid XP3 archive. */
int is_xp3_archive(FILE *archive) {
    char* magic_buffer = malloc(11);
    rewind(archive);
    fread(magic_buffer, 11, 1, archive);
    if (memcmp(magic_buffer, XP3_MAGIC, 11)) {
        free(magic_buffer);
        return 0;
    }
    free(magic_buffer);
    return 1;
}


/* Returns the version of XP3 used to pack the archive. */
int get_archive_version(FILE *archive) {
    uint32_t version_word;
    fseek(archive, XP3_VERSION_OFFSET, SEEK_SET);
    fread(&version_word, sizeof(uint32_t), 1, archive);
    /* 0x00 indicates version 1, and 0x01 indicates version 2. */
    return version_word == 1 ? 2 : 1;
}


/* Subroutine for finding the archive's table offset. If the
   minor_version is invalid, the program will exit.  */
uint64_t get_table_offset(FILE *archive, uint8_t archive_version) {
    uint64_t table_offset;
    fseek(archive, XP3_TABLE_OFFSET, SEEK_SET);
    fread(&table_offset, sizeof(uint64_t), 1, archive);
    if (archive_version == 1)
        return table_offset;
    /* The minor version is only present in XP3 version 2. */
    uint32_t minor_version;
    fread(&minor_version, sizeof(uint32_t), 1, archive);
    if (minor_version != 1) {
        fprintf(stderr, "Minor version not implemented.\n");
        fclose(archive);
    }
    /* The read table_offset is actually an offset to the real
       table offset. XP3 Version 2 is a little strange. */
    fseek(archive, table_offset, SEEK_SET);
    /* Flags and size of table are ignored in the parsing process. */
    fseek(archive, sizeof(uint8_t) + sizeof(uint64_t), SEEK_CUR);
    fread(&table_offset, sizeof(uint64_t), 1, archive);
    return table_offset;
}