/* * Copyright (C) 2011 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 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; } unsigned char gfinv[256]; #define POWER_PARITY 4 #define POWER_DISK 255 /** * Setup the power matrix used to generate parity. * The first row is formed by all 1. * The second row contains power of 2, the third power of 4, and so on... * Note that this matrix is valid only for up to Quad Parity and for up to 21 data disks. */ void set_power(unsigned char* matrix) { int i, j; unsigned char g; g = 1; for(i=0;i> 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[8 * 256]; printf("/*\n"); printf(" * Copyright (C) 2011 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"); /* 4^a */ printf("const unsigned char __attribute__((aligned(256))) gfexp4[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, 4); 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"); /* x4l */ printf("#define GFX4L { "); for(i=0;i<16;++i) { v = gfmul(0x4, i); printf("0x%02x", (unsigned)v); if (i != 15) printf(", "); } printf(" }\n"); /* x4h */ printf("#define GFX4H { "); for(i=0;i<16;++i) { v = gfmul(0x40, i); printf("0x%02x", (unsigned)v); if (i != 15) printf(", "); } printf(" }\n"); /* x8l */ printf("#define GFX8L { "); for(i=0;i<16;++i) { v = gfmul(0x8, i); printf("0x%02x", (unsigned)v); if (i != 15) printf(", "); } printf(" }\n"); /* x8h */ printf("#define GFX8H { "); for(i=0;i<16;++i) { v = gfmul(0x80, i); printf("0x%02x", (unsigned)v); if (i != 15) printf(", "); } printf(" }\n"); /* poly */ printf("#define GFPOLY8 { 0x1d, 0x1d, 0x1d, 0x1d, 0x1d, 0x1d, 0x1d, 0x1d } \n"); printf("#define GFPOLY16 { 0x1d, 0x1d, 0x1d, 0x1d, 0x1d, 0x1d, 0x1d, 0x1d, 0x1d, 0x1d, 0x1d, 0x1d, 0x1d, 0x1d, 0x1d, 0x1d } \n"); printf("#define GFMASK16 { 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f } \n"); printf("\n"); /* power matrix */ set_power(matrix); printf("/**\n"); printf(" * Power matrix used to generate parity.\n"); printf(" * This matrix is valid for up to 4 parity with 21 data disks.\n"); printf(" *\n"); for(p=0;p