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
|
/* 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 <stdio.h>
#include <stdlib.h>
#include <zlib.h>
#include "defs.h"
#include "extract.h"
/* Returns a pointer to a buffer containing the
inflated contents of a given memory chunk. */
Bytef *inflate_chunk(Bytef *chunk, uint64_t chunk_size,
uint64_t decompressed_size) {
Bytef *decompressed_data = malloc(decompressed_size);
if (decompressed_data == NULL) {
fprintf(stderr, "Insufficient memory to decompress archive.\n");
free(decompressed_data);
return NULL;
}
z_stream data_stream;
data_stream.zalloc = Z_NULL;
data_stream.zfree = Z_NULL;
data_stream.opaque = Z_NULL;
data_stream.avail_in = 0;
data_stream.next_in = Z_NULL;
if (inflateInit(&data_stream) != Z_OK) {
fprintf(stderr, "Could not initialize zlib.\n");
free(decompressed_data);
return NULL;
}
int status_code;
do {
data_stream.avail_in = chunk_size;
if (data_stream.avail_in == 0)
break;
data_stream.next_in = chunk;
// Flushing probably isn't required here.
do {
data_stream.avail_out = decompressed_size;
data_stream.next_out = decompressed_data;
status_code = inflate(&data_stream, Z_NO_FLUSH);
} while (data_stream.avail_out == 0);
} while (status_code != Z_STREAM_END);
// inflateEnd(&data_stream);
return decompressed_data;
}
/* Decompresses a memory_stream data structure into a new one. */
memory_stream decompress_stream(memory_stream compressed_data, uint64_t size) {
Bytef *decompressed_data = malloc(size);
/* This is a pretty shitty way of handling it. */
if (decompressed_data == NULL) {
fprintf(stderr, "Insufficient memory to decompress archive.\n");
free(compressed_data.start);
free(decompressed_data);
exit(EXIT_FAILURE);
}
decompressed_data = inflate_chunk(compressed_data.start,
compressed_data.stream_length,
size);
if (decompressed_data == NULL)
exit(EXIT_FAILURE);
return (memory_stream) {size, decompressed_data, decompressed_data};
}
/* Reads a FILE pointer into a memory_stream data structure. */
memory_stream read_to_stream(FILE *archive, uint64_t buffer_size) {
Bytef *buffer = malloc(buffer_size);
/* This is a pretty shitty way of handling it. */
if (buffer == NULL) {
fprintf(stderr, "Insufficient memory to load archive.\n");
free(buffer);
fclose(archive);
exit(EXIT_FAILURE);
}
fread(buffer, buffer_size, 1, archive);
return (memory_stream) {buffer_size, buffer, buffer};
}
|