----------------------------------------------------------------------
($symsize, $gfpoly, $fcr, $prim, $nroots, $pad)
| 2310 | |
| 2311 | //---------------------------------------------------------------------- |
| 2312 | public static function init_rs_char($symsize, $gfpoly, $fcr, $prim, $nroots, $pad) |
| 2313 | { |
| 2314 | // Common code for intializing a Reed-Solomon control block (char or int symbols) |
| 2315 | // Copyright 2004 Phil Karn, KA9Q |
| 2316 | // May be used under the terms of the GNU Lesser General Public License (LGPL) |
| 2317 | |
| 2318 | $rs = null; |
| 2319 | |
| 2320 | // Check parameter ranges |
| 2321 | if($symsize < 0 || $symsize > 8) return $rs; |
| 2322 | if($fcr < 0 || $fcr >= (1<<$symsize)) return $rs; |
| 2323 | if($prim <= 0 || $prim >= (1<<$symsize)) return $rs; |
| 2324 | if($nroots < 0 || $nroots >= (1<<$symsize)) return $rs; // Can't have more roots than symbol values! |
| 2325 | if($pad < 0 || $pad >= ((1<<$symsize) -1 - $nroots)) return $rs; // Too much padding |
| 2326 | |
| 2327 | $rs = new QRrsItem(); |
| 2328 | $rs->mm = $symsize; |
| 2329 | $rs->nn = (1<<$symsize)-1; |
| 2330 | $rs->pad = $pad; |
| 2331 | |
| 2332 | $rs->alpha_to = array_fill(0, $rs->nn+1, 0); |
| 2333 | $rs->index_of = array_fill(0, $rs->nn+1, 0); |
| 2334 | |
| 2335 | // PHP style macro replacement ;) |
| 2336 | $NN =& $rs->nn; |
| 2337 | $A0 =& $NN; |
| 2338 | |
| 2339 | // Generate Galois field lookup tables |
| 2340 | $rs->index_of[0] = $A0; // log(zero) = -inf |
| 2341 | $rs->alpha_to[$A0] = 0; // alpha**-inf = 0 |
| 2342 | $sr = 1; |
| 2343 | |
| 2344 | for($i=0; $i<$rs->nn; $i++) { |
| 2345 | $rs->index_of[$sr] = $i; |
| 2346 | $rs->alpha_to[$i] = $sr; |
| 2347 | $sr <<= 1; |
| 2348 | if($sr & (1<<$symsize)) { |
| 2349 | $sr ^= $gfpoly; |
| 2350 | } |
| 2351 | $sr &= $rs->nn; |
| 2352 | } |
| 2353 | |
| 2354 | if($sr != 1){ |
| 2355 | // field generator polynomial is not primitive! |
| 2356 | $rs = NULL; |
| 2357 | return $rs; |
| 2358 | } |
| 2359 | |
| 2360 | /* Form RS code generator polynomial from its roots */ |
| 2361 | $rs->genpoly = array_fill(0, $nroots+1, 0); |
| 2362 | |
| 2363 | $rs->fcr = $fcr; |
| 2364 | $rs->prim = $prim; |
| 2365 | $rs->nroots = $nroots; |
| 2366 | $rs->gfpoly = $gfpoly; |
| 2367 | |
| 2368 | /* Find prim-th root of 1, used in decoding */ |
| 2369 | for($iprim=1;($iprim % $prim) != 0;$iprim += $rs->nn) |