function _Encode($aData, $aN, $aP, $aBitLen, &$aSymbols) { $n = count($aData); $m = count($aBitLen); $i = 0; $idx = 0; $aSymbols = array(); while ($n >= $aN) { $tmp = 0; for ($j = 0; $j < $aN; ++$j) { $tmp += $aP[$j] * $this->iT[$this->iSelectSchema][$aData[$i + $j]]; } Word2Bits($tmp, $aSymbols[$idx], $aBitLen[$m - 1]); ++$idx; $n -= $aN; $i += $aN; } if ($n > 0) { $tmp = 0; for ($j = 0; $j < $n; ++$j) { $tmp += $aP[$j] * $this->iT[$this->iSelectSchema][$aData[$i + $j]]; } Word2Bits($tmp, $aSymbols[$idx], $aBitLen[$n - 1]); } }
function _UnitTest() { // Test1 $test_data = array(0x3, 0x0, 0x41, 0x42, 0x31, 0x32, 0x2d, 0x58); $test_data_crc = 0x9aae; //$this->ByteReverse($test_data); $crc_res = $this->Get($test_data, false); if ($crc_res == $test_data_crc) { printf("CRC-CCITT = 0x%X (CORRECT)\n", $crc_res); $bits = array(); Word2Bits($crc_res, $bits, 16); print_r($bits); return true; } else { printf("CRC-CCITT = 0x%X WRONG !!! Should be \n", $crc_res, $test_data_crc); return false; } }
function Enc($aData, $aDebug = false) { if ($this->iTilde) { $r = tilde_process($aData); if ($r === false) { $this->iError = -10; return false; } $aData = $r; } $this->iConv = null; $this->iConv = ECC_Factory::Create($this->iErrLevel); // Start by splitting the input string to an array $data = str_split($aData); $ndata = count($data); // Automatically select the smallest encodation schema $this->iEncodation->AutoSelect($data); // Create the output bit array $bits = array(); // Get 5 Prefix bits that specified format id $bits = $this->iEncodation->GetPrefix(); $bidx = 5; // Calculate the CRC-CCITT (16 bits) for the original data and add it master bit stream. $crc_prefix = array(chr($this->iEncodation->GetCRCPrefix()), chr(0)); $crc_data = array_merge($crc_prefix, $data); $crc = $this->iCRC_CCITT->Get($crc_data); $crcbits = array(); Word2Bits($crc, $crcbits, 16); for ($i = 0; $i < 16; ++$i) { $bits[$bidx++] = $crcbits[$i]; } // Get data length as a 9 bit sequence bit reversed $lenbits = array(); Word2Bits($ndata, $lenbits, 9); $lenbits = array_reverse($lenbits); for ($i = 0; $i < 9; ++$i) { $bits[$bidx++] = $lenbits[$i]; } // Encode data and copy to master bit stream. $databits = array(); $this->iEncodation->Encode($data, $databits); // Number of code words. Each codeword is represented as an array of bits $m = count($databits); // In preparation to adding to the master bit stream each symbol // must first be bit reversed according to the standard for ($i = 0; $i < $m; ++$i) { $databits[$i] = array_reverse($databits[$i]); } // Add each code word in its bit-reversed form to the master bit stream for ($i = 0; $i < $m; ++$i) { $k = count($databits[$i]); for ($j = 0; $j < $k; ++$j) { $bits[$bidx++] = $databits[$i][$j]; } } // Now do the convolutional coding to create the protected bit stream $protectedbits = array(); $this->iConv->_Get($bits, $protectedbits); // Now get the header (depends on the ECC chosen) $headerbits = $this->iConv->GetHeader(); // Find out how many trailer bit (set to zero) we need to either // a) Make it the smallest possible size of matrix or // b) Fill it out to the user specified size of matrix $totBits = count($headerbits) + count($protectedbits); if ($this->iSize == -1) { // Find the smallest possible size to use $mat_size = 7; $mat_idx = 0; while ($mat_size <= 47 && $mat_size * $mat_size < $totBits) { $mat_idx++; $mat_size += 2; } if ($mat_size > 47) { $this->iError = -31; return false; } $this->iSize = $mat_size; $ntrailerbits = $mat_size * $mat_size - $totBits; } else { // User specified size $mat_size = $this->iSize; if ($mat_size * $mat_size < $totBits) { $this->iError = -31; return false; } $ntrailerbits = $mat_size * $mat_size - $totBits; $mat_idx = ($mat_size - 7) / 2; } $trailerbits = array_fill(0, $ntrailerbits, 0); // We now have the final bit stream by concatenating // header + protected bit stream + trailer bits $bits = array_merge($headerbits, $protectedbits, $trailerbits); $ret = $this->iMasterRand->Randomize($bits); if ($ret === false) { $this->iError = -33; return false; } // Place the bits in the matrice according to the bit placement and // add alignment edges to the output matrix $outputMatrix = array(array(), array()); $this->iBitPlacement->Set($mat_idx, $bits, $outputMatrix); $pspec = new PrintSpecification(DM_TYPE_140, $data, $outputMatrix, $this->iEncodation->iSelectSchema, $this->iErrLevel); return $pspec; }
function _Encode($aData, $aN, $aP, $aBitLen, &$aSymbols) { $n = count($aData); $m = count($aBitLen); $i = 0; $idx = 0; $aSymbols = array(); // Loop while we can complete full conversion for the chracters while ($n >= $aN) { $tmp = 0; for ($j = 0; $j < $aN; ++$j) { $tmp += $aP[$j] * $this->iT[$this->iSelectSchema][$aData[$i + $j]]; } Word2Bits($tmp, $aSymbols[$idx], $aBitLen[$m - 1]); ++$idx; $n -= $aN; $i += $aN; } // Now we have either processed all words or there are less than a // full conversion length left. In that case the specifications will // tell us how many bits to use when there are X characters left. if ($n > 0) { $tmp = 0; for ($j = 0; $j < $n; ++$j) { $tmp += $aP[$j] * $this->iT[$this->iSelectSchema][$aData[$i + $j]]; } Word2Bits($tmp, $aSymbols[$idx], $aBitLen[$n - 1]); } }
function Enc($aData, $aDebug = false) { if ($this->iTilde) { $r = tilde_process($aData); if ($r === false) { $this->iError = -10; return false; } $aData = $r; } $this->iConv = null; $this->iConv = ECC_Factory::Create($this->iErrLevel); $data = str_split($aData); $ndata = count($data); $this->iEncodation->AutoSelect($data); $bits = array(); $bits = $this->iEncodation->GetPrefix(); $bidx = 5; $crc_prefix = array(chr($this->iEncodation->GetCRCPrefix()), chr(0)); $crc_data = array_merge($crc_prefix, $data); $crc = $this->iCRC_CCITT->Get($crc_data); $crcbits = array(); Word2Bits($crc, $crcbits, 16); for ($i = 0; $i < 16; ++$i) { $bits[$bidx++] = $crcbits[$i]; } $lenbits = array(); Word2Bits($ndata, $lenbits, 9); $lenbits = array_reverse($lenbits); for ($i = 0; $i < 9; ++$i) { $bits[$bidx++] = $lenbits[$i]; } $databits = array(); $this->iEncodation->Encode($data, $databits); $m = count($databits); for ($i = 0; $i < $m; ++$i) { $databits[$i] = array_reverse($databits[$i]); } for ($i = 0; $i < $m; ++$i) { $k = count($databits[$i]); for ($j = 0; $j < $k; ++$j) { $bits[$bidx++] = $databits[$i][$j]; } } $protectedbits = array(); $this->iConv->_Get($bits, $protectedbits); $headerbits = $this->iConv->GetHeader(); $totBits = count($headerbits) + count($protectedbits); if ($this->iSize == -1) { $mat_size = 7; $mat_idx = 0; while ($mat_size <= 47 && $mat_size * $mat_size < $totBits) { $mat_idx++; $mat_size += 2; } if ($mat_size > 47) { $this->iError = -31; return false; } $this->iSize = $mat_size; $ntrailerbits = $mat_size * $mat_size - $totBits; } else { $mat_size = $this->iSize; if ($mat_size * $mat_size < $totBits) { $this->iError = -31; return false; } $ntrailerbits = $mat_size * $mat_size - $totBits; $mat_idx = ($mat_size - 7) / 2; } $trailerbits = array_fill(0, $ntrailerbits, 0); $bits = array_merge($headerbits, $protectedbits, $trailerbits); $ret = $this->iMasterRand->Randomize($bits); if ($ret === false) { $this->iError = -33; return false; } $outputMatrix = array(array(), array()); $this->iBitPlacement->Set($mat_idx, $bits, $outputMatrix); $pspec = new PrintSpecification(DM_TYPE_140, $data, $outputMatrix, $this->iEncodation->iSelectSchema, $this->iErrLevel); return $pspec; }