Subversion Repositories wimsdev

Rev

Rev 8147 | 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.  
  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.         /* Points to the end of the word */
  42. char *find_word_end(char *p)
  43. {
  44.     int i;
  45.     for(i=0;!isspace(*p) && *p!=0 && i<MAX_LINELEN; p++,i++);
  46.     return p;
  47. }
  48.  
  49.         /* Strips leading spaces */
  50. char *find_word_start(char *p)
  51. {
  52.     int i;
  53.     for(i=0; isspace(*p) && i<MAX_LINELEN; p++,i++);
  54.     return p;
  55. }
  56.  
  57. int main(int argc, char *argv[])
  58. {
  59.     char *parm, *p1, *p2, c1, c2;
  60.     char pbuf[1024];
  61.     int i,j,k;
  62.     double dt;
  63.    
  64.     parm=getenv("wims_exec_parm");
  65.     if(parm==NULL || *parm==0) errorquit("no_parameter");
  66.     snprintf(pbuf,sizeof(pbuf),"%s",parm);
  67.     p1=find_word_start(pbuf); p2=find_word_end(p1);
  68.     if(*p2!=0) *p2++=0;
  69.     ch=atoi(p1); p1=find_word_start(p2);
  70.     if(ch!=2 && ch!=3 && ch!=5 && ch!=7) errorquit("bad_characteristics");
  71.     p2=find_word_end(p1); if(*p2!=0) *p2++=0;
  72.     p2=find_word_start(p2); *find_word_end(p2)=0;
  73.     degree=strlen(p1);
  74.     if(degree!=strlen(p2)) errorquit("unequal_degrees");
  75.     if(degree>MAX_DEGREE) errorquit("degree_too_high");
  76.     dt=pow(ch,degree);
  77.     if(dt>=(double) MAX_LENGTH) errorquit("length_overflow");
  78.     length=dt-1-degree;
  79.     for(i=0;i<degree;i++) {
  80.         c1=*(p1+i); c2=*(p2+i);
  81.         if(!isdigit(c1) || c1>=ch+'0') errorquit("bad_polynomial");
  82.         if(!isdigit(c2) || c2>=ch+'0') errorquit("bad_chain");
  83.         poly[i]=ch-(c1-'0'); chain[i]=c2-'0';
  84.     }
  85.     for(i=0;i<length;i++) {
  86.         for(j=k=0;j<degree;k+=poly[j]*chain[j],j++);
  87.         k%=ch; printf("%d",k);
  88.         memmove(chain,chain+1,degree-1); chain[degree-1]=k;
  89.     }
  90.     return 0;
  91. }
  92.  
  93.