//	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 , 2015
//


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

#define		BuffL			128
#define		BtM			8*256
#define		TBEnd			24

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		rfn[256]="r.mar";
char		rifn[256]="ri.mar";

char		dfn[256]="out.mar";
char		ar_fn[256]=".mar";

char		fn_[256];

int 			ba2b[256][256][5];	int ba2bi=0;
int 			fb_[3][1024];
int			r_[256][2];
int			ri_=0;

int			b8o,b8e;
int			b16o,b16e;
int 			ba16[16][16][16][16][2];	int ba16i=0;
int			da[16][3];			
int			df=0;

long 			bd[65536][3];
int			bdi=0;

long 			bd_[65536][3];
int			bdi_=0;

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 p_d_(int b1_,int b2_,int b3_)
{
int	b1,b2,b3;
int	b8o,b8e;

	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;

	d1_=((((b2&b8e)>>1)^(b1&b8o))|((b2&b8e)^((b1&b8o)<<1)));	
	d2_=((((b2&b8e)>>1)^(b3&b8o))|((b2&b8e)^((b3&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	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;}

	//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;}
	}

	return(l);

}

int p_c(void)
{

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

int	b1_,b2_,b3_;
int	b1,b2,b3;

int	d1,d2,d3;
int	d1_,d2_,d3_;

int	fbyte;
int	bi;

int	mc=0,unmc=0;

	for(i=0;i<256;++i){for(j=0;j<256;++j){ba2b[i][j][0]=-1;}}ba2bi=0;
	
	f1=fopen(infn,"rb");if(f1==NULL){return(-1);}
	while(!feof(f1))
	{

		fb_[0][0]=1;fb_[1][0]=0;fb_[2][0]=0;
		while(!feof(f1)&&fb_[0][0]<1024){fb_[1][fb_[0][0]]==0;fb_[2][fb_[0][0]]=0;fb_[0][fb_[0][0]]=getc(f1);++fb_[0][0];}

		i=1;b1_=fb_[0][i];b2_=fb_[0][++i];b3_=fb_[0][++i];
		while(i<fb_[0][0])
		{
			
			b1=b1_<<1;
			b2=b2_<<1;
			b3=b3_<<1;

			p_d_(b1,b2,b3);d1__=d1_;d2__=d2_;

			if(ba2b[(b1_&0xff)][(b3_&0xff)][0]==-1)
			{

				ba2b[(b1_&0xff)][(b3_&0xff)][0]=ba2bi;++ba2bi;
				ba2b[(b1_&0xff)][(b3_&0xff)][1]=b2_;
				ba2b[(b1_&0xff)][(b3_&0xff)][2]=1;
				ba2b[(b1_&0xff)][(b3_&0xff)][3]=d1__;
				ba2b[(b1_&0xff)][(b3_&0xff)][4]=d2__;
			
			}else{

				++ba2b[(b1_&0xff)][(b3_&0xff)][2];

			}

			if((ba2b[(b1_&0xff)][(b3_&0xff)][3]!=d1__)||(ba2b[(b1_&0xff)][(b3_&0xff)][4]!=d2__)){++unmc;fb_[1][-2+i]=1;fb_[1][-1+i]=1;fb_[1][i]=1;}
			if((ba2b[(b1_&0xff)][(b3_&0xff)][3]==d1__)&&(ba2b[(b1_&0xff)][(b3_&0xff)][4]==d2__)){++mc;fb_[2][-2+i]=1;fb_[2][-1+i]=1;fb_[2][i]=1;}

			b1_=b2_;
			b2_=b3_;
			b3_=fb_[0][++i];

		}
		
		j=1;
		while(j<fb_[0][0])
		{
			if(fb_[2][j]==1)
			{
					while((fb_[2][j]==1)&&(j<fb_[0][0]))
					{
						//printf("%c",fb_[0][j]);
						if(ri_==0){r_[ri_][0]=fb_[0][j];r_[ri_][1]=1;++ri_;}else{
						for(i=0;i<ri_;++i){if((r_[i][0]&0xff)==fb_[0][j]){++r_[i][1];break;}}
						if(i==ri_){r_[ri_][0]=fb_[0][j];r_[ri_][1]=1;++ri_;}
						}
						++j;
					}
					continue;					
			}
			++j;
		}
	}
	fclose(f1);
	
	for(i=0;i<ri_;++i)
	{
		for(j=0;j<ri_;++j)
		{
			if(r_[i][1]>r_[j][1])
			{
				k=r_[i][0];
				m=r_[i][1];
				r_[i][0]=r_[j][0];
				r_[i][1]=r_[j][1];
				r_[j][0]=k;
				r_[j][1]=m;
			}		
		}
	}
	
	//for(i=0;i<ri_;++i){printf("!%d %d!",r_[i][0],r_[i][1]);}
	//printf("!%d!\n",ri_);
	
	bdi_=0;
	for(i=0;i<256;++i){for(j=0;j<256;++j){

		if(ba2b[i][j][0]!=-1)
		{
			bd_[bdi_][0]=(i<<8)|(j<<0);
			bd_[bdi_][1]=0;
			bd_[bdi_][2]=ba2b[i][j][2];
			++bdi_;if(bdi_>65536){printf("a Error...\n");return(-1);}
		}
	}}
	
	//printf("!%d!\n",bdi_);

	for(i=0;i<bdi_;++i)
	{
		for(j=0;j<bdi_;++j)
		{
			if(bd_[i][2]>bd_[j][2])
			{
				bi=bd_[i][0];
				k=bd_[i][1];
				m=bd_[i][2];
				bd_[i][0]=bd_[j][0];
				bd_[i][1]=bd_[j][1];
				bd_[i][2]=bd_[j][2];
				bd_[j][0]=bi;
				bd_[j][1]=k;
				bd_[j][2]=m;
			}
		}
	}

	for(bi=0;bi<65536;++bi){bd[bi][0]=-1;}
	for(k=0;k<bdi_;++k){bd[bd_[k][0]][0]=k;}

	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);}
	f7=fopen(rfn,"wb");if(f7==NULL){return(-1);}

	b1=getc(f1);
	b2=getc(f1);
	b3=getc(f1);

	while(!feof(f1))
	{

		if(bd[((((b1<<8)&0xff00)|(b3<<0))&0xffff)][0]!=-1)
		{
			//
			bc+=3;
			bitc+=l=pf_(bd[((((b1<<8)&0xff00)|(b3<<0))&0xffff)][0]&0xffff);

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

			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___;}
			}
						
			//
			
			for(i=0;i<ri_;++i){if((r_[i][0]&0xff)==b2){break;}}
			if(i==ri_){printf("r Error... %c \n",b2);return(-1);}

			j=pr24_(i&0xff,bt32_1,bt32i_1,1);
			if(j<=4){br__[bri__]=1;++bri__;for(j=0;j<4;++j){br__[bri__]=bt32_1[(24-4)+j];++bri__;}bitc+=5;}else{
				if(j<=8){
					br__[bri__]=0;++bri__;for(j=0;j<8;++j){br__[bri__]=bt32_1[(24-8)+j];++bri__;}bitc+=9;
				}
			}

			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(f7,"%c",j&0xff);
					++i;
				}
				bi=bri__;bri__=0;for(j=i;j<bi;++j){br__[bri__]=br__[j];++bri__;}
			}

		}

		b1=getc(f1);if(feof(f1)){break;}
		b2=getc(f1);if(feof(f1)){break;}
		b3=getc(f1);if(feof(f1)){break;}
	}
	fclose(f1);

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

	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(i=0;i<bdi_;++i){fprintf(f7,"%c%c",((bd_[i][0]&0xff00)>>8)&0xff,((bd_[i][0]&0x00ff)>>0)&0xff);}fclose(f7);

	f7=fopen(rifn,"wb");if(f7==NULL){return(-1);}
	for(i=0;i<ri_;++i){fprintf(f7,"%c",r_[i][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	b1,b2,b3;

int	d1,d2,d3;
int	d1_,d2_,d3_;

int	fbyte;
long	bi;

int	mc=0,unmc=0;

	f7=fopen(cifn,"rb");if(f7==NULL){return(-1);}
	bdi_=0;
	b1=getc(f7);b2=getc(f7);
	while(!feof(f7))
	{
		bd_[bdi_][0]=((((b1<<8)&0xff00)|(b2<<0))&0xffff);++bdi_;
		b1=getc(f7);b2=getc(f7);
	}
	fclose(f7);

	f7=fopen(rifn,"rb");if(f7==NULL){return(-1);}
	ri_=0;
	b1=getc(f7);
	while(!feof(f7))
	{
		r_[ri_][0]=b1&0xff;
		++ri_;
		b1=getc(f7);
	}
	fclose(f7);

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

	pr24_((((getc(f3)<<16)&0xff0000)|((getc(f3)<<8)&0xff00)|(getc(f3)<<0)),bt32_1,bt32i_1,1);
	bti=0;for(j=0;j<24;++j){bt[bti]=bt32_1[j];++bti;}

	pr24_((((getc(f4)<<16)&0xff0000)|((getc(f4)<<8)&0xff00)|(getc(f4)<<0)),bt32_2,bt32i_2,1);
	bri_=0;for(j=0;j<24;++j){br_[bri_]=bt32_2[j];++bri_;}

	pr24_((((getc(f7)<<16)&0xff0000)|((getc(f7)<<8)&0xff00)|(getc(f7)<<0)),bt32_2,bt32i_2,1);
	bri=0;for(j=0;j<24;++j){br[bri]=bt32_2[j];++bri;}

		while(!feof(f3)&&bti<32-8)
		{
			b1=getc(f3);
			if(!feof(f3))
			{					
				pr24_(((b1<<16)|(0<<8)|(0<<0)),bt32_1,bt32i_1,1);
				for(j=0;j<8;++j){bt[bti]=bt32_1[j];++bti;}
			}
		}
			

		//

		n=0;l=0;	
		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(!feof(f4)&&bri_<32-8)
		{
			b1=getc(f4);
			while(!feof(f4)&&bri_<32-8)
			{					
				pr24_(((b1<<16)|(0<<8)|(0<<0)),bt32_1,bt32i_1,1);
				for(j=0;j<8;++j){br_[bri_]=bt32_1[j];++bri_;}
			}
		}		
		if(l==0) {d=2;}
		if(l==1) {d=4;}
		if(l==2) {d=6;}
		if(l==3) {d=8;}
		if(l==4){d=10;}
		if(l==5){d=12;}
		if(l==6){d=14;}
		if(l==7){d=16;}
		if(l==8){d=18;}
		if(l==9){d=20;}
		//
				
		for(i=0;i<d&&i<bti;)
		{
			if(i==0){m=0;}else{m=m<<2;}
			if(bt[i]){m|=1<<1;}
			if(bt[++i]){m|=1<<0;}
			++i;				
		}
		if(!feof(f3)&&i<l&&i==bti){printf("bl Error...%d %d\n",l,i);return(-1);}
		
		bi=bti;bti=0;for(j=i;j<bi;++j){bt[bti]=bt[j];++bti;}
	
		fprintf(f6,"%c",((bd_[m][0]&0xff00)>>8)&0xff);
	
	while(!feof(f3))
	{
		//---

		if(!feof(f7)&&bri<32-8)
		{
			b1=getc(f7);
			if(!feof(f7)&&bri<32-8)
			{					
				pr24_(((b1<<16)|(0<<8)|(0<<0)),bt32_1,bt32i_1,1);
				for(j=0;j<8;++j){br[bri]=bt32_1[j];++bri;}
			}
		}				
		n=0;
		if(br[n])
		{
			j=0;
			//if(br[++n]){j|=1<<4;}
			if(br[++n]){j|=1<<3;}
			if(br[++n]){j|=1<<2;}
			if(br[++n]){j|=1<<1;}
			if(br[++n]){j|=1<<0;}
			if(j<ri_){fprintf(f6,"%c",r_[j][0]&0xff);}else{printf("ri Error...%d %d\n",ri_,j);return(-1);}
			++n;
		}else{
			j=0;
			if(br[++n]){j|=1<<7;}
			if(br[++n]){j|=1<<6;}
			if(br[++n]){j|=1<<5;}
			if(br[++n]){j|=1<<4;}						
			if(br[++n]){j|=1<<3;}
			if(br[++n]){j|=1<<2;}
			if(br[++n]){j|=1<<1;}
			if(br[++n]){j|=1<<0;}
			if(j<ri_){fprintf(f6,"%c",r_[j][0]&0xff);}else{printf("ri Error...%d %d\n",ri_,j);return(-1);}
			++n;
		}
		bi=bri;bri=0;for(j=n;j<bi;++j){br[bri]=br[j];++bri;}										
		if(!feof(f7)&&bri<32-8)
		{
			b1=getc(f7);
			if(!feof(f7)&&bri<32-8)
			{					
				pr24_(((b1<<16)|(0<<8)|(0<<0)),bt32_1,bt32i_1,1);
				for(j=0;j<8;++j){br[bri]=bt32_1[j];++bri;}
			}
		}

		fprintf(f6,"%c",((bd_[m][0]&0x00ff)>>0)&0xff);
		
		//---

		while(!feof(f3)&&bti<32-8)
		{
			b1=getc(f3);
			if(!feof(f3))
			{					
				pr24_(((b1<<16)|(0<<8)|(0<<0)),bt32_1,bt32i_1,1);
				for(j=0;j<8;++j){bt[bti]=bt32_1[j];++bti;}
			}
		}
			

		//

		n=0;l=0;	
		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(!feof(f4)&&bri_<32-8)
		{
			b1=getc(f4);
			while(!feof(f4)&&bri_<32-8)
			{					
				pr24_(((b1<<16)|(0<<8)|(0<<0)),bt32_1,bt32i_1,1);
				for(j=0;j<8;++j){br_[bri_]=bt32_1[j];++bri_;}
			}
		}		
		if(l==0) {d=2;}
		if(l==1) {d=4;}
		if(l==2) {d=6;}
		if(l==3) {d=8;}
		if(l==4){d=10;}
		if(l==5){d=12;}
		if(l==6){d=14;}
		if(l==7){d=16;}
		if(l==8){d=18;}
		if(l==9){d=20;}
		//
				
		for(i=0;i<d&&i<bti;)
		{
			if(i==0){m=0;}else{m=m<<2;}
			if(bt[i]){m|=1<<1;}
			if(bt[++i]){m|=1<<0;}
			++i;				
		}
		if(!feof(f3)&&i<l&&i==bti){printf("bl Error...%d %d\n",l,i);return(-1);}
		
		bi=bti;bti=0;for(j=i;j<bi;++j){bt[bti]=bt[j];++bti;}
	
		fprintf(f6,"%c",((bd_[m][0]&0xff00)>>8)&0xff);
				

	}

		//---

		if(!feof(f7)&&bri<32-8)
		{
			b1=getc(f7);
			if(!feof(f7)&&bri<32-8)
			{					
				pr24_(((b1<<16)|(0<<8)|(0<<0)),bt32_1,bt32i_1,1);
				for(j=0;j<8;++j){br[bri]=bt32_1[j];++bri;}
			}
		}				
		n=0;
		if(br[n])
		{
			j=0;
			//if(br[++n]){j|=1<<4;}
			if(br[++n]){j|=1<<3;}
			if(br[++n]){j|=1<<2;}
			if(br[++n]){j|=1<<1;}
			if(br[++n]){j|=1<<0;}
			if(j<ri_){fprintf(f6,"%c",r_[j][0]&0xff);}else{printf("ri Error...%d %d\n",ri_,j);return(-1);}
			++n;
		}else{
			j=0;
			if(br[++n]){j|=1<<7;}
			if(br[++n]){j|=1<<6;}
			if(br[++n]){j|=1<<5;}
			if(br[++n]){j|=1<<4;}						
			if(br[++n]){j|=1<<3;}
			if(br[++n]){j|=1<<2;}
			if(br[++n]){j|=1<<1;}
			if(br[++n]){j|=1<<0;}
			if(j<ri_){fprintf(f6,"%c",r_[j][0]&0xff);}else{printf("ri Error...%d %d\n",ri_,j);return(-1);}
			++n;
		}
		bi=bri;bri=0;for(j=n;j<bi;++j){br[bri]=br[j];++bri;}										
		if(!feof(f7)&&bri<32-8)
		{
			b1=getc(f7);
			if(!feof(f7)&&bri<32-8)
			{					
				pr24_(((b1<<16)|(0<<8)|(0<<0)),bt32_1,bt32i_1,1);
				for(j=0;j<8;++j){br[bri]=bt32_1[j];++bri;}
			}
		}

		fprintf(f6,"%c",((bd_[m][0]&0x00ff)>>0)&0xff);

		//---

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

	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 2.0.0\n");
	printf("\twritten by Dzinleski Jasenko 2012 , 2015\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("\tmbpc -c<filename to compress>");
			printf("\n");
			printf("\tmbpc -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 , 2015 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 - 2015 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 2.0.0\n");
			printf("\twritten by Dzinleski Jasenko  2012 , 2015\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("\tmbpc -c<filename to compress>");
				printf("\n");
				printf("\tmbpc -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){if(rfn[i]!='\0'){fn_[j]=rfn[i];++j;}else{break;}}
				strcpy(rfn,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(rifn[i]!='\0'){fn_[j]=rifn[i];++j;}else{break;}}
				strcpy(rifn,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();
				
				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){if(rfn[i]!='\0'){fn_[j]=rfn[i];++j;}else{break;}}
				strcpy(rfn,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(rifn[i]!='\0'){fn_[j]=rifn[i];++j;}else{break;}}
				strcpy(rifn,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);

}