summaryrefslogtreecommitdiff
path: root/Examples
diff options
context:
space:
mode:
authorJim Plank <plank@cs.utk.edu>2013-10-01 13:25:12 -0400
committerJim Plank <plank@cs.utk.edu>2013-10-01 13:25:12 -0400
commitb5745fa4f1bdc3521153ca4fb51c6e7bae5b1511 (patch)
tree223bffef5ff6b62823a3cf97958b3447e8e4b35a /Examples
downloadjerasure-b5745fa4f1bdc3521153ca4fb51c6e7bae5b1511.tar.gz
Adding the current jerasure 1.2A
Diffstat (limited to 'Examples')
-rwxr-xr-xExamples/.swpbin0 -> 12288 bytes
-rwxr-xr-xExamples/cauchy_01.c90
-rwxr-xr-xExamples/cauchy_02.c210
-rwxr-xr-xExamples/cauchy_03.c204
-rwxr-xr-xExamples/cauchy_04.c198
-rwxr-xr-xExamples/decoder.c399
-rwxr-xr-xExamples/encoder.c636
-rwxr-xr-xExamples/jerasure_01.c88
-rwxr-xr-xExamples/jerasure_02.c86
-rwxr-xr-xExamples/jerasure_03.c113
-rwxr-xr-xExamples/jerasure_04.c111
-rwxr-xr-xExamples/jerasure_05.c217
-rwxr-xr-xExamples/jerasure_06.c220
-rwxr-xr-xExamples/jerasure_07.c206
-rwxr-xr-xExamples/jerasure_08.c214
-rwxr-xr-xExamples/liberation_01.c190
-rwxr-xr-xExamples/makefile186
-rwxr-xr-xExamples/reed_sol_01.c177
-rwxr-xr-xExamples/reed_sol_02.c101
-rwxr-xr-xExamples/reed_sol_03.c178
-rwxr-xr-xExamples/reed_sol_04.c112
21 files changed, 3936 insertions, 0 deletions
diff --git a/Examples/.swp b/Examples/.swp
new file mode 100755
index 0000000..caf0e44
--- /dev/null
+++ b/Examples/.swp
Binary files differ
diff --git a/Examples/cauchy_01.c b/Examples/cauchy_01.c
new file mode 100755
index 0000000..f13338e
--- /dev/null
+++ b/Examples/cauchy_01.c
@@ -0,0 +1,90 @@
+/* Examples/cauchy_01.c
+
+Jerasure - A C/C++ Library for a Variety of Reed-Solomon and RAID-6 Erasure Coding Techniques
+
+Revision 1.2A
+May 24, 2011
+
+James S. Plank
+Department of Electrical Engineering and Computer Science
+University of Tennessee
+Knoxville, TN 37996
+plank@cs.utk.edu
+
+Copyright (c) 2011, James S. Plank
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+ - Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ - Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+
+ - Neither the name of the University of Tennessee nor the names of its
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
+WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "jerasure.h"
+#include "reed_sol.h"
+
+#define talloc(type, num) (type *) malloc(sizeof(type)*(num))
+
+usage(char *s)
+{
+ fprintf(stderr, "usage: cauchy_01 n w - Prints the number of ones in the bitmatrix representation of n in GF(2^w).\n");
+ fprintf(stderr, " \n");
+ fprintf(stderr, " It also prints out the bit-matrix and confirms that the number of ones is correct.\n");
+ fprintf(stderr, " \n");
+ fprintf(stderr, "This demonstrates: cauchy_n_ones()\n");
+ fprintf(stderr, " jerasure_matrix_to_bitmatrix()\n");
+ fprintf(stderr, " jerasure_print_bitmatrix()\n");
+ if (s != NULL) fprintf(stderr, "%s\n", s);
+ exit(1);
+}
+
+int main(int argc, char **argv)
+{
+ int n, i, no, w;
+ int *bitmatrix;
+
+ if (argc != 3) usage(NULL);
+ if (sscanf(argv[1], "%d", &n) == 0 || n <= 0) usage("Bad n");
+ if (sscanf(argv[2], "%d", &w) == 0 || w <= 0 || w > 32) usage("Bad w");
+ if (w < 30 && n >= (1 << w)) usage("n is too big");
+
+ bitmatrix = jerasure_matrix_to_bitmatrix(1, 1, w, &n);
+ no = 0;
+ for (i = 0; i < w*w; i++) no += bitmatrix[i];
+
+ printf("# Ones: %d\n", cauchy_n_ones(n, w));
+ printf("\n");
+ printf("Bitmatrix has %d ones\n", no);
+ printf("\n");
+ jerasure_print_bitmatrix(bitmatrix, w, w, w);
+
+ return 0;
+}
diff --git a/Examples/cauchy_02.c b/Examples/cauchy_02.c
new file mode 100755
index 0000000..21f50f5
--- /dev/null
+++ b/Examples/cauchy_02.c
@@ -0,0 +1,210 @@
+/* Examples/cauchy_02.c
+Jerasure - A C/C++ Library for a Variety of Reed-Solomon and RAID-6 Erasure Coding Techniques
+
+Revision 1.2A
+May 24, 2011
+
+James S. Plank
+Department of Electrical Engineering and Computer Science
+University of Tennessee
+Knoxville, TN 37996
+plank@cs.utk.edu
+
+Copyright (c) 2011, James S. Plank
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+ - Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ - Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+
+ - Neither the name of the University of Tennessee nor the names of its
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
+WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+
+ */
+
+
+/*
+ revised by S. Simmerman
+ 2/25/08
+*/
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "jerasure.h"
+#include "cauchy.h"
+
+#define talloc(type, num) (type *) malloc(sizeof(type)*(num))
+
+usage(char *s)
+{
+ fprintf(stderr, "usage: cauchy_02 k m w - Scheduled CRS coding example with the original matrix in GF(2^w).\n");
+ fprintf(stderr, " \n");
+ fprintf(stderr, " k+m must be <= 2^w. It sets up a Cauchy distribution matrix using the original\n");
+ fprintf(stderr, " Cauchy Distribution matrix construction algorithm, then it encodes\n");
+ fprintf(stderr, " k devices of w*%d bytes using smart bit-matrix scheduling.\n", sizeof(long));
+ fprintf(stderr, " It decodes using bit-matrix scheduling as well.\n");
+ fprintf(stderr, " \n");
+ fprintf(stderr, "This demonstrates: cauchy_original_coding_matrix()\n");
+ fprintf(stderr, " cauchy_xy_coding_matrix()\n");
+ fprintf(stderr, " cauchy_n_ones()\n");
+ fprintf(stderr, " jerasure_smart_bitmatrix_to_schedule()\n");
+ fprintf(stderr, " jerasure_schedule_encode()\n");
+ fprintf(stderr, " jerasure_schedule_decode_lazy()\n");
+ fprintf(stderr, " jerasure_print_matrix()\n");
+ fprintf(stderr, " jerasure_get_stats()\n");
+ if (s != NULL) fprintf(stderr, "%s\n", s);
+ exit(1);
+}
+
+static void print_data_and_coding(int k, int m, int w, int psize,
+ char **data, char **coding)
+{
+ int i, j, x, n, sp;
+ long l;
+
+ if(k > m) n = k;
+ else n = m;
+ sp = psize * 2 + (psize/4) + 12;
+
+ printf("%-*sCoding\n", sp, "Data");
+ for(i = 0; i < n; i++) {
+ for (j = 0; j < w; j++) {
+ if(i < k) {
+
+ if(j==0) printf("D%-2d p%-2d:", i,j);
+ else printf(" p%-2d:", j);
+ for(x = 0; x < psize; x +=4) {
+ memcpy(&l, data[i]+j*psize+x, sizeof(long));
+ printf(" %08lx", l);
+ }
+ printf(" ");
+ }
+ else printf("%*s", sp, "");
+ if(i < m) {
+ if(j==0) printf("C%-2d p%-2d:", i,j);
+ else printf(" p%-2d:", j);
+ for(x = 0; x < psize; x +=4) {
+ memcpy(&l, coding[i]+j*psize+x, sizeof(long));
+ printf(" %08lx", l);
+ }
+ }
+ printf("\n");
+ }
+ }
+
+ printf("\n");
+}
+
+int main(int argc, char **argv)
+{
+ long l;
+ int k, w, i, j, m;
+ int *matrix, *bitmatrix, *m2, *x, *y;
+ char **data, **coding, **ptrs;
+ int **smart;
+ int no;
+ int *erasures, *erased;
+ double stats[3];
+
+ if (argc != 4) usage(NULL);
+ if (sscanf(argv[1], "%d", &k) == 0 || k <= 0) usage("Bad k");
+ if (sscanf(argv[2], "%d", &m) == 0 || m <= 0) usage("Bad m");
+ if (sscanf(argv[3], "%d", &w) == 0 || w <= 0 || w > 32) usage("Bad w");
+ if (w < 30 && (k+m) > (1 << w)) usage("k + m is too big");
+
+ matrix = cauchy_original_coding_matrix(k, m, w);
+ if (matrix == NULL) {
+ usage("couldn't make coding matrix");
+ }
+
+ no = 0;
+ for (i = 0; i < k*m; i++) {
+ no += cauchy_n_ones(matrix[i], w);
+ }
+ printf("Matrix has %d ones\n\n", no);
+ jerasure_print_matrix(matrix, m, k, w);
+ printf("\n", no);
+ bitmatrix = jerasure_matrix_to_bitmatrix(k, m, w, matrix);
+
+ smart = jerasure_smart_bitmatrix_to_schedule(k, m, w, bitmatrix);
+
+ srand48(0);
+ data = talloc(char *, k);
+ for (i = 0; i < k; i++) {
+ data[i] = talloc(char, sizeof(long)*w);
+ for (j = 0; j < w; j++) {
+ l = lrand48();
+ memcpy(data[i]+j*sizeof(long), &l, sizeof(long));
+ }
+ }
+
+ coding = talloc(char *, m);
+ for (i = 0; i < m; i++) {
+ coding[i] = talloc(char, sizeof(long)*w);
+ }
+
+ jerasure_schedule_encode(k, m, w, smart, data, coding, w*sizeof(long), sizeof(long));
+ jerasure_get_stats(stats);
+ printf("Smart Encoding Complete: - %.0lf XOR'd bytes\n\n", stats[0]);
+ print_data_and_coding(k, m, w, sizeof(long), data, coding);
+
+ erasures = talloc(int, (m+1));
+ erased = talloc(int, (k+m));
+ for (i = 0; i < m+k; i++) erased[i] = 0;
+ for (i = 0; i < m; ) {
+ erasures[i] = lrand48()%(k+m);
+ if (erased[erasures[i]] == 0) {
+ erased[erasures[i]] = 1;
+ bzero((erasures[i] < k) ? data[erasures[i]] : coding[erasures[i]-k], sizeof(long)*w);
+ i++;
+ }
+ }
+ erasures[i] = -1;
+
+ printf("Erased %d random devices:\n\n", m);
+ print_data_and_coding(k, m, w, sizeof(long), data, coding);
+
+ jerasure_schedule_decode_lazy(k, m, w, bitmatrix, erasures, data, coding, w*sizeof(long), sizeof(long), 1);
+ jerasure_get_stats(stats);
+
+ printf("State of the system after decoding: %.0lf XOR'd bytes\n\n", stats[0]);
+ print_data_and_coding(k, m, w, sizeof(long), data, coding);
+
+ x = talloc(int, m);
+ y = talloc(int, k);
+ if (x == NULL || y == NULL) { perror("malloc"); exit(1); }
+ for (i = 0; i < m; i++) x[i] = i;
+ for (i = 0; i < k; i++) y[i] = m+i;
+ m2 = cauchy_xy_coding_matrix(k, m, w, x, y);
+ if (memcmp(matrix, m2, sizeof(int)*k*m) != 0) {
+ printf("Error -- the matrices made by original and xy don't match\n");
+ exit(1);
+ } else {
+ printf("Generated the identical matrix using cauchy_xy_coding_matrix()\n");
+ }
+
+
+ return 0;
+}
diff --git a/Examples/cauchy_03.c b/Examples/cauchy_03.c
new file mode 100755
index 0000000..6153799
--- /dev/null
+++ b/Examples/cauchy_03.c
@@ -0,0 +1,204 @@
+/* Examples/cauchy_03.c
+ * James S. Plank
+
+Jerasure - A C/C++ Library for a Variety of Reed-Solomon and RAID-6 Erasure Coding Techniques
+
+Revision 1.2A
+May 24, 2011
+
+James S. Plank
+Department of Electrical Engineering and Computer Science
+University of Tennessee
+Knoxville, TN 37996
+plank@cs.utk.edu
+
+Copyright (c) 2011, James S. Plank
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+ - Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ - Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+
+ - Neither the name of the University of Tennessee nor the names of its
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
+WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+
+ */
+
+
+/*
+ revised by S. Simmerman
+ 2/25/08
+*/
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "jerasure.h"
+#include "cauchy.h"
+
+#define talloc(type, num) (type *) malloc(sizeof(type)*(num))
+
+usage(char *s)
+{
+ fprintf(stderr, "usage: cauchy_03 k m w - Scheduled CRS coding example with improved matrix in GF(2^w).\n");
+ fprintf(stderr, " \n");
+ fprintf(stderr, " k+m must be <= 2^w. It sets up a Cauchy distribution matrix using the original\n");
+ fprintf(stderr, " Cauchy Distribution matrix construction algorithm, then improves it\n");
+ fprintf(stderr, " with cauchy_improve_coding_matrix(). Then it does the same encoding and\n");
+ fprintf(stderr, " decoding as cauchy_02.\n");
+ fprintf(stderr, " \n");
+ fprintf(stderr, "This demonstrates: cauchy_original_coding_matrix()\n");
+ fprintf(stderr, " cauchy_improve_coding_matrix()\n");
+ fprintf(stderr, " cauchy_n_ones()\n");
+ fprintf(stderr, " jerasure_smart_bitmatrix_to_schedule()\n");
+ fprintf(stderr, " jerasure_schedule_encode()\n");
+ fprintf(stderr, " jerasure_schedule_decode_lazy()\n");
+ fprintf(stderr, " jerasure_print_matrix()\n");
+ fprintf(stderr, " jerasure_get_stats()\n");
+ if (s != NULL) fprintf(stderr, "%s\n", s);
+ exit(1);
+}
+
+static void print_data_and_coding(int k, int m, int w, int psize,
+ char **data, char **coding)
+{
+ int i, j, x, n, sp;
+ long l;
+
+ if(k > m) n = k;
+ else n = m;
+ sp = psize * 2 + (psize/4) + 12;
+
+ printf("%-*sCoding\n", sp, "Data");
+ for(i = 0; i < n; i++) {
+ for (j = 0; j < w; j++) {
+ if(i < k) {
+
+ if(j==0) printf("D%-2d p%-2d:", i,j);
+ else printf(" p%-2d:", j);
+ for(x = 0; x < psize; x +=4) {
+ memcpy(&l, data[i]+j*psize+x, sizeof(long));
+ printf(" %08lx", l);
+ }
+ printf(" ");
+ }
+ else printf("%*s", sp, "");
+ if(i < m) {
+ if(j==0) printf("C%-2d p%-2d:", i,j);
+ else printf(" p%-2d:", j);
+ for(x = 0; x < psize; x +=4) {
+ memcpy(&l, coding[i]+j*psize+x, sizeof(long));
+ printf(" %08lx", l);
+ }
+ }
+ printf("\n");
+ }
+ }
+
+ printf("\n");
+}
+
+int main(int argc, char **argv)
+{
+ long l;
+ int k, w, i, j, m;
+ int *matrix, *bitmatrix, *m2, *x, *y;
+ char **data, **coding, **ptrs;
+ int **smart;
+ int no;
+ int *erasures, *erased;
+ double stats[3];
+
+ if (argc != 4) usage(NULL);
+ if (sscanf(argv[1], "%d", &k) == 0 || k <= 0) usage("Bad k");
+ if (sscanf(argv[2], "%d", &m) == 0 || m <= 0) usage("Bad m");
+ if (sscanf(argv[3], "%d", &w) == 0 || w <= 0 || w > 32) usage("Bad w");
+ if (w < 30 && (k+m) > (1 << w)) usage("k + m is too big");
+
+ matrix = cauchy_original_coding_matrix(k, m, w);
+ if (matrix == NULL) {
+ usage("couldn't make coding matrix");
+ }
+
+ no = 0;
+ for (i = 0; i < k*m; i++) {
+ no += cauchy_n_ones(matrix[i], w);
+ }
+ printf("The Original Matrix has %d ones\n", no);
+ cauchy_improve_coding_matrix(k, m, w, matrix);
+ no = 0;
+ for (i = 0; i < k*m; i++) {
+ no += cauchy_n_ones(matrix[i], w);
+ }
+ printf("The Improved Matrix has %d ones\n\n", no);
+ jerasure_print_matrix(matrix, m, k, w);
+ printf("\n", no);
+ bitmatrix = jerasure_matrix_to_bitmatrix(k, m, w, matrix);
+
+ smart = jerasure_smart_bitmatrix_to_schedule(k, m, w, bitmatrix);
+
+ srand48(0);
+ data = talloc(char *, k);
+ for (i = 0; i < k; i++) {
+ data[i] = talloc(char, sizeof(long)*w);
+ for (j = 0; j < w; j++) {
+ l = lrand48();
+ memcpy(data[i]+j*sizeof(long), &l, sizeof(long));
+ }
+ }
+
+ coding = talloc(char *, m);
+ for (i = 0; i < m; i++) {
+ coding[i] = talloc(char, sizeof(long)*w);
+ }
+
+ jerasure_schedule_encode(k, m, w, smart, data, coding, w*sizeof(long), sizeof(long));
+ jerasure_get_stats(stats);
+ printf("Smart Encoding Complete: - %.0lf XOR'd bytes\n\n", stats[0]);
+ print_data_and_coding(k, m, w, sizeof(long), data, coding);
+
+ erasures = talloc(int, (m+1));
+ erased = talloc(int, (k+m));
+ for (i = 0; i < m+k; i++) erased[i] = 0;
+ for (i = 0; i < m; ) {
+ erasures[i] = lrand48()%(k+m);
+ if (erased[erasures[i]] == 0) {
+ erased[erasures[i]] = 1;
+ bzero((erasures[i] < k) ? data[erasures[i]] : coding[erasures[i]-k], sizeof(long)*w);
+ i++;
+ }
+ }
+ erasures[i] = -1;
+
+ printf("Erased %d random devices:\n\n", m);
+ print_data_and_coding(k, m, w, sizeof(long), data, coding);
+
+ jerasure_schedule_decode_lazy(k, m, w, bitmatrix, erasures, data, coding, w*sizeof(long), sizeof(long), 1);
+ jerasure_get_stats(stats);
+
+ printf("State of the system after decoding: %.0lf XOR'd bytes\n\n", stats[0]);
+ print_data_and_coding(k, m, w, sizeof(long), data, coding);
+
+ return 0;
+}
diff --git a/Examples/cauchy_04.c b/Examples/cauchy_04.c
new file mode 100755
index 0000000..94ee3c0
--- /dev/null
+++ b/Examples/cauchy_04.c
@@ -0,0 +1,198 @@
+/* Examples/cauchy_04.c
+ * James S. Plank
+
+Jerasure - A C/C++ Library for a Variety of Reed-Solomon and RAID-6 Erasure Coding Techniques
+
+Revision 1.2A
+May 24, 2011
+
+James S. Plank
+Department of Electrical Engineering and Computer Science
+University of Tennessee
+Knoxville, TN 37996
+plank@cs.utk.edu
+
+Copyright (c) 2011, James S. Plank
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+ - Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ - Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+
+ - Neither the name of the University of Tennessee nor the names of its
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
+WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+
+
+ */
+
+
+/*
+ revised by S. Simmerman
+ 2/25/08
+*/
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "jerasure.h"
+#include "cauchy.h"
+
+#define talloc(type, num) (type *) malloc(sizeof(type)*(num))
+
+usage(char *s)
+{
+ fprintf(stderr, "usage: cauchy_04 k m w - Scheduled CRS coding example with a good matrix in GF(2^w).\n");
+ fprintf(stderr, " \n");
+ fprintf(stderr, " k+m must be <= 2^w. It sets up a Cauchy distribution matrix using \n");
+ fprintf(stderr, " cauchy_good_coding_matrix(), then it encodes\n");
+ fprintf(stderr, " k devices of w*%d bytes using smart bit-matrix scheduling.\n", sizeof(long));
+ fprintf(stderr, " It decodes using bit-matrix scheduling as well.\n");
+ fprintf(stderr, " \n");
+ fprintf(stderr, "This demonstrates: cauchy_original_coding_matrix()\n");
+ fprintf(stderr, " cauchy_n_ones()\n");
+ fprintf(stderr, " jerasure_smart_bitmatrix_to_schedule()\n");
+ fprintf(stderr, " jerasure_schedule_encode()\n");
+ fprintf(stderr, " jerasure_schedule_decode_lazy()\n");
+ fprintf(stderr, " jerasure_print_matrix()\n");
+ fprintf(stderr, " jerasure_get_stats()\n");
+ if (s != NULL) fprintf(stderr, "%s\n", s);
+ exit(1);
+}
+
+static void print_data_and_coding(int k, int m, int w, int psize,
+ char **data, char **coding)
+{
+ int i, j, x, n, sp;
+ long l;
+
+ if(k > m) n = k;
+ else n = m;
+ sp = psize * 2 + (psize/4) + 12;
+
+ printf("%-*sCoding\n", sp, "Data");
+ for(i = 0; i < n; i++) {
+ for (j = 0; j < w; j++) {
+ if(i < k) {
+
+ if(j==0) printf("D%-2d p%-2d:", i,j);
+ else printf(" p%-2d:", j);
+ for(x = 0; x < psize; x +=4) {
+ memcpy(&l, data[i]+j*psize+x, sizeof(long));
+ printf(" %08lx", l);
+ }
+ printf(" ");
+ }
+ else printf("%*s", sp, "");
+ if(i < m) {
+ if(j==0) printf("C%-2d p%-2d:", i,j);
+ else printf(" p%-2d:", j);
+ for(x = 0; x < psize; x +=4) {
+ memcpy(&l, coding[i]+j*psize+x, sizeof(long));
+ printf(" %08lx", l);
+ }
+ }
+ printf("\n");
+ }
+ }
+
+ printf("\n");
+}
+
+int main(int argc, char **argv)
+{
+ long l;
+ int k, w, i, j, m;
+ int *matrix, *bitmatrix;
+ char **data, **coding, **ptrs;
+ int **smart;
+ int no;
+ int *erasures, *erased;
+ double stats[3];
+
+ if (argc != 4) usage(NULL);
+ if (sscanf(argv[1], "%d", &k) == 0 || k <= 0) usage("Bad k");
+ if (sscanf(argv[2], "%d", &m) == 0 || m <= 0) usage("Bad m");
+ if (sscanf(argv[3], "%d", &w) == 0 || w <= 0 || w > 32) usage("Bad w");
+ if (w < 30 && (k+m) > (1 << w)) usage("k + m is too big");
+
+ matrix = cauchy_good_general_coding_matrix(k, m, w);
+ if (matrix == NULL) {
+ usage("couldn't make coding matrix");
+ }
+
+ no = 0;
+ for (i = 0; i < k*m; i++) {
+ no += cauchy_n_ones(matrix[i], w);
+ }
+ printf("Matrix has %d ones\n\n", no);
+ jerasure_print_matrix(matrix, m, k, w);
+ printf("\n");
+ bitmatrix = jerasure_matrix_to_bitmatrix(k, m, w, matrix);
+
+ smart = jerasure_smart_bitmatrix_to_schedule(k, m, w, bitmatrix);
+
+ srand48(0);
+ data = talloc(char *, k);
+ for (i = 0; i < k; i++) {
+ data[i] = talloc(char, sizeof(long)*w);
+ for (j = 0; j < w; j++) {
+ l = lrand48();
+ memcpy(data[i]+j*sizeof(long), &l, sizeof(long));
+ }
+ }
+
+ coding = talloc(char *, m);
+ for (i = 0; i < m; i++) {
+ coding[i] = talloc(char, sizeof(long)*w);
+ }
+
+ jerasure_schedule_encode(k, m, w, smart, data, coding, w*sizeof(long), sizeof(long));
+ jerasure_get_stats(stats);
+ printf("Smart Encoding Complete: - %.0lf XOR'd bytes\n\n", stats[0]);
+ print_data_and_coding(k, m, w, sizeof(long), data, coding);
+
+ erasures = talloc(int, (m+1));
+ erased = talloc(int, (k+m));
+ for (i = 0; i < m+k; i++) erased[i] = 0;
+ for (i = 0; i < m; ) {
+ erasures[i] = lrand48()%(k+m);
+ if (erased[erasures[i]] == 0) {
+ erased[erasures[i]] = 1;
+ bzero((erasures[i] < k) ? data[erasures[i]] : coding[erasures[i]-k], sizeof(long)*w);
+ i++;
+ }
+ }
+ erasures[i] = -1;
+
+ printf("Erased %d random pieces of data/coding:\n\n", m);
+ print_data_and_coding(k, m, w, sizeof(long), data, coding);
+
+ jerasure_schedule_decode_lazy(k, m, w, bitmatrix, erasures, data, coding, w*sizeof(long), sizeof(long), 1);
+ jerasure_get_stats(stats);
+
+ printf("State of the system after decoding: %.0lf XOR'd bytes\n\n", stats[0]);
+ print_data_and_coding(k, m, w, sizeof(long), data, coding);
+
+ return 0;
+}
diff --git a/Examples/decoder.c b/Examples/decoder.c
new file mode 100755
index 0000000..9194d8a
--- /dev/null
+++ b/Examples/decoder.c
@@ -0,0 +1,399 @@
+/* Examples/decoder.c
+ * Catherine D. Schuman, James S. Plank
+
+Jerasure - A C/C++ Library for a Variety of Reed-Solomon and RAID-6 Erasure Coding Techniques
+
+Revision 1.2A
+May 24, 2011
+
+James S. Plank
+Department of Electrical Engineering and Computer Science
+University of Tennessee
+Knoxville, TN 37996
+plank@cs.utk.edu
+
+Copyright (c) 2011, James S. Plank
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+ - Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ - Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+
+ - Neither the name of the University of Tennessee nor the names of its
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
+WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+
+
+*/
+
+/*
+This program takes as input an inputfile, k, m, a coding
+technique, w, and packetsize. It is the companion program
+of encoder.c, which creates k+m files. This program assumes
+that up to m erasures have occurred in the k+m files. It
+reads in the k+m files or marks the file as erased. It then
+recreates the original file and creates a new file with the
+suffix "decoded" with the decoded contents of the file.
+
+This program does not error check command line arguments because
+it is assumed that encoder.c has been called previously with the
+same arguments, and encoder.c does error check.
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/time.h>
+#include <sys/stat.h>
+#include <signal.h>
+#include "jerasure.h"
+#include "reed_sol.h"
+#include "galois.h"
+#include "cauchy.h"
+#include "liberation.h"
+
+#define N 10
+
+enum Coding_Technique {Reed_Sol_Van, Reed_Sol_R6_Op, Cauchy_Orig, Cauchy_Good, Liberation, Blaum_Roth, Liber8tion, RDP, EVENODD, No_Coding};
+
+char *Methods[N] = {"reed_sol_van", "reed_sol_r6_op", "cauchy_orig", "cauchy_good", "liberation", "blaum_roth", "liber8tion", "rdp", "evenodd", "no_coding"};
+
+/* Global variables for signal handler */
+enum Coding_Technique method;
+int readins, n;
+
+/* Function prototype */
+void ctrl_bs_handler(int dummy);
+
+int main (int argc, char **argv) {
+ FILE *fp; // File pointer
+
+ /* Jerasure arguments */
+ char **data;
+ char **coding;
+ int *erasures;
+ int *erased;
+ int *matrix;
+ int *bitmatrix;
+
+ /* Parameters */
+ int k, m, w, packetsize, buffersize;
+ enum Coding_Technique tech;
+ char *c_tech;
+
+ int i, j; // loop control variables
+ int blocksize; // size of individual files
+ int origsize; // size of file before padding
+ int total; // used to write data, not padding to file
+ struct stat status; // used to find size of individual files
+ int numerased; // number of erased files
+
+ /* Used to recreate file names */
+ char *temp;
+ char *cs1, *cs2, *extension;
+ char *fname;
+ int md;
+ char *curdir;
+
+ /* Used to time decoding */
+ struct timeval t1, t2, t3, t4;
+ struct timezone tz;
+ double tsec;
+ double totalsec;
+
+
+ signal(SIGQUIT, ctrl_bs_handler);
+
+ matrix = NULL;
+ bitmatrix = NULL;
+ totalsec = 0.0;
+
+ /* Start timing */
+ gettimeofday(&t1, &tz);
+
+ /* Error checking parameters */
+ if (argc != 2) {
+ fprintf(stderr, "usage: inputfile\n");
+ exit(0);
+ }
+ curdir = (char *)malloc(sizeof(char)*100);
+ getcwd(curdir, 100);
+
+ /* Begin recreation of file names */
+ cs1 = (char*)malloc(sizeof(char)*strlen(argv[1]));
+ cs2 = strrchr(argv[1], '/');
+ if (cs2 != NULL) {
+ cs2++;
+ strcpy(cs1, cs2);
+ }
+ else {
+ strcpy(cs1, argv[1]);
+ }
+ cs2 = strchr(cs1, '.');
+ if (cs2 != NULL) {
+ extension = strdup(cs2);
+ *cs2 = '\0';
+ } else {
+ extension = strdup("");
+ }
+ fname = (char *)malloc(sizeof(char*)*(100+strlen(argv[1])+10));
+
+ /* Read in parameters from metadata file */
+ sprintf(fname, "%s/Coding/%s_meta.txt", curdir, cs1);
+
+ fp = fopen(fname, "rb");
+ if (fp == NULL) {
+ fprintf(stderr, "Error: no metadata file %s\n", fname);
+ exit(1);
+ }
+ temp = (char *)malloc(sizeof(char)*(strlen(argv[1])+10));
+ fscanf(fp, "%s", temp);
+
+ if (fscanf(fp, "%d", &origsize) != 1) {
+ fprintf(stderr, "Original size is not valid\n");
+ exit(0);
+ }
+ if (fscanf(fp, "%d %d %d %d %d", &k, &m, &w, &packetsize, &buffersize) != 5) {
+ fprintf(stderr, "Parameters are not correct\n");
+ exit(0);
+ }
+ c_tech = (char *)malloc(sizeof(char)*(strlen(argv[1])+10));
+ fscanf(fp, "%s", c_tech);
+ fscanf(fp, "%d", &tech);
+ method = tech;
+ fscanf(fp, "%d", &readins);
+ fclose(fp);
+
+ /* Allocate memory */
+ erased = (int *)malloc(sizeof(int)*(k+m));
+ for (i = 0; i < k+m; i++)
+ erased[i] = 0;
+ erasures = (int *)malloc(sizeof(int)*(k+m));
+
+ data = (char **)malloc(sizeof(char *)*k);
+ coding = (char **)malloc(sizeof(char *)*m);
+ if (buffersize != origsize) {
+ for (i = 0; i < k; i++) {
+ data[i] = (char *)malloc(sizeof(char)*(buffersize/k));
+ }
+ for (i = 0; i < m; i++) {
+ coding[i] = (char *)malloc(sizeof(char)*(buffersize/k));
+ }
+ blocksize = buffersize/k;
+ }
+
+ sprintf(temp, "%d", k);
+ md = strlen(temp);
+ gettimeofday(&t3, &tz);
+
+ /* Create coding matrix or bitmatrix */
+ switch(tech) {
+ case No_Coding:
+ break;
+ case Reed_Sol_Van:
+ matrix = reed_sol_vandermonde_coding_matrix(k, m, w);
+ break;
+ case Reed_Sol_R6_Op:
+ matrix = reed_sol_r6_coding_matrix(k, w);
+ break;
+ case Cauchy_Orig:
+ matrix = cauchy_original_coding_matrix(k, m, w);
+ bitmatrix = jerasure_matrix_to_bitmatrix(k, m, w, matrix);
+ break;
+ case Cauchy_Good:
+ matrix = cauchy_good_general_coding_matrix(k, m, w);
+ bitmatrix = jerasure_matrix_to_bitmatrix(k, m, w, matrix);
+ break;
+ case Liberation:
+ bitmatrix = liberation_coding_bitmatrix(k, w);
+ break;
+ case Blaum_Roth:
+ bitmatrix = blaum_roth_coding_bitmatrix(k, w);
+ break;
+ case Liber8tion:
+ bitmatrix = liber8tion_coding_bitmatrix(k);
+ }
+ gettimeofday(&t4, &tz);
+ tsec = 0.0;
+ tsec += t4.tv_usec;
+ tsec -= t3.tv_usec;
+ tsec /= 1000000.0;
+ tsec += t4.tv_sec;
+ tsec -= t3.tv_sec;
+ totalsec += tsec;
+
+ /* Begin decoding process */
+ total = 0;
+ n = 1;
+ while (n <= readins) {
+ numerased = 0;
+ /* Open files, check for erasures, read in data/coding */
+ for (i = 1; i <= k; i++) {
+ sprintf(fname, "%s/Coding/%s_k%0*d%s", curdir, cs1, md, i, extension);
+ fp = fopen(fname, "rb");
+ if (fp == NULL) {
+ erased[i-1] = 1;
+ erasures[numerased] = i-1;
+ numerased++;
+ //printf("%s failed\n", fname);
+ }
+ else {
+ if (buffersize == origsize) {
+ stat(fname, &status);
+ blocksize = status.st_size;
+ data[i-1] = (char *)malloc(sizeof(char)*blocksize);
+ fread(data[i-1], sizeof(char), blocksize, fp);
+ }
+ else {
+ fseek(fp, blocksize*(n-1), SEEK_SET);
+ fread(data[i-1], sizeof(char), buffersize/k, fp);
+ }
+ fclose(fp);
+ }
+ }
+ for (i = 1; i <= m; i++) {
+ sprintf(fname, "%s/Coding/%s_m%0*d%s", curdir, cs1, md, i, extension);
+ fp = fopen(fname, "rb");
+ if (fp == NULL) {
+ erased[k+(i-1)] = 1;
+ erasures[numerased] = k+i-1;
+ numerased++;
+ //printf("%s failed\n", fname);
+ }
+ else {
+ if (buffersize == origsize) {
+ stat(fname, &status);
+ blocksize = status.st_size;
+ coding[i-1] = (char *)malloc(sizeof(char)*blocksize);
+ fread(coding[i-1], sizeof(char), blocksize, fp);
+ }
+ else {
+ fseek(fp, blocksize*(n-1), SEEK_SET);
+ fread(coding[i-1], sizeof(char), blocksize, fp);
+ }
+ fclose(fp);
+ }
+ }
+ /* Finish allocating data/coding if needed */
+ if (n == 1) {
+ for (i = 0; i < numerased; i++) {
+ if (erasures[i] < k) {
+ data[erasures[i]] = (char *)malloc(sizeof(char)*blocksize);
+ }
+ else {
+ coding[erasures[i]-k] = (char *)malloc(sizeof(char)*blocksize);
+ }
+ }
+ }
+
+ erasures[numerased] = -1;
+ gettimeofday(&t3, &tz);
+
+ /* Choose proper decoding method */
+ if (tech == Reed_Sol_Van || tech == Reed_Sol_R6_Op) {
+ i = jerasure_matrix_decode(k, m, w, matrix, 1, erasures, data, coding, blocksize);
+ }
+ else if (tech == Cauchy_Orig || tech == Cauchy_Good || tech == Liberation || tech == Blaum_Roth || tech == Liber8tion) {
+ i = jerasure_schedule_decode_lazy(k, m, w, bitmatrix, erasures, data, coding, blocksize, packetsize, 1);
+ }
+ else {
+ fprintf(stderr, "Not a valid coding technique.\n");
+ exit(0);
+ }
+ gettimeofday(&t4, &tz);
+
+ /* Exit if decoding was unsuccessful */
+ if (i == -1) {
+ fprintf(stderr, "Unsuccessful!\n");
+ exit(0);
+ }
+
+ /* Create decoded file */
+ sprintf(fname, "%s/Coding/%s_decoded%s", curdir, cs1, extension);
+ if (n == 1) {
+ fp = fopen(fname, "wb");
+ }
+ else {
+ fp = fopen(fname, "ab");
+ }
+ for (i = 0; i < k; i++) {
+ if (total+blocksize <= origsize) {
+ fwrite(data[i], sizeof(char), blocksize, fp);
+ total+= blocksize;
+ }
+ else {
+ for (j = 0; j < blocksize; j++) {
+ if (total < origsize) {
+ fprintf(fp, "%c", data[i][j]);
+ total++;
+ }
+ else {
+ break;
+ }
+
+ }
+ }
+ }
+ n++;
+ fclose(fp);
+ tsec = 0.0;
+ tsec += t4.tv_usec;
+ tsec -= t3.tv_usec;
+ tsec /= 1000000.0;
+ tsec += t4.tv_sec;
+ tsec -= t3.tv_sec;
+ totalsec += tsec;
+ }
+
+ /* Free allocated memory */
+ free(cs1);
+ free(extension);
+ free(fname);
+ free(data);
+ free(coding);
+ free(erasures);
+ free(erased);
+
+ /* Stop timing and print time */
+ gettimeofday(&t2, &tz);
+ tsec = 0;
+ tsec += t2.tv_usec;
+ tsec -= t1.tv_usec;
+ tsec /= 1000000.0;
+ tsec += t2.tv_sec;
+ tsec -= t1.tv_sec;
+ printf("Decoding (MB/sec): %0.10f\n", (origsize/1024/1024)/totalsec);
+ printf("De_Total (MB/sec): %0.10f\n\n", (origsize/1024/1024)/tsec);
+}
+
+void ctrl_bs_handler(int dummy) {
+ time_t mytime;
+ mytime = time(0);
+ fprintf(stderr, "\n%s\n", ctime(&mytime));
+ fprintf(stderr, "You just typed ctrl-\\ in decoder.c\n");
+ fprintf(stderr, "Total number of read ins = %d\n", readins);
+ fprintf(stderr, "Current read in: %d\n", n);
+ fprintf(stderr, "Method: %s\n\n", Methods[method]);
+ signal(SIGQUIT, ctrl_bs_handler);
+}
diff --git a/Examples/encoder.c b/Examples/encoder.c
new file mode 100755
index 0000000..11eb9c1
--- /dev/null
+++ b/Examples/encoder.c
@@ -0,0 +1,636 @@
+/* Examples/encoder.c
+ * Catherine D. Schuman, James S. Plank
+
+Jerasure - A C/C++ Library for a Variety of Reed-Solomon and RAID-6 Erasure Coding Techniques
+
+Revision 1.2A
+May 24, 2011
+
+James S. Plank
+Department of Electrical Engineering and Computer Science
+University of Tennessee
+Knoxville, TN 37996
+plank@cs.utk.edu
+
+Copyright (c) 2011, James S. Plank
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+ - Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ - Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+
+ - Neither the name of the University of Tennessee nor the names of its
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
+WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+
+
+ */
+
+/*
+
+This program takes as input an inputfile, k, m, a coding
+technique, w, and packetsize. It creates k+m files from
+the original file so that k of these files are parts of
+the original file and m of the files are encoded based on
+the given coding technique. The format of the created files
+is the file name with "_k#" or "_m#" and then the extension.
+(For example, inputfile test.txt would yield file "test_k1.txt".)
+
+ */
+#include <sys/time.h>
+#include <sys/stat.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <signal.h>
+#include "jerasure.h"
+#include "reed_sol.h"
+#include "galois.h"
+#include "cauchy.h"
+#include "liberation.h"
+
+#define N 10
+
+enum Coding_Technique {Reed_Sol_Van, Reed_Sol_R6_Op, Cauchy_Orig, Cauchy_Good, Liberation, Blaum_Roth, Liber8tion, RDP, EVENODD, No_Coding};
+
+char *Methods[N] = {"reed_sol_van", "reed_sol_r6_op", "cauchy_orig", "cauchy_good", "liberation", "blaum_roth", "liber8tion", "no_coding"};
+
+/* Global variables for signal handler */
+int readins, n;
+enum Coding_Technique method;
+
+/* Function prototypes */
+int is_prime(int w);
+void ctrl_bs_handler(int dummy);
+
+int jfread(void *ptr, int size, int nmembers, FILE *stream)
+{
+ int nd;
+ int *li, i;
+ if (stream != NULL) return fread(ptr, size, nmembers, stream);
+
+ nd = size/sizeof(int);
+ li = (int *) ptr;
+ for (i = 0; i < nd; i++) li[i] = mrand48();
+ return size;
+}
+
+
+int main (int argc, char **argv) {
+ FILE *fp, *fp2; // file pointers
+ char *memblock; // reading in file
+ char *block; // padding file
+ int size, newsize; // size of file and temp size
+ struct stat status; // finding file size
+
+
+ enum Coding_Technique tech; // coding technique (parameter)
+ int k, m, w, packetsize; // parameters
+ int buffersize; // paramter
+ int i, j; // loop control variables
+ int blocksize; // size of k+m files
+ int total;
+ int extra;
+
+ /* Jerasure Arguments */
+ char **data;
+ char **coding;
+ int *matrix;
+ int *bitmatrix;
+ int **schedule;
+ int *erasure;
+ int *erased;
+
+ /* Creation of file name variables */
+ char temp[5];
+ char *s1, *s2, *extension;
+ char *fname;
+ int md;
+ char *curdir;
+
+ /* Timing variables */
+ struct timeval t1, t2, t3, t4;
+ struct timezone tz;
+ double tsec;
+ double totalsec;
+ struct timeval start, stop;
+
+ /* Find buffersize */
+ int up, down;
+
+
+ signal(SIGQUIT, ctrl_bs_handler);
+
+ /* Start timing */
+ gettimeofday(&t1, &tz);
+ totalsec = 0.0;
+ matrix = NULL;
+ bitmatrix = NULL;
+ schedule = NULL;
+
+ /* Error check Arguments*/
+ if (argc != 8) {
+ fprintf(stderr, "usage: inputfile k m coding_technique w (packetsize) (buffersize)\n");
+ fprintf(stderr, "\nChoose one of the following coding techniques: \nreed_sol_van, \nreed_sol_r6_op, \ncauchy_orig, \ncauchy_good, \nliberation, \nblaum_roth, \nliber8tion");
+ exit(0);
+ }
+ /* Conversion of parameters and error checking */
+ if (sscanf(argv[2], "%d", &k) == 0 || k <= 0) {
+ fprintf(stderr, "Invalid value for k\n");
+ exit(0);
+ }
+ if (sscanf(argv[3], "%d", &m) == 0 || m < 0) {
+ fprintf(stderr, "Invalid value for m\n");
+ exit(0);
+ }
+ if (sscanf(argv[5],"%d", &w) == 0 || w <= 0) {
+ fprintf(stderr, "Invalid value for w.\n");
+ exit(0);
+ }
+ if (argc == 6) {
+ packetsize = 0;
+ }
+ else {
+ if (sscanf(argv[6], "%d", &packetsize) == 0 || packetsize < 0) {
+ fprintf(stderr, "Invalid value for packetsize.\n");
+ exit(0);
+ }
+ }
+ if (argc != 8) {
+ buffersize = 0;
+ }
+ else {
+ if (sscanf(argv[7], "%d", &buffersize) == 0 || buffersize < 0) {
+ fprintf(stderr, "Invalid value for buffersize\n");
+ exit(0);
+ }
+
+ }
+
+ /* Determine proper buffersize by finding the closest valid buffersize to the input value */
+ if (buffersize != 0) {
+ if (packetsize != 0 && buffersize%(sizeof(int)*w*k*packetsize) != 0) {
+ up = buffersize;
+ down = buffersize;
+ while (up%(sizeof(int)*w*k*packetsize) != 0 && (down%(sizeof(int)*w*k*packetsize) != 0)) {
+ up++;
+ if (down == 0) {
+ down--;
+ }
+ }
+ if (up%(sizeof(int)*w*k*packetsize) == 0) {
+ buffersize = up;
+ }
+ else {
+ if (down != 0) {
+ buffersize = down;
+ }
+ }
+ }
+ else if (packetsize == 0 && buffersize%(sizeof(int)*w*k) != 0) {
+ up = buffersize;
+ down = buffersize;
+ while (up%(sizeof(int)*w*k) != 0 && down%(sizeof(int)*w*k) != 0) {
+ up++;
+ down--;
+ }
+ if (up%(sizeof(int)*w*k) == 0) {
+ buffersize = up;
+ }
+ else {
+ buffersize = down;
+ }
+ }
+ }
+
+ /* Setting of coding technique and error checking */
+
+ if (strcmp(argv[4], "no_coding") == 0) {
+ tech = No_Coding;
+ }
+ else if (strcmp(argv[4], "reed_sol_van") == 0) {
+ tech = Reed_Sol_Van;
+ if (w != 8 && w != 16 && w != 32) {
+ fprintf(stderr, "w must be one of {8, 16, 32}\n");
+ exit(0);
+ }
+ }
+ else if (strcmp(argv[4], "reed_sol_r6_op") == 0) {
+ if (m != 2) {
+ fprintf(stderr, "m must be equal to 2\n");
+ exit(0);
+ }
+ if (w != 8 && w != 16 && w != 32) {
+ fprintf(stderr, "w must be one of {8, 16, 32}\n");
+ exit(0);
+ }
+ tech = Reed_Sol_R6_Op;
+ }
+ else if (strcmp(argv[4], "cauchy_orig") == 0) {
+ tech = Cauchy_Orig;
+ if (packetsize == 0) {
+ fprintf(stderr, "Must include packetsize.\n");
+ exit(0);
+ }
+ }
+ else if (strcmp(argv[4], "cauchy_good") == 0) {
+ tech = Cauchy_Good;
+ if (packetsize == 0) {
+ fprintf(stderr, "Must include packetsize.\n");
+ exit(0);
+ }
+ }
+ else if (strcmp(argv[4], "liberation") == 0) {
+ if (k > w) {
+ fprintf(stderr, "k must be less than or equal to w\n");
+ exit(0);
+ }
+ if (w <= 2 || !(w%2) || !is_prime(w)) {
+ fprintf(stderr, "w must be greater than two and w must be prime\n");
+ exit(0);
+ }
+ if (packetsize == 0) {
+ fprintf(stderr, "Must include packetsize.\n");
+ exit(0);
+ }
+ if ((packetsize%(sizeof(int))) != 0) {
+ fprintf(stderr, "packetsize must be a multiple of sizeof(int)\n");
+ exit(0);
+ }
+ tech = Liberation;
+ }
+ else if (strcmp(argv[4], "blaum_roth") == 0) {
+ if (k > w) {
+ fprintf(stderr, "k must be less than or equal to w\n");
+ exit(0);
+ }
+ if (w <= 2 || !((w+1)%2) || !is_prime(w+1)) {
+ fprintf(stderr, "w must be greater than two and w+1 must be prime\n");
+ exit(0);
+ }
+ if (packetsize == 0) {
+ fprintf(stderr, "Must include packetsize.\n");
+ exit(0);
+ }
+ if ((packetsize%(sizeof(int))) != 0) {
+ fprintf(stderr, "packetsize must be a multiple of sizeof(int)\n");
+ exit(0);
+ }
+ tech = Blaum_Roth;
+ }
+ else if (strcmp(argv[4], "liber8tion") == 0) {
+ if (packetsize == 0) {
+ fprintf(stderr, "Must include packetsize\n");
+ exit(0);
+ }
+ if (w != 8) {
+ fprintf(stderr, "w must equal 8\n");
+ exit(0);
+ }
+ if (m != 2) {
+ fprintf(stderr, "m must equal 2\n");
+ exit(0);
+ }
+ if (k > w) {
+ fprintf(stderr, "k must be less than or equal to w\n");
+ exit(0);
+ }
+ tech = Liber8tion;
+ }
+ else {
+ fprintf(stderr, "Not a valid coding technique. Choose one of the following: reed_sol_van, reed_sol_r6_op, cauchy_orig, cauchy_good, liberation, blaum_roth, liber8tion, no_coding\n");
+ exit(0);
+ }
+
+ /* Set global variable method for signal handler */
+ method = tech;
+
+ /* Get current working directory for construction of file names */
+ curdir = (char*)malloc(sizeof(char)*1000);
+ getcwd(curdir, 1000);
+
+ if (argv[1][0] != '-') {
+
+ /* Open file and error check */
+ fp = fopen(argv[1], "rb");
+ if (fp == NULL) {
+ fprintf(stderr, "Unable to open file.\n");
+ exit(0);
+ }
+
+ /* Create Coding directory */
+ i = mkdir("Coding", S_IRWXU);
+ if (i == -1 && errno != EEXIST) {
+ fprintf(stderr, "Unable to create Coding directory.\n");
+ exit(0);
+ }
+
+ /* Determine original size of file */
+ stat(argv[1], &status);
+ size = status.st_size;
+ } else {
+ if (sscanf(argv[1]+1, "%d", &size) != 1 || size <= 0) {
+ fprintf(stderr, "Files starting with '-' should be sizes for randomly created input\n");
+ exit(1);
+ }
+ fp = NULL;
+ srand48(time(0));
+ }
+
+ newsize = size;
+
+ /* Find new size by determining next closest multiple */
+ if (packetsize != 0) {
+ if (size%(k*w*packetsize*sizeof(int)) != 0) {
+ while (newsize%(k*w*packetsize*sizeof(int)) != 0)
+ newsize++;
+ }
+ }
+ else {
+ if (size%(k*w*sizeof(int)) != 0) {
+ while (newsize%(k*w*sizeof(int)) != 0)
+ newsize++;
+ }
+ }
+
+ if (buffersize != 0) {
+ while (newsize%buffersize != 0) {
+ newsize++;
+ }
+ }
+
+
+ /* Determine size of k+m files */
+ blocksize = newsize/k;
+
+ /* Allow for buffersize and determine number of read-ins */
+ if (size > buffersize && buffersize != 0) {
+ if (newsize%buffersize != 0) {
+ readins = newsize/buffersize;
+ }
+ else {
+ readins = newsize/buffersize;
+ }
+ block = (char *)malloc(sizeof(char)*buffersize);
+ blocksize = buffersize/k;
+ }
+ else {
+ readins = 1;
+ buffersize = size;
+ block = (char *)malloc(sizeof(char)*newsize);
+ }
+
+ /* Break inputfile name into the filename and extension */
+ s1 = (char*)malloc(sizeof(char)*(strlen(argv[1])+10));
+ s2 = strrchr(argv[1], '/');
+ if (s2 != NULL) {
+ s2++;
+ strcpy(s1, s2);
+ }
+ else {
+ strcpy(s1, argv[1]);
+ }
+ s2 = strchr(s1, '.');
+ if (s2 != NULL) {
+ extension = strdup(s2);
+ *s2 = '\0';
+ } else {
+ extension = strdup("");
+ }
+
+ /* Allocate for full file name */
+ fname = (char*)malloc(sizeof(char)*(strlen(argv[1])+strlen(curdir)+10));
+ sprintf(temp, "%d", k);
+ md = strlen(temp);
+
+ /* Allocate data and coding */
+ data = (char **)malloc(sizeof(char*)*k);
+ coding = (char **)malloc(sizeof(char*)*m);
+ for (i = 0; i < m; i++) {
+ coding[i] = (char *)malloc(sizeof(char)*blocksize);
+ if (coding[i] == NULL) { perror("malloc"); exit(1); }
+ }
+
+
+
+ /* Create coding matrix or bitmatrix and schedule */
+ gettimeofday(&t3, &tz);
+ switch(tech) {
+ case No_Coding:
+ break;
+ case Reed_Sol_Van:
+ matrix = reed_sol_vandermonde_coding_matrix(k, m, w);
+ break;
+ case Cauchy_Orig:
+ matrix = cauchy_original_coding_matrix(k, m, w);
+ bitmatrix = jerasure_matrix_to_bitmatrix(k, m, w, matrix);
+ schedule = jerasure_smart_bitmatrix_to_schedule(k, m, w, bitmatrix);
+ break;
+ case Cauchy_Good:
+ matrix = cauchy_good_general_coding_matrix(k, m, w);
+ bitmatrix = jerasure_matrix_to_bitmatrix(k, m, w, matrix);
+ schedule = jerasure_smart_bitmatrix_to_schedule(k, m, w, bitmatrix);
+ break;
+ case Liberation:
+ bitmatrix = liberation_coding_bitmatrix(k, w);
+ schedule = jerasure_smart_bitmatrix_to_schedule(k, m, w, bitmatrix);
+ break;
+ case Blaum_Roth:
+ bitmatrix = blaum_roth_coding_bitmatrix(k, w);
+ schedule = jerasure_smart_bitmatrix_to_schedule(k, m, w, bitmatrix);
+ break;
+ case Liber8tion:
+ bitmatrix = liber8tion_coding_bitmatrix(k);
+ schedule = jerasure_smart_bitmatrix_to_schedule(k, m, w, bitmatrix);
+ break;
+ }
+ gettimeofday(&start, &tz);
+ gettimeofday(&t4, &tz);
+ tsec = 0.0;
+ tsec += t4.tv_usec;
+ tsec -= t3.tv_usec;
+ tsec /= 1000000.0;
+ tsec += t4.tv_sec;
+ tsec -= t3.tv_sec;
+ totalsec += tsec;
+
+
+ /* Read in data until finished */
+ n = 1;
+ total = 0;
+
+ while (n <= readins) {
+ /* Check if padding is needed, if so, add appropriate
+ number of zeros */
+ if (total < size && total+buffersize <= size) {
+ total += jfread(block, sizeof(char), buffersize, fp);
+ }
+ else if (total < size && total+buffersize > size) {
+ extra = jfread(block, sizeof(char), buffersize, fp);
+ for (i = extra; i < buffersize; i++) {
+ block[i] = '0';
+ }
+ }
+ else if (total == size) {
+ for (i = 0; i < buffersize; i++) {
+ block[i] = '0';
+ }
+ }
+
+
+ /* Set pointers to point to file data */
+ for (i = 0; i < k; i++) {
+ data[i] = block+(i*blocksize);
+ }
+
+ gettimeofday(&t3, &tz);
+ /* Encode according to coding method */
+ switch(tech) {
+ case No_Coding:
+ break;
+ case Reed_Sol_Van:
+ jerasure_matrix_encode(k, m, w, matrix, data, coding, blocksize);
+ break;
+ case Reed_Sol_R6_Op:
+ reed_sol_r6_encode(k, w, data, coding, blocksize);
+ break;
+ case Cauchy_Orig:
+ jerasure_schedule_encode(k, m, w, schedule, data, coding, blocksize, packetsize);
+ break;
+ case Cauchy_Good:
+ jerasure_schedule_encode(k, m, w, schedule, data, coding, blocksize, packetsize);
+ break;
+ case Liberation:
+ jerasure_schedule_encode(k, m, w, schedule, data, coding, blocksize, packetsize);
+ break;
+ case Blaum_Roth:
+ jerasure_schedule_encode(k, m, w, schedule, data, coding, blocksize, packetsize);
+ break;
+ case Liber8tion:
+ jerasure_schedule_encode(k, m, w, schedule, data, coding, blocksize, packetsize);
+ break;
+ }
+ gettimeofday(&t4, &tz);
+
+ /* Write data and encoded data to k+m files */
+ for (i = 1; i <= k; i++) {
+ if (fp == NULL) {
+ bzero(data[i-1], blocksize);
+ } else {
+ sprintf(fname, "%s/Coding/%s_k%0*d%s", curdir, s1, md, i, extension);
+ if (n == 1) {
+ fp2 = fopen(fname, "wb");
+ }
+ else {
+ fp2 = fopen(fname, "ab");
+ }
+ fwrite(data[i-1], sizeof(char), blocksize, fp2);
+ fclose(fp2);
+ }
+
+ }
+ for (i = 1; i <= m; i++) {
+ if (fp == NULL) {
+ bzero(data[i-1], blocksize);
+ } else {
+ sprintf(fname, "%s/Coding/%s_m%0*d%s", curdir, s1, md, i, extension);
+ if (n == 1) {
+ fp2 = fopen(fname, "wb");
+ }
+ else {
+ fp2 = fopen(fname, "ab");
+ }
+ fwrite(coding[i-1], sizeof(char), blocksize, fp2);
+ fclose(fp2);
+ }
+ }
+ n++;
+ /* Calculate encoding time */
+ tsec = 0.0;
+ tsec += t4.tv_usec;
+ tsec -= t3.tv_usec;
+ tsec /= 1000000.0;
+ tsec += t4.tv_sec;
+ tsec -= t3.tv_sec;
+ totalsec += tsec;
+ }
+
+ /* Create metadata file */
+ if (fp != NULL) {
+ sprintf(fname, "%s/Coding/%s_meta.txt", curdir, s1);
+ fp2 = fopen(fname, "wb");
+ fprintf(fp2, "%s\n", argv[1]);
+ fprintf(fp2, "%d\n", size);
+ fprintf(fp2, "%d %d %d %d %d\n", k, m, w, packetsize, buffersize);
+ fprintf(fp2, "%s\n", argv[4]);
+ fprintf(fp2, "%d\n", tech);
+ fprintf(fp2, "%d\n", readins);
+ fclose(fp2);
+ }
+
+
+ /* Free allocated memory */
+ free(s1);
+ free(fname);
+ free(block);
+ free(curdir);
+
+ /* Calculate rate in MB/sec and print */
+ gettimeofday(&t2, &tz);
+ tsec = 0.0;
+ tsec += t2.tv_usec;
+ tsec -= t1.tv_usec;
+ tsec /= 1000000.0;
+ tsec += t2.tv_sec;
+ tsec -= t1.tv_sec;
+ printf("Encoding (MB/sec): %0.10f\n", (size/1024/1024)/totalsec);
+ printf("En_Total (MB/sec): %0.10f\n", (size/1024/1024)/tsec);
+}
+
+/* is_prime returns 1 if number if prime, 0 if not prime */
+int is_prime(int w) {
+ int prime55[] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,
+ 73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,
+ 181,191,193,197,199,211,223,227,229,233,239,241,251,257};
+ int i;
+ for (i = 0; i < 55; i++) {
+ if (w%prime55[i] == 0) {
+ if (w == prime55[i]) return 1;
+ else { return 0; }
+ }
+ }
+}
+
+/* Handles ctrl-\ event */
+void ctrl_bs_handler(int dummy) {
+ time_t mytime;
+ mytime = time(0);
+ fprintf(stderr, "\n%s\n", ctime(&mytime));
+ fprintf(stderr, "You just typed ctrl-\\ in encoder.c.\n");
+ fprintf(stderr, "Total number of read ins = %d\n", readins);
+ fprintf(stderr, "Current read in: %d\n", n);
+ fprintf(stderr, "Method: %s\n\n", Methods[method]);
+ signal(SIGQUIT, ctrl_bs_handler);
+}
diff --git a/Examples/jerasure_01.c b/Examples/jerasure_01.c
new file mode 100755
index 0000000..139485f
--- /dev/null
+++ b/Examples/jerasure_01.c
@@ -0,0 +1,88 @@
+/* Examples/jerasure_01.c
+ * James S. Plank
+
+Jerasure - A C/C++ Library for a Variety of Reed-Solomon and RAID-6 Erasure Coding Techniques
+
+Revision 1.2A
+May 24, 2011
+
+James S. Plank
+Department of Electrical Engineering and Computer Science
+University of Tennessee
+Knoxville, TN 37996
+plank@cs.utk.edu
+
+Copyright (c) 2011, James S. Plank
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+ - Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ - Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+
+ - Neither the name of the University of Tennessee nor the names of its
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
+WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+
+
+ */
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include "jerasure.h"
+
+#define talloc(type, num) (type *) malloc(sizeof(type)*(num))
+
+usage(char *s)
+{
+ fprintf(stderr, "usage: jerasure_01 r c w - creates and prints out a matrix in GF(2^w).\n\n");
+ fprintf(stderr, " Element i,j is equal to 2^(i*c+j)\n");
+ fprintf(stderr, " \n");
+ fprintf(stderr, "This demonstrates jerasure_print_matrix().\n");
+ if (s != NULL) fprintf(stderr, "%s\n", s);
+ exit(1);
+}
+
+int main(int argc, char **argv)
+{
+ int r, c, w, i, n;
+ int *matrix;
+
+ if (argc != 4) usage(NULL);
+ if (sscanf(argv[1], "%d", &r) == 0 || r <= 0) usage("Bad r");
+ if (sscanf(argv[2], "%d", &c) == 0 || c <= 0) usage("Bad c");
+ if (sscanf(argv[3], "%d", &w) == 0 || w <= 0) usage("Bad w");
+
+ matrix = talloc(int, r*c);
+
+ n = 1;
+ for (i = 0; i < r*c; i++) {
+ matrix[i] = n;
+ n = galois_single_multiply(n, 2, w);
+ }
+
+ jerasure_print_matrix(matrix, r, c, w);
+ return 0;
+}
+
diff --git a/Examples/jerasure_02.c b/Examples/jerasure_02.c
new file mode 100755
index 0000000..b924691
--- /dev/null
+++ b/Examples/jerasure_02.c
@@ -0,0 +1,86 @@
+/* Examples/jerasure_02.c
+
+Jerasure - A C/C++ Library for a Variety of Reed-Solomon and RAID-6 Erasure Coding Techniques
+
+Revision 1.2A
+May 24, 2011
+
+James S. Plank
+Department of Electrical Engineering and Computer Science
+University of Tennessee
+Knoxville, TN 37996
+plank@cs.utk.edu
+
+Copyright (c) 2011, James S. Plank
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+ - Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ - Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+
+ - Neither the name of the University of Tennessee nor the names of its
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
+WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include "jerasure.h"
+
+#define talloc(type, num) (type *) malloc(sizeof(type)*(num))
+
+usage(char *s)
+{
+ fprintf(stderr, "usage: jerasure_02 r c w - Converts the matrix of jerasure_01 to a bit matrix.\n");
+ fprintf(stderr, " \n");
+ fprintf(stderr, "This demonstrates jerasure_print_bitmatrix() and jerasure_matrix_to_bitmatrix().\n");
+ if (s != NULL) fprintf(stderr, "%s\n", s);
+ exit(1);
+}
+
+int main(int argc, char **argv)
+{
+ int r, c, w, i, n;
+ int *matrix;
+ int *bitmatrix;
+
+ if (argc != 4) usage(NULL);
+ if (sscanf(argv[1], "%d", &r) == 0 || r <= 0) usage("Bad r");
+ if (sscanf(argv[2], "%d", &c) == 0 || c <= 0) usage("Bad c");
+ if (sscanf(argv[3], "%d", &w) == 0 || w <= 0) usage("Bad w");
+
+ matrix = talloc(int, r*c);
+
+ n = 1;
+ for (i = 0; i < r*c; i++) {
+ matrix[i] = n;
+ n = galois_single_multiply(n, 2, w);
+ }
+
+ bitmatrix = jerasure_matrix_to_bitmatrix(c, r, w, matrix);
+ jerasure_print_bitmatrix(bitmatrix, r*w, c*w, w);
+ return 0;
+}
+
diff --git a/Examples/jerasure_03.c b/Examples/jerasure_03.c
new file mode 100755
index 0000000..40a9098
--- /dev/null
+++ b/Examples/jerasure_03.c
@@ -0,0 +1,113 @@
+/* Examples/jerasure_03.c
+Jerasure - A C/C++ Library for a Variety of Reed-Solomon and RAID-6 Erasure Coding Techniques
+
+Revision 1.2A
+May 24, 2011
+
+James S. Plank
+Department of Electrical Engineering and Computer Science
+University of Tennessee
+Knoxville, TN 37996
+plank@cs.utk.edu
+
+Copyright (c) 2011, James S. Plank
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+ - Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ - Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+
+ - Neither the name of the University of Tennessee nor the names of its
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
+WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+
+
+ */
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "jerasure.h"
+
+#define talloc(type, num) (type *) malloc(sizeof(type)*(num))
+
+usage(char *s)
+{
+ fprintf(stderr, "usage: jerasure_03 k w - Creates a kxk Cauchy matrix in GF(2^w). \n\n");
+ fprintf(stderr, " k must be < 2^w. Element i,j is 1/(i+(2^w-j-1)). (If that is\n");
+ fprintf(stderr, " 1/0, then it sets it to zero). \n");
+ fprintf(stderr, " It then tests whether that matrix is invertible.\n");
+ fprintf(stderr, " If it is invertible, then it prints out the inverse.\n");
+ fprintf(stderr, " Finally, it prints the product of the matrix and its inverse.\n");
+ fprintf(stderr, " \n");
+ fprintf(stderr, "This demonstrates: jerasure_print_matrix()\n");
+ fprintf(stderr, " jerasure_invertible_matrix()\n");
+ fprintf(stderr, " jerasure_invert_matrix()\n");
+ fprintf(stderr, " jerasure_matrix_multiply().\n");
+ if (s != NULL) fprintf(stderr, "%s\n", s);
+ exit(1);
+}
+
+int main(int argc, char **argv)
+{
+ unsigned int k, w, i, j, n;
+ int *matrix;
+ int *matrix_copy;
+ int *inverse;
+ int *identity;
+
+ if (argc != 3) usage(NULL);
+ if (sscanf(argv[1], "%d", &k) == 0 || k <= 0) usage("Bad k");
+ if (sscanf(argv[2], "%d", &w) == 0 || w <= 0 || w > 31) usage("Bad w");
+ if (k >= (1 << w)) usage("K too big");
+
+ matrix = talloc(int, k*k);
+ matrix_copy = talloc(int, k*k);
+ inverse = talloc(int, k*k);
+
+ for (i = 0; i < k; i++) {
+ for (j = 0; j < k; j++) {
+ n = i ^ ((1 << w)-1-j);
+ matrix[i*k+j] = (n == 0) ? 0 : galois_single_divide(1, n, w);
+ }
+ }
+
+ printf("The Cauchy Matrix:\n");
+ jerasure_print_matrix(matrix, k, k, w);
+ memcpy(matrix_copy, matrix, sizeof(int)*k*k);
+ i = jerasure_invertible_matrix(matrix_copy, k, w);
+ printf("\nInvertible: %s\n", (i == 1) ? "Yes" : "No");
+ if (i == 1) {
+ printf("\nInverse:\n");
+ memcpy(matrix_copy, matrix, sizeof(int)*k*k);
+ i = jerasure_invert_matrix(matrix_copy, inverse, k, w);
+ jerasure_print_matrix(inverse, k, k, w);
+ identity = jerasure_matrix_multiply(inverse, matrix, k, k, k, k, w);
+ printf("\nInverse times matrix (should be identity):\n");
+ jerasure_print_matrix(identity, k, k, w);
+ }
+ return 0;
+}
+
diff --git a/Examples/jerasure_04.c b/Examples/jerasure_04.c
new file mode 100755
index 0000000..5d3837a
--- /dev/null
+++ b/Examples/jerasure_04.c
@@ -0,0 +1,111 @@
+/* Examples/jerasure_04.c
+Jerasure - A C/C++ Library for a Variety of Reed-Solomon and RAID-6 Erasure Coding Techniques
+
+Revision 1.2A
+May 24, 2011
+
+James S. Plank
+Department of Electrical Engineering and Computer Science
+University of Tennessee
+Knoxville, TN 37996
+plank@cs.utk.edu
+
+Copyright (c) 2011, James S. Plank
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+ - Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ - Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+
+ - Neither the name of the University of Tennessee nor the names of its
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
+WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+
+
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "jerasure.h"
+
+#define talloc(type, num) (type *) malloc(sizeof(type)*(num))
+
+usage(char *s)
+{
+ fprintf(stderr, "usage: jerasure_04 k w - Performs the analogous bit-matrix operations to jerasure_03.\n\n");
+ fprintf(stderr, " It converts the matrix to a kw*kw bit matrix and does the same operations.\n");
+ fprintf(stderr, " k must be < 2^w.\n");
+ fprintf(stderr, "This demonstrates: jerasure_print_bitmatrix()\n");
+ fprintf(stderr, " jerasure_matrix_to_bitmatrix()\n");
+ fprintf(stderr, " jerasure_invertible_bitmatrix()\n");
+ fprintf(stderr, " jerasure_invert_bitmatrix()\n");
+ fprintf(stderr, " jerasure_matrix_multiply().\n");
+ if (s != NULL) fprintf(stderr, "%s\n", s);
+ exit(1);
+}
+
+int main(int argc, char **argv)
+{
+ unsigned int k, w, i, j, n;
+ int *matrix;
+ int *bitmatrix;
+ int *bitmatrix_copy;
+ int *inverse;
+ int *identity;
+
+ if (argc != 3) usage(NULL);
+ if (sscanf(argv[1], "%d", &k) == 0 || k <= 0) usage("Bad k");
+ if (sscanf(argv[2], "%d", &w) == 0 || w <= 0 || w > 31) usage("Bad w");
+ if (k >= (1 << w)) usage("K too big");
+
+ matrix = talloc(int, k*k);
+ bitmatrix_copy = talloc(int, k*w*k*w);
+ inverse = talloc(int, k*w*k*w);
+
+ for (i = 0; i < k; i++) {
+ for (j = 0; j < k; j++) {
+ n = i ^ ((1 << w)-1-j);
+ matrix[i*k+j] = (n == 0) ? 0 : galois_single_divide(1, n, w);
+ }
+ }
+ bitmatrix = jerasure_matrix_to_bitmatrix(k, k, w, matrix);
+
+ printf("The Cauchy Bit-Matrix:\n");
+ jerasure_print_bitmatrix(bitmatrix, k*w, k*w, w);
+ memcpy(bitmatrix_copy, bitmatrix, sizeof(int)*k*w*k*w);
+ i = jerasure_invertible_bitmatrix(bitmatrix_copy, k*w);
+ printf("\nInvertible: %s\n", (i == 1) ? "Yes" : "No");
+ if (i == 1) {
+ printf("\nInverse:\n");
+ memcpy(bitmatrix_copy, bitmatrix, sizeof(int)*k*w*k*w);
+ i = jerasure_invert_bitmatrix(bitmatrix_copy, inverse, k*w);
+ jerasure_print_bitmatrix(inverse, k*w, k*w, w);
+ identity = jerasure_matrix_multiply(inverse, bitmatrix, k*w, k*w, k*w, k*w, 2);
+ printf("\nInverse times matrix (should be identity):\n");
+ jerasure_print_bitmatrix(identity, k*w, k*w, w);
+ }
+ return 0;
+}
+
diff --git a/Examples/jerasure_05.c b/Examples/jerasure_05.c
new file mode 100755
index 0000000..5acce9b
--- /dev/null
+++ b/Examples/jerasure_05.c
@@ -0,0 +1,217 @@
+/* Examples/jerasure_05.c
+Jerasure - A C/C++ Library for a Variety of Reed-Solomon and RAID-6 Erasure Coding Techniques
+
+Revision 1.2A
+May 24, 2011
+
+James S. Plank
+Department of Electrical Engineering and Computer Science
+University of Tennessee
+Knoxville, TN 37996
+plank@cs.utk.edu
+
+Copyright (c) 2011, James S. Plank
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+ - Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ - Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+
+ - Neither the name of the University of Tennessee nor the names of its
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
+WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+
+ */
+
+
+/*
+ revised by S. Simmerman
+ 2/25/08
+*/
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "jerasure.h"
+
+#define talloc(type, num) (type *) malloc(sizeof(type)*(num))
+
+usage(char *s)
+{
+ fprintf(stderr, "usage: jerasure_05 k m w size - Does a simple Reed-Solomon coding example in GF(2^w).\n");
+ fprintf(stderr, " \n");
+ fprintf(stderr, " k+m must be <= 2^w. w can be 8, 16 or 32.\n");
+ fprintf(stderr, " It sets up a Cauchy distribution matrix and encodes\n");
+ fprintf(stderr, " k devices of size bytes with it. Then it decodes.\n", sizeof(long));
+ fprintf(stderr, " After that, it decodes device 0 by using jerasure_make_decoding_matrix()\n");
+ fprintf(stderr, " and jerasure_matrix_dotprod().\n");
+ fprintf(stderr, " \n");
+ fprintf(stderr, "This demonstrates: jerasure_matrix_encode()\n");
+ fprintf(stderr, " jerasure_matrix_decode()\n");
+ fprintf(stderr, " jerasure_print_matrix()\n");
+ fprintf(stderr, " jerasure_make_decoding_matrix()\n");
+ fprintf(stderr, " jerasure_matrix_dotprod()\n");
+ if (s != NULL) fprintf(stderr, "\n%s\n\n", s);
+ exit(1);
+}
+
+static void print_data_and_coding(int k, int m, int w, int size,
+ char **data, char **coding)
+{
+ int i, j, x;
+ int n, sp;
+ long l;
+
+ if(k > m) n = k;
+ else n = m;
+ sp = size * 2 + size/(w/8) + 8;
+
+ printf("%-*sCoding\n", sp, "Data");
+ for(i = 0; i < n; i++) {
+ if(i < k) {
+ printf("D%-2d:", i);
+ for(j=0;j< size; j+=(w/8)) {
+ printf(" ");
+ for(x=0;x < w/8;x++){
+ printf("%02x", (unsigned char)data[i][j+x]);
+ }
+ }
+ printf(" ");
+ }
+ else printf("%*s", sp, "");
+ if(i < m) {
+ printf("C%-2d:", i);
+ for(j=0;j< size; j+=(w/8)) {
+ printf(" ");
+ for(x=0;x < w/8;x++){
+ printf("%02x", (unsigned char)coding[i][j+x]);
+ }
+ }
+ }
+ printf("\n");
+ }
+ printf("\n");
+}
+
+int main(int argc, char **argv)
+{
+ long l;
+ int k, m, w, size;
+ int i, j;
+ int *matrix;
+ char **data, **coding;
+ int *erasures, *erased;
+ int *decoding_matrix, *dm_ids;
+
+ if (argc != 5) usage(NULL);
+ if (sscanf(argv[1], "%d", &k) == 0 || k <= 0) usage("Bad k");
+ if (sscanf(argv[2], "%d", &m) == 0 || m <= 0) usage("Bad m");
+ if (sscanf(argv[3], "%d", &w) == 0 || (w != 8 && w != 16 && w != 32))
+ usage("Bad w");
+ if (w < 32 && k + m > (1 << w)) usage("k + m must be <= 2 ^ w");
+ if (sscanf(argv[4], "%d", &size) == 0 || size % sizeof(long) != 0)
+ usage("size must be multiple of sizeof(long)");
+
+ matrix = talloc(int, m*k);
+ for (i = 0; i < m; i++) {
+ for (j = 0; j < k; j++) {
+ matrix[i*k+j] = galois_single_divide(1, i ^ (m + j), w);
+ }
+ }
+
+ printf("The Coding Matrix (the last m rows of the Distribution Matrix):\n\n");
+ jerasure_print_matrix(matrix, m, k, w);
+ printf("\n");
+
+ srand48(0);
+ data = talloc(char *, k);
+ for (i = 0; i < k; i++) {
+ data[i] = talloc(char, size);
+ for(j = 0; j < size; j+=sizeof(long)) {
+ l = lrand48();
+ memcpy(data[i] + j, &l, sizeof(long));
+ }
+ }
+
+ coding = talloc(char *, m);
+ for (i = 0; i < m; i++) {
+ coding[i] = talloc(char, size);
+ }
+
+ jerasure_matrix_encode(k, m, w, matrix, data, coding, size);
+
+ printf("Encoding Complete:\n\n");
+ print_data_and_coding(k, m, w, size, data, coding);
+
+ erasures = talloc(int, (m+1));
+ erased = talloc(int, (k+m));
+ for (i = 0; i < m+k; i++) erased[i] = 0;
+ l = 0;
+ for (i = 0; i < m; ) {
+ erasures[i] = lrand48()%(k+m);
+ if (erased[erasures[i]] == 0) {
+ erased[erasures[i]] = 1;
+
+ bzero((erasures[i] < k) ? data[erasures[i]] : coding[erasures[i]-k], size);
+ i++;
+ }
+ }
+ erasures[i] = -1;
+
+ printf("Erased %d random devices:\n\n", m);
+ print_data_and_coding(k, m, w, size, data, coding);
+
+ i = jerasure_matrix_decode(k, m, w, matrix, 0, erasures, data, coding, size);
+
+ printf("State of the system after decoding:\n\n");
+ print_data_and_coding(k, m, w, size, data, coding);
+
+ decoding_matrix = talloc(int, k*k);
+ dm_ids = talloc(int, k);
+
+ for (i = 0; i < m; i++) erased[i] = 1;
+ for (; i < k+m; i++) erased[i] = 0;
+
+ jerasure_make_decoding_matrix(k, m, w, matrix, erased, decoding_matrix, dm_ids);
+
+ printf("Suppose we erase the first %d devices. Here is the decoding matrix:\n\n", m);
+ jerasure_print_matrix(decoding_matrix, k, k, w);
+ printf("\n");
+ printf("And dm_ids:\n\n");
+ jerasure_print_matrix(dm_ids, 1, k, w);
+
+ bzero(data[0], size);
+ jerasure_matrix_dotprod(k, w, decoding_matrix, dm_ids, 0, data, coding, size);
+
+ printf("\nAfter calling jerasure_matrix_dotprod, we calculate the value of device #0 to be:\n\n");
+ printf("D0 :");
+ for(i=0;i< size; i+=(w/8)) {
+ printf(" ");
+ for(j=0;j < w/8;j++){
+ printf("%02x", (unsigned char)data[0][i+j]);
+ }
+ }
+ printf("\n\n");
+
+ return 0;
+}
diff --git a/Examples/jerasure_06.c b/Examples/jerasure_06.c
new file mode 100755
index 0000000..de6a065
--- /dev/null
+++ b/Examples/jerasure_06.c
@@ -0,0 +1,220 @@
+/* Examples/jerasure_06.c
+Jerasure - A C/C++ Library for a Variety of Reed-Solomon and RAID-6 Erasure Coding Techniques
+
+Revision 1.2A
+May 24, 2011
+
+James S. Plank
+Department of Electrical Engineering and Computer Science
+University of Tennessee
+Knoxville, TN 37996
+plank@cs.utk.edu
+
+Copyright (c) 2011, James S. Plank
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+ - Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ - Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+
+ - Neither the name of the University of Tennessee nor the names of its
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
+WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+
+
+ */
+
+/*
+ revised by S. Simmerman
+ 2/25/08
+*/
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "jerasure.h"
+
+#define talloc(type, num) (type *) malloc(sizeof(type)*(num))
+
+usage(char *s)
+{
+ fprintf(stderr, "usage: jerasure_06 k m w packetsize\n");
+ fprintf(stderr, "Does a simple Cauchy Reed-Solomon coding example in GF(2^w).\n");
+ fprintf(stderr, " \n");
+ fprintf(stderr, " k+m must be < 2^w. Packetsize must be a multiple of sizeof(long)\n");
+ fprintf(stderr, " It sets up a Cauchy distribution matrix and encodes k devices of w*packetsize bytes.\n");
+ fprintf(stderr, " After that, it decodes device 0 by using jerasure_make_decoding_bitmatrix()\n");
+ fprintf(stderr, " and jerasure_bitmatrix_dotprod().\n");
+ fprintf(stderr, " \n");
+ fprintf(stderr, "This demonstrates: jerasure_bitmatrix_encode()\n");
+ fprintf(stderr, " jerasure_bitmatrix_decode()\n");
+ fprintf(stderr, " jerasure_print_bitmatrix()\n");
+ fprintf(stderr, " jerasure_make_decoding_bitmatrix()\n");
+ fprintf(stderr, " jerasure_bitmatrix_dotprod()\n");
+ if (s != NULL) fprintf(stderr, "\n%s\n\n", s);
+ exit(1);
+}
+
+static void print_data_and_coding(int k, int m, int w, int psize,
+ char **data, char **coding)
+{
+ int i, j, x, n, sp;
+ long l;
+
+ if(k > m) n = k;
+ else n = m;
+ sp = psize * 2 + (psize/4) + 12;
+
+ printf("%-*sCoding\n", sp, "Data");
+ for(i = 0; i < n; i++) {
+ for (j = 0; j < w; j++) {
+ if(i < k) {
+
+ if(j==0) printf("D%-2d p%-2d:", i,j);
+ else printf(" p%-2d:", j);
+ for(x = 0; x < psize; x +=4) {
+ memcpy(&l, data[i]+j*psize+x, sizeof(long));
+ printf(" %08lx", l);
+ }
+ printf(" ");
+ }
+ else printf("%*s", sp, "");
+ if(i < m) {
+ if(j==0) printf("C%-2d p%-2d:", i,j);
+ else printf(" p%-2d:", j);
+ for(x = 0; x < psize; x +=4) {
+ memcpy(&l, coding[i]+j*psize+x, sizeof(long));
+ printf(" %08lx", l);
+ }
+ }
+ printf("\n");
+ }
+ }
+
+ printf("\n");
+}
+
+int main(int argc, char **argv)
+{
+ long l;
+ int k, w, i, j, m, psize, x;
+ int *matrix, *bitmatrix;
+ char **data, **coding;
+ int *erasures, *erased;
+ int *decoding_matrix, *dm_ids;
+
+ if (argc != 5) usage(NULL);
+ if (sscanf(argv[1], "%d", &k) == 0 || k <= 0) usage("Bad k");
+ if (sscanf(argv[2], "%d", &m) == 0 || m <= 0) usage("Bad m");
+ if (sscanf(argv[3], "%d", &w) == 0 || w <= 0 || w > 32) usage("Bad w");
+ if (w < 30 && (k+m) > (1 << w)) usage("k + m is too big");
+ if (sscanf(argv[4], "%d", &psize) == 0 || psize <= 0) usage("Bad packetsize");
+ if(psize%sizeof(long) != 0) usage("Packetsize must be multiple of sizeof(long)");
+
+ matrix = talloc(int, m*k);
+ for (i = 0; i < m; i++) {
+ for (j = 0; j < k; j++) {
+ matrix[i*k+j] = galois_single_divide(1, i ^ (m + j), w);
+ }
+ }
+ bitmatrix = jerasure_matrix_to_bitmatrix(k, m, w, matrix);
+
+ printf("Last (m * w) rows of the Binary Distribution Matrix:\n\n");
+ jerasure_print_bitmatrix(bitmatrix, w*m, w*k, w);
+ printf("\n");
+
+ srand48(0);
+ data = talloc(char *, k);
+ for (i = 0; i < k; i++) {
+ data[i] = talloc(char, psize*w);
+ for (j = 0; j < w; j++) {
+ for(x = 0; x < psize; x += 4) {
+ l = lrand48();
+ memcpy(data[i]+j*psize+x, &l, sizeof(long));
+ }
+
+ }
+ }
+
+ coding = talloc(char *, m);
+ for (i = 0; i < m; i++) {
+ coding[i] = talloc(char, psize*w);
+ }
+
+ jerasure_bitmatrix_encode(k, m, w, bitmatrix, data, coding, w*psize, psize);
+
+ printf("Encoding Complete:\n\n");
+ print_data_and_coding(k, m, w, psize, data, coding);
+
+ erasures = talloc(int, (m+1));
+ erased = talloc(int, (k+m));
+ for (i = 0; i < m+k; i++) erased[i] = 0;
+ for (i = 0; i < m; ) {
+ erasures[i] = lrand48()%(k+m);
+ if (erased[erasures[i]] == 0) {
+ erased[erasures[i]] = 1;
+ bzero((erasures[i] < k) ? data[erasures[i]] : coding[erasures[i]-k], psize*w);
+ i++;
+ }
+ }
+ erasures[i] = -1;
+
+ printf("Erased %d random devices:\n\n", m);
+ print_data_and_coding(k, m, w, psize, data, coding);
+
+ i = jerasure_bitmatrix_decode(k, m, w, bitmatrix, 0, erasures, data, coding,
+ w*psize, psize);
+
+ printf("State of the system after decoding:\n\n");
+ print_data_and_coding(k, m, w, psize, data, coding);
+
+ decoding_matrix = talloc(int, k*k*w*w);
+ dm_ids = talloc(int, k);
+
+ for (i = 0; i < m; i++) erased[i] = 1;
+ for (; i < k+m; i++) erased[i] = 0;
+
+ jerasure_make_decoding_bitmatrix(k, m, w, bitmatrix, erased, decoding_matrix, dm_ids);
+
+ printf("Suppose we erase the first %d devices. Here is the decoding matrix:\n\n", m);
+ jerasure_print_bitmatrix(decoding_matrix, k*w, k*w, w);
+ printf("\n");
+ printf("And dm_ids:\n\n");
+ jerasure_print_matrix(dm_ids, 1, k, w);
+
+ //memcpy(&l, data[0], sizeof(long));
+ //printf("\nThe value of device #%d, word 0 is: %lx\n", 0, l);
+ bzero(data[0], w*psize);
+ jerasure_bitmatrix_dotprod(k, w, decoding_matrix, dm_ids, 0, data, coding, w*psize, psize);
+
+ printf("\nAfter calling jerasure_matrix_dotprod, we calculate the value of device #0, packet 0 to be:\n");
+ printf("\nD0 p0 :");
+ for(i = 0; i < psize; i +=sizeof(long)) {
+ memcpy(&l, data[0]+i, sizeof(long));
+ printf(" %08lx", l);
+ }
+ printf("\n\n");
+ //memcpy(&l, data[0], sizeof(long));
+
+ return 0;
+}
diff --git a/Examples/jerasure_07.c b/Examples/jerasure_07.c
new file mode 100755
index 0000000..44d6251
--- /dev/null
+++ b/Examples/jerasure_07.c
@@ -0,0 +1,206 @@
+/* Examples/jerasure_07.c
+Jerasure - A C/C++ Library for a Variety of Reed-Solomon and RAID-6 Erasure Coding Techniques
+
+Revision 1.2A
+May 24, 2011
+
+James S. Plank
+Department of Electrical Engineering and Computer Science
+University of Tennessee
+Knoxville, TN 37996
+plank@cs.utk.edu
+
+Copyright (c) 2011, James S. Plank
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+ - Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ - Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+
+ - Neither the name of the University of Tennessee nor the names of its
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
+WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+
+ */
+
+
+/*
+ revised by S. Simmerman
+ 2/25/08
+*/
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "jerasure.h"
+
+#define talloc(type, num) (type *) malloc(sizeof(type)*(num))
+
+usage(char *s)
+{
+ fprintf(stderr, "usage: jerasure_07 k m w - Scheduled Cauchy Reed-Solomon coding example in GF(2^w).\n");
+ fprintf(stderr, " \n");
+ fprintf(stderr, " k+m must be <= 2^w. It sets up a Cauchy distribution matrix and encodes\n");
+ fprintf(stderr, " k sets of w*%d bytes. It uses bit-matrix scheduling, both smart and dumb.\n", sizeof(long));
+ fprintf(stderr, " It decodes using bit-matrix scheduling, then shows an example of\n");
+ fprintf(stderr, " using jerasure_do_scheduled_operations().\n");
+ fprintf(stderr, " \n");
+ fprintf(stderr, "This demonstrates: jerasure_dumb_bitmatrix_to_schedule()\n");
+ fprintf(stderr, " jerasure_smart_bitmatrix_to_schedule()\n");
+ fprintf(stderr, " jerasure_schedule_encode()\n");
+ fprintf(stderr, " jerasure_schedule_decode_lazy()\n");
+ fprintf(stderr, " jerasure_do_scheduled_operations()\n");
+ fprintf(stderr, " jerasure_get_stats()\n");
+ if (s != NULL) fprintf(stderr, "%s\n", s);
+ exit(1);
+}
+
+static void print_data_and_coding(int k, int m, int w, int psize,
+ char **data, char **coding)
+{
+ int i, j, x, n, sp;
+ long l;
+
+ if(k > m) n = k;
+ else n = m;
+ sp = psize * 2 + (psize/4) + 12;
+
+ printf("%-*sCoding\n", sp, "Data");
+ for(i = 0; i < n; i++) {
+ for (j = 0; j < w; j++) {
+ if(i < k) {
+
+ if(j==0) printf("D%-2d p%-2d:", i,j);
+ else printf(" p%-2d:", j);
+ for(x = 0; x < psize; x +=4) {
+ memcpy(&l, data[i]+j*psize+x, sizeof(long));
+ printf(" %08lx", l);
+ }
+ printf(" ");
+ }
+ else printf("%*s", sp, "");
+ if(i < m) {
+ if(j==0) printf("C%-2d p%-2d:", i,j);
+ else printf(" p%-2d:", j);
+ for(x = 0; x < psize; x +=4) {
+ memcpy(&l, coding[i]+j*psize+x, sizeof(long));
+ printf(" %08lx", l);
+ }
+ }
+ printf("\n");
+ }
+ }
+
+ printf("\n");
+}
+
+int main(int argc, char **argv)
+{
+ long l;
+ int k, w, i, j, m;
+ int *matrix, *bitmatrix;
+ char **data, **coding, **ptrs;
+ int **smart, **dumb;
+ int *erasures, *erased;
+ double stats[3];
+
+ if (argc != 4) usage(NULL);
+ if (sscanf(argv[1], "%d", &k) == 0 || k <= 0) usage("Bad k");
+ if (sscanf(argv[2], "%d", &m) == 0 || m <= 0) usage("Bad m");
+ if (sscanf(argv[3], "%d", &w) == 0 || w <= 0 || w > 32) usage("Bad m");
+ if (w < 30 && (k+m) > (1 << w)) usage("k + m is too big");
+
+ matrix = talloc(int, m*k);
+ for (i = 0; i < m; i++) {
+ for (j = 0; j < k; j++) {
+ matrix[i*k+j] = galois_single_divide(1, i ^ (m + j), w);
+ }
+ }
+ bitmatrix = jerasure_matrix_to_bitmatrix(k, m, w, matrix);
+
+ printf("Last m rows of the Binary Distribution Matrix:\n\n");
+ jerasure_print_bitmatrix(bitmatrix, w*m, w*k, w);
+ printf("\n");
+
+ dumb = jerasure_dumb_bitmatrix_to_schedule(k, m, w, bitmatrix);
+ smart = jerasure_smart_bitmatrix_to_schedule(k, m, w, bitmatrix);
+
+ srand48(0);
+ data = talloc(char *, k);
+ for (i = 0; i < k; i++) {
+ data[i] = talloc(char, sizeof(long)*w);
+ for (j = 0; j < w; j++) {
+ l = lrand48();
+ memcpy(data[i]+j*sizeof(long), &l, sizeof(long));
+ }
+ }
+
+ coding = talloc(char *, m);
+ for (i = 0; i < m; i++) {
+ coding[i] = talloc(char, sizeof(long)*w);
+ }
+
+ jerasure_schedule_encode(k, m, w, dumb, data, coding, w*sizeof(long), sizeof(long));
+ jerasure_get_stats(stats);
+ printf("Dumb Encoding Complete: - %.0lf XOR'd bytes\n\n", stats[0]);
+ print_data_and_coding(k, m, w, sizeof(long), data, coding);
+
+ jerasure_schedule_encode(k, m, w, smart, data, coding, w*sizeof(long), sizeof(long));
+ jerasure_get_stats(stats);
+ printf("Smart Encoding Complete: - %.0lf XOR'd bytes\n\n", stats[0]);
+ print_data_and_coding(k, m, w, sizeof(long), data, coding);
+
+ erasures = talloc(int, (m+1));
+ erased = talloc(int, (k+m));
+ for (i = 0; i < m+k; i++) erased[i] = 0;
+ for (i = 0; i < m; ) {
+ erasures[i] = lrand48()%(k+m);
+ if (erased[erasures[i]] == 0) {
+ erased[erasures[i]] = 1;
+ bzero((erasures[i] < k) ? data[erasures[i]] : coding[erasures[i]-k], sizeof(long)*w);
+ i++;
+ }
+ }
+ erasures[i] = -1;
+
+ printf("Erased %d random devices:\n\n", m);
+ print_data_and_coding(k, m, w, sizeof(long), data, coding);
+
+ jerasure_schedule_decode_lazy(k, m, w, bitmatrix, erasures, data, coding, w*sizeof(long), sizeof(long), 1);
+ jerasure_get_stats(stats);
+
+ printf("State of the system after decoding: %.0lf XOR'd bytes\n\n", stats[0]);
+ print_data_and_coding(k, m, w, sizeof(long), data, coding);
+
+ ptrs = talloc(char *, (k+m));
+ for (i = 0; i < k; i++) ptrs[i] = data[i];
+ for (i = 0; i < m; i++) ptrs[k+i] = coding[i];
+
+ for (j = 0; j < m; j++) bzero(coding[j], sizeof(long)*w);
+ jerasure_do_scheduled_operations(ptrs, smart, sizeof(long));
+ printf("State of the system after deleting the coding devices and\n");
+ printf("using jerasure_do_scheduled_operations(): %.0lf XOR'd bytes\n\n", stats[0]);
+ print_data_and_coding(k, m, w, sizeof(long), data, coding);
+
+ return 0;
+}
diff --git a/Examples/jerasure_08.c b/Examples/jerasure_08.c
new file mode 100755
index 0000000..573e5b1
--- /dev/null
+++ b/Examples/jerasure_08.c
@@ -0,0 +1,214 @@
+/* Examples/jerasure_08.c
+Jerasure - A C/C++ Library for a Variety of Reed-Solomon and RAID-6 Erasure Coding Techniques
+
+Revision 1.2A
+May 24, 2011
+
+James S. Plank
+Department of Electrical Engineering and Computer Science
+University of Tennessee
+Knoxville, TN 37996
+plank@cs.utk.edu
+
+Copyright (c) 2011, James S. Plank
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+ - Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ - Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+
+ - Neither the name of the University of Tennessee nor the names of its
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
+WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+
+
+ */
+
+
+/*
+ revised by S. Simmerman
+ 2/25/08
+*/
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "jerasure.h"
+
+#define talloc(type, num) (type *) malloc(sizeof(type)*(num))
+
+usage(char *s)
+{
+ fprintf(stderr, "usage: jerasure_08 k w - Example schedule cache usage with RAID-6\n");
+ fprintf(stderr, " \n");
+ fprintf(stderr, " m=2. k+m must be <= 2^w. It sets up a RAID-6 distribution matrix and encodes\n");
+ fprintf(stderr, " k sets of w*%d bytes. It creates a schedule cache for decoding.\n", sizeof(long));
+ fprintf(stderr, " It demonstrates using the schedule cache for both encoding and decoding.\n");
+ fprintf(stderr, " Then it demonstrates using jerasure_do_parity() to re-encode the first.\n");
+ fprintf(stderr, " coding device\n");
+ fprintf(stderr, " \n");
+ fprintf(stderr, "This demonstrates: jerasure_generate_schedule_cache()\n");
+ fprintf(stderr, " jerasure_smart_bitmatrix_to_schedule()\n");
+ fprintf(stderr, " jerasure_schedule_encode()\n");
+ fprintf(stderr, " jerasure_schedule_decode_cache()\n");
+ fprintf(stderr, " jerasure_free_schedule()\n");
+ fprintf(stderr, " jerasure_free_schedule_cache()\n");
+ fprintf(stderr, " jerasure_get_stats()\n");
+ fprintf(stderr, " jerasure_do_parity()\n");
+ if (s != NULL) fprintf(stderr, "%s\n", s);
+ exit(1);
+}
+
+
+static void print_data_and_coding(int k, int m, int w, int psize,
+ char **data, char **coding)
+{
+ int i, j, x, n, sp;
+ long l;
+
+ if(k > m) n = k;
+ else n = m;
+ sp = psize * 2 + (psize/4) + 12;
+
+ printf("%-*sCoding\n", sp, "Data");
+ for(i = 0; i < n; i++) {
+ for (j = 0; j < w; j++) {
+ if(i < k) {
+
+ if(j==0) printf("D%-2d p%-2d:", i,j);
+ else printf(" p%-2d:", j);
+ for(x = 0; x < psize; x +=4) {
+ memcpy(&l, data[i]+j*psize+x, sizeof(long));
+ printf(" %08lx", l);
+ }
+ printf(" ");
+ }
+ else printf("%*s", sp, "");
+ if(i < m) {
+ if(j==0) printf("C%-2d p%-2d:", i,j);
+ else printf(" p%-2d:", j);
+ for(x = 0; x < psize; x +=4) {
+ memcpy(&l, coding[i]+j*psize+x, sizeof(long));
+ printf(" %08lx", l);
+ }
+ }
+ printf("\n");
+ }
+ }
+
+ printf("\n");
+}
+
+int main(int argc, char **argv)
+{
+ long l;
+ int k, w, i, j, m;
+ int *matrix, *bitmatrix;
+ char **data, **coding, **ptrs;
+ int **smart, ***cache;
+ int *erasures, *erased;
+ double stats[3];
+
+ if (argc != 3) usage(NULL);
+ if (sscanf(argv[1], "%d", &k) == 0 || k <= 0) usage("Bad k");
+ if (sscanf(argv[2], "%d", &w) == 0 || w <= 0 || w > 32) usage("Bad m");
+ m = 2;
+ if (w < 30 && (k+m) > (1 << w)) usage("k + m is too big");
+
+ matrix = talloc(int, m*k);
+ for (j = 0; j < k; j++) matrix[j] = 1;
+ i = 1;
+ for (j = 0; j < k; j++) {
+ matrix[k+j] = i;
+ i = galois_single_multiply(i, 2, w);
+ }
+ bitmatrix = jerasure_matrix_to_bitmatrix(k, m, w, matrix);
+
+ smart = jerasure_smart_bitmatrix_to_schedule(k, m, w, bitmatrix);
+ cache = jerasure_generate_schedule_cache(k, m, w, bitmatrix, 1);
+
+ srand48(0);
+ data = talloc(char *, k);
+ for (i = 0; i < k; i++) {
+ data[i] = talloc(char, sizeof(long)*w);
+ for (j = 0; j < w; j++) {
+ l = lrand48();
+ memcpy(data[i]+j*sizeof(long), &l, sizeof(long));
+ }
+ }
+
+ coding = talloc(char *, m);
+ for (i = 0; i < m; i++) {
+ coding[i] = talloc(char, sizeof(long)*w);
+ }
+
+ jerasure_schedule_encode(k, m, w, smart, data, coding, w*sizeof(long), sizeof(long));
+ jerasure_get_stats(stats);
+ printf("Encoding Complete: - %.0lf XOR'd bytes\n\n", stats[0]);
+ print_data_and_coding(k, m, w, sizeof(long), data, coding);
+
+ erasures = talloc(int, (m+1));
+ erasures[0] = k;
+ erasures[1] = k+1;
+ erasures[2] = -1;
+ for (j = 0; j < m; j++) bzero(coding[j], sizeof(long)*w);
+
+ jerasure_schedule_decode_cache(k, m, w, cache, erasures, data, coding, w*sizeof(long), sizeof(long));
+ jerasure_get_stats(stats);
+ printf("Encoding Using the Schedule Cache: - %.0lf XOR'd bytes\n\n", stats[0]);
+ print_data_and_coding(k, m, w, sizeof(long), data, coding);
+
+ erased = talloc(int, (k+m));
+ for (i = 0; i < m+k; i++) erased[i] = 0;
+ for (i = 0; i < m; ) {
+ erasures[i] = lrand48()%(k+m);
+ if (erased[erasures[i]] == 0) {
+ erased[erasures[i]] = 1;
+ bzero((erasures[i] < k) ? data[erasures[i]] : coding[erasures[i]-k], sizeof(long)*w);
+ i++;
+ }
+ }
+ erasures[i] = -1;
+
+ printf("Erased %d random devices:\n\n", m);
+ print_data_and_coding(k, m, w, sizeof(long), data, coding);
+
+ jerasure_schedule_decode_cache(k, m, w, cache, erasures, data, coding, w*sizeof(long), sizeof(long));
+ jerasure_get_stats(stats);
+
+ printf("State of the system after decoding: %.0lf XOR'd bytes\n\n", stats[0]);
+ print_data_and_coding(k, m, w, sizeof(long), data, coding);
+
+ bzero(coding[0], sizeof(long)*w);
+ jerasure_do_parity(k, data, coding[0], sizeof(long)*w);
+ printf("State of the system after deleting coding device 0 and using\n");
+ printf("jerasure_do_parity to re-encode it:\n\n");
+ print_data_and_coding(k, m, w, sizeof(long), data, coding);
+
+ jerasure_free_schedule(smart);
+ jerasure_free_schedule_cache(k, m, cache);
+
+ printf("Smart schedule and cache freed\n\n");
+
+ return 0;
+}
diff --git a/Examples/liberation_01.c b/Examples/liberation_01.c
new file mode 100755
index 0000000..2920fa9
--- /dev/null
+++ b/Examples/liberation_01.c
@@ -0,0 +1,190 @@
+/* Examples/liberation_01.c
+Jerasure - A C/C++ Library for a Variety of Reed-Solomon and RAID-6 Erasure Coding Techniques
+
+Revision 1.2A
+May 24, 2011
+
+James S. Plank
+Department of Electrical Engineering and Computer Science
+University of Tennessee
+Knoxville, TN 37996
+plank@cs.utk.edu
+
+Copyright (c) 2011, James S. Plank
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+ - Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ - Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+
+ - Neither the name of the University of Tennessee nor the names of its
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
+WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+
+
+ */
+
+
+/*
+ revised by S. Simmerman
+ 2/25/08
+*/
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "jerasure.h"
+#include "liberation.h"
+
+#define talloc(type, num) (type *) malloc(sizeof(type)*(num))
+
+usage(char *s)
+{
+ fprintf(stderr, "usage: liberation_01 k w - Liberation RAID-6 coding/decoding example in GF(2^w).\n");
+ fprintf(stderr, " \n");
+ fprintf(stderr, " w must be prime and k <= w. It sets up a Liberation bit-matrix\n");
+ fprintf(stderr, " then it encodes k devices of w*%d bytes using dumb bit-matrix scheduling.\n", sizeof(long));
+ fprintf(stderr, " It decodes using smart bit-matrix scheduling.\n");
+ fprintf(stderr, " \n");
+ fprintf(stderr, "This demonstrates: liberation_coding_bitmatrix()\n");
+ fprintf(stderr, " jerasure_smart_bitmatrix_to_schedule()\n");
+ fprintf(stderr, " jerasure_dumb_bitmatrix_to_schedule()\n");
+ fprintf(stderr, " jerasure_schedule_encode()\n");
+ fprintf(stderr, " jerasure_schedule_decode_lazy()\n");
+ fprintf(stderr, " jerasure_print_bitmatrix()\n");
+ fprintf(stderr, " jerasure_get_stats()\n");
+ if (s != NULL) fprintf(stderr, "%s\n", s);
+ exit(1);
+}
+
+
+static void print_data_and_coding(int k, int m, int w, int psize,
+ char **data, char **coding)
+{
+ int i, j, x, n, sp;
+ long l;
+
+ if(k > m) n = k;
+ else n = m;
+ sp = psize * 2 + (psize/4) + 12;
+
+ printf("%-*sCoding\n", sp, "Data");
+ for(i = 0; i < n; i++) {
+ for (j = 0; j < w; j++) {
+ if(i < k) {
+
+ if(j==0) printf("D%-2d p%-2d:", i,j);
+ else printf(" p%-2d:", j);
+ for(x = 0; x < psize; x +=4) {
+ memcpy(&l, data[i]+j*psize+x, sizeof(long));
+ printf(" %08lx", l);
+ }
+ printf(" ");
+ }
+ else printf("%*s", sp, "");
+ if(i < m) {
+ if(j==0) printf("C%-2d p%-2d:", i,j);
+ else printf(" p%-2d:", j);
+ for(x = 0; x < psize; x +=4) {
+ memcpy(&l, coding[i]+j*psize+x, sizeof(long));
+ printf(" %08lx", l);
+ }
+ }
+ printf("\n");
+ }
+ }
+
+ printf("\n");
+}
+
+int main(int argc, char **argv)
+{
+ long l;
+ int k, w, i, j, m;
+ int *bitmatrix;
+ char **data, **coding, **ptrs;
+ int **dumb;
+ int *erasures, *erased;
+ double stats[3];
+
+ if (argc != 3) usage(NULL);
+ if (sscanf(argv[1], "%d", &k) == 0 || k <= 0) usage("Bad k");
+ if (sscanf(argv[2], "%d", &w) == 0 || w <= 0 || w > 32) usage("Bad w");
+ m = 2;
+ if (w < k) usage("k is too big");
+
+ bitmatrix = liberation_coding_bitmatrix(k, w);
+ if (bitmatrix == NULL) {
+ usage("couldn't make coding matrix");
+ }
+
+ printf("Coding Bit-Matrix:\n\n");
+ jerasure_print_bitmatrix(bitmatrix, w*m, w*k, w);
+ printf("\n");
+
+ dumb = jerasure_dumb_bitmatrix_to_schedule(k, m, w, bitmatrix);
+
+ srand48(0);
+ data = talloc(char *, k);
+ for (i = 0; i < k; i++) {
+ data[i] = talloc(char, sizeof(long)*w);
+ for (j = 0; j < w; j++) {
+ l = lrand48();
+ memcpy(data[i]+j*sizeof(long), &l, sizeof(long));
+ }
+ }
+
+ coding = talloc(char *, m);
+ for (i = 0; i < m; i++) {
+ coding[i] = talloc(char, sizeof(long)*w);
+ }
+
+ jerasure_schedule_encode(k, m, w, dumb, data, coding, w*sizeof(long), sizeof(long));
+ jerasure_get_stats(stats);
+ printf("Smart Encoding Complete: - %.0lf XOR'd bytes\n\n", stats[0]);
+ print_data_and_coding(k, m, w, sizeof(long), data, coding);
+
+ erasures = talloc(int, (m+1));
+ erased = talloc(int, (k+m));
+ for (i = 0; i < m+k; i++) erased[i] = 0;
+ for (i = 0; i < m; ) {
+ erasures[i] = lrand48()%(k+m);
+ if (erased[erasures[i]] == 0) {
+ erased[erasures[i]] = 1;
+ bzero((erasures[i] < k) ? data[erasures[i]] : coding[erasures[i]-k], sizeof(long)*w);
+ i++;
+ }
+ }
+ erasures[i] = -1;
+
+ printf("Erased %d random devices:\n\n", m);
+ print_data_and_coding(k, m, w, sizeof(long), data, coding);
+
+ jerasure_schedule_decode_lazy(k, m, w, bitmatrix, erasures, data, coding, w*sizeof(long), sizeof(long), 1);
+ jerasure_get_stats(stats);
+
+ printf("State of the system after decoding: %.0lf XOR'd bytes\n\n", stats[0]);
+ print_data_and_coding(k, m, w, sizeof(long), data, coding);
+
+ return 0;
+}
diff --git a/Examples/makefile b/Examples/makefile
new file mode 100755
index 0000000..c01977f
--- /dev/null
+++ b/Examples/makefile
@@ -0,0 +1,186 @@
+# Examples/makefile
+# Jerasure - A C/C++ Library for a Variety of Reed-Solomon and RAID-6 Erasure Coding Techniques
+#
+# Revision 1.2A
+# May 24, 2011
+#
+# James S. Plank
+# Department of Electrical Engineering and Computer Science
+# University of Tennessee
+# Knoxville, TN 37996
+# plank@cs.utk.edu
+#
+# Copyright (c) 2011, James S. Plank
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# - Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+#
+# - Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in
+# the documentation and/or other materials provided with the
+# distribution.
+#
+# - Neither the name of the University of Tennessee nor the names of its
+# contributors may be used to endorse or promote products derived
+# from this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
+# WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+
+
+CC = gcc
+CFLAGS = -O2 -I$(HOME)/include
+
+ALL = jerasure_01 \
+ jerasure_02 \
+ jerasure_03 \
+ jerasure_04 \
+ jerasure_05 \
+ jerasure_06 \
+ jerasure_07 \
+ jerasure_08 \
+ reed_sol_01 \
+ reed_sol_02 \
+ reed_sol_03 \
+ reed_sol_04 \
+ cauchy_01 \
+ cauchy_02 \
+ cauchy_03 \
+ cauchy_04 \
+ liberation_01 \
+ encoder \
+ decoder \
+
+all: $(ALL)
+
+clean:
+ rm -f core *.o $(ALL) a.out cauchy.h cauchy.c liberation.h liberation.c reed_sol.c reed_sol.h\
+ jerasure.c jerasure.h galois.c galois.h
+
+.SUFFIXES: .c .o
+.c.o:
+ $(CC) $(CFLAGS) -c $*.c
+
+liberation.h: ../liberation.h
+ rm -f liberation.h ; cp ../liberation.h . ; chmod 0444 liberation.h
+
+liberation.c: ../liberation.c
+ rm -f liberation.c ; cp ../liberation.c . ; chmod 0444 liberation.c
+
+cauchy.h: ../cauchy.h
+ rm -f cauchy.h ; cp ../cauchy.h . ; chmod 0444 cauchy.h
+
+cauchy.c: ../cauchy.c
+ rm -f cauchy.c ; cp ../cauchy.c . ; chmod 0444 cauchy.c
+
+reed_sol.h: ../reed_sol.h
+ rm -f reed_sol.h ; cp ../reed_sol.h . ; chmod 0444 reed_sol.h
+
+reed_sol.c: ../reed_sol.c
+ rm -f reed_sol.c ; cp ../reed_sol.c . ; chmod 0444 reed_sol.c
+
+jerasure.h: ../jerasure.h
+ rm -f jerasure.h ; cp ../jerasure.h . ; chmod 0444 jerasure.h
+
+jerasure.c: ../jerasure.c
+ rm -f jerasure.c ; cp ../jerasure.c . ; chmod 0444 jerasure.c
+
+galois.h: ../galois.h
+ rm -f galois.h ; cp ../galois.h . ; chmod 0444 galois.h
+
+galois.c: ../galois.c
+ rm -f galois.c ; cp ../galois.c . ; chmod 0444 galois.c
+
+galois.o: galois.h
+jerasure.o: jerasure.h galois.h
+
+jerasure_01.o: galois.h jerasure.h
+jerasure_01: jerasure_01.o galois.o jerasure.o
+ $(CC) $(CFLAGS) -o jerasure_01 jerasure_01.o jerasure.o galois.o
+
+jerasure_02.o: galois.h jerasure.h
+jerasure_02: jerasure_02.o galois.o jerasure.o
+ $(CC) $(CFLAGS) -o jerasure_02 jerasure_02.o jerasure.o galois.o
+
+jerasure_03.o: galois.h jerasure.h
+jerasure_03: jerasure_03.o galois.o jerasure.o
+ $(CC) $(CFLAGS) -o jerasure_03 jerasure_03.o jerasure.o galois.o
+
+jerasure_04.o: galois.h jerasure.h
+jerasure_04: jerasure_04.o galois.o jerasure.o
+ $(CC) $(CFLAGS) -o jerasure_04 jerasure_04.o jerasure.o galois.o
+
+jerasure_05.o: galois.h jerasure.h
+jerasure_05: jerasure_05.o galois.o jerasure.o
+ $(CC) $(CFLAGS) -o jerasure_05 jerasure_05.o jerasure.o galois.o
+
+jerasure_06.o: galois.h jerasure.h
+jerasure_06: jerasure_06.o galois.o jerasure.o
+ $(CC) $(CFLAGS) -o jerasure_06 jerasure_06.o jerasure.o galois.o
+
+jerasure_07.o: galois.h jerasure.h
+jerasure_07: jerasure_07.o galois.o jerasure.o
+ $(CC) $(CFLAGS) -o jerasure_07 jerasure_07.o jerasure.o galois.o
+
+jerasure_08.o: galois.h jerasure.h
+jerasure_08: jerasure_08.o galois.o jerasure.o
+ $(CC) $(CFLAGS) -o jerasure_08 jerasure_08.o jerasure.o galois.o
+
+reed_sol_01.o: galois.h reed_sol.h jerasure.h
+reed_sol_01: reed_sol_01.o galois.o jerasure.o reed_sol.o
+ $(CC) $(CFLAGS) -o reed_sol_01 reed_sol_01.o reed_sol.o jerasure.o galois.o
+
+reed_sol_02.o: galois.h reed_sol.h jerasure.h
+reed_sol_02: reed_sol_02.o galois.o jerasure.o reed_sol.o
+ $(CC) $(CFLAGS) -o reed_sol_02 reed_sol_02.o reed_sol.o jerasure.o galois.o
+
+reed_sol_03.o: galois.h reed_sol.h jerasure.h
+reed_sol_03: reed_sol_03.o galois.o jerasure.o reed_sol.o
+ $(CC) $(CFLAGS) -o reed_sol_03 reed_sol_03.o reed_sol.o jerasure.o galois.o
+
+reed_sol_04.o: galois.h reed_sol.h jerasure.h
+reed_sol_04: reed_sol_04.o galois.o jerasure.o reed_sol.o
+ $(CC) $(CFLAGS) -o reed_sol_04 reed_sol_04.o reed_sol.o jerasure.o galois.o
+
+cauchy_01.o: galois.h cauchy.h jerasure.h
+cauchy_01: cauchy_01.o galois.o jerasure.o cauchy.o
+ $(CC) $(CFLAGS) -o cauchy_01 cauchy_01.o cauchy.o jerasure.o galois.o
+
+cauchy_02.o: galois.h cauchy.h jerasure.h
+cauchy_02: cauchy_02.o galois.o jerasure.o cauchy.o
+ $(CC) $(CFLAGS) -o cauchy_02 cauchy_02.o cauchy.o jerasure.o galois.o
+
+cauchy_03.o: galois.h cauchy.h jerasure.h
+cauchy_03: cauchy_03.o galois.o jerasure.o cauchy.o
+ $(CC) $(CFLAGS) -o cauchy_03 cauchy_03.o cauchy.o jerasure.o galois.o
+
+cauchy_04.o: galois.h cauchy.h jerasure.h
+cauchy_04: cauchy_04.o galois.o jerasure.o cauchy.o
+ $(CC) $(CFLAGS) -o cauchy_04 cauchy_04.o cauchy.o jerasure.o galois.o
+
+liberation_01.o: galois.h liberation.h jerasure.h
+liberation_01: liberation_01.o galois.o jerasure.o liberation.o
+ $(CC) $(CFLAGS) -o liberation_01 liberation_01.o liberation.o jerasure.o galois.o
+
+encoder.o: galois.h liberation.h jerasure.h reed_sol.h cauchy.h
+encoder: encoder.o galois.o jerasure.o liberation.o reed_sol.o cauchy.o
+ $(CC) $(CFLAGS) -o encoder encoder.o liberation.o jerasure.o galois.o reed_sol.o cauchy.o
+
+decoder.o: galois.h liberation.h jerasure.h reed_sol.h cauchy.h
+decoder: decoder.o galois.o jerasure.o liberation.o reed_sol.o cauchy.o
+ $(CC) $(CFLAGS) -o decoder decoder.o liberation.o jerasure.o galois.o reed_sol.o cauchy.o
diff --git a/Examples/reed_sol_01.c b/Examples/reed_sol_01.c
new file mode 100755
index 0000000..e1cea5c
--- /dev/null
+++ b/Examples/reed_sol_01.c
@@ -0,0 +1,177 @@
+/* Examples/reed_sol_01.c
+Jerasure - A C/C++ Library for a Variety of Reed-Solomon and RAID-6 Erasure Coding Techniques
+
+Revision 1.2A
+May 24, 2011
+
+James S. Plank
+Department of Electrical Engineering and Computer Science
+University of Tennessee
+Knoxville, TN 37996
+plank@cs.utk.edu
+
+Copyright (c) 2011, James S. Plank
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+ - Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ - Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+
+ - Neither the name of the University of Tennessee nor the names of its
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
+WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+
+
+ */
+
+
+/*
+ revised by S. Simmerman
+ 2/25/08
+*/
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "jerasure.h"
+#include "reed_sol.h"
+
+#define talloc(type, num) (type *) malloc(sizeof(type)*(num))
+
+usage(char *s)
+{
+ fprintf(stderr, "usage: reed_sol_01 k m w - Does a simple Reed-Solomon coding example in GF(2^w).\n");
+ fprintf(stderr, " \n");
+ fprintf(stderr, " w must be 8, 16 or 32. k+m must be <= 2^w. It sets up a classic\n");
+ fprintf(stderr, " Vandermonde-based distribution matrix and encodes k devices of\n");
+ fprintf(stderr, " %d bytes each with it. Then it decodes.\n", sizeof(long));
+ fprintf(stderr, " \n");
+ fprintf(stderr, "This demonstrates: jerasure_matrix_encode()\n");
+ fprintf(stderr, " jerasure_matrix_decode()\n");
+ fprintf(stderr, " jerasure_print_matrix()\n");
+ fprintf(stderr, " reed_sol_vandermonde_coding_matrix()\n");
+ if (s != NULL) fprintf(stderr, "%s\n", s);
+ exit(1);
+}
+
+static void print_data_and_coding(int k, int m, int w, int size,
+ char **data, char **coding)
+{
+ int i, j, x;
+ int n, sp;
+ long l;
+
+ if(k > m) n = k;
+ else n = m;
+ sp = size * 2 + size/(w/8) + 8;
+
+ printf("%-*sCoding\n", sp, "Data");
+ for(i = 0; i < n; i++) {
+ if(i < k) {
+ printf("D%-2d:", i);
+ for(j=0;j< size; j+=(w/8)) {
+ printf(" ");
+ for(x=0;x < w/8;x++){
+ printf("%02x", (unsigned char)data[i][j+x]);
+ }
+ }
+ printf(" ");
+ }
+ else printf("%*s", sp, "");
+ if(i < m) {
+ printf("C%-2d:", i);
+ for(j=0;j< size; j+=(w/8)) {
+ printf(" ");
+ for(x=0;x < w/8;x++){
+ printf("%02x", (unsigned char)coding[i][j+x]);
+ }
+ }
+ }
+ printf("\n");
+ }
+ printf("\n");
+}
+
+int main(int argc, char **argv)
+{
+ long l;
+ int k, w, i, j, m;
+ int *matrix;
+ char **data, **coding;
+ int *erasures, *erased;
+ int *decoding_matrix, *dm_ids;
+
+ if (argc != 4) usage(NULL);
+ if (sscanf(argv[1], "%d", &k) == 0 || k <= 0) usage("Bad k");
+ if (sscanf(argv[2], "%d", &m) == 0 || m <= 0) usage("Bad m");
+ if (sscanf(argv[3], "%d", &w) == 0 || (w != 8 && w != 16 && w != 32)) usage("Bad w");
+ if (w <= 16 && k + m > (1 << w)) usage("k + m is too big");
+
+ matrix = reed_sol_vandermonde_coding_matrix(k, m, w);
+
+ printf("Last m rows of the Distribution Matrix:\n\n");
+ jerasure_print_matrix(matrix, m, k, w);
+ printf("\n");
+
+ srand48(0);
+ data = talloc(char *, k);
+ for (i = 0; i < k; i++) {
+ data[i] = talloc(char, sizeof(long));
+ l = lrand48();
+ memcpy(data[i], &l, sizeof(long));
+ }
+
+ coding = talloc(char *, m);
+ for (i = 0; i < m; i++) {
+ coding[i] = talloc(char, sizeof(long));
+ }
+
+ jerasure_matrix_encode(k, m, w, matrix, data, coding, sizeof(long));
+
+ printf("Encoding Complete:\n\n");
+ print_data_and_coding(k, m, w, sizeof(long), data, coding);
+
+ erasures = talloc(int, (m+1));
+ erased = talloc(int, (k+m));
+ for (i = 0; i < m+k; i++) erased[i] = 0;
+ l = 0;
+ for (i = 0; i < m; ) {
+ erasures[i] = lrand48()%(k+m);
+ if (erased[erasures[i]] == 0) {
+ erased[erasures[i]] = 1;
+ memcpy((erasures[i] < k) ? data[erasures[i]] : coding[erasures[i]-k], &l, sizeof(long));
+ i++;
+ }
+ }
+ erasures[i] = -1;
+
+ printf("Erased %d random devices:\n\n", m);
+ print_data_and_coding(k, m, w, sizeof(long), data, coding);
+
+ i = jerasure_matrix_decode(k, m, w, matrix, 1, erasures, data, coding, sizeof(long));
+
+ printf("State of the system after decoding:\n\n");
+ print_data_and_coding(k, m, w, sizeof(long), data, coding);
+
+ return 0;
+}
diff --git a/Examples/reed_sol_02.c b/Examples/reed_sol_02.c
new file mode 100755
index 0000000..198869b
--- /dev/null
+++ b/Examples/reed_sol_02.c
@@ -0,0 +1,101 @@
+/* Examples/reed_sol_02.c
+Jerasure - A C/C++ Library for a Variety of Reed-Solomon and RAID-6 Erasure Coding Techniques
+
+Revision 1.2A
+May 24, 2011
+
+James S. Plank
+Department of Electrical Engineering and Computer Science
+University of Tennessee
+Knoxville, TN 37996
+plank@cs.utk.edu
+
+Copyright (c) 2011, James S. Plank
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+ - Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ - Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+
+ - Neither the name of the University of Tennessee nor the names of its
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
+WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+
+
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "jerasure.h"
+#include "reed_sol.h"
+
+#define talloc(type, num) (type *) malloc(sizeof(type)*(num))
+
+usage(char *s)
+{
+ fprintf(stderr, "usage: reed_sol_02 k m w - Vandermonde matrices in GF(2^w).\n");
+ fprintf(stderr, " \n");
+ fprintf(stderr, " k+m must be <= 2^w. This simply prints out the \n");
+ fprintf(stderr, " Vandermonde matrix in GF(2^w), and then the distribution\n");
+ fprintf(stderr, " matrix that is constructed from it. See [Plank-Ding-05] for\n");
+ fprintf(stderr, " information on how this construction proceeds\n");
+ fprintf(stderr, " \n");
+ fprintf(stderr, "This demonstrates: reed_sol_extended_vandermonde_matrix()\n");
+ fprintf(stderr, " reed_sol_big_vandermonde_coding_matrix()\n");
+ fprintf(stderr, " reed_sol_vandermonde_coding_matrix()\n");
+ fprintf(stderr, " jerasure_print_matrix()\n");
+ if (s != NULL) fprintf(stderr, "%s\n", s);
+ exit(1);
+}
+
+int main(int argc, char **argv)
+{
+ long l;
+ int k, w, i, j, m;
+ int *matrix;
+
+ if (argc != 4) usage(NULL);
+ if (sscanf(argv[1], "%d", &k) == 0 || k <= 0) usage("Bad k");
+ if (sscanf(argv[2], "%d", &m) == 0 || m <= 0) usage("Bad m");
+ if (sscanf(argv[3], "%d", &w) == 0 || w <= 0 || w > 32) usage("Bad w");
+ if (w <= 30 && k + m > (1 << w)) usage("k + m is too big");
+
+ matrix = reed_sol_extended_vandermonde_matrix(k+m, k, w);
+ printf("Extended Vandermonde Matrix:\n\n");
+ jerasure_print_matrix(matrix, k+m, k, w);
+ printf("\n");
+
+ matrix = reed_sol_big_vandermonde_distribution_matrix(k+m, k, w);
+ printf("Vandermonde Distribution Matrix:\n\n");
+ jerasure_print_matrix(matrix, k+m, k, w);
+ printf("\n");
+
+ matrix = reed_sol_vandermonde_coding_matrix(k, m, w);
+ printf("Vandermonde Coding Matrix:\n\n");
+ jerasure_print_matrix(matrix, m, k, w);
+ printf("\n");
+
+
+ return 0;
+}
diff --git a/Examples/reed_sol_03.c b/Examples/reed_sol_03.c
new file mode 100755
index 0000000..a2c14c9
--- /dev/null
+++ b/Examples/reed_sol_03.c
@@ -0,0 +1,178 @@
+/* Examples/reed_sol_03.c
+Jerasure - A C/C++ Library for a Variety of Reed-Solomon and RAID-6 Erasure Coding Techniques
+
+Revision 1.2A
+May 24, 2011
+
+James S. Plank
+Department of Electrical Engineering and Computer Science
+University of Tennessee
+Knoxville, TN 37996
+plank@cs.utk.edu
+
+Copyright (c) 2011, James S. Plank
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+ - Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ - Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+
+ - Neither the name of the University of Tennessee nor the names of its
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
+WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+
+
+ */
+
+
+/*
+ revised by S. Simmerman
+ 2/25/08
+*/
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "jerasure.h"
+#include "reed_sol.h"
+
+#define talloc(type, num) (type *) malloc(sizeof(type)*(num))
+
+usage(char *s)
+{
+ fprintf(stderr, "usage: reed_sol_03 k w - Does a simple RAID-6 coding example in GF(2^w).\n");
+ fprintf(stderr, " \n");
+ fprintf(stderr, " w must be 8, 16 or 32. k+2 must be <= 2^w. It sets up a classic\n");
+ fprintf(stderr, " RAID-6 coding matrix based on Anvin's optimization and encodes\n");
+ fprintf(stderr, " %d-byte devices with it. Then it decodes.\n", sizeof(long));
+ fprintf(stderr, " \n");
+ fprintf(stderr, "This demonstrates: reed_sol_r6_encode()\n");
+ fprintf(stderr, " reed_sol_r6_coding_matrix()\n");
+ fprintf(stderr, " jerasure_matrix_decode()\n");
+ fprintf(stderr, " jerasure_print_matrix()\n");
+ if (s != NULL) fprintf(stderr, "%s\n", s);
+ exit(1);
+}
+
+
+static void print_data_and_coding(int k, int m, int w, int size,
+ char **data, char **coding)
+{
+ int i, j, x;
+ int n, sp;
+ long l;
+
+ if(k > m) n = k;
+ else n = m;
+ sp = size * 2 + size/(w/8) + 8;
+
+ printf("%-*sCoding\n", sp, "Data");
+ for(i = 0; i < n; i++) {
+ if(i < k) {
+ printf("D%-2d:", i);
+ for(j=0;j< size; j+=(w/8)) {
+ printf(" ");
+ for(x=0;x < w/8;x++){
+ printf("%02x", (unsigned char)data[i][j+x]);
+ }
+ }
+ printf(" ");
+ }
+ else printf("%*s", sp, "");
+ if(i < m) {
+ printf("C%-2d:", i);
+ for(j=0;j< size; j+=(w/8)) {
+ printf(" ");
+ for(x=0;x < w/8;x++){
+ printf("%02x", (unsigned char)coding[i][j+x]);
+ }
+ }
+ }
+ printf("\n");
+ }
+ printf("\n");
+}
+
+int main(int argc, char **argv)
+{
+ long l;
+ int k, w, i, j, m;
+ int *matrix;
+ char **data, **coding;
+ int *erasures, *erased;
+ int *decoding_matrix, *dm_ids;
+
+ if (argc != 3) usage(NULL);
+ if (sscanf(argv[1], "%d", &k) == 0 || k <= 0) usage("Bad k");
+ if (sscanf(argv[2], "%d", &w) == 0 || (w != 8 && w != 16 && w != 32)) usage("Bad w");
+ m = 2;
+ if (w <= 16 && k + m > (1 << w)) usage("k + m is too big");
+
+ matrix = reed_sol_r6_coding_matrix(k, w);
+
+ printf("Last 2 rows of the Distribution Matrix:\n\n");
+ jerasure_print_matrix(matrix, m, k, w);
+ printf("\n");
+
+ srand48(0);
+ data = talloc(char *, k);
+ for (i = 0; i < k; i++) {
+ data[i] = talloc(char, sizeof(long));
+ l = lrand48();
+ memcpy(data[i], &l, sizeof(long));
+ }
+
+ coding = talloc(char *, m);
+ for (i = 0; i < m; i++) {
+ coding[i] = talloc(char, sizeof(long));
+ }
+
+ reed_sol_r6_encode(k, w, data, coding, sizeof(long));
+
+ printf("Encoding Complete:\n\n");
+ print_data_and_coding(k, m, w, sizeof(long), data, coding);
+
+ erasures = talloc(int, (m+1));
+ erased = talloc(int, (k+m));
+ for (i = 0; i < m+k; i++) erased[i] = 0;
+ l = 0;
+ for (i = 0; i < m; ) {
+ erasures[i] = lrand48()%(k+m);
+ if (erased[erasures[i]] == 0) {
+ erased[erasures[i]] = 1;
+ memcpy((erasures[i] < k) ? data[erasures[i]] : coding[erasures[i]-k], &l, sizeof(long));
+ i++;
+ }
+ }
+ erasures[i] = -1;
+
+ printf("Erased %d random devices:\n\n", m);
+ print_data_and_coding(k, m, w, sizeof(long), data, coding);
+
+ i = jerasure_matrix_decode(k, m, w, matrix, 1, erasures, data, coding, sizeof(long));
+
+ printf("State of the system after decoding:\n\n");
+ print_data_and_coding(k, m, w, sizeof(long), data, coding);
+
+ return 0;
+}
diff --git a/Examples/reed_sol_04.c b/Examples/reed_sol_04.c
new file mode 100755
index 0000000..686a5b4
--- /dev/null
+++ b/Examples/reed_sol_04.c
@@ -0,0 +1,112 @@
+/* Examples/reed_sol_04.c
+
+Jerasure - A C/C++ Library for a Variety of Reed-Solomon and RAID-6 Erasure Coding Techniques
+
+Revision 1.2A
+May 24, 2011
+
+James S. Plank
+Department of Electrical Engineering and Computer Science
+University of Tennessee
+Knoxville, TN 37996
+plank@cs.utk.edu
+
+Copyright (c) 2011, James S. Plank
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+ - Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ - Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+
+ - Neither the name of the University of Tennessee nor the names of its
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
+WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+
+
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "jerasure.h"
+#include "reed_sol.h"
+
+#define talloc(type, num) (type *) malloc(sizeof(type)*(num))
+
+usage(char *s)
+{
+ fprintf(stderr, "usage: reed_sol_04 w - Shows reed_sol_galois_wXX_region_multby_2\n");
+ fprintf(stderr, " \n");
+ fprintf(stderr, " w must be 8, 16 or 32. Sets up an array of 4 random words in\n");
+ fprintf(stderr, " GF(2^w) and multiplies them by two. \n");
+ fprintf(stderr, " \n");
+ fprintf(stderr, "This demonstrates: reed_sol_galois_w08_region_multby_2()\n");
+ fprintf(stderr, " reed_sol_galois_w16_region_multby_2()\n");
+ fprintf(stderr, " reed_sol_galois_w32_region_multby_2()\n");
+ if (s != NULL) fprintf(stderr, "%s\n", s);
+ exit(1);
+}
+
+int main(int argc, char **argv)
+{
+ unsigned char *x, *y;
+ unsigned short *xs, *ys;
+ unsigned int *xi, *yi;
+ int *a32, *copy;
+ int i;
+ int w;
+
+ if (argc != 2) usage(NULL);
+ if (sscanf(argv[1], "%d", &w) == 0 || (w != 8 && w != 16 && w != 32)) usage("Bad w");
+
+ srand48(time(0));
+ a32 = talloc(int, 4);
+ copy = talloc(int, 4);
+ y = (unsigned char *) a32;
+ for (i = 0; i < 4*sizeof(int); i++) y[i] = lrand48()%255;
+ memcpy(copy, a32, sizeof(int)*4);
+
+ if (w == 8) {
+ x = (unsigned char *) copy;
+ y = (unsigned char *) a32;
+ reed_sol_galois_w08_region_multby_2((char *) a32, sizeof(int)*4);
+ for (i = 0; i < 4*sizeof(int)/sizeof(char); i++) {
+ printf("Char %2d: %3u *2 = %3u\n", i, x[i], y[i]);
+ }
+ } else if (w == 16) {
+ xs = (unsigned short *) copy;
+ ys = (unsigned short *) a32;
+ reed_sol_galois_w16_region_multby_2((char *) a32, sizeof(int)*4);
+ for (i = 0; i < 4*sizeof(int)/sizeof(short); i++) {
+ printf("Short %2d: %5u *2 = %5u\n", i, xs[i], ys[i]);
+ }
+ } else if (w == 32) {
+ xi = (unsigned int *) copy;
+ yi = (unsigned int *) a32;
+ reed_sol_galois_w16_region_multby_2((char *) a32, sizeof(int)*4);
+ for (i = 0; i < 4*sizeof(int)/sizeof(int); i++) {
+ printf("Int %2d: %10u *2 = %10u\n", i, xi[i], yi[i]);
+ }
+ }
+}