34
34
*/
35
35
final class ReedSolomonDecoder{
36
36
37
+ /**
38
+ * Error-correct and copy data blocks together into a stream of bytes
39
+ */
40
+ public function decode (array $ dataBlocks ):array {
41
+ $ resultBytes = [];
42
+
43
+ foreach ($ dataBlocks as $ dataBlock ){
44
+ [$ numDataCodewords , $ codewordBytes ] = $ dataBlock ;
45
+
46
+ $ corrected = $ this ->correctErrors ($ codewordBytes , $ numDataCodewords );
47
+
48
+ for ($ i = 0 ; $ i < $ numDataCodewords ; $ i ++){
49
+ $ resultBytes [] = $ corrected [$ i ];
50
+ }
51
+ }
52
+
53
+ return $ resultBytes ;
54
+ }
55
+
56
+ /**
57
+ * Given data and error-correction codewords received, possibly corrupted by errors, attempts to
58
+ * correct the errors in-place using Reed-Solomon error correction.
59
+ */
60
+ private function correctErrors (array $ codewordBytes , int $ numDataCodewords ):array {
61
+ // First read into an array of ints
62
+ $ codewordsInts = [];
63
+
64
+ foreach ($ codewordBytes as $ codewordByte ){
65
+ $ codewordsInts [] = $ codewordByte & 0xFF ;
66
+ }
67
+
68
+ $ decoded = $ this ->decodeWords ($ codewordsInts , (count ($ codewordBytes ) - $ numDataCodewords ));
69
+
70
+ // Copy back into array of bytes -- only need to worry about the bytes that were data
71
+ // We don't care about errors in the error-correction codewords
72
+ for ($ i = 0 ; $ i < $ numDataCodewords ; $ i ++){
73
+ $ codewordBytes [$ i ] = $ decoded [$ i ];
74
+ }
75
+
76
+ return $ codewordBytes ;
77
+ }
78
+
37
79
/**
38
80
* Decodes given set of received codewords, which include both data and error-correction
39
81
* codewords. Really, this means it uses Reed-Solomon to detect and correct errors, in-place,
@@ -45,7 +87,7 @@ final class ReedSolomonDecoder{
45
87
* @return int[]
46
88
* @throws \RuntimeException if decoding fails for any reason
47
89
*/
48
- public function decode (array $ received , int $ numEccCodewords ):array {
90
+ private function decodeWords (array $ received , int $ numEccCodewords ):array {
49
91
$ poly = new GenericGFPoly ($ received );
50
92
$ syndromeCoefficients = [];
51
93
$ error = false ;
0 commit comments