2 * Copyright (c) 2014, James S. Plank and Kevin Greenan
5 * Jerasure - A C/C++ Library for a Variety of Reed-Solomon and RAID-6 Erasure
8 * Revision 2.0: Galois Field backend now links to GF-Complete
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * - Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * - Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in
19 * the documentation and/or other materials provided with the
22 * - Neither the name of the University of Tennessee nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
32 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
33 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
34 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
36 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
40 /* Jerasure's authors:
42 Revision 2.x - 2014: James S. Plank and Kevin M. Greenan.
43 Revision 1.2 - 2008: James S. Plank, Scott Simmerman and Catherine D. Schuman.
44 Revision 1.0 - 2007: James S. Plank.
55 #define talloc(type, num) (type *) malloc(sizeof(type)*(num))
57 static void usage(char *s)
59 fprintf(stderr, "usage: reed_sol_01 k m w seed - Does a simple Reed-Solomon coding example in GF(2^w).\n");
60 fprintf(stderr, " \n");
61 fprintf(stderr, "w must be 8, 16 or 32. k+m must be <= 2^w. It sets up a classic\n");
62 fprintf(stderr, "Vandermonde-based generator matrix and encodes k devices of\n");
63 fprintf(stderr, "%ld bytes each with it. Then it decodes.\n", sizeof(long));
64 fprintf(stderr, " \n");
65 fprintf(stderr, "This demonstrates: jerasure_matrix_encode()\n");
66 fprintf(stderr, " jerasure_matrix_decode()\n");
67 fprintf(stderr, " jerasure_print_matrix()\n");
68 fprintf(stderr, " reed_sol_vandermonde_coding_matrix()\n");
69 if (s != NULL) fprintf(stderr, "%s\n", s);
73 static void print_data_and_coding(int k, int m, int w, int size,
74 char **data, char **coding)
81 sp = size * 2 + size/(w/8) + 8;
83 printf("%-*sCoding\n", sp, "Data");
84 for(i = 0; i < n; i++) {
87 for(j=0;j< size; j+=(w/8)) {
90 printf("%02x", (unsigned char)data[i][j+x]);
95 else printf("%*s", sp, "");
98 for(j=0;j< size; j+=(w/8)) {
100 for(x=0;x < w/8;x++){
101 printf("%02x", (unsigned char)coding[i][j+x]);
110 int main(int argc, char **argv)
115 char **data, **coding, **dcopy, **ccopy;
117 int *erasures, *erased;
120 if (argc != 5) usage(NULL);
121 if (sscanf(argv[1], "%d", &k) == 0 || k <= 0) usage("Bad k");
122 if (sscanf(argv[2], "%d", &m) == 0 || m <= 0) usage("Bad m");
123 if (sscanf(argv[3], "%d", &w) == 0 || (w != 8 && w != 16 && w != 32)) usage("Bad w");
124 if (sscanf(argv[4], "%u", &seed) == 0) usage("Bad seed");
125 if (w <= 16 && k + m > (1 << w)) usage("k + m is too big");
127 matrix = reed_sol_vandermonde_coding_matrix(k, m, w);
129 printf("<HTML><TITLE>reed_sol_01 %d %d %d %d</title>\n", k, m, w, seed);
130 printf("<h3>reed_sol_01 %d %d %d %d</h3>\n", k, m, w, seed);
132 printf("Last m rows of the generator Matrix (G^T):\n\n");
133 jerasure_print_matrix(matrix, m, k, w);
137 data = talloc(char *, k);
138 dcopy = talloc(char *, k);
139 for (i = 0; i < k; i++) {
140 data[i] = talloc(char, sizeof(long));
141 dcopy[i] = talloc(char, sizeof(long));
142 for (j = 0; j < sizeof(long); j++) {
143 uc = MOA_Random_W(8, 1);
144 data[i][j] = (char) uc;
146 memcpy(dcopy[i], data[i], sizeof(long));
149 coding = talloc(char *, m);
150 ccopy = talloc(char *, m);
151 for (i = 0; i < m; i++) {
152 coding[i] = talloc(char, sizeof(long));
153 ccopy[i] = talloc(char, sizeof(long));
156 jerasure_matrix_encode(k, m, w, matrix, data, coding, sizeof(long));
158 for (i = 0; i < m; i++) {
159 memcpy(ccopy[i], coding[i], sizeof(long));
162 printf("Encoding Complete:\n\n");
163 print_data_and_coding(k, m, w, sizeof(long), data, coding);
165 erasures = talloc(int, (m+1));
166 erased = talloc(int, (k+m));
167 for (i = 0; i < m+k; i++) erased[i] = 0;
169 for (i = 0; i < m; ) {
170 erasures[i] = MOA_Random_W(31, 0)%(k+m);
171 if (erased[erasures[i]] == 0) {
172 erased[erasures[i]] = 1;
173 memcpy((erasures[i] < k) ? data[erasures[i]] : coding[erasures[i]-k], &l, sizeof(long));
179 printf("Erased %d random devices:\n\n", m);
180 print_data_and_coding(k, m, w, sizeof(long), data, coding);
182 i = jerasure_matrix_decode(k, m, w, matrix, 1, erasures, data, coding, sizeof(long));
184 printf("State of the system after decoding:\n\n");
185 print_data_and_coding(k, m, w, sizeof(long), data, coding);
187 for (i = 0; i < k; i++) if (memcmp(data[i], dcopy[i], sizeof(long)) != 0) {
188 printf("ERROR: D%x after decoding does not match its state before decoding!<br>\n", i);
190 for (i = 0; i < m; i++) if (memcmp(coding[i], ccopy[i], sizeof(long)) != 0) {
191 printf("ERROR: C%x after decoding does not match its state before decoding!<br>\n", i);