Replaced bitbuf global error handler with per class error recovery handler
This commit is contained in:
parent
522f5a11ee
commit
df008f5a73
52
external/sourcesdk/bitbuf.cpp
vendored
52
external/sourcesdk/bitbuf.cpp
vendored
@ -58,27 +58,11 @@ inline unsigned int CountTrailingZeros(unsigned int elem)
|
||||
#define FAST_BIT_SCAN 0
|
||||
#endif
|
||||
|
||||
|
||||
static BitBufErrorHandler g_BitBufErrorHandler = 0;
|
||||
|
||||
inline int BitForBitnum(int bitnum)
|
||||
{
|
||||
return GetBitForBitnum(bitnum);
|
||||
}
|
||||
|
||||
void InternalBitBufErrorHandler( BitBufErrorType errorType, const char *pDebugName )
|
||||
{
|
||||
if ( g_BitBufErrorHandler )
|
||||
g_BitBufErrorHandler( errorType, pDebugName );
|
||||
}
|
||||
|
||||
|
||||
void SetBitBufErrorHandler( BitBufErrorHandler fn )
|
||||
{
|
||||
g_BitBufErrorHandler = fn;
|
||||
}
|
||||
|
||||
|
||||
// #define BB_PROFILING
|
||||
|
||||
unsigned long g_LittleBits[32];
|
||||
@ -130,6 +114,7 @@ bf_write::bf_write()
|
||||
m_bOverflow = false;
|
||||
m_bAssertOnOverflow = true;
|
||||
m_pDebugName = NULL;
|
||||
m_errorHandler = NULL;
|
||||
}
|
||||
|
||||
bf_write::bf_write( const char *pDebugName, void *pData, int nBytes, int nBits )
|
||||
@ -196,6 +181,19 @@ void bf_write::SetDebugName( const char *pDebugName )
|
||||
m_pDebugName = pDebugName;
|
||||
}
|
||||
|
||||
void bf_write::SetErrorHandler(IBitBufOverErrorHandler* handler)
|
||||
{
|
||||
m_errorHandler = handler;
|
||||
}
|
||||
|
||||
bool bf_write::CallErrorHandler(BitBufErrorType errorType)
|
||||
{
|
||||
if (m_errorHandler)
|
||||
{
|
||||
return m_errorHandler->HandleError(errorType, GetDebugName());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void bf_write::SeekToBit( int bitPos )
|
||||
{
|
||||
@ -452,12 +450,15 @@ bool bf_write::WriteBits(const void *pInData, int nBits)
|
||||
int nBitsLeft = nBits;
|
||||
|
||||
// Bounds checking..
|
||||
if ( (m_iCurBit+nBits) > m_nDataBits )
|
||||
if (GetNumBitsLeft() < nBits)
|
||||
{
|
||||
const bool recovered = CallErrorHandler(BITBUFERROR_BUFFER_OVERRUN);
|
||||
if (!recovered || (recovered && (GetNumBitsLeft() < nBits)))
|
||||
{
|
||||
SetOverflowFlag();
|
||||
CallErrorHandler( BITBUFERROR_BUFFER_OVERRUN, GetDebugName() );
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Align output to dword boundary
|
||||
while (((unsigned long)pOut & 3) != 0 && nBitsLeft >= 8)
|
||||
@ -794,6 +795,7 @@ bf_read::bf_read()
|
||||
m_bOverflow = false;
|
||||
m_bAssertOnOverflow = true;
|
||||
m_pDebugName = NULL;
|
||||
m_errorHandler = NULL;
|
||||
}
|
||||
|
||||
bf_read::bf_read( const void *pData, int nBytes, int nBits )
|
||||
@ -847,6 +849,20 @@ void bf_read::SetDebugName( const char *pName )
|
||||
m_pDebugName = pName;
|
||||
}
|
||||
|
||||
void bf_read::SetErrorHandler(IBitBufOverErrorHandler* handler)
|
||||
{
|
||||
m_errorHandler = handler;
|
||||
}
|
||||
|
||||
bool bf_read::CallErrorHandler(BitBufErrorType errorType)
|
||||
{
|
||||
if (m_errorHandler)
|
||||
{
|
||||
return m_errorHandler->HandleError(errorType, GetDebugName());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void bf_read::SetOverflowFlag()
|
||||
{
|
||||
if ( m_bAssertOnOverflow )
|
||||
|
70
external/sourcesdk/include/sourcesdk/bitbuf.h
vendored
70
external/sourcesdk/include/sourcesdk/bitbuf.h
vendored
@ -36,21 +36,11 @@ typedef enum
|
||||
BITBUFERROR_NUM_ERRORS
|
||||
} BitBufErrorType;
|
||||
|
||||
|
||||
typedef void (*BitBufErrorHandler)( BitBufErrorType errorType, const char *pDebugName );
|
||||
|
||||
|
||||
#if defined( _DEBUG )
|
||||
extern void InternalBitBufErrorHandler( BitBufErrorType errorType, const char *pDebugName );
|
||||
#define CallErrorHandler( errorType, pDebugName ) InternalBitBufErrorHandler( errorType, pDebugName );
|
||||
#else
|
||||
#define CallErrorHandler( errorType, pDebugName )
|
||||
#endif
|
||||
|
||||
|
||||
// Use this to install the error handler. Call with NULL to uninstall your error handler.
|
||||
void SetBitBufErrorHandler( BitBufErrorHandler fn );
|
||||
|
||||
class IBitBufOverErrorHandler
|
||||
{
|
||||
public:
|
||||
virtual bool HandleError(BitBufErrorType errorType, const char *pDebugName) = 0;
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Helpers.
|
||||
@ -150,6 +140,8 @@ public:
|
||||
const char* GetDebugName();
|
||||
void SetDebugName( const char *pDebugName );
|
||||
|
||||
void SetErrorHandler(IBitBufOverErrorHandler* handler);
|
||||
bool CallErrorHandler(BitBufErrorType errorType);
|
||||
|
||||
// Seek to a specific position.
|
||||
public:
|
||||
@ -253,6 +245,7 @@ private:
|
||||
|
||||
bool m_bAssertOnOverflow;
|
||||
const char *m_pDebugName;
|
||||
IBitBufOverErrorHandler* m_errorHandler;
|
||||
};
|
||||
|
||||
|
||||
@ -298,10 +291,13 @@ inline const unsigned char* bf_write::GetData() const
|
||||
|
||||
inline bool bf_write::CheckForOverflow(int nBits)
|
||||
{
|
||||
if ( m_iCurBit + nBits > m_nDataBits )
|
||||
if (GetNumBitsLeft() < nBits)
|
||||
{
|
||||
const bool recovered = CallErrorHandler(BITBUFERROR_BUFFER_OVERRUN);
|
||||
if (!recovered || (recovered && GetNumBitsLeft() < nBits))
|
||||
{
|
||||
SetOverflowFlag();
|
||||
CallErrorHandler( BITBUFERROR_BUFFER_OVERRUN, GetDebugName() );
|
||||
}
|
||||
}
|
||||
|
||||
return m_bOverflow;
|
||||
@ -339,11 +335,14 @@ inline void bf_write::WriteOneBitNoCheck(int nValue)
|
||||
inline void bf_write::WriteOneBit(int nValue)
|
||||
{
|
||||
if( m_iCurBit >= m_nDataBits )
|
||||
{
|
||||
const bool recovered = CallErrorHandler(BITBUFERROR_BUFFER_OVERRUN);
|
||||
if (!recovered || (recovered && (m_iCurBit >= m_nDataBits)))
|
||||
{
|
||||
SetOverflowFlag();
|
||||
CallErrorHandler( BITBUFERROR_BUFFER_OVERRUN, GetDebugName() );
|
||||
return;
|
||||
}
|
||||
}
|
||||
WriteOneBitNoCheck( nValue );
|
||||
}
|
||||
|
||||
@ -351,11 +350,14 @@ inline void bf_write::WriteOneBit(int nValue)
|
||||
inline void bf_write::WriteOneBitAt( int iBit, int nValue )
|
||||
{
|
||||
if( iBit >= m_nDataBits )
|
||||
{
|
||||
const bool recovered = CallErrorHandler(BITBUFERROR_BUFFER_OVERRUN);
|
||||
if (!recovered || (recovered && (iBit >= m_nDataBits)))
|
||||
{
|
||||
SetOverflowFlag();
|
||||
CallErrorHandler( BITBUFERROR_BUFFER_OVERRUN, GetDebugName() );
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
#if __i386__
|
||||
if(nValue)
|
||||
@ -379,7 +381,7 @@ inline void bf_write::WriteUBitLong( unsigned int curData, int numbits, bool bCh
|
||||
{
|
||||
if ( curData >= (unsigned long)(1 << numbits) )
|
||||
{
|
||||
CallErrorHandler( BITBUFERROR_VALUE_OUT_OF_RANGE, GetDebugName() );
|
||||
CallErrorHandler(BITBUFERROR_VALUE_OUT_OF_RANGE);
|
||||
}
|
||||
}
|
||||
Assert( numbits >= 0 && numbits <= 32 );
|
||||
@ -387,11 +389,13 @@ inline void bf_write::WriteUBitLong( unsigned int curData, int numbits, bool bCh
|
||||
|
||||
if ( GetNumBitsLeft() < numbits )
|
||||
{
|
||||
m_iCurBit = m_nDataBits;
|
||||
const bool recovered = CallErrorHandler(BITBUFERROR_BUFFER_OVERRUN);
|
||||
if (!recovered || (recovered && (GetNumBitsLeft() < numbits)))
|
||||
{
|
||||
SetOverflowFlag();
|
||||
CallErrorHandler( BITBUFERROR_BUFFER_OVERRUN, GetDebugName() );
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
int iCurBitMasked = m_iCurBit & 31;
|
||||
int iDWord = m_iCurBit >> 5;
|
||||
@ -504,6 +508,9 @@ public:
|
||||
const char* GetDebugName() const { return m_pDebugName; }
|
||||
void SetDebugName( const char *pName );
|
||||
|
||||
void SetErrorHandler(IBitBufOverErrorHandler* handler);
|
||||
bool CallErrorHandler(BitBufErrorType errorType);
|
||||
|
||||
void ExciseBits( int startbit, int bitstoremove );
|
||||
|
||||
|
||||
@ -651,6 +658,7 @@ private:
|
||||
bool m_bAssertOnOverflow;
|
||||
|
||||
const char *m_pDebugName;
|
||||
IBitBufOverErrorHandler* m_errorHandler;
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
@ -700,10 +708,13 @@ inline bool bf_read::SeekRelative(int iBitDelta)
|
||||
|
||||
inline bool bf_read::CheckForOverflow(int nBits)
|
||||
{
|
||||
if( m_iCurBit + nBits > m_nDataBits )
|
||||
if (GetNumBitsLeft() < nBits)
|
||||
{
|
||||
const bool recovered = CallErrorHandler(BITBUFERROR_BUFFER_OVERRUN);
|
||||
if (!recovered || (recovered && (GetNumBitsLeft() < nBits)))
|
||||
{
|
||||
SetOverflowFlag();
|
||||
CallErrorHandler( BITBUFERROR_BUFFER_OVERRUN, GetDebugName() );
|
||||
}
|
||||
}
|
||||
|
||||
return m_bOverflow;
|
||||
@ -729,11 +740,14 @@ inline int bf_read::ReadOneBitNoCheck()
|
||||
inline int bf_read::ReadOneBit()
|
||||
{
|
||||
if( GetNumBitsLeft() <= 0 )
|
||||
{
|
||||
const bool recovered = CallErrorHandler(BITBUFERROR_BUFFER_OVERRUN);
|
||||
if (!recovered || (recovered && (GetNumBitsLeft() <= 0)))
|
||||
{
|
||||
SetOverflowFlag();
|
||||
CallErrorHandler( BITBUFERROR_BUFFER_OVERRUN, GetDebugName() );
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return ReadOneBitNoCheck();
|
||||
}
|
||||
|
||||
@ -762,11 +776,13 @@ inline unsigned int bf_read::ReadUBitLong( int numbits ) __restrict
|
||||
|
||||
if ( GetNumBitsLeft() < numbits )
|
||||
{
|
||||
m_iCurBit = m_nDataBits;
|
||||
const bool recovered = CallErrorHandler(BITBUFERROR_BUFFER_OVERRUN);
|
||||
if (!recovered || (recovered && (GetNumBitsLeft() < numbits)))
|
||||
{
|
||||
SetOverflowFlag();
|
||||
CallErrorHandler( BITBUFERROR_BUFFER_OVERRUN, GetDebugName() );
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int iStartBit = m_iCurBit & 31u;
|
||||
int iLastBit = m_iCurBit + numbits - 1;
|
||||
|
Loading…
Reference in New Issue
Block a user