1.1 --- a/core/bitbuf.cpp Fri Mar 28 06:21:34 2008 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,961 +0,0 @@
1.4 -//========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
1.5 -//
1.6 -// Purpose:
1.7 -//
1.8 -// $NoKeywords: $
1.9 -//
1.10 -//=============================================================================//
1.11 -
1.12 -#include "bitbuf.h"
1.13 -#include "coordsize.h"
1.14 -#include "vector.h"
1.15 -#include "mathlib/math_base.h"
1.16 -#include "vstdlib/strtools.h"
1.17 -
1.18 -
1.19 -// FIXME: Can't use this until we get multithreaded allocations in tier0 working for tools
1.20 -// This is used by VVIS and fails to link
1.21 -// NOTE: This must be the last file included!!!
1.22 -//#include "tier0/memdbgon.h"
1.23 -
1.24 -#ifdef _XBOX
1.25 -// mandatory ... wary of above comment and isolating, tier0 is built as MT though
1.26 -#include "tier0/memdbgon.h"
1.27 -#endif
1.28 -
1.29 -static BitBufErrorHandler g_BitBufErrorHandler = 0;
1.30 -
1.31 -
1.32 -void InternalBitBufErrorHandler( BitBufErrorType errorType, const char *pDebugName )
1.33 -{
1.34 - if ( g_BitBufErrorHandler )
1.35 - g_BitBufErrorHandler( errorType, pDebugName );
1.36 -}
1.37 -
1.38 -
1.39 -void SetBitBufErrorHandler( BitBufErrorHandler fn )
1.40 -{
1.41 - g_BitBufErrorHandler = fn;
1.42 -}
1.43 -
1.44 -
1.45 -// #define BB_PROFILING
1.46 -
1.47 -
1.48 -// Precalculated bit masks for WriteUBitLong. Using these tables instead of
1.49 -// doing the calculations gives a 33% speedup in WriteUBitLong.
1.50 -unsigned long g_BitWriteMasks[32][33];
1.51 -
1.52 -// (1 << i) - 1
1.53 -unsigned long g_ExtraMasks[32];
1.54 -
1.55 -class CBitWriteMasksInit
1.56 -{
1.57 -public:
1.58 - CBitWriteMasksInit()
1.59 - {
1.60 - for( unsigned int startbit=0; startbit < 32; startbit++ )
1.61 - {
1.62 - for( unsigned int nBitsLeft=0; nBitsLeft < 33; nBitsLeft++ )
1.63 - {
1.64 - unsigned int endbit = startbit + nBitsLeft;
1.65 - g_BitWriteMasks[startbit][nBitsLeft] = (1 << startbit) - 1;
1.66 - if(endbit < 32)
1.67 - g_BitWriteMasks[startbit][nBitsLeft] |= ~((1 << endbit) - 1);
1.68 - }
1.69 - }
1.70 -
1.71 - for ( unsigned int maskBit=0; maskBit < 32; maskBit++ )
1.72 - g_ExtraMasks[maskBit] = (1 << maskBit) - 1;
1.73 - }
1.74 -};
1.75 -CBitWriteMasksInit g_BitWriteMasksInit;
1.76 -
1.77 -
1.78 -// ---------------------------------------------------------------------------------------- //
1.79 -// bf_write
1.80 -// ---------------------------------------------------------------------------------------- //
1.81 -
1.82 -bf_write::bf_write()
1.83 -{
1.84 - m_pData = NULL;
1.85 - m_nDataBytes = 0;
1.86 - m_nDataBits = -1; // set to -1 so we generate overflow on any operation
1.87 - m_iCurBit = 0;
1.88 - m_bOverflow = false;
1.89 - m_bAssertOnOverflow = true;
1.90 - m_pDebugName = NULL;
1.91 -}
1.92 -
1.93 -bf_write::bf_write( const char *pDebugName, void *pData, int nBytes, int nBits )
1.94 -{
1.95 - m_bAssertOnOverflow = true;
1.96 - m_pDebugName = pDebugName;
1.97 - StartWriting( pData, nBytes, 0, nBits );
1.98 -}
1.99 -
1.100 -bf_write::bf_write( void *pData, int nBytes, int nBits )
1.101 -{
1.102 - m_bAssertOnOverflow = true;
1.103 - StartWriting( pData, nBytes, 0, nBits );
1.104 -}
1.105 -
1.106 -void bf_write::StartWriting( void *pData, int nBytes, int iStartBit, int nBits )
1.107 -{
1.108 - // Make sure it's dword aligned and padded.
1.109 - Assert( (nBytes % 4) == 0 );
1.110 - Assert(((unsigned long)pData & 3) == 0);
1.111 -
1.112 - m_pData = (unsigned char*)pData;
1.113 - m_nDataBytes = nBytes;
1.114 -
1.115 - if ( nBits == -1 )
1.116 - {
1.117 - m_nDataBits = nBytes << 3;
1.118 - }
1.119 - else
1.120 - {
1.121 - Assert( nBits <= nBytes*8 );
1.122 - m_nDataBits = nBits;
1.123 - }
1.124 -
1.125 - m_iCurBit = iStartBit;
1.126 - m_bOverflow = false;
1.127 -}
1.128 -
1.129 -void bf_write::Reset()
1.130 -{
1.131 - m_iCurBit = 0;
1.132 - m_bOverflow = false;
1.133 -}
1.134 -
1.135 -
1.136 -void bf_write::SetAssertOnOverflow( bool bAssert )
1.137 -{
1.138 - m_bAssertOnOverflow = bAssert;
1.139 -}
1.140 -
1.141 -
1.142 -const char* bf_write::GetDebugName()
1.143 -{
1.144 - return m_pDebugName;
1.145 -}
1.146 -
1.147 -
1.148 -void bf_write::SetDebugName( const char *pDebugName )
1.149 -{
1.150 - m_pDebugName = pDebugName;
1.151 -}
1.152 -
1.153 -
1.154 -void bf_write::SeekToBit( int bitPos )
1.155 -{
1.156 - m_iCurBit = bitPos;
1.157 -}
1.158 -
1.159 -
1.160 -// Sign bit comes first
1.161 -void bf_write::WriteSBitLong( int data, int numbits )
1.162 -{
1.163 - // Do we have a valid # of bits to encode with?
1.164 - Assert( numbits >= 1 );
1.165 -
1.166 - // Note: it does this wierdness here so it's bit-compatible with regular integer data in the buffer.
1.167 - // (Some old code writes direct integers right into the buffer).
1.168 - if(data < 0)
1.169 - {
1.170 -#ifdef _DEBUG
1.171 - if( numbits < 32 )
1.172 - {
1.173 - // Make sure it doesn't overflow.
1.174 -
1.175 - if( data < 0 )
1.176 - {
1.177 - Assert( data >= -(1 << (numbits-1)) );
1.178 - }
1.179 - else
1.180 - {
1.181 - Assert( data < (1 << (numbits-1)) );
1.182 - }
1.183 - }
1.184 -#endif
1.185 -
1.186 - WriteUBitLong( (unsigned int)(0x80000000 + data), numbits - 1, false );
1.187 - WriteOneBit( 1 );
1.188 - }
1.189 - else
1.190 - {
1.191 - WriteUBitLong((unsigned int)data, numbits - 1);
1.192 - WriteOneBit( 0 );
1.193 - }
1.194 -}
1.195 -
1.196 -// writes an unsigned integer with variable bit length
1.197 -void bf_write::WriteUBitVar( unsigned int data )
1.198 -{
1.199 - unsigned int bits = 0;
1.200 - unsigned int base = 0;
1.201 -
1.202 - while (data > (base<<1))
1.203 - {
1.204 - bits++;
1.205 - base = (1<<bits)-1;
1.206 - }
1.207 -
1.208 - // how many bits do we use
1.209 - if ( bits > 0)
1.210 - WriteUBitLong( 0, bits );
1.211 -
1.212 - // end marker
1.213 - WriteOneBit( 1 );
1.214 -
1.215 - // write the value
1.216 - if ( bits > 0)
1.217 - WriteUBitLong( data - base , bits );
1.218 -}
1.219 -
1.220 -void bf_write::WriteBitLong(unsigned int data, int numbits, bool bSigned)
1.221 -{
1.222 - if(bSigned)
1.223 - WriteSBitLong((int)data, numbits);
1.224 - else
1.225 - WriteUBitLong(data, numbits);
1.226 -}
1.227 -
1.228 -bool bf_write::WriteBits(const void *pInData, int nBits)
1.229 -{
1.230 -#if defined( BB_PROFILING )
1.231 - VPROF( "bf_write::WriteBits" );
1.232 -#endif
1.233 -
1.234 - unsigned char *pOut = (unsigned char*)pInData;
1.235 - int nBitsLeft = nBits;
1.236 -
1.237 - if((m_iCurBit+nBits) > m_nDataBits)
1.238 - {
1.239 - SetOverflowFlag();
1.240 - CallErrorHandler( BITBUFERROR_BUFFER_OVERRUN, GetDebugName() );
1.241 - return false;
1.242 - }
1.243 -
1.244 - // Get output dword-aligned.
1.245 - while(((unsigned long)pOut & 3) != 0 && nBitsLeft >= 8)
1.246 - {
1.247 -
1.248 - WriteUBitLong( *pOut, 8, false );
1.249 - ++pOut;
1.250 - nBitsLeft -= 8;
1.251 - }
1.252 -
1.253 - // check if we can use fast memcpy if m_iCurBit is byte aligned
1.254 - if ( (nBitsLeft >= 32) && (m_iCurBit & 7) == 0 )
1.255 - {
1.256 - int numbytes = (nBitsLeft >> 3);
1.257 - int numbits = numbytes << 3;
1.258 -
1.259 - // Bounds checking..
1.260 - // TODO: May not need this check anymore
1.261 - if((m_iCurBit+numbits) > m_nDataBits)
1.262 - {
1.263 - m_iCurBit = m_nDataBits;
1.264 - SetOverflowFlag();
1.265 - CallErrorHandler( BITBUFERROR_BUFFER_OVERRUN, GetDebugName() );
1.266 - return false;
1.267 - }
1.268 -
1.269 - Q_memcpy( m_pData+(m_iCurBit>>3), pOut, numbytes );
1.270 - pOut += numbytes;
1.271 - nBitsLeft -= numbits;
1.272 - m_iCurBit += numbits;
1.273 - }
1.274 -
1.275 - // Read dwords.
1.276 - while(nBitsLeft >= 32)
1.277 - {
1.278 - WriteUBitLong( *((unsigned long*)pOut), 32, false );
1.279 - pOut += sizeof(unsigned long);
1.280 - nBitsLeft -= 32;
1.281 - }
1.282 -
1.283 - // Read the remaining bytes.
1.284 - while(nBitsLeft >= 8)
1.285 - {
1.286 - WriteUBitLong( *pOut, 8, false );
1.287 - ++pOut;
1.288 - nBitsLeft -= 8;
1.289 - }
1.290 -
1.291 - // Read the remaining bits.
1.292 - if(nBitsLeft)
1.293 - {
1.294 - WriteUBitLong( *pOut, nBitsLeft, false );
1.295 - }
1.296 -
1.297 - return !IsOverflowed();
1.298 -}
1.299 -
1.300 -
1.301 -bool bf_write::WriteBitsFromBuffer( bf_read *pIn, int nBits )
1.302 -{
1.303 - // This could be optimized a little by
1.304 - while ( nBits > 32 )
1.305 - {
1.306 - WriteUBitLong( pIn->ReadUBitLong( 32 ), 32 );
1.307 - nBits -= 32;
1.308 - }
1.309 -
1.310 - WriteUBitLong( pIn->ReadUBitLong( nBits ), nBits );
1.311 - return !IsOverflowed() && !pIn->IsOverflowed();
1.312 -}
1.313 -
1.314 -
1.315 -void bf_write::WriteBitAngle( float fAngle, int numbits )
1.316 -{
1.317 - int d;
1.318 - unsigned int mask;
1.319 - unsigned int shift;
1.320 -
1.321 - shift = (1<<numbits);
1.322 - mask = shift - 1;
1.323 -
1.324 - d = (int)( (fAngle / 360.0) * shift );
1.325 - d &= mask;
1.326 -
1.327 - WriteUBitLong((unsigned int)d, numbits);
1.328 -}
1.329 -
1.330 -void bf_write::WriteBitCoord (const float f)
1.331 -{
1.332 -#if defined( BB_PROFILING )
1.333 - VPROF( "bf_write::WriteBitCoord" );
1.334 -#endif
1.335 - int signbit = (f <= -COORD_RESOLUTION);
1.336 - int intval = abs(static_cast<int>(f));
1.337 - int fractval = abs((int)(f*COORD_DENOMINATOR)) & (COORD_DENOMINATOR-1);
1.338 -
1.339 -
1.340 - // Send the bit flags that indicate whether we have an integer part and/or a fraction part.
1.341 - WriteOneBit( intval );
1.342 - WriteOneBit( fractval );
1.343 -
1.344 - if ( intval || fractval )
1.345 - {
1.346 - // Send the sign bit
1.347 - WriteOneBit( signbit );
1.348 -
1.349 - // Send the integer if we have one.
1.350 - if ( intval )
1.351 - {
1.352 - // Adjust the integers from [1..MAX_COORD_VALUE] to [0..MAX_COORD_VALUE-1]
1.353 - intval--;
1.354 - WriteUBitLong( (unsigned int)intval, COORD_INTEGER_BITS );
1.355 - }
1.356 -
1.357 - // Send the fraction if we have one
1.358 - if ( fractval )
1.359 - {
1.360 - WriteUBitLong( (unsigned int)fractval, COORD_FRACTIONAL_BITS );
1.361 - }
1.362 - }
1.363 -}
1.364 -
1.365 -void bf_write::WriteBitFloat(float val)
1.366 -{
1.367 - long intVal;
1.368 -
1.369 - Assert(sizeof(long) == sizeof(float));
1.370 - Assert(sizeof(float) == 4);
1.371 -
1.372 - void *v = &val;
1.373 - intVal = *reinterpret_cast<long *>(v);
1.374 - WriteUBitLong( intVal, 32 );
1.375 -}
1.376 -
1.377 -void bf_write::WriteBitVec3Coord( const Vector& fa )
1.378 -{
1.379 - int xflag, yflag, zflag;
1.380 -
1.381 - xflag = (fa[0] >= COORD_RESOLUTION) || (fa[0] <= -COORD_RESOLUTION);
1.382 - yflag = (fa[1] >= COORD_RESOLUTION) || (fa[1] <= -COORD_RESOLUTION);
1.383 - zflag = (fa[2] >= COORD_RESOLUTION) || (fa[2] <= -COORD_RESOLUTION);
1.384 -
1.385 - WriteOneBit( xflag );
1.386 - WriteOneBit( yflag );
1.387 - WriteOneBit( zflag );
1.388 -
1.389 - if ( xflag )
1.390 - WriteBitCoord( fa[0] );
1.391 - if ( yflag )
1.392 - WriteBitCoord( fa[1] );
1.393 - if ( zflag )
1.394 - WriteBitCoord( fa[2] );
1.395 -}
1.396 -
1.397 -void bf_write::WriteBitNormal( float f )
1.398 -{
1.399 - int signbit = (f <= -NORMAL_RESOLUTION);
1.400 -
1.401 - // NOTE: Since +/-1 are valid values for a normal, I'm going to encode that as all ones
1.402 - unsigned int fractval = abs( (int)(f*NORMAL_DENOMINATOR) );
1.403 -
1.404 - // clamp..
1.405 - if (fractval > NORMAL_DENOMINATOR)
1.406 - fractval = NORMAL_DENOMINATOR;
1.407 -
1.408 - // Send the sign bit
1.409 - WriteOneBit( signbit );
1.410 -
1.411 - // Send the fractional component
1.412 - WriteUBitLong( fractval, NORMAL_FRACTIONAL_BITS );
1.413 -}
1.414 -
1.415 -void bf_write::WriteBitVec3Normal( const Vector& fa )
1.416 -{
1.417 - int xflag, yflag;
1.418 -
1.419 - xflag = (fa[0] >= NORMAL_RESOLUTION) || (fa[0] <= -NORMAL_RESOLUTION);
1.420 - yflag = (fa[1] >= NORMAL_RESOLUTION) || (fa[1] <= -NORMAL_RESOLUTION);
1.421 -
1.422 - WriteOneBit( xflag );
1.423 - WriteOneBit( yflag );
1.424 -
1.425 - if ( xflag )
1.426 - WriteBitNormal( fa[0] );
1.427 - if ( yflag )
1.428 - WriteBitNormal( fa[1] );
1.429 -
1.430 - // Write z sign bit
1.431 - int signbit = (fa[2] <= -NORMAL_RESOLUTION);
1.432 - WriteOneBit( signbit );
1.433 -}
1.434 -
1.435 -void bf_write::WriteBitAngles( const QAngle& fa )
1.436 -{
1.437 - // FIXME:
1.438 - Vector tmp( fa.x, fa.y, fa.z );
1.439 - WriteBitVec3Coord( tmp );
1.440 -}
1.441 -
1.442 -void bf_write::WriteChar(int val)
1.443 -{
1.444 - WriteSBitLong(val, sizeof(char) << 3);
1.445 -}
1.446 -
1.447 -void bf_write::WriteByte(int val)
1.448 -{
1.449 - WriteUBitLong(val, sizeof(unsigned char) << 3);
1.450 -}
1.451 -
1.452 -void bf_write::WriteShort(int val)
1.453 -{
1.454 - WriteSBitLong(val, sizeof(short) << 3);
1.455 -}
1.456 -
1.457 -void bf_write::WriteWord(int val)
1.458 -{
1.459 - WriteUBitLong(val, sizeof(unsigned short) << 3);
1.460 -}
1.461 -
1.462 -void bf_write::WriteLong(long val)
1.463 -{
1.464 - WriteSBitLong(val, sizeof(long) << 3);
1.465 -}
1.466 -
1.467 -void bf_write::WriteFloat(float val)
1.468 -{
1.469 - WriteBits(&val, sizeof(val) << 3);
1.470 -}
1.471 -
1.472 -bool bf_write::WriteBytes( const void *pBuf, int nBytes )
1.473 -{
1.474 - return WriteBits(pBuf, nBytes << 3);
1.475 -}
1.476 -
1.477 -bool bf_write::WriteString(const char *pStr)
1.478 -{
1.479 - if(pStr)
1.480 - {
1.481 - do
1.482 - {
1.483 - WriteChar( *pStr );
1.484 - ++pStr;
1.485 - } while( *(pStr-1) != 0 );
1.486 - }
1.487 - else
1.488 - {
1.489 - WriteChar( 0 );
1.490 - }
1.491 -
1.492 - return !IsOverflowed();
1.493 -}
1.494 -
1.495 -// ---------------------------------------------------------------------------------------- //
1.496 -// bf_read
1.497 -// ---------------------------------------------------------------------------------------- //
1.498 -
1.499 -bf_read::bf_read()
1.500 -{
1.501 - m_pData = NULL;
1.502 - m_nDataBytes = 0;
1.503 - m_nDataBits = -1; // set to -1 so we overflow on any operation
1.504 - m_iCurBit = 0;
1.505 - m_bOverflow = false;
1.506 - m_bAssertOnOverflow = true;
1.507 - m_pDebugName = NULL;
1.508 -}
1.509 -
1.510 -bf_read::bf_read( const void *pData, int nBytes, int nBits )
1.511 -{
1.512 - m_bAssertOnOverflow = true;
1.513 - StartReading( pData, nBytes, 0, nBits );
1.514 -}
1.515 -
1.516 -bf_read::bf_read( const char *pDebugName, const void *pData, int nBytes, int nBits )
1.517 -{
1.518 - m_bAssertOnOverflow = true;
1.519 - m_pDebugName = pDebugName;
1.520 - StartReading( pData, nBytes, 0, nBits );
1.521 -}
1.522 -
1.523 -void bf_read::StartReading( const void *pData, int nBytes, int iStartBit, int nBits )
1.524 -{
1.525 - // Make sure we're dword aligned.
1.526 - Assert(((unsigned long)pData & 3) == 0);
1.527 -
1.528 - m_pData = (unsigned char*)pData;
1.529 - m_nDataBytes = nBytes;
1.530 -
1.531 - if ( nBits == -1 )
1.532 - {
1.533 - m_nDataBits = m_nDataBytes << 3;
1.534 - }
1.535 - else
1.536 - {
1.537 - Assert( nBits <= nBytes*8 );
1.538 - m_nDataBits = nBits;
1.539 - }
1.540 -
1.541 - m_iCurBit = iStartBit;
1.542 - m_bOverflow = false;
1.543 -}
1.544 -
1.545 -void bf_read::Reset()
1.546 -{
1.547 - m_iCurBit = 0;
1.548 - m_bOverflow = false;
1.549 -}
1.550 -
1.551 -void bf_read::SetAssertOnOverflow( bool bAssert )
1.552 -{
1.553 - m_bAssertOnOverflow = bAssert;
1.554 -}
1.555 -
1.556 -const char* bf_read::GetDebugName()
1.557 -{
1.558 - return m_pDebugName;
1.559 -}
1.560 -
1.561 -void bf_read::SetDebugName( const char *pName )
1.562 -{
1.563 - m_pDebugName = pName;
1.564 -}
1.565 -
1.566 -unsigned int bf_read::CheckReadUBitLong(int numbits)
1.567 -{
1.568 - // Ok, just read bits out.
1.569 - int i, nBitValue;
1.570 - unsigned int r = 0;
1.571 -
1.572 - for(i=0; i < numbits; i++)
1.573 - {
1.574 - nBitValue = ReadOneBitNoCheck();
1.575 - r |= nBitValue << i;
1.576 - }
1.577 - m_iCurBit -= numbits;
1.578 -
1.579 - return r;
1.580 -}
1.581 -
1.582 -bool bf_read::ReadBits(void *pOutData, int nBits)
1.583 -{
1.584 -#if defined( BB_PROFILING )
1.585 - VPROF( "bf_write::ReadBits" );
1.586 -#endif
1.587 -
1.588 - unsigned char *pOut = (unsigned char*)pOutData;
1.589 - int nBitsLeft = nBits;
1.590 -
1.591 -
1.592 - // Get output dword-aligned.
1.593 - while(((unsigned long)pOut & 3) != 0 && nBitsLeft >= 8)
1.594 - {
1.595 - *pOut = (unsigned char)ReadUBitLong(8);
1.596 - ++pOut;
1.597 - nBitsLeft -= 8;
1.598 - }
1.599 -
1.600 - // Read dwords.
1.601 - while(nBitsLeft >= 32)
1.602 - {
1.603 - *((unsigned long*)pOut) = ReadUBitLong(32);
1.604 - pOut += sizeof(unsigned long);
1.605 - nBitsLeft -= 32;
1.606 - }
1.607 -
1.608 - // Read the remaining bytes.
1.609 - while(nBitsLeft >= 8)
1.610 - {
1.611 - *pOut = ReadUBitLong(8);
1.612 - ++pOut;
1.613 - nBitsLeft -= 8;
1.614 - }
1.615 -
1.616 - // Read the remaining bits.
1.617 - if(nBitsLeft)
1.618 - {
1.619 - *pOut = ReadUBitLong(nBitsLeft);
1.620 - }
1.621 -
1.622 - return !IsOverflowed();
1.623 -}
1.624 -
1.625 -float bf_read::ReadBitAngle( int numbits )
1.626 -{
1.627 - float fReturn;
1.628 - int i;
1.629 - float shift;
1.630 -
1.631 - shift = (float)( 1 << numbits );
1.632 -
1.633 - i = ReadUBitLong( numbits );
1.634 - fReturn = (float)i * (360.0 / shift);
1.635 -
1.636 - return fReturn;
1.637 -}
1.638 -
1.639 -unsigned int bf_read::PeekUBitLong( int numbits )
1.640 -{
1.641 - unsigned int r;
1.642 - int i, nBitValue;
1.643 -#ifdef BIT_VERBOSE
1.644 - int nShifts = numbits;
1.645 -#endif
1.646 -
1.647 - bf_read savebf;
1.648 -
1.649 - savebf = *this; // Save current state info
1.650 -
1.651 - r = 0;
1.652 - for(i=0; i < numbits; i++)
1.653 - {
1.654 - nBitValue = ReadOneBit();
1.655 -
1.656 - // Append to current stream
1.657 - if ( nBitValue )
1.658 - {
1.659 - r |= 1 << i;
1.660 - }
1.661 - }
1.662 -
1.663 - *this = savebf;
1.664 -
1.665 -#ifdef BIT_VERBOSE
1.666 - Con_Printf( "PeekBitLong: %i %i\n", nShifts, (unsigned int)r );
1.667 -#endif
1.668 -
1.669 - return r;
1.670 -}
1.671 -
1.672 -// Append numbits least significant bits from data to the current bit stream
1.673 -int bf_read::ReadSBitLong( int numbits )
1.674 -{
1.675 - int r, sign;
1.676 -
1.677 - r = ReadUBitLong(numbits - 1);
1.678 -
1.679 - // Note: it does this wierdness here so it's bit-compatible with regular integer data in the buffer.
1.680 - // (Some old code writes direct integers right into the buffer).
1.681 - sign = ReadOneBit();
1.682 - if(sign)
1.683 - r = -((1 << (numbits-1)) - r);
1.684 -
1.685 - return r;
1.686 -}
1.687 -
1.688 -unsigned int bf_read::ReadUBitVar()
1.689 -{
1.690 - int bits = 0; // how many bits are used to encode delta offset
1.691 -
1.692 - // how many bits do we use
1.693 - while ( ReadOneBit() == 0 )
1.694 - bits++;
1.695 -
1.696 - unsigned int data = (1<<bits)-1;
1.697 -
1.698 - // read the value
1.699 - if ( bits > 0)
1.700 - data += ReadUBitLong( bits );
1.701 -
1.702 - return data;
1.703 -}
1.704 -
1.705 -
1.706 -unsigned int bf_read::ReadBitLong(int numbits, bool bSigned)
1.707 -{
1.708 - if(bSigned)
1.709 - return (unsigned int)ReadSBitLong(numbits);
1.710 - else
1.711 - return ReadUBitLong(numbits);
1.712 -}
1.713 -
1.714 -
1.715 -// Basic Coordinate Routines (these contain bit-field size AND fixed point scaling constants)
1.716 -float bf_read::ReadBitCoord (void)
1.717 -{
1.718 -#if defined( BB_PROFILING )
1.719 - VPROF( "bf_write::ReadBitCoord" );
1.720 -#endif
1.721 - int intval=0,fractval=0,signbit=0;
1.722 - float value = 0.0;
1.723 -
1.724 -
1.725 - // Read the required integer and fraction flags
1.726 - intval = ReadOneBit();
1.727 - fractval = ReadOneBit();
1.728 -
1.729 - // If we got either parse them, otherwise it's a zero.
1.730 - if ( intval || fractval )
1.731 - {
1.732 - // Read the sign bit
1.733 - signbit = ReadOneBit();
1.734 -
1.735 - // If there's an integer, read it in
1.736 - if ( intval )
1.737 - {
1.738 - // Adjust the integers from [0..MAX_COORD_VALUE-1] to [1..MAX_COORD_VALUE]
1.739 - intval = ReadUBitLong( COORD_INTEGER_BITS ) + 1;
1.740 - }
1.741 -
1.742 - // If there's a fraction, read it in
1.743 - if ( fractval )
1.744 - {
1.745 - fractval = ReadUBitLong( COORD_FRACTIONAL_BITS );
1.746 - }
1.747 -
1.748 - // Calculate the correct floating point value
1.749 - value = intval + ((float)fractval * COORD_RESOLUTION);
1.750 -
1.751 - // Fixup the sign if negative.
1.752 - if ( signbit )
1.753 - value = -value;
1.754 - }
1.755 -
1.756 - return value;
1.757 -}
1.758 -
1.759 -void bf_read::ReadBitVec3Coord( Vector& fa )
1.760 -{
1.761 - int xflag, yflag, zflag;
1.762 -
1.763 - // This vector must be initialized! Otherwise, If any of the flags aren't set,
1.764 - // the corresponding component will not be read and will be stack garbage.
1.765 - fa.Init( 0, 0, 0 );
1.766 -
1.767 - xflag = ReadOneBit();
1.768 - yflag = ReadOneBit();
1.769 - zflag = ReadOneBit();
1.770 -
1.771 - if ( xflag )
1.772 - fa[0] = ReadBitCoord();
1.773 - if ( yflag )
1.774 - fa[1] = ReadBitCoord();
1.775 - if ( zflag )
1.776 - fa[2] = ReadBitCoord();
1.777 -}
1.778 -
1.779 -float bf_read::ReadBitNormal (void)
1.780 -{
1.781 - // Read the sign bit
1.782 - int signbit = ReadOneBit();
1.783 -
1.784 - // Read the fractional part
1.785 - unsigned int fractval = ReadUBitLong( NORMAL_FRACTIONAL_BITS );
1.786 -
1.787 - // Calculate the correct floating point value
1.788 - float value = (float)fractval * NORMAL_RESOLUTION;
1.789 -
1.790 - // Fixup the sign if negative.
1.791 - if ( signbit )
1.792 - value = -value;
1.793 -
1.794 - return value;
1.795 -}
1.796 -
1.797 -void bf_read::ReadBitVec3Normal( Vector& fa )
1.798 -{
1.799 - int xflag = ReadOneBit();
1.800 - int yflag = ReadOneBit();
1.801 -
1.802 - if (xflag)
1.803 - fa[0] = ReadBitNormal();
1.804 - else
1.805 - fa[0] = 0.0f;
1.806 -
1.807 - if (yflag)
1.808 - fa[1] = ReadBitNormal();
1.809 - else
1.810 - fa[1] = 0.0f;
1.811 -
1.812 - // The first two imply the third (but not its sign)
1.813 - int znegative = ReadOneBit();
1.814 -
1.815 - float fafafbfb = fa[0] * fa[0] + fa[1] * fa[1];
1.816 - if (fafafbfb < 1.0f)
1.817 - fa[2] = sqrt( 1.0f - fafafbfb );
1.818 - else
1.819 - fa[2] = 0.0f;
1.820 -
1.821 - if (znegative)
1.822 - fa[2] = -fa[2];
1.823 -}
1.824 -
1.825 -void bf_read::ReadBitAngles( QAngle& fa )
1.826 -{
1.827 - Vector tmp;
1.828 - ReadBitVec3Coord( tmp );
1.829 - fa.Init( tmp.x, tmp.y, tmp.z );
1.830 -}
1.831 -
1.832 -int bf_read::ReadChar()
1.833 -{
1.834 - return ReadSBitLong(sizeof(char) << 3);
1.835 -}
1.836 -
1.837 -int bf_read::ReadByte()
1.838 -{
1.839 - return ReadUBitLong(sizeof(unsigned char) << 3);
1.840 -}
1.841 -
1.842 -int bf_read::ReadShort()
1.843 -{
1.844 - return ReadSBitLong(sizeof(short) << 3);
1.845 -}
1.846 -
1.847 -int bf_read::ReadWord()
1.848 -{
1.849 - return ReadUBitLong(sizeof(unsigned short) << 3);
1.850 -}
1.851 -
1.852 -long bf_read::ReadLong()
1.853 -{
1.854 - return ReadSBitLong(sizeof(long) << 3);
1.855 -}
1.856 -
1.857 -float bf_read::ReadFloat()
1.858 -{
1.859 - float ret;
1.860 - Assert( sizeof(ret) == 4 );
1.861 - ReadBits(&ret, 32);
1.862 - return ret;
1.863 -}
1.864 -
1.865 -bool bf_read::ReadBytes(void *pOut, int nBytes)
1.866 -{
1.867 - return ReadBits(pOut, nBytes << 3);
1.868 -}
1.869 -
1.870 -bool bf_read::ReadString( char *pStr, int maxLen, bool bLine, int *pOutNumChars )
1.871 -{
1.872 - Assert( maxLen != 0 );
1.873 -
1.874 - bool bTooSmall = false;
1.875 - int iChar = 0;
1.876 - while(1)
1.877 - {
1.878 - char val = ReadChar();
1.879 - if ( val == 0 )
1.880 - break;
1.881 - else if ( bLine && val == '\n' )
1.882 - break;
1.883 -
1.884 - if ( iChar < (maxLen-1) )
1.885 - {
1.886 - pStr[iChar] = val;
1.887 - ++iChar;
1.888 - }
1.889 - else
1.890 - {
1.891 - bTooSmall = true;
1.892 - }
1.893 - }
1.894 -
1.895 - // Make sure it's null-terminated.
1.896 - Assert( iChar < maxLen );
1.897 - pStr[iChar] = 0;
1.898 -
1.899 - if ( pOutNumChars )
1.900 - *pOutNumChars = iChar;
1.901 -
1.902 - return !IsOverflowed() && !bTooSmall;
1.903 -}
1.904 -
1.905 -
1.906 -char* bf_read::ReadAndAllocateString( bool *pOverflow )
1.907 -{
1.908 - char str[2048];
1.909 -
1.910 - int nChars;
1.911 - bool bOverflow = !ReadString( str, sizeof( str ), false, &nChars );
1.912 - if ( pOverflow )
1.913 - *pOverflow = bOverflow;
1.914 -
1.915 - // Now copy into the output and return it;
1.916 - char *pRet = new char[ nChars + 1 ];
1.917 - for ( int i=0; i <= nChars; i++ )
1.918 - pRet[i] = str[i];
1.919 -
1.920 - return pRet;
1.921 -}
1.922 -
1.923 -
1.924 -bool bf_read::Seek(int iBit)
1.925 -{
1.926 - if(iBit < 0)
1.927 - {
1.928 - SetOverflowFlag();
1.929 - m_iCurBit = m_nDataBits;
1.930 - return false;
1.931 - }
1.932 - else if(iBit > m_nDataBits)
1.933 - {
1.934 - SetOverflowFlag();
1.935 - m_iCurBit = m_nDataBits;
1.936 - return false;
1.937 - }
1.938 - else
1.939 - {
1.940 - m_iCurBit = iBit;
1.941 - return true;
1.942 - }
1.943 -}
1.944 -
1.945 -void bf_read::ExciseBits( int startbit, int bitstoremove )
1.946 -{
1.947 - int endbit = startbit + bitstoremove;
1.948 - int remaining_to_end = m_nDataBits - endbit;
1.949 -
1.950 - bf_write temp;
1.951 - temp.StartWriting( (void *)m_pData, m_nDataBits << 3, startbit );
1.952 -
1.953 - Seek( endbit );
1.954 -
1.955 - for ( int i = 0; i < remaining_to_end; i++ )
1.956 - {
1.957 - temp.WriteOneBit( ReadOneBit() );
1.958 - }
1.959 -
1.960 - Seek( startbit );
1.961 -
1.962 - m_nDataBits -= bitstoremove;
1.963 - m_nDataBytes = m_nDataBits >> 3;
1.964 -}