~ubuntu-branches/ubuntu/gutsy/wireshark/gutsy-security

« back to all changes in this revision

Viewing changes to epan/reedsolomon.c

  • Committer: Bazaar Package Importer
  • Author(s): Frederic Peters
  • Date: 2007-04-01 08:58:40 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070401085840-or3qhrpv8alt1bwg
Tags: 0.99.5-1
* New upstream release.
* debian/patches/09_idl2wrs.dpatch: updated to patch idl2wrs.sh.in.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Reed-Solomon coding and decoding
 
3
 * Phil Karn (karn@ka9q.ampr.org) September 1996
 
4
 * Separate CCSDS version create Dec 1998, merged into this version May 1999
 
5
 * 
 
6
 * This file is derived from my generic RS encoder/decoder, which is
 
7
 * in turn based on the program "new_rs_erasures.c" by Robert
 
8
 * Morelos-Zaragoza (robert@spectra.eng.hawaii.edu) and Hari Thirumoorthy
 
9
 * (harit@spectra.eng.hawaii.edu), Aug 1995
 
10
 
 
11
 * Copyright 1999 Phil Karn, KA9Q
 
12
 * May be used under the terms of the GNU public license
 
13
 */
 
14
#include <stdio.h>
 
15
#include "reedsolomon.h"
 
16
 
 
17
#ifdef CCSDS
 
18
/* CCSDS field generator polynomial: 1+x+x^2+x^7+x^8 */
 
19
int Pp[MM+1] = { 1, 1, 1, 0, 0, 0, 0, 1, 1 };
 
20
 
 
21
#else /* not CCSDS */
 
22
/* MM, KK, B0, PRIM are user-defined in rs.h */
 
23
 
 
24
/* Primitive polynomials - see Lin & Costello, Appendix A,
 
25
 * and  Lee & Messerschmitt, p. 453.
 
26
 */
 
27
#if(MM == 2)/* Admittedly silly */
 
28
int Pp[MM+1] = { 1, 1, 1 };
 
29
 
 
30
#elif(MM == 3)
 
31
/* 1 + x + x^3 */
 
32
int Pp[MM+1] = { 1, 1, 0, 1 };
 
33
 
 
34
#elif(MM == 4)
 
35
/* 1 + x + x^4 */
 
36
int Pp[MM+1] = { 1, 1, 0, 0, 1 };
 
37
 
 
38
#elif(MM == 5)
 
39
/* 1 + x^2 + x^5 */
 
40
int Pp[MM+1] = { 1, 0, 1, 0, 0, 1 };
 
41
 
 
42
#elif(MM == 6)
 
43
/* 1 + x + x^6 */
 
44
int Pp[MM+1] = { 1, 1, 0, 0, 0, 0, 1 };
 
45
 
 
46
#elif(MM == 7)
 
47
/* 1 + x^3 + x^7 */
 
48
int Pp[MM+1] = { 1, 0, 0, 1, 0, 0, 0, 1 };
 
49
 
 
50
#elif(MM == 8)
 
51
/* 1+x^2+x^3+x^4+x^8 */
 
52
int Pp[MM+1] = { 1, 0, 1, 1, 1, 0, 0, 0, 1 };
 
53
 
 
54
#elif(MM == 9)
 
55
/* 1+x^4+x^9 */
 
56
int Pp[MM+1] = { 1, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
 
57
 
 
58
#elif(MM == 10)
 
59
/* 1+x^3+x^10 */
 
60
int Pp[MM+1] = { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1 };
 
61
 
 
62
#elif(MM == 11)
 
63
/* 1+x^2+x^11 */
 
64
int Pp[MM+1] = { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
 
65
 
 
66
#elif(MM == 12)
 
67
/* 1+x+x^4+x^6+x^12 */
 
68
int Pp[MM+1] = { 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1 };
 
69
 
 
70
#elif(MM == 13)
 
71
/* 1+x+x^3+x^4+x^13 */
 
72
int Pp[MM+1] = { 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
 
73
 
 
74
#elif(MM == 14)
 
75
/* 1+x+x^6+x^10+x^14 */
 
76
int Pp[MM+1] = { 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1 };
 
77
 
 
78
#elif(MM == 15)
 
79
/* 1+x+x^15 */
 
80
int Pp[MM+1] = { 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
 
81
 
 
82
#elif(MM == 16)
 
83
/* 1+x+x^3+x^12+x^16 */
 
84
int Pp[MM+1] = { 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1 };
 
85
 
 
86
#else
 
87
#error "Either CCSDS must be defined, or MM must be set in range 2-16"
 
88
#endif
 
89
 
 
90
#endif
 
91
 
 
92
#ifdef STANDARD_ORDER /* first byte transmitted is index of x**(KK-1) in message poly*/
 
93
        /* definitions used in the encode routine*/
 
94
        #define MESSAGE(i) data[KK-(i)-1]
 
95
        #define REMAINDER(i) bb[NN-KK-(i)-1]
 
96
        /* definitions used in the decode routine*/
 
97
        #define RECEIVED(i) data[NN-1-(i)]
 
98
        #define ERAS_INDEX(i) (NN-1-eras_pos[i])
 
99
        #define INDEX_TO_POS(i) (NN-1-(i))
 
100
#else /* first byte transmitted is index of x**0 in message polynomial*/
 
101
        /* definitions used in the encode routine*/
 
102
        #define MESSAGE(i) data[i]
 
103
        #define REMAINDER(i) bb[i]
 
104
        /* definitions used in the decode routine*/
 
105
        #define RECEIVED(i) data[i]
 
106
        #define ERAS_INDEX(i) eras_pos[i]
 
107
        #define INDEX_TO_POS(i) i
 
108
#endif
 
109
 
 
110
 
 
111
/* This defines the type used to store an element of the Galois Field
 
112
 * used by the code. Make sure this is something larger than a char if
 
113
 * if anything larger than GF(256) is used.
 
114
 *
 
115
 * Note: unsigned char will work up to GF(256) but int seems to run
 
116
 * faster on the Pentium.
 
117
 */
 
118
typedef int gf;
 
119
 
 
120
/* index->polynomial form conversion table */
 
121
static gf Alpha_to[NN + 1];
 
122
 
 
123
/* Polynomial->index form conversion table */
 
124
static gf Index_of[NN + 1];
 
125
 
 
126
/* No legal value in index form represents zero, so
 
127
 * we need a special value for this purpose
 
128
 */
 
129
#define A0      (NN)
 
130
 
 
131
/* Generator polynomial g(x) in index form */
 
132
static gf Gg[NN - KK + 1];
 
133
 
 
134
static int RS_init; /* Initialization flag */
 
135
 
 
136
/* Compute x % NN, where NN is 2**MM - 1,
 
137
 * without a slow divide
 
138
 */
 
139
/* static inline gf*/
 
140
static gf
 
141
modnn(int x)
 
142
{
 
143
  while (x >= NN) {
 
144
    x -= NN;
 
145
    x = (x >> MM) + (x & NN);
 
146
  }
 
147
  return x;
 
148
}
 
149
 
 
150
#define min_(a,b)       ((a) < (b) ? (a) : (b))
 
151
 
 
152
#define CLEAR(a,n) {\
 
153
int ci;\
 
154
for(ci=(n)-1;ci >=0;ci--)\
 
155
(a)[ci] = 0;\
 
156
}
 
157
 
 
158
#define COPY(a,b,n) {\
 
159
int ci;\
 
160
for(ci=(n)-1;ci >=0;ci--)\
 
161
(a)[ci] = (b)[ci];\
 
162
}
 
163
 
 
164
#define COPYDOWN(a,b,n) {\
 
165
int ci;\
 
166
for(ci=(n)-1;ci >=0;ci--)\
 
167
(a)[ci] = (b)[ci];\
 
168
}
 
169
 
 
170
static void init_rs(void);
 
171
 
 
172
#ifdef CCSDS
 
173
/* Conversion lookup tables from conventional alpha to Berlekamp's
 
174
 * dual-basis representation. Used in the CCSDS version only.
 
175
 * taltab[] -- convert conventional to dual basis
 
176
 * tal1tab[] -- convert dual basis to conventional
 
177
 
 
178
 * Note: the actual RS encoder/decoder works with the conventional basis.
 
179
 * So data is converted from dual to conventional basis before either
 
180
 * encoding or decoding and then converted back.
 
181
 */
 
182
static unsigned char taltab[NN+1],tal1tab[NN+1];
 
183
 
 
184
static unsigned char tal[] = { 0x8d, 0xef, 0xec, 0x86, 0xfa, 0x99, 0xaf, 0x7b };
 
185
 
 
186
/* Generate conversion lookup tables between conventional alpha representation
 
187
 * (@**7, @**6, ...@**0)
 
188
 *  and Berlekamp's dual basis representation
 
189
 * (l0, l1, ...l7)
 
190
 */
 
191
static void
 
192
gen_ltab(void)
 
193
{
 
194
  int i,j,k;
 
195
 
 
196
  for(i=0;i<256;i++){/* For each value of input */
 
197
    taltab[i] = 0;
 
198
    for(j=0;j<8;j++) /* for each column of matrix */
 
199
      for(k=0;k<8;k++){ /* for each row of matrix */
 
200
        if(i & (1<<k))
 
201
           taltab[i] ^= tal[7-k] & (1<<j);
 
202
      }
 
203
    tal1tab[taltab[i]] = i;
 
204
  }
 
205
}
 
206
#endif /* CCSDS */
 
207
 
 
208
#if PRIM != 1
 
209
static int Ldec;/* Decrement for aux location variable in Chien search */
 
210
 
 
211
static void
 
212
gen_ldec(void)
 
213
{
 
214
  for(Ldec=1;(Ldec % PRIM) != 0;Ldec+= NN)
 
215
    ;
 
216
  Ldec /= PRIM;
 
217
}
 
218
#else
 
219
#define Ldec 1
 
220
#endif
 
221
 
 
222
/* generate GF(2**m) from the irreducible polynomial p(X) in Pp[0]..Pp[m]
 
223
   lookup tables:  index->polynomial form   alpha_to[] contains j=alpha**i;
 
224
                   polynomial form -> index form  index_of[j=alpha**i] = i
 
225
   alpha=2 is the primitive element of GF(2**m)
 
226
   HARI's COMMENT: (4/13/94) alpha_to[] can be used as follows:
 
227
        Let @ represent the primitive element commonly called "alpha" that
 
228
   is the root of the primitive polynomial p(x). Then in GF(2^m), for any
 
229
   0 <= i <= 2^m-2,
 
230
        @^i = a(0) + a(1) @ + a(2) @^2 + ... + a(m-1) @^(m-1)
 
231
   where the binary vector (a(0),a(1),a(2),...,a(m-1)) is the representation
 
232
   of the integer "alpha_to[i]" with a(0) being the LSB and a(m-1) the MSB. Thus for
 
233
   example the polynomial representation of @^5 would be given by the binary
 
234
   representation of the integer "alpha_to[5]".
 
235
                   Similarily, index_of[] can be used as follows:
 
236
        As above, let @ represent the primitive element of GF(2^m) that is
 
237
   the root of the primitive polynomial p(x). In order to find the power
 
238
   of @ (alpha) that has the polynomial representation
 
239
        a(0) + a(1) @ + a(2) @^2 + ... + a(m-1) @^(m-1)
 
240
   we consider the integer "i" whose binary representation with a(0) being LSB
 
241
   and a(m-1) MSB is (a(0),a(1),...,a(m-1)) and locate the entry
 
242
   "index_of[i]". Now, @^index_of[i] is that element whose polynomial 
 
243
    representation is (a(0),a(1),a(2),...,a(m-1)).
 
244
   NOTE:
 
245
        The element alpha_to[2^m-1] = 0 always signifying that the
 
246
   representation of "@^infinity" = 0 is (0,0,0,...,0).
 
247
        Similarily, the element index_of[0] = A0 always signifying
 
248
   that the power of alpha which has the polynomial representation
 
249
   (0,0,...,0) is "infinity".
 
250
 
 
251
*/
 
252
 
 
253
static void
 
254
generate_gf(void)
 
255
{
 
256
  register int i, mask;
 
257
 
 
258
  mask = 1;
 
259
  Alpha_to[MM] = 0;
 
260
  for (i = 0; i < MM; i++) {
 
261
    Alpha_to[i] = mask;
 
262
    Index_of[Alpha_to[i]] = i;
 
263
    /* If Pp[i] == 1 then, term @^i occurs in poly-repr of @^MM */
 
264
    if (Pp[i] != 0)
 
265
      Alpha_to[MM] ^= mask;     /* Bit-wise EXOR operation */
 
266
    mask <<= 1; /* single left-shift */
 
267
  }
 
268
  Index_of[Alpha_to[MM]] = MM;
 
269
  /*
 
270
   * Have obtained poly-repr of @^MM. Poly-repr of @^(i+1) is given by
 
271
   * poly-repr of @^i shifted left one-bit and accounting for any @^MM
 
272
   * term that may occur when poly-repr of @^i is shifted.
 
273
   */
 
274
  mask >>= 1;
 
275
  for (i = MM + 1; i < NN; i++) {
 
276
    if (Alpha_to[i - 1] >= mask)
 
277
      Alpha_to[i] = Alpha_to[MM] ^ ((Alpha_to[i - 1] ^ mask) << 1);
 
278
    else
 
279
      Alpha_to[i] = Alpha_to[i - 1] << 1;
 
280
    Index_of[Alpha_to[i]] = i;
 
281
  }
 
282
  Index_of[0] = A0;
 
283
  Alpha_to[NN] = 0;
 
284
}
 
285
 
 
286
/*
 
287
 * Obtain the generator polynomial of the TT-error correcting, length
 
288
 * NN=(2**MM -1) Reed Solomon code from the product of (X+@**(B0+i)), i = 0,
 
289
 * ... ,(2*TT-1)
 
290
 *
 
291
 * Examples:
 
292
 *
 
293
 * If B0 = 1, TT = 1. deg(g(x)) = 2*TT = 2.
 
294
 * g(x) = (x+@) (x+@**2)
 
295
 *
 
296
 * If B0 = 0, TT = 2. deg(g(x)) = 2*TT = 4.
 
297
 * g(x) = (x+1) (x+@) (x+@**2) (x+@**3)
 
298
 */
 
299
static void
 
300
gen_poly(void)
 
301
{
 
302
  register int i, j;
 
303
 
 
304
  Gg[0] = 1;
 
305
  for (i = 0; i < NN - KK; i++) {
 
306
    Gg[i+1] = 1;
 
307
    /*
 
308
     * Below multiply (Gg[0]+Gg[1]*x + ... +Gg[i]x^i) by
 
309
     * (@**(B0+i)*PRIM + x)
 
310
     */
 
311
    for (j = i; j > 0; j--)
 
312
      if (Gg[j] != 0)
 
313
        Gg[j] = Gg[j - 1] ^ Alpha_to[modnn((Index_of[Gg[j]]) + (B0 + i) *PRIM)];
 
314
      else
 
315
        Gg[j] = Gg[j - 1];
 
316
    /* Gg[0] can never be zero */
 
317
    Gg[0] = Alpha_to[modnn(Index_of[Gg[0]] + (B0 + i) * PRIM)];
 
318
  }
 
319
  /* convert Gg[] to index form for quicker encoding */
 
320
  for (i = 0; i <= NN - KK; i++)
 
321
    Gg[i] = Index_of[Gg[i]];
 
322
}
 
323
 
 
324
 
 
325
/*
 
326
 * take the string of symbols in data[i], i=0..(k-1) and encode
 
327
 * systematically to produce NN-KK parity symbols in bb[0]..bb[NN-KK-1] data[]
 
328
 * is input and bb[] is output in polynomial form. Encoding is done by using
 
329
 * a feedback shift register with appropriate connections specified by the
 
330
 * elements of Gg[], which was generated above. Codeword is   c(X) =
 
331
 * data(X)*X**(NN-KK)+ b(X)
 
332
 */
 
333
 
 
334
int
 
335
encode_rs(dtype data[KK], dtype bb[NN-KK])
 
336
{
 
337
  register int i, j;
 
338
  gf feedback;
 
339
 
 
340
#if DEBUG >= 1 && MM != 8
 
341
  /* Check for illegal input values */
 
342
  for(i=0;i<KK;i++)
 
343
    if(MESSAGE(i) > NN)
 
344
      return -1;
 
345
#endif
 
346
 
 
347
  if(!RS_init)
 
348
    init_rs();
 
349
 
 
350
  CLEAR(bb,NN-KK);
 
351
 
 
352
#ifdef CCSDS
 
353
  /* Convert to conventional basis */
 
354
  for(i=0;i<KK;i++)
 
355
    MESSAGE(i) = tal1tab[MESSAGE(i)];
 
356
#endif
 
357
 
 
358
  for(i = KK - 1; i >= 0; i--) {
 
359
    feedback = Index_of[MESSAGE(i) ^ REMAINDER(NN - KK - 1)];
 
360
    if (feedback != A0) {       /* feedback term is non-zero */
 
361
      for (j = NN - KK - 1; j > 0; j--)
 
362
                if (Gg[j] != A0)
 
363
                  REMAINDER(j) = REMAINDER(j - 1) ^ Alpha_to[modnn(Gg[j] + feedback)];
 
364
                else
 
365
                  REMAINDER(j) = REMAINDER(j - 1);
 
366
                  REMAINDER(0) = Alpha_to[modnn(Gg[0] + feedback)];
 
367
    } else {    /* feedback term is zero. encoder becomes a
 
368
                 * single-byte shifter */
 
369
      for (j = NN - KK - 1; j > 0; j--)
 
370
        REMAINDER(j) = REMAINDER(j - 1);
 
371
      REMAINDER(0) = 0;
 
372
    }
 
373
  }
 
374
#ifdef CCSDS
 
375
  /* Convert to l-basis */
 
376
  for(i=0;i<NN;i++)
 
377
    MESSAGE(i) = taltab[MESSAGE(i)];
 
378
#endif
 
379
 
 
380
  return 0;
 
381
}
 
382
 
 
383
/*
 
384
 * Performs ERRORS+ERASURES decoding of RS codes. If decoding is successful,
 
385
 * writes the codeword into data[] itself. Otherwise data[] is unaltered.
 
386
 *
 
387
 * Return number of symbols corrected, or -1 if codeword is illegal
 
388
 * or uncorrectable. If eras_pos is non-null, the detected error locations
 
389
 * are written back. NOTE! This array must be at least NN-KK elements long.
 
390
 * 
 
391
 * First "no_eras" erasures are declared by the calling program. Then, the
 
392
 * maximum # of errors correctable is t_after_eras = floor((NN-KK-no_eras)/2).
 
393
 * If the number of channel errors is not greater than "t_after_eras" the
 
394
 * transmitted codeword will be recovered. Details of algorithm can be found
 
395
 * in R. Blahut's "Theory ... of Error-Correcting Codes".
 
396
 
 
397
 * Warning: the eras_pos[] array must not contain duplicate entries; decoder failure
 
398
 * will result. The decoder *could* check for this condition, but it would involve
 
399
 * extra time on every decoding operation.
 
400
 */
 
401
 
 
402
int
 
403
eras_dec_rs(dtype data[NN], int eras_pos[NN-KK], int no_eras)
 
404
{
 
405
  int deg_lambda, el, deg_omega;
 
406
  int i, j, r,k;
 
407
  gf u,q,tmp,num1,num2,den,discr_r;
 
408
  gf lambda[NN-KK + 1], s[NN-KK + 1];   /* Err+Eras Locator poly
 
409
                                         * and syndrome poly */
 
410
  gf b[NN-KK + 1], t[NN-KK + 1], omega[NN-KK + 1];
 
411
  gf root[NN-KK], reg[NN-KK + 1], loc[NN-KK];
 
412
  int syn_error, count;
 
413
 
 
414
  if(!RS_init)
 
415
    init_rs();
 
416
 
 
417
#ifdef CCSDS
 
418
  /* Convert to conventional basis */
 
419
  for(i=0;i<NN;i++)
 
420
    RECEIVED(i) = tal1tab[RECEIVED(i)];
 
421
#endif
 
422
 
 
423
#if DEBUG >= 1 && MM != 8
 
424
  /* Check for illegal input values */
 
425
  for(i=0;i<NN;i++)
 
426
    if(RECEIVED(i) > NN)
 
427
      return -1;
 
428
#endif
 
429
  /* form the syndromes; i.e., evaluate data(x) at roots of g(x)
 
430
   * namely @**(B0+i)*PRIM, i = 0, ... ,(NN-KK-1)
 
431
   */
 
432
  for(i=1;i<=NN-KK;i++){
 
433
    s[i] = RECEIVED(0);
 
434
  }
 
435
  for(j=1;j<NN;j++){
 
436
    if(RECEIVED(j) == 0)
 
437
      continue;
 
438
    tmp = Index_of[RECEIVED(j)];
 
439
    
 
440
    /*  s[i] ^= Alpha_to[modnn(tmp + (B0+i-1)*j)]; */
 
441
    for(i=1;i<=NN-KK;i++)
 
442
      s[i] ^= Alpha_to[modnn(tmp + (B0+i-1)*PRIM*j)];
 
443
  }
 
444
  /* Convert syndromes to index form, checking for nonzero condition */
 
445
  syn_error = 0;
 
446
  for(i=1;i<=NN-KK;i++){
 
447
    syn_error |= s[i];
 
448
        /*printf("syndrome %d = %x\n",i,s[i]);*/
 
449
    s[i] = Index_of[s[i]];
 
450
  }
 
451
  
 
452
  if (!syn_error) {
 
453
    /* if syndrome is zero, data[] is a codeword and there are no
 
454
     * errors to correct. So return data[] unmodified
 
455
     */
 
456
    count = 0;
 
457
    goto finish;
 
458
  }
 
459
  CLEAR(&lambda[1],NN-KK);
 
460
  lambda[0] = 1;
 
461
 
 
462
  if (no_eras > 0) {
 
463
    /* Init lambda to be the erasure locator polynomial */
 
464
    lambda[1] = Alpha_to[modnn(PRIM * ERAS_INDEX(0))];
 
465
    for (i = 1; i < no_eras; i++) {
 
466
      u = modnn(PRIM*ERAS_INDEX(i));
 
467
      for (j = i+1; j > 0; j--) {
 
468
        tmp = Index_of[lambda[j - 1]];
 
469
        if(tmp != A0)
 
470
          lambda[j] ^= Alpha_to[modnn(u + tmp)];
 
471
      }
 
472
    }
 
473
#if DEBUG >= 1
 
474
    /* Test code that verifies the erasure locator polynomial just constructed
 
475
       Needed only for decoder debugging. */
 
476
    
 
477
    /* find roots of the erasure location polynomial */
 
478
    for(i=1;i<=no_eras;i++)
 
479
      reg[i] = Index_of[lambda[i]];
 
480
    count = 0;
 
481
    for (i = 1,k=NN-Ldec; i <= NN; i++,k = modnn(NN+k-Ldec)) {
 
482
      q = 1;
 
483
      for (j = 1; j <= no_eras; j++)
 
484
        if (reg[j] != A0) {
 
485
          reg[j] = modnn(reg[j] + j);
 
486
          q ^= Alpha_to[reg[j]];
 
487
        }
 
488
      if (q != 0)
 
489
        continue;
 
490
      /* store root and error location number indices */
 
491
      root[count] = i;
 
492
      loc[count] = k;
 
493
      count++;
 
494
    }
 
495
    if (count != no_eras) {
 
496
      printf("\n lambda(x) is WRONG\n");
 
497
      count = -1;
 
498
      goto finish;
 
499
    }
 
500
#if DEBUG >= 2
 
501
    printf("\n Erasure positions as determined by roots of Eras Loc Poly:\n");
 
502
    for (i = 0; i < count; i++)
 
503
      printf("%d ", loc[i]);
 
504
    printf("\n");
 
505
#endif
 
506
#endif
 
507
  }
 
508
  for(i=0;i<NN-KK+1;i++)
 
509
    b[i] = Index_of[lambda[i]];
 
510
  
 
511
  /*
 
512
   * Begin Berlekamp-Massey algorithm to determine error+erasure
 
513
   * locator polynomial
 
514
   */
 
515
  r = no_eras;
 
516
  el = no_eras;
 
517
  while (++r <= NN-KK) {        /* r is the step number */
 
518
    /* Compute discrepancy at the r-th step in poly-form */
 
519
    discr_r = 0;
 
520
    for (i = 0; i < r; i++){
 
521
      if ((lambda[i] != 0) && (s[r - i] != A0)) {
 
522
        discr_r ^= Alpha_to[modnn(Index_of[lambda[i]] + s[r - i])];
 
523
      }
 
524
    }
 
525
    discr_r = Index_of[discr_r];        /* Index form */
 
526
    if (discr_r == A0) {
 
527
      /* 2 lines below: B(x) <-- x*B(x) */
 
528
      COPYDOWN(&b[1],b,NN-KK);
 
529
      b[0] = A0;
 
530
    } else {
 
531
      /* 7 lines below: T(x) <-- lambda(x) - discr_r*x*b(x) */
 
532
      t[0] = lambda[0];
 
533
      for (i = 0 ; i < NN-KK; i++) {
 
534
        if(b[i] != A0)
 
535
          t[i+1] = lambda[i+1] ^ Alpha_to[modnn(discr_r + b[i])];
 
536
        else
 
537
          t[i+1] = lambda[i+1];
 
538
      }
 
539
      if (2 * el <= r + no_eras - 1) {
 
540
        el = r + no_eras - el;
 
541
        /*
 
542
         * 2 lines below: B(x) <-- inv(discr_r) *
 
543
         * lambda(x)
 
544
         */
 
545
        for (i = 0; i <= NN-KK; i++)
 
546
          b[i] = (lambda[i] == 0) ? A0 : modnn(Index_of[lambda[i]] - discr_r + NN);
 
547
      } else {
 
548
        /* 2 lines below: B(x) <-- x*B(x) */
 
549
        COPYDOWN(&b[1],b,NN-KK);
 
550
        b[0] = A0;
 
551
      }
 
552
      COPY(lambda,t,NN-KK+1);
 
553
    }
 
554
  }
 
555
 
 
556
  /* Convert lambda to index form and compute deg(lambda(x)) */
 
557
  deg_lambda = 0;
 
558
  for(i=0;i<NN-KK+1;i++){
 
559
    lambda[i] = Index_of[lambda[i]];
 
560
    if(lambda[i] != A0)
 
561
      deg_lambda = i;
 
562
  }
 
563
  /*
 
564
   * Find roots of the error+erasure locator polynomial by Chien
 
565
   * Search
 
566
   */
 
567
  COPY(&reg[1],&lambda[1],NN-KK);
 
568
  count = 0;            /* Number of roots of lambda(x) */
 
569
  for (i = 1,k=NN-Ldec; i <= NN; i++,k = modnn(NN+k-Ldec)) {
 
570
    q = 1;
 
571
    for (j = deg_lambda; j > 0; j--){
 
572
      if (reg[j] != A0) {
 
573
        reg[j] = modnn(reg[j] + j);
 
574
        q ^= Alpha_to[reg[j]];
 
575
      }
 
576
    }
 
577
    if (q != 0)
 
578
      continue;
 
579
    /* store root (index-form) and error location number */
 
580
    root[count] = i;
 
581
    loc[count] = k;
 
582
    /* If we've already found max possible roots,
 
583
     * abort the search to save time
 
584
     */
 
585
    if(++count == deg_lambda)
 
586
      break;
 
587
  }
 
588
  if (deg_lambda != count) {
 
589
    /*
 
590
     * deg(lambda) unequal to number of roots => uncorrectable
 
591
     * error detected
 
592
     */
 
593
    count = -1;
 
594
    goto finish;
 
595
  }
 
596
  /*
 
597
   * Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo
 
598
   * x**(NN-KK)). in index form. Also find deg(omega).
 
599
   */
 
600
  deg_omega = 0;
 
601
  for (i = 0; i < NN-KK;i++){
 
602
    tmp = 0;
 
603
    j = (deg_lambda < i) ? deg_lambda : i;
 
604
    for(;j >= 0; j--){
 
605
      if ((s[i + 1 - j] != A0) && (lambda[j] != A0))
 
606
        tmp ^= Alpha_to[modnn(s[i + 1 - j] + lambda[j])];
 
607
    }
 
608
    if(tmp != 0)
 
609
      deg_omega = i;
 
610
    omega[i] = Index_of[tmp];
 
611
  }
 
612
  omega[NN-KK] = A0;
 
613
  
 
614
  /*
 
615
   * Compute error values in poly-form. num1 = omega(inv(X(l))), num2 =
 
616
   * inv(X(l))**(B0-1) and den = lambda_pr(inv(X(l))) all in poly-form
 
617
   */
 
618
  for (j = count-1; j >=0; j--) {
 
619
    num1 = 0;
 
620
    for (i = deg_omega; i >= 0; i--) {
 
621
      if (omega[i] != A0)
 
622
        num1  ^= Alpha_to[modnn(omega[i] + i * root[j])];
 
623
    }
 
624
    num2 = Alpha_to[modnn(root[j] * (B0 - 1) + NN)];
 
625
    den = 0;
 
626
    
 
627
    /* lambda[i+1] for i even is the formal derivative lambda_pr of lambda[i] */
 
628
    for (i = min_(deg_lambda,NN-KK-1) & ~1; i >= 0; i -=2) {
 
629
      if(lambda[i+1] != A0)
 
630
        den ^= Alpha_to[modnn(lambda[i+1] + i * root[j])];
 
631
    }
 
632
    if (den == 0) {
 
633
#if DEBUG >= 1
 
634
      printf("\n ERROR: denominator = 0\n");
 
635
#endif
 
636
      /* Convert to dual- basis */
 
637
      count = -1;
 
638
      goto finish;
 
639
    }
 
640
    /* Apply error to data */
 
641
    if (num1 != 0) {
 
642
      RECEIVED(loc[j]) ^= Alpha_to[modnn(Index_of[num1] + Index_of[num2] + NN - Index_of[den])];
 
643
    }
 
644
  }
 
645
 finish:
 
646
#ifdef CCSDS
 
647
    /* Convert to dual- basis */
 
648
    for(i=0;i<NN;i++)
 
649
      RECEIVED(i) = taltab[RECEIVED(i)];
 
650
#endif
 
651
    if(eras_pos != NULL){
 
652
      for(i=0;i<count;i++){
 
653
      if(eras_pos!= NULL)
 
654
        eras_pos[i] = INDEX_TO_POS(loc[i]);
 
655
      }
 
656
    }
 
657
    return count;
 
658
}
 
659
/* Encoder/decoder initialization - call this first! */
 
660
static void
 
661
init_rs(void)
 
662
{
 
663
  generate_gf();
 
664
  gen_poly();
 
665
#ifdef CCSDS
 
666
  gen_ltab();
 
667
#endif
 
668
#if PRIM != 1
 
669
  gen_ldec();
 
670
#endif
 
671
  RS_init = 1;
 
672
}