/* * Copyright (C) 2013 Andrea Mazzoleni * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include /** * Multiplication in GF(2^8). */ unsigned char gfmul(unsigned char a, unsigned char b) { unsigned char v; v = 0; while (b) { if ((b & 1) != 0) v ^= a; if ((a & 0x80) != 0) { a <<= 1; a ^= 0x1d; } else { a <<= 1; } b >>= 1; } return v; } /** * Inversion table in GF(2^8). */ unsigned char gfinv[256]; /** * Number of parity. * This is the number of rows of the generation matrix. */ #define PARITY 6 /** * Number of disks. * This is the number of columns of the generation matrix. */ #define DISK (257-PARITY) /** * Setup the Cauchy matrix used to generate the parity. */ void set_cauchy(unsigned char* matrix) { int i, j; unsigned char inv_x, y; /* * First row is formed by all 1. * * This is an Extended Cauchy matrix built from a Cauchy matrix * adding the first row of all 1. */ for(i=0;i=0,j>=1,i+j<255 */ y = 2; for(j=0;j> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >> 16; ++v; return v; } int main(void) { unsigned char v; int i, j, k, p; unsigned char matrix[PARITY * 256]; printf("/*\n"); printf(" * Copyright (C) 2013 Andrea Mazzoleni\n"); printf(" *\n"); printf(" * This program is free software: you can redistribute it and/or modify\n"); printf(" * it under the terms of the GNU General Public License as published by\n"); printf(" * the Free Software Foundation, either version 2 of the License, or\n"); printf(" * (at your option) any later version.\n"); printf(" *\n"); printf(" * This program is distributed in the hope that it will be useful,\n"); printf(" * but WITHOUT ANY WARRANTY; without even the implied warranty of\n"); printf(" * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"); printf(" * GNU General Public License for more details.\n"); printf(" *\n"); printf(" * You should have received a copy of the GNU General Public License\n"); printf(" * along with this program. If not, see .\n"); printf(" */\n"); printf("\n"); /* a*b */ printf("const unsigned char __attribute__((aligned(256))) gfmul[256][256] =\n"); printf("{\n"); for(i=0;i<256;++i) { printf("\t{\n"); for(j=0;j<256;++j) { if (j % 8 == 0) printf("\t\t"); v = gfmul(i, j); if (v == 1) gfinv[i] = j; printf("0x%02x,", (unsigned)v); if (j % 8 == 7) printf("\n"); else printf(" "); } printf("\t},\n"); } printf("};\n\n"); /* 2^a */ printf("const unsigned char __attribute__((aligned(256))) gfexp2[256] =\n"); printf("{\n"); v = 1; for(i=0;i<256;++i) { if (i % 8 == 0) printf("\t"); printf("0x%02x,", v); v = gfmul(v, 2); if (i % 8 == 7) printf("\n"); else printf(" "); } printf("};\n\n"); /* 1/a */ printf("const unsigned char __attribute__((aligned(256))) gfinv[256] =\n"); printf("{\n"); printf("\t/* note that the first element is not significative */\n"); for(i=0;i<256;++i) { if (i % 8 == 0) printf("\t"); if (i == 0) v = 0; else v = gfinv[i]; printf("0x%02x,", v); if (i % 8 == 7) printf("\n"); else printf(" "); } printf("};\n\n"); /* power matrix */ set_power(matrix); printf("/**\n"); printf(" * Power matrix used to generate parity.\n"); printf(" * This matrix is valid for up to %u parity with %u data disks.\n", 3, DISK); printf(" *\n"); for(p=0;p<3;++p) { printf(" * "); for(i=0;i