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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
/* main.c -- Command-line entry point to the Nekopack.
Copyright (C) 2017 Jakob Kreuze, 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 <errno.h>
#include <sys/stat.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cli.h"
#include "compress.h"
#include "crypto.h"
#include "header.h"
#include "io.h"
#include "table.h"
#define EXIT_FAILURE 1
#define EXIT_SUCCESS 0
#define NEKOPACK_VERSION "2.1.0b1"
static void print_usage(char *progn) {
fprintf(stderr, "Usage: %s [OPTIONS] (ARCHIVES) [PATHS]\n", progn);
}
static void print_version(void) {
printf("Nekopack version %s\nProgrammed by Jakob. "
"<http://jakob.space>\n", NEKOPACK_VERSION);
}
static void print_help(void) {
printf("A tool for decompressing the XP3 archives used by Nekopara.\n\n"
" -h, --help\t\tDisplay this help page and exit.\n"
" -v, --version\tDisplay the currently installed version and "
"exit.\n\n"
" -e, --extract\tExtract the contents of the archive. This is "
"the default\n\t\t\taction if no mode argument is provided.\n"
" -l, --list\t\tList the contents of the archive.\n"
" -c, --create\t\tCreate a new XP3 archive.\n\n"
" -o, --output\t\tPath to extract files to.\n"
" -g, --game\t\tGame the archive is from. Required for file "
"decryption.\n"
" -q, --quiet\t\tDon't display information about extracted "
"files.\n\n"
"Supported games: nekopara_volume_0, nekopara_volume_0_steam,\n"
"nekopara_volume_1, nekopara_volume_1_steam\n");
}
static struct stream *load_table(struct stream *s) {
uint8_t compressed;
uint64_t len, decompressed_len;
stream_read(&compressed, s, sizeof(uint8_t));
stream_read(&len, s, sizeof(uint64_t));
stream_read(&decompressed_len, s, sizeof(uint64_t));
if (compressed)
return stream_inflate(s, len, decompressed_len);
return stream_clone(s, len);
}
/* Creates any directories that do not already exist in `path`,
including non-existent parents. */
void make_dirs(char *path) {
char *buf = calloc(0x100, 1);
char *buf_start = buf;
struct stat tmp = {0};
for (int i = 0; i < 0x100 && path[i] != '\0'; i++) {
if (path[i] == '/') {
*buf++ = '/';
*buf = '\0';
if (stat(buf_start, &tmp) == -1)
mkdir(buf_start, 0777);
} else {
*buf++ = path[i];
}
}
free(buf_start);
}
/* Joins `name` and the output path specified in `p`. */
static char *get_path(struct params p, char *name) {
size_t name_len = strlen(name);
char *path = malloc(p.out_len + name_len + 2);
if (path == NULL)
return NULL;
strcpy(path, p.out);
strcpy(path + p.out_len, name);
return path;
}
static void list(struct table_entry *e) {
if (e->filename == NULL || e->segments == NULL) {
fprintf(stderr, "Unpaired entries found. Archive may be corrupted.\n");
return;
}
printf("%s\n", e->filename);
}
static void extract(struct stream *s, struct table_entry *e, struct params p) {
if (e->filename == NULL || e->segments == NULL) {
fprintf(stderr, "Unpaired entries found. Archive may be corrupted.\n");
return;
}
char *path = get_path(p, e->filename);
if (path == NULL)
return;
make_dirs(path);
FILE *fp = fopen(path, "wb+");
struct stream *segm_data;
struct segment *segm;
if (fp == NULL) {
perror(path);
exit(EXIT_FAILURE);
} else if (p.verbose) {
printf("Extracting %s to %s...\n", e->filename, path);
}
for (uint64_t i = 0; i < e->segment_count; i++) {
segm = e->segments[i];
stream_seek(s, segm->offset, SEEK_SET);
if (e->segments[i]->compressed) {
segm_data = stream_inflate(s, segm->compressed_size,
segm->decompressed_size);
} else {
segm_data = stream_clone(s, segm->compressed_size);
}
struct game_key k = get_key(p.game);
uint8_t initial = derive_initial(k, e->key);
uint8_t primary = derive_primary(k, e->key);
stream_xor(segm_data, initial, primary);
stream_dump(fp, segm_data, segm_data->len);
stream_free(segm_data);
}
fclose(fp);
free(path);
}
/* Loads an XP3 archive and maps the function associated to the current
mode of operation to every entry. */
static void map_entries(char *path, struct params p) {
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;
}
stream_seek(archive, h->table_offset, SEEK_SET);
struct stream *table = load_table(archive);
if (table == NULL) {
fprintf(stderr, "Error allocating memory.\n");
return;
}
struct table_entry *root = read_table(table);
if (root == NULL) {
fprintf(stderr, "Error allocating memory.\n");
}
for (struct table_entry *cur = root->next; cur != NULL; cur = cur->next) {
switch (p.mode) {
case LIST:
list(cur);
break;
case EXTRACT:
extract(archive, cur, p);
}
}
entry_free(root);
stream_free(table);
stream_free(archive);
free(h);
}
/* Creates a new XP3 archive. The destination of the archive is the
first string in `paths`, and the files at any following paths will be
flattened into the archive. */
static void create_archive(char **paths, int argc, struct params p) {
struct stream *table_compressed, *file_compressed, *file;
struct table_entry *cur;
FILE *fp = fopen(paths[0], "wb+");
if (fp == NULL) {
perror(paths[0]);
return;
}
struct table_entry *root = calloc(sizeof(struct table_entry), 1);
if (root == NULL) {
fprintf(stderr, "Error allocating memory.\n");
return;
}
struct header *h = create_header();
if (h == NULL) {
fprintf(stderr, "Error allocating memory.\n");
return;
}
struct stream *data = stream_new(1);
struct stream *table = stream_new(1);
if (data == NULL || table == NULL) {
fprintf(stderr, "Error allocating memory.\n");
return;
}
uint64_t data_size = 0;
uint64_t table_size = 0;
for (int i = 1; i < argc - p.vararg_index; i++) {
file = stream_from_file(paths[i]);
if (file == NULL) {
perror(paths[i]);
continue;
}
file_compressed = stream_deflate(file, file->len);
if (file_compressed == NULL) {
fprintf(stderr, "Unable to compress data.\n");
return;
}
data_size += file_compressed->len;
stream_concat(data, file_compressed, file_compressed->len);
cur = add_file(root, paths[i]);
if (cur == NULL) {
fprintf(stderr, "Error allocating memory.\n");
return;
}
/* TODO: Separate into own function. */
table_size += 20 + strlen(cur->filename) * 2;
table_size += 28 * cur->segment_count + 56;
cur->segments = malloc(sizeof(struct segment *));
if (cur->segments == NULL) {
fprintf(stderr, "Error allocating memory.\n");
return;
}
cur->segments[0] = malloc(sizeof(struct segment));
if (cur->segments[0] == NULL) {
fprintf(stderr, "Error allocating memory.\n");
return;
}
cur->segments[0]->compressed = 1;
cur->segments[0]->offset = 40 + data_size;
cur->segments[0]->decompressed_size = file->len;
cur->segments[0]->compressed_size = file_compressed->len;
stream_free(file_compressed);
stream_free(file);
}
dump_table(table, root);
stream_seek(table, 0, SEEK_SET);
table_compressed = stream_deflate(table, table_size);
stream_free(table);
if (table_compressed == NULL) {
fprintf(stderr, "Unable to compress data.\n");
return;
}
h->table_size = data->len; // This isn't right <:^)
h->table_offset = 40 + data->len;
dump_header(fp, h);
/* FIXME: Formatting and this initial seek might not be necessary. */
stream_seek(data, 0, SEEK_SET);
stream_dump(fp, data, data->len);
/* FIXME: May be an incompatible value. Also, move this shitty hack. */
uint8_t compressed = 1;
fwrite(&compressed, sizeof(uint8_t), 1, fp);
fwrite(&table_compressed->len, sizeof(uint64_t), 1, fp);
fwrite(&table_size, sizeof(uint64_t), 1, fp);
stream_dump(fp, table_compressed, table_compressed->len);
}
int main(int argc, char **argv) {
struct params p = parse_args(argc, argv);
switch (p.mode) {
case USAGE:
print_usage(argv[0]);
free(p.out);
return EXIT_FAILURE;
case VERSION:
print_version();
break;
case HELP:
print_help();
break;
case LIST:
case EXTRACT:
for (int i = p.vararg_index; i < argc; i++)
map_entries(argv[i], p);
break;
case CREATE:
create_archive(argv + p.vararg_index, argc, p);
}
free(p.out);
return EXIT_SUCCESS;
}
|