-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decompress.c
126 lines (108 loc) · 3.42 KB
/
decompress.c
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
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zlib.h>
// This function is copied from QuickBMS
int unzip_deflate(unsigned char *in, int insz, unsigned char *out, int outsz, int no_error) {
static z_stream *z = NULL;
int ret,
sync = Z_FINISH;
if(!in && !out) {
if(z) {
inflateEnd(z);
free(z);
}
z = NULL;
return -1;
}
if(!z) {
z = calloc(sizeof(z_stream), 1);
if(!z) return -1;
if(inflateInit2(z, -15)) {
fprintf(stderr, "\nError: zlib initialization error\n");
return -1;
}
}
inflateReset(z);
z->next_in = in;
z->avail_in = insz;
z->next_out = out;
z->avail_out = outsz;
ret = inflate(z, sync);
if((ret != Z_STREAM_END) && !no_error) {
fprintf(stderr, "\nError: the compressed zlib/deflate input is wrong or incomplete (%d)\n", ret);
return -1;
}
return z->total_out;
}
static unsigned short GetShort(FILE *file)
{
const unsigned char byte = fgetc(file);
return (byte << 8) | fgetc(file);
}
static unsigned long GetLong(FILE *file)
{
const unsigned char byte1 = fgetc(file);
const unsigned char byte2 = fgetc(file);
const unsigned char byte3 = fgetc(file);
return (byte1 << 24) | (byte2 << 16) | (byte3 << 8) | fgetc(file);
}
int main(int argc, char *argv[])
{
if (argc >= 1)
{
FILE *in_file = fopen(argv[1], "rb");
if (in_file)
{
FILE *out_file = fopen("out.ar.00", "wb");
char fourcc[4];
fread(fourcc, 4, 1, in_file);
if (!memcmp(fourcc, "segs", 4))
{
const unsigned short unknown = GetShort(in_file);
printf("Unknown -> 0x%X\n", unknown);
const unsigned short chunk_count = GetShort(in_file);
printf("Chunk count -> 0x%X\n", chunk_count);
const unsigned long uncompressed_size = GetLong(in_file);
const unsigned long compressed_size = GetLong(in_file);
for (unsigned int i = 0; i < chunk_count; ++i)
{
const unsigned short chunk_compressed_size = GetShort(in_file);
printf("Chunk compressed size -> 0x%X\n", chunk_compressed_size);
unsigned long chunk_uncompressed_size = GetShort(in_file);
if (chunk_uncompressed_size == 0)
chunk_uncompressed_size = 0x10000;
printf("Chunk uncompressed size -> 0x%lX\n", chunk_uncompressed_size);
const unsigned long chunk_offset = GetLong(in_file);
printf("Chunk offset -> 0x%lX\n", chunk_offset);
const long position = ftell(in_file);
fseek(in_file, chunk_offset - 1, SEEK_SET);
unsigned char *uncompressed_buffer = malloc(chunk_uncompressed_size);
unsigned char *compressed_buffer = malloc(chunk_compressed_size);
fread(compressed_buffer, 1, chunk_compressed_size, in_file);
unzip_deflate(compressed_buffer, chunk_compressed_size, uncompressed_buffer, chunk_uncompressed_size, 0);
fwrite(uncompressed_buffer, 1, chunk_uncompressed_size, out_file);
free(uncompressed_buffer);
free(compressed_buffer);
fseek(in_file, position, SEEK_SET);
}
}
else
{
printf("File is not a segs archive\n");
}
unzip_deflate(NULL, 0, NULL, 0, 0); // Deinit
fclose(in_file);
fclose(out_file);
}
else
{
printf("Could not open input file\n");
}
}
else
{
printf("No input file\n");
}
}