]> icculus.org git repositories - taylor/freespace2.git/blob - src/scramble/scramble.cpp
make sure SDL doesn't mess up main()
[taylor/freespace2.git] / src / scramble / scramble.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell 
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 /*
10  * $Logfile: /Freespace2/code/scramble/scramble.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * Module for file scrambler
16  *
17  * $Log$
18  * Revision 1.3  2003/01/30 20:05:01  relnev
19  * ported (Taylor Richards)
20  *
21  * Revision 1.2  2002/06/09 04:41:25  relnev
22  * added copyright header
23  *
24  * Revision 1.1.1.1  2002/05/03 03:28:10  root
25  * Initial import.
26  *
27  * 
28  * 3     3/25/99 11:26a Dave
29  * Beefed up encryption scheme so that even someone viewing the
30  * disassembly would have a hard time cracking it.
31  * 
32  * 2     10/24/98 11:41p Dave
33  * 
34  * 1     10/24/98 11:31p Dave
35  * 
36  * 7     8/09/98 4:44p Lawrance
37  * support alternate encryption scheme (doesn't pack chars into 7 bits)
38  * 
39  * 6     4/14/98 4:14p Lawrance
40  * fix bug with ships and weapons tbl preprocessing
41  * 
42  * 5     4/14/98 1:39p Lawrance
43  * Add command line switches to preprocess ship and weapon tables
44  * 
45  * 4     3/31/98 1:14a Lawrance
46  * Get .tbl and mission file encryption working.
47  * 
48  * 3     3/30/98 5:57p Lawrance
49  * add some comments
50  * 
51  * 2     3/30/98 5:51p Lawrance
52  * file encryption and decryption
53  *
54  * $NoKeywords: $
55  *
56 */
57
58 #include <stdlib.h>
59 #include <stdio.h>
60 #ifndef PLAT_UNIX
61 #include <io.h>
62 #else
63 #include <sys/stat.h>
64 #endif
65 #include <string.h>
66
67 #define SDL_MAIN_HANDLED
68
69 #include "pstypes.h"
70 #include "encrypt.h"
71 #include "scramble.h"
72
73 #undef malloc
74 #undef free
75 #undef strdup
76
77 #ifndef PLAT_UNIX
78 #define strncasecmp strnicmp
79 #define strcasecmp stricmp
80 #else
81 int _filelength (int fd)
82 {
83         struct stat buf;
84         if (fstat (fd, &buf) == -1)
85                 return -1;
86
87         return buf.st_size;
88 }
89 #endif
90
91 #define MAX_LINE_LEN    512
92
93 //static int Use_8bit = 0;      // set to 1 to disable 7bit character packing
94 static int Use_8bit = 1;
95
96
97 // strip out any ships tbl data not used in demo (ie entries without @ preceding name)
98 void scramble_read_ships_tbl(char **text, int *text_len, FILE *fp)
99 {
100         char    line[MAX_LINE_LEN+1];
101         char    token_line[MAX_LINE_LEN+1];
102         char    *dest;
103         int     line_len, discard_line = 1, keep_all_lines = 0, post_discard = 0;
104         char    seps[]   = " ,\t\n";
105         char    *token;
106
107         *text_len = _filelength(fileno(fp));
108         *text = (char*)malloc(*text_len+1);
109
110         dest = *text;
111
112         while ( fgets(line, MAX_LINE_LEN, fp) != NULL ) {
113
114                 line_len = strlen(line);
115                 memcpy(token_line, line, line_len+1);
116
117                 if ( !keep_all_lines ) {
118                         token = strtok( token_line, seps );
119
120                         if ( token ) {
121                                 if ( !strncasecmp("#End", token, 4) ) {
122                                         keep_all_lines = 1;
123                                 } else if ( !strncasecmp("#Ship", token, 5) ) {
124                                         discard_line = 0;
125                                         post_discard = 1;
126                                 } else if ( !strncasecmp("$Name:", token, 6) ) {
127                                         token = strtok( NULL, seps );
128                                         if ( token ) {
129                                                 if ( token[0] == '@' ) {
130                                                         discard_line = 0;
131                                                 } else {
132                                                         discard_line = 1;
133                                                 }
134                                         }
135                                 }
136                         }
137                 }
138
139                 if ( !discard_line || keep_all_lines ) {
140                         memcpy(dest, line, line_len);
141                         dest += line_len;
142                 }
143
144                 if ( post_discard ) {
145                         discard_line = 1;
146                         post_discard = 0;
147                 } 
148         }
149
150         *text_len = dest - *text;
151 }
152
153 // strip out any weapons tbl data not used in demo (ie entries without @ preceding name)
154 void scramble_read_weapons_tbl(char **text, int *text_len, FILE *fp)
155 {
156         char    line[MAX_LINE_LEN+1];
157         char    token_line[MAX_LINE_LEN+1];
158         char    *dest;
159         int     line_len, discard_line = 1, keep_all_lines = 0, post_discard = 0;
160         char    seps[]   = " ,\t\n";
161         char    *token = NULL;
162
163         *text_len = _filelength(fileno(fp));
164         *text = (char*)malloc(*text_len+1);
165
166         dest = *text;
167
168         while ( fgets(line, MAX_LINE_LEN, fp) != NULL ) {
169
170                 line_len = strlen(line);
171                 memcpy(token_line, line, line_len+1);
172
173                 if ( !keep_all_lines ) {
174                         token = strtok( token_line, seps );
175
176                         if ( token ) {
177                                 if ( !strncasecmp("#Countermeasures", token, 16) ) {
178                                         keep_all_lines = 1;
179                                 } else if ( !strncasecmp("#End", token, 4) || !strncasecmp("#Beam", token, 5) || !strncasecmp("#Primary", token, 8) || !strncasecmp("#Secondary", token, 10) ) {
180                                         discard_line = 0;
181                                         post_discard = 1;
182                                 } else if ( !strncasecmp("$Name:", token, 6) ) {
183                                         discard_line = 1;
184                                         token = strtok( NULL, seps );
185                                         if ( token ) {
186                                                 if ( token[0] == '@' ) {
187                                                         discard_line = 0;
188                                                 }
189                                         }
190                                 }
191                         }
192                 }
193
194                 if ( (token[0] != ';') && (!discard_line || keep_all_lines) ) {
195                         memcpy(dest, line, line_len);
196                         dest += line_len;
197                 }
198
199                 if ( post_discard ) {
200                         discard_line = 1;
201                         post_discard = 0;
202                 } 
203         }
204
205         *text_len = dest - *text;
206 }
207
208 void scramble_read_default(char **text, int *text_len, FILE *fp)
209 {
210         *text_len = _filelength(fileno(fp));
211         *text = (char*)malloc(*text_len+1);
212         fread( *text, *text_len, 1, fp );
213 }
214
215 // scramble a file
216 //
217 // input:       src_filename    =>      filename of text to scramble
218 //                              dest_filename   =>      optional, this is the filename scrambled data will get stored to
219 void scramble_file(char *src_filename, char *dest_filename, int preprocess)
220 {
221         FILE    *fp;
222         int     text_len, scramble_len;
223         char    *text, *scramble_text;
224
225         fp = fopen(src_filename, "rb");
226         if ( !fp ) {
227                 return;
228         }
229
230         // read in data, maybe preprocess
231         switch(preprocess) {
232         case PREPROCESS_SHIPS_TBL:
233                 scramble_read_ships_tbl(&text, &text_len, fp);
234                 break;
235         case PREPROCESS_WEAPONS_TBL:
236                 scramble_read_weapons_tbl(&text, &text_len, fp);
237                 break;
238         default:
239                 // read in the raw data
240                 scramble_read_default(&text, &text_len, fp);
241                 break;
242         }
243
244         fclose(fp);
245
246         // open up file for writing scrambled text
247         if ( dest_filename ) {
248                 fp = fopen(dest_filename, "wb");
249         } else {
250                 fp = fopen(src_filename, "wb");
251         }
252
253         if ( !fp ) {
254                 return;
255         }
256
257         scramble_text = (char*)malloc(text_len+32);
258
259         encrypt(text, text_len, scramble_text, &scramble_len, Use_8bit);
260         
261         // write out scrambled data
262         fwrite( scramble_text, scramble_len, 1, fp );
263
264         free(text);
265         free(scramble_text);
266         fclose(fp);
267 }
268
269 // unscramble a file
270 //
271 // input:       src_filename    =>      filename of scrambled text
272 //                              dest_filename   =>      optional, this is the filename unscrambled text data will get stored to
273 void unscramble_file(char *src_filename, char *dest_filename)
274 {
275         FILE    *fp;
276         int     scramble_len, text_len;
277         char    *text, *scramble_text;
278
279         fp = fopen(src_filename, "rb");
280         if ( !fp ) {
281                 return;
282         }
283
284         // read in the scrambled data
285         scramble_len = _filelength(fileno(fp));
286         scramble_text = (char*)malloc(scramble_len+1);
287         fread( scramble_text, scramble_len, 1, fp );
288         fclose(fp);
289
290         // open up file for writing unscrambled text
291         if ( dest_filename ) {
292                 fp = fopen(dest_filename, "wb");
293         } else {
294                 fp = fopen(src_filename, "wb");
295         }
296         if ( !fp ) {
297                 return;
298         }
299
300         // assume original text no larger than double scrambled size
301         text = (char*)malloc(scramble_len*2);
302
303         unencrypt(scramble_text, scramble_len, text, &text_len);
304
305         // write out unscrambled data
306         fwrite( text, text_len, 1, fp );
307
308         free(text);
309         free(scramble_text);
310         fclose(fp);
311 }
312
313 void print_instructions()
314 {
315         printf("Encrypt: scramble [-st] [-wt] <filename_in> [filename_out] \n");
316         printf("Decrypt: scramble -u <filename_in> [filename_out] \n");
317 }
318
319 int main(int argc, char *argv[])
320 {
321         switch (argc) {
322         case 2:
323                 encrypt_init();
324                 scramble_file(argv[1]);
325                 break;
326         case 3:
327                 encrypt_init();
328                 if ( !strcasecmp("-u", argv[1]) ) {
329                         unscramble_file(argv[2]);
330                 } else if ( !strcasecmp("-st", argv[1]) ) {
331                         scramble_file(argv[2], argv[2], PREPROCESS_SHIPS_TBL);
332                 } else if ( !strcasecmp("-wt", argv[1]) ) {
333                         scramble_file(argv[2], argv[2], PREPROCESS_WEAPONS_TBL);
334                 } else {
335                         scramble_file(argv[1], argv[2]);
336                 }
337                 break;
338         case 4:
339                 encrypt_init();
340                 if ( !strcasecmp("-u", argv[1]) ) {
341                         unscramble_file(argv[2], argv[3]);
342                 } else if ( !strcasecmp("-st", argv[1]) ) {
343                         scramble_file(argv[2], argv[3], PREPROCESS_SHIPS_TBL);
344                 } else if ( !strcasecmp("-wt", argv[1]) ) {
345                         scramble_file(argv[2], argv[3], PREPROCESS_WEAPONS_TBL);
346                 } else {
347                         print_instructions();
348                 }
349                 break;
350         default:
351                 print_instructions();
352                 break;
353         }
354
355         return 0;
356 }
357