2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
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
10 * $Logfile: /Freespace2/code/scramble/scramble.cpp $
15 * Module for file scrambler
18 * Revision 1.3 2003/01/30 20:05:01 relnev
19 * ported (Taylor Richards)
21 * Revision 1.2 2002/06/09 04:41:25 relnev
22 * added copyright header
24 * Revision 1.1.1.1 2002/05/03 03:28:10 root
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.
32 * 2 10/24/98 11:41p Dave
34 * 1 10/24/98 11:31p Dave
36 * 7 8/09/98 4:44p Lawrance
37 * support alternate encryption scheme (doesn't pack chars into 7 bits)
39 * 6 4/14/98 4:14p Lawrance
40 * fix bug with ships and weapons tbl preprocessing
42 * 5 4/14/98 1:39p Lawrance
43 * Add command line switches to preprocess ship and weapon tables
45 * 4 3/31/98 1:14a Lawrance
46 * Get .tbl and mission file encryption working.
48 * 3 3/30/98 5:57p Lawrance
51 * 2 3/30/98 5:51p Lawrance
52 * file encryption and decryption
67 #define SDL_MAIN_HANDLED
78 #define strncasecmp strnicmp
79 #define strcasecmp stricmp
81 int _filelength (int fd)
84 if (fstat (fd, &buf) == -1)
91 #define MAX_LINE_LEN 512
93 //static int Use_8bit = 0; // set to 1 to disable 7bit character packing
94 static int Use_8bit = 1;
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)
100 char line[MAX_LINE_LEN+1];
101 char token_line[MAX_LINE_LEN+1];
103 int line_len, discard_line = 1, keep_all_lines = 0, post_discard = 0;
104 char seps[] = " ,\t\n";
107 *text_len = _filelength(fileno(fp));
108 *text = (char*)malloc(*text_len+1);
112 while ( fgets(line, MAX_LINE_LEN, fp) != NULL ) {
114 line_len = strlen(line);
115 memcpy(token_line, line, line_len+1);
117 if ( !keep_all_lines ) {
118 token = strtok( token_line, seps );
121 if ( !strncasecmp("#End", token, 4) ) {
123 } else if ( !strncasecmp("#Ship", token, 5) ) {
126 } else if ( !strncasecmp("$Name:", token, 6) ) {
127 token = strtok( NULL, seps );
129 if ( token[0] == '@' ) {
139 if ( !discard_line || keep_all_lines ) {
140 memcpy(dest, line, line_len);
144 if ( post_discard ) {
150 *text_len = dest - *text;
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)
156 char line[MAX_LINE_LEN+1];
157 char token_line[MAX_LINE_LEN+1];
159 int line_len, discard_line = 1, keep_all_lines = 0, post_discard = 0;
160 char seps[] = " ,\t\n";
163 *text_len = _filelength(fileno(fp));
164 *text = (char*)malloc(*text_len+1);
168 while ( fgets(line, MAX_LINE_LEN, fp) != NULL ) {
170 line_len = strlen(line);
171 memcpy(token_line, line, line_len+1);
173 if ( !keep_all_lines ) {
174 token = strtok( token_line, seps );
177 if ( !strncasecmp("#Countermeasures", token, 16) ) {
179 } else if ( !strncasecmp("#End", token, 4) || !strncasecmp("#Beam", token, 5) || !strncasecmp("#Primary", token, 8) || !strncasecmp("#Secondary", token, 10) ) {
182 } else if ( !strncasecmp("$Name:", token, 6) ) {
184 token = strtok( NULL, seps );
186 if ( token[0] == '@' ) {
194 if ( (token[0] != ';') && (!discard_line || keep_all_lines) ) {
195 memcpy(dest, line, line_len);
199 if ( post_discard ) {
205 *text_len = dest - *text;
208 void scramble_read_default(char **text, int *text_len, FILE *fp)
210 *text_len = _filelength(fileno(fp));
211 *text = (char*)malloc(*text_len+1);
213 if ( !fread(*text, *text_len, 1, fp) ) {
222 // input: src_filename => filename of text to scramble
223 // dest_filename => optional, this is the filename scrambled data will get stored to
224 void scramble_file(char *src_filename, char *dest_filename, int preprocess)
227 int text_len, scramble_len;
228 char *text, *scramble_text;
230 fp = fopen(src_filename, "rb");
235 // read in data, maybe preprocess
237 case PREPROCESS_SHIPS_TBL:
238 scramble_read_ships_tbl(&text, &text_len, fp);
240 case PREPROCESS_WEAPONS_TBL:
241 scramble_read_weapons_tbl(&text, &text_len, fp);
244 // read in the raw data
245 scramble_read_default(&text, &text_len, fp);
255 // open up file for writing scrambled text
256 if ( dest_filename ) {
257 fp = fopen(dest_filename, "wb");
259 fp = fopen(src_filename, "wb");
266 scramble_text = (char*)malloc(text_len+32);
268 encrypt(text, text_len, scramble_text, &scramble_len, Use_8bit);
270 // write out scrambled data
271 fwrite( scramble_text, scramble_len, 1, fp );
280 // input: src_filename => filename of scrambled text
281 // dest_filename => optional, this is the filename unscrambled text data will get stored to
282 void unscramble_file(char *src_filename, char *dest_filename)
285 int scramble_len, text_len;
286 char *text, *scramble_text;
288 fp = fopen(src_filename, "rb");
293 // read in the scrambled data
294 scramble_len = _filelength(fileno(fp));
295 scramble_text = (char*)malloc(scramble_len+1);
297 if ( !fread(scramble_text, scramble_len, 1, fp) ) {
305 // open up file for writing unscrambled text
306 if ( dest_filename ) {
307 fp = fopen(dest_filename, "wb");
309 fp = fopen(src_filename, "wb");
315 // assume original text no larger than double scrambled size
316 text = (char*)malloc(scramble_len*2);
318 unencrypt(scramble_text, scramble_len, text, &text_len);
320 // write out unscrambled data
321 fwrite( text, text_len, 1, fp );
328 void print_instructions()
330 printf("Encrypt: scramble [-st] [-wt] <filename_in> [filename_out] \n");
331 printf("Decrypt: scramble -u <filename_in> [filename_out] \n");
334 int main(int argc, char *argv[])
339 scramble_file(argv[1]);
343 if ( !strcasecmp("-u", argv[1]) ) {
344 unscramble_file(argv[2]);
345 } else if ( !strcasecmp("-st", argv[1]) ) {
346 scramble_file(argv[2], argv[2], PREPROCESS_SHIPS_TBL);
347 } else if ( !strcasecmp("-wt", argv[1]) ) {
348 scramble_file(argv[2], argv[2], PREPROCESS_WEAPONS_TBL);
350 scramble_file(argv[1], argv[2]);
355 if ( !strcasecmp("-u", argv[1]) ) {
356 unscramble_file(argv[2], argv[3]);
357 } else if ( !strcasecmp("-st", argv[1]) ) {
358 scramble_file(argv[2], argv[3], PREPROCESS_SHIPS_TBL);
359 } else if ( !strcasecmp("-wt", argv[1]) ) {
360 scramble_file(argv[2], argv[3], PREPROCESS_WEAPONS_TBL);
362 print_instructions();
366 print_instructions();