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
|
/* table.c -- Code for handling the archive's table section.
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 <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "encoding.h"
#include "io.h"
#include "table.h"
#define ELIF_MAGIC 0x46696c65
#define FILE_MAGIC 0x656c6946
#define HNFN_MAGIC 0x6e666e68
#define NEKO_MAGIC 0x6f6b656e
#define ADLR_MAGIC 0x726c6461
#define SEGM_MAGIC 0x6d676573
#define INFO_MAGIC 0x6f666e69
#define TIME_MAGIC 0x656d6974
/* Reads a segm chunk into the table_entry specified by `tmp`. */
static void read_segm(struct stream *s, struct table_entry *tmp, uint64_t count) {
tmp->segment_count = count;
tmp->segments = malloc(sizeof(struct segment *) * count);
if (tmp->segments == NULL) return;
for (uint64_t i = 0; i < count; i++) {
tmp->segments[i] = malloc(sizeof(struct segment));
if (tmp->segments[i] == NULL) return;
stream_read(&tmp->segments[i]->compressed, s, sizeof(uint32_t));
stream_read(&tmp->segments[i]->offset, s, sizeof(uint64_t));
stream_read(&tmp->segments[i]->decompressed_size, s, sizeof(uint64_t));
stream_read(&tmp->segments[i]->compressed_size, s, sizeof(uint64_t));
}
}
/* Reads an adlr chunk into the table_entry specified by `tmp`. */
static void read_adlr(struct stream *s, struct table_entry *tmp) {
stream_read(&tmp->key, s, sizeof(uint32_t));
}
/* Reads a time chunk into the table_entry specified by `tmp`. */
static void read_time(struct stream *s, struct table_entry *tmp) {
stream_read(&tmp->ctime, s, sizeof(uint64_t));
}
/* Reads the contents of a File chunk. If there is an entry with a
matching key in the linked list specified by `root`, that structure
will be modified. Otherwise, a new entry will be created and appended
to the linked list. */
void read_file(struct stream *s, struct table_entry *root) {
bool ended = false;
uint32_t magic;
uint64_t size;
struct table_entry *cur, *tmp = calloc(sizeof(struct table_entry), 1);
do {
stream_read(&magic, s, sizeof(uint32_t));
stream_read(&size, s, sizeof(uint64_t));
switch (magic) {
case ADLR_MAGIC:
read_adlr(s, tmp);
break;
case SEGM_MAGIC:
read_segm(s, tmp, size / 28);
break;
case INFO_MAGIC:
stream_seek(s, size, SEEK_CUR);
break;
case TIME_MAGIC:
read_time(s, tmp);
break;
default:
ended = true;
stream_seek(s, -sizeof(uint32_t) - sizeof(uint64_t), SEEK_CUR);
}
} while (!ended);
cur = get_node(root, tmp->key);
cur->segment_count = tmp->segment_count;
cur->segments = tmp->segments;
cur->ctime = tmp->ctime;
free(tmp);
}
/* Reads the contents of an eliF chunk. If there is an entry with a
matching key in the linked list specified by `root`, that structure
will be modified. Otherwise, a new entry will be created and appended
to the linked list. */
void read_elif(struct stream *s, struct table_entry *root) {
char *name, *buf, *tmp;
uint16_t name_len;
uint32_t key;
struct table_entry *cur;
stream_read(&key, s, sizeof(uint32_t));
stream_read(&name_len, s, sizeof(uint16_t));
/* The value provided by the archive represents the number of
UTF-16LE characters, not the number of bytes in the string. */
name_len = name_len * 2 + 2;
cur = get_node(root, key);
if (cur->filename != NULL) {
return;
}
if (name_len < 0x100) {
buf = malloc(name_len);
name = malloc(name_len);
if (buf == NULL || name == NULL) return;
stream_read(buf, s, name_len);
utf16le_decode(buf, name, name_len);
free(buf);
tmp = realloc(name, strlen(name) + 1);
if (tmp == NULL) {
free(name);
return;
}
name = tmp;
} else {
/* strdup isn't defined in ISO/IEC 9899:1999 C. */
name = malloc(14);
if (name == NULL) return;
strncpy(name, "COPYRIGHT.txt", 14);
stream_seek(s, name_len, SEEK_CUR);
}
cur->filename = name;
}
/* Returns the root of a linked list containing all of the files listed
in the archive's table section. */
struct table_entry *read_table(struct stream *s) {
struct table_entry *root = calloc(sizeof(struct table_entry), 1);
bool ended = false;
uint32_t magic;
uint64_t size;
do {
stream_read(&magic, s, sizeof(uint32_t));
stream_read(&size, s, sizeof(uint64_t));
switch (magic) {
case ELIF_MAGIC:
case HNFN_MAGIC:
case NEKO_MAGIC:
read_elif(s, root);
break;
case FILE_MAGIC:
read_file(s, root);
break;
default:
ended = 1;
}
} while (!ended);
return root;
}
/* Inserts the file specified by `path` into the table linked list
specified by `root`. */
struct table_entry *add_file(struct table_entry *root, char *path) {
struct table_entry *new = calloc(sizeof(struct table_entry), 1);
size_t name_len = strlen(path);
new->filename = malloc(name_len);
strncpy(new->filename, path, name_len);
new->key = 0xffffffff;
new->ctime = 0;
new->segment_count = 0;
entry_append(root, new);
return new;
}
/* Dumps the adlr segment for `key` into the table at `s` and returns
the number of bytes written. */
static uint64_t dump_adlr(struct stream *s, uint32_t key) {
uint32_t magic = ADLR_MAGIC;
uint64_t entry_size = sizeof(uint32_t);
stream_write(s, &magic, sizeof(uint32_t));
stream_write(s, &entry_size, sizeof(uint64_t));
stream_write(s, &key, sizeof(uint32_t));
return 16;
}
/* Dumps the time segment for `timestamp` into the table at `s` and
returns the number of bytes written. */
static uint64_t dump_time(struct stream *s, uint64_t timestamp) {
uint32_t magic = TIME_MAGIC;
uint64_t entry_size = sizeof(uint64_t);
stream_write(s, &magic, sizeof(uint32_t));
stream_write(s, &entry_size, sizeof(uint64_t));
stream_write(s, ×tamp, sizeof(uint64_t));
return 20;
}
/* Dumps the segm segment for `cur` into the table at `s` and returns
the number of bytes written. */
static uint64_t dump_segm(struct stream *s, struct table_entry *cur) {
struct segment *segm;
uint32_t magic = SEGM_MAGIC;
uint64_t entry_size = cur->segment_count * 28;
stream_write(s, &magic, sizeof(uint32_t));
stream_write(s, &entry_size, sizeof(uint64_t));
for (uint64_t i = 0; i < cur->segment_count; i++) {
segm = cur->segments[i];
stream_write(s, &segm->compressed, sizeof(uint32_t));
stream_write(s, &segm->offset, sizeof(uint64_t));
stream_write(s, &segm->decompressed_size, sizeof(uint64_t));
stream_write(s, &segm->compressed_size, sizeof(uint64_t));
}
return 12 + 28 * cur->segment_count;
}
/* Dumps the File entry for `cur` into the table at `s`. */
static void dump_file(struct stream *s, struct table_entry *cur) {
uint32_t magic = FILE_MAGIC;
uint64_t bytes_written;
stream_write(s, &magic, sizeof(uint32_t));
stream_write(s, &bytes_written, sizeof(uint64_t));
bytes_written = dump_adlr(s, cur->key);
bytes_written += dump_time(s, cur->ctime);
bytes_written += dump_segm(s, cur);
stream_seek(s, -bytes_written, SEEK_CUR);
stream_write(s, &bytes_written, sizeof(uint64_t));
stream_seek(s, bytes_written, SEEK_CUR);
}
/* Dumps the eliF entry for `cur` into the table at `s`. */
static void dump_elif(struct stream *s, struct table_entry *cur) {
uint32_t magic = ELIF_MAGIC;
uint16_t name_len = strlen(cur->filename);
uint64_t entry_size = name_len * 2 + 8;
char *encoded = malloc(name_len * 2 + 2);
utf16le_encode(cur->filename, encoded, name_len);
stream_write(s, &magic, sizeof(uint32_t));
stream_write(s, &entry_size, sizeof(uint64_t));
stream_write(s, &cur->key, sizeof(uint32_t));
stream_write(s, &name_len, sizeof(uint16_t));
stream_write(s, encoded, name_len * 2 + 2);
}
/* Dumps the XP3 table specified by `root` into `s`. */
void dump_table(struct stream *s, struct table_entry *root) {
struct table_entry *cur;
for (cur = root->next; cur != NULL; cur = cur->next) {
dump_elif(s, cur);
dump_file(s, cur);
}
}
/* Traverses `root` for a node with the given key. If the linked list
lacks a node with the key, a new node is created and appended. */
struct table_entry *get_node(struct table_entry *root, uint32_t key) {
struct table_entry *cur;
for (cur = root; cur != NULL && cur->key != key; cur = cur->next);
if (cur == NULL) {
cur = calloc(sizeof(struct table_entry), 1);
if (cur == NULL) return NULL;
entry_append(root, cur);
cur->key = key;
}
return cur;
}
/* Inserts `e` to the end of the linked list specified by `root`. */
void entry_append(struct table_entry *root, struct table_entry *e) {
struct table_entry *cur;
for (cur = root; cur->next != NULL; cur = cur->next);
cur->next = e;
}
/* Frees every entry in the linked list specified by `cur`. */
void entry_free(struct table_entry *cur) {
if (cur->next != NULL)
entry_free(cur->next);
if (cur->filename != NULL)
free(cur->filename);
if (cur->segments != NULL) {
for (uint64_t i = 0; i < cur->segment_count; i++)
free(cur->segments[i]);
free(cur->segments);
}
free(cur);
}
|