/*====================================================================*
 *
 *   chkpib.c - validate pib files using internal checksums;
 *
 *   validate one or more PIB files using header checksum and length;
 *
 *   This software and documentation is the property of Atheros 
 *   Corporation, Ocala, Florida. It is provided 'as is' without 
 *   expressed or implied warranty of any kind to anyone for any 
 *   reason. Atheros assumes no responsibility or liability for 
 *   errors or omissions in the software or documentation and 
 *   reserves the right to make changes without notification. 
 *   
 *   Atheros customers may modify and distribute the software 
 *   without obligation to Atheros. Since use of this software 
 *   is optional, users shall bear sole responsibility and 
 *   liability for any consequences of it's use. 
 *
 *.  Atheros Powerline Toolkit for HomePlug AV;
 *:  Published 2006-2008 by Intellon Corp. ALL RIGHTS RESERVED;
 *;  For demonstration; Not for production use;
 *
 *   Contributor(s):
 *		Charles Maier <charles.maier@atheros.com>
 *
 *--------------------------------------------------------------------*/

#define _GETOPT_H

/*====================================================================*
 *   system header files;
 *--------------------------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

/*====================================================================*
 *   custom header files;
 *--------------------------------------------------------------------*/

#include "../tools/getoptv.h"
#include "../tools/flags.h"
#include "../tools/error.h"
#include "../tools/files.h"
#include "../crypt/HPAVKey.h"
#include "../pib/pib.h"

/*====================================================================*
 *   custom source files;
 *--------------------------------------------------------------------*/

#ifndef MAKEFILE
#include "../tools/getoptv.c"
#include "../tools/putoptv.c"
#include "../tools/version.c"
#include "../tools/checksum_32.c"
#include "../tools/fdchecksum_32.c"
#include "../tools/hexstring.c"
#include "../tools/hexdecode.c"
#include "../tools/error.c"
// #include "../tools/alert.c"
#endif

#ifndef MAKEFILE
#include "../pib/pibpeek.c"
#include "../pib/pibcodes.c"
#endif 

#ifndef MAKEFILE
#include "../crypt/SHA256Reset.c"
#include "../crypt/SHA256Block.c"
#include "../crypt/SHA256Write.c"
#include "../crypt/SHA256Fetch.c"
#include "../crypt/HPAVKeyNID.c"
#include "../crypt/keys.c"
#endif

/*====================================================================*
 *
 *   signed chkpib (const void * memory, size_t extent, char const * title, flag_t flags);
 *
 *   pib.h
 *
 *   the computed checksum of the file should be 0 because the file
 *   includes the original checksum;
 *
 *   there are three types of error: bad version, bad checksum and
 *   I/O error and they all return -1; errno will be EINVAL if the
 *   pib version is bad, 0 if the checksum is bad or the I/O error
 *   code;
 *
 *   This software and documentation is the property of Atheros 
 *   Corporation, Ocala, Florida. It is provided 'as is' without 
 *   expressed or implied warranty of any kind to anyone for any 
 *   reason. Atheros assumes no responsibility or liability for 
 *   errors or omissions in the software or documentation and 
 *   reserves the right to make changes without notification. 
 *   
 *   Atheros customers may modify and distribute the software 
 *   without obligation to Atheros. Since use of this software 
 *   is optional, users shall bear sole responsibility and 
 *   liability for any consequences of it's use. 
 *
 *.  Atheros Powerline Toolkit for HomePlug AV;
 *:  Published 2006-2008 by Intellon Corp. ALL RIGHTS RESERVED;
 *;  For demonstration; Not for production use;
 *
 *   Contributor(s):
 *	Charles Maier <charles.maier@atheros.com>
 *
 *--------------------------------------------------------------------*/

signed chkpib2 (const void * memory, size_t extent, char const * title, flag_t flags) 

{
	uint8_t NID [HPAVKEY_NID_LEN];
	struct simple_pib * header_pib = (struct simple_pib *)(memory);
	if (_anyset (flags, PIB_VERBOSE)) 
	{
		printf ("------- %s -------\n", title);
		if (pibpeek (memory))
		{
			error (0, 0, "Bad version: %s", title);
			return (-1);
		}
	}
	if (extent != LE16TOH (header_pib->PIBLENGTH)) 
	{
		if (_allclr (flags, PIB_SILENCE))
		{
			error (0, 0, "Bad length: %s", title);
		}
		return (-1);
	}
	if (checksum_32 (memory, extent, 0)) 
	{
		if (_allclr (flags, PIB_SILENCE))
		{
			error (0, 0, "Bad checksum: %s", title);
		}
		return (-1);
	}
	HPAVKeyNID (NID, header_pib->NMK, header_pib->PreferredNID [HPAVKEY_NID_LEN-1]>>4);
	if (memcmp (NID, header_pib->PreferredNID, sizeof (NID))) 
	{
		if (_allclr (flags, PIB_SILENCE))
		{
			error (0, 0, "Bad network identifier: %s", title);
		}
		return (-1);
	}
	return (0);
}

/*====================================================================*
 *   
int main (int argc, const char * argv []) 

 *--------------------------------------------------------------------*/

int main (int argc, const char * argv []) 

{
	static const char *optv [] = 
	{
		"qv",
		"file [file] [...]",
		"validate PIB files based on internal checksums",
		"q\tquiet",
		"v\tverbose messages",
		(const char *) (0) 
	};
	void * memory;
	signed length;
	signed state = 0;
	flag_t flags = (flag_t)(0);
	signed c;
	optind = 1;
	opterr = 1;
	while ((c = getoptv (argc, argv, optv)) != -1) 
	{
		switch ((char) (c)) 
		{
		case 'q':
			_setbits (flags, PIB_SILENCE);
			break;
		case 'v':
			_setbits (flags, PIB_VERBOSE);
			break;
		default:
			break;
		}
	}
	argc -= optind;
	argv += optind;
	while ((argc) && (*argv)) 
	{
		signed fd;
		if ((fd = open (*argv, O_BINARY|O_RDONLY)) == -1) 
		{
			error (0, errno, "Can't open %s", *argv);
			state = 1;
		}
		else if ((length = lseek (fd, 0, SEEK_END)) == -1) 
		{
			error (0, errno, "Can't size %s", *argv);
			state = 1;
		}
		else if (!(memory = malloc (length))) 
		{
			error (0, errno, "Can't allocate memory");
			state = 1;
		}
		else if (lseek (fd, 0, SEEK_SET)) 
		{
			error (0, errno, "Can't home %s", *argv);
			state = 1;
		}
		else if (read (fd, memory, length) < length) 
		{
			error (0, errno, "Can't read %s", *argv);
			state = 1;
		}
		else if (chkpib2 (memory, length, *argv, flags)) 
		{
			error (0, 0, "%s is corrupt", *argv);
			state = 1;
		}
		else if (_allclr (flags, PIB_SILENCE)) 
		{
			printf ("%s looks good\n", *argv);
		}
		close (fd);
		argc--;
		argv++;
	}
	return (state);
}