Friday, September 4, 2009

Bitread by Bitstream

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>


typedef unsigned char BITSTREAM;
unsigned long int readdata;
unsigned long int *ptr,*x,*y,*Z;





BITSTREAM * bs_open(unsigned char *buf,char *mode)
{
BITSTREAM * bfp;


bfp= buf;

return bfp;
}

unsigned long bs_read(int bitcnt, BITSTREAM *bfp)
{
int bitrem =0;
static unsigned long bitOffset=0;



bfp += (bitOffset/8);
bitrem= 8 - (bitOffset % 8);

char ch=*bfp;


int value1=0;
int value2=0;


/*increase offset for next call*/
bitOffset+=bitcnt;


if(bitrem != 0)
{
if(bitrem >= bitcnt)
{
value1= (unsigned char)(ch <<(8- bitrem)) >> (8- bitcnt) ;

}
else
{
int bitfrom2nd = bitOffset % 8;
value1= (unsigned char)(ch << (8- bitrem) ) >> (8- (bitrem+bitfrom2nd));

if(bitfrom2nd != 0)
{
bfp +=1;
ch=*bfp;
value2= (unsigned char)(ch >> (8- bitfrom2nd) ) ;

printf(" hello %d %d %d \n", value1,value2, bitcnt );
value1|=value2;
}
}
}
else
{
value1= ch >> (8 - bitcnt);
}

printf("Returning Value = %d \n", value1);
return value1;

}


int bs_checkread(BITSTREAM *bfp)
{


if((readdata=bs_read(4,bfp))!=0x4)
{
return 1;
}


if((readdata=bs_read(8,bfp))!=0x14)
{
return 1;
}

if((readdata=bs_read(6,bfp))!=0x9)
{
return 1;
}


if((readdata=bs_read(3,bfp))!=0)
{
return 1;
}


if((readdata=bs_read(4,bfp))!=0x6)
{
return 1;
}



if((readdata=bs_read(7,bfp))!=0x44)
{
return 1;
}


return 0;
}



int bs_close(BITSTREAM *bfp)
{
bfp=NULL;
return *bfp;
}



void main()
{
unsigned char buf[4]={'A','B','C','D'};
BITSTREAM *bfp;
bfp=bs_open(buf,"r");

printf("file \n %s \n\n" , bfp);

if(bfp == NULL )
{
printf("bs:failed to open file \n");
}

if(bs_checkread(bfp))
{
printf("bs:failed while reading input\n");

}
else
{
printf("read operation success-1level 1 success\n");
}
/*bs_close(bfp);*/

/*getch();*/
}

No comments:

Post a Comment