1 /* Example use of Reed-Solomon library
3 * Copyright Henry Minsky (hqm@alum.mit.edu) 1991-2009
5 * This software library is licensed under terms of the GNU GENERAL
8 * RSCODE is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * RSCODE is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Rscode. If not, see <http://www.gnu.org/licenses/>.
21 * Commercial licensing is available under a separate license, please
22 * contact author for details.
24 * This same code demonstrates the use of the encodier and
25 * decoder/error-correction routines.
27 * We are assuming we have at least four bytes of parity (NPAR >= 4).
29 * This gives us the ability to correct up to two errors, or
32 * In general, with E errors, and K erasures, you will need
33 * 2E + K bytes of parity to be able to correct the codeword
34 * back to recover the original message data.
36 * You could say that each error 'consumes' two bytes of the parity,
37 * whereas each erasure 'consumes' one byte.
39 * Thus, as demonstrated below, we can inject one error (location unknown)
40 * and two erasures (with their locations specified) and the
41 * error-correction routine will be able to correct the codeword
42 * back to the original message.
49 unsigned char msg
[] = "Nervously I loaded the twin ducks aboard the revolving pl\
51 unsigned char codeword
[256];
53 /* Some debugging routines to introduce errors or erasures
57 /* Introduce a byte error at LOC */
59 byte_err (int err
, int loc
, unsigned char *dst
)
61 printf("Adding Error at loc %d, data %#x\n", loc
, dst
[loc
-1]);
65 /* Pass in location of error (first byte position is
66 labeled starting at 1, not 0), and the codeword.
69 byte_erasure (int loc
, unsigned char dst
[], int cwsize
, int erasures
[])
71 printf("Erasure at loc %d, data %#x\n", loc
, dst
[loc
-1]);
77 main (int argc
, char *argv
[])
83 /* Initialization the ECC library */
89 /* Encode data into codeword, adding NPAR parity bytes */
90 encode_data(msg
, sizeof(msg
), codeword
);
92 printf("Encoded data is: \"%s\"\n", codeword
);
94 #define ML (sizeof (msg) + NPAR)
97 /* Add one error and two erasures */
98 byte_err(0x35, 3, codeword
);
100 byte_err(0x23, 17, codeword
);
101 byte_err(0x34, 19, codeword
);
104 printf("with some errors: \"%s\"\n", codeword
);
106 /* We need to indicate the position of the erasures. Eraseure
107 positions are indexed (1 based) from the end of the message... */
109 erasures
[nerasures
++] = ML
-17;
110 erasures
[nerasures
++] = ML
-19;
113 /* Now decode -- encoded codeword size must be passed */
114 decode_data(codeword
, ML
);
116 /* check if syndrome is all zeros */
117 if (check_syndrome () != 0) {
118 correct_errors_erasures (codeword
,
123 printf("Corrected codeword: \"%s\"\n", codeword
);