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.2 2002/06/09 04:41:25 relnev
19 * added copyright header
21 * Revision 1.1.1.1 2002/05/03 03:28:10 root
25 * 3 3/25/99 11:26a Dave
26 * Beefed up encryption scheme so that even someone viewing the
27 * disassembly would have a hard time cracking it.
29 * 2 10/24/98 11:41p Dave
31 * 1 10/24/98 11:31p Dave
33 * 7 8/09/98 4:44p Lawrance
34 * support alternate encryption scheme (doesn't pack chars into 7 bits)
36 * 6 4/14/98 4:14p Lawrance
37 * fix bug with ships and weapons tbl preprocessing
39 * 5 4/14/98 1:39p Lawrance
40 * Add command line switches to preprocess ship and weapon tables
42 * 4 3/31/98 1:14a Lawrance
43 * Get .tbl and mission file encryption working.
45 * 3 3/30/98 5:57p Lawrance
48 * 2 3/30/98 5:51p Lawrance
49 * file encryption and decryption
63 #define MAX_LINE_LEN 512
65 static int Use_8bit = 0; // set to 1 to disable 7bit character packing
67 // strip out any ships tbl data not used in demo (ie entries without @ preceding name)
68 void scramble_read_ships_tbl(char **text, int *text_len, FILE *fp)
70 char line[MAX_LINE_LEN+1];
71 char token_line[MAX_LINE_LEN+1];
73 int line_len, discard_line = 1, keep_all_lines = 0, post_discard = 0;
74 char seps[] = " ,\t\n";
77 *text_len = _filelength(fileno(fp));
78 *text = (char*)malloc(*text_len+1);
82 while ( fgets(line, MAX_LINE_LEN, fp) != NULL ) {
84 line_len = strlen(line);
85 memcpy(token_line, line, line_len+1);
87 if ( !keep_all_lines ) {
88 token = strtok( token_line, seps );
91 if ( !strnicmp("#End", token, 4) ) {
93 } else if ( !strnicmp("#Ship", token, 5) ) {
96 } else if ( !strnicmp("$Name:", token, 6) ) {
97 token = strtok( NULL, seps );
99 if ( token[0] == '@' ) {
109 if ( !discard_line || keep_all_lines ) {
110 memcpy(dest, line, line_len);
114 if ( post_discard ) {
120 *text_len = dest - *text;
123 // strip out any weapons tbl data not used in demo (ie entries without @ preceding name)
124 void scramble_read_weapons_tbl(char **text, int *text_len, FILE *fp)
126 char line[MAX_LINE_LEN+1];
127 char token_line[MAX_LINE_LEN+1];
129 int line_len, discard_line = 1, keep_all_lines = 0, post_discard = 0;
130 char seps[] = " ,\t\n";
133 *text_len = _filelength(fileno(fp));
134 *text = (char*)malloc(*text_len+1);
138 while ( fgets(line, MAX_LINE_LEN, fp) != NULL ) {
140 line_len = strlen(line);
141 memcpy(token_line, line, line_len+1);
143 if ( !keep_all_lines ) {
144 token = strtok( token_line, seps );
147 if ( !strnicmp("#Countermeasures", token, 16) ) {
149 } else if ( !strnicmp("#End", token, 4) || !strnicmp("#Beam", token, 5) || !strnicmp("#Primary", token, 8) || !strnicmp("#Secondary", token, 10) ) {
152 } else if ( !strnicmp("$Name:", token, 6) ) {
154 token = strtok( NULL, seps );
156 if ( token[0] == '@' ) {
164 if ( (token[0] != ';') && (!discard_line || keep_all_lines) ) {
165 memcpy(dest, line, line_len);
169 if ( post_discard ) {
175 *text_len = dest - *text;
178 void scramble_read_default(char **text, int *text_len, FILE *fp)
180 *text_len = _filelength(fileno(fp));
181 *text = (char*)malloc(*text_len+1);
182 fread( *text, *text_len, 1, fp );
187 // input: src_filename => filename of text to scramble
188 // dest_filename => optional, this is the filename scrambled data will get stored to
189 void scramble_file(char *src_filename, char *dest_filename, int preprocess)
192 int text_len, scramble_len;
193 char *text, *scramble_text;
195 fp = fopen(src_filename, "rb");
200 // read in data, maybe preprocess
202 case PREPROCESS_SHIPS_TBL:
203 scramble_read_ships_tbl(&text, &text_len, fp);
205 case PREPROCESS_WEAPONS_TBL:
206 scramble_read_weapons_tbl(&text, &text_len, fp);
209 // read in the raw data
210 scramble_read_default(&text, &text_len, fp);
216 // open up file for writing scrambled text
217 if ( dest_filename ) {
218 fp = fopen(dest_filename, "wb");
220 fp = fopen(src_filename, "wb");
227 scramble_text = (char*)malloc(text_len+32);
229 encrypt(text, text_len, scramble_text, &scramble_len, Use_8bit);
231 // write out scrambled data
232 fwrite( scramble_text, scramble_len, 1, fp );
241 // input: src_filename => filename of scrambled text
242 // dest_filename => optional, this is the filename unscrambled text data will get stored to
243 void unscramble_file(char *src_filename, char *dest_filename)
246 int scramble_len, text_len;
247 char *text, *scramble_text;
249 fp = fopen(src_filename, "rb");
254 // read in the scrambled data
255 scramble_len = _filelength(fileno(fp));
256 scramble_text = (char*)malloc(scramble_len+1);
257 fread( scramble_text, scramble_len, 1, fp );
260 // open up file for writing unscrambled text
261 if ( dest_filename ) {
262 fp = fopen(dest_filename, "wb");
264 fp = fopen(src_filename, "wb");
270 // assume original text no larger than double scrambled size
271 text = (char*)malloc(scramble_len*2);
273 unencrypt(scramble_text, scramble_len, text, &text_len);
275 // write out unscrambled data
276 fwrite( text, text_len, 1, fp );
283 void print_instructions()
285 printf("Encrypt: scramble [-st] [-wt] <filename_in> [filename_out] \n");
286 printf("Decrypt: scramble -u <filename_in> [filename_out] \n");
289 void main(int argc, char *argv[])
294 scramble_file(argv[1]);
298 if ( !stricmp("-u", argv[1]) ) {
299 unscramble_file(argv[2]);
300 } else if ( !stricmp("-st", argv[1]) ) {
301 scramble_file(argv[2], argv[2], PREPROCESS_SHIPS_TBL);
302 } else if ( !stricmp("-wt", argv[1]) ) {
303 scramble_file(argv[2], argv[2], PREPROCESS_WEAPONS_TBL);
305 scramble_file(argv[1], argv[2]);
310 if ( !stricmp("-u", argv[1]) ) {
311 unscramble_file(argv[2], argv[3]);
312 } else if ( !stricmp("-st", argv[1]) ) {
313 scramble_file(argv[2], argv[3], PREPROCESS_SHIPS_TBL);
314 } else if ( !stricmp("-wt", argv[1]) ) {
315 scramble_file(argv[2], argv[3], PREPROCESS_WEAPONS_TBL);
317 print_instructions();
321 print_instructions();