//	This source and Compression method apply to GNU General Public License. 
//			Copyright (C) 2012 , 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 - 2012 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.

//
//		Bit Parity Compression
//		written by Dzinleski Jasenko 2012 , November 2016
//


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

#define	BtM	8*128

FILE		*f1,*f2,*f3,*f4,*f5,*f6,*f7;

char		infn[256]="War_and_Peace_NT.txt";

char		cfn[256]="c.mar";
char		cifn[256]="ci.mar";
char		nfn[256]="n.mar";
char		dfn[256]="out.mar";
char		ar_fn[256]=".mar";

char		fn_[256];

int 		fbuff[3][1024+1];

int			b8o,b8e;

int			bt32_1[32];	int	bt32i_1=0;
int			bt32_2[32];	int	bt32i_2=0;

int			bc_=0,bc=0,bitc=0;
int			bytc=0,bytc1=0;

int			br[BtM];	int	bri=0;
int			br_[BtM];	int	bri_=0;
int			br__[BtM];	int	bri__=0;
int			br___[BtM];	int	bri___=0;

int			bt[BtM];	int	bti=0;
int			bt_[BtM];	int	bti_=0;

int			bfl,bfl_;

char		c_l[256]="{[--][++]}{[hh][ll][ee][cc][??][dd][xx]}{[AZ][az][09][__][  ][..][\'\'][\"\"][--][//][\\\\][&&][||][::][**]}";

int			d1,d2,d3;
int			d1_,d2_,d3_;
int			d1__,d2__,d3__;

int    	ba1[256][256][2];	int ba1i=0;
int    	ba2[256][256][2];	int ba2i=0;
int    	ba3[256][256][2];	int ba3i=0;

long 	bac[0xffffff][5];long baci=0;
long 	bac_[0xffffff];
long 	bac__[0xffffff];long baci__=0;

int		df=0;

int bp_(int b1 , int b2 , int b3, int b4, int b5)
{
	int b1_,b2_,b3_,b4_,b5_;
	
	b8o=0;
	b8o|=1<<0;
	b8o|=1<<2;
	b8o|=1<<4;
	b8o|=1<<6;

	b8e=0;
	b8e|=1<<1;
	b8e|=1<<3;
	b8e|=1<<5;
	b8e|=1<<7;

	b1_=b1<<1;
	b2_=b2<<1;
	b3_=b3<<1;
	b4_=b4<<1;
	b5_=b5<<1;	
	
	d1=((((b1_&b8e)>>1)^(b2_&b8o))|((b1_&b8e)^((b2_&b8o)<<1)));	
	d2=((((b1_&b8e)>>1)^(b3_&b8o))|((b1_&b8e)^((b3_&b8o)<<1)));
  	//d3=((((d1&b8e)>>1)^(d2&b8o))|((d1&b8e)^((d2&b8o)<<1)));

	d1_=((((b2_&b8e)>>1)^(b3_&b8o))|((b2_&b8e)^((b3_&b8o)<<1)));	
	d2_=((((b2_&b8e)>>1)^(b4_&b8o))|((b2_&b8e)^((b4_&b8o)<<1)));
  	//d3_=((((d2_&b8e)>>1)^(d1_&b8o))|((d2_&b8e)^((d1_&b8o)<<1)));

	d1__=((((b3_&b8e)>>1)^(b4_&b8o))|((b3_&b8e)^((b4_&b8o)<<1)));	
	d2__=((((b3_&b8e)>>1)^(b5_&b8o))|((b3_&b8e)^((b5_&b8o)<<1)));
  	//d3__=((((d2__&b8e)>>1)^(d1__&b8o))|((d2__&b8e)^((d1__&b8o)<<1)));

	return(0);
}

int	c_p(char cl_a[256],int ci,int tf,char r_a[256])
{
	int	a,b,c,d=0;
	
	for(a=0;a<256;++a){r_a[a]='\0';}
	if(tf)
	{
		for(a=0;a<strlen(c_l);++a){
			if(cl_a[a]=='{'){++d;for(b=a+1;b<strlen(cl_a);++b){if(cl_a[b]=='}'){if(d==ci){break;}}}}
			if(b!=strlen(cl_a)){break;}
		}
	}else{
		for(a=0;a<strlen(cl_a);++a){
			if(cl_a[a]=='['){++d;for(b=a+1;b<strlen(cl_a);++b){if(cl_a[b]==']'){if(d==ci){break;}}}}
			if(b!=strlen(cl_a)){break;}
		}
	}

	d=0;for(c=a+1;c<b;++c){r_a[d]=cl_a[c];++d;if(df){printf("%c",cl_a[c]);}}

	return(0);

}

int	p_r(FILE *fw1,int brw[BtM],int brwi)
{
	int	i,ii=0,j,k,l;

	l=7;j=0;
	for(i=0;i<brwi;)
	{

		if(i<brwi){if(brw[i]  ){j|=1<<l;}--l;}else{break;}
		if(1+i<brwi){if(brw[++i]){j|=1<<l;}--l;}else{break;}
		if(1+i<brwi){if(brw[++i]){j|=1<<l;}--l;}else{break;}
		if(1+i<brwi){if(brw[++i]){j|=1<<l;}--l;}else{break;}
		if(1+i<brwi){if(brw[++i]){j|=1<<l;}--l;}else{break;}
		if(1+i<brwi){if(brw[++i]){j|=1<<l;}--l;}else{break;}
		if(1+i<brwi){if(brw[++i]){j|=1<<l;}--l;}else{break;}
		if(1+i<brwi){if(brw[++i]){j|=1<<l;}--l;}else{break;}

		if(l==-1){fprintf(fw1,"%c",(j&0x00ff));++bytc;++bytc1;l=7;j=0;++i;ii=i;}
					
	}

	//bitc+=i;

	if(i==brwi){brwi=0;}else{

		bti=0;for(j=ii;j<brwi;++j){bt[bti]=brw[j];++bti;}
		i=0;l=7;j=0;
		if(i<bti){if(bt[i]){j|=1<<l;}++i;--l;}
		if(i<bti){if(bt[i]){j|=1<<l;}++i;--l;}
		if(i<bti){if(bt[i]){j|=1<<l;}++i;--l;}
		if(i<bti){if(bt[i]){j|=1<<l;}++i;--l;}
		if(i<bti){if(bt[i]){j|=1<<l;}++i;--l;}
		if(i<bti){if(bt[i]){j|=1<<l;}++i;--l;}
		if(i<bti){if(bt[i]){j|=1<<l;}++i;--l;}
		if(i<bti){if(bt[i]){j|=1<<l;}++i;--l;}

		fprintf(fw1,"%c",(j&0x00ff));++bytc;++bytc1;
	}

	//bitc+=i;

	return(0);
}

int	pr32_(long e,int b_[32],int b_i,int af)
{

	int	a,b=0,d=-1,c;
	b_i=0;
	e&=0xffffffff;
	for(a=32-1;a>=0;--a)
	{
		c=0;c=1<<a;
		if(((c&e)>>a)==1){if(d==-1){d=a;}if(af){b_[b_i]=1;++b_i;}}else{
			if(af){b_[b_i]=0;++b_i;}
		}
	}
	//printf("!%d %d!",e,d);
	return(d+1);

}

int	pr24_(long e,int b_[32],int b_i,int af)
{

	int	a,b=0,d=-1,c;
	b_i=0;
	e&=0x00ffffff;
	for(a=24-1;a>=0;--a)
	{
		c=0;c=1<<a;
		if(((c&e)>>a)==1){if(d==-1){d=a;}if(af){b_[b_i]=1;++b_i;}}else{
			if(af){b_[b_i]=0;++b_i;}
		}
	}
	//printf("!%d %d!",e,d);
	return(d+1);

}

int pf_(long n)
{

	int	d,i,j,l=0;

	d=pr24_(n,bt32_1,bt32i_1,0);

	if(d>=0&&d<=2) {d=2;}
	if(d>2&&d<=4)  {d=4;}
	if(d>4&&d<=6)  {d=6;}
	if(d>6&&d<=8)  {d=8;}
	if(d>8&&d<=10) {d=10;}
	if(d>10&&d<=12){d=12;}
	if(d>12&&d<=14){d=14;}
	if(d>14&&d<=16){d=16;}
	if(d>16&&d<=18){d=18;}
	if(d>18&&d<=20){d=20;}
	if(d>20&&d<=22){d=22;}
	if(d>22&&d<=24){d=24;}	
	//printf("!%d!",d);

	for(i=0;i<d;i+=2)
	{
		if
		(
			(((n&(0x3<<(d-2-i)))>>(d-2-i))&0x3)!=0
		)
		{
			pr24_((((n&(0x3<<(d-2-i)))>>(d-2-i))&0x3),bt32_1,bt32i_1,1);
			for(j=0;j<2;++j){br[bri]=bt32_1[22+j];++bri;++l;}

		}else{br[bri]=0;++bri;++l;br[bri]=0;++bri;++l;}
	}
	//if(l>16){printf("!%d!",l);}
	return(l);

}

int p_c(void)
{

int	a,b,c,d,e;
int	i,j,k,l,m;
int	b1,b2,b3,b4,b5;

int	fbyte;
int	bi;
long li;


	for(i=0;i<0xffffff;++i){bac[i][0]=-1;bac[i][3]=0;bac_[i]=-1;bac__[i]=-1;}
	for(i=0;i<256;++i){for(j=0;j<256;++j){ba1[i][j][0]=-1;ba2[i][j][1]=0;ba2[i][j][0]=-1;ba3[i][j][1]=0;ba3[i][j][0]=-1;ba1[i][j][3]=0;ba2[i][j][3]=0;ba3[i][j][3]=0;}}

	f1=fopen(infn,"rb");if(f1==NULL){return(-1);}
	while(!feof(f1))
	{
		fbuff[0][0]=1;
		while(!feof(f1)&&(fbuff[0][0]<1024)){fbuff[0][fbuff[0][0]]=getc(f1);fbuff[1][fbuff[0][0]]=0;fbuff[2][fbuff[0][0]]=0;++fbuff[0][0];}
		i=1;b1=fbuff[0][i];b2=fbuff[0][++i];b3=fbuff[0][++i];b4=fbuff[0][++i];b5=fbuff[0][++i];
		while(i<fbuff[0][0])
		{
			bp_(b1,b2,b3,b4,b5);
			if(ba1[d1][d2][0]==-1){ba1[d1][d2][0]=ba1i;++ba1i;}else{++ba1[d1][d2][1];}
			if(ba2[d1_][d2_][0]==-1){ba2[d1_][d2_][0]=ba2i;++ba2i;}else{++ba2[d1_][d2_][1];}
			if(ba3[d1__][d2__][0]==-1){ba3[d1__][d2__][0]=ba3i;++ba3i;}else{++ba3[d1__][d2__][1];}			
			if(bac[(b2<<16)|(b3<<8)|(b4<<0)][0]==-1)
			{
				bac[(b2<<16)|(b3<<8)|(b4<<0)][0]=ba1[d1][d2][0];
				bac[(b2<<16)|(b3<<8)|(b4<<0)][1]=ba2[d1_][d2_][0];
				bac[(b2<<16)|(b3<<8)|(b4<<0)][2]=ba3[d1__][d2__][0];
				bac[(b2<<16)|(b3<<8)|(b4<<0)][4]=baci;++baci;
				++bac[(b2<<16)|(b3<<8)|(b4<<0)][3];
			}else{++bac[(b2<<16)|(b3<<8)|(b4<<0)][3];}			
			b1=b2;b2=b3;b3=b4;b4=b5;b5=fbuff[0][++i];
		}
	}
	fclose(f1);
	
	f1=fopen(infn,"rb");if(f1==NULL){return(-1);}
	b1=getc(f1);b2=b1;b3=getc(f1);
	if(bac_[(((b1<<16)|(b2<<8)|(b3<<0))&0xffffff)]==-1)
	{
		bac__[baci__]=(((b1<<16)|(b2<<8)|(b3<<0))&0xffffff);bac_[bac__[baci__]]=baci__;++baci__;
	}	
	fclose(f1);
	
	f1=fopen(infn,"rb");if(f1==NULL){return(-1);}
	while(!feof(f1))
	{
		fbuff[0][0]=1;
		while(!feof(f1)&&(fbuff[0][0]<1024)){fbuff[0][fbuff[0][0]]=getc(f1);fbuff[1][fbuff[0][0]]=0;fbuff[2][fbuff[0][0]]=0;++fbuff[0][0];}
		i=1;b1=fbuff[0][i];b2=fbuff[0][++i];b3=fbuff[0][++i];b4=fbuff[0][++i];b5=fbuff[0][++i];
		while(i<fbuff[0][0])
		{
			bp_(b1,b2,b3,b4,b5);
			if
			(
			ba1[d1][d2][0]==bac[(b2<<16)|(b3<<8)|(b4<<0)][0]
			&&
			ba2[d1_][d2_][0]==bac[(b2<<16)|(b3<<8)|(b4<<0)][1]
			&&			
			ba3[d1__][d2__][0]==bac[(b2<<16)|(b3<<8)|(b4<<0)][2]
			)
			{
				fbuff[2][-1-1-1+i]=1;fbuff[2][-1-1+i]=1;fbuff[2][-1+i]=1;
				if(bac_[(((b2<<16)|(b3<<8)|(b4<<0))&0xffffff)]==-1)
				{
					bac__[baci__]=(((b2<<16)|(b3<<8)|(b4<<0))&0xffffff);bac_[bac__[baci__]]=baci__;++baci__;
				}
			}
			b1=b2;b2=b3;b3=b4;b4=b5;b5=fbuff[0][++i];
		}
	}
	fclose(f1);
	
	bc=0;bc_=0;bitc=0;
	f1=fopen(infn,"rb");if(f1==NULL){return(-1);}
	f3=fopen(cfn,"wb");if(f3==NULL){return(-1);}
	f4=fopen(nfn,"wb");if(f4==NULL){return(-1);}
	
	while(!feof(f1))
	{
		
			b1=getc(f1);if(b1<0){b1=0;}
			b2=getc(f1);if(b2<0){b2=0;}
			b3=getc(f1);if(b3<0){b3=0;}
		
			if(bac_[(((b1<<16)|(b2<<8)|(b3<<0))&0xffffff)]==-1)
			{
				bac__[baci__]=(((b1<<16)|(b2<<8)|(b3<<0))&0xffffff);bac_[bac__[baci__]]=baci__;++baci__;
			}
			li=bac_[(((b1<<16)|(b2<<8)|(b3<<0))&0xffffff)];
			
			//
			bc+=3;
			bitc+=l=pf_(li);

			if(bri>128)
			{
				a=bri;
				while(a>128&&((double)a/(double)8)==(a/8)){--a;}
				i=0;
				while(i<a-8)
				{
					j=0;
					if(br[i]){j|=1<<7;}
					if(br[++i]){j|=1<<6;}
					if(br[++i]){j|=1<<5;}
					if(br[++i]){j|=1<<4;}
					if(br[++i]){j|=1<<3;}
					if(br[++i]){j|=1<<2;}
					if(br[++i]){j|=1<<1;}
					if(br[++i]){j|=1<<0;}
					fprintf(f3,"%c",j&0xff);
					++i;
				}
				bi=bri;bri=0;for(j=i;j<bi;++j){br[bri]=br[j];++bri;}
			}

			//
			
			if(l==2) {d=1;}
			if(l==4) {d=2;}
			if(l==6) {d=3;}
			if(l==8) {d=4;}
			if(l==10){d=5;}
			if(l==12){d=6;}
			if(l==14){d=7;}
			if(l==16){d=8;}
			if(l==18){d=9;}
			if(l==20){d=10;}
			if(l==22){d=11;}
			if(l==24){d=12;}
			pr24_(d&0xff,bt32_1,bt32i_1,1);
			for(j=0;j<4;++j){br___[bri___]=bt32_1[(24-4)+j];++bri___;}bitc+=4;

			if(bri___>128)
			{
				a=bri___;
				while(a>128&&((double)a/(double)8)==(a/8)){--a;}
				i=0;
				while(i<a-8)
				{
					j=0;
					if(br___[i]){j|=1<<7;}
					if(br___[++i]){j|=1<<6;}
					if(br___[++i]){j|=1<<5;}
					if(br___[++i]){j|=1<<4;}
					if(br___[++i]){j|=1<<3;}
					if(br___[++i]){j|=1<<2;}
					if(br___[++i]){j|=1<<1;}
					if(br___[++i]){j|=1<<0;}
					fprintf(f4,"%c",j&0xff);
					++i;
				}
				bi=bri___;bri___=0;for(j=i;j<bi;++j){br___[bri___]=br___[j];++bri___;}
			}
			
	}
	fclose(f1);

	bri=p_r(f3,br,bri);fclose(f3);
	bri___=p_r(f4,br___,bri___);fclose(f4);

	printf("\t File Bytes %d bytes \n\t Compressed %d bytes \n\t to %d bytes \n",
	(bc_+bc),
	bc,
	(bitc/8));

	printf("\t Ratio %e \n\n",100.0*(((double)bitc/(double)8)/(double)bc));
	
	f7=fopen(cifn,"wb");if(f7==NULL){return(-1);}
	for(li=0;li<baci__;++li)
	{		
		fprintf(f7,"%c%c%c",((int)((bac__[li]&0xff0000)>>16)&0xff),(int)(((bac__[li]&0x00ff00)>>8)&0xff),(int)(((bac__[li]&0x0000ff)>>0)&0xff));
	}
	fclose(f7);

	return(0);

}

int	p_d(void)
{

int	a,b,c,d,e;
int	i,j,k,l,m,n,q;
int b1,b2,b3;
int	fbyte;
long	bi;

	f7=fopen(cifn,"rb");if(f7==NULL){return(-1);}
	baci__=0;
	while(!feof(f7))
	{
		b1=getc(f7);b2=getc(f7);b3=getc(f7);
		bac__[baci__]=(((b1<<16)|(b2<<8)|(b3<<0))&0xffffff);++baci__;
	}
	fclose(f7);

	f3=fopen(cfn,"rb");if(f3==NULL){return(-1);}
	f4=fopen(nfn,"rb");if(f4==NULL){return(-1);}
	f6=fopen(dfn,"wb");if(f6==NULL){return(-1);}

	bti=0;bri_=0;
	while(!feof(f3))
	{

		while(bti<128)
		{
			b1=getc(f3);if(b1<0){b1=0;}b2=getc(f3);if(b2<0){b2=0;}b3=getc(f3);if(b3<0){b3=0;}
			pr24_((b1<<16)|(b2<<8)|(b3<<0),bt32_1,bt32i_1,1);
			for(j=0;j<24;++j){bt[bti]=bt32_1[j];++bti;}
			if(feof(f3)){break;}
		}
		while(bri_<128)
		{
			b1=getc(f4);if(b1<0){b1=0;}b2=getc(f4);if(b2<0){b2=0;}b3=getc(f4);if(b3<0){b3=0;}
			pr24_((b1<<16)|(b2<<8)|(b3<<0),bt32_1,bt32i_1,1);
			for(j=0;j<24;++j){br_[bri_]=bt32_1[j];++bri_;}
			if(feof(f4)){break;}
		}

		while(bti/8>3&&bri_/8>3)
		{

		n=0;l=0;	
		if(br_[n]){l|=1<<3;}
		if(br_[++n]){l|=1<<2;}
		if(br_[++n]){l|=1<<1;}	
		if(br_[++n]){l|=1<<0;}	
					
		++n;bi=bri_;bri_=0;for(j=n;j<bi;++j){br_[bri_]=br_[j];++bri_;}

		if(l==1) {d=2;}
		if(l==2) {d=4;}
		if(l==3) {d=6;}
		if(l==4) {d=8;}
		if(l==5){d=10;}
		if(l==6){d=12;}
		if(l==7){d=14;}
		if(l==8){d=16;}
		if(l==9){d=18;}
		if(l==10){d=20;}
		if(l==11){d=22;}
		if(l==12){d=24;}
		
		m=0;i=0;
		while(i<d&&i<bti)
		{
			m=m<<2;
			if(bt[i]){m|=1<<1;}
			if(bt[++i]){m|=1<<0;}
			++i;				
		}
		if(i==bti){printf("-1.bl Error...%d %d\n",l,i);return(-1);}

		bi=bti;bti=0;for(j=i;j<bi;++j){bt[bti]=bt[j];++bti;}
				
		if(m>baci__){printf("-2.bl Error...%d %d\n",m,(int)baci__);return(-1);}
		fprintf(f6,"%c%c%c",((int)((bac__[m]&0xff0000)>>16)&0xff),(int)(((bac__[m]&0x00ff00)>>8)&0xff),(int)(((bac__[m]&0x0000ff)>>0)&0xff));
		//printf("%c%c%c",((int)((bac__[m]&0xff0000)>>16)&0xff),(int)(((bac__[m]&0x00ff00)>>8)&0xff),(int)(((bac__[m]&0x0000ff)>>0)&0xff));

		}
	}

	fclose(f3);
	fclose(f4);
	fclose(f6);

	return(0);

}

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

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

	char	cmd[50][256];
	char	c_1[256];
	char	c_2[256];
	int		ci;
	
	char	a1a[256];
	char	a2a[256];
	char	a3a[256];	
	int		aia,a1f;
	long	bi,b1i,b2i,b3i,b4i,b5i,b6i;
	int		fbyte;

	int		a1,a2,a3,a4;
	
	printf("\n\tBit Parity Compression 4.0.2\n");
	printf("\twritten by Dzinleski Jasenko 2012 , 2016\n\n");

	if(argv[1]==NULL&&argv[2]==NULL){return(0);}

	for(j=0;j<256;++j){a1a[j]='\0';a2a[j]='\0';a3a[j]='\0';}
	for(i=0;i<50;++i){for(j=0;j<256;++j){cmd[i][j]='\0';}}
	for(i=0;i<argc;++i){strcpy(cmd[i],argv[i]);}
	//for(j=0;j<i;++j){printf("%s\n",cmd[j]);}

	if(strlen(cmd[1])==0){return(0);}
	//if(strlen(cmd[2])==0){return(0);}

	ci=1;
	c_p(c_l,ci,1,c_1);
	c_p(c_1,ci,0,c_2);
	if(c_2[0]<=cmd[1][0]&&cmd[1][0]<=c_2[1])
	{
		if(df){printf("%c\n",cmd[1][0]);}
		ci=2;
		c_p(c_l,ci,1,c_1);
		ci=1;
		c_p(c_1,ci,0,c_2);

		if(c_2[0]<=cmd[1][1]&&cmd[1][1]<=c_2[1])
		{
			printf("\n");
			printf("\tbpc_402 -c<filename to compress>");
			printf("\n");
			printf("\tbpc_402 -d<filename to decompress>");
			printf("\n");

			return(0);

		}else{
			ci=2;
			c_p(c_1,ci,0,c_2);
			if(c_2[0]<=cmd[1][1]&&cmd[1][1]<=c_2[1])
			{
			printf("\tThis source and Compression method apply to GNU General Public License.  \n");
			printf("\tCopyright (C) 2012 , 2016 Jasenko Dzinleski  \n");
			printf("\n");
			printf("\tThis source applies to the GNU General Public License as \n");
			printf("\tpublished by the Free Software Foundation  \n");
			printf("\tand can not be used, copied, sold, redistributed or  \n");
			printf("\tused in any other way but only by written permission by Jasenko Dzinleski .  \n");
			printf("\tCopyright (C) from 2001 - 2016 and later by Jasenko Dzinleski  \n");
			printf("\n");
			printf("\tThis program is distributed in the hope that it will be useful, but \n");
			printf("\tWITHOUT ANY WARRANTY; without even the implied warranty of \n");
			printf("\tMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU \n");
			printf("\tGeneral Public License for more details.  \n");
			printf("\n");
			printf("\tYou should have received a copy of the GNU General Public License along \n");
			printf("\twith this program; if not, write to the Free Software Foundation, Inc., \n");
			printf("\t51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. \n");
			printf("\n");
			printf("\tBit Parity Compression 3.0.2\n");
			printf("\twritten by Dzinleski Jasenko  2012 , November 2016\n");
			printf("\n");

	 		return(0);

			}else{
			
				ci=5;
				c_p(c_1,ci,0,c_2);
				if(c_2[0]<=cmd[1][1]&&cmd[1][1]<=c_2[1])
				{
				printf("\n");
				printf("\tbpc_402 -c<filename to compress>");
				printf("\n");
				printf("\tbpc_402 -d<filename to decompress>");
				printf("\n");

				return(0);
				
				}
			}
		}

	}else{printf("Error Token %c\n",cmd[1][0]);return(0);}

	ci=1;
	c_p(c_l,ci,1,c_1);
	c_p(c_1,ci,0,c_2);
	if(c_2[0]<=cmd[1][0]&&cmd[1][0]<=c_2[1])
	{
		if(df){printf("%c\n",cmd[1][0]);}
		ci=2;
		c_p(c_l,ci,1,c_1);
		ci=4;
		c_p(c_1,ci,0,c_2);
		if(c_2[0]<=cmd[1][1]&&cmd[1][1]<=c_2[1])
		{
			if(df){printf("%c\n",cmd[1][1]);}
			ci=3;
			c_p(c_l,ci,1,c_1);
			aia=0;
			for(j=2;j<strlen(cmd[1]);++j)
			{
				i=1;
				while(i<=11)
				{
					ci=i;
					c_p(c_1,ci,0,c_2);
					if(c_2[0]<=cmd[1][j]&&cmd[1][j]<=c_2[1]){if(df){printf("%c\n",cmd[1][j]);}a1a[aia]=cmd[1][j];++aia;break;}else{++i;continue;}
					++i;
				}
			}
			if(j==strlen(cmd[1]))
			{

				a1f=1;
				strcpy(infn,a1a);f1=fopen(infn,"rb");
				if(f1==NULL){return(0);}else{fclose(f1);}
				
				for(i=0;i<256;++i){fn_[i]='\0';}
				j=0;for(i=0;i<256;++i){if(infn[i]!='.'){fn_[j]=infn[i];++j;}else{break;}}
				fn_[j]='_';++j;
				for(i=0;i<256;++i){if(cfn[i]!='\0'){fn_[j]=cfn[i];++j;}else{break;}}
				strcpy(cfn,fn_);
				
				for(i=0;i<256;++i){fn_[i]='\0';}
				j=0;for(i=0;i<256;++i){if(infn[i]!='.'){fn_[j]=infn[i];++j;}else{break;}}
				fn_[j]='_';++j;
				for(i=0;i<256;++i){if(cifn[i]!='\0'){fn_[j]=cifn[i];++j;}else{break;}}
				strcpy(cifn,fn_);
				
				for(i=0;i<256;++i){fn_[i]='\0';}
				j=0;for(i=0;i<256;++i){if(infn[i]!='.'){fn_[j]=infn[i];++j;}else{break;}}
				fn_[j]='_';++j;
				for(i=0;i<256;++i){if(nfn[i]!='\0'){fn_[j]=nfn[i];++j;}else{break;}}
				strcpy(nfn,fn_);
								
				for(i=0;i<256;++i){fn_[i]='\0';}
				j=0;for(i=0;i<256;++i){if(infn[i]!='.'){fn_[j]=infn[i];++j;}else{break;}}
				fn_[j]='_';++j;
				
				for(i=0;i<256;++i){fn_[i]='\0';}
				j=0;for(i=0;i<256;++i){if(infn[i]!='.'){fn_[j]=infn[i];++j;}else{break;}}
				fn_[j]='_';++j;
				for(i=0;i<256;++i){if(dfn[i]!='\0'){fn_[j]=dfn[i];++j;}else{break;}}
				strcpy(dfn,fn_);
				
				//(1.1)
				bc_=0,bc=0,bitc=0;
				printf("\n\tFilename: %s\n\n",infn);
				e=p_c();//p_d();
				
				return(0);
				
			}else{printf("Error Parameter %s\n",cmd[1]);return(0);}

		}else{
		
			if(df){printf("%c\n",cmd[1][0]);}
			ci=2;
			c_p(c_l,ci,1,c_1);
			ci=6;
			c_p(c_1,ci,0,c_2);
			if(c_2[0]<=cmd[1][1]&&cmd[1][1]<=c_2[1])
			{
				if(df){printf("%c\n",cmd[1][1]);}
				ci=3;
				c_p(c_l,ci,1,c_1);
				aia=0;
				for(j=2;j<strlen(cmd[1]);++j)
				{
					i=1;
					while(i<=11)
					{
						ci=i;
						c_p(c_1,ci,0,c_2);
						if(c_2[0]<=cmd[1][j]&&cmd[1][j]<=c_2[1]){if(df){printf("%c\n",cmd[1][j]);}a1a[aia]=cmd[1][j];++aia;break;}else{++i;continue;}
						++i;
					}
				}
				if(j==strlen(cmd[1]))
				{

				a1f=1;
				strcpy(infn,a1a);

				for(i=0;i<256;++i){fn_[i]='\0';}
				j=0;for(i=0;i<256;++i){if(infn[i]!='.'){fn_[j]=infn[i];++j;}else{break;}}
				fn_[j]='_';++j;
				for(i=0;i<256;++i){if(cfn[i]!='\0'){fn_[j]=cfn[i];++j;}else{break;}}
				strcpy(cfn,fn_);
				
				for(i=0;i<256;++i){fn_[i]='\0';}
				j=0;for(i=0;i<256;++i){if(infn[i]!='.'){fn_[j]=infn[i];++j;}else{break;}}
				fn_[j]='_';++j;
				for(i=0;i<256;++i){if(cifn[i]!='\0'){fn_[j]=cifn[i];++j;}else{break;}}
				strcpy(cifn,fn_);
				
				for(i=0;i<256;++i){fn_[i]='\0';}
				j=0;for(i=0;i<256;++i){if(infn[i]!='.'){fn_[j]=infn[i];++j;}else{break;}}
				fn_[j]='_';++j;
				for(i=0;i<256;++i){if(nfn[i]!='\0'){fn_[j]=nfn[i];++j;}else{break;}}
				strcpy(nfn,fn_);
								
				for(i=0;i<256;++i){fn_[i]='\0';}
				j=0;for(i=0;i<256;++i){if(infn[i]!='.'){fn_[j]=infn[i];++j;}else{break;}}
				fn_[j]='_';++j;
				
				for(i=0;i<256;++i){fn_[i]='\0';}
				j=0;for(i=0;i<256;++i){if(infn[i]!='.'){fn_[j]=infn[i];++j;}else{break;}}
				fn_[j]='_';++j;
				for(i=0;i<256;++i){if(dfn[i]!='\0'){fn_[j]=dfn[i];++j;}else{break;}}
				strcpy(dfn,fn_);

				for(i=0;i<256;++i){fn_[i]='\0';}
				j=0;for(i=0;i<256;++i){if(infn[i]!='.'){fn_[j]=infn[i];++j;}else{break;}}
				fn_[j]='_';++j;
				for(i=0;i<256;++i){if(ar_fn[i]!='\0'){fn_[j]=ar_fn[i];++j;}else{break;}}
				strcpy(ar_fn,fn_);

				for(i=0;i<256;++i){fn_[i]='\0';}
				j=0;for(i=0;i<256;++i){if(infn[i]!='.'){fn_[j]=infn[i];++j;}else{break;}}
				fn_[j]='_';++j;

				e=p_d();if(e!=-1){printf("\tOk\n\n");}

				return(0);
		
							
				}else{printf("Error Parameter %s\n",cmd[1]);return(0);}		
		
			}else{printf("Error Token %c\n",cmd[1][1]);return(0);}
		}		
	}else{printf("Error Token %c\n",cmd[1][0]);return(0);}
	
	return(0);

}