Example #1
0
//
// Use getopts()
//
int main(int argc, char **argv){
    //struct that holds the passed arguments
    struct arguments facts;
    //input format
    //2 argument
    //--file to read
    //--number of chunks to create
    //check input
    if(argc == 3){
        //2 arguments
        facts = parse_args(argc, argv);
    } else {
        //too many arguments
        printf("Invalid input. Valid arguments:\n");
        printf("Filename and number of chunks to make.\n");
        return 0;
    }

    // Initialize GF
    GF16init();
    
    //gets file and encodes
    encodeFile(facts);
    
    printf("Done\n");
    
    return 0;
    
    //intput
    //uint16_t *input;
    
    /*
    //output chunks
    struct chunk output[facts.num_of_chunks];
    int i;
    for(i = 0 ; i < facts.num_of_chunks ; i++){
        struct chunk out;
        out.numEmpty = 0;
        out.output = NULL;
        output[i] = out;
    }
    */

    
    
    
    /*
    //get coefficients (new)
    for (i = 0; i < 3; i++) {
        for(j = 0; j < 3; j++) {
            //make sure coefficients aren't 0
            while(1) {
                output[i].coef[j] = mt_rand16();
                if(output[i].coef[j] != 0)
                    break;
            }
        }
    }

    //    uint16_t    a[3][3], b[3], x[3];
    //b[0] = a[0][0] * x[0] + a[0][1] * x[1] + a[0][2] * x[2];
    //b[1] = a[1][0] * x[0] + a[1][1] * x[1] + a[1][2] * x[2];
    //b[2] = a[2][0] * x[0] + a[2][1] * x[1] + a[2][2] * x[2];

    printf("Encoding\n");
    
    //encode section
    for(i = 0, t = 0; i < DATA_LENGTH ; t++, i += 3){
        output[0].output[t] = GF16mul(output[0].coef[0], input[i]) ^ GF16mul(output[0].coef[1], input[i+1]) ^ GF16mul(output[0].coef[2], input[i+2]);
        output[1].output[t] = GF16mul(output[1].coef[0], input[i]) ^ GF16mul(output[1].coef[1], input[i+1]) ^ GF16mul(output[1].coef[2], input[i+2]);
        output[2].output[t] = GF16mul(output[2].coef[0], input[i]) ^ GF16mul(output[2].coef[1], input[i+1]) ^ GF16mul(output[2].coef[2], input[i+2]);
    }
    */
    /*
    printf("Decoding\n");
    
    //variables
    uint16_t *final;
    
    //allocate space for final file
    if ((final = malloc(DATA_LENGTH * sizeof *input)) == NULL) { // 16bit
        perror("malloc");
        exit(1);
    }
    
    //set up c variables
    uint16_t c0;
    uint16_t c1;
    uint16_t c2;
    uint16_t c3;
    uint16_t c4;
    
    //do math for c variables
    c0 = GF16mul(output[1].coef[0], output[0].coef[1]) ^ GF16mul(output[0].coef[0], output[1].coef[1]);
    c1 = GF16mul(output[1].coef[0], output[0].coef[2]) ^ GF16mul(output[0].coef[0], output[1].coef[2]);
    c2 = GF16mul(output[2].coef[0], output[0].coef[1]) ^ GF16mul(output[0].coef[0], output[2].coef[1]);
    c3 = GF16mul(output[2].coef[0], output[0].coef[2]) ^ GF16mul(output[0].coef[0], output[2].coef[2]);
    c4 = GF16mul(c1, c2) ^ GF16mul(c0, c3);
    
    uint16_t *t0 = malloc(DATA_LENGTH/3 * sizeof *t0);
    uint16_t *t1 = malloc(DATA_LENGTH/3 * sizeof *t1);
    
    for(i = 0 ; i < DATA_LENGTH/3 ; i++){
        t0[i] = GF16mul(output[1].coef[0], output[0].output[i]) ^ GF16mul(output[0].coef[0], output[1].output[i]);
        t1[i] = GF16mul(output[2].coef[0], output[0].output[i]) ^ GF16mul(output[0].coef[0], output[2].output[i]);
    }
    
    uint16_t *x1 = malloc(DATA_LENGTH/3 * sizeof *x1);
    uint16_t *x2 = malloc(DATA_LENGTH/3 * sizeof *x2);
    uint16_t *x3 = malloc(DATA_LENGTH/3 * sizeof *x3);
    
    for(i = 0 ; i < DATA_LENGTH/3 ; i++){
        x2[i] = GF16div((GF16mul(c2, t0[i]) ^ GF16mul(c0, t1[i])),c4);
        x1[i] = GF16div((t0[i] ^ GF16mul(c1, x2[i])),c0);
        x3[i] = GF16div((output[0].output[i] ^ GF16mul(output[0].coef[1], x1[i]) ^ GF16mul(output[0].coef[2], x2[i])),output[0].coef[0]);
    }
    
    for(i = 0, t = 0 ; i < DATA_LENGTH ; i +=3, t++){
        //final[i] = x1[t];
        //final[i+1] = x2[t];
        //final[i+2] = x3[t];
        final[i] = x3[t];
        final[i+1] = x1[t];
        final[i+2] = x2[t];
    }
    
    printf("Checking...\n");
    
    //check
    for( i = 0 ; i < DATA_LENGTH ; i++){
        if(input[i] != final[i]){
            printf("Not equal at %d\n", i);
            printf("%d and %d\n", input[i], final[i]);
            //printf("%d and %d\n", input[i+1], final[i+1]);
            //printf("%d and %d\n", input[i+2], final[i+2]);
            break;
        }
    }
    */
    
    //printf("Done\n");
    
    /*
    free(input);
    free(output[0].output);
    free(output[1].output);
    free(output[2].output);
    */
    /*
    free(final);
    free(x1);
    free(x2);
    free(x3);
    */
    
    //return 0;
}
Example #2
0
//
// Use getopts()
//
int main(int argc, char **argv) {
    //input format
    //no input for random input
    //1 argument that is a file and number of chunks to create
    //3 arguments that are the chunks to use to create a file
    //check inout
    int state;
    if(argc == 1) {
        //0 argument
        //random input
        state = 0;
    } else if(argc == 3) {
        //2 arguments
        //file input
        //number of chunks to make
        state = 1;
    } else if(argc == 4) {
        //3 arguments
        //3 chunks to use in creating file
        state = 2;
    } else {
        //too many arguments
        printf("Invalid input. Valid arguments:\n");
        printf("No arguments for random input.\nFilename and number of chunks to make.\nOr three files to reassemble\n");
        return 0;
    }

    // Initialize GF
    GF16init();

    //set up of variables
    //intput
    uint16_t *input;

    //output chunks
    struct chunk output[3];
    struct chunk out1;
    struct chunk out2;
    struct chunk out3;

    output[0] = out1;
    output[1] = out2;
    output[2] = out3;

    //loop variables
    int i, j, t;

    //data length variable
    int DATA_LENGTH;

    // Allocate size for input and output
    //check based on state
    if( state == 0 ) {
        //No file. So random file
        DATA_LENGTH = SPACE / sizeof(uint16_t);
        //input allocation
        if ((input = malloc(DATA_LENGTH * sizeof *input)) == NULL) { // 16bit
            perror("malloc");
            exit(1);
        }
        //fill input with random numbers
        for (i = 0; i < DATA_LENGTH ; i++) {
            input[i] = mt_rand16();
        }
        //output allocation
        if ((output[0].output = malloc(DATA_LENGTH * sizeof *output[0].output)) == NULL) { // 16bit
            perror("malloc");
            exit(1);
        }
        if ((output[1].output = malloc(DATA_LENGTH * sizeof *output[1].output)) == NULL) { // 16bit
            perror("malloc");
            exit(1);
        }
        if ((output[2].output = malloc(DATA_LENGTH * sizeof *output[2].output)) == NULL) { // 16bit
            perror("malloc");
            exit(1);
        }
    } else if(state == 1) {
        //file input
        //file io operations here
        //...
        encodeFile(argv, output);
    } else if(state == 2) {
        //3 files input for reassembly
    }

    //get coefficients (new)
    for (i = 0; i < 3; i++) {
        for(j = 0; j < 3; j++) {
            //make sure coefficients aren't 0
            while(1) {
                output[i].coef[j] = mt_rand16();
                if(output[i].coef[j] != 0)
                    break;
            }
        }
    }

    //    uint16_t    a[3][3], b[3], x[3];
    //b[0] = a[0][0] * x[0] + a[0][1] * x[1] + a[0][2] * x[2];
    //b[1] = a[1][0] * x[0] + a[1][1] * x[1] + a[1][2] * x[2];
    //b[2] = a[2][0] * x[0] + a[2][1] * x[1] + a[2][2] * x[2];

    printf("Encoding\n");

    //encode section
    for(i = 0, t = 0; i < DATA_LENGTH ; t++, i += 3) {
        output[0].output[t] = GF16mul(output[0].coef[0], input[i]) ^ GF16mul(output[0].coef[1], input[i+1]) ^ GF16mul(output[0].coef[2], input[i+2]);
        output[1].output[t] = GF16mul(output[1].coef[0], input[i]) ^ GF16mul(output[1].coef[1], input[i+1]) ^ GF16mul(output[1].coef[2], input[i+2]);
        output[2].output[t] = GF16mul(output[2].coef[0], input[i]) ^ GF16mul(output[2].coef[1], input[i+1]) ^ GF16mul(output[2].coef[2], input[i+2]);
    }


    printf("Decoding\n");

    //variables
    uint16_t *final;

    //allocate space for final file
    if ((final = malloc(DATA_LENGTH * sizeof *input)) == NULL) { // 16bit
        perror("malloc");
        exit(1);
    }

    //set up c variables
    uint16_t c0;
    uint16_t c1;
    uint16_t c2;
    uint16_t c3;
    uint16_t c4;

    //do math for c variables
    c0 = GF16mul(output[1].coef[0], output[0].coef[1]) ^ GF16mul(output[0].coef[0], output[1].coef[1]);
    c1 = GF16mul(output[1].coef[0], output[0].coef[2]) ^ GF16mul(output[0].coef[0], output[1].coef[2]);
    c2 = GF16mul(output[2].coef[0], output[0].coef[1]) ^ GF16mul(output[0].coef[0], output[2].coef[1]);
    c3 = GF16mul(output[2].coef[0], output[0].coef[2]) ^ GF16mul(output[0].coef[0], output[2].coef[2]);
    c4 = GF16mul(c1, c2) ^ GF16mul(c0, c3);

    uint16_t *t0 = malloc(DATA_LENGTH/3 * sizeof *t0);
    uint16_t *t1 = malloc(DATA_LENGTH/3 * sizeof *t1);

    for(i = 0 ; i < DATA_LENGTH/3 ; i++) {
        t0[i] = GF16mul(output[1].coef[0], output[0].output[i]) ^ GF16mul(output[0].coef[0], output[1].output[i]);
        t1[i] = GF16mul(output[2].coef[0], output[0].output[i]) ^ GF16mul(output[0].coef[0], output[2].output[i]);
    }

    uint16_t *x1 = malloc(DATA_LENGTH/3 * sizeof *x1);
    uint16_t *x2 = malloc(DATA_LENGTH/3 * sizeof *x2);
    uint16_t *x3 = malloc(DATA_LENGTH/3 * sizeof *x3);

    for(i = 0 ; i < DATA_LENGTH/3 ; i++) {
        x2[i] = GF16div((GF16mul(c2, t0[i]) ^ GF16mul(c0, t1[i])),c4);
        x1[i] = GF16div((t0[i] ^ GF16mul(c1, x2[i])),c0);
        x3[i] = GF16div((output[0].output[i] ^ GF16mul(output[0].coef[1], x1[i]) ^ GF16mul(output[0].coef[2], x2[i])),output[0].coef[0]);
    }

    for(i = 0, t = 0 ; i < DATA_LENGTH ; i +=3, t++) {
        //final[i] = x1[t];
        //final[i+1] = x2[t];
        //final[i+2] = x3[t];
        final[i] = x3[t];
        final[i+1] = x1[t];
        final[i+2] = x2[t];