Validate Longitudinal Redundancy Checksum (LRC) source code

This snippet submitted by Darko Kolaković on 2005-09-06. It has been viewed 53257 times.
Rating of 5.4 with 216 votes

/*$Workfile: KIsLRC.cpp$: implementation file
  $Revision: 5$ $Date: 2005-06-17 18:10:52$
  $Author: Darko Kolakovic$

  Validates Longitudinal Redundancy Checksum (LRC)
  CommonSoft Inc.
  Jan. 2k Darko Kolakovic
*/

/*Note: MS VC/C++ - Disable precompiled headers (/Yu"stdafx.h" option)       */

#ifndef _KCHARCST_H_
  #include "KCharCst.h" //ETX
#endif

#ifdef _DEBUG
  #define new DEBUG_NEW
  #undef THIS_FILE
  static char THIS_FILE[] = __FILE__;
#endif

#ifndef BYTE
  #define BYTE unsigned char
#endif

#ifndef NULL
  #ifdef  __cplusplus
      //NULL pointer value
    #define NULL  0
  #else
      /*NULL pointer value */
    #define NULL  ((void *)0)
  #endif
#endif

extern "C" BYTE GetLRC(BYTE* pbData,int iCount);

//IsLRCValid()-----------------------------------------------------------------
/*Validates Longitudinal Redundancy Checksum (LRC).

  Returns: true if given LRC is same as calculated. If checksums are different
  or if the last byte in data array is not termination character (EOT/ETB/ETX)
  or if the length of the data is zero, returned value is false.

  Example:

      {
      ...
      BYTE chData[10] = {STX,'1','2','3','4','5','6','7',EOT};
      chData[9] = GetLRC(&chData[1],8);
      ...
      if(IsLRCValid(chData[9],&chData[1],8))
          TRACE0("LRC is valid\n");
      }
 */
bool IsLRCValid(const BYTE chReceivedLRC, //[in] value to be verified
                BYTE* pbData,             //[in] data buffer including
                                          //termination character (EOT/ETB/ETX)
                int iCount    //[in] size of data in bytes including
                              //termination character (minimum value is 1)
                )
{
if ((pbData != NULL) && (iCount > 0) )
  {
  if ((pbData[iCount - 1] == EOT) ||
      (pbData[iCount - 1] == ETB) ||
      (pbData[iCount - 1] == ETX))
    if (chReceivedLRC == GetLRC(pbData,iCount))
      return true;
  }

return false;
}

///////////////////////////////////////////////////////////////////////////////
/*$Workfile: KGetLRC.cpp$: implementation file
  $Revision: 4$ $Date: 2004-06-01 17:50:31$
  $Author: Darko$

  Longitudinal Redundancy Checksum (LRC)
  Copyright: CommonSoft Inc.
  4.5.1989 Darko Kolakovic (original C version)
 */

/*Note: MS VC/C++ - Disable precompiled headers (/Yu"stdafx.h" option)       */

#ifndef _KCHARCST_H_
  #include "KCharCst.h" //ETX
#endif

#ifdef _DEBUG
  #define new DEBUG_NEW
  #undef THIS_FILE
  static char THIS_FILE[] = __FILE__;
#endif

#ifndef BYTE
  #define BYTE unsigned char
#endif

extern "C" BYTE GetLRC(BYTE* pbData,int iCount);

//GetLRC()---------------------------------------------------------------------
/*Calculates one byte Longitudinal Redundancy Checksum (LRC). The LRC is an 
  exclusive-or calculation on all data bytes. The LRC is calculated from the 
  begining of data buffer to the termination character, including termination 
  character itself. 

  Returns: Longitudinal Redundancy Checksum (LRC).


          +---+
          |STX|  Start of Text [1 Byte]
       /--+---+--
       |  | D |  Data [N bytes]
       L  | a |
       R  | t |
       C  | a |
          +---+
       s  |ETX|
       u  |ETB|  Termination Character [1 Byte]
       m  |EOT|
       \--+---+--
          |LRC|  Longitudinal Redundancy Checksum [1 Byte]
          +---+
                 iCount = N+1 [byte]

 */
BYTE GetLRC(BYTE* pbData, //[in] data buffer including 
                          //termination character (ETB/ETX/EOT)
            int iCount    //[in] size of data in bytes including
                          //termination character
            )
{
BYTE chLRC = 0;
while(iCount > 0)
  {
  chLRC ^= *pbData++;
  iCount--;
  }

return chLRC;
}

///////////////////////////////////////////////////////////////////////////////




More C and C++ source code snippets