How to calculate crc for byte array? - c

I understand how CRC for a byte can be calculated by modulo 2 division with a polynomial but I don't understand how do you calculate CRC for data consisting of byte arrays. CRC for a single byte can be calculated by this following code
#define WIDTH 8
#define TOPBIT 1 << 7
#define POLYNOMIAL 0xD8
uint8_t(uint8_t const message)
{
uint8_t remainder = 0;
remainder ^= message;
for (uint8_t bit = 8; bit > 0; --bit)
{
if (remainder & TOPBIT)
{
remainder = (remainder << 1) ^ POLYNOMIAL;
}
else
{
remainder = (remainder << 1);
}
}
return (remainder);
}
but what about byte array ? I found above code on this site, Author also gave the code for byte array where he just XOR'ed current remainder with next byte
remainder ^= (message[byte] << (WIDTH - 8));
I don't quite understand why? why he XOR'ed to get in the next byte into remainder?

See A painless guide to CRC error detection algorithms. It has everything on CRCs, including your question. An array is treated as a single massive number so the remainder is carried over to the next byte. CRC is the remainder that is left over at the end.

Look at page on wikipedia Mathematics of cyclic redundancy checks It turns out that CRC is a linear operation meaning that crc(x^y^z) = crc(x)crc(y)crc(x) and hence the author XOR'd the remainder of previous byte with the next byte

Related

Bit mask operation: what does the following code mean in C?

I am trying to debugging a piece of code at the moment and I could not figure it out what the following piece of code really does. Could anyone help me explain or give me ideas on the functionality of the following code?
uint8_t get_pca9955a_slave_loaded(uint8_t _slave)
{
uint8_t x = (uint8_t)(_slave / 16);
uint8_t y = (uint8_t)(_slave % 16);
uint16_t mask = 1U << y;
if (check_pca9955a_slave_valid(_slave)) {
return (uint8_t)((pca9955a_slaves_loaded[x] & mask) ? 1U : 0U);
} else {
return 0U;
}
}
Kind Regards,
Cheung
It unpacks a byte into its high nibble x and its low nibble y. Taking mod 16 is the same as taking the four lowest bits, and dividing by 16 is the same as taking the remaining bits.
It then uses y to calculate a bitmask and x as an offset into some kind of table of global variables that you didn’t share, checks that bit y is set in entry x or not, and returns either 1 or 0 in an unsigned byte.

CRC-15 giving wrong values

I am trying to create a CRC-15 check in c and the output is never correct for each line of the file. I am trying to output the CRC for each line cumulatively next to each line. I use: #define POLYNOMIAL 0xA053 for the divisor and text for the dividend. I need to represent numbers as 32-bit unsigned integers. I have tried printing out the hex values to keep track and flipping different shifts around. However, I just can't seem to figure it out! I have a feeling it has something to do with the way I am padding things. Is there a flaw to my logic?
The CRC is to be represented in four hexadecimal numbers, that sequence will have four leading 0's. For example, it will look like 0000xxxx where the x's are the hexadecimal digits. The polynomial I use is 0xA053.
I thought about using a temp variable and do 4 16 bit chunks of code per line every XOR, however, I'm not quite sure how I could use shifts to accomplish this so I settled for a checksum of the letters on the line and then XORing that to try to calculate the CRC code.
I am testing my code using the following input and padding with . until the string is of length 504 because that is what the pad character needs to be via the requirements given:
"This is the lesson: never give in, never give in, never, never, never, never - in nothing, great or small, large or petty - never give in except to convictions of honor and good sense. Never yield to force; never yield to the apparently overwhelming might of the enemy."
The CRC of the first 64 char line ("This is the lesson: never give in, never give in, never, never,) is supposed to be 000015fa and I am getting bfe6ec00.
My logic:
In CRCCalculation I add each character to a 32-bit unsigned integer and after 64 (the length of one line) I send it into the XOR function.
If it the top bit is not 1, I shift the number to the left one
causing 0s to pad the right and loop around again.
If the top bit is 1, I XOR the dividend with the divisor and then shift the dividend to the left one.
After all calculations are done, I return the dividend shifted to the left four ( to add four zeros to the front) to the calculation function
Add result to the running total of the result
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <ctype.h>
#define POLYNOMIAL 0xA053
void crcCalculation(char *text, int length)
{
int i;
uint32_t dividend = atoi(text);
uint32_t result;
uint32_t sumText = 0;
// Calculate CRC
printf("\nCRC 15 calculation progress:\n");
i = length;
// padding
if(i < 504)
{
for(; i!=504; i++)
{
// printf("i is %d\n", i);
text[i] = '.';
}
}
// Try calculating by first line of crc by summing the values then calcuating, then add in the next line
for (i = 0; i < 504; i++)
{
if(i%64 == 0 && i != 0)
{
result = XOR(POLYNOMIAL, sumText);
printf(" - %x\n",result);
}
sumText +=(uint32_t)text[i];
printf("%c", text[i]);
}
printf("\n\nCRC15 result : %x\n", result);
}
uint32_t XOR(uint32_t divisor, uint32_t dividend)
{
uint32_t divRemainder = dividend;
uint32_t currentBit;
// Note: 4 16 bit chunks
for(currentBit = 32; currentBit > 0; --currentBit)
{
// if topbit is 1
if(divRemainder & 0x80)
{
//divRemainder = (divRemainder << 1) ^ divisor;
divRemainder ^= divisor;
printf("%x %x\n", divRemainder, divisor);
}
// else
// divisor = divisor >> 1;
divRemainder = (divRemainder << 1);
}
//return divRemainder; , have tried shifting to right and left, want to add 4 zeros to front so >>
//return divRemainder >> 4;
return divRemainder >> 4;
}
The first issue I see is the top bit check, it should be:
if(divRemainder & 0x8000)
The question doesn't state if the CRC is bit reflected (xor data into low order bits of CRC, right shift for cycle) or not (xor data into high order bits of CRC, left shift for cycle), so I can't offer help for the rest of the code.
The question doesn't state the initial value of CRC (0x0000 or 0x7fff), or if the CRC is post complemented.
The logic for a conventional CRC is:
xor a byte of data into the CRC (upper or lower bits)
cycle the CRC 8 times (or do a table lookup)
After generating the CRC for an entire message, the CRC can be appended to the message. If a CRC is generated for a message with the appended CRC and there are no errors, the CRC will be zero (or a constant value if the CRC is post complemented).
here is a typical CRC16, extracted from: <www8.cs.umu.se/~isak/snippets/crc-16.c>
#define POLY 0x8408
/*
// 16 12 5
// this is the CCITT CRC 16 polynomial X + X + X + 1.
// This works out to be 0x1021, but the way the algorithm works
// lets us use 0x8408 (the reverse of the bit pattern). The high
// bit is always assumed to be set, thus we only use 16 bits to
// represent the 17 bit value.
*/
unsigned short crc16(char *data_p, unsigned short length)
{
unsigned char i;
unsigned int data;
unsigned int crc = 0xffff;
if (length == 0)
return (~crc);
do
{
for (i=0, data=(unsigned int)0xff & *data_p++;
i < 8;
i++, data >>= 1)
{
if ((crc & 0x0001) ^ (data & 0x0001))
crc = (crc >> 1) ^ POLY;
else crc >>= 1;
}
} while (--length);
crc = ~crc;
data = crc;
crc = (crc << 8) | (data >> 8 & 0xff);
return (crc);
}
Since you want to calculate a CRC15 rather than a CRC16, the logic will be more complex as cannot work with whole bytes, so there will be a lot of bit shifting and ANDing to extract the desire 15 bits.
Note: the OP did not mention if the initial value of the CRC is 0x0000 or 0x7FFF, nor if the result is to be complemented, nor certain other criteria, so this posted code can only be a guide.

CRC code and implementation compatibility

the mechanism or the steps for the CRC checksum is easy , but the software is somehow much complicated and there are some steps in software that are not compatible with the steps of CRC
the following picture is the steps for getting the checksum of the CRC ( which is simply a modulo 2 division):
the checksum is the remainder = 001
the software for calculating the CRC checksum is for a string of bits is:
/*
* The width of the CRC calculation and result.
* Modify the typedef for a 16 or 32-bit CRC standard.
*/
typedef uint8_t crc;
#define WIDTH (8 * sizeof(crc))
#define TOPBIT (1 << (WIDTH - 1))
crc
crcSlow(uint8_t const message[], int nBytes)
{
crc remainder = 0;
/*
* Perform modulo-2 division, a byte at a time.
*/
for (int byte = 0; byte < nBytes; ++byte)
{
/*
* Bring the next byte into the remainder.
*/
remainder ^= (message[byte] << (WIDTH - 8));
/*
* Perform modulo-2 division, a bit at a time.
*/
for (uint8_t bit = 8; bit > 0; --bit)
{
/*
* Try to divide the current data bit.
*/
if (remainder & TOPBIT)
{
remainder = (remainder << 1) ^ POLYNOMIAL;
}
else
{
remainder = (remainder << 1);
}
}
}
/*
* The final remainder is the CRC result.
*/
return (remainder);
}
I see that there is incompatibility in the software in the part( remainder<<1 ) because the shifting will always add 0 at the right even if the following bit is not 0.
and also in the part:
remainder ^= (message[byte] << (WIDTH - 8));
when putting the first byte I don't see problem because the initial value is because the initial value is 0, but when inserting the next bytes why we xor every byte of them with the previous remainder
The code example appears to use a variable sized CRC, where the size of the CRC is WIDTH. POLYNOMIAL is the bottom WIDTH bits of a WIDTH+1 bit polynomial, which will have the least significant bit set to 1. Since the operations are XOR, the order in which the data bits are XOR'ed into the remainder doesn't matter, so 8 data bits can be XOR'ed into the upper bits of remainder at the same time. Then the bit at a time feedback cycle occurs for 8 bits. Since the bottom bit of POLYNOMIAL is a 1, that will keep the cycle going, as long as there are any 1 bits in the data.

What Truncated polynomial mean in CRC16 CCITT context

Trying to understand this explanation of CRC16 CCITT, I came across to the term "truncated polynomial". Comparing the long-hand calculation for a one-byte message with the corresponding C code, I found out that the macro definition of poly doesnt match the calculation example from the above. Into the C code the polynomial is 0x1021 while into the calculation example above the polynomial used is bigger, 0x11021.
They use the term truncated polynomial for this: 0x1021. What pattern do they use to extend this 0x1021 to this 0x11021?
0x11021 represents polynomial p = x^16+x^12+x^5+x^0 from F2[X]. Message (together with initial value and augmentation) is also represented by a polynomial. CRC is basically just message modulo polynomial p. Therefore CRC never needs more than 2 bytes. Since p = 0 mod p we can write x^16 = x^12+x^5+x^0 mod p. So 0x1021 represents x^12+x^5+x^0.
Now lets see how update_good_crc works:
void update_good_crc(unsigned short ch)
{
unsigned short i, v, xor_flag;
/*
Align test bit with leftmost bit of the message byte.
*/
v = 0x80;
for (i=0; i<8; i++)
{
if (good_crc & 0x8000)
{
xor_flag= 1;
}
else
{
xor_flag= 0;
}
good_crc = good_crc << 1;
if (ch & v)
{
/*
Append next bit of message to end of CRC if it is not zero.
The zero bit placed there by the shift above need not be
changed if the next bit of the message is zero.
*/
good_crc= good_crc + 1;
}
if (xor_flag)
{
good_crc = good_crc ^ poly;
}
/*
Align test bit with next bit of the message byte.
*/
v = v >> 1;
}
}
This checks whether most significant bit of good_crc is set to zero. In other words, it checks whether coefficient at x^15 is set to 1 or 0.
if (good_crc & 0x8000)
{
xor_flag= 1;
}
good_crc = good_crc << 1; this multiplies good_crc by x. Thus coefficient at x^15 becomes coefficient at x^16 and good_crc "overflows" its 16 bits (that's why we stored xor_flag).
good_crc = good_crc ^ poly; if xor_flag is set then this "subtracts" x^16 = x^12+x^5+x^0 mod p from good_crc.

Generate a 4 bit checksum of an integer

I have an Arduino serial connection with 4 bits per transmission left for a check sum. I have never done a check sum algorithm before and am sadly lost right now.
CRC-4 implementation in C# has already started a question but the answer isn't really helping me go forward.
Input should be an integer of which only 10 bits are important.
Output should be 4 bits of a byte.
Can someone explain to me how I can adapt the answer from the other question or point me another direction?
byte checksum(int message)
{
//Check sum algorithm
}
well the other algorithm is easily translatable to pure C:
uint8_t calculate(byte[] bytes) {
uint16_t crc = 0xFFFF; // initial value
// loop, calculating CRC for each byte of the string
for (uint8_t byteIndex = 0; byteIndex < bytes.Length; ++byteIndex) {
uint8_t bit = 0x80; // initialize bit currently being tested
for (uint8_t bitIndex = 0; bitIndex < 8; ++bitIndex) {
bool xorFlag = ((crc & 0x8000) == 0x8000);
crc <<= 1;
if (((bytes[byteIndex] & bit) ^ (uint8_t)0xff) != (uint8_t)0xff)
crc = crc + 1;
if (xorFlag)
crc = crc ^ 0x1021;
bit >>= 1;
}
}
return (uint8_t)crc;
}
the only difference being the use of the stdint.h types.
I also changed the type of crc to be exactly a 16 bits unsigned, and for the
indexes, only for sparing some arduino memory which is precious (every byte counts
when you got only 2.5k of RAM! :-) )
Though, I did neither test or proof read that code, so it should be as good as the
original C# one. If it's buggy, that one will be as well.
EDIT: As the OP added in comment, this resource is a good explanation of how the above CRC algorithm work: http://www.barrgroup.com/Embedded-Systems/How-To/CRC-Calculation-C-Code.
HTH

Resources