Subversion Repositories wimsdev

Rev

Rev 8185 | 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 "../Lib/libwims.h"
  29.  
  30. #define MAX_DEGREE 256
  31. #define MAX_LENGTH 1024000
  32.  
  33. int ch,degree,length;
  34. char poly[MAX_DEGREE], chain[MAX_DEGREE];
  35.  
  36. void errorquit(char *msg)
  37. {
  38.   fprintf(stderr,"%s\n",msg); exit(1);
  39. }
  40.  
  41. int main(int argc, char *argv[])
  42. {
  43.   char *parm, *p1, *p2, c1, c2;
  44.   char pbuf[1024];
  45.   int i,j,k;
  46.   double dt;
  47.  
  48.   parm=getenv("wims_exec_parm");
  49.   if(parm==NULL || *parm==0) errorquit("no_parameter");
  50.   snprintf(pbuf,sizeof(pbuf),"%s",parm);
  51.   p1=find_word_start(pbuf); p2=find_word_end(p1);
  52.   if(*p2!=0) *p2++=0;
  53.   ch=atoi(p1); p1=find_word_start(p2);
  54.   if(ch!=2 && ch!=3 && ch!=5 && ch!=7) errorquit("bad_characteristics");
  55.   p2=find_word_end(p1); if(*p2!=0) *p2++=0;
  56.   p2=find_word_start(p2); *find_word_end(p2)=0;
  57.   degree=strlen(p1);
  58.   if(degree!=strlen(p2)) errorquit("unequal_degrees");
  59.   if(degree>MAX_DEGREE) errorquit("degree_too_high");
  60.   dt=pow(ch,degree);
  61.   if(dt>=(double) MAX_LENGTH) errorquit("length_overflow");
  62.   length=dt-1-degree;
  63.   for(i=0;i<degree;i++) {
  64.     c1=*(p1+i); c2=*(p2+i);
  65.     if(!isdigit(c1) || c1>=ch+'0') errorquit("bad_polynomial");
  66.     if(!isdigit(c2) || c2>=ch+'0') errorquit("bad_chain");
  67.     poly[i]=ch-(c1-'0'); chain[i]=c2-'0';
  68.   }
  69.   for(i=0;i<length;i++) {
  70.     for(j=k=0;j<degree;k+=poly[j]*chain[j],j++);
  71.     k%=ch; printf("%d",k);
  72.     memmove(chain,chain+1,degree-1); chain[degree-1]=k;
  73.   }
  74.   return 0;
  75. }
  76.