Back to index

wims  3.65+svn20090927
cyclicode.c
Go to the documentation of this file.
00001 /*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
00002  *
00003  *  This program is free software; you can redistribute it and/or modify
00004  *  it under the terms of the GNU General Public License as published by
00005  *  the Free Software Foundation; either version 2 of the License, or
00006  *  (at your option) any later version.
00007  *
00008  *  This program is distributed in the hope that it will be useful,
00009  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  *  GNU General Public License for more details.
00012  *
00013  *  You should have received a copy of the GNU General Public License
00014  *  along with this program; if not, write to the Free Software
00015  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00016  */
00017 
00018  /* This program generates cyclic code from a polynomial
00019   * defined over a prime field.
00020   * It does not check whether the polynomial is primitive or irreducible.
00021   * Accepted parameter: 3 words. 
00022   * Word 1: field characteristics, limited to 2,3,5,7.
00023   * Word 2: The polynomial coefficients (except the leading one, from
00024                                     lower degree to higher)
00025   * Word 3: The starting status (starting from the first bit).
00026   */
00027 
00028 #include "../wims.h"
00029 
00030 #define MAX_DEGREE 256
00031 #define MAX_LENGTH 1024000
00032 
00033 int ch,degree,length;
00034 char poly[MAX_DEGREE], chain[MAX_DEGREE];
00035 
00036 void errorquit(char *msg)
00037 {
00038     fprintf(stderr,"%s\n",msg); exit(1);
00039 }
00040 
00041        /* Points to the end of the word */
00042 char *find_word_end(char *p)
00043 {
00044     int i;
00045     for(i=0;!isspace(*p) && *p!=0 && i<MAX_LINELEN; p++,i++);
00046     return p;
00047 }
00048 
00049        /* Strips leading spaces */
00050 char *find_word_start(char *p)
00051 {
00052     int i;
00053     for(i=0; isspace(*p) && i<MAX_LINELEN; p++,i++);
00054     return p;
00055 }
00056 
00057 int main(int argc, char *argv[])
00058 {
00059     char *parm, *p1, *p2, c1, c2;
00060     char pbuf[1024];
00061     int i,j,k;
00062     double dt;
00063     
00064     parm=getenv("wims_exec_parm");
00065     if(parm==NULL || *parm==0) errorquit("no_parameter");
00066     snprintf(pbuf,sizeof(pbuf),"%s",parm);
00067     p1=find_word_start(pbuf); p2=find_word_end(p1);
00068     if(*p2!=0) *p2++=0;
00069     ch=atoi(p1); p1=find_word_start(p2);
00070     if(ch!=2 && ch!=3 && ch!=5 && ch!=7) errorquit("bad_characteristics");
00071     p2=find_word_end(p1); if(*p2!=0) *p2++=0;
00072     p2=find_word_start(p2); *find_word_end(p2)=0;
00073     degree=strlen(p1);
00074     if(degree!=strlen(p2)) errorquit("unequal_degrees");
00075     if(degree>MAX_DEGREE) errorquit("degree_too_high");
00076     dt=pow(ch,degree);
00077     if(dt>=(double) MAX_LENGTH) errorquit("length_overflow");
00078     length=dt-1-degree;
00079     for(i=0;i<degree;i++) {
00080        c1=*(p1+i); c2=*(p2+i);
00081        if(!isdigit(c1) || c1>=ch+'0') errorquit("bad_polynomial");
00082        if(!isdigit(c2) || c2>=ch+'0') errorquit("bad_chain");
00083        poly[i]=ch-(c1-'0'); chain[i]=c2-'0';
00084     }
00085     for(i=0;i<length;i++) {
00086        for(j=k=0;j<degree;k+=poly[j]*chain[j],j++);
00087        k%=ch; printf("%d",k);
00088        memmove(chain,chain+1,degree-1); chain[degree-1]=k;
00089     }
00090     return 0;
00091 }
00092