⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 utility.cpp

📁 针对cdma2000层3信令的SMS的unicode ,解析出文本格式的SMS的源码,在c++builder环境下开发.
💻 CPP
字号:
/*===================================================================
SERVICES:   UTILITY ROUTINES FOR COMPUTING THE CRC and Bit copying
            routines.
DESCRIPTION:
   This module provides utilities form computing a 30 bit CRC as 
   specified in the IS-95A and IS-2000 and also provides bit copying
   routines
===================================================================*/

//===================================================================
//   INCLUDES AND PUBLIC DATA DECLARATIONS
//===================================================================

//-------------------------------------------------------------------
// Defines
//-------------------------------------------------------------------

#define  CRC_MASK    0x6030B9C7

//-------------------------------------------------------------------
// Include Files
//-------------------------------------------------------------------
//#include "vxWorks.h"
//#include "csmtypes.h"
//#include "csmdef.h"
#include "utility.h"

#include "stdio.h"
#include "stdlib.h"

//-------------------------------------------------------------------
// Global Constant Declarations 
//-------------------------------------------------------------------

/*-------------------------------------------------------------------
   Static Variable Definitions  
-------------------------------------------------------------------*/

//- ------------------------------------------------------------------
//  Type Declarations
//- ------------------------------------------------------------------
    
/*- ------------------------------------------------------------------
    Function Prototypes
--- ----------------------------------------------------------------*/
    
//===================================================================
//  CLASS DEFINITIONS and FUNCTION DECLARATIONS
//===================================================================



/*=========================================================================
FUNCTION:   BCopy

DESCRIPTION:
   Arbitrary bit copy.

===========================================================================*/
//
bool BCopy
(
   const byte*    sourceBitBuffer,
   uint32         sourceBitOffset,
   byte*          destinationBitBuffer,
   uint32         destinationBitOffset,
   uint32         bitCount                // Number of bits to copy
)
{
   uint32         sourceBitIndexInByte;
   uint32         destinationBitIndexInByte;
   uint32         sourceByteIndex;
   uint32         destinationByteIndex;
   uint32         i;

   sourceByteIndex = sourceBitOffset / 8;
   sourceBitIndexInByte = sourceBitOffset % 8;

   destinationByteIndex = destinationBitOffset / 8;
   destinationBitIndexInByte = destinationBitOffset % 8;

   for( i=0; i<bitCount; i++ )
   {
      if( sourceBitBuffer[sourceByteIndex] & (1<<(7-sourceBitIndexInByte)) )
      {
         // This bit is set, copy to the destination bit buffer
         destinationBitBuffer[destinationByteIndex] |=
                                    (1<<(7-destinationBitIndexInByte));
      }
      else
      {
         destinationBitBuffer[destinationByteIndex] &=
                                    ~(byte)(1<<(7-destinationBitIndexInByte));
      }

      sourceBitIndexInByte += 1;
      if( sourceBitIndexInByte == 8 )
      {
         sourceBitIndexInByte = 0;
         sourceByteIndex += 1;
      }

      destinationBitIndexInByte += 1;
      if( destinationBitIndexInByte == 8 )
      {
         destinationBitIndexInByte = 0;
         destinationByteIndex += 1;
      }
   }

   return true;
}  // BCopy



/*=========================================================================
FUNCTION:   BitCopyFromBitStream

DESCRIPTION:
   Function copies bitCount bits from the supplied byte stream sourceBitBuffer
   starting at sourceBitOffset into the supplied byte result.

===========================================================================*/

void BitCopyFromBitStream
(
   const byte* sourceBitBuffer,
   uint32      sourceBitOffset,
   byte&       result,
   uint32      bitCount
)
{
   byte           bitIndexInByte;
   uint32         byteIndex;
   uint32         i;
   byte           resultOffset;

   if( bitCount == 0 )
   {
      return;
   }

   if( bitCount > (int)sizeof(result)*8 )
   {
      return;
   }

   byteIndex      = sourceBitOffset / 8;
   bitIndexInByte = sourceBitOffset % 8;

   resultOffset = 1 << (bitCount-1);

   result = 0;

   for( i=0; i<bitCount; i++ )
   {
      
      if( sourceBitBuffer[byteIndex] & (1<<(7-bitIndexInByte)) )
      {
         // This bit is set, copy to the result
         result += resultOffset;
      }
 
      resultOffset >>= 1;
      bitIndexInByte += 1;
      if( bitIndexInByte == 8 )
      {
         bitIndexInByte = 0;
         byteIndex += 1;
      }
   }

   return;
}  // BitCopyFromBitStream



/*=========================================================================
FUNCTION:   BitCopyFromBitStream

DESCRIPTION:
   Function copies bitCount bits from the supplied byte stream sourceBitBuffer
   starting at sourceBitOffset into the supplied uint16 result.

===========================================================================*/

void BitCopyFromBitStream
(
   const byte* sourceBitBuffer,
   uint32      sourceBitOffset,
   uint16&     result,
   uint32      bitCount
)
{
   byte           bitIndexInByte;
   uint32         byteIndex;
   uint32         i;
   uint16         resultOffset;

   if( bitCount == 0 )
   {
      return;
   }

   if( bitCount > (int)sizeof(result)*8 )
   {
      return;
   }

   byteIndex      = sourceBitOffset / 8;
   bitIndexInByte = sourceBitOffset % 8;

   resultOffset = 1 << (bitCount-1);

   result = 0;

   for( i=0; i<bitCount; i++ )
   {
      if( sourceBitBuffer[byteIndex] & (1<<(7-bitIndexInByte)) )
      {
         // This bit is set, copy to the result
         result += resultOffset;
      }

      resultOffset >>= 1;
      bitIndexInByte += 1;
      if( bitIndexInByte == 8 )
      {
         bitIndexInByte = 0;
         byteIndex += 1;
      }
   }

   return;
}  // BitCopyFromBitStream



/*=========================================================================
FUNCTION:   BitCopyFromBitStream

DESCRIPTION:
   Function copies bitCount bits from the supplied byte stream sourceBitBuffer
   starting at sourceBitOffset into the supplied uint32 result.

===========================================================================*/

void BitCopyFromBitStream
(
   const byte* sourceBitBuffer,
   uint32      sourceBitOffset,
   uint32&     result,
   uint32      bitCount
)
{
   byte           bitIndexInByte;
   uint32         byteIndex;
   uint32         i;
   uint32         resultOffset;

   if( bitCount == 0 )
   {
      return;
   }

   if( bitCount > (int)sizeof(result)*8 )
   {
      return;
   }

   byteIndex      = sourceBitOffset / 8;
   bitIndexInByte = sourceBitOffset % 8;

   resultOffset = 1 << (bitCount-1);

   result = 0;

   for( i=0; i<bitCount; i++ )
   {
      if( sourceBitBuffer[byteIndex] & (1<<(7-bitIndexInByte)) )
      {
         // This bit is set, copy to the result
         result += resultOffset;
      }

      resultOffset >>= 1;
      bitIndexInByte += 1;
      if( bitIndexInByte == 8 )
      {
         bitIndexInByte = 0;
         byteIndex += 1;
      }
   }

   return;
}  // BitCopyFromBitStream



/*=========================================================================
FUNCTION:   BitCopyFromBitStream

DESCRIPTION:
   Function copies bitCount bits from the supplied byte stream sourceBitBuffer
   starting at sourceBitOffset into the supplied uint64 result.

===========================================================================*/

void BitCopyFromBitStream
(
   const byte* sourceBitBuffer,
   uint32      sourceBitOffset,
   uint64&     result,
   uint32      bitCount
)
{
   byte           bitIndexInByte;
   uint32         byteIndex;
   uint32         i;
   uint64         resultOffset;

   if( bitCount == 0 )
   {
      return;
   }

   if( bitCount > (int)sizeof(result)*8 )
   {
      return;
   }

   byteIndex      = sourceBitOffset / 8;
   bitIndexInByte = sourceBitOffset % 8;

   resultOffset = 1 << (bitCount-1);

   result = (uint32)0;

   for( i=0; i<bitCount; i++ )
   {
      if( sourceBitBuffer[byteIndex] & (1<<(7-bitIndexInByte)) )
      {
         // This bit is set, copy to the result
         result = result + resultOffset;
      }

      resultOffset >>= 1;
      bitIndexInByte += 1;
      if( bitIndexInByte == 8 )
      {
         bitIndexInByte = 0;
         byteIndex += 1;
      }
   }

   return;
}  // BitCopyFromBitStream



/*=========================================================================
FUNCTION:   BitCopyToBitStream

DESCRIPTION:
   Function copies bitCount bits from the supplied byte source into the
   destinationBitBuffer starting at destinationBitOffset.

===========================================================================*/
void BitCopyToBitStream
(
   const byte& source,
   byte*       destinationBitBuffer,
   uint32      destinationBitOffset,
   uint32      bitCount
)
{
   byte           bitIndexInByte;
   uint32         byteIndex;
   uint32         i;
   byte           sourceOffset;

   if( bitCount == 0 )
   {
      return;
   }

   if( bitCount > (int)sizeof(source)*8 )
   {
      return;
   }

   byteIndex      = destinationBitOffset / 8;
   bitIndexInByte = destinationBitOffset % 8;

   sourceOffset = 1 << (bitCount-1);

   for( i=0; i<bitCount; i++ )
   {
      if( source & sourceOffset )
      {
         // This bit is set, copy to the result
         destinationBitBuffer[byteIndex] |= (1<<(7-bitIndexInByte));
      }
      else
      {
         destinationBitBuffer[byteIndex] &= ~(byte)(1<<(7-bitIndexInByte));
      }

      sourceOffset >>= 1;
      bitIndexInByte += 1;
      if( bitIndexInByte == 8 )
      {
         bitIndexInByte = 0;
         byteIndex += 1;
      }
   }

   return;
}  // BitCopyToBitStream


/*=========================================================================
FUNCTION:   BitCopyToBitStream

DESCRIPTION:
   Function copies bitCount bits from the supplied uint16 source into the
   destinationBitBuffer starting at destinationBitOffset.

===========================================================================*/
void BitCopyToBitStream
(
   const uint16&  source,
   byte*          destinationBitBuffer,
   uint32         destinationBitOffset,
   uint32         bitCount )
{
   byte           bitIndexInByte;
   uint32         byteIndex;
   uint32         i;
   uint16         sourceOffset;

   if( bitCount == 0 )
   {
      return;
   }

   if( bitCount > (int)sizeof(source)*8 )
   {
      return;
   }

   byteIndex      = destinationBitOffset / 8;
   bitIndexInByte = destinationBitOffset % 8;

   sourceOffset = 1 << (bitCount-1);

   for( i=0; i<bitCount; i++ )
   {
      if( source & sourceOffset )
      {
         // This bit is set, copy to the result
         destinationBitBuffer[byteIndex] |= (1<<(7-bitIndexInByte));
      }
      else
      {
         destinationBitBuffer[byteIndex] &= ~(byte)(1<<(7-bitIndexInByte));
      }

      sourceOffset >>= 1;
      bitIndexInByte += 1;
      if( bitIndexInByte == 8 )
      {
         bitIndexInByte = 0;
         byteIndex += 1;
      }
   }

   return;
}  // BitCopyToBitStream


/*=========================================================================
FUNCTION:   BitCopyToBitStream

DESCRIPTION:
   Function copies bitCount bits from the supplied uint32 source into the
   destinationBitBuffer starting at destinationBitOffset.

===========================================================================*/
void BitCopyToBitStream
(
   const uint32&  source,
   byte*          destinationBitBuffer,
   uint32         destinationBitOffset,
   uint32         bitCount
)
{
   byte           bitIndexInByte;
   uint32         byteIndex;
   uint32         i;
   uint32         sourceOffset;

   if( bitCount == 0 )
   {
      return;
   }

   if( bitCount > (int)sizeof(source)*8 )
   {
      return;
   }

   byteIndex      = destinationBitOffset / 8;
   bitIndexInByte = destinationBitOffset % 8;

   sourceOffset = 1 << (bitCount-1);

   for( i=0; i<bitCount; i++ )
   {
      if( source & sourceOffset )
      {
         // This bit is set, copy to the result
         destinationBitBuffer[byteIndex] |= (1<<(7-bitIndexInByte));
      }
      else
      {
         destinationBitBuffer[byteIndex] &= ~(byte)(1<<(7-bitIndexInByte));
      }

      sourceOffset >>= 1;
      bitIndexInByte += 1;
      if( bitIndexInByte == 8 )
      {
         bitIndexInByte = 0;
         byteIndex += 1;
      }
   }

   return;
}  // BitCopyToBitStream


/*=========================================================================
FUNCTION:   BitCopyToBitStream

DESCRIPTION:
   Function copies bitCount bits from the supplied uint64 source into the
   destinationBitBuffer starting at destinationBitOffset.

===========================================================================*/
void BitCopyToBitStream
(
   const uint64&  source,
   byte*          destinationBitBuffer,
   uint32         destinationBitOffset,
   uint32         bitCount
)
{
   byte           bitIndexInByte;
   uint32         byteIndex;
   uint32         i;
   uint64         sourceOffset=0;
   uint64         zero=0;

   if( bitCount == 0 )
   {
      return;
   }

   if( bitCount > (int)sizeof(source)*8 )
   {
      return;
   }

   byteIndex      = destinationBitOffset / 8;
   bitIndexInByte = destinationBitOffset % 8;

   sourceOffset = 1;
   sourceOffset <<= (bitCount-1);

   for( i=0; i<bitCount; i++ )
   {
      if( (source & sourceOffset) != zero )
      {
         // This bit is set, copy to the result
         destinationBitBuffer[byteIndex] |= (1<<(7-bitIndexInByte));
      }
      else
      {
         destinationBitBuffer[byteIndex] &= ~(byte)(1<<(7-bitIndexInByte));
      }

      sourceOffset >>= (uint32)1;
      bitIndexInByte += 1;
      if( bitIndexInByte == 8 )
      {
         bitIndexInByte = 0;
         byteIndex += 1;
      }
   }

   return;
}  // BitCopyToBitStream



/**************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -