Search code examples
ccrccrc16

CRC 16 program to calculate check sum


I'm currently writing a CRC16 program which computes the CRC of characters using the CRC 16 polynomial X^16 + X^15 + X^2 + 1. the program should read data from the standard input and output the 16 bit CRC in hexadecimal. Nonetheless, when I execute the program I get the wrong value for output.

Here is my code:

#include <stdint.h>

#define CRC16 0x8005

unsigned short crc(unsigned char msg[], int len)
{
    unsigned short out = 0;
    int bits = 0, t_flag;
    int x = 0;

    /* Sanity check: */
    if(msg == NULL)
        return 0;

    while(len > x)
    {
        unsigned short data = msg[x];
        t_flag = out >> 15;

        /* Get next bit: */
        out <<= 1;
        out |= (data >> bits) & 1; // item a) work from the least significant bits

        /* Increment bit counter: */
        bits++;
        if(bits > 7)
        {
            bits = 0;
            data++;
            len--;
        }

        /* Cycle check: */
        if(t_flag)
            out ^= CRC16;

    }

    // item b) "push out" the last 16 bits
    int i;
    for (i = 0; i < 16; ++i) {
        t_flag = out >> 15;
        out <<= 1;
        if(t_flag)
            out ^= CRC16;
    }

    // item c) reverse the bits
    unsigned short crc1 = 0;
    i = 0x8000;
    int j = 0x0001;
    for (; i != 0; i >>=1, j <<= 1) {
        if (i & out) crc1 |= j;
    }

    return crc1;
}
int main (int argc, char *argv[]) {
//if (argv[1] == "trace") {

//printf(argv[1]);
//}
char ARGV;


if(argc < 1) {

    printf("Must have atleast one arguments\n");
        return 1;

}
char buf[256];
int c , r;
int count = -1;

while((c = getchar())!=EOF) {
    buf[count++] = putchar(c);
}

 r = crc(buf, count);

 //printf("%s\n",argv[1] );

 printf("%04hx\n", r);
 //print("%x\n", argv[1]);
 return (0);
//printf(" %4x\n", crc(argv[1], 16));

}

Output:

(I'm reading in 123456789 in my txt file)

./crc1 < testfile.txt
123456789
7bda

It's supposed to be BB3D but I'm getting 7bda. Can someone help me figure out what I'm doing wrong?


Solution

  • #include<stdio.h>
    #include<stdint.h>
    
    #define CRC16 0x8005
    
    uint16_t gen_crc16(const uint8_t *data, uint16_t size)
    {
        uint16_t out = 0;
        int bits_read = 0, bit_flag;
    
    
        // test
        printf("buffer in function %s\n", data);
    
        /* Sanity check: */
        if(data == NULL)
            return 0;
    
        while(size > 0)
        {
            bit_flag = out >> 15;
    
            /* Get next bit: */
            out <<= 1;
            out |= (*data >> bits_read) & 1; // item a) work from the least significant bits
    
            /* Increment bit counter: */
            bits_read++;
            if(bits_read > 7)
            {
                bits_read = 0;
                data++;
                size--;
            }
    
            /* Cycle check: */
            if(bit_flag)
                out ^= CRC16;
    
        }
    
        // item b) "push out" the last 16 bits
        int i;
        for (i = 0; i < 16; ++i) {
            bit_flag = out >> 15;
            out <<= 1;
            if(bit_flag)
                out ^= CRC16;
        }
    
        // item c) reverse the bits
        uint16_t crc = 0;
        i = 0x8000;
        int j = 0x0001;
        for (; i != 0; i >>=1, j <<= 1) {
            if (i & out) crc |= j;
        }
    
        return crc;
    }
    
    int main()
    {
        char buf[]="123456789";
        int c , r;
        printf ("the buf has %s", buf);
        r = gen_crc16(buf,sizeof(buf)-1);
        printf("%04hx\n", r);
    
        return (0);
    }