test_pi_ecc.c 3.33 KB
#include <stdio.h>
#include <string.h>
#include <malloc.h>

#include "pi_ecc.h"

/*
    Reading DATA file
*/
get_data_gen(unsigned char *file_name,unsigned char *data)
{
    int i,j;
    FILE *fdata;

    if ((fdata=fopen(file_name,"r"))==0) {    /* Open DATA file               */
        printf("Can't open '%s'!\7\n",file_name);
        exit(0);
    }
    for(i=0; i<256; ++i){
        fscanf(fdata,"%x",&j);    /* Reading DATA      */
        data[i]=(unsigned char)j;
    }
    fclose(fdata);                            /* Close DATA file              */
}

/*
    Reading DATA file
*/
get_data_cor(unsigned char *file_name,
             unsigned char *data,
             unsigned char *ecc
    )
{
    int i,j;
    FILE *fdata;

    if ((fdata=fopen(file_name,"r"))==0) {    /* Open DATA file               */
        printf("Can't open '%s'!\7\n",file_name);
        exit(0);
    }
    for(i=0; i<256; ++i){
        fscanf(fdata,"%x",&j);    /* Reading DATA      */
        data[i]=(unsigned char)j;
    }
    for(i=0;i<3;i++){
        fscanf(fdata,"%x",&j);    /* Reading DATA      */
        ecc[i]=(unsigned char)j;
    }
    fclose(fdata);                            /* Close DATA file              */
}

put_data(unsigned char *data,unsigned char *ecc)
{
    int i,j;

    for(i=0; i<16; ++i) {
        for(j=0; j<16; ++j)
            printf("%02x ",data[i*16+j]);    /* Wrinting DATA          */
        printf("\n");
    }
    printf("%02x %02x %02x",ecc[0],ecc[1],ecc[2]); /* Writing ECC code*/
}


main(int argc,char *argv[])
{
    int a;
    unsigned char *data;                     /* DATA                          */
    unsigned char inp_file[1024];            /* File name                     */
    unsigned char ecc[3];
       /* ecc[0]: LP15,LP14,LP13,...            */
       /* ecc[1]: LP07,LP06,LP05,...            */
       /* ecc[2]: CP5,CP4,CP3,...,"1","1"       */

    if(argc<3){
        printf("usage: test_pi_ecc <mode> <input_data_filename>\n");
        printf("    where <mode>:\n     g --> generate\n     c --> correct\n");
        printf("  <input_data_filename> contents such as cor_in_ex.dat.\n");
        printf("  The last 3 bytes of the file are output when the generate\n");
        printf("  option 'g' is used, and are not needed for 'c' (correct).\n");
        return;
    }

    /* DATA file default 'eec.dat'    */
    strcpy(inp_file,argv[2]);

    data=malloc(256);                        /* For DATA                      */
    if (data==0) {                           /* If no memory                  */
        printf("Insufficient memory!\n");
        exit(-1);
    }

    if(argv[1][0]=='g'){
        get_data_gen(inp_file,data);   /* Get DATA for calculating      */
        calculate_ecc(data,ecc);    /* Calculating ECC code   */
        printf("Result %02x %02x %02x\n",ecc[0],ecc[1],ecc[2]);
    }
    else if(argv[1][0]=='c'){
        get_data_cor(inp_file,data,ecc); /* Get DATA for calculating     */
        a = correct_ecc(data,ecc);
        printf("Result: ");
        switch(a) {
        case 0: printf("No error"); break;
        case 1: printf("Corrected\n"); put_data(data,ecc);break;
        case 2: printf("ECC error\n"); put_data(data,ecc);break;
        case 3: printf("Uncorrectable"); break;
        default: printf("Unknown error"); break;
        }
        printf("\n");
    }
    else
        printf("bad mode argument\n");
}