Porting CRC lookup tables between platforms?

2

I have a C++ compile-at-run-time script written for a VxWorks derived PowerPC platform, which calculates checksums for files on the embedded OS. It uses a pair of lookup tables to compute the checksum (see my code below).

I'm trying to port this script to use on Windows, because the platform dictates that I must hardcode a new script for every single file I wish to calculate the checksum for, and with what I'm trying to do that will take many hours. I need to change a bunch of files on the platform file system and provide their checksums to the system or it will refuse to load them.

I've got the code running, having changed all of the UI/output and file input stuff to work with Windows, but I'm getting the wrong checksum. On my test file, I should be getting 404f and I'm actually getting 5fb4.

Having done some research I believe I may need to convert or regenerate the lookup tables, but I have no idea how to go about this and would be grateful for any input.

Thanks!

// rt6_crc_checker.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#define TRUE    1
#define FALSE   0
#define SKIP    1

#include <windows.h>
#include <iostream>
#include <cstdlib>
#include <sys/stat.h>

using namespace std;

int  PL_FileExists( char *p_pPath );
void PL_itoa10( int i , char* s );
void PL_itoa16( int i , char* s );

int change_endian(int num){
 int byte0, byte1, byte2, byte3;
 byte0 = (num & 0x000000FF) >> 0 ;
 byte1 = (num & 0x0000FF00) >> 8 ;
 byte2 = (num & 0x00FF0000) >> 16 ;
 byte3 = (num & 0xFF000000) >> 24 ;
 return((byte0 << 24) | (byte1 << 16) | (byte2 << 8) | (byte3 << 0));
}

/*** MAIN ***/

int main ( int argc, char **argv )
{
    char carlu;
    char s_nb[16];
    char s_i[16];
    char CRC_FIC[16];
    char CRC_INF[16];
    char MessPL[256];
    int  nb;
    int  size_FIC;
    int  size_INF;
    int  answ;
    int  CRC;
    int  c1;
    int  c2;
    int  rg;
    int  i;
    int  j;
    FILE *l_fileIN;
    FILE *l_fileOUT;

    char l_line[20];

    int table_h[]= 
    {
    0x00, 0xdf, 0xbe, 0x61, 0x7c, 0xa3, 0xc2, 0x1d, 0xd3, 0x0c, 0x6d, 0xb2, 0xaf, 0x70, 0x11, 0xce,
    0x8d, 0x52, 0x33, 0xec, 0xf1, 0x2e, 0x4f, 0x90, 0x5e, 0x81, 0xe0, 0x3f, 0x22, 0xfd, 0x9c, 0x43,
    0x1a, 0xc5, 0xa4, 0x7b, 0x66, 0xb9, 0xd8, 0x07, 0xc9, 0x16, 0x77, 0xa8, 0xb5, 0x6a, 0x0b, 0xd4,
    0x97, 0x48, 0x29, 0xf6, 0xeb, 0x34, 0x55, 0x8a, 0x44, 0x9b, 0xfa, 0x25, 0x38, 0xe7, 0x86, 0x59,
    0x1f, 0xc0, 0xa1, 0x7e, 0x63, 0xbc, 0xdd, 0x02, 0xcc, 0x13, 0x72, 0xad, 0xb0, 0x6f, 0x0e, 0xd1,
    0x92, 0x4d, 0x2c, 0xf3, 0xee, 0x31, 0x50, 0x8f, 0x41, 0x9e, 0xff, 0x20, 0x3d, 0xe2, 0x83, 0x5c,
    0x05, 0xda, 0xbb, 0x64, 0x79, 0xa6, 0xc7, 0x18, 0xd6, 0x09, 0x68, 0xb7, 0xaa, 0x75, 0x14, 0xcb,
    0x88, 0x57, 0x36, 0xe9, 0xf4, 0x2b, 0x4a, 0x95, 0x5b, 0x84, 0xe5, 0x3a, 0x27, 0xf8, 0x99, 0x46,
    0x15, 0xca, 0xab, 0x74, 0x69, 0xb6, 0xd7, 0x08, 0xc6, 0x19, 0x78, 0xa7, 0xba, 0x65, 0x04, 0xdb,
    0x98, 0x47, 0x26, 0xf9, 0xe4, 0x3b, 0x5a, 0x85, 0x4b, 0x94, 0xf5, 0x2a, 0x37, 0xe8, 0x89, 0x56,
    0x0f, 0xd0, 0xb1, 0x6e, 0x73, 0xac, 0xcd, 0x12, 0xdc, 0x03, 0x62, 0xbd, 0xa0, 0x7f, 0x1e, 0xc1,
    0x82, 0x5d, 0x3c, 0xe3, 0xfe, 0x21, 0x40, 0x9f, 0x51, 0x8e, 0xef, 0x30, 0x2d, 0xf2, 0x93, 0x4c,
    0x0a, 0xd5, 0xb4, 0x6b, 0x76, 0xa9, 0xc8, 0x17, 0xd9, 0x06, 0x67, 0xb8, 0xa5, 0x7a, 0x1b, 0xc4,
    0x87, 0x58, 0x39, 0xe6, 0xfb, 0x24, 0x45, 0x9a, 0x54, 0x8b, 0xea, 0x35, 0x28, 0xf7, 0x96, 0x49,
    0x10, 0xcf, 0xae, 0x71, 0x6c, 0xb3, 0xd2, 0x0d, 0xc3, 0x1c, 0x7d, 0xa2, 0xbf, 0x60, 0x01, 0xde,
    0x9d, 0x42, 0x23, 0xfc, 0xe1, 0x3e, 0x5f, 0x80, 0x4e, 0x91, 0xf0, 0x2f, 0x32, 0xed, 0x8c, 0x53
    };

    int table_l[]= 
    {
    0x00, 0x2b, 0x57, 0x7c, 0xaf, 0x84, 0xf8, 0xd3, 0xf6, 0xdd, 0xa1, 0x8a, 0x59, 0x72, 0x0e, 0x25,
    0x45, 0x6e, 0x12, 0x39, 0xea, 0xc1, 0xbd, 0x96, 0xb3, 0x98, 0xe4, 0xcf, 0x1c, 0x37, 0x4b, 0x60,
    0x8b, 0xa0, 0xdc, 0xf7, 0x24, 0x0f, 0x73, 0x58, 0x7d, 0x56, 0x2a, 0x01, 0xd2, 0xf9, 0x85, 0xae,
    0xce, 0xe5, 0x99, 0xb2, 0x61, 0x4a, 0x36, 0x1d, 0x38, 0x13, 0x6f, 0x44, 0x97, 0xbc, 0xc0, 0xeb,
    0xbe, 0x95, 0xe9, 0xc2, 0x11, 0x3a, 0x46, 0x6d, 0x48, 0x63, 0x1f, 0x34, 0xe7, 0xcc, 0xb0, 0x9b,
    0xfb, 0xd0, 0xac, 0x87, 0x54, 0x7f, 0x03, 0x28, 0x0d, 0x26, 0x5a, 0x71, 0xa2, 0x89, 0xf5, 0xde,
    0x35, 0x1e, 0x62, 0x49, 0x9a, 0xb1, 0xcd, 0xe6, 0xc3, 0xe8, 0x94, 0xbf, 0x6c, 0x47, 0x3b, 0x10,
    0x70, 0x5b, 0x27, 0x0c, 0xdf, 0xf4, 0x88, 0xa3, 0x86, 0xad, 0xd1, 0xfa, 0x29, 0x02, 0x7e, 0x55,
    0xd4, 0xff, 0x83, 0xa8, 0x7b, 0x50, 0x2c, 0x07, 0x22, 0x09, 0x75, 0x5e, 0x8d, 0xa6, 0xda, 0xf1,
    0x91, 0xba, 0xc6, 0xed, 0x3e, 0x15, 0x69, 0x42, 0x67, 0x4c, 0x30, 0x1b, 0xc8, 0xe3, 0x9f, 0xb4,
    0x5f, 0x74, 0x08, 0x23, 0xf0, 0xdb, 0xa7, 0x8c, 0xa9, 0x82, 0xfe, 0xd5, 0x06, 0x2d, 0x51, 0x7a,
    0x1a, 0x31, 0x4d, 0x66, 0xb5, 0x9e, 0xe2, 0xc9, 0xec, 0xc7, 0xbb, 0x90, 0x43, 0x68, 0x14, 0x3f,
    0x6a, 0x41, 0x3d, 0x16, 0xc5, 0xee, 0x92, 0xb9, 0x9c, 0xb7, 0xcb, 0xe0, 0x33, 0x18, 0x64, 0x4f,
    0x2f, 0x04, 0x78, 0x53, 0x80, 0xab, 0xd7, 0xfc, 0xd9, 0xf2, 0x8e, 0xa5, 0x76, 0x5d, 0x21, 0x0a,
    0xe1, 0xca, 0xb6, 0x9d, 0x4e, 0x65, 0x19, 0x32, 0x17, 0x3c, 0x40, 0x6b, 0xb8, 0x93, 0xef, 0xc4,
    0xa4, 0x8f, 0xf3, 0xd8, 0x0b, 0x20, 0x5c, 0x77, 0x52, 0x79, 0x05, 0x2e, 0xfd, 0xd6, 0xaa, 0x81
    };

    i    = 0;
    j    = 0;
    size_FIC = 0;
    size_INF = 0;
    c1   = 0;
    c2   = 0;
    rg   = 0;
    CRC  = 0;

    if ( PL_FileExists( "crc_check.ext" ) != TRUE )
    {
        cout << "input MISSING !";
        return 0;
    }

    struct stat stat_buf;
    int rc = stat("crc_check.ext", &stat_buf);
    size_FIC = rc == 0 ? stat_buf.st_size : -1;

    cout << "input found " << size_FIC << endl;

    PL_itoa10 ( size_FIC , s_nb );
    strcpy ( MessPL , "Size : " );
    strcat ( MessPL , s_nb );
    strcat ( MessPL , " K..." );
    cout << MessPL << endl;

    // Calculate CRC...

    l_fileIN = fopen ( "crc_check.ext", "r");

    if ( NULL == l_fileIN )
    {
        cout << "Error opening" << endl;
    }
    else
    {
        cout << "Ok will calculate CRC bear with" << endl;

        c1 = 0;
        c2 = 0;
        rg = 0;

        for ( i = 0 ; i < size_FIC ; i++ )
        {
            fseek ( l_fileIN , i , 0 );

            if ( i % 4000 == 0 )
            {
                j = ( 100 * i ) / size_FIC ;                
            }

            if ( NULL == fgets ( l_line , 2 , l_fileIN ) )
            {
                PL_itoa10 ( i , s_i );
                strcpy ( MessPL , "READ Error ! " );
                strcat ( MessPL , s_i );
                cout << MessPL << endl;
                i = size_FIC;
            }
            else
            {
                nb = (int) l_line[0];

                if ( nb < 0 )
                    nb += 256;

                rg = c1 ^ nb;
                c1 = c2 ^ table_h[rg];
                c2 = table_l[rg];

            }
        }
        fclose(l_fileIN);
    } 

    PL_itoa16 ( c1 , s_nb );
    if ( c1 < 16 )
    {
        strcpy ( CRC_FIC , "0" );
        strcat ( CRC_FIC , s_nb );
    }
    else
        strcpy ( CRC_FIC , s_nb );

    PL_itoa16 ( c2 , s_nb );
    if ( c2 < 16 )
    {
        strcat ( CRC_FIC , "0" );
        strcat ( CRC_FIC , s_nb );
    }
    else
        strcat ( CRC_FIC , s_nb );

    cout << CRC_FIC << endl;
}

void PL_itoa10 (int i , char* s)
{
    char l_s[16];
    int  n = 0 , m = 0;
    if ( i < 0 )
    {
        s[m++] = '-';
        i=-i;
    }
    do
    {
        l_s[n++] = (char)('0'+(i%10));
        i /= 10;
    }
    while ( i );
    for ( --n ; n>=0 ; n--,m++ )
        s[m] = l_s[n];
    s[m]=0;
}

void PL_itoa16 (int i , char* s)
{
    char l_s[16];
    int  n = 0 , m = 0;
    if ( i < 0 )
    {
        s[m++] = '-';
        i=-i;
    }
    do
        {
        if ( i%16 < 10 )
            l_s[n++] = (char)('0'+(i%16));
        else
            l_s[n++] = (char)('a'+(i%16)-10);
        i /= 16;
        }
    while ( i );
    for ( --n ; n>=0 ; n--,m++ )
        s[m] = l_s[n];
    s[m]=0;
}

int PL_FileExists ( char *p_pPath )
{
    FILE* l_pFich = NULL;

    if (NULL == (l_pFich = fopen (p_pPath, "r")))
    {
        return (FALSE);
    }
    else
    {
        fclose (l_pFich);
        return (TRUE);
    }
}
c++
checksum
lookup-tables
powerpc
asked on Stack Overflow Dec 17, 2017 by T Kilney • edited Jan 10, 2018 by klutt

1 Answer

1

This code is labeled as CCITT Standard CRC16 non-reflected CRC using the polynomial 0x1021. Would produced that type of table. Try this:

static unsigned short crctable[256];

void make_crc_table( void )
    {
        int i, j;
        unsigned long poly, c;
        static const byte p[] = {0,5,12}; 
        poly = 0L;
        for ( i = 0; i < sizeof( p ) / sizeof( byte ); i++ )
        {
            poly |= 1L << p[i];
        }
        for ( i = 0; i < 256; i++ )
        {
            c = i << 8;
            for ( j = 0; j < 8; j++ )
            {
                c = ( c & 0x8000 ) ? poly ^ ( c << 1 ) : ( c << 1 );
            }
            crctable[i] = (unsigned short) c;
        }
    }
answered on Stack Overflow Dec 17, 2017 by Turbo WhizKid

User contributions licensed under CC BY-SA 3.0