#include #include #include #include #include #include #define SERIAL_LENGTH 10 #ifdef LINUX #define O_BINARY 0 #endif typedef char byte; const long MAGIC  = 0x343FD; const long MAGIC2 = 0x269eC3; /*  what to do, what to do ..... */ byte *  read_file( char * file_name, int * length  ); int     write_file( char * file_name, int length, byte * data ); void    decrypt_data( byte key, int length, byte * data ); void    encrypt_data( byte key, int length, byte * data ); void    set_serial( char * serial, byte * data ); void    fix_freqs( byte * data ); void    print_freqs( byte * data ); byte * read_file( char * file_name, int * length  )  { unsigned long   file_end_offset; int             file_handle; byte *          file_data; int             file_error; int             file_length; file_handle = open( file_name, O_RDONLY | O_BINARY ); if ( 1 >  file_handle ) { printf("Couldn't open input file %s \n", file_name ); exit(-1); } /* get the file length by seeking back 0 bytes from the end */ file_end_offset = lseek( file_handle, 0, SEEK_END ); /* and go back to the beginnin of the file */ lseek( file_handle, 0, SEEK_SET ); if ( file_end_offset > 65536 ) { printf("File longer than 64K -- Can't use it \n"); exit(-1); } file_length = file_end_offset;  /* CAST! */ file_data = (byte *)malloc( file_length ); if (!file_data ) { printf("Couldn't allocate %ld bytes\n", file_length ); exit(-1); } file_error = read( file_handle, file_data, file_length ); if ( file_error != file_length ) { printf("Only read %d bytes, expected %d \n", file_error, file_length ); free( file_data ); exit(-1); } close( file_handle ); *length = file_length; return file_data;               } int write_file( char * filename, int length, byte * data ) { int     write_handle; int     write_bytes; write_handle = open( filename, O_RDONLY ); if ( write_handle >  1 ) { printf("File exists: %s.  Delete the file first\n", filename ); close( write_handle ); return -1; } write_handle = open( filename, O_CREAT  | O_WRONLY | O_BINARY, S_IREAD | S_IWRITE ); if ( write_handle < 1 ) { printf("Couldn't create write file %s \n", filename ); return -1; } write_bytes = write( write_handle, data, length ); if ( length != write_bytes ) { printf("Wrote %d bytes of %d \n", write_bytes, length ); perror(""); return -1; } close( write_handle ); return 0; } void decrypt_data( byte key, int length,  byte * data) { long    random; byte    c; random  = 1; while ( length -- ) { *data ^= key; c = *(data++); random *= MAGIC; random += MAGIC2; key += ( random >> 16 ) & 0xFF; key += c; key &= 0xFF; } } void encrypt_data( byte key, int length, byte * data) { long    random; byte    c; random = 1;     while ( length-- ) { c = *data; *(data++) ^= key; random *= MAGIC; random += MAGIC2; key += ( (random >> 16 ) & 0XFF ); key += c; key &= 0xFF; } } #define FREQS_START 0xB0A #define FREQS_LENGTH 25 void print_freqs( byte * data ) { int f; int   i; int  checksum; for ( f = 0; f < 128 ; f++ )  { printf("%d: ", f ); checksum = 0; for (i = 0; i < FREQS_LENGTH;i++ ) { printf("%x ", *((unsigned char *)data) ); checksum += *((unsigned char *)data);   data++; } if ( (checksum & 0xFF) !=  *((unsigned char *)data) ) { printf("ERROR %x %x\n", checksum, *data ); return; } data++; printf("\n"); } } void fix_freqs( byte * data ) { int f; int   i; int  checksum; for ( f = 0; f < 128 ; f++ )  { checksum = 0; for (i = 0; i < FREQS_LENGTH;i++ ) { checksum += *((unsigned char *)data);   data++; } *((unsigned char *)data) = checksum & 0xFF; data++; } }       #define SERIAL_OFFSET   0x11D #define CHECKSUM_START  0x100 #define CHECKSUM_LENGTH 45 #define CHECKSUM_OFFSET 0x12E void set_serial( char * serial, byte * data ) { byte    checksum; int     i; for ( i = 0 ; i < SERIAL_LENGTH ; i++ ) { data[ SERIAL_OFFSET + i ] = serial[i]; } checksum = 0; for ( i = 0 ; i < CHECKSUM_LENGTH ; i++ ) { checksum += data[ CHECKSUM_START + i ]; } data[ CHECKSUM_OFFSET ] = checksum; } #define KEY_1           0x55 #define KEY_2           0x59 #define KEY_3           0x5b #define ENCODED_START   0x60 void main( int argc, char ** argv ) { byte    * eeprom; int     eeprom_length; byte    key;    int     error; if ( argc < 4 ) { printf("Usage: \n"); printf( "Commands are:\n" " D decrypt infile to outfile\n" " E encrypt infile to outfile\n" " S change serial number \n" " F print out frequences \n" "\n" "NOTE: can NOT exist.  This is for safety.\n"); exit(0);        } error = 0; eeprom = read_file( argv[1] , &eeprom_length); key = eeprom[ KEY_1 ] + eeprom[ KEY_2 ] - eeprom[ KEY_3 ]; switch( argv[3][0] ) { case 'D': case 'd': decrypt_data( key, eeprom_length - ENCODED_START, eeprom + ENCODED_START ); break; case 'E': case 'e': encrypt_data( key, eeprom_length - ENCODED_START, eeprom + ENCODED_START ); break;  case 'S': case 's': if ( argc != 5 ) { printf("No serial number found \n"); error = 1; break; } if ( strlen( argv[4] ) != SERIAL_LENGTH ) { printf("Serial number of incorrect length\n"); error = 1; exit(-1); } decrypt_data( key, eeprom_length - ENCODED_START, eeprom + ENCODED_START ); set_serial( argv[4], eeprom );  encrypt_data( key, eeprom_length - ENCODED_START, eeprom + ENCODED_START ); break; case 'F': case 'f': /* NO WRITE! */ decrypt_data( key, eeprom_length - ENCODED_START, eeprom + ENCODED_START ); print_freqs( eeprom + FREQS_START ); free( eeprom ); exit(0); case 'Y': case 'y': decrypt_data( key, eeprom_length - ENCODED_START, eeprom + ENCODED_START ); fix_freqs( eeprom + FREQS_START ); encrypt_data( key, eeprom_length - ENCODED_START, eeprom + ENCODED_START ); break; default: printf("Bad option: %s \n", argv[3] ); error = -1; } if ( error ) exit(-1); error = write_file( argv[2], eeprom_length, eeprom );  free( eeprom ); if ( !error )  printf("\nDone.\n"); #if 0   #define OFFSET 256+47 + 226 + 236 #undef OFFSET #define OFFSET 256 { int     i; char    checksum; unsigned char c2; checksum = c2 = 0; for ( i = 0; i < eeprom_length ; i++ ) { unsigned char uc; char c; c = eeprom[ i + OFFSET ]; uc = ((unsigned char *)eeprom)[i + OFFSET]; if ( checksum == c ) { printf("Found checksum at: %d \n", i); checksum = 0; c2 = 0; continue; }  if ( c2 == uc ) { printf("Found c2 at: %d \n", i); c2 = 0; checksum = 0; continue; }  checksum += c; c2 += uc; if ( ( c < ' ' ) | ( c > 126 ) ) c = '.'; printf("%c", c ); } printf("\nValues: %x %x ", checksum, c2); } #endif }