eccSamsung.c 7.12 KB
/*******************************************************************/
/* Revision History                        */
/* Revision 2.1 was revised for speed-up               */
/* Revision 3.0 was revised for speed-up               */
/* Description : The following source code shows an example for    */
/* generating of 3bytes per 256bytes, based on hamming code ecc    */ 
/*******************************************************************/
#include <stdio.h>
#include <string.h>
#include <malloc.h>


typedef unsigned int uint;
typedef unsigned char uchar;


/*
    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              */
}



main(int argc,char *argv[])
{
    uint i, j;
    uchar   ecc_gen[3],write_data[256],paritr[256], tmp = 0, tmp2=0;
    uchar   data_table0[16] = {0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0};
    uchar   data_table1[16] = {1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1};
    uchar   sum=0,paritc = 0;
    uchar   parit0c = 0, parit1c = 0, parit2c = 0, parit3c = 0;
    uchar   parit4c = 0, parit5c = 0, parit6c = 0, parit7c = 0;
    uchar   parit1_1, parit1_2, parit2_1, parit2_2, parit4_1, parit4_2;
    uchar   parit8_1 = 0, parit8_2 = 0, parit16_1 = 0, parit16_2 = 0, parit32_1 = 0, parit32_2 = 0;
    uchar   parit64_1 = 0, parit64_2 = 0, parit128_1 = 0, parit128_2 = 0, parit256_1 = 0, parit256_2 = 0;
    uchar   parit512_1 = 0, parit512_2 = 0, parit1024_1 = 0, parit1024_2 = 0;

    if(argc!=2){
        printf("takes one argument which is data file name\n");
        exit(1);
    }

    //strcpy(inp_file,argv[2]);
    get_data_gen(argv[1],write_data);   /* Get DATA for calculating      */

    for( i = 0; i < 256; i++)
    {
        paritc = paritc ^ (*(write_data+i));

        tmp = (*(write_data+i) & 0xf0) >> 4;
        tmp2 = *(write_data+i) & 0x0f;
        switch(tmp)
        {
            case 0:
                *(paritr + i) = *(data_table0 + tmp2);
                break;
            case 1:
                *(paritr + i) = *(data_table1 + tmp2);
                break;
            case 2:
                *(paritr + i) = *(data_table1 + tmp2);
                break;
            case 3:
                *(paritr + i) = *(data_table0 + tmp2);
                break;
            case 4:
                *(paritr + i) = *(data_table1 + tmp2);
                break;
            case 5:
                *(paritr + i) = *(data_table0 + tmp2);
                break;
            case 6:
                *(paritr + i) = *(data_table0 + tmp2);
                break;
            case 7:
                *(paritr + i) = *(data_table1 + tmp2);
                break;
            case 8:
                *(paritr + i) = *(data_table1 + tmp2);
                break;
            case 9:
                *(paritr + i) = *(data_table0 + tmp2);
                break;
            case 10:
                *(paritr + i) = *(data_table0 + tmp2);
                break;
            case 11:
                *(paritr + i) = *(data_table1 + tmp2);
                break;
            case 12:
                *(paritr + i) = *(data_table0 + tmp2);
                break;
            case 13:
                *(paritr + i) = *(data_table1 + tmp2);
                break;
            case 14:
                *(paritr + i) = *(data_table1 + tmp2);
                break;
            case 15:
                *(paritr + i) = *(data_table0 + tmp2);
                break;
        }

    }

    parit0c = ((paritc & 0x01) ? 1 : 0);
    parit1c = ((paritc & 0x02) ? 1 : 0);
    parit2c = ((paritc & 0x04) ? 1 : 0);
    parit3c = ((paritc & 0x08) ? 1 : 0);
    parit4c = ((paritc & 0x10) ? 1 : 0);
    parit5c = ((paritc & 0x20) ? 1 : 0);
    parit6c = ((paritc & 0x40) ? 1 : 0);
    parit7c = ((paritc & 0x80) ? 1 : 0);

    parit1_2 = parit6c ^ parit4c ^ parit2c ^ parit0c;
    parit1_1 = parit7c ^ parit5c ^ parit3c ^ parit1c;
    parit2_2 = parit5c ^ parit4c ^ parit1c ^ parit0c;
    parit2_1 = parit7c ^ parit6c ^ parit3c ^ parit2c;
    parit4_2 = parit3c ^ parit2c ^ parit1c ^ parit0c;
    parit4_1 = parit7c ^ parit6c ^ parit5c ^ parit4c;

    for( i = 0 ; i < 256;i++)   sum=sum ^ (*(paritr+i));

    for ( i = 0; i < 256; i = i+2 )
        parit8_2 = parit8_2 ^ (*(paritr + i));
    for ( i = 0; i < 256; i = i+4 )
    {
        parit16_2 = parit16_2 ^ (*(paritr + i));
        parit16_2 = parit16_2 ^ (*(paritr + i + 1));
    }
    for ( i = 0; i < 256; i = i+8 )
    {
        for ( j = 0; j <= 3; j++ )
            parit32_2 = parit32_2 ^ (*(paritr + i + j));
    }
    for ( i = 0; i < 256; i = i+16 )
    {
        for ( j = 0; j <= 7; j++ )
            parit64_2 = parit64_2 ^ (*(paritr + i + j));
    }
    for ( i = 0; i < 256; i = i+32 )
    {
        for ( j = 0; j <= 15; j++ )
            parit128_2 = parit128_2 ^ (*(paritr + i + j));
    }
    for ( i = 0; i < 256; i = i+64 )
    {
        for ( j = 0; j <= 31; j++ )
            parit256_2 = parit256_2 ^ (*(paritr + i + j));
    }
    for ( i = 0; i < 256; i = i+128 )
    {
        for ( j = 0; j <= 63; j++ )
            parit512_2 = parit512_2 ^ (*(paritr + i + j));
    }
    for ( i = 0; i < 256; i = i+256 )
    {
        for ( j = 0; j <= 127; j++ )
            parit1024_2 = parit1024_2 ^ (*(paritr + i + j));
    }

    if(sum==0){
        parit1024_1=parit1024_2;
        parit512_1=parit512_2;
        parit256_1=parit256_2;
        parit128_1=parit128_2;
        parit64_1=parit64_2;
        parit32_1=parit32_2;
        parit16_1=parit16_2;
        parit8_1=parit8_2;
    }
    else{
        parit1024_1 = (parit1024_2 ? 0 : 1);
        parit512_1  = (parit512_2  ? 0 : 1);
        parit256_1  = (parit256_2  ? 0 : 1);
        parit128_1  = (parit128_2  ? 0 : 1);
        parit64_1   = (parit64_2   ? 0 : 1);
        parit32_1   = (parit32_2   ? 0 : 1);
        parit16_1   = (parit16_2   ? 0 : 1);
        parit8_1    = (parit8_2    ? 0 : 1);
    }

    parit1_2 <<= 2;
    parit1_1 <<= 3;
    parit2_2 <<= 4;
    parit2_1 <<= 5;
    parit4_2 <<= 6;
    parit4_1 <<= 7;
    parit128_1 <<= 1;
    parit256_2 <<= 2;
    parit256_1 <<= 3;
    parit512_2 <<= 4;
    parit512_1 <<= 5;
    parit1024_2 <<= 6;
    parit1024_1 <<= 7;
    parit8_1 <<= 1;
    parit16_2 <<= 2;

    parit16_1 <<= 3;
    parit32_2 <<= 4;
    parit32_1 <<= 5;
    parit64_2 <<= 6;
    parit64_1 <<= 7;

    *(ecc_gen + 0) = ~( parit64_1|parit64_2|parit32_1|parit32_2|parit16_1|parit16_2|parit8_1|parit8_2 );
    *(ecc_gen + 1) = ~( parit1024_1|parit1024_2|parit512_1|parit512_2|parit256_1|parit256_2|parit128_1|parit128_2 );
    *(ecc_gen + 2) = ~( parit4_1|parit4_2|parit2_1|parit2_2|parit1_1|parit1_2|0x00|0x00 );

    printf("Result %02X %02X %02X\n",ecc_gen[0],ecc_gen[1],ecc_gen[2]);
}