CRC Verification Algorithm

Source: Internet
Author: User

Recently, a protocol was developed to implement a hidden channel. The CRC verification algorithm is required. because the number of data digits is small, the CRC verification algorithm is used.
This algorithm is mainly used to implement a divisor operation. The basic principle is XOR and shift.
The algorithm of the Modulo-2 operation is as follows (in C language ):

MoD 2 Operation
// Crcmod2 function //////////////////////////////////// ///////////////////
// Function: modulo 2 Operation
// Parameter description;
// M -- Divisor
// P -- Divisor
// Return: Remainder
Unsigned long crcmod2 (unsigned long M, unsigned long P)
{
Int m_bits = 0, p_bits = 0, a = 1, bit = 0, flag = 0;
Unsigned long m_temp = m, p_temp = P;
Unsigned long remainder = 0, anddata = 0x00000001;

Unsigned long dividend, divisor;

// Obtain the number of divisor digits
While (m_temp)
{
M_temp/= 2;
++ M_bits;
}

// Obtain the divisor number.
While (p_temp)
{
P_temp/= 2;
++ P_bits;
}

// If the number of divisor digits is smaller than the divisor, the return value is the remainder.
If (m_bits <p_bits)
Return m;

// The first modulo 2 operation is started here
M_temp = m> (m_bits-p_bits); // shifts the number of digits to the right.

Dividend = m_temp; // get the divisor for the first operation
Divisor = P; // obtain the Divisor

Remainder = dividend ^ divisor; // obtain the result of mod 2 as the remainder.

M_bits-= p_bits; // The first computation is completed, minus the number of digits that have been calculated

While (m_bits)
{
If (! Flag)
{
Dividend = (remainder <1); // The remainder obtained in the previous operation is the highest of the divisor.
M_temp = m> (m_bits-1 );
Bit = anddata & m_temp;
Dividend | = bit; // a certain digit of the original dividend as the lowest digit of the current dividend
}
Else
{
Dividend <= 1;
M_temp = m> (m_bits-1 );
Bit = anddata & m_temp;
Dividend | = bit;
}

// Compare the maximum divisor size and the maximum divisor size
If (dividend & (anddata <(p_bits-1) >=( divisor & (anddata <(p_bits-1 ))))
{
Flag = 0;
Remainder = dividend ^ divisor; // obtain the result of mod 2 as the remainder.
}
Else
{
// Requires a bid
Flag = 1;
}

-- M_bits;
}

If (FLAG)
Remainder = dividend;

Return remainder;
}

The crcgetcode function obtains the original data and the CRC generates the polynomial, and uses the second operation to obtain new data. The algorithm is as follows:
// Crcgetcode function //////////////////////////////////// ///////////////
// Function: encode the entire data through the original data and the CRC polynomial.
// Parameter description:
// Originaldata -- raw data
// Crcpolynomial -- CRC generates Polynomial
// Return: Raw Data + Verification Code
Int crcgetcode (unsigned long originaldata, unsigned long crcpolynomial)
{
Unsigned long checkbits = 0, temp = crcpolynomial, M = originaldata, FCS;

While (temp)
{
Temp/= 2;
++ Checkbits; // The number of check bits plus one
}
-- Checkbits; // Number of vertices obtained (one digit less than the generated polynomial)
M <= checkbits; // obtain the divisor of the mod 2 operation.

If (checkbits = 0)
Return m;

FCS = crcmod2 (M, crcpolynomial );

M | = FCS; // Add the FC checksum.

Return m; // return the entire encoding.
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.