| 106 | } |
| 107 | |
| 108 | bool |
| 109 | ReedSolomonDecode(const GenericGF& field, std::vector<int>& message, int numECCodeWords) |
| 110 | { |
| 111 | GenericGFPoly poly(field, message); |
| 112 | |
| 113 | std::vector<int> syndromes(numECCodeWords); |
| 114 | for (int i = 0; i < numECCodeWords; i++) |
| 115 | syndromes[numECCodeWords - 1 - i] = poly.evaluateAt(field.exp(i + field.generatorBase())); |
| 116 | |
| 117 | // if all syndromes are 0 there is no error to correct |
| 118 | if (std::all_of(syndromes.begin(), syndromes.end(), [](int c) { return c == 0; })) |
| 119 | return true; |
| 120 | |
| 121 | ZX_THREAD_LOCAL GenericGFPoly sigma, omega; |
| 122 | |
| 123 | if (!RunEuclideanAlgorithm(field, std::move(syndromes), sigma, omega)) |
| 124 | return false; |
| 125 | |
| 126 | auto errorLocations = FindErrorLocations(field, sigma); |
| 127 | if (errorLocations.empty()) |
| 128 | return false; |
| 129 | |
| 130 | auto errorMagnitudes = FindErrorMagnitudes(field, omega, errorLocations); |
| 131 | |
| 132 | int msgLen = Size(message); |
| 133 | for (int i = 0; i < Size(errorLocations); ++i) { |
| 134 | int position = msgLen - 1 - field.log(errorLocations[i]); |
| 135 | if (position < 0) |
| 136 | return false; |
| 137 | |
| 138 | message[position] ^= errorMagnitudes[i]; |
| 139 | } |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | } // namespace ZXing |
no test coverage detected