home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Security / Security.zip / sct01b.zip / sct.c < prev    next >
C/C++ Source or Header  |  1998-06-10  |  6KB  |  199 lines

  1. /*
  2.    C-Source for sCT tool
  3.    written by Julian Buss, julian@wh.fh-wedel.de
  4.  
  5.    This source code is distributed under the GPL licence. See file COPYING.SCT for details.
  6.  
  7.    I am NOT a skilled C developer - I just started learning C.
  8.  
  9.    So if you find bugs or you have hints for better code - please tell me!
  10.    eMail to julian@wh.fh-wedel.de, constructive critism is always welcome.
  11.  
  12. */
  13.  
  14. #include <stdio.h>
  15. #include <limits.h>
  16. #include <strings.h>
  17. #include "cxio.h"
  18.  
  19. /* for debugging version... is not used much at this time... */
  20. //#define __DEBUG
  21.  
  22. #define PRGNAME "sCT - simple crypto tool"
  23. #define VERSION "0.1beta"
  24.  
  25. char *password = NULL;
  26. char *workmode = NULL;
  27. char *fin = NULL;
  28. char *fout = NULL;
  29. int verbose = 1;        /* be verbose or not */
  30.  
  31. void help(char *msg)
  32. {
  33.  printf("%s version %s by Julian Buss, julian@wh.fh-wedel.de.\n", PRGNAME, VERSION);
  34.  printf("Encodes and decodes a file with a password.\n\n");
  35.  printf("Based on cxio routines from Julian Buss and Holger Leskien.\n");
  36.  printf("Support free OS/2 software - visit http://www.netlabs.org!\n\n");
  37.  printf("%s\n", msg);
  38.  printf("Usage: sct e|d [option 1] .. [option n]\n");
  39.  printf("       e                encode\n");
  40.  printf("       d                decode\n");
  41.  printf("       -fin <file>      use this file for input ('stdin' ok)\n");
  42.  printf("       -fout <file>     use this file for output ('stdout' ok)\n");
  43.  printf("       -p <password>    use this password\n\n");
  44.  printf("If no infile is given, read from stdin.\n");
  45.  printf("If no outfile is given, write to stdout.\n");
  46.  printf("If no password is given, ask (not possible when input comes from stdin).\n");
  47.  printf("If no work mode or is given, terminate.\n");
  48.  printf("\n");
  49.  printf("Examples:\n");
  50.  printf("  sct e -fin readme -fout readmy.crypt\n");
  51.  printf("  sct d -fin readme\n");
  52.  printf("  sct e -fin mypicture.jpg -fout crypted.jpg\n");
  53.  printf("       encodes picture, saves in other file and asks for password.\n");
  54.  printf("  dir | sct e -p test\n");
  55.  printf("       encodes output from dir command to stdout.\n\n");
  56.  
  57.  exit(1);
  58. }
  59.  
  60.  
  61. /* get command line parameters and init the needed vars */
  62. void init(int argc, char *argv[])
  63. {
  64.   int i;
  65.   char buffer[256];
  66.   char tmp[256];
  67.  
  68.   for (i = 1; i < argc; ++i)
  69.   {
  70.     if (strchr(argv[i], '?') != NULL) help("Help wanted. Here it is:");  /* if there's a ? in the arg, print out help */
  71.     if (i == 1) workmode = argv[i];
  72.     if (strcmp(argv[i], "-fin") == 0) fin = argv[++i];          /* take this as in file */
  73.     if (strcmp(argv[i], "-fout") == 0) fout = argv[++i];        /* take this as out file */
  74.     if (strcmp(argv[i], "-p") == 0) password = argv[++i];        /* and finally the password */
  75.   }
  76.   if (!fin) fin = "stdin";                        /* if fin is not defined, take stdin */
  77.   if (!fout) fout = "stdout";                        /* if fout is not defined, take stdout */
  78.   if (strcmp(fout, "stdout") == 0) verbose = 0;
  79.   if (!workmode) help("ERROR: No workmode specified!");            /* no workmode? terminate with help! */
  80.  
  81.   if (verbose)
  82.   {
  83.     printf("%s version %s by Julian Buss, julian@wh.fh-wedel.de.\n", PRGNAME, VERSION);
  84.     printf("Support free OS/2 software - visit http://www.netlabs.org!\n\n");
  85.   }
  86.  
  87.   if (!password && (strcmp(fin, "stdin") != 0) )/* still no password? ask, but not if fin is stdin! */
  88.   {
  89.     printf("password: ");
  90.     fgets(buffer, 255, stdin);            
  91.     i = sscanf(buffer, "%s", tmp);
  92.     if (i == -1) help("ERROR: No password given!");
  93.  
  94.     password = strdup((char*)tmp);                    /* donnow why "password = &tmp" doesn't work... */
  95.   }
  96.   if (!password) help("ERROR: No password given!");            /* still no password (because no one given on commandline
  97.                                          and fin == "stdin"), exit */
  98.  
  99.   if (verbose)
  100.   {
  101.     if (strcmp(workmode, "e") == 0) printf("Encoding"); else printf("Decoding");
  102.     printf(" file %s to %s...", fin, fout);
  103.   }
  104. }
  105.  
  106. void progressChar(int count)
  107. {
  108.   switch (count)
  109.   {
  110.     case 10: { printf("\x08|"); break; }
  111.     case 20: { printf("\x08/"); break; }
  112.     case 30: { printf("\x08-"); break; }
  113.     case 40: { printf("\x08\\"); break; }
  114.   }
  115. }
  116.  
  117. /* encode the file
  118.    read with normal fgets the file
  119.    write out with cx_fputs
  120. */
  121. void encode()
  122. {
  123.   FILE *instream;
  124.   cx_file *cx_outstream;
  125.   char buffer[256];
  126.   int  rc = -1;
  127.   int count = 1;
  128.  
  129.   if (verbose) printf("working... ");
  130.   if ( strcmp(fin, "stdin") == 0 ) instream = stdin; else instream = fopen(fin, "rb");
  131.   cx_outstream = cx_fopen(fout, "w");
  132.   cx_setkey(password, cx_outstream);
  133.  
  134.   while (!feof(instream))
  135.   {
  136.     rc = fread(buffer, sizeof(char), 255, instream);
  137.     rc = cx_fwritec(buffer, sizeof(char), rc, cx_outstream);
  138.  
  139.     count++;
  140.     progressChar(count);
  141.     if (count == 40) count = 1;
  142.   }
  143.  
  144.   fclose(instream);
  145.   cx_fclose(cx_outstream);
  146.   if (verbose) printf("\ndone.\n");
  147. }
  148.  
  149.  
  150. /* decode the file
  151.    read with cx_fgets
  152.    put out with normal fputs
  153. */
  154. void decode()
  155. {
  156.   FILE *outstream;
  157.   cx_file *cx_instream;
  158.   char buffer[256];
  159.   int  rc = -1;
  160.   int count = 1;
  161.  
  162.   if (verbose) printf("working... ");
  163.   cx_instream = cx_fopen(fin, "r");
  164.   if ( strcmp(fout, "stdout") == 0 ) outstream = stdout; else outstream = fopen(fout, "wb");
  165.   cx_setkey(password, cx_instream);
  166.  
  167.   while (!cx_feof(cx_instream))
  168.   {
  169.     rc = cx_freadc(buffer, sizeof(char), 255, cx_instream);
  170.     rc = fwrite(buffer, sizeof(char), rc, outstream);
  171.  
  172.     count++;
  173.     progressChar(count);
  174.     if (count == 40) count = 1;
  175.   }
  176.  
  177.   cx_fclose(cx_instream);
  178.   fclose(outstream);
  179.   if (verbose) printf("\ndone.\n");
  180. }
  181.  
  182. void go(void)
  183. {
  184.   if (*workmode == 'e') encode(); else decode();
  185. }
  186.  
  187. int main (int argc, char *argv[])
  188. {
  189.  
  190.   init(argc, argv);
  191.   go();
  192.  
  193.   return 0;
  194. }
  195.  
  196.  
  197. /* the end - simple, eh? */
  198.  
  199.