/
misc_util.cpp
355 lines (293 loc) · 6.41 KB
/
misc_util.cpp
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#include "PlatformPrecomp.h"
#include "misc_util.h"
#include "util/MiscUtils.h"
void strchar(char *string, char ch)
/* This acts in the same way as strcat except it combines a string and
a single character, updating the null at the end. */
{
int last;
last=strlen(string);
string[last]=ch;
string[last+1]=0;
}
void dink_decompress( unsigned char *in, char * destBuf )
{
const int stackSize = 2*1024;
unsigned char stack[stackSize], pair[128][2];
int c, top = 0;
memset(stack, 0, stackSize);
memset(pair, 0, 128*2);
int outputSize = 0;
c = *in; in++;
if (c > 127)
{
//read optional pair count and pair table
int readCount = (c-128)*2;
memcpy(&pair,in, readCount );
in += readCount;
}
else
{
if (c == '\r') c = '\n';
if (c == 9) c = ' ';
strchar(destBuf,c);
}
// putc(c,out);
for (;;)
{
/* Pop byte from stack or read byte from file */
if (top)
c = stack[--top];
else
{
if ((c = *in) == 0) break;
in++;
}
/* Push pair on stack or output byte to file */
if (c > 127)
{
if (top >= stackSize )
{
#ifdef _DEBUG
LogMsg("Malformed .d file, can't read it. Would overwrite random memory on the old Dink versions.");
LogMsg("Decompressed to %d bytes", outputSize);
#endif
destBuf[outputSize] = 0;
return;
}
stack[top++] = pair[c-128][1];
stack[top++] = pair[c-128][0];
}
else
{
if (c == '\r') c = '\n';
if (c == 9) c = ' ';
strchar(destBuf,c);// putc(c,out);
outputSize++;
}
}
destBuf[outputSize] = 0;
#ifdef _DEBUG
//LogMsg("Decompressed to %d bytes", outputSize);
#endif
}
//this legacy code is very stupid, but I'll leave it for now, it works, whatever
void decompress_nocomp (byte *in, char destBuf[])
{
const int stackSize = 2*1024;
unsigned char stack[stackSize], pair[128][2];
memset(stack, 0, stackSize);
int c, top = 0;
int outputSize = 0;
c = *in; in++;
if (0)
{
//read optional pair count and pair table
int readCount = (c-128)*2;
memcpy(&pair,in, readCount );
in += readCount;
}
else
{
if (c == '\r') c = '\n';
if (c == 9) c = ' ';
strchar(destBuf,c);
}
// putc(c,out);
for (;;)
{
/* Pop byte from stack or read byte from file */
if (top)
c = stack[--top];
else
{
if ((c = *in) == 0) break;
in++;
}
/* Push pair on stack or output byte to file */
if (0)
{
if (top >= stackSize )
{
#ifdef _DEBUG
LogMsg("Malformed .c file, can't read it. Would overwrite random memory on the old Dink versions.");
destBuf[outputSize] = 0;
#endif
return;
}
stack[top++] = pair[c-128][1];
stack[top++] = pair[c-128][0];
}
else
{
if (c == '\r') c = '\n';
if (c == 9) c = ' ';
strchar(destBuf,c);// putc(c,out);
outputSize++;
}
}
destBuf[outputSize] = 0;
#ifdef _DEBUG
//LogMsg("Decompressed to %d bytes", outputSize);
#endif
}
/*
void decompress (FILE *in, char destBuf[])
{
const int stackSize = 512*1024;
unsigned char stack[stackSize], pair[128][2];
int c, top = 0;
int outputSize = 0;
if ((c = getc(in)) > 127)
fread(pair,2,c-128,in);
else
{
if (c == '\r') c = '\n';
if (c == 9) c = ' ';
strchar(destBuf,c);
}
// putc(c,out);
for (;;) {
if (top)
c = stack[--top];
else if ((c = getc(in)) == EOF)
break;
if (c > 127)
{
if (top >= stackSize )
{
LogMsg("Malformed .d file, can't read it. Would overwrite random memory on the old Dink versions.");
destBuf[outputSize] = 0;
return;
}
stack[top++] = pair[c-128][1];
stack[top++] = pair[c-128][0];
}
else
{
if (c == '\r') c = '\n';
if (c == 9) c = ' ';
strchar(destBuf,c);// putc(c,out);
outputSize++;
}
}
destBuf[outputSize] = 0;
#ifdef _DEBUG
//LogMsg("Decompressed to %d bytes", outputSize);
#endif
}
void decompress_nocomp (FILE *in, char destBuf[])
{
//let's do it, only this time decompile OUR style
unsigned char stack[16], pair[128][2];
short c, top = 0;
if ((c = getc(in)) > 255)
fread(pair,2,c-128,in);
else
{
if (c == '\r') c = '\n';
if (c == 9) c = ' ';
strchar(destBuf,c);
}
// putc(c,out);
for (;;) {
if (top)
c = stack[--top];
else if ((c = getc(in)) == EOF)
break;
if (c > 255) {
stack[top++] = pair[c-128][1];
stack[top++] = pair[c-128][0];
}
else
{
if (c == '\r') c = '\n';
if (c == 9) c = ' ';
strchar(destBuf,c);// putc(c,out);
}
}
}
*/
void replace(const char *this1, char *that, char *line)
{
char hold[500];
char thisup[512],lineup[512];
int u,i;
int checker;
start:
strcpy(hold,"");
strcpy(lineup,line);
strcpy(thisup,this1);
ToUpperCase(lineup);
ToUpperCase(thisup);
if (strstr(lineup,thisup) == NULL) return;
checker = -1;
strcpy(hold,"");
for (u = 0; u < strlen(line); u++)
{
if (checker > -1)
{
if (toupper(line[u]) == toupper(this1[checker]))
{
if (checker+1 == strlen(this1))
{
doit:
u = u - strlen(this1);
u++;
for (i = 0; i < u; i++) hold[i] = line[i];
for (i = 0; i < strlen(that); i++) hold[(u)+i]=that[i];
hold[strlen(that)+u] = 0;
for (i = 0; i < (strlen(line)-u)-strlen(this1); i++)
{
hold[(u+strlen(that))+i] = line[(u+strlen(this1))+i];
}
hold[(strlen(line)-strlen(this1))+strlen(that)] = 0;
strcpy(line,hold);
goto start;
}
checker++;
} else { checker = -1; }
}
if( checker == -1)
{
if (toupper(line[u]) == toupper(this1[0]))
{
checker = 1;
if (strlen(this1) == 1) goto doit;
}
}
}
}
bool separate_string (const char str[255], int num, char liney, char *return1)
{
int l;
int k;
l = 1;
strcpy(return1 ,"");
for (k = 0; k <= strlen(str); k++)
{
if (str[k] == liney)
{
l++;
if (l == num+1)
goto done;
if (k < strlen(str)) strcpy(return1,"");
}
if (str[k] != liney)
sprintf(return1, "%s%c",return1 ,str[k]);
}
if (l < num) strcpy(return1,"");
replace("\n","",return1); //Take the /n off it.
/*
char s[2];
s[0] = 9; //tab
s[1] = 0; //null
replace(s,"",return1); //Take the /n off it.
*/
return(false);
done:
if (l < num) strcpy(return1,"");
replace("\n","",return1); //Take the /n off it.
//Msg("Took %s and turned it to %s.",str, return1);
return(true);
}