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.
50 #include <gf_complete.h>
51 #include <gf_method.h>
60 static void *malloc16(int size) {
61 void *mem = malloc(size+16+sizeof(void*));
62 void **ptr = (void**)((long)(mem+16+sizeof(void*)) & ~(15));
69 static void free16(void *ptr) {
70 free(((void**)ptr)[-1]);
74 #define talloc(type, num) (type *) malloc16(sizeof(type)*(num))
76 static void usage(char *s)
78 fprintf(stderr, "usage: reed_sol_test_gf k m w seed (additional GF args) - Tests Reed-Solomon in GF(2^w).\n");
79 fprintf(stderr, " \n");
80 fprintf(stderr, " w must be 8, 16 or 32. k+m must be <= 2^w.\n");
81 fprintf(stderr, " See the README for information on the additional GF args.\n");
82 fprintf(stderr, " Set up a Vandermonde-based distribution matrix and encodes k devices of\n");
83 fprintf(stderr, " %d bytes each with it. Then it decodes.\n", BUFSIZE);
84 fprintf(stderr, " \n");
85 fprintf(stderr, "This tests: jerasure_matrix_encode()\n");
86 fprintf(stderr, " jerasure_matrix_decode()\n");
87 fprintf(stderr, " jerasure_print_matrix()\n");
88 fprintf(stderr, " galois_change_technique()\n");
89 fprintf(stderr, " reed_sol_vandermonde_coding_matrix()\n");
90 if (s != NULL) fprintf(stderr, "%s\n", s);
94 gf_t* get_gf(int w, int argc, char **argv, int starting)
96 gf_t *gf = (gf_t*)malloc(sizeof(gf_t));
97 if (create_gf_from_argv(gf, w, argc, argv, starting) == 0) {
104 int main(int argc, char **argv)
108 char **data, **coding, **old_values;
109 int *erasures, *erased;
113 if (argc < 6) usage("Not enough command line arguments");
114 if (sscanf(argv[1], "%d", &k) == 0 || k <= 0) usage("Bad k");
115 if (sscanf(argv[2], "%d", &m) == 0 || m <= 0) usage("Bad m");
116 if (sscanf(argv[3], "%d", &w) == 0 || (w != 8 && w != 16 && w != 32)) usage("Bad w");
117 if (sscanf(argv[4], "%d", &seed) == 0) usage("Bad seed");
118 if (w <= 16 && k + m > (1 << w)) usage("k + m is too big");
122 gf = get_gf(w, argc, argv, 5);
125 usage("Invalid arguments given for GF!\n");
128 galois_change_technique(gf, w);
130 matrix = reed_sol_vandermonde_coding_matrix(k, m, w);
132 printf("<HTML><TITLE>reed_sol_test_gf");
133 for (i = 1; i < argc; i++) printf(" %s", argv[i]);
134 printf("</TITLE>\n");
135 printf("<h3>reed_sol_test_gf");
136 for (i = 1; i < argc; i++) printf(" %s", argv[i]);
140 printf("Last m rows of the generator matrix (G^T):\n\n");
141 jerasure_print_matrix(matrix, m, k, w);
144 data = talloc(char *, k);
145 for (i = 0; i < k; i++) {
146 data[i] = talloc(char, BUFSIZE);
147 MOA_Fill_Random_Region(data[i], BUFSIZE);
150 coding = talloc(char *, m);
151 old_values = talloc(char *, m);
152 for (i = 0; i < m; i++) {
153 coding[i] = talloc(char, BUFSIZE);
154 old_values[i] = talloc(char, BUFSIZE);
157 jerasure_matrix_encode(k, m, w, matrix, data, coding, BUFSIZE);
159 erasures = talloc(int, (m+1));
160 erased = talloc(int, (k+m));
161 for (i = 0; i < m+k; i++) erased[i] = 0;
162 for (i = 0; i < m; ) {
163 erasures[i] = ((unsigned int)MOA_Random_W(w,1))%(k+m);
164 if (erased[erasures[i]] == 0) {
165 erased[erasures[i]] = 1;
166 memcpy(old_values[i], (erasures[i] < k) ? data[erasures[i]] : coding[erasures[i]-k], BUFSIZE);
167 bzero((erasures[i] < k) ? data[erasures[i]] : coding[erasures[i]-k], BUFSIZE);
173 i = jerasure_matrix_decode(k, m, w, matrix, 1, erasures, data, coding, BUFSIZE);
175 for (i = 0; i < m; i++) {
176 if (erasures[i] < k) {
177 if (memcmp(data[erasures[i]], old_values[i], BUFSIZE)) {
178 fprintf(stderr, "Decoding failed for %d!\n", erasures[i]);
182 if (memcmp(coding[erasures[i]-k], old_values[i], BUFSIZE)) {
183 fprintf(stderr, "Decoding failed for %d!\n", erasures[i]);
189 printf("Encoding and decoding were both successful.\n");