PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.20
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.20
1.10.20 1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 1.10.1 1.10.0 1.9.17 1.9.15 1.9.16 All 164 releases
woocommerce-pos / vendor_prefixed / chillerlan / php-qrcode / src / Data / ReedSolomonEncoder.php

ReedSolomonEncoder.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.20, at vendor_prefixed/chillerlan/php-qrcode/src/Data/ReedSolomonEncoder.php

110 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class ReedSolomonEncoder
5 *
6 * @created 07.01.2021
7 * @author smiley <[email protected]>
8 * @copyright 2021 smiley
9 * @license MIT
10 */
11 namespace WCPOS\Vendor\chillerlan\QRCode\Data;
12
13 use WCPOS\Vendor\chillerlan\QRCode\Common\BitBuffer;
14 use WCPOS\Vendor\chillerlan\QRCode\Common\EccLevel;
15 use WCPOS\Vendor\chillerlan\QRCode\Common\GenericGFPoly;
16 use WCPOS\Vendor\chillerlan\QRCode\Common\GF256;
17 use WCPOS\Vendor\chillerlan\QRCode\Common\Version;
18 use function array_fill, array_merge, count, max;
19 /**
20 * Reed-Solomon encoding - ISO/IEC 18004:2000 Section 8.5 ff
21 *
22 * @see http://www.thonky.com/qr-code-tutorial/error-correction-coding
23 */
24 final class ReedSolomonEncoder
25 {
26 private Version $version;
27 private EccLevel $eccLevel;
28 private array $interleavedData;
29 private int $interleavedDataIndex;
30 /**
31 * ReedSolomonDecoder constructor
32 */
33 public function __construct(Version $version, EccLevel $eccLevel)
34 {
35 $this->version = $version;
36 $this->eccLevel = $eccLevel;
37 }
38 /**
39 * ECC encoding and interleaving
40 *
41 * @throws \chillerlan\QRCode\QRCodeException
42 */
43 public function interleaveEcBytes(BitBuffer $bitBuffer) : array
44 {
45 [$numEccCodewords, [[$l1, $b1], [$l2, $b2]]] = $this->version->getRSBlocks($this->eccLevel);
46 $rsBlocks = array_fill(0, $l1, [$numEccCodewords + $b1, $b1]);
47 if ($l2 > 0) {
48 $rsBlocks = array_merge($rsBlocks, array_fill(0, $l2, [$numEccCodewords + $b2, $b2]));
49 }
50 $bitBufferData = $bitBuffer->getBuffer();
51 $dataBytes = [];
52 $ecBytes = [];
53 $maxDataBytes = 0;
54 $maxEcBytes = 0;
55 $dataByteOffset = 0;
56 foreach ($rsBlocks as $key => [$rsBlockTotal, $dataByteCount]) {
57 $dataBytes[$key] = [];
58 for ($i = 0; $i < $dataByteCount; $i++) {
59 $dataBytes[$key][$i] = $bitBufferData[$i + $dataByteOffset] & 0xff;
60 }
61 $ecByteCount = $rsBlockTotal - $dataByteCount;
62 $ecBytes[$key] = $this->encode($dataBytes[$key], $ecByteCount);
63 $maxDataBytes = max($maxDataBytes, $dataByteCount);
64 $maxEcBytes = max($maxEcBytes, $ecByteCount);
65 $dataByteOffset += $dataByteCount;
66 }
67 $this->interleavedData = array_fill(0, $this->version->getTotalCodewords(), 0);
68 $this->interleavedDataIndex = 0;
69 $numRsBlocks = $l1 + $l2;
70 $this->interleave($dataBytes, $maxDataBytes, $numRsBlocks);
71 $this->interleave($ecBytes, $maxEcBytes, $numRsBlocks);
72 return $this->interleavedData;
73 }
74 /**
75 *
76 */
77 private function encode(array $dataBytes, int $ecByteCount) : array
78 {
79 $rsPoly = new GenericGFPoly([1]);
80 for ($i = 0; $i < $ecByteCount; $i++) {
81 $rsPoly = $rsPoly->multiply(new GenericGFPoly([1, GF256::exp($i)]));
82 }
83 $rsPolyDegree = $rsPoly->getDegree();
84 $modCoefficients = (new GenericGFPoly($dataBytes, $rsPolyDegree))->mod($rsPoly)->getCoefficients();
85 $ecBytes = array_fill(0, $rsPolyDegree, 0);
86 $count = count($modCoefficients) - $rsPolyDegree;
87 foreach ($ecBytes as $i => &$val) {
88 $modIndex = $i + $count;
89 $val = 0;
90 if ($modIndex >= 0) {
91 $val = $modCoefficients[$modIndex];
92 }
93 }
94 return $ecBytes;
95 }
96 /**
97 *
98 */
99 private function interleave(array $byteArray, int $maxBytes, int $numRsBlocks) : void
100 {
101 for ($x = 0; $x < $maxBytes; $x++) {
102 for ($y = 0; $y < $numRsBlocks; $y++) {
103 if ($x < count($byteArray[$y])) {
104 $this->interleavedData[$this->interleavedDataIndex++] = $byteArray[$y][$x];
105 }
106 }
107 }
108 }
109 }
110