/*============================================================================= | | NAME | | testCRC.cpp | | DESCRIPTION | | Unit test program for CRC generation and checking. | | AUTHOR | | Sean O'Connor | | NOTES/METHOD | | Create as a console application under Windows NT. | | LEGAL | | CRCDemo Version 2.1 - A Program for generating and checking CRC codes. | Copyright (C) 1999-2008 by Sean Erik O'Connor. All Rights Reserved. | | This program is free software; you can redistribute it and/or | modify it under the terms of the GNU General Public License | as published by the Free Software Foundation; version 2 | of the License. | | This program is distributed in the hope that it will be useful, | but WITHOUT ANY WARRANTY; without even the implied warranty of | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | GNU General Public License for more details. | | You should have received a copy of the GNU General Public License | along with this program; if not, write to the Free Software | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | | The author's address is artifex@seanerikoconnor.freeservers.com | +============================================================================*/ #include #include "shiftRegister.h" // Shift register class (needed for CRC code). #include "crcCode.h" // CRC code class. char * legalNotice = { "\n" "CRCDemo Version 2.1 - A Program for generating and checking CRC codes.\n" "Copyright (C) 1999-2008 by Sean Erik O'Connor. All Rights Reserved.\n" "\n" "CRCDemo comes with ABSOLUTELY NO WARRANTY; for details see the\n" "GNU General Public License. This is free software, and you are welcome\n" "to redistribute it under certain conditions; see the GNU General Public License\n" "for details.\n\n" } ; int main( int argc, char * argv[] ) { /* Show the legal notice first. */ printf( "%s", legalNotice ) ; printf( "\n\nCRC-16 test: x ^ 16 + x ^ 15 + x ^ 2 + 1\n\n" ) ; // Construct CRC code object from the CRC's generator polynomial. // We use the standard code, CRC-16. CRCCode crc16( "x ^ 16 + x ^ 15 + x ^ 2 + 1" ) ; // Do an error check to see if the object was constructed OK. if (crc16.getNumParityBytes() == 0) { printf( "Error in creating CRC-16\n" ) ; } // Create a short sample message to be encoded. // Allow for 16 bits extra space at the end of the buffer for // parity bits. unsigned char buffer[ 18 ] = { 0x0E, 0x00, 0x00, 0x00, 0x01, 0x05, 0x02, 0x00, 0x63, 0x65, 0x6e, 0x74, 0x75, 0x72, 0x61, 0x00, 0x00, 0x00 // 16 bits extra space for parity bits. } ; long int bufferLen = 16 ; // Add the parity bits onto the end of the message to // create a systematically encoded codeword. crc16.addParityToBuffer( buffer, bufferLen ) ; // Update the buffer length now, since it has added parity bytes. long int bufferPlusParityLen = bufferLen + crc16.getNumParityBytes() ; // Check the codeword. unsigned long int shiftedSyndrome = crc16.shiftedSyndrome( buffer, bufferPlusParityLen ) ; printf( "Shifted syndrome of codeword = %04x (should be zero)\n", shiftedSyndrome ) ; // Compute parity bits for the message. unsigned long int parityBits = crc16.parityBits( buffer, bufferLen ) ; printf( "Parity bits = %04x (should be 35cf)\n", (unsigned int)( ((parityBits & 0xFFFF0000) >> 16) ) ) ; // Add noise to the codeword. buffer[ 2 ] |= 0x10 ; // Check the codeword. shiftedSyndrome = crc16.shiftedSyndrome( buffer, bufferLen ) ; printf( "Shifted syndrome of noisy codeword = %04x (should be non-zero)\n", shiftedSyndrome ) ; //============================================================= // Create a short sample message to be encoded. unsigned char buffer32Q[ 6 ] = { 0x01, 0x02, 0x00, 0x00, 0x00, 0x00 // 32 bits extra space for parity bits. } ; bufferLen = 2 ; printf( "\n\nCRC-32Q test: x^32+x^31+x^24+x^22+x^16+x^14+x^8+x^7+x^5+x^3+x+1\n\n" ) ; // Construct CRC code object from the CRC's generator polynomial. CRCCode crc32Q( "x^32+x^31+x^24+x^22+x^16+x^14+x^8+x^7+x^5+x^3+x+1" ) ; // Do an error check to see if the object was constructed OK. if (crc32Q.getNumParityBytes() == 0) { printf( "Error in creating CRC-32\n" ) ; } // Add the parity bits onto the end of the message to // create a systematically encoded codeword. crc32Q.addParityToBuffer( buffer32Q, bufferLen ) ; // Update the buffer length now, since it has added parity bytes. bufferPlusParityLen = bufferLen + crc32Q.getNumParityBytes() ; // Check the codeword. shiftedSyndrome = crc32Q.shiftedSyndrome( buffer32Q, bufferPlusParityLen ) ; printf( "Shifted syndrome of codeword = %08x (should be zero)\n", shiftedSyndrome ) ; // Compute parity bits for the message. parityBits = crc32Q.parityBits( buffer32Q, bufferLen ) ; printf( "Parity bits = %08x (should be 03c371cf)\n", (unsigned int)(parityBits)) ; // Add noise to the codeword. buffer32Q[ 1 ] |= 0x10 ; // Check the codeword. shiftedSyndrome = crc32Q.shiftedSyndrome( buffer32Q, bufferLen ) ; printf( "Shifted syndrome of noisy codeword = %08x (should be non-zero)\n", shiftedSyndrome ) ; //============================================================= printf( "\n\nCRC-CCITT test using shift register preset to FFFF\n" "and parity bit inversion: x ^ 16 + x ^ 12 + x ^ 5 + 1\n\n" ) ; // Construct CRC code object from the CRC's generator polynomial. // We use the standard code, CRC-CCITT, but we preset the shift // register contents to FFFF, and we 1's complement the parity // bits. CRCCode crcCCITT( "x ^ 16 + x ^ 12 + x ^ 5 + 1", 0xFFFF, true ) ; // Do an error check to see if the object was constructed OK. if (crcCCITT.getNumParityBytes() == 0) { printf( "Error in creating CRC-CCITT\n" ) ; } // Create a short sample message to be encoded. // Allow for 16 bits extra space at the end of the buffer for // parity bits. unsigned char bufferITT[ 7 ] = { 0x00, 0x05, 0x04, 0x00, 0x10, 0x00, 0x00 // 16 bits extra space for parity bits. } ; bufferLen = 5 ; // Add the parity bits onto the end of the message to // create a systematically encoded codeword. crcCCITT.addParityToBuffer( bufferITT, bufferLen ) ; // Update the buffer length now, since it has added parity bytes. bufferPlusParityLen = bufferLen + crcCCITT.getNumParityBytes() ; // Check the codeword. shiftedSyndrome = crcCCITT.shiftedSyndrome( bufferITT, bufferPlusParityLen ) ; printf( "Shifted syndrome of codeword = %04x (should be zero)\n", shiftedSyndrome ) ; // Compute parity bits for the message. parityBits = crcCCITT.parityBits( bufferITT, bufferLen ) ; printf( "Parity bits = %04x (should be 9c47)\n", (unsigned int)( ((parityBits & 0xFFFF0000) >> 16) ) ) ; // Add noise to the codeword. bufferITT[ 2 ] |= 0x10 ; // Check the codeword. shiftedSyndrome = crcCCITT.shiftedSyndrome( bufferITT, bufferLen ) ; printf( "Shifted syndrome of noisy codeword = %04x (should be non-zero)\n", shiftedSyndrome ) ; //============================================================= printf( "\n\nCRC-DNP test using shift register preset to 0\n" "and no parity bit inversion" "x^16 + x^13 + x^12 + x^11 + x^10 + x^8 + x^6 + x^5 + x^2 + 1\n\n" ) ; // Construct CRC code object from the CRC's generator polynomial. CRCCode crcDNP( "x^16 + x^13 + x^12 + x^11 + x^10 + x^8 + x^6 + x^5 + x^2 + 1", 0x0, false ) ; // Do an error check to see if the object was constructed OK. if (crcDNP.getNumParityBytes() == 0) { printf( "Error in creating CRC-DNP\n" ) ; } // Create a short sample message to be encoded. // Allow for 16 bits extra space at the end of the buffer for // parity bits. unsigned char bufferDNP[ 10 ] = { 0x05, 0x64, 0x11, 0xc4, 0x65, 0x00, 0x02, 0x00, 0x00, 0x00 // 16 bits extra space for parity bits. } ; bufferLen = 8 ; // Add the parity bits onto the end of the message to // create a systematically encoded codeword. crcDNP.addParityToBuffer( bufferDNP, bufferLen ) ; // Update the buffer length now, since it has added parity bytes. bufferPlusParityLen = bufferLen + crcDNP.getNumParityBytes() ; // Check the codeword. shiftedSyndrome = crcDNP.shiftedSyndrome( bufferDNP, bufferPlusParityLen ) ; printf( "Shifted syndrome of codeword = %04x (should be zero)\n", shiftedSyndrome ) ; // Compute parity bits for the message. parityBits = crcDNP.parityBits( bufferDNP, bufferLen ) ; printf( "Parity bits = %04x (should be b1ae)\n", (unsigned int)( ((parityBits & 0xFFFF0000) >> 16) ) ) ; // Add noise to the codeword. bufferDNP[ 2 ] |= 0xE0 ; // Check the codeword. shiftedSyndrome = crcDNP.shiftedSyndrome( bufferDNP, bufferLen ) ; printf( "Shifted syndrome of noisy codeword = %04x (should be non-zero)\n", shiftedSyndrome ) ; return( 0 ) ; }