1/*=============================================================================
  2|
  3| NAME
  4|  
  5|     crcCode.hpp
  6|
  7| DESCRIPTION
  8|  
  9|     Class definition for CRCCode
 10|
 11| AUTHOR
 12|
 13|      Sean O'Connor
 14|    
 15| LEGAL
 16|
 17|     CRCDemo Version 2.1 - A Program for generating and checking CRC codes.
 18|     Copyright (C) 1999-2025 by Sean Erik O'Connor.  All Rights Reserved.
 19|
 20|     This program is free software: you can redistribute it and/or modify
 21|     it under the terms of the GNU General Public License as published by
 22|     the Free Software Foundation, either version 3 of the License, or
 23|     (at your option) any later version.
 24|
 25|     This program is distributed in the hope that it will be useful,
 26|     but WITHOUT ANY WARRANTY; without even the implied warranty of
 27|     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 28|     GNU General Public License for more details.
 29|
 30|     You should have received a copy of the GNU General Public License
 31|     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 32|     
 33|     The author's address is seanerikoconnor!AT!gmail!DOT!com
 34|     with !DOT! replaced by . and the !AT! replaced by @
 35|
 36+============================================================================*/
 37
 38#ifndef __CRC_CODE_H__
 39#define __CRC_CODE_H__
 40
 41#include <string>
 42using namespace std ;
 43
 44
 45
 46
 47/*=============================================================================
 48|
 49| NAME
 50|  
 51|    CRCCode     
 52|
 53| DESCRIPTION
 54|  
 55|    Class for CRC (Cyclic Redundancy Check) code encoding and 
 56|    decoding.
 57|
 58| METHOD
 59|
 60|    Encoding (systematic):
 61|
 62|      c( x ) = m( x ) + p( x )
 63|
 64|                  n-k
 65|      m( x ) =  x     i( x )             Message portion of codeword c(x).
 66|                  n-k
 67|      p( x ) = -x     i( x ) mod g( x )  Parity portion of codeword c(x).
 68|
 69|    Decoding:
 70|
 71|      s( x ) = c( x ) mod g( x )         Syndrome of codeword c(x).
 72|
 73|      The shift register computes
 74|
 75|          n-k
 76|      [ x     c( x ) ] mod g( x )
 77|
 78|      This shifted syndrome is OK to use for error checking because it 
 79|      is zero iff c( x ) was a codeword.
 80|
 81+============================================================================*/
 82
 83class CRCCode
 84{
 85    public:
 86		// Construct CRC from given generator polynomial.
 87        CRCCode( const string &                       g, 
 88			     syndrome_t    shiftRegisterPreset = 0L,
 89				 bool          invertParity        = false ) ;
 90
 91		~CRCCode() ;
 92
 93		// Return the parity bits computed on the data in the buffer.
 94        // Parity bits are in the most significant bits of the buffer.
 95		syndrome_t parityBits( unsigned char * buffer, long int n ) ;
 96
 97        // Append CRC parity bytes to the end of the buffer.
 98		// The buffer must be long enough to accomodate the
 99		// extra parity bits.  Buffer length is increased
100		// upon exit.
101        void addParityToBuffer( unsigned char * buffer, long int n ) ;
102
103        // Compute the shifted syndrome of the codeword.
104		// Zero indicates we have a codeword.
105		syndrome_t shiftedSyndrome( unsigned char * buffer, 
106				                    long int        n ) ;
107
108		// Return the number of parity bytes.
109		int getNumParityBytes( void ) const ;
110
111	private:
112		// The linear feedback shift register circuit we will use
113		// to do all the computations.
114		ShiftRegister shiftRegister_ ;
115
116		// Number of parity bits, n - k
117		int numParityBits_ ;
118
119		// Shift register may be loaded with a value
120		// (usually FFF..FFF) instead of zero.
121		int shiftRegisterPreset_ ;
122
123		// Some implementations do a 1's complement
124		// of the parity bits.
125		bool invertParity_ ;
126
127	private:
128		// Disable automatic generation of copy constructor
129		// and assignment operator.
130		CRCCode( const CRCCode & ) ;
131		CRCCode operator=( const CRCCode & ) ;
132} ;
133
134#endif // __CRC_CODE_H__