summaryrefslogtreecommitdiff
path: root/src/file.c
blob: d2a9b722044f0c634bb7f96b08e1d08cd2fe2b48 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* 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 <inttypes.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

#include "extract.h"
#include "file.h"

#define ADLR_MAGIC 0x726c6461
#define SEGM_MAGIC 0x6d676573
#define INFO_MAGIC 0x6f666e69
#define TIME_MAGIC 0x656d6974

void read_info_chunk(memory_stream *data_stream, file_node *parsed);
void read_adlr_chunk(memory_stream *data_stream, file_node *parsed);
void read_time_chunk(memory_stream *data_stream, file_node *parsed);
void read_segm_chunk(memory_stream *data_stream, file_node *parsed,
                     uint64_t segment_count);


/* Creates a node for a File entry, which can be deferred in a linked
   list. NULL will be returned if there isn't enough memory to contain
   the node structure. */
file_node *read_file_entry(memory_stream *data_stream, Bytef *section_end) {
    uint32_t entry_magic;
    uint64_t entry_size;
    file_node *parsed = calloc(sizeof(file_node), 1);
    if (parsed == NULL)
        return NULL;
    while (data_stream->data < section_end) {
        read_stream(&entry_magic, &data_stream->data, sizeof(uint32_t));
        read_stream(&entry_size, &data_stream->data, sizeof(uint64_t));
        switch (entry_magic) {
            case ADLR_MAGIC:
                read_adlr_chunk(data_stream, parsed);
                break;
            case SEGM_MAGIC:
                /* The segment count is not included in the table and
                   has to be calculated. Segments are 28 bytes each. */
                read_segm_chunk(data_stream, parsed, entry_size / 28);
                /* There can't really be a check for this
                   in read_segm_chunk, so it's done here. */
                if (parsed->segments == NULL) {
                    free(parsed);
                    return NULL;
                }
                break;
            case INFO_MAGIC:
                read_info_chunk(data_stream, parsed);
                break;
            case TIME_MAGIC:
                read_time_chunk(data_stream, parsed);
                break;
            default:
                printf("Unknown magic: %" PRIx32 "\n", entry_magic);
        }
    }
    return parsed;
}


/* Reads the contents of an info chunk into a file node. */
void read_info_chunk(memory_stream *data_stream, file_node *parsed) {
    uint32_t encrypted;
    uint64_t decompressed_size, compressed_size;
    uint16_t file_name_size;
    read_stream(&encrypted, &data_stream->data, sizeof(uint32_t));
    read_stream(&decompressed_size, &data_stream->data, sizeof(uint64_t));
    read_stream(&compressed_size, &data_stream->data, sizeof(uint64_t));
    read_stream(&file_name_size, &data_stream->data, sizeof(uint16_t));
    parsed->encrypted = encrypted;

    char *file_name = malloc(file_name_size * 2 + 2);
    read_stream(file_name, &data_stream->data, file_name_size * 2 + 2);
    free(file_name);
}


/* Reads the contents of a segm chunk into a file node. */
void read_segm_chunk(memory_stream *data_stream, file_node *parsed,
                     uint64_t segment_count) {
    /* Segments are stored in an array of segment pointers. */
    segment **segments = malloc(sizeof(segment *) * segment_count);
    if (segments == NULL)
        return;

    for (uint64_t i = 0; i < segment_count; i++) {
        segments[i] = malloc(sizeof(segment));
        read_stream(&segments[i]->compressed,
                    &data_stream->data,
                    sizeof(uint32_t));
        read_stream(&segments[i]->offset,
                    &data_stream->data,
                    sizeof(uint64_t));
        read_stream(&segments[i]->decompressed_size,
                    &data_stream->data,
                    sizeof(uint64_t));
        read_stream(&segments[i]->compressed_size,
                    &data_stream->data,
                    sizeof(uint64_t));
        parsed->file_size += segments[i]->decompressed_size;
    }
    parsed->segment_count = segment_count;
    parsed->segments = segments;
}


/* Reads the contents of an adlr chunk into a file_node. */
void read_adlr_chunk(memory_stream *data_stream, file_node *parsed) {
    uint32_t key;
    read_stream(&key, &data_stream->data, sizeof(uint32_t));
    parsed->key = key;
}


/* Reads the contents of a time chunk into a file_node. */
void read_time_chunk(memory_stream *data_stream, file_node *parsed) {
    uint64_t timestamp;
    read_stream(&timestamp, &data_stream->data, sizeof(uint64_t));
}