Rev 10 | Rev 8147 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 10 | Rev 7676 | ||
---|---|---|---|
Line 16... | Line 16... | ||
16 | */ |
16 | */ |
17 | 17 | ||
18 | /* This program generates cyclic code from a polynomial |
18 | /* This program generates cyclic code from a polynomial |
19 | * defined over a prime field. |
19 | * defined over a prime field. |
20 | * It does not check whether the polynomial is primitive or irreducible. |
20 | * It does not check whether the polynomial is primitive or irreducible. |
21 | * Accepted parameter: 3 words. |
21 | * Accepted parameter: 3 words. |
22 | * Word 1: field characteristics, limited to 2,3,5,7. |
22 | * Word 1: field characteristics, limited to 2,3,5,7. |
23 | * Word 2: The polynomial coefficients (except the leading one, from |
23 | * Word 2: The polynomial coefficients (except the leading one, from |
24 |
|
24 | lower degree to higher) |
25 | * Word 3: The starting status (starting from the first bit). |
25 | * Word 3: The starting status (starting from the first bit). |
26 | */ |
26 | */ |
27 | 27 | ||
28 | #include "../wims.h" |
28 | #include "../wims.h" |
29 | 29 | ||
Line 36... | Line 36... | ||
36 | void errorquit(char *msg) |
36 | void errorquit(char *msg) |
37 | { |
37 | { |
38 | fprintf(stderr,"%s\n",msg); exit(1); |
38 | fprintf(stderr,"%s\n",msg); exit(1); |
39 | } |
39 | } |
40 | 40 | ||
41 |
|
41 | /* Points to the end of the word */ |
42 | char *find_word_end(char *p) |
42 | char *find_word_end(char *p) |
43 | { |
43 | { |
44 | int i; |
44 | int i; |
45 | for(i=0;!isspace(*p) && *p!=0 && i<MAX_LINELEN; p++,i++); |
45 | for(i=0;!isspace(*p) && *p!=0 && i<MAX_LINELEN; p++,i++); |
46 | return p; |
46 | return p; |
47 | } |
47 | } |
48 | 48 | ||
49 |
|
49 | /* Strips leading spaces */ |
50 | char *find_word_start(char *p) |
50 | char *find_word_start(char *p) |
51 | { |
51 | { |
52 | int i; |
52 | int i; |
53 | for(i=0; isspace(*p) && i<MAX_LINELEN; p++,i++); |
53 | for(i=0; isspace(*p) && i<MAX_LINELEN; p++,i++); |
54 | return p; |
54 | return p; |
Line 58... | Line 58... | ||
58 | { |
58 | { |
59 | char *parm, *p1, *p2, c1, c2; |
59 | char *parm, *p1, *p2, c1, c2; |
60 | char pbuf[1024]; |
60 | char pbuf[1024]; |
61 | int i,j,k; |
61 | int i,j,k; |
62 | double dt; |
62 | double dt; |
63 | 63 | ||
64 | parm=getenv("wims_exec_parm"); |
64 | parm=getenv("wims_exec_parm"); |
65 | if(parm==NULL || *parm==0) errorquit("no_parameter"); |
65 | if(parm==NULL || *parm==0) errorquit("no_parameter"); |
66 | snprintf(pbuf,sizeof(pbuf),"%s",parm); |
66 | snprintf(pbuf,sizeof(pbuf),"%s",parm); |
67 | p1=find_word_start(pbuf); p2=find_word_end(p1); |
67 | p1=find_word_start(pbuf); p2=find_word_end(p1); |
68 | if(*p2!=0) *p2++=0; |
68 | if(*p2!=0) *p2++=0; |
Line 75... | Line 75... | ||
75 | if(degree>MAX_DEGREE) errorquit("degree_too_high"); |
75 | if(degree>MAX_DEGREE) errorquit("degree_too_high"); |
76 | dt=pow(ch,degree); |
76 | dt=pow(ch,degree); |
77 | if(dt>=(double) MAX_LENGTH) errorquit("length_overflow"); |
77 | if(dt>=(double) MAX_LENGTH) errorquit("length_overflow"); |
78 | length=dt-1-degree; |
78 | length=dt-1-degree; |
79 | for(i=0;i<degree;i++) { |
79 | for(i=0;i<degree;i++) { |
80 |
|
80 | c1=*(p1+i); c2=*(p2+i); |
81 |
|
81 | if(!isdigit(c1) || c1>=ch+'0') errorquit("bad_polynomial"); |
82 |
|
82 | if(!isdigit(c2) || c2>=ch+'0') errorquit("bad_chain"); |
83 |
|
83 | poly[i]=ch-(c1-'0'); chain[i]=c2-'0'; |
84 | } |
84 | } |
85 | for(i=0;i<length;i++) { |
85 | for(i=0;i<length;i++) { |
86 |
|
86 | for(j=k=0;j<degree;k+=poly[j]*chain[j],j++); |
87 |
|
87 | k%=ch; printf("%d",k); |
88 |
|
88 | memmove(chain,chain+1,degree-1); chain[degree-1]=k; |
89 | } |
89 | } |
90 | return 0; |
90 | return 0; |
91 | } |
91 | } |
92 | - |