/* ------------------------------------------------------------------------- */
/* Copyright (c) 2016 FIRMWARE DEVELOPMENTS SAS                              */
/* ------------------------------------------------------------------------- */
/* This program is free software: you can redistribute it and/or modify      */
/* it under the terms of the GNU Lesser Public License as published by       */
/* the Free Software Foundation, version 3 of the License.                   */
/*                                                                           */
/* This program is distributed in the hope that it will be useful,           */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of            */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             */
/* GNU Lesser Public License for more details.                               */
/*                                                                           */
/* You should have received a copy of the GNU Lesser Public License          */
/* along with this program.  If not, see <http://www.gnu.org/licenses/>.     */
/* ------------------------------------------------------------------------- */
/* Purpose     : one-time-pad cipher : implements an exclusive OR on bytes   */
/* File:       : xor.c                                                       */
/* Author      : Laurent Le Faucheur                                         */
/* Created     : May 2016                                                    */
/* Description : tool used to (de)cipher a file                              */
/*               http://firmware-developments.com/WEB/P6x/XOR                */
/* Release     : 1.0                                                         */
/* ------------------------------------------------------------------------- */ 
#include <stdio.h>

/*
 * program arguments : 
 *     1)  input file to process
 *     2)  file key, usually a JPEG picture of at least 8kbytes long
 *     3)  output file process
 */

void main (int argc, char *argv[])
{
  FILE *out_file, *inp_file, *key_file;
  int i, n;
  char ci, ck, co;

  // open the binary files TAR folder, key and output
  if ((0 == (inp_file = fopen (argv[1], "rb"))) ||
      (0 == (key_file = fopen (argv[2], "rb"))) ||
      (0 == (out_file = fopen (argv[3], "wb")))
     )  { printf("fopen error, usage XOR.exe INPUT KEY OUTPUT \n");}
  else
  {   i = 0;    
  
      // loop on input byte
      n = fread (&ci, 1, 1, inp_file);
      while (n > 0) 
      {
        // read one byte of key per input byte
        fread  (&ck, 1, 1, key_file);

        // (de)cipher is a XOR 
        co = ci ^ ck;

        // save the resulting byte
        fwrite (&co, 1, 1, out_file);
        i++;

        // rewind the key after 64kbits
        if (i > 8191)
        {
          fclose (key_file);
          key_file = fopen (argv[2], "rb");
          i = 0;
        }
        n = fread (&ci, 1, 1, inp_file);
      }

      fclose (out_file); 
      fclose (key_file); 
      fclose (inp_file); 
  }
}