1 Star 0 Fork 0

wenzhihzhon/cbrother-for-qrcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
QRrsItem.cb 6.26 KB
一键复制 编辑 原始数据 按行查看 历史
wenzhihzhon 提交于 2022-12-04 17:17 . init code
class QRrsItem {
var $mm; // Bits per symbol
var $nn; // Symbols per block (= (1<<mm)-1)
var $alpha_to = new Array(); // log lookup table
var $index_of = new Array(); // Antilog lookup table
var $genpoly = new Array(); // Generator polynomial
var $nroots; // Number of generator roots = number of parity symbols
var $fcr; // First consecutive root, index form
var $prim; // Primitive element, index form
var $iprim; // prim-th root of 1, index form
var $pad; // Padding bytes in shortened block
var $gfpoly;
//----------------------------------------------------------------------
function modnn($x)
{
while ($x >= this.$nn) {
$x -= this.$nn;
$x = ($x >> this.$mm) + ($x & this.$nn);
}
return $x;
}
//----------------------------------------------------------------------
function init_rs_char($symsize, $gfpoly, $fcr, $prim, $nroots, $pad)
{
// Common code for intializing a Reed-Solomon control block (char or int symbols)
// Copyright 2004 Phil Karn, KA9Q
// May be used under the terms of the GNU Lesser General License (LGPL)
var $rs = null;
// Check parameter ranges
if($symsize < 0 || $symsize > 8) return $rs;
if($fcr < 0 || $fcr >= (1<<$symsize)) return $rs;
if($prim <= 0 || $prim >= (1<<$symsize)) return $rs;
if($nroots < 0 || $nroots >= (1<<$symsize)) return $rs; // Can"t have more roots than symbol values!
if($pad < 0 || $pad >= ((1<<$symsize) -1 - $nroots)) return $rs; // Too much padding
$rs = new QRrsItem();
$rs.$mm = $symsize;
$rs.$nn = (1<<$symsize)-1;
$rs.$pad = $pad;
$rs.$alpha_to = php::array_fill(0, $rs.$nn+1, 0);
$rs.$index_of = php::array_fill(0, $rs.$nn+1, 0);
// PHP style macro replacement ;)
// var $NN =& $rs.$nn;
// var $A0 =& $NN;
var $NN = $rs.$nn;
var $A0 = $NN;
// Generate Galois field lookup tables
$rs.$index_of[0] = $A0; // log(zero) = -inf
$rs.$alpha_to[$A0] = 0; // alpha**-inf = 0
var $sr = 1;
for(var $i=0; $i<$rs.$nn; $i++) {
$rs.$index_of[$sr] = $i;
$rs.$alpha_to[$i] = $sr;
$sr <<= 1;
if($sr & (1<<$symsize)) {
$sr ^= $gfpoly;
}
$sr &= $rs.$nn;
}
if($sr != 1){
// field generator polynomial is not primitive!
$rs = null;
return $rs;
}
/* Form RS code generator polynomial from its roots */
$rs.$genpoly = php::array_fill(0, $nroots+1, 0);
$rs.$fcr = $fcr;
$rs.$prim = $prim;
$rs.$nroots = $nroots;
$rs.$gfpoly = $gfpoly;
// /* Find prim-th root of 1, used in decoding */
// for($iprim=1; ($iprim % $prim) != 0;$iprim += $rs.$nn)
// ; // intentional empty-body loop!
for(var $iprim=1; ($iprim % $prim) != 0;$iprim += $rs.$nn){}
$rs.$iprim = INT($iprim / $prim);
$rs.$genpoly[0] = 1;
var $root=$fcr*$prim;
for (var $i = 0; $i < $nroots; $i++, $root += $prim) {
$rs.$genpoly[$i+1] = 1;
// Multiply rs.$genpoly[] by @**(root + x)
for (var $j = $i; $j > 0; $j--) {
if ($rs.$genpoly[$j] != 0) {
$rs.$genpoly[$j] = $rs.$genpoly[$j-1] ^ $rs.$alpha_to[$rs.modnn($rs.$index_of[$rs.$genpoly[$j]] + $root)];
} else {
$rs.$genpoly[$j] = $rs.$genpoly[$j-1];
}
}
// rs.$genpoly[0] can never be zero
$rs.$genpoly[0] = $rs.$alpha_to[$rs.modnn($rs.$index_of[$rs.$genpoly[0]] + $root)];
}
// convert rs.$genpoly[] to index form for quicker encoding
for (var $i = 0; $i <= $nroots; $i++)
$rs.$genpoly[$i] = $rs.$index_of[$rs.$genpoly[$i]];
return $rs;
}
//----------------------------------------------------------------------
function encode_rs_char($data, $parity)
{
// $MM =& this.mm;
// $NN =& this.nn;
// $ALPHA_TO =& this.alpha_to;
// $INDEX_OF =& this.index_of;
// $GENPOLY =& this.genpoly;
// $NROOTS =& this.nroots;
// $FCR =& this.fcr;
// $PRIM =& this.prim;
// $IPRIM =& this.$iprim;
// $PAD =& this.pad;
// $A0 =& $NN;
var $MM = this.$mm;
var $NN = this.$nn;
var $ALPHA_TO = this.$alpha_to;
var $INDEX_OF = this.$index_of;
var $GENPOLY = this.$genpoly;
var $NROOTS = this.$nroots;
var $FCR = this.$fcr;
var $PRIM = this.$prim;
var $IPRIM = this.$iprim;
var $PAD = this.$pad;
var $A0 = $NN;
// = php::array_fill(0, $NROOTS, 0);
var tmpSize = 0;
if($parity && $parity.size()){ tmpSize = $parity.size();
for(var i = 0; i<$NROOTS; i++){
if(i<=tmpSize) $parity[i] = 0;
else $parity.add(0);
}
}
for(var $i=0; $i< ($NN-$NROOTS-$PAD); $i++) {
var $feedback = $INDEX_OF[$data[$i] ^ $parity[0]];
if($feedback != $A0) {
// feedback term is non-zero
// This line is unnecessary when GENPOLY[NROOTS] is unity, as it must
// always be for the polynomials constructed by init_rs()
$feedback = this.modnn($NN - $GENPOLY[$NROOTS] + $feedback);
for(var $j=1;$j<$NROOTS;$j++) {
$parity[$j] ^= $ALPHA_TO[this.modnn($feedback + $GENPOLY[$NROOTS-$j])];
}
}
// Shift
php::array_shift($parity);
if($feedback != $A0) {
php::array_push($parity, $ALPHA_TO[this.modnn($feedback + $GENPOLY[0])]);
} else {
php::array_push($parity, 0);
}
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
其他
1
https://gitee.com/wenzhihzhon/cbrother-for-qrcode.git
git@gitee.com:wenzhihzhon/cbrother-for-qrcode.git
wenzhihzhon
cbrother-for-qrcode
cbrother-for-qrcode
master

搜索帮助