//	This source and File 16-bit bitparity patterns method apply to GNU General Public License. 
//			Copyright (C) 2015  Jasenko Dzinleski 

//	This source applies to the GNU General Public License as
//	published by the Free Software Foundation 
//	and can not be used, copied, sold, redistributed or 
//	used in any other way but only by written permission by Jasenko Dzinleski . 
//	Copyright (C) from 2001 - 2013 and later by Jasenko Dzinleski 

//	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
//	General Public License for more details. 

//	You should have received a copy of the GNU General Public License along
//	with this program; if not, write to the Free Software Foundation, Inc.,
//	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

//
//		16-bit bitparity patterns
//		written by Dzinleski Jasenko  February-March , 2015
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

FILE		*f1,*f2;

char		infn[256];
char		ofn[256]="_cbp.mar";

int		b16o,b16e;


char	sqa[0xffff][6];
int		sqai=0,sqai_;

int		sqa_[0xffff];

int	ba16_1[256][256][2];int ba16_1i=0; 
int	ba16_2[256][256][2];int ba16_2i=0;

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

int	a,b,c,d,e;
int	i,j,k,l,m;

int	b1_,b2_,b3_;
int	b1,b2,b3;
int	d1,d2,d3,d4;
int	d1_,d2_,d3_;
int	mc=0,unmc=0;


	printf("\n\n\t16-bit bitparity patterns\n");
	printf("\twritten by Dzinleski Jasenko  February , 2015\n\n");

	if(argv[1]==NULL){return(0);}else{
		strcpy(infn,argv[1]);f1=fopen(infn,"rb");
		if(f1==NULL){printf("No file...\n");return(0);}else{fclose(f1);}
	}

	b16o=0;
	b16o|=1<<0;
	b16o|=1<<2;
	b16o|=1<<4;
	b16o|=1<<6;
	b16o|=1<<8;
	b16o|=1<10;
	b16o|=1<<12;
	b16o|=1<<14;

	b16e=0;
	b16e|=1<<1;
	b16e|=1<<3;
	b16e|=1<<5;
	b16e|=1<<7;
	b16e|=1<<9;
	b16e|=1<<11;
	b16e|=1<<13;
	b16e|=1<<15;


	for(i=0;i<0xffff;++i){sqa_[i]=0;}

    f1=fopen(infn,"rb");
   
    b1_=(getc(f1)<<8)|getc(f1);
    b2_=(getc(f1)<<8)|getc(f1);
    b3_=(getc(f1)<<8)|getc(f1);
 
	while(!feof(f1))
	{

		if(sqai==0)
		{
			sqa[sqai][0]=(b1_&0xff00)>>8;
			sqa[sqai][1]=(b1_&0x00ff)>>0;
			sqa[sqai][2]=(b2_&0xff00)>>8;
			sqa[sqai][3]=(b2_&0x00ff)>>0;
			sqa[sqai][4]=(b3_&0xff00)>>8;
			sqa[sqai][5]=(b3_&0x00ff)>>0;
			sqai_=sqai;
			++sqai;
		}else{
			for(i=0;i<sqai;++i)
			{
				if
				(
				sqa[i][0]==(b1_&0xff00)>>8&&
				sqa[i][1]==(b1_&0x00ff)>>0&&
				sqa[i][2]==(b2_&0xff00)>>8&&
				sqa[i][3]==(b2_&0x00ff)>>0&&
				sqa[i][4]==(b3_&0xff00)>>8&&
				sqa[i][5]==(b3_&0x00ff)>>0
				){sqai_=i;break;}
			}
			if(i==sqai)
			{
				sqa[sqai][0]=(b1_&0xff00)>>8;
				sqa[sqai][1]=(b1_&0x00ff)>>0;
				sqa[sqai][2]=(b2_&0xff00)>>8;
				sqa[sqai][3]=(b2_&0x00ff)>>0;
				sqa[sqai][4]=(b3_&0xff00)>>8;
				sqa[sqai][5]=(b3_&0x00ff)>>0;
				sqai_=sqai;
				++sqai;
			}
		}


		for(i=0;i<256;++i){for(j=0;j<256;++j){ba16_1[i][j][0]=-1;ba16_2[i][j][1]=-1;}}
		ba16_1i=0;ba16_2i=0;

        	i=0;
        	while(i<8)
        	{
			j=0;
            		while(j<16)
            		{
	
				b1=b1_<<i;
				b2=b2_<<i;
				b3=b3_<<i;
	
				d1=((((b1&b16e)>>i)^(b2&b16o))|((b1&b16e)^((b2&b16o)<<j)));	
				d2=((((b1&b16e)>>i)^(b3&b16o))|((b1&b16e)^((b3&b16o)<<j)));

				if(ba16_1[(d1&0xff00)>>8][d1&0x00ff][0]==-1)
				{
					ba16_1[(d1&0xff00)>>8][d1&0x00ff][0]=ba16_1i;++ba16_1i;
					ba16_1[(d1&0xff00)>>8][d1&0x00ff][1]=1;
				}else{++ba16_1[(d1&0xff00)>>8][d1&0x00ff][1];}
				
				if(ba16_2[(d2&0xff00)>>8][d2&0x00ff][0]==-1)
				{
					ba16_2[(d2&0xff00)>>8][d2&0x00ff][0]=ba16_2i;++ba16_2i;
					ba16_2[(d2&0xff00)>>8][d2&0x00ff][1]=1;
				}else{++ba16_2[(d2&0xff00)>>8][d2&0x00ff][1];}

				++j; 
			}
			++i;
		}

        	for(i=0;i<256;++i){for(j=0;j<256;++j){
			if((ba16_1[i][j][0]==1)&&(ba16_2[i][j][1]==1)){++sqa_[sqai_];}
		}}


	    	b1_=b2_;
	    	b2_=b3_;
	    	b3_=(getc(f1)<<8)|getc(f1);

	}
	fclose(f1);

	printf("\tin : %s\n",infn);	
	j=strlen(infn);for(i=0;i<strlen(ofn);++i){infn[j]=ofn[i];++j;}
	printf("\tout: %s\n",infn);

	f2=fopen(infn,"w");
	for(i=0;i<0xffff;++i)
	{
		if(sqa_[i]>=1){
		//printf("%c%c%c%c%c%c\t%d\n",sqa[i][0],sqa[i][1],sqa[i][2],sqa[i][3],sqa[i][4],sqa[i][5],sqa_[i]);
		fprintf(f2,"%c%c%c%c%c%c\t%d\n",sqa[i][0],sqa[i][1],sqa[i][2],sqa[i][3],sqa[i][4],sqa[i][5],sqa_[i]);
		}
	}
	fclose(f2);

    return(0);

}