-
Notifications
You must be signed in to change notification settings - Fork 0
/
qcd_main.c
228 lines (200 loc) · 4.35 KB
/
qcd_main.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
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
#include "progsint.h"
//#include "qcc.h"
//#define AVAIL_ZLIB
#ifdef AVAIL_ZLIB
#ifdef _WIN32
#define ZEXPORT VARGS
#include "../libs/zlib.h"
# pragma comment (lib, "../libs/zlib.lib")
#else
#include <zlib.h>
#endif
#endif
pbool QC_decodeMethodSupported(int method)
{
if (method == 0)
return true;
if (method == 1)
return true;
if (method == 2)
{
#ifdef AVAIL_ZLIB
return false;
#endif
}
return false;
}
char *QC_decode(progfuncs_t *progfuncs, int complen, int len, int method, char *info, char *buffer)
{
int i;
if (method == 0) //copy
{
if (complen != len) Sys_Error("lengths do not match");
memcpy(buffer, info, len);
}
else if (method == 1) //xor encryption
{
if (complen != len) Sys_Error("lengths do not match");
for (i = 0; i < len; i++)
buffer[i] = info[i] ^ 0xA5;
}
else if (method == 2) //compression (ZLIB)
{
#ifdef AVAIL_ZLIB
z_stream strm = {
info,
complen,
0,
buffer,
len,
0,
NULL,
NULL,
NULL,
NULL,
NULL,
Z_BINARY,
0,
0
};
inflateInit(&strm);
if (Z_STREAM_END != inflate(&strm, Z_FINISH)) //decompress it in one go.
Sys_Error("Failed block decompression\n");
inflateEnd(&strm);
#endif
}
//add your decryption/decompression routine here.
else
Sys_Error("Bad file encryption routine\n");
return buffer;
}
#ifndef MINIMAL
void SafeWrite(int hand, void *buf, long count);
int SafeSeek(int hand, int ofs, int mode);
//we are allowed to trash our input here.
int QC_encode(progfuncs_t *progfuncs, int len, int method, char *in, int handle)
{
int i;
if (method == 0) //copy
{
SafeWrite(handle, in, len);
return len;
}
else if (method == 1) //xor encryption
{
for (i = 0; i < len; i++)
in[i] = in[i] ^ 0xA5;
SafeWrite(handle, in, len);
return len;
}
else if (method == 2) //compression (ZLIB)
{
#ifdef AVAIL_ZLIB
char out[8192];
z_stream strm = {
in,
len,
0,
out,
sizeof(out),
0,
NULL,
NULL,
NULL,
NULL,
NULL,
Z_BINARY,
0,
0
};
i=0;
deflateInit(&strm, Z_BEST_COMPRESSION);
while(deflate(&strm, Z_FINISH) == Z_OK)
{
SafeWrite(handle, out, sizeof(out) - strm.avail_out); //compress in chunks of 8192. Saves having to allocate a huge-mega-big buffer
i+=sizeof(out) - strm.avail_out;
strm.next_out = out;
strm.avail_out = sizeof(out);
}
SafeWrite(handle, out, sizeof(out) - strm.avail_out);
i+=sizeof(out) - strm.avail_out;
deflateEnd(&strm);
return i;
#endif
Sys_Error("ZLIB compression not supported in this build");
return 0;
}
//add your compression/decryption routine here.
else
{
Sys_Error("Wierd method");
return 0;
}
}
#endif
char *filefromprogs(progfuncs_t *progfuncs, progsnum_t prnum, char *fname, int *size, char *buffer)
{
int num;
includeddatafile_t *s;
if (!pr_progstate[prnum].progs)
return NULL;
if (pr_progstate[prnum].progs->version != PROG_EXTENDEDVERSION)
return NULL;
if (!pr_progstate[prnum].progs->secondaryversion != PROG_SECONDARYVERSION16 &&
!pr_progstate[prnum].progs->secondaryversion != PROG_SECONDARYVERSION32)
return NULL;
num = *(int*)((char *)pr_progstate[prnum].progs + pr_progstate[prnum].progs->ofsfiles);
s = (includeddatafile_t *)((char *)pr_progstate[prnum].progs + pr_progstate[prnum].progs->ofsfiles+4);
while(num>0)
{
if (!strcmp(s->filename, fname))
{
if (size)
*size = s->size;
if (!buffer)
return (char *)0xffffffff;
return QC_decode(progfuncs, s->compsize, s->size, s->compmethod, (char *)pr_progstate[prnum].progs+s->ofs, buffer);
}
s++;
num--;
}
if (size)
*size = 0;
return NULL;
}
/*
char *filefromnewprogs(progfuncs_t *progfuncs, char *prname, char *fname, int *size, char *buffer)
{
int num;
includeddatafile_t *s;
progstate_t progs;
if (!PR_ReallyLoadProgs(progfuncs, prname, -1, &progs, false))
{
if (size)
*size = 0;
return NULL;
}
if (progs.progs->version < PROG_EXTENDEDVERSION)
return NULL;
if (!progs.progs->ofsfiles)
return NULL;
num = *(int*)((char *)progs.progs + progs.progs->ofsfiles);
s = (includeddatafile_t *)((char *)progs.progs + progs.progs->ofsfiles+4);
while(num>0)
{
if (!strcmp(s->filename, fname))
{
if (size)
*size = s->size;
if (!buffer)
return (char *)0xffffffff;
return QC_decode(progfuncs, s->compsize, s->size, s->compmethod, (char *)progs.progs+s->ofs, buffer);
}
s++;
num--;
}
if (size)
*size = 0;
return NULL;
}
*/