| Total Complexity | 40 |
| Total Lines | 280 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ReedSolomonDecoder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ReedSolomonDecoder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | final class ReedSolomonDecoder{ |
||
| 36 | |||
| 37 | private Version $version; |
||
| 38 | private EccLevel $eccLevel; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * ReedSolomonDecoder constructor |
||
| 42 | */ |
||
| 43 | public function __construct(Version $version, EccLevel $eccLevel){ |
||
| 44 | $this->version = $version; |
||
| 45 | $this->eccLevel = $eccLevel; |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Error-correct and copy data blocks together into a stream of bytes |
||
| 50 | */ |
||
| 51 | public function decode(array $rawCodewords):BitBuffer{ |
||
| 52 | $dataBlocks = $this->deinterleaveRawBytes($rawCodewords); |
||
| 53 | $dataBytes = []; |
||
| 54 | |||
| 55 | foreach($dataBlocks as $dataBlock){ |
||
| 56 | [$numDataCodewords, $codewordBytes] = $dataBlock; |
||
| 57 | |||
| 58 | $corrected = $this->correctErrors($codewordBytes, $numDataCodewords); |
||
| 59 | |||
| 60 | for($i = 0; $i < $numDataCodewords; $i++){ |
||
| 61 | $dataBytes[] = $corrected[$i]; |
||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | return new BitBuffer($dataBytes); |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * When QR Codes use multiple data blocks, they are actually interleaved. |
||
| 70 | * That is, the first byte of data block 1 to n is written, then the second bytes, and so on. This |
||
| 71 | * method will separate the data into original blocks. |
||
| 72 | * |
||
| 73 | * @throws \chillerlan\QRCode\Decoder\QRCodeDecoderException |
||
| 74 | */ |
||
| 75 | private function deinterleaveRawBytes(array $rawCodewords):array{ |
||
| 76 | // Figure out the number and size of data blocks used by this version and |
||
| 77 | // error correction level |
||
| 78 | [$numEccCodewords, $eccBlocks] = $this->version->getRSBlocks($this->eccLevel); |
||
| 79 | |||
| 80 | // Now establish DataBlocks of the appropriate size and number of data codewords |
||
| 81 | $result = [];//new DataBlock[$totalBlocks]; |
||
| 82 | $numResultBlocks = 0; |
||
| 83 | |||
| 84 | foreach($eccBlocks as $blockData){ |
||
| 85 | [$numEccBlocks, $eccPerBlock] = $blockData; |
||
| 86 | |||
| 87 | for($i = 0; $i < $numEccBlocks; $i++, $numResultBlocks++){ |
||
| 88 | $result[$numResultBlocks] = [$eccPerBlock, array_fill(0, $numEccCodewords + $eccPerBlock, 0)]; |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | // All blocks have the same amount of data, except that the last n |
||
| 93 | // (where n may be 0) have 1 more byte. Figure out where these start. |
||
| 94 | /** @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset */ |
||
| 95 | $shorterBlocksTotalCodewords = count($result[0][1]); |
||
| 96 | $longerBlocksStartAt = count($result) - 1; |
||
| 97 | |||
| 98 | while($longerBlocksStartAt >= 0){ |
||
| 99 | $numCodewords = count($result[$longerBlocksStartAt][1]); |
||
| 100 | |||
| 101 | if($numCodewords == $shorterBlocksTotalCodewords){ |
||
| 102 | break; |
||
| 103 | } |
||
| 104 | |||
| 105 | $longerBlocksStartAt--; |
||
| 106 | } |
||
| 107 | |||
| 108 | $longerBlocksStartAt++; |
||
| 109 | |||
| 110 | $shorterBlocksNumDataCodewords = $shorterBlocksTotalCodewords - $numEccCodewords; |
||
| 111 | // The last elements of result may be 1 element longer; |
||
| 112 | // first fill out as many elements as all of them have |
||
| 113 | $rawCodewordsOffset = 0; |
||
| 114 | |||
| 115 | for($i = 0; $i < $shorterBlocksNumDataCodewords; $i++){ |
||
| 116 | for($j = 0; $j < $numResultBlocks; $j++){ |
||
| 117 | $result[$j][1][$i] = $rawCodewords[$rawCodewordsOffset++]; |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | // Fill out the last data block in the longer ones |
||
| 122 | for($j = $longerBlocksStartAt; $j < $numResultBlocks; $j++){ |
||
| 123 | $result[$j][1][$shorterBlocksNumDataCodewords] = $rawCodewords[$rawCodewordsOffset++]; |
||
| 124 | } |
||
| 125 | |||
| 126 | // Now add in error correction blocks |
||
| 127 | /** @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset */ |
||
| 128 | $max = count($result[0][1]); |
||
| 129 | |||
| 130 | for($i = $shorterBlocksNumDataCodewords; $i < $max; $i++){ |
||
| 131 | for($j = 0; $j < $numResultBlocks; $j++){ |
||
| 132 | $iOffset = $j < $longerBlocksStartAt ? $i : $i + 1; |
||
| 133 | $result[$j][1][$iOffset] = $rawCodewords[$rawCodewordsOffset++]; |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | // DataBlocks containing original bytes, "de-interleaved" from representation in the QR Code |
||
| 138 | return $result; |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Given data and error-correction codewords received, possibly corrupted by errors, attempts to |
||
| 143 | * correct the errors in-place using Reed-Solomon error correction. |
||
| 144 | */ |
||
| 145 | private function correctErrors(array $codewordBytes, int $numDataCodewords):array{ |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Decodes given set of received codewords, which include both data and error-correction |
||
| 166 | * codewords. Really, this means it uses Reed-Solomon to detect and correct errors, in-place, |
||
| 167 | * in the input. |
||
| 168 | * |
||
| 169 | * @param array $received data and error-correction codewords |
||
| 170 | * @param int $numEccCodewords number of error-correction codewords available |
||
| 171 | * |
||
| 172 | * @return int[] |
||
| 173 | * @throws \chillerlan\QRCode\QRCodeException if decoding fails for any reason |
||
| 174 | */ |
||
| 175 | private function decodeWords(array $received, int $numEccCodewords):array{ |
||
| 176 | $poly = new GenericGFPoly($received); |
||
| 177 | $syndromeCoefficients = []; |
||
| 178 | $error = false; |
||
| 179 | |||
| 180 | for($i = 0; $i < $numEccCodewords; $i++){ |
||
| 181 | $syndromeCoefficients[$i] = $poly->evaluateAt(GF256::exp($i)); |
||
| 182 | |||
| 183 | if($syndromeCoefficients[$i] !== 0){ |
||
| 184 | $error = true; |
||
| 185 | } |
||
| 186 | } |
||
| 187 | |||
| 188 | if(!$error){ |
||
|
|
|||
| 189 | return $received; |
||
| 190 | } |
||
| 191 | |||
| 192 | [$sigma, $omega] = $this->runEuclideanAlgorithm( |
||
| 193 | GF256::buildMonomial($numEccCodewords, 1), |
||
| 194 | new GenericGFPoly(array_reverse($syndromeCoefficients)), |
||
| 195 | $numEccCodewords |
||
| 196 | ); |
||
| 197 | |||
| 198 | $errorLocations = $this->findErrorLocations($sigma); |
||
| 199 | $errorMagnitudes = $this->findErrorMagnitudes($omega, $errorLocations); |
||
| 200 | $errorLocationsCount = count($errorLocations); |
||
| 201 | $receivedCount = count($received); |
||
| 202 | |||
| 203 | for($i = 0; $i < $errorLocationsCount; $i++){ |
||
| 204 | $position = $receivedCount - 1 - GF256::log($errorLocations[$i]); |
||
| 205 | |||
| 206 | if($position < 0){ |
||
| 207 | throw new QRCodeException('Bad error location'); |
||
| 208 | } |
||
| 209 | |||
| 210 | $received[$position] ^= $errorMagnitudes[$i]; |
||
| 211 | } |
||
| 212 | |||
| 213 | return $received; |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @return \chillerlan\QRCode\Common\GenericGFPoly[] [sigma, omega] |
||
| 218 | * @throws \chillerlan\QRCode\QRCodeException |
||
| 219 | */ |
||
| 220 | private function runEuclideanAlgorithm(GenericGFPoly $a, GenericGFPoly $b, int $R):array{ |
||
| 221 | // Assume a's degree is >= b's |
||
| 222 | if($a->getDegree() < $b->getDegree()){ |
||
| 223 | $temp = $a; |
||
| 224 | $a = $b; |
||
| 225 | $b = $temp; |
||
| 226 | } |
||
| 227 | |||
| 228 | $rLast = $a; |
||
| 229 | $r = $b; |
||
| 230 | $tLast = new GenericGFPoly([0]); |
||
| 231 | $t = new GenericGFPoly([1]); |
||
| 232 | |||
| 233 | // Run Euclidean algorithm until r's degree is less than R/2 |
||
| 234 | while(2 * $r->getDegree() >= $R){ |
||
| 235 | $rLastLast = $rLast; |
||
| 236 | $tLastLast = $tLast; |
||
| 237 | $rLast = $r; |
||
| 238 | $tLast = $t; |
||
| 239 | |||
| 240 | // Divide rLastLast by rLast, with quotient in q and remainder in r |
||
| 241 | [$q, $r] = $rLastLast->divide($rLast); |
||
| 242 | |||
| 243 | $t = $q->multiply($tLast)->addOrSubtract($tLastLast); |
||
| 244 | |||
| 245 | if($r->getDegree() >= $rLast->getDegree()){ |
||
| 246 | throw new QRCodeException('Division algorithm failed to reduce polynomial?'); |
||
| 247 | } |
||
| 248 | } |
||
| 249 | |||
| 250 | $sigmaTildeAtZero = $t->getCoefficient(0); |
||
| 251 | |||
| 252 | if($sigmaTildeAtZero === 0){ |
||
| 253 | throw new QRCodeException('sigmaTilde(0) was zero'); |
||
| 254 | } |
||
| 255 | |||
| 256 | $inverse = GF256::inverse($sigmaTildeAtZero); |
||
| 257 | |||
| 258 | return [$t->multiplyInt($inverse), $r->multiplyInt($inverse)]; |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @throws \chillerlan\QRCode\QRCodeException |
||
| 263 | */ |
||
| 264 | private function findErrorLocations(GenericGFPoly $errorLocator):array{ |
||
| 265 | // This is a direct application of Chien's search |
||
| 266 | $numErrors = $errorLocator->getDegree(); |
||
| 267 | |||
| 268 | if($numErrors === 1){ // shortcut |
||
| 269 | return [$errorLocator->getCoefficient(1)]; |
||
| 270 | } |
||
| 271 | |||
| 272 | $result = array_fill(0, $numErrors, 0); |
||
| 273 | $e = 0; |
||
| 274 | |||
| 275 | for($i = 1; $i < 256 && $e < $numErrors; $i++){ |
||
| 276 | if($errorLocator->evaluateAt($i) === 0){ |
||
| 277 | $result[$e] = GF256::inverse($i); |
||
| 278 | $e++; |
||
| 279 | } |
||
| 280 | } |
||
| 281 | |||
| 282 | if($e !== $numErrors){ |
||
| 283 | throw new QRCodeException('Error locator degree does not match number of roots'); |
||
| 284 | } |
||
| 285 | |||
| 286 | return $result; |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * |
||
| 291 | */ |
||
| 292 | private function findErrorMagnitudes(GenericGFPoly $errorEvaluator, array $errorLocations):array{ |
||
| 315 | } |
||
| 316 | |||
| 317 | } |
||
| 318 |