How to calculate reconciliation factor in CRC32 for CVN number and CALID

274 Views Asked by At

How to calculate reconciliation factor in CRC32 for CVN number and CALID Can someone help he with this How to calculate reconciliation factor in CRC32 and crc16.

but calculated factor value should be like to get same sum Every time for CVN calculation

I want formula of get offset or Reconciliation factor for CRC32 calculation .

Let me clarify for this : CVN : calibration Vehicle network identification . The solution as i want for ex: I have two different structure where we have 10 paramater and from that check sum value as ex 0xfefeABCD and 0x12345678 and on that structure im have to add one more parameter which calibration factor , When I add this parameter on both structure the checksum value get modify , but I need a algorithm for that to get same checksum value for both the structure by adding calibration factor and offset . NOTE : both structure have same 10 Variables but value is different , Im not having idea about value of these structure ,but still i need same checksum by adding factor value on structure .

Im using this function :

As the data im passing to function is :

final result will be store in buffer Please let me know what thing im missing so to get result as same we want . Im sure that im missing something .

Start code :

 #include <stdio.h>


#define CRCPOLY 0xEDB88320
#define CRCINV 0x5B358FD3 // inverse poly of (x ^N) mod CRCPOLY
#define INITXOR 0xFFFFFFFF
#define FINALXOR 0xFFFFFFFF



void make_crc_revtable ( unsigned int * crc_revtable ) ;
int crc32_bitoriented ( unsigned char * buffer , int length );
unsigned int  crc_table[256];
unsigned char  buffer[]= { 0x0, 0x1, 0x2, 0x3, 0x4,0x5, 0x6, 0x7, 0x8, 0x9,0xA, 0xB, 0xC, 0xD, 0xE,0xF, 0x0, 0x1, 0x2, 0x3,0x0, 0x0, 0x0, 0x0, 0x0 };
unsigned int  crc_revtable [256];
unsigned int   tcrcreg  ;
unsigned int   CRC_32 ;
unsigned int fix_pos = 21;
unsigned int length = 256;
void fix_crc_pos ( unsigned char * buffer ,int length ,unsigned int tcrcreg ,int fix_pos ,unsigned int * crc_table ,unsigned int * crc_revtable )
{
    int i;
    // make sure fix_pos is within 0..( length -1)
    fix_pos = ((fix_pos % length) + length) % length;

    // calculate crc register at position fix_pos ; this is essentially crc32 ()
    unsigned int crcreg = INITXOR ;
    for (i = 0; i < fix_pos ; ++i) 
    {
        crcreg = (crcreg >> 8) ^ crc_table[((crcreg ^ buffer [i]) & 0xFF)];
    }

    // inject crcreg as content
    for (i = 0; i < 4; ++i)
    {
        buffer[fix_pos + i] = ((crcreg >> i * 8) & 0xFF);
    }
    // calculate crc backwards to fix_pos , beginning at the end
    tcrcreg = (tcrcreg ^FINALXOR) ;
    for (i = length - 1; i >= fix_pos ; --i) 
    {
        tcrcreg = ((tcrcreg << 8) ^ (crc_revtable[tcrcreg >> 3*8] ^ buffer[i]));
    }

    // inject new content
    for (i = 0; i < 4; ++i)
    {
        buffer[fix_pos + i] = (tcrcreg >> i * 8) & 0xFF;
    }

}
void make_crc_revtable ( unsigned int *crc_revtable ) 
{
    unsigned int c;
    int n , k;
    for (n = 0; n < 256; n ++) 
    {
        c = n << 3*8;
        for (k = 0; k < 8; k ++) 
        {
            if (( c & 0x80000000 ) != 0) 
            {
                c = ((( c ^ CRCPOLY ) << 1) | 1);
            } 
            else 
            {
                c = (c <<1);
            }
        }
        crc_revtable [n] = c;
    }
}
void make_crc_table ( unsigned int * table ) 
{
    unsigned int c;
    int n , k;
    for (n = 0; n < 256; n ++) 
    {
        c = n ;
        for (k = 0; k < 8; k ++) 
        {
            if (( c & 1) != 0) 
            {
                c = CRCPOLY ^ ( c >> 1);
            } 
            else 
            {
                c = c >> 1;
            }
        }
        table [n] = c;
    }
}
int crc32_bitoriented ( unsigned char * buffer , int length ) 
{
    int i , j;
    unsigned int crcreg = INITXOR ;
    for (j = 0; j < length ; ++ j ) 
    {
        unsigned char b = buffer [ j ];
        for (i = 0; i < 8; ++ i) 
        {
            if (( crcreg ^ b ) & 1) 
            {
                crcreg = ( crcreg >> 1) ^ CRCPOLY ;
            } 
            else 
            {
                crcreg >>= 1;
            }
            b >>= 1;
        }
    }

return crcreg ^ FINALXOR ;
}
int main()
{
    length = sizeof(buffer);
    CRC_32 = crc32_bitoriented( buffer , length );
    
    printf("\nCRC_32 :%x ",CRC_32);
    make_crc_table(&crc_table[0]);

    make_crc_revtable(&crc_revtable[0]);
    
    fix_crc_pos(buffer, length, tcrcreg, fix_pos, &crc_table[0], &crc_revtable[0]);
    printf("\nModified Buffer:\n");

    for(int i=1;i<=length ;i++)
    {
        printf("0x%x  ",buffer[i-1]);
        if(0== (i%5))
        {
            printf("\n");
        }
    }printf("\n");
    
    CRC_32 = crc32_bitoriented( buffer , length );
    printf("\nFinal CRC_32 :%x ",CRC_32);   
    return 0;
    
}

----------------------END Code---------------

How do we get Offset and reconciliation factor value to get same CRC every time ? unchanged data in buffer: 0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xA 0xB 0xC 0xD 0xE 0xF 0x0 0x1 0x2 0x3 0x0 0x0 0x0 0x0 0x0

1

There are 1 best solutions below

2
On BEST ANSWER

Your code lost some things in copying. There needs to be a length parameter in fix_crc_pos(). In make_crc_revtable() you go to 256, not 25. You need a place to put the reverse table: uint32_t crc_revtable[256];. You need to make the forward table:

void make_crc_table(uint32_t *crc_table) {
    for (int n = 0; n < 256; n++) {
        uint32_t crc = n;
        for (int k = 0; k < 8; k++)
            crc = crc & 1 ? (crc >> 1) ^ CRCPOLY : crc >> 1;
        crc_table[n] = crc;
    }
}

and a place to put that somewhere: uint32_t crc_table[256];.

Then fix_crc_pos() will work. You give it the data you want the CRC over, buffer with length length. You give it the CRC you want to force the data to have, tcrcreg and the offset in the data, fix_pos, where there are four bytes you are allowing it to modify to get that CRC. And you provide it with the two tables you built by calling the make functions.