-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract.c
182 lines (147 loc) · 3.81 KB
/
extract.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
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
#include "archive.h"
#include "compress.h"
#include "graphics.h"
#include "extract.h"
#include "musx.h"
#include "utils.h"
#include <stdio.h>
#include <stdlib.h>
#ifdef __riscos
#include "kernel.h"
#include "swis.h"
#elif defined(_WIN32) || defined(__WATCOMC__)
#include <direct.h>
#include <io.h>
#else
#include <sys/stat.h>
#endif
#ifdef __riscos
# define PATH_SEP "."
#elif defined(_WIN32)
# define PATH_SEP "\\"
#else
# define PATH_SEP "/"
#endif
static void my_mkdir(const char *path) {
#ifdef __riscos
_kernel_swi_regs regs;
regs.r[0] = 8;
regs.r[1] = (int)path;
_kernel_swi(OS_File, ®s, ®s);
#elif defined(_WIN32)
_mkdir(path);
#elif defined(__WATCOMC__)
mkdir(path);
#else
mkdir(path, 0755);
#endif
}
static void my_settype(const char *path, int type) {
#ifdef __riscos
_kernel_swi_regs regs;
regs.r[0] = 18;
regs.r[1] = (int)path;
regs.r[2] = type;
_kernel_swi(OS_File, ®s, ®s);
#else
(void)path;
(void)type;
#endif
}
FILE *open_output_file(const char *path, const char *name, int ftype, int nfs_exts) {
FILE *output;
char filename[1024];
if (nfs_exts) {
snprintf(filename, 1024, "%s" PATH_SEP "%s,%03x", path, name, ftype);
} else {
snprintf(filename, 1024, "%s" PATH_SEP "%s", path, name);
}
my_mkdir(path);
output = fopen(filename, "wb");
if (!output) {
warningf("Could not open file %s", filename);
return NULL;
}
my_settype(filename, ftype);
return output;
}
uint32_t dump_entry_to_file(FILE *input, biik_archive_entry *entry, const char *path, int nfs_exts) {
FILE *output;
size_t size, read;
output = open_output_file(path, entry->name, entry_to_file_type(entry->type), nfs_exts);
if (!output) {
return 0;
}
size = (size_t)(entry->size - entry->header_size);
read = fcopy(input, output, size);
fclose(output);
return (uint32_t)(read);
}
uint32_t dump_graphic_to_file(FILE *input, biik_archive_entry *entry, const char *path, int nfs_exts) {
FILE *output;
uint32_t size;
output = open_output_file(path, entry->name, 0xff9, nfs_exts);
if (!output) {
return 0;
}
size = decompress_graphic(input, output);
fclose(output);
return size;
}
uint32_t dump_tracker_to_file(FILE *input, biik_archive_entry *entry, const char *path, int nfs_exts) {
FILE *output;
uint32_t size;
output = open_output_file(path, entry->name, 0xcb6, nfs_exts);
if (!output) {
return 0;
}
size = decompress_musx(input, output);
fclose(output);
return size;
}
uint32_t dump_script_to_file(FILE *input, biik_archive_entry *entry, const char *path, int nfs_exts) {
FILE *output;
uint32_t unknown, size, realsize;
unsigned char buf[8];
unsigned char *block;
fread(buf, 8, 1, input);
if (buf[3]) {
unknown = read_u32(buf + 0, 1);
size = read_u32(buf + 4, 1);
} else {
unknown = read_u32(buf + 0, 0);
size = read_u32(buf + 4, 0);
}
if (unknown != 1)
warningf("Unexpected script header: %d", unknown);
block = malloc(size);
if (!block) {
return 0;
}
realsize = lzw_decode(input, block, size);
if (size != realsize)
warningf("Unexpected end of stream in file %s: expected %d bytes, got %d bytes", entry->name, size, realsize);
output = open_output_file(path, entry->name, 0xfff, nfs_exts);
if (!output) {
free(block);
return 0;
}
fwrite(block, realsize, 1, output);
fclose(output);
free(block);
return realsize;
}
uint32_t dump_to_file(FILE *context, biik_archive_entry *entry, const char *path, int nfs_exts, int convert) {
fseek(context, (long)(entry->offset + entry->header_size), SEEK_SET);
if (convert) {
switch (entry->type) {
case ENTRY_TYPE_GRAPHIC:
return dump_graphic_to_file(context, entry, path, nfs_exts);
case ENTRY_TYPE_TRACKER:
return dump_tracker_to_file(context, entry, path, nfs_exts);
case ENTRY_TYPE_SCRIPT:
return dump_script_to_file(context, entry, path, nfs_exts);
}
}
return dump_entry_to_file(context, entry, path, nfs_exts);
}