📄 binaryset.c
字号:
/*****************************************************************************//* FIle Name : main.c *//* Description : WLAN FEC scrambler for Encoder/Decoder *//* S(x) = x^7 + x^4 + 1 ; *//* author : miffie *//* Date : aug/04/05 *//* Copyright (c) 2005 miffie All rights reserved. *//*****************************************************************************/#include <stdio.h>#include <math.h>struct binaryset { int size ; char format ; //0:binary(1bit) 1:byte(8bit) char *data ;} ;struct binarysetset { int size ; struct binaryset *set ;} ;struct binaryset read_hexfile(char *fname ) { FILE *fp ;char *p ;char *top ;int size ;struct binaryset bset ; if ((fp = fopen( fname, "r" ))==NULL) { //not opened PRINTF("file %s cannot be opened\n", fname ) ; }//not opened else { //opened PRINTF(" %s opend\n" , fname) ; //1000 byte can be read from a file if ((top = (char *)malloc(1000*sizeof(char)) ) == NULL) { PRINTF( " malloc failed in read_hexfile\n") ; } //fail else { //allocated p = top ; size = 0 ; while( fscanf( fp, "%x", p)!=EOF ) {//while size++ ; PRINTF("%d %x \n", size, *p ) ; p++ ; }//while p = NULL ; //the Last byte } //allocated } //opened if (((char *)realloc(top,(size+1)*sizeof(char)) ) == NULL) { PRINTF( " realloc failed in read_hexfile\n") ; } //fail bset.format =1 ; bset.size = size ; bset.data = top ; return(bset) ;}//read_hexfile struct binaryset byte2binary(struct binaryset datain) { char *p , *q ;char *top ;int ii ,jj ;int size ;struct binaryset bset ; //size is number of byte PRINTF(" byte2binary size = %d\n" , datain.size ) ; if ((top = (char *)malloc(datain.size*8*sizeof(char)) ) == NULL) { PRINTF( " malloc failed in byte2binary\n") ; } //fail else { //allocated p = datain.data ; q = top ; for (jj=0;jj<datain.size;jj++) { //for //LSBfirst for (ii=0;ii<8;ii++) { //for *q++ = ((*p>>ii) & 0x1) ; } //for ii p++ ; }//for jj bset.format = 0 ; bset.size = datain.size*8 ; bset.data = top ; } //allocated return(bset) ;}//byte2binary struct binaryset byte2binary_MSB(struct binaryset datain) { char *p , *q ;char *top ;int ii ,jj ;int size ;struct binaryset bset ; //size is number of byte PRINTF(" byte2binary_MSB size = %d\n" , datain.size ) ; if ((top = (char *)malloc(datain.size*8*sizeof(char)) ) == NULL) { PRINTF( " malloc failed in byte2binary_MSB\n") ; } //fail else { //allocated p = datain.data ; q = top ; for (jj=0;jj<datain.size;jj++) { //for //MSBfirst for (ii=0;ii<8;ii++) { //for *q++ = ((*p>>(7-ii)) & 0x1) ; } //for ii p++ ; }//for jj bset.format = 0 ; bset.size = datain.size*8 ; bset.data = top ; } //allocated return(bset) ;}//byte2binary_MSB struct binaryset binary2byte(struct binaryset datain) { char *p , *q ;char *top ;char tmp ;int ii ,jj ;int size ;struct binaryset bset ; //size is number of byte PRINTF(" binary2byte size = %d\n" , datain.size ) ; if ((top = (char *)malloc((datain.size/8)*sizeof(char)) ) == NULL) { PRINTF( " malloc failed in binary2byte\n") ; } //fail else { //allocated p = datain.data ; q = top ; for (jj=0;jj<datain.size;jj=jj+8) { //for //LSBfirst tmp = 0 ; for (ii=0;ii<8;ii++) { //for tmp += (*p++&0x1)<<ii ; } //for ii *q++ = tmp ; }//for jj bset.format = 1 ; bset.size = datain.size/8 ; bset.data = top ; } //allocated return(bset) ;}//binary2bytestruct binaryset binary2byte_MSB(struct binaryset datain) { char *p , *q ;char *top ;char tmp ;int ii ,jj ;int size ;struct binaryset bset ; //size is number of byte PRINTF(" binary2byte_MSB size = %d\n" , datain.size ) ; if ((top = (char *)malloc((datain.size/8)*sizeof(char)) ) == NULL) { PRINTF( " malloc failed in binary2byte_MSB\n") ; } //fail else { //allocated p = datain.data ; q = top ; for (jj=0;jj<datain.size;jj=jj+8) { //for //MSB first tmp = 0 ; for (ii=0;ii<8;ii++) { //for tmp += ((*p++&0x1)<<(7-ii)) ; } //for ii *q++ = tmp ; }//for jj bset.format = 1 ; bset.size = datain.size/8 ; bset.data = top ; } //allocated return(bset) ;}//binary2byte_MSBstruct binaryset copy_binaryset(struct binaryset datain) { char *p , *q ;char *top ;int ii ,jj ;int size ;struct binaryset bset ; //size is number of byte PRINTF(" copy_binaryset size = %d\n" , datain.size ) ; if ((top = (char *)malloc((datain.size)*sizeof(char)) ) == NULL) { PRINTF( " malloc failed in copy_binaryset\n") ; } //fail else { //allocated p = datain.data ; q = top ; for (jj=0;jj<datain.size;jj++) { //for *q++ = *p++ ; }//for jj bset.format = datain.format ; bset.size = datain.size ; bset.data = top ; } //allocated return(bset) ;}//copy_binaryset void print_binaryset(struct binaryset datain ) { char *p ;int ii ; if ( print_on ) { PRINTF("print_binaryset format=%d size=%d\n", datain.format, datain.size ) ; p = datain.data ; ii = 0 ; for( ii=0;ii<datain.size;ii++) {//for PRINTF("%d %x\n", ii, (*p)&0xff ) ; p++ ; } //for } //print_on }//print_binaryset struct binaryset pad_binaryset(int size ) { //pad_binaryset //size is the number of bitstruct binaryset bset ;char *tmp1, *tmp2 ;int ii ; if ((tmp1 = (char *)malloc(size*sizeof(char)) ) == NULL) { PRINTF( " malloc failed in pad_binaryset\n") ; } //fail else { //allocated //bset1 for padding bit bset.format = 0 ; //a binary bset.size = size ; // bset.data = tmp1 ; // tmp2 = tmp1 ; for (ii=0;ii<size;ii++) {//for *tmp2++ = 0 ; } //for } //allocated return( bset ) ;}//pad_binaryset struct binaryset cat_binaryset(struct binaryset data1, struct binaryset data2) { char *p , *q ;char *top ;int ii ,jj ;int size ;struct binaryset bset ; //size is number of bit PRINTF("cat_binaryset size=%d + %d\n", data1.size, data2.size) ; size = data1.size + data2.size ; if ((top = (char *)malloc(size*sizeof(char)) ) == NULL) { PRINTF( " malloc failed in cat_binaryset size=%d\n", size) ; } //fail else { //allocated p = data1.data ; q = top ; for (jj=0;jj<data1.size;jj++) { //for *q++ = *p++ ; }//for jj p = data2.data ; for (jj=0;jj<data2.size;jj++) { //for *q++ = *p++ ; }//for jj bset.format = data1.format ; bset.size = size ; bset.data = top ; } //allocated //free( data1.data ) ; //free( data2.data ) ; return(bset) ;}//cat_binarysetstruct binaryset extract_binaryset(struct binaryset datain, int offset, int size ) {struct binaryset bset ;char *top , *tmp1, *tmp2 ;int ii ;//extract_binaryset does not change "format" if ((offset+size) > datain.size ) { //error bset.size = 0 ; bset.data = NULL ; } //error else { //ok if ((top = (char *)malloc(size*sizeof(char)) ) == NULL) { PRINTF( " malloc failed in extract_binaryset\n") ; } //fail else { //allocated bset.size = size ; bset.format = datain.format ; bset.data = top ; tmp1 = top ; tmp2 = &datain.data[offset] ; for (ii=0;ii<size;ii++) { //for *tmp1++ = *tmp2++ ; }//for } //allocated } //ok printf("extract_binaryset offset=%d size=%d\n", offset, bset.size) ; return(bset) ;}//extract_binarysetchar compare_binaryset(struct binaryset data1, struct binaryset data2 ) { char *p, *q ;int ii ;char fail = 1 ; //1: Failed 0: passedint number_fail = 0 ; PRINTF("compare_binaryset format=%d size=%d\n", data1.format, data1.size ) ; if ((data1.format==data2.format) && (data1.size==data2.size)) { //compare p = data1.data ; q = data2.data ; for( ii=0;ii<data1.size;ii++) {//for if (*p != *q) { //fail number_fail++ ; PRINTF("mismatched(%d) @%d data1=%x data2=%x\n", number_fail, ii, *p, *q ) ; } //fail *q++ ; *p++ ; } //for if( number_fail==0) { //pass fail= 0 ; PRINTF(" No comparison Error found\n") ; } //pass } //compare return ( fail ) ;}//compare_binaryset
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -