Chinaunix首页 | 论坛 | 博客
  • 博客访问: 158107
  • 博文数量: 35
  • 博客积分: 45
  • 博客等级: 民兵
  • 技术积分: 180
  • 用 户 组: 普通用户
  • 注册时间: 2013-01-10 11:16
文章分类

全部博文(35)

文章存档

2016年(12)

2015年(10)

2014年(4)

2013年(9)

我的朋友

分类: C/C++

2013-04-07 14:33:09

废话少说,上代码(可在linux上编译运行,字符数组里的数按照大端存放,main里采用6字节),最下边的__rang_reduce函数作为参考,它计算一个短整型范围的掩码块。


点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. int numlistcmp(unsigned char *num1,unsigned char *num2,int bytes)
  5. {
  6.     int i,ret;
  7.     for(i=0;i<bytes;i++)
  8.         if(0 != (ret=num1[i]-num2[i]))
  9.             return ret;

  10.     return 0;
  11. }
  12. int numlistiszero(unsigned char *num,int bytes)
  13. {
  14.     int i;
  15.  
  16.     for(i=0;i<bytes;i++)
  17.         if(num[i]!=0)
  18.             return 0;

  19.     return 1;
  20. }
  21. void maskbitsf(unsigned char *mask,int bits)
  22. {
  23.     int i,bytes;

  24.     bytes=(bits+7)/8;
  25.     mask[0]=0xff>>(bytes*8-bits);

  26.     for(i=1;i<bytes;i++)
  27.         mask[i]=0xff;
  28. }
  29. void numlistshr(unsigned char *num,int bytes,int shbits)
  30. {//移位运算符为算术右移,最高位为1时不易处理,故用unsigned char
  31.     int i,shbyte,shbit;

  32.     shbyte = shbits/8;
  33.     shbit = shbits%8;

  34.     for(i=bytes-1;i>0;i--)
  35.     {
  36.         num[i] >>= shbit;
  37.         num[i] |= num[i-1] << 8-shbit;
  38.     }
  39.     num[i] >>= shbit;

  40.     if(shbyte>0)
  41.         for(i=bytes-1-shbyte;i>0;i--)
  42.             num[i+shbyte]=num[i];
  43.  
  44.     for(i=0;i<shbyte;i++)
  45.         num[i]=0;
  46. }
  47. void numlistshl(unsigned char *num,int bits,int shbits)
  48. {
  49.     int i,bytes,shbyte,shbit;

  50.     bytes=(bits+7)/8;
  51.     shbyte = shbits/8;
  52.     shbit = shbits%8;

  53.     for(i=0;i<bytes-1;i++)
  54.     {
  55.         num[i] <<= shbit;
  56.         num[i] |= num[i+1] >> 8-shbit;
  57.     }
  58.     num[i] <<= shbit;

  59.     if(shbyte>0)
  60.         for(i=shbyte;i<bytes;i++)
  61.             num[i-shbyte] = num[i];
  62.  
  63.     for(i=bytes-shbyte;i<bytes;i++)
  64.         num[i] = 0;
  65.  
  66.     num[0] &= 0xff>>(bytes*8-bits);
  67. }
  68. void numlistadd1(unsigned char *num,int bits)
  69. {
  70.     int i,bytes;

  71.     bytes=(bits+7)/8;
  72.         for(i=bytes-1;i>-1&&(num[i]+=1)==0;i--)
  73.             ;
  74.     if(i==0)
  75.         num[0] &= 0xff>>(bytes*8-bits);
  76. }
  77. void numlistnor(unsigned char * num,int bits)
  78. {
  79.     int i,bytes;

  80.     bytes = (bits+7)/8;

  81.     for(i=0;i<bytes;i++)
  82.         num[i] = ~num[i];

  83.     num[0] &= 0xff>>(bytes*8-bits);
  84. }
  85. void numlistand(unsigned char * num1,unsigned char *num2,int bytes)
  86. {
  87.     int i;

  88.     for(i=0;i<bytes;i++)
  89.         num1[i] = num1[i]&num2[i];
  90. }
  91. void numlistor(unsigned char * num1,unsigned char *num2,int bytes)
  92. {
  93.     int i;

  94.     for(i=0;i<bytes;i++)
  95.         num1[i] = num1[i]|num2[i];
  96. }
  97. int _range_reduce(char *mi, char *max, char* (*list)[2],int mbits)
  98. {
  99.     int nentry=0;
  100.     int bytes=(mbits+sizeof(char)-1)/sizeof(char);
  101.     char *temp=NULL,*mask=NULL,*temp1=NULL,*min=NULL;

  102.     if(NULL==(temp=malloc(bytes))
  103.         goto export;
  104.     if(NULL==(temp1=malloc(bytes))
  105.         goto export;
  106.     if(NULL==(mask=malloc(bytes))
  107.         goto export;
  108.     if(NULL=(min=malloc(bytes))
  109.         goto export;

  110.     memcpy(min,mi,bytes);
  111.     //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);

  112.     while (numlistcmp(min,max,bytes)<=0)
  113.     {/* count low order 0 bits in min */
  114.         //printf("1\n");
  115.         if (numlistiszero(min,bytes) != 0)
  116.         {
  117.             maskbitsf(mask,mbits);
  118.         }
  119.         else
  120.         {
  121.             for (memcpy(temp,min,bytes), memset(mask,0,bytes); (temp[bytes-1] & 1) == 0; numlistshr(temp,bytes,1))
  122.             {
  123.                 //printf("2\n");
  124.                 numlistshl(mask,mbits,1);
  125.                 mask[bytes-1] |= 1;
  126.             }
  127.             //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);
  128.         }
  129.         //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);
  130.         //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);
  131.         memcpy(temp1,mask,bytes);
  132.         numlistnor(temp1,mbits);
  133.         numlistand(temp1,min,bytes);
  134.         numlistor(temp1,mask,bytes);
  135.         memcpy(temp,temp1,bytes);
  136.   
  137.         if (numlistcmp(temp,max,bytes)<=0)
  138.         {
  139.             ;
  140.         }
  141.         else
  142.         {
  143.             //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);
  144.             while (numlistcmp(temp,max,bytes)>0)
  145.             {
  146.                 //printf("3\n");
  147.                 //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);
  148.                 //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);
  149.                 numlistshr(mask,bytes,1);
  150.                 //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);
  151.                 memcpy(temp1,mask,bytes);
  152.                 numlistnor(temp1,mbits);
  153.                 numlistand(temp1,min,bytes);
  154.                 numlistor(temp1,mask,bytes);
  155.                 memcpy(temp,temp1,bytes);
  156.             }
  157.         }
  158.   
  159.         if (list)
  160.         {
  161.             memcpy(list[nentry][0],min,bytes);
  162.             memcpy(temp1,mask,bytes);
  163.             numlistnor(temp1,mbits);
  164.             memcpy(list[nentry][1],temp1,bytes);
  165.         }
  166.   
  167.         nentry += 1;
  168.         if(numlistcmp(temp,max,bytes)==0)
  169.         {
  170.             break;
  171.         }
  172.  
  173.         memcpy(temp1,temp,bytes);
  174.         numlistadd1(temp1,mbits);
  175.         memcpy(min,temp1,bytes);
  176.         //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);
  177.         //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);
  178.     }
  179. export:
  180.     if(temp)
  181.         free(temp);
  182.     if(temp1)
  183.         free(temp1);
  184.     if(mask)
  185.         free(mask);
  186.     if(min)
  187.         free(min);

  188.     return nentry;
  189. }
  190. int main(int argc,char *argv[])
  191. {
  192.     char macmin[6]={0x00,0x11,0x22,0x33,0x44,0x55};
  193.     char macmax[6]={0x00,0x11,0x22,0x33,0x66,0x55};
  194.     char *(*list)[2]=NULL;
  195.     int temp,i,totol;
  196.     int mbits=0;

  197.     for(i=0;i<6;i++)
  198.     {
  199.         scanf("%x",&temp);
  200.         macmin[i]=temp;
  201.     }
  202.     for(i=0;i<6;i++)
  203.     {
  204.         scanf("%x",&temp);
  205.         macmax[i]=temp;
  206.     }
  207.     scanf("%d",&mbits);
  208.     totol = _range_reduce(macmin, macmax,list,mbits);
  209.     printf("%d\n",totol);
  210.     list =(char *(*)[2])calloc(totol,sizeof(char *[2]));
  211.     for(i=0;i<totol;i++)
  212.     {
  213.         list[i][0]=(char *)calloc(6,1);
  214.         list[i][1]=(char *)calloc(6,1);
  215.     }
  216.     printf("%d\n",i);
  217.     _range_reduce(macmin, macmax,list,mbits);
  218.     for(i=0;i<totol;i++)
  219.     {
  220.         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);
  221.         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);
  222.     }
  223.     for(i=0;i<totol;i++)
  224.     {
  225.         free(list[i][0]);
  226.         free(list[i][1]);
  227.     }
  228.     free(list);
  229. }
  230. int __range_reduce(unsigned short min, unsigned short max, unsigned short (*list)[2])
  231. {
  232.     int nentry;
  233.     unsigned short temp, mask;
  234.     nentry = 0;

  235.     while (min <= max)
  236.     {/* count low order 0 bits in min */
  237.         if (min == 0)
  238.             mask = 0xffff;
  239.         else
  240.             for (temp = min, mask = 0; (temp & 1) == 0; temp >>= 1)
  241.                 mask = (mask << 1) | 1;

  242.         temp = (min & ~mask) | mask;

  243.         if (temp <= max)
  244.             ;
  245.         else
  246.             while (temp > max)
  247.             {
  248.                 mask >>= 1;
  249.                 temp = (min & ~mask) | mask;
  250.             }

  251.         if (list)
  252.         {
  253.             list[nentry][0] = min;
  254.             list[nentry][1] = ~mask & 0xffff;
  255.         }
  256.         nentry += 1;
  257.         if (temp >= max)
  258.             break;
  259.         min = temp + 1;
  260.     }
  261.     return nentry;
  262. }


阅读(2144) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:FVWM新手入门不完全手册

给主人留下些什么吧!~~