废话少说,上代码(可在linux上编译运行,字符数组里的数按照大端存放,main里采用6字节),最下边的__rang_reduce函数作为参考,它计算一个短整型范围的掩码块。
-
#include<stdio.h>
-
#include<stdlib.h>
-
#include<string.h>
-
int numlistcmp(unsigned char *num1,unsigned char *num2,int bytes)
-
{
-
int i,ret;
-
for(i=0;i<bytes;i++)
-
if(0 != (ret=num1[i]-num2[i]))
-
return ret;
-
-
return 0;
-
}
-
int numlistiszero(unsigned char *num,int bytes)
-
{
-
int i;
-
-
for(i=0;i<bytes;i++)
-
if(num[i]!=0)
-
return 0;
-
-
return 1;
-
}
-
void maskbitsf(unsigned char *mask,int bits)
-
{
-
int i,bytes;
-
-
bytes=(bits+7)/8;
-
mask[0]=0xff>>(bytes*8-bits);
-
-
for(i=1;i<bytes;i++)
-
mask[i]=0xff;
-
}
-
void numlistshr(unsigned char *num,int bytes,int shbits)
-
{//移位运算符为算术右移,最高位为1时不易处理,故用unsigned char
-
int i,shbyte,shbit;
-
-
shbyte = shbits/8;
-
shbit = shbits%8;
-
-
for(i=bytes-1;i>0;i--)
-
{
-
num[i] >>= shbit;
-
num[i] |= num[i-1] << 8-shbit;
-
}
-
num[i] >>= shbit;
-
-
if(shbyte>0)
-
for(i=bytes-1-shbyte;i>0;i--)
-
num[i+shbyte]=num[i];
-
-
for(i=0;i<shbyte;i++)
-
num[i]=0;
-
}
-
void numlistshl(unsigned char *num,int bits,int shbits)
-
{
-
int i,bytes,shbyte,shbit;
-
-
bytes=(bits+7)/8;
-
shbyte = shbits/8;
-
shbit = shbits%8;
-
-
for(i=0;i<bytes-1;i++)
-
{
-
num[i] <<= shbit;
-
num[i] |= num[i+1] >> 8-shbit;
-
}
-
num[i] <<= shbit;
-
-
if(shbyte>0)
-
for(i=shbyte;i<bytes;i++)
-
num[i-shbyte] = num[i];
-
-
for(i=bytes-shbyte;i<bytes;i++)
-
num[i] = 0;
-
-
num[0] &= 0xff>>(bytes*8-bits);
-
}
-
void numlistadd1(unsigned char *num,int bits)
-
{
-
int i,bytes;
-
-
bytes=(bits+7)/8;
-
for(i=bytes-1;i>-1&&(num[i]+=1)==0;i--)
-
;
-
if(i==0)
-
num[0] &= 0xff>>(bytes*8-bits);
-
}
-
void numlistnor(unsigned char * num,int bits)
-
{
-
int i,bytes;
-
-
bytes = (bits+7)/8;
-
-
for(i=0;i<bytes;i++)
-
num[i] = ~num[i];
-
-
num[0] &= 0xff>>(bytes*8-bits);
-
}
-
void numlistand(unsigned char * num1,unsigned char *num2,int bytes)
-
{
-
int i;
-
-
for(i=0;i<bytes;i++)
-
num1[i] = num1[i]&num2[i];
-
}
-
void numlistor(unsigned char * num1,unsigned char *num2,int bytes)
-
{
-
int i;
-
-
for(i=0;i<bytes;i++)
-
num1[i] = num1[i]|num2[i];
-
}
-
int _range_reduce(char *mi, char *max, char* (*list)[2],int mbits)
-
{
-
int nentry=0;
-
int bytes=(mbits+sizeof(char)-1)/sizeof(char);
-
char *temp=NULL,*mask=NULL,*temp1=NULL,*min=NULL;
-
-
if(NULL==(temp=malloc(bytes))
-
goto export;
-
if(NULL==(temp1=malloc(bytes))
-
goto export;
-
if(NULL==(mask=malloc(bytes))
-
goto export;
-
if(NULL=(min=malloc(bytes))
-
goto export;
-
-
memcpy(min,mi,bytes);
-
//printf("max:%02x:%02x:%02x:%02x:%02x:%02x\n",max[0]&0xff,max[1]&0xff,max[2]&0xff,max[3]&0xff,max[4]&0xff,max[5]&0xff);
-
-
while (numlistcmp(min,max,bytes)<=0)
-
{/* count low order 0 bits in min */
-
//printf("1\n");
-
if (numlistiszero(min,bytes) != 0)
-
{
-
maskbitsf(mask,mbits);
-
}
-
else
-
{
-
for (memcpy(temp,min,bytes), memset(mask,0,bytes); (temp[bytes-1] & 1) == 0; numlistshr(temp,bytes,1))
-
{
-
//printf("2\n");
-
numlistshl(mask,mbits,1);
-
mask[bytes-1] |= 1;
-
}
-
//printf("temp:%02x:%02x:%02x:%02x:%02x:%02x\n",temp[0]&0xff,temp[1]&0xff,temp[2]&0xff,temp[3]&0xff,temp[4]&0xff,temp[5]&0xff);
-
}
-
//printf("temp:%02x:%02x:%02x:%02x:%02x:%02x\n",temp[0]&0xff,temp[1]&0xff,temp[2]&0xff,temp[3]&0xff,temp[4]&0xff,temp[5]&0xff);
-
//printf("mask:%02x:%02x:%02x:%02x:%02x:%02x\n",mask[0]&0xff,mask[1]&0xff,mask[2]&0xff,mask[3]&0xff,mask[4]&0xff,mask[5]&0xff);
-
memcpy(temp1,mask,bytes);
-
numlistnor(temp1,mbits);
-
numlistand(temp1,min,bytes);
-
numlistor(temp1,mask,bytes);
-
memcpy(temp,temp1,bytes);
-
-
if (numlistcmp(temp,max,bytes)<=0)
-
{
-
;
-
}
-
else
-
{
-
//printf("temp:%02x:%02x:%02x:%02x:%02x:%02x\n",temp[0]&0xff,temp[1]&0xff,temp[2]&0xff,temp[3]&0xff,temp[4]&0xff,temp[5]&0xff);
-
while (numlistcmp(temp,max,bytes)>0)
-
{
-
//printf("3\n");
-
//printf("temp:%02x:%02x:%02x:%02x:%02x:%02x\n",temp[0]&0xff,temp[1]&0xff,temp[2]&0xff,temp[3]&0xff,temp[4]&0xff,temp[5]&0xff);
-
//printf("mask:%02x:%02x:%02x:%02x:%02x:%02x\n",mask[0]&0xff,mask[1]&0xff,mask[2]&0xff,mask[3]&0xff,mask[4]&0xff,mask[5]&0xff);
-
numlistshr(mask,bytes,1);
-
//printf("mask:%02x:%02x:%02x:%02x:%02x:%02x\n",mask[0]&0xff,mask[1]&0xff,mask[2]&0xff,mask[3]&0xff,mask[4]&0xff,mask[5]&0xff);
-
memcpy(temp1,mask,bytes);
-
numlistnor(temp1,mbits);
-
numlistand(temp1,min,bytes);
-
numlistor(temp1,mask,bytes);
-
memcpy(temp,temp1,bytes);
-
}
-
}
-
-
if (list)
-
{
-
memcpy(list[nentry][0],min,bytes);
-
memcpy(temp1,mask,bytes);
-
numlistnor(temp1,mbits);
-
memcpy(list[nentry][1],temp1,bytes);
-
}
-
-
nentry += 1;
-
if(numlistcmp(temp,max,bytes)==0)
-
{
-
break;
-
}
-
-
memcpy(temp1,temp,bytes);
-
numlistadd1(temp1,mbits);
-
memcpy(min,temp1,bytes);
-
//printf("temp:%02x:%02x:%02x:%02x:%02x:%02x\n",temp[0]&0xff,temp[1]&0xff,temp[2]&0xff,temp[3]&0xff,temp[4]&0xff,temp[5]&0xff);
-
//printf("min:%02x:%02x:%02x:%02x:%02x:%02x\n",min[0]&0xff,min[1]&0xff,min[2]&0xff,min[3]&0xff,min[4]&0xff,min[5]&0xff);
-
}
-
export:
-
if(temp)
-
free(temp);
-
if(temp1)
-
free(temp1);
-
if(mask)
-
free(mask);
-
if(min)
-
free(min);
-
-
return nentry;
-
}
-
int main(int argc,char *argv[])
-
{
-
char macmin[6]={0x00,0x11,0x22,0x33,0x44,0x55};
-
char macmax[6]={0x00,0x11,0x22,0x33,0x66,0x55};
-
char *(*list)[2]=NULL;
-
int temp,i,totol;
-
int mbits=0;
-
-
for(i=0;i<6;i++)
-
{
-
scanf("%x",&temp);
-
macmin[i]=temp;
-
}
-
for(i=0;i<6;i++)
-
{
-
scanf("%x",&temp);
-
macmax[i]=temp;
-
}
-
scanf("%d",&mbits);
-
totol = _range_reduce(macmin, macmax,list,mbits);
-
printf("%d\n",totol);
-
list =(char *(*)[2])calloc(totol,sizeof(char *[2]));
-
for(i=0;i<totol;i++)
-
{
-
list[i][0]=(char *)calloc(6,1);
-
list[i][1]=(char *)calloc(6,1);
-
}
-
printf("%d\n",i);
-
_range_reduce(macmin, macmax,list,mbits);
-
for(i=0;i<totol;i++)
-
{
-
printf("%02x:%02x:%02x:%02x:%02x:%02x\t",list[i][0][0]&0xff,list[i][0][1]&0xff,list[i][0][2]&0xff,list[i][0][3]&0xff,list[i][0][4]&0xff,list[i][0][5]&0xff);
-
printf("%02x:%02x:%02x:%02x:%02x:%02x\n",list[i][1][0]&0xff,list[i][1][1]&0xff,list[i][1][2]&0xff,list[i][1][3]&0xff,list[i][1][4]&0xff,list[i][1][5]&0xff);
-
}
-
for(i=0;i<totol;i++)
-
{
-
free(list[i][0]);
-
free(list[i][1]);
-
}
-
free(list);
-
}
-
int __range_reduce(unsigned short min, unsigned short max, unsigned short (*list)[2])
-
{
-
int nentry;
-
unsigned short temp, mask;
-
nentry = 0;
-
-
while (min <= max)
-
{/* count low order 0 bits in min */
-
if (min == 0)
-
mask = 0xffff;
-
else
-
for (temp = min, mask = 0; (temp & 1) == 0; temp >>= 1)
-
mask = (mask << 1) | 1;
-
-
temp = (min & ~mask) | mask;
-
-
if (temp <= max)
-
;
-
else
-
while (temp > max)
-
{
-
mask >>= 1;
-
temp = (min & ~mask) | mask;
-
}
-
-
if (list)
-
{
-
list[nentry][0] = min;
-
list[nentry][1] = ~mask & 0xffff;
-
}
-
nentry += 1;
-
if (temp >= max)
-
break;
-
min = temp + 1;
-
}
-
return nentry;
-
}
阅读(2137) | 评论(0) | 转发(0) |