]> de.git.xonotic.org Git - voretournament/voretournament.git/blob - misc/source/fteqcc-src/qcd_main.c
Get VoreTournament code to compile with gmqcc. To be compiled with the same parameter...
[voretournament/voretournament.git] / misc / source / fteqcc-src / qcd_main.c
1 #include "progsint.h"
2 //#include "qcc.h"
3
4 //#define AVAIL_ZLIB
5
6 #ifdef AVAIL_ZLIB
7 #ifdef _WIN32
8 #define ZEXPORT VARGS
9 #include "../libs/zlib.h"
10
11 #ifdef _WIN64
12 # pragma comment (lib, "../libs/zlib64.lib") 
13 #else
14 # pragma comment (lib, "../libs/zlib.lib") 
15 #endif
16 #else
17 #include <zlib.h>
18 #endif
19 #endif
20
21 pbool QC_decodeMethodSupported(int method)
22 {
23         if (method == 0)
24                 return true;
25         if (method == 1)
26                 return true;
27         if (method == 2)
28         {
29 #ifdef AVAIL_ZLIB
30                 return false;
31 #endif
32         }
33         return false;
34 }
35
36 char *QC_decode(progfuncs_t *progfuncs, int complen, int len, int method, char *info, char *buffer)
37 {
38         int i;
39         if (method == 0)        //copy
40         {
41                 if (complen != len) Sys_Error("lengths do not match");
42                 memcpy(buffer, info, len);              
43         }
44         else if (method == 1)   //xor encryption
45         {
46                 if (complen != len) Sys_Error("lengths do not match");
47                 for (i = 0; i < len; i++)
48                         buffer[i] = info[i] ^ 0xA5;             
49         }
50         else if (method == 2)   //compression (ZLIB)
51         {
52 #ifdef AVAIL_ZLIB
53                 z_stream strm = {
54                         info,
55                         complen,
56                         0,
57
58                         buffer,
59                         len,
60                         0,
61
62                         NULL,
63                         NULL,
64
65                         NULL,
66                         NULL,
67                         NULL,
68
69                         Z_BINARY,
70                         0,
71                         0
72                 };
73
74                 inflateInit(&strm);
75                 if (Z_STREAM_END != inflate(&strm, Z_FINISH))   //decompress it in one go.
76                         Sys_Error("Failed block decompression\n");
77                 inflateEnd(&strm);
78 #endif
79         }
80         //add your decryption/decompression routine here.
81         else
82                 Sys_Error("Bad file encryption routine\n");
83
84
85         return buffer;
86 }
87
88 #ifndef MINIMAL
89 void SafeWrite(int hand, void *buf, long count);
90 int SafeSeek(int hand, int ofs, int mode);
91 //we are allowed to trash our input here.
92 int QC_encode(progfuncs_t *progfuncs, int len, int method, char *in, int handle)
93 {
94         int i;
95         if (method == 0) //copy
96         {               
97                 SafeWrite(handle, in, len);
98                 return len;
99         }
100         else if (method == 1)   //xor encryption
101         {
102                 for (i = 0; i < len; i++)
103                         in[i] = in[i] ^ 0xA5;
104                 SafeWrite(handle, in, len);
105                 return len;
106         }
107         else if (method == 2)   //compression (ZLIB)
108         {
109 #ifdef AVAIL_ZLIB
110                 char out[8192];
111
112                 z_stream strm = {
113                         in,
114                         len,
115                         0,
116
117                         out,
118                         sizeof(out),
119                         0,
120
121                         NULL,
122                         NULL,
123
124                         NULL,
125                         NULL,
126                         NULL,
127
128                         Z_BINARY,
129                         0,
130                         0
131                 };
132                 i=0;
133
134                 deflateInit(&strm, Z_BEST_COMPRESSION);
135                 while(deflate(&strm, Z_FINISH) == Z_OK)
136                 {
137                         SafeWrite(handle, out, sizeof(out) - strm.avail_out);   //compress in chunks of 8192. Saves having to allocate a huge-mega-big buffer
138                         i+=sizeof(out) - strm.avail_out;
139                         strm.next_out = out;
140                         strm.avail_out = sizeof(out);
141                 }
142                 SafeWrite(handle, out, sizeof(out) - strm.avail_out);
143                 i+=sizeof(out) - strm.avail_out;
144                 deflateEnd(&strm);
145                 return i;
146 #endif
147                 Sys_Error("ZLIB compression not supported in this build");
148                 return 0;
149         }
150         //add your compression/decryption routine here.
151         else
152         {
153                 Sys_Error("Wierd method");
154                 return 0;
155         }
156 }
157 #endif
158
159 char *filefromprogs(progfuncs_t *progfuncs, progsnum_t prnum, char *fname, int *size, char *buffer)
160 {
161         int num;
162         includeddatafile_t *s;
163         if (!pr_progstate[prnum].progs)
164                 return NULL;
165         if (pr_progstate[prnum].progs->version != PROG_EXTENDEDVERSION)
166                 return NULL;
167         if (!pr_progstate[prnum].progs->secondaryversion != PROG_SECONDARYVERSION16 &&
168                 !pr_progstate[prnum].progs->secondaryversion != PROG_SECONDARYVERSION32)
169                 return NULL;
170         
171         num = *(int*)((char *)pr_progstate[prnum].progs + pr_progstate[prnum].progs->ofsfiles);
172         s = (includeddatafile_t *)((char *)pr_progstate[prnum].progs + pr_progstate[prnum].progs->ofsfiles+4);  
173         while(num>0)
174         {
175                 if (!strcmp(s->filename, fname))
176                 {
177                         if (size)
178                                 *size = s->size;
179                         if (!buffer)
180                                 return (char *)0xffffffff;
181                         return QC_decode(progfuncs, s->compsize, s->size, s->compmethod, (char *)pr_progstate[prnum].progs+s->ofs, buffer);
182                 }
183
184                 s++;
185                 num--;
186         }       
187
188         if (size)
189                 *size = 0;
190         return NULL;
191 }
192
193 /*
194 char *filefromnewprogs(progfuncs_t *progfuncs, char *prname, char *fname, int *size, char *buffer)
195 {
196         int num;
197         includeddatafile_t *s;  
198         progstate_t progs;
199         if (!PR_ReallyLoadProgs(progfuncs, prname, -1, &progs, false))
200         {
201                 if (size)
202                         *size = 0;
203                 return NULL;
204         }
205
206         if (progs.progs->version < PROG_EXTENDEDVERSION)
207                 return NULL;
208         if (!progs.progs->ofsfiles)
209                 return NULL;
210
211         num = *(int*)((char *)progs.progs + progs.progs->ofsfiles);
212         s = (includeddatafile_t *)((char *)progs.progs + progs.progs->ofsfiles+4);      
213         while(num>0)
214         {
215                 if (!strcmp(s->filename, fname))
216                 {
217                         if (size)
218                                 *size = s->size;
219                         if (!buffer)
220                                 return (char *)0xffffffff;
221                         return QC_decode(progfuncs, s->compsize, s->size, s->compmethod, (char *)progs.progs+s->ofs, buffer);
222                 }
223
224                 s++;
225                 num--;
226         }       
227
228         if (size)
229                 *size = 0;
230         return NULL;
231 }
232 */