/*
 * otp.c: A simple implementation of the One Time Pad encryption algorithm.
 *
 *
 * Copyright (c) 2003, eazy <eazy@ondaquadra.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * *  Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * *  Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

#define MAXLEN 255

void 
err_quit()
{
	perror("error");
	exit(-1);
}

void
usage(char *prog)
{
	fprintf(stderr, "Usage: %s -p padfile [-f filename]\n", prog);
	exit(-1);
}

int
otp(char *text, char *pad, int len)
{
	int             i;

	if (!text || !pad)
		return -1;

	for (i = 0; i < len; i++)
		text[i] ^= pad[i];

	return 1;
}

int
main(int argc, char **argv)
{
	int             filefd = STDIN_FILENO, padfd, opt, blen, plen;
	unsigned char   buf[MAXLEN], pad[MAXLEN];
	char           *filename = NULL, *padfile = NULL;

	opterr = 0;
	while ((opt = getopt(argc, argv, "f:p:")) != -1) {

		switch (opt) {
		case 'f':
			filename = optarg;
			break;
		case 'p':
			padfile = optarg;
			break;
		case '?':
		default:
			usage(argv[0]);
		}
	}

	if (optind < argc)
		usage(argv[0]);

	if (!padfile)
		usage(argv[0]);

	if (filename)
		if ((filefd = open(filename, 0)) == -1)
			err_quit();

	if ((padfd = open(padfile, 0)) == -1)
		err_quit();

	while ((blen = read(filefd, buf, MAXLEN)) > 0) {

		if ((plen = read(padfd, pad, blen)) == -1)
			err_quit();
		if (plen < blen) {
			fprintf(stderr, "Lunghezza del pad non sufficiente\n");
			exit(-1);
		}
		if (otp(buf, pad, blen) == -1) {
			fprintf(stderr, "otp error\n");
			exit(-1);
		}
		if (write(STDOUT_FILENO, buf, blen) == -1)
			err_quit();
	}
	if (blen == -1)
		err_quit();

}

