2 * $Logfile: /Freespace2/code/scramble/scramble.cpp $
7 * Module for file scrambler
10 * Revision 1.1 2002/05/03 03:28:10 root
14 * 3 3/25/99 11:26a Dave
15 * Beefed up encryption scheme so that even someone viewing the
16 * disassembly would have a hard time cracking it.
18 * 2 10/24/98 11:41p Dave
20 * 1 10/24/98 11:31p Dave
22 * 7 8/09/98 4:44p Lawrance
23 * support alternate encryption scheme (doesn't pack chars into 7 bits)
25 * 6 4/14/98 4:14p Lawrance
26 * fix bug with ships and weapons tbl preprocessing
28 * 5 4/14/98 1:39p Lawrance
29 * Add command line switches to preprocess ship and weapon tables
31 * 4 3/31/98 1:14a Lawrance
32 * Get .tbl and mission file encryption working.
34 * 3 3/30/98 5:57p Lawrance
37 * 2 3/30/98 5:51p Lawrance
38 * file encryption and decryption
52 #define MAX_LINE_LEN 512
54 static int Use_8bit = 0; // set to 1 to disable 7bit character packing
56 // strip out any ships tbl data not used in demo (ie entries without @ preceding name)
57 void scramble_read_ships_tbl(char **text, int *text_len, FILE *fp)
59 char line[MAX_LINE_LEN+1];
60 char token_line[MAX_LINE_LEN+1];
62 int line_len, discard_line = 1, keep_all_lines = 0, post_discard = 0;
63 char seps[] = " ,\t\n";
66 *text_len = _filelength(fileno(fp));
67 *text = (char*)malloc(*text_len+1);
71 while ( fgets(line, MAX_LINE_LEN, fp) != NULL ) {
73 line_len = strlen(line);
74 memcpy(token_line, line, line_len+1);
76 if ( !keep_all_lines ) {
77 token = strtok( token_line, seps );
80 if ( !strnicmp("#End", token, 4) ) {
82 } else if ( !strnicmp("#Ship", token, 5) ) {
85 } else if ( !strnicmp("$Name:", token, 6) ) {
86 token = strtok( NULL, seps );
88 if ( token[0] == '@' ) {
98 if ( !discard_line || keep_all_lines ) {
99 memcpy(dest, line, line_len);
103 if ( post_discard ) {
109 *text_len = dest - *text;
112 // strip out any weapons tbl data not used in demo (ie entries without @ preceding name)
113 void scramble_read_weapons_tbl(char **text, int *text_len, FILE *fp)
115 char line[MAX_LINE_LEN+1];
116 char token_line[MAX_LINE_LEN+1];
118 int line_len, discard_line = 1, keep_all_lines = 0, post_discard = 0;
119 char seps[] = " ,\t\n";
122 *text_len = _filelength(fileno(fp));
123 *text = (char*)malloc(*text_len+1);
127 while ( fgets(line, MAX_LINE_LEN, fp) != NULL ) {
129 line_len = strlen(line);
130 memcpy(token_line, line, line_len+1);
132 if ( !keep_all_lines ) {
133 token = strtok( token_line, seps );
136 if ( !strnicmp("#Countermeasures", token, 16) ) {
138 } else if ( !strnicmp("#End", token, 4) || !strnicmp("#Beam", token, 5) || !strnicmp("#Primary", token, 8) || !strnicmp("#Secondary", token, 10) ) {
141 } else if ( !strnicmp("$Name:", token, 6) ) {
143 token = strtok( NULL, seps );
145 if ( token[0] == '@' ) {
153 if ( (token[0] != ';') && (!discard_line || keep_all_lines) ) {
154 memcpy(dest, line, line_len);
158 if ( post_discard ) {
164 *text_len = dest - *text;
167 void scramble_read_default(char **text, int *text_len, FILE *fp)
169 *text_len = _filelength(fileno(fp));
170 *text = (char*)malloc(*text_len+1);
171 fread( *text, *text_len, 1, fp );
176 // input: src_filename => filename of text to scramble
177 // dest_filename => optional, this is the filename scrambled data will get stored to
178 void scramble_file(char *src_filename, char *dest_filename, int preprocess)
181 int text_len, scramble_len;
182 char *text, *scramble_text;
184 fp = fopen(src_filename, "rb");
189 // read in data, maybe preprocess
191 case PREPROCESS_SHIPS_TBL:
192 scramble_read_ships_tbl(&text, &text_len, fp);
194 case PREPROCESS_WEAPONS_TBL:
195 scramble_read_weapons_tbl(&text, &text_len, fp);
198 // read in the raw data
199 scramble_read_default(&text, &text_len, fp);
205 // open up file for writing scrambled text
206 if ( dest_filename ) {
207 fp = fopen(dest_filename, "wb");
209 fp = fopen(src_filename, "wb");
216 scramble_text = (char*)malloc(text_len+32);
218 encrypt(text, text_len, scramble_text, &scramble_len, Use_8bit);
220 // write out scrambled data
221 fwrite( scramble_text, scramble_len, 1, fp );
230 // input: src_filename => filename of scrambled text
231 // dest_filename => optional, this is the filename unscrambled text data will get stored to
232 void unscramble_file(char *src_filename, char *dest_filename)
235 int scramble_len, text_len;
236 char *text, *scramble_text;
238 fp = fopen(src_filename, "rb");
243 // read in the scrambled data
244 scramble_len = _filelength(fileno(fp));
245 scramble_text = (char*)malloc(scramble_len+1);
246 fread( scramble_text, scramble_len, 1, fp );
249 // open up file for writing unscrambled text
250 if ( dest_filename ) {
251 fp = fopen(dest_filename, "wb");
253 fp = fopen(src_filename, "wb");
259 // assume original text no larger than double scrambled size
260 text = (char*)malloc(scramble_len*2);
262 unencrypt(scramble_text, scramble_len, text, &text_len);
264 // write out unscrambled data
265 fwrite( text, text_len, 1, fp );
272 void print_instructions()
274 printf("Encrypt: scramble [-st] [-wt] <filename_in> [filename_out] \n");
275 printf("Decrypt: scramble -u <filename_in> [filename_out] \n");
278 void main(int argc, char *argv[])
283 scramble_file(argv[1]);
287 if ( !stricmp("-u", argv[1]) ) {
288 unscramble_file(argv[2]);
289 } else if ( !stricmp("-st", argv[1]) ) {
290 scramble_file(argv[2], argv[2], PREPROCESS_SHIPS_TBL);
291 } else if ( !stricmp("-wt", argv[1]) ) {
292 scramble_file(argv[2], argv[2], PREPROCESS_WEAPONS_TBL);
294 scramble_file(argv[1], argv[2]);
299 if ( !stricmp("-u", argv[1]) ) {
300 unscramble_file(argv[2], argv[3]);
301 } else if ( !stricmp("-st", argv[1]) ) {
302 scramble_file(argv[2], argv[3], PREPROCESS_SHIPS_TBL);
303 } else if ( !stricmp("-wt", argv[1]) ) {
304 scramble_file(argv[2], argv[3], PREPROCESS_WEAPONS_TBL);
306 print_instructions();
310 print_instructions();