home *** CD-ROM | disk | FTP | other *** search
/ No Fragments Archive 10: Diskmags / nf_archive_10.iso / MAGS / ST_USER / 1990 / USERJL90.MSA / LISTINGS.ARC / ENCRYPT.C < prev    next >
C/C++ Source or Header  |  1990-05-16  |  5KB  |  144 lines

  1. /* ===================================================================== */
  2.  
  3. /* Title:   Atari ST Encrypt/Decipher Utility - version 1.00             
  4.  
  5.    Author:  Paul Overaa                                                  
  6.  
  7.    Date:    24th May 1990                                                
  8.  
  9.    Notes:   I'm not an ST programmer, so some of  these notes are
  10.             to help me as much as you... 
  11.  
  12.             Firstly, the compiled program has to  be  stored with
  13.             a .TTP  extension  so that  the  command line  can be
  14.             collected. 
  15.  
  16.             Secondly, if you want to deal with  larger files just 
  17.             alter the  BUFFER_SIZE define value.  Better still...
  18.             re-write using a  dynamic buffer  so program attempts
  19.             to use all available memory before it gives up !
  20.  
  21.             Thirdly I was not sure what the current 'ST compiler'
  22.             situation  is like  as far as  either  ANSII C versus 
  23.             K&R C, or the ST's binary file handling, goes.  I did
  24.             not want line-feeds being translated to  LF-CR  pairs  
  25.             so I've opted for using binary files coupled with the 
  26.             UNIX type read/write functions. 
  27.  
  28.             Lastly, the open() statements return -1 on failure. I
  29.             have tested against EOF  which is fine for the ST but
  30.             be careful...  if  you  port to a system which uses a 
  31.             different EOF value check the  value that is returned
  32.             when an open() call fails. 
  33.  
  34. */
  35.  
  36. /* --------------------------------------------------------------------- */
  37. #include <stdio.h>
  38. #include <string.h>
  39. #include <fcntl.h>
  40.  
  41. #define NORMAL_EOF          1
  42. #define WRONG_PARAMETERS    2
  43. #define NO_SOURCE           3
  44. #define OVERSIZE_FILE       4
  45. #define NO_DEST             5
  46. #define WRITE_ERROR         6
  47.  
  48. #define BUFFER_SIZE         5000
  49. #define S_MODE              O_RDONLY|O_BINARY
  50. #define D_MODE              O_CREAT|O_TRUNC|O_WRONLY|O_BINARY
  51.  
  52. static int *message[] = {
  53.    "ST Encrypt/Decipher Utility v1.00 (Paul Andreas Overaa) 1990\n",
  54.    "Function complete",
  55.    "Usage... <SourceFile> <DestinationFile> <Key(NoSpaces)>",
  56.    "Sorry... cannot find your source file",
  57.    "Sorry... source file>5000 bytes maximum",
  58.    "Sorry... couldn't open destination file",
  59.    "Destination write-error has occured (no space ?)",
  60.    " - ANY KEY to continue\n" 
  61.    };  
  62.  
  63. /* --------------------------------------------------------------------- */
  64.  
  65. main(argc, argv)
  66.     
  67. int argc; char *argv[];
  68.  
  69. {
  70.  
  71. int source_fh, dest_fh, read_count, write_count; 
  72. int key_length, position=0, exit_flag=NORMAL_EOF;
  73. register int i;
  74. char buffer[BUFFER_SIZE+1];
  75.  
  76. printf("%s",message[0]); 
  77.  
  78. if(argc!=4) {exit_flag=WRONG_PARAMETERS;}
  79.         
  80. else { 
  81.       
  82.       if ((source_fh=open(argv[1],S_MODE))==EOF) {exit_flag=NO_SOURCE;}
  83.    
  84.        else {
  85.             
  86.             if((dest_fh=open(argv[2],D_MODE))==EOF) {exit_flag=NO_DEST;}
  87.       
  88.              else {
  89.  
  90.                   key_length=strlen(argv[3]);
  91.  
  92.                   read_count=read(source_fh,buffer,BUFFER_SIZE);   
  93.  
  94.                   /* Up to this point all that we've done is to work
  95.                      our way down a  'nested test scheme'  trying to
  96.                      open the required files.  Since we have arrived
  97.                      here the source file exists... so now it can be
  98.                      encoded (or decoded) and saved.
  99.                    */              
  100.                   
  101.                   if (read_count<BUFFER_SIZE)
  102.                      
  103.                      {
  104.                      
  105.                       for (i=0;i<read_count;i++) 
  106.                   
  107.                            { /* Here's the magic function... */
  108.  
  109.                            buffer[i]=buffer[i]^
  110.                            (argv[3][position%key_length]+position%256);
  111.  
  112.                            position++;
  113.  
  114.                            }                   
  115.  
  116.                       } else {exit_flag=OVERSIZE_FILE;}
  117.  
  118.                   write_count=write(dest_fh,buffer,read_count);
  119.  
  120.                   close(dest_fh);
  121.  
  122.                   if (read_count!=write_count) exit_flag=WRITE_ERROR;                   
  123.  
  124.                   }
  125.        
  126.             close(source_fh);       
  127.    
  128.             }
  129.      
  130.     }
  131.  
  132. if (exit_flag!=1) unlink(argv[2]);
  133.  
  134. printf("%s%s",message[exit_flag],message[7]);
  135. getchar(); /* Wait for user to quit */
  136.  
  137.  
  138. /* ====================================================================== */
  139.  
  140.  
  141.  
  142.  
  143.