Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //***************
- // "endian.h" *
- //***************
- enum endianness_t {
- BIG_ENDIAN,
- LITTLE_ENDIAN
- };
- // I could make this a namespace if those had private and public, since this class has only static members.
- class Endian {
- private:
- // Gets the endianness of the system
- static const endianness_t _SysEndian( void );
- static endianness_t _endian;
- static short SwapShort( const short nValue );
- static short KeepShort( const short nValue );
- static int SwapInt( const int nValue );
- static int KeepInt( const int nValue );
- static long SwapLong( const long nValue );
- static long KeepLong( const long nValue );
- static float SwapFloat( const float fValue );
- static float KeepFloat( const float fValue );
- static double SwapDouble( const double fValue );
- static double KeepDouble( const double fValue );
- static void SwapRevBytes( void *pBase, const unsigned int nUnitSize, int nUnitCount );
- static void KeepRevBytes( void *pBase, const unsigned int nUnitSize, int nUnitCount );
- public:
- static const endianness_t *sysEndian;
- static void Init( void );
- // Method pointers to convert from little/big endian to the system's endian. Call Init() before use - that's where they're set, just to avoid branching.
- static short ( *_BigShort )( const short nValue );
- static short ( *_LittleShort )( const short nValue );
- static int ( *_BigInt )( const int nValue );
- static int ( *_LittleInt )( const int nValue );
- static long ( *_BigLong )( const long nValue );
- static long ( *_LittleLong )( const long nValue );
- static float ( *_BigFloat )( const float fValue );
- static float ( *_LittleFloat )( const float fValue );
- static double ( *_BigDouble )( const double fValue );
- static double ( *_LittleDouble )( const double fValue );
- static void ( *_BigRevBytes )( void *pBase, const unsigned int nUnitSize, int nUnitCount );
- static void ( *_LittleRevBytes )( void *pBase, const unsigned int nUnitSize, int nUnitCount );
- };
- //***************
- // "endian.cpp" *
- //***************
- #include "endian.h"
- short BigShort( const short nValue ) { return Endian::_BigShort( nValue ); }
- short LittleShort( const short nValue ) { return Endian::_LittleShort( nValue ); }
- int BigInt( const int nValue ) { return Endian::_BigInt( nValue ); }
- int LittleInt( const int nValue ) { return Endian::_LittleInt( nValue ); }
- long BigLong( const long nValue ) { return Endian::_BigLong( nValue ); }
- long LittleLong( const long nValue ) { return Endian::_LittleLong( nValue ); }
- float BigFloat( const float fValue ) { return Endian::_BigFloat( fValue ); }
- float LittleFloat( const float fValue ) { return Endian::_LittleFloat( fValue ); }
- double BigDouble( const double fValue ) { return Endian::_BigDouble( fValue ); }
- double LittleDouble( const double fValue ) { return Endian::_LittleDouble( fValue ); }
- void BigRevBytes( void *pBase, const unsigned int nUnitSize, int nUnitCount ) { Endian::_BigRevBytes( pBase, nUnitSize, nUnitCount ); }
- void LittleRevBytes( void *pBase, const unsigned int nUnitSize, int nUnitCount ) { Endian::_LittleRevBytes( pBase, nUnitSize, nUnitCount ); }
- /*
- ====================
- SysEndian
- Returns a value indicating the system's endianness.
- ====================
- */
- const endianness_t Endian::_SysEndian( void ) {
- unsigned char endianTest[] = { 1, 0 };
- if ( *( short * )endianTest == 1 ) {
- return LITTLE_ENDIAN;
- } else {
- return BIG_ENDIAN;
- }
- }
- /*
- ====================
- Init
- Sets up variables for use of the member functions/variables.
- ====================
- */
- void Endian::Init( void ) {
- _endian = _SysEndian();
- sysEndian = &_endian;
- if( _endian == LITTLE_ENDIAN ) {
- // likely an x86 machine
- _BigShort = SwapShort;
- _LittleShort = KeepShort;
- _BigInt = SwapInt;
- _LittleInt = KeepInt;
- _BigLong = SwapLong;
- _LittleLong = KeepLong;
- _BigFloat = SwapFloat;
- _LittleFloat = KeepFloat;
- _BigDouble = SwapDouble;
- _LittleDouble = KeepDouble;
- _BigRevBytes = SwapRevBytes;
- _LittleRevBytes = KeepRevBytes;
- } else {
- // mac or what do I know
- _BigShort = KeepShort;
- _LittleShort = SwapShort;
- _BigInt = KeepInt;
- _LittleInt = SwapInt;
- _BigLong = KeepLong;
- _LittleLong = SwapLong;
- _BigFloat = KeepFloat;
- _LittleFloat = SwapFloat;
- _BigDouble = KeepDouble;
- _LittleDouble = SwapDouble;
- _BigRevBytes = KeepRevBytes;
- _LittleRevBytes = SwapRevBytes;
- }
- }
- /*
- ====================
- SwapShort
- ====================
- */
- short Endian::SwapShort( const short nValue ) {
- unsigned char FirstByte, SecondByte;
- SecondByte = nValue & 255;
- FirstByte = ( nValue >> 8 ) & 255;
- return ( SecondByte << 8 ) + FirstByte;
- }
- /*
- ====================
- KeepShort
- ====================
- */
- short Endian::KeepShort( const short nValue ) {
- return nValue;
- }
- /*
- ====================
- SwapInt
- ====================
- */
- int Endian::SwapInt( const int nValue ) {
- unsigned char FirstByte, SecondByte, ThirdByte, FourthByte;
- FourthByte = ( nValue ) & 255;
- ThirdByte = ( nValue >> 8 ) & 255;
- SecondByte = ( nValue >> 16 ) & 255;
- FirstByte = ( nValue >> 24 ) & 255;
- return ( ( int ) FourthByte << 24 ) + ( ( int ) ThirdByte << 16 ) + ( ( int ) SecondByte << 8 ) + FirstByte;
- }
- /*
- ====================
- KeepInt
- ====================
- */
- int Endian::KeepInt( const int nValue ) {
- return nValue;
- }
- /*
- ====================
- SwapLong
- ====================
- */
- long Endian::SwapLong( const long nValue ) {
- unsigned char FirstByte, SecondByte, ThirdByte, FourthByte, FifthByte, SixthByte, SeventhByte, EighthByte;
- EighthByte = ( nValue ) & 255;
- SeventhByte = ( nValue >> 8 ) & 255;
- SixthByte = ( nValue >> 16 ) & 255;
- FifthByte = ( nValue >> 24 ) & 255;
- FourthByte = ( nValue >> 32 ) & 255;
- ThirdByte = ( nValue >> 40 ) & 255;
- SecondByte = ( nValue >> 48 ) & 255;
- FirstByte = ( nValue >> 56 ) & 255;
- return ( ( long ) EighthByte << 56 )
- + ( ( long ) SeventhByte << 48 )
- + ( ( long ) SixthByte << 40 )
- + ( ( long ) FifthByte << 32 )
- + ( ( long ) FourthByte << 24 )
- + ( ( long ) ThirdByte << 16 )
- + ( ( long ) SecondByte << 8 )
- + FirstByte;
- }
- /*
- ====================
- KeepLong
- ====================
- */
- long Endian::KeepLong( const long nValue ) {
- return nValue;
- }
- /*
- ====================
- SwapFloat
- ====================
- */
- float Endian::SwapFloat( const float fValue ) {
- union {
- float f;
- unsigned char b[ 4 ];
- } Data1, Data2;
- Data1.f = fValue;
- Data2.b[ 0 ] = Data1.b[ 3 ];
- Data2.b[ 1 ] = Data1.b[ 2 ];
- Data2.b[ 2 ] = Data1.b[ 1 ];
- Data2.b[ 3 ] = Data1.b[ 0 ];
- return Data2.f;
- }
- /*
- ====================
- KeepFloat
- ====================
- */
- float Endian::KeepFloat( const float fValue ) {
- return fValue;
- }
- /*
- ====================
- SwapDouble
- ====================
- */
- double Endian::SwapDouble( const double fValue ) {
- union {
- double f;
- unsigned char b[ 8 ];
- } Data1, Data2;
- Data1.f = fValue;
- Data2.b[ 0 ] = Data1.b[ 7 ];
- Data2.b[ 1 ] = Data1.b[ 6 ];
- Data2.b[ 2 ] = Data1.b[ 5 ];
- Data2.b[ 3 ] = Data1.b[ 4 ];
- Data2.b[ 4 ] = Data1.b[ 3 ];
- Data2.b[ 5 ] = Data1.b[ 2 ];
- Data2.b[ 6 ] = Data1.b[ 1 ];
- Data2.b[ 7 ] = Data1.b[ 0 ];
- return Data2.f;
- }
- /*
- ====================
- KeepDouble
- ====================
- */
- double Endian::KeepDouble( const double fValue ) {
- return fValue;
- }
- /*
- ====================
- SwapRevBytes
- ====================
- */
- void Endian::SwapRevBytes( void *pBase, const unsigned int nUnitSize, int nUnitCount ) {
- register unsigned char *pLower, *pUpper;
- pLower = ( unsigned char* )pBase;
- if( nUnitSize == 2 ) {
- pUpper = pLower + 1;
- while( nUnitCount-- ) {
- *pLower ^= *pUpper;
- *pUpper ^= *pLower;
- *pLower ^= *pUpper;
- pLower += 2;
- pUpper += 2;
- }
- return;
- }
- while( nUnitCount-- ) {
- pUpper = pLower + nUnitSize - 1;
- while( pLower < pUpper ) {
- *pLower ^= *pUpper;
- *pUpper ^= *pLower;
- *pLower ^= *pUpper;
- ++pLower;
- --pUpper;
- }
- pLower += nUnitSize >> 1;
- }
- }
- /*
- ====================
- KeepRevBytes
- ====================
- */
- void Endian::KeepRevBytes( void *pBase, const unsigned int nUnitSize, int nUnitCount ) {
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment