Subversion Repositories wimsdev

Rev

Rev 8147 | Rev 8185 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
  2.  *
  3.  *  This program is free software; you can redistribute it and/or modify
  4.  *  it under the terms of the GNU General Public License as published by
  5.  *  the Free Software Foundation; either version 2 of the License, or
  6.  *  (at your option) any later version.
  7.  *
  8.  *  This program is distributed in the hope that it will be useful,
  9.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.  *  GNU General Public License for more details.
  12.  *
  13.  *  You should have received a copy of the GNU General Public License
  14.  *  along with this program; if not, write to the Free Software
  15.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16.  */
  17.  
  18.  /* This program generates cyclic code from a polynomial
  19.   * defined over a prime field.
  20.   * It does not check whether the polynomial is primitive or irreducible.
  21.   * Accepted parameter: 3 words.
  22.   * Word 1: field characteristics, limited to 2,3,5,7.
  23.   * Word 2: The polynomial coefficients (except the leading one, from
  24.      lower degree to higher)
  25.   * Word 3: The starting status (starting from the first bit).
  26.   */
  27.  
  28. #include "../wims.h"
  29. #include "../Lib/libwims.h"
  30.  
  31. #define MAX_DEGREE 256
  32. #define MAX_LENGTH 1024000
  33.  
  34. int ch,degree,length;
  35. char poly[MAX_DEGREE], chain[MAX_DEGREE];
  36.  
  37. void errorquit(char *msg)
  38. {
  39.     fprintf(stderr,"%s\n",msg); exit(1);
  40. }
  41.  
  42. int main(int argc, char *argv[])
  43. {
  44.     char *parm, *p1, *p2, c1, c2;
  45.     char pbuf[1024];
  46.     int i,j,k;
  47.     double dt;
  48.  
  49.     parm=getenv("wims_exec_parm");
  50.     if(parm==NULL || *parm==0) errorquit("no_parameter");
  51.     snprintf(pbuf,sizeof(pbuf),"%s",parm);
  52.     p1=find_word_start(pbuf); p2=find_word_end(p1);
  53.     if(*p2!=0) *p2++=0;
  54.     ch=atoi(p1); p1=find_word_start(p2);
  55.     if(ch!=2 && ch!=3 && ch!=5 && ch!=7) errorquit("bad_characteristics");
  56.     p2=find_word_end(p1); if(*p2!=0) *p2++=0;
  57.     p2=find_word_start(p2); *find_word_end(p2)=0;
  58.     degree=strlen(p1);
  59.     if(degree!=strlen(p2)) errorquit("unequal_degrees");
  60.     if(degree>MAX_DEGREE) errorquit("degree_too_high");
  61.     dt=pow(ch,degree);
  62.     if(dt>=(double) MAX_LENGTH) errorquit("length_overflow");
  63.     length=dt-1-degree;
  64.     for(i=0;i<degree;i++) {
  65.       c1=*(p1+i); c2=*(p2+i);
  66.       if(!isdigit(c1) || c1>=ch+'0') errorquit("bad_polynomial");
  67.       if(!isdigit(c2) || c2>=ch+'0') errorquit("bad_chain");
  68.       poly[i]=ch-(c1-'0'); chain[i]=c2-'0';
  69.     }
  70.     for(i=0;i<length;i++) {
  71.       for(j=k=0;j<degree;k+=poly[j]*chain[j],j++);
  72.       k%=ch; printf("%d",k);
  73.       memmove(chain,chain+1,degree-1); chain[degree-1]=k;
  74.     }
  75.     return 0;
  76. }
  77.