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
|
#include "extract.h"
#include <errno.h>
#include <string.h>
#include <time.h>
#define ZSTD_STATIC_LINKING_ONLY // ZSTD_findDecompressedSize
#include <zstd.h>
static int nfl_extract_default(FILE *f, nfl_state_t *state);
static int nfl_extract_zstd(FILE *f, nfl_state_t *state);
static int nfl_extract_lz4(FILE *f, nfl_state_t *state);
static int nfl_verify_header(nfl_header_t *header) {
if(header->cksum != nfl_header_cksum(header))
return -1;
if (header->id > MAX_TRUNK_ID)
return -1;
if (header->max_n_entries < header->n_entries)
return -1;
time_t now = time(NULL);
if ((time_t)header->start_time >= now || (time_t)header->end_time >= now ||
header->start_time > header->end_time)
return -1;
return 0;
}
static int nfl_extract_default(FILE *f, nfl_state_t *state) {
fread(state->store, state->header->n_entries, sizeof(nfl_entry_t), f);
WARN_RETURN(ferror(f), "%s", strerror(errno));
return 0;
}
static int nfl_extract_zstd(FILE *f, nfl_state_t *state) {
char *buf;
size_t const compressed_size = nfl_get_filesize(f) - sizeof(nfl_header_t),
expected_decom_size = state->header->n_entries * sizeof(nfl_entry_t);
ERR(!(buf = malloc(compressed_size)), "zstd: cannot malloc");
fread(buf, compressed_size, 1, f);
WARN_RETURN(ferror(f), "%s", strerror(errno));
size_t const estimate_decom_size = ZSTD_findDecompressedSize(buf, compressed_size);
if (estimate_decom_size == ZSTD_CONTENTSIZE_ERROR)
FATAL("zstd: file was not compressed by zstd.\n");
else if (estimate_decom_size == ZSTD_CONTENTSIZE_UNKNOWN)
FATAL("zstd: original size unknown. Use streaming decompression instead");
size_t const actual_decom_size =
ZSTD_decompress(state->store, expected_decom_size, buf, compressed_size);
if (actual_decom_size != expected_decom_size) {
FATAL("zstd: error decoding current file: %s \n", ZSTD_getErrorName(actual_decom_size));
}
free(buf);
return 0;
}
static int nfl_extract_lz4(FILE *f, nfl_state_t *state) {
/* TODO */
return 0;
}
int nfl_extract_worker(const char *filename, nfl_state_t *state) {
FILE *f;
int got = 0, ret = 0;
nfl_header_t *h;
debug("Extracting from file %s", filename);
ERR((f = fopen(filename, "rb")) == NULL, "extract worker");
/* ERR(nfl_check_file(f) < 0, "extract worker"); */
// Read header
ERR(!(state->header = malloc(sizeof(nfl_header_t))), "extract malloc header");
got = fread(state->header, sizeof(nfl_header_t), 1, f);
h = state->header;
// Check header validity
WARN_RETURN(ferror(f), "%s", strerror(errno));
WARN_RETURN(!got || nfl_verify_header(h) < 0, "File %s has corrupted header.", filename);
// Read body
ERR(!(state->store = malloc(sizeof(nfl_entry_t) * h->n_entries)), "extract malloc store");
switch(h->compression_opt) {
case COMPRESS_NONE:
debug("Extract worker #%u: extract without compression\n", h->id)
nfl_extract_default(f, state);
break;
case COMPRESS_LZ4:
debug("Extract worker #%u: extract with compression algorithm: lz4", h->id)
nfl_extract_lz4(f, state);
break;
case COMPRESS_ZSTD:
debug("Extract worker #%u: extract with compression algorithm: zstd", h->id)
nfl_extract_zstd(f, state);
break;
// Must not reach here ...
default: FATAL("Unknown compression option detected");
}
fclose(f);
return ret;
}
|