14e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi/* The guts of the Reed-Solomon encoder, meant to be #included
24e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi * into a function body with the following typedefs, macros and variables supplied
34e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi * according to the code parameters:
44e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi
54e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi * data_t - a typedef for the data symbol
64e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi * data_t data[] - array of NN-NROOTS-PAD and type data_t to be encoded
74e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi * data_t parity[] - an array of NROOTS and type data_t to be written with parity symbols
84e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi * NROOTS - the number of roots in the RS code generator polynomial,
94e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi *          which is the same as the number of parity symbols in a block.
104e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi            Integer variable or literal.
114e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi	    *
124e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi * NN - the total number of symbols in a RS block. Integer variable or literal.
134e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi * PAD - the number of pad symbols in a block. Integer variable or literal.
144e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi * ALPHA_TO - The address of an array of NN elements to convert Galois field
154e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi *            elements in index (log) form to polynomial form. Read only.
164e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi * INDEX_OF - The address of an array of NN elements to convert Galois field
174e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi *            elements in polynomial form to index (log) form. Read only.
184e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi * MODNN - a function to reduce its argument modulo NN. May be inline or a macro.
194e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi * GENPOLY - an array of NROOTS+1 elements containing the generator polynomial in index form
204e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi
214e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi * The memset() and memmove() functions are used. The appropriate header
224e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi * file declaring these functions (usually <string.h>) must be included by the calling
234e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi * program.
244e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi
254e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi * Copyright 2004, Phil Karn, KA9Q
264e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi * May be used under the terms of the GNU Lesser General Public License (LGPL)
274e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi */
284e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi
294e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi
304e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi#undef A0
314e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi#define A0 (NN) /* Special reserved value encoding zero in index form */
324e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi
334e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi{
344e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi  int i, j;
354e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi  data_t feedback;
364e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi
374e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi  memset(parity,0,NROOTS*sizeof(data_t));
384e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi
394e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi  for(i=0;i<NN-NROOTS-PAD;i++){
404e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi    feedback = INDEX_OF[data[i] ^ parity[0]];
414e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi    if(feedback != A0){      /* feedback term is non-zero */
424e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi#ifdef UNNORMALIZED
434e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi      /* This line is unnecessary when GENPOLY[NROOTS] is unity, as it must
444e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi       * always be for the polynomials constructed by init_rs()
454e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi       */
464e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi      feedback = MODNN(NN - GENPOLY[NROOTS] + feedback);
474e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi#endif
484e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi      for(j=1;j<NROOTS;j++)
494e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi	parity[j] ^= ALPHA_TO[MODNN(feedback + GENPOLY[NROOTS-j])];
504e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi    }
514e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi    /* Shift */
524e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi    memmove(&parity[0],&parity[1],sizeof(data_t)*(NROOTS-1));
534e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi    if(feedback != A0)
544e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi      parity[NROOTS-1] = ALPHA_TO[MODNN(feedback + GENPOLY[0])];
554e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi    else
564e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi      parity[NROOTS-1] = 0;
574e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi  }
584e213d510f437769f8a28578dd4f786fb7d16c4Bill Yi}
59