core/bitbuf.cpp
author David Anderson <dvander@alliedmods.net>
Tue Dec 04 15:58:07 2007 +0000 (2007-12-04)
changeset 1764 e4f24e9789fd
parent 1541 e46ad208b9b4
permissions -rw-r--r--
- removed ancient bcompat shim for translations/core.cfg file
- fixed msvc8 whining about BfReadString
     1 //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
     2 //
     3 // Purpose: 
     4 //
     5 // $NoKeywords: $
     6 //
     7 //=============================================================================//
     8 
     9 #include "bitbuf.h"
    10 #include "coordsize.h"
    11 #include "vector.h"
    12 #include "mathlib/math_base.h"
    13 #include "vstdlib/strtools.h"
    14 
    15 
    16 // FIXME: Can't use this until we get multithreaded allocations in tier0 working for tools
    17 // This is used by VVIS and fails to link
    18 // NOTE: This must be the last file included!!!
    19 //#include "tier0/memdbgon.h"
    20 
    21 #ifdef _XBOX
    22 // mandatory ... wary of above comment and isolating, tier0 is built as MT though
    23 #include "tier0/memdbgon.h"
    24 #endif
    25 
    26 static BitBufErrorHandler g_BitBufErrorHandler = 0;
    27 
    28 
    29 void InternalBitBufErrorHandler( BitBufErrorType errorType, const char *pDebugName )
    30 {
    31 	if ( g_BitBufErrorHandler )
    32 		g_BitBufErrorHandler( errorType, pDebugName );
    33 }
    34 
    35 
    36 void SetBitBufErrorHandler( BitBufErrorHandler fn )
    37 {
    38 	g_BitBufErrorHandler = fn;
    39 }
    40 
    41 
    42 // #define BB_PROFILING
    43 
    44 
    45 // Precalculated bit masks for WriteUBitLong. Using these tables instead of 
    46 // doing the calculations gives a 33% speedup in WriteUBitLong.
    47 unsigned long g_BitWriteMasks[32][33];
    48 
    49 // (1 << i) - 1
    50 unsigned long g_ExtraMasks[32];
    51 
    52 class CBitWriteMasksInit
    53 {
    54 public:
    55 	CBitWriteMasksInit()
    56 	{
    57 		for( unsigned int startbit=0; startbit < 32; startbit++ )
    58 		{
    59 			for( unsigned int nBitsLeft=0; nBitsLeft < 33; nBitsLeft++ )
    60 			{
    61 				unsigned int endbit = startbit + nBitsLeft;
    62 				g_BitWriteMasks[startbit][nBitsLeft] = (1 << startbit) - 1;
    63 				if(endbit < 32)
    64 					g_BitWriteMasks[startbit][nBitsLeft] |= ~((1 << endbit) - 1);
    65 			}
    66 		}
    67 
    68 		for ( unsigned int maskBit=0; maskBit < 32; maskBit++ )
    69 			g_ExtraMasks[maskBit] = (1 << maskBit) - 1;
    70 	}
    71 };
    72 CBitWriteMasksInit g_BitWriteMasksInit;
    73 
    74 
    75 // ---------------------------------------------------------------------------------------- //
    76 // bf_write
    77 // ---------------------------------------------------------------------------------------- //
    78 
    79 bf_write::bf_write()
    80 {
    81 	m_pData = NULL;
    82 	m_nDataBytes = 0;
    83 	m_nDataBits = -1; // set to -1 so we generate overflow on any operation
    84 	m_iCurBit = 0;
    85 	m_bOverflow = false;
    86 	m_bAssertOnOverflow = true;
    87 	m_pDebugName = NULL;
    88 }
    89 
    90 bf_write::bf_write( const char *pDebugName, void *pData, int nBytes, int nBits )
    91 {
    92 	m_bAssertOnOverflow = true;
    93 	m_pDebugName = pDebugName;
    94 	StartWriting( pData, nBytes, 0, nBits );
    95 }
    96 
    97 bf_write::bf_write( void *pData, int nBytes, int nBits )
    98 {
    99 	m_bAssertOnOverflow = true;
   100 	StartWriting( pData, nBytes, 0, nBits );
   101 }
   102 
   103 void bf_write::StartWriting( void *pData, int nBytes, int iStartBit, int nBits )
   104 {
   105 	// Make sure it's dword aligned and padded.
   106 	Assert( (nBytes % 4) == 0 );
   107 	Assert(((unsigned long)pData & 3) == 0);
   108 
   109 	m_pData = (unsigned char*)pData;
   110 	m_nDataBytes = nBytes;
   111 
   112 	if ( nBits == -1 )
   113 	{
   114 		m_nDataBits = nBytes << 3;
   115 	}
   116 	else
   117 	{
   118 		Assert( nBits <= nBytes*8 );
   119 		m_nDataBits = nBits;
   120 	}
   121 
   122 	m_iCurBit = iStartBit;
   123 	m_bOverflow = false;
   124 }
   125 
   126 void bf_write::Reset()
   127 {
   128 	m_iCurBit = 0;
   129 	m_bOverflow = false;
   130 }
   131 
   132 
   133 void bf_write::SetAssertOnOverflow( bool bAssert )
   134 {
   135 	m_bAssertOnOverflow = bAssert;
   136 }
   137 
   138 
   139 const char* bf_write::GetDebugName()
   140 {
   141 	return m_pDebugName;
   142 }
   143 
   144 
   145 void bf_write::SetDebugName( const char *pDebugName )
   146 {
   147 	m_pDebugName = pDebugName;
   148 }
   149 
   150 
   151 void bf_write::SeekToBit( int bitPos )
   152 {
   153 	m_iCurBit = bitPos;
   154 }
   155 
   156 
   157 // Sign bit comes first
   158 void bf_write::WriteSBitLong( int data, int numbits )
   159 {
   160 	// Do we have a valid # of bits to encode with?
   161 	Assert( numbits >= 1 );
   162 
   163 	// Note: it does this wierdness here so it's bit-compatible with regular integer data in the buffer.
   164 	// (Some old code writes direct integers right into the buffer).
   165 	if(data < 0)
   166 	{
   167 #ifdef _DEBUG
   168 	if( numbits < 32 )
   169 	{
   170 		// Make sure it doesn't overflow.
   171 
   172 		if( data < 0 )
   173 		{
   174 			Assert( data >= -(1 << (numbits-1)) );
   175 		}
   176 		else
   177 		{
   178 			Assert( data < (1 << (numbits-1)) );
   179 		}
   180 	}
   181 #endif
   182 
   183 		WriteUBitLong( (unsigned int)(0x80000000 + data), numbits - 1, false );
   184 		WriteOneBit( 1 );
   185 	}
   186 	else
   187 	{
   188 		WriteUBitLong((unsigned int)data, numbits - 1);
   189 		WriteOneBit( 0 );
   190 	}
   191 }
   192 
   193 // writes an unsigned integer with variable bit length
   194 void bf_write::WriteUBitVar( unsigned int data )
   195 {
   196 	unsigned int bits = 0;
   197 	unsigned int base = 0;
   198 
   199 	while (data > (base<<1))
   200 	{
   201 		bits++;
   202 		base = (1<<bits)-1;
   203 	}
   204 
   205 	// how many bits do we use
   206 	if ( bits > 0)
   207 		WriteUBitLong( 0, bits );
   208 
   209 	// end marker
   210 	WriteOneBit( 1 );  
   211 
   212 	// write the value
   213 	if ( bits > 0)
   214 		WriteUBitLong( data - base , bits );
   215 }
   216 
   217 void bf_write::WriteBitLong(unsigned int data, int numbits, bool bSigned)
   218 {
   219 	if(bSigned)
   220 		WriteSBitLong((int)data, numbits);
   221 	else
   222 		WriteUBitLong(data, numbits);
   223 }
   224 
   225 bool bf_write::WriteBits(const void *pInData, int nBits)
   226 {
   227 #if defined( BB_PROFILING )
   228 	VPROF( "bf_write::WriteBits" );
   229 #endif
   230 
   231 	unsigned char *pOut = (unsigned char*)pInData;
   232 	int nBitsLeft = nBits;
   233 
   234 	if((m_iCurBit+nBits) > m_nDataBits)
   235 	{
   236 		SetOverflowFlag();
   237 		CallErrorHandler( BITBUFERROR_BUFFER_OVERRUN, GetDebugName() );
   238 		return false;
   239 	}
   240 
   241 	// Get output dword-aligned.
   242 	while(((unsigned long)pOut & 3) != 0 && nBitsLeft >= 8)
   243 	{
   244 
   245 		WriteUBitLong( *pOut, 8, false );
   246 		++pOut;
   247 		nBitsLeft -= 8;
   248 	}
   249 	
   250 	// check if we can use fast memcpy if m_iCurBit is byte aligned
   251 	if ( (nBitsLeft >= 32) && (m_iCurBit & 7) == 0 )
   252 	{
   253 		int numbytes = (nBitsLeft >> 3); 
   254 		int numbits = numbytes << 3;
   255 		
   256 		// Bounds checking..
   257 		// TODO: May not need this check anymore
   258 		if((m_iCurBit+numbits) > m_nDataBits)
   259 		{
   260 			m_iCurBit = m_nDataBits;
   261 			SetOverflowFlag();
   262 			CallErrorHandler( BITBUFERROR_BUFFER_OVERRUN, GetDebugName() );
   263 			return false;
   264 		}
   265 		
   266 		Q_memcpy( m_pData+(m_iCurBit>>3), pOut, numbytes );
   267 		pOut += numbytes;
   268 		nBitsLeft -= numbits;
   269 		m_iCurBit += numbits;
   270 	}
   271 
   272 	// Read dwords.
   273 	while(nBitsLeft >= 32)
   274 	{
   275 		WriteUBitLong( *((unsigned long*)pOut), 32, false );
   276 		pOut += sizeof(unsigned long);
   277 		nBitsLeft -= 32;
   278 	}
   279 
   280 	// Read the remaining bytes.
   281 	while(nBitsLeft >= 8)
   282 	{
   283 		WriteUBitLong( *pOut, 8, false );
   284 		++pOut;
   285 		nBitsLeft -= 8;
   286 	}
   287 	
   288 	// Read the remaining bits.
   289 	if(nBitsLeft)
   290 	{
   291 		WriteUBitLong( *pOut, nBitsLeft, false );
   292 	}
   293 
   294 	return !IsOverflowed();
   295 }
   296 
   297 
   298 bool bf_write::WriteBitsFromBuffer( bf_read *pIn, int nBits )
   299 {
   300 	// This could be optimized a little by
   301 	while ( nBits > 32 )
   302 	{
   303 		WriteUBitLong( pIn->ReadUBitLong( 32 ), 32 );
   304 		nBits -= 32;
   305 	}
   306 
   307 	WriteUBitLong( pIn->ReadUBitLong( nBits ), nBits );
   308 	return !IsOverflowed() && !pIn->IsOverflowed();
   309 }
   310 
   311 
   312 void bf_write::WriteBitAngle( float fAngle, int numbits )
   313 {
   314 	int d;
   315 	unsigned int mask;
   316 	unsigned int shift;
   317 
   318 	shift = (1<<numbits);
   319 	mask = shift - 1;
   320 
   321 	d = (int)( (fAngle / 360.0) * shift );
   322 	d &= mask;
   323 
   324 	WriteUBitLong((unsigned int)d, numbits);
   325 }
   326 
   327 void bf_write::WriteBitCoord (const float f)
   328 {
   329 #if defined( BB_PROFILING )
   330 	VPROF( "bf_write::WriteBitCoord" );
   331 #endif
   332 	int		signbit = (f <= -COORD_RESOLUTION);
   333 	int		intval = abs(static_cast<int>(f));
   334 	int		fractval = abs((int)(f*COORD_DENOMINATOR)) & (COORD_DENOMINATOR-1);
   335 
   336 
   337 	// Send the bit flags that indicate whether we have an integer part and/or a fraction part.
   338 	WriteOneBit( intval );
   339 	WriteOneBit( fractval );
   340 
   341 	if ( intval || fractval )
   342 	{
   343 		// Send the sign bit
   344 		WriteOneBit( signbit );
   345 
   346 		// Send the integer if we have one.
   347 		if ( intval )
   348 		{
   349 			// Adjust the integers from [1..MAX_COORD_VALUE] to [0..MAX_COORD_VALUE-1]
   350 			intval--;
   351 			WriteUBitLong( (unsigned int)intval, COORD_INTEGER_BITS );
   352 		}
   353 		
   354 		// Send the fraction if we have one
   355 		if ( fractval )
   356 		{
   357 			WriteUBitLong( (unsigned int)fractval, COORD_FRACTIONAL_BITS );
   358 		}
   359 	}
   360 }
   361 
   362 void bf_write::WriteBitFloat(float val)
   363 {
   364 	long intVal;
   365 
   366 	Assert(sizeof(long) == sizeof(float));
   367 	Assert(sizeof(float) == 4);
   368 
   369 	void *v = &val;
   370 	intVal = *reinterpret_cast<long *>(v);
   371 	WriteUBitLong( intVal, 32 );
   372 }
   373 
   374 void bf_write::WriteBitVec3Coord( const Vector& fa )
   375 {
   376 	int		xflag, yflag, zflag;
   377 
   378 	xflag = (fa[0] >= COORD_RESOLUTION) || (fa[0] <= -COORD_RESOLUTION);
   379 	yflag = (fa[1] >= COORD_RESOLUTION) || (fa[1] <= -COORD_RESOLUTION);
   380 	zflag = (fa[2] >= COORD_RESOLUTION) || (fa[2] <= -COORD_RESOLUTION);
   381 
   382 	WriteOneBit( xflag );
   383 	WriteOneBit( yflag );
   384 	WriteOneBit( zflag );
   385 
   386 	if ( xflag )
   387 		WriteBitCoord( fa[0] );
   388 	if ( yflag )
   389 		WriteBitCoord( fa[1] );
   390 	if ( zflag )
   391 		WriteBitCoord( fa[2] );
   392 }
   393 
   394 void bf_write::WriteBitNormal( float f )
   395 {
   396 	int	signbit = (f <= -NORMAL_RESOLUTION);
   397 
   398 	// NOTE: Since +/-1 are valid values for a normal, I'm going to encode that as all ones
   399 	unsigned int fractval = abs( (int)(f*NORMAL_DENOMINATOR) );
   400 
   401 	// clamp..
   402 	if (fractval > NORMAL_DENOMINATOR)
   403 		fractval = NORMAL_DENOMINATOR;
   404 
   405 	// Send the sign bit
   406 	WriteOneBit( signbit );
   407 
   408 	// Send the fractional component
   409 	WriteUBitLong( fractval, NORMAL_FRACTIONAL_BITS );
   410 }
   411 
   412 void bf_write::WriteBitVec3Normal( const Vector& fa )
   413 {
   414 	int		xflag, yflag;
   415 
   416 	xflag = (fa[0] >= NORMAL_RESOLUTION) || (fa[0] <= -NORMAL_RESOLUTION);
   417 	yflag = (fa[1] >= NORMAL_RESOLUTION) || (fa[1] <= -NORMAL_RESOLUTION);
   418 
   419 	WriteOneBit( xflag );
   420 	WriteOneBit( yflag );
   421 
   422 	if ( xflag )
   423 		WriteBitNormal( fa[0] );
   424 	if ( yflag )
   425 		WriteBitNormal( fa[1] );
   426 	
   427 	// Write z sign bit
   428 	int	signbit = (fa[2] <= -NORMAL_RESOLUTION);
   429 	WriteOneBit( signbit );
   430 }
   431 
   432 void bf_write::WriteBitAngles( const QAngle& fa )
   433 {
   434 	// FIXME:
   435 	Vector tmp( fa.x, fa.y, fa.z );
   436 	WriteBitVec3Coord( tmp );
   437 }
   438 
   439 void bf_write::WriteChar(int val)
   440 {
   441 	WriteSBitLong(val, sizeof(char) << 3);
   442 }
   443 
   444 void bf_write::WriteByte(int val)
   445 {
   446 	WriteUBitLong(val, sizeof(unsigned char) << 3);
   447 }
   448 
   449 void bf_write::WriteShort(int val)
   450 {
   451 	WriteSBitLong(val, sizeof(short) << 3);
   452 }
   453 
   454 void bf_write::WriteWord(int val)
   455 {
   456 	WriteUBitLong(val, sizeof(unsigned short) << 3);
   457 }
   458 
   459 void bf_write::WriteLong(long val)
   460 {
   461 	WriteSBitLong(val, sizeof(long) << 3);
   462 }
   463 
   464 void bf_write::WriteFloat(float val)
   465 {
   466 	WriteBits(&val, sizeof(val) << 3);
   467 }
   468 
   469 bool bf_write::WriteBytes( const void *pBuf, int nBytes )
   470 {
   471 	return WriteBits(pBuf, nBytes << 3);
   472 }
   473 
   474 bool bf_write::WriteString(const char *pStr)
   475 {
   476 	if(pStr)
   477 	{
   478 		do
   479 		{
   480 			WriteChar( *pStr );
   481 			++pStr;
   482 		} while( *(pStr-1) != 0 );
   483 	}
   484 	else
   485 	{
   486 		WriteChar( 0 );
   487 	}
   488 
   489 	return !IsOverflowed();
   490 }
   491 
   492 // ---------------------------------------------------------------------------------------- //
   493 // bf_read
   494 // ---------------------------------------------------------------------------------------- //
   495 
   496 bf_read::bf_read()
   497 {
   498 	m_pData = NULL;
   499 	m_nDataBytes = 0;
   500 	m_nDataBits = -1; // set to -1 so we overflow on any operation
   501 	m_iCurBit = 0;
   502 	m_bOverflow = false;
   503 	m_bAssertOnOverflow = true;
   504 	m_pDebugName = NULL;
   505 }
   506 
   507 bf_read::bf_read( const void *pData, int nBytes, int nBits )
   508 {
   509 	m_bAssertOnOverflow = true;
   510 	StartReading( pData, nBytes, 0, nBits );
   511 }
   512 
   513 bf_read::bf_read( const char *pDebugName, const void *pData, int nBytes, int nBits )
   514 {
   515 	m_bAssertOnOverflow = true;
   516 	m_pDebugName = pDebugName;
   517 	StartReading( pData, nBytes, 0, nBits );
   518 }
   519 
   520 void bf_read::StartReading( const void *pData, int nBytes, int iStartBit, int nBits )
   521 {
   522 	// Make sure we're dword aligned.
   523 	Assert(((unsigned long)pData & 3) == 0);
   524 
   525 	m_pData = (unsigned char*)pData;
   526 	m_nDataBytes = nBytes;
   527 
   528 	if ( nBits == -1 )
   529 	{
   530 		m_nDataBits = m_nDataBytes << 3;
   531 	}
   532 	else
   533 	{
   534 		Assert( nBits <= nBytes*8 );
   535 		m_nDataBits = nBits;
   536 	}
   537 
   538 	m_iCurBit = iStartBit;
   539 	m_bOverflow = false;
   540 }
   541 
   542 void bf_read::Reset()
   543 {
   544 	m_iCurBit = 0;
   545 	m_bOverflow = false;
   546 }
   547 
   548 void bf_read::SetAssertOnOverflow( bool bAssert )
   549 {
   550 	m_bAssertOnOverflow = bAssert;
   551 }
   552 
   553 const char* bf_read::GetDebugName()
   554 {
   555 	return m_pDebugName;
   556 }
   557 
   558 void bf_read::SetDebugName( const char *pName )
   559 {
   560 	m_pDebugName = pName;
   561 }
   562 
   563 unsigned int bf_read::CheckReadUBitLong(int numbits)
   564 {
   565 	// Ok, just read bits out.
   566 	int i, nBitValue;
   567 	unsigned int r = 0;
   568 
   569 	for(i=0; i < numbits; i++)
   570 	{
   571 		nBitValue = ReadOneBitNoCheck();
   572 		r |= nBitValue << i;
   573 	}
   574 	m_iCurBit -= numbits;
   575 	
   576 	return r;
   577 }
   578 
   579 bool bf_read::ReadBits(void *pOutData, int nBits)
   580 {
   581 #if defined( BB_PROFILING )
   582 	VPROF( "bf_write::ReadBits" );
   583 #endif
   584 
   585 	unsigned char *pOut = (unsigned char*)pOutData;
   586 	int nBitsLeft = nBits;
   587 
   588 	
   589 	// Get output dword-aligned.
   590 	while(((unsigned long)pOut & 3) != 0 && nBitsLeft >= 8)
   591 	{
   592 		*pOut = (unsigned char)ReadUBitLong(8);
   593 		++pOut;
   594 		nBitsLeft -= 8;
   595 	}
   596 
   597 	// Read dwords.
   598 	while(nBitsLeft >= 32)
   599 	{
   600 		*((unsigned long*)pOut) = ReadUBitLong(32);
   601 		pOut += sizeof(unsigned long);
   602 		nBitsLeft -= 32;
   603 	}
   604 
   605 	// Read the remaining bytes.
   606 	while(nBitsLeft >= 8)
   607 	{
   608 		*pOut = ReadUBitLong(8);
   609 		++pOut;
   610 		nBitsLeft -= 8;
   611 	}
   612 	
   613 	// Read the remaining bits.
   614 	if(nBitsLeft)
   615 	{
   616 		*pOut = ReadUBitLong(nBitsLeft);
   617 	}
   618 
   619 	return !IsOverflowed();
   620 }
   621 
   622 float bf_read::ReadBitAngle( int numbits )
   623 {
   624 	float fReturn;
   625 	int i;
   626 	float shift;
   627 
   628 	shift = (float)( 1 << numbits );
   629 
   630 	i = ReadUBitLong( numbits );
   631 	fReturn = (float)i * (360.0 / shift);
   632 
   633 	return fReturn;
   634 }
   635 
   636 unsigned int bf_read::PeekUBitLong( int numbits )
   637 {
   638 	unsigned int r;
   639 	int i, nBitValue;
   640 #ifdef BIT_VERBOSE
   641 	int nShifts = numbits;
   642 #endif
   643 
   644 	bf_read savebf;
   645 
   646 	savebf = *this;  // Save current state info
   647 
   648 	r = 0;
   649 	for(i=0; i < numbits; i++)
   650 	{
   651 		nBitValue = ReadOneBit();
   652 
   653 		// Append to current stream
   654 		if ( nBitValue )
   655 		{
   656 			r |= 1 << i;
   657 		}
   658 	}
   659 	
   660 	*this = savebf;
   661 
   662 #ifdef BIT_VERBOSE
   663 	Con_Printf( "PeekBitLong:  %i %i\n", nShifts, (unsigned int)r );
   664 #endif
   665 
   666 	return r;
   667 }
   668 
   669 // Append numbits least significant bits from data to the current bit stream
   670 int bf_read::ReadSBitLong( int numbits )
   671 {
   672 	int r, sign;
   673 
   674 	r = ReadUBitLong(numbits - 1);
   675 
   676 	// Note: it does this wierdness here so it's bit-compatible with regular integer data in the buffer.
   677 	// (Some old code writes direct integers right into the buffer).
   678 	sign = ReadOneBit();
   679 	if(sign)
   680 		r = -((1 << (numbits-1)) - r);
   681 
   682 	return r;
   683 }
   684 
   685 unsigned int bf_read::ReadUBitVar()
   686 {
   687 	int bits = 0; // how many bits are used to encode delta offset
   688 
   689 		// how many bits do we use
   690 	while ( ReadOneBit() == 0 )
   691 		bits++;
   692 
   693 	unsigned int data = (1<<bits)-1;
   694 	
   695 	// read the value
   696 	if ( bits > 0)
   697 		data += ReadUBitLong( bits );
   698 
   699 	return data;
   700 }
   701 
   702 
   703 unsigned int bf_read::ReadBitLong(int numbits, bool bSigned)
   704 {
   705 	if(bSigned)
   706 		return (unsigned int)ReadSBitLong(numbits);
   707 	else
   708 		return ReadUBitLong(numbits);
   709 }
   710 
   711 
   712 // Basic Coordinate Routines (these contain bit-field size AND fixed point scaling constants)
   713 float bf_read::ReadBitCoord (void)
   714 {
   715 #if defined( BB_PROFILING )
   716 	VPROF( "bf_write::ReadBitCoord" );
   717 #endif
   718 	int		intval=0,fractval=0,signbit=0;
   719 	float	value = 0.0;
   720 
   721 
   722 	// Read the required integer and fraction flags
   723 	intval = ReadOneBit();
   724 	fractval = ReadOneBit();
   725 
   726 	// If we got either parse them, otherwise it's a zero.
   727 	if ( intval || fractval )
   728 	{
   729 		// Read the sign bit
   730 		signbit = ReadOneBit();
   731 
   732 		// If there's an integer, read it in
   733 		if ( intval )
   734 		{
   735 			// Adjust the integers from [0..MAX_COORD_VALUE-1] to [1..MAX_COORD_VALUE]
   736 			intval = ReadUBitLong( COORD_INTEGER_BITS ) + 1;
   737 		}
   738 
   739 		// If there's a fraction, read it in
   740 		if ( fractval )
   741 		{
   742 			fractval = ReadUBitLong( COORD_FRACTIONAL_BITS );
   743 		}
   744 
   745 		// Calculate the correct floating point value
   746 		value = intval + ((float)fractval * COORD_RESOLUTION);
   747 
   748 		// Fixup the sign if negative.
   749 		if ( signbit )
   750 			value = -value;
   751 	}
   752 
   753 	return value;
   754 }
   755 
   756 void bf_read::ReadBitVec3Coord( Vector& fa )
   757 {
   758 	int		xflag, yflag, zflag;
   759 
   760 	// This vector must be initialized! Otherwise, If any of the flags aren't set, 
   761 	// the corresponding component will not be read and will be stack garbage.
   762 	fa.Init( 0, 0, 0 );
   763 
   764 	xflag = ReadOneBit();
   765 	yflag = ReadOneBit(); 
   766 	zflag = ReadOneBit();
   767 
   768 	if ( xflag )
   769 		fa[0] = ReadBitCoord();
   770 	if ( yflag )
   771 		fa[1] = ReadBitCoord();
   772 	if ( zflag )
   773 		fa[2] = ReadBitCoord();
   774 }
   775 
   776 float bf_read::ReadBitNormal (void)
   777 {
   778 	// Read the sign bit
   779 	int	signbit = ReadOneBit();
   780 
   781 	// Read the fractional part
   782 	unsigned int fractval = ReadUBitLong( NORMAL_FRACTIONAL_BITS );
   783 
   784 	// Calculate the correct floating point value
   785 	float value = (float)fractval * NORMAL_RESOLUTION;
   786 
   787 	// Fixup the sign if negative.
   788 	if ( signbit )
   789 		value = -value;
   790 
   791 	return value;
   792 }
   793 
   794 void bf_read::ReadBitVec3Normal( Vector& fa )
   795 {
   796 	int xflag = ReadOneBit();
   797 	int yflag = ReadOneBit(); 
   798 
   799 	if (xflag)
   800 		fa[0] = ReadBitNormal();
   801 	else
   802 		fa[0] = 0.0f;
   803 
   804 	if (yflag)
   805 		fa[1] = ReadBitNormal();
   806 	else
   807 		fa[1] = 0.0f;
   808 
   809 	// The first two imply the third (but not its sign)
   810 	int znegative = ReadOneBit();
   811 
   812 	float fafafbfb = fa[0] * fa[0] + fa[1] * fa[1];
   813 	if (fafafbfb < 1.0f)
   814 		fa[2] = sqrt( 1.0f - fafafbfb );
   815 	else
   816 		fa[2] = 0.0f;
   817 
   818 	if (znegative)
   819 		fa[2] = -fa[2];
   820 }
   821 
   822 void bf_read::ReadBitAngles( QAngle& fa )
   823 {
   824 	Vector tmp;
   825 	ReadBitVec3Coord( tmp );
   826 	fa.Init( tmp.x, tmp.y, tmp.z );
   827 }
   828 
   829 int bf_read::ReadChar()
   830 {
   831 	return ReadSBitLong(sizeof(char) << 3);
   832 }
   833 
   834 int bf_read::ReadByte()
   835 {
   836 	return ReadUBitLong(sizeof(unsigned char) << 3);
   837 }
   838 
   839 int bf_read::ReadShort()
   840 {
   841 	return ReadSBitLong(sizeof(short) << 3);
   842 }
   843 
   844 int bf_read::ReadWord()
   845 {
   846 	return ReadUBitLong(sizeof(unsigned short) << 3);
   847 }
   848 
   849 long bf_read::ReadLong()
   850 {
   851 	return ReadSBitLong(sizeof(long) << 3);
   852 }
   853 
   854 float bf_read::ReadFloat()
   855 {
   856 	float ret;
   857 	Assert( sizeof(ret) == 4 );
   858 	ReadBits(&ret, 32);
   859 	return ret;
   860 }
   861 
   862 bool bf_read::ReadBytes(void *pOut, int nBytes)
   863 {
   864 	return ReadBits(pOut, nBytes << 3);
   865 }
   866 
   867 bool bf_read::ReadString( char *pStr, int maxLen, bool bLine, int *pOutNumChars )
   868 {
   869 	Assert( maxLen != 0 );
   870 
   871 	bool bTooSmall = false;
   872 	int iChar = 0;
   873 	while(1)
   874 	{
   875 		char val = ReadChar();
   876 		if ( val == 0 )
   877 			break;
   878 		else if ( bLine && val == '\n' )
   879 			break;
   880 
   881 		if ( iChar < (maxLen-1) )
   882 		{
   883 			pStr[iChar] = val;
   884 			++iChar;
   885 		}
   886 		else
   887 		{
   888 			bTooSmall = true;
   889 		}
   890 	}
   891 
   892 	// Make sure it's null-terminated.
   893 	Assert( iChar < maxLen );
   894 	pStr[iChar] = 0;
   895 
   896 	if ( pOutNumChars )
   897 		*pOutNumChars = iChar;
   898 
   899 	return !IsOverflowed() && !bTooSmall;
   900 }
   901 
   902 
   903 char* bf_read::ReadAndAllocateString( bool *pOverflow )
   904 {
   905 	char str[2048];
   906 	
   907 	int nChars;
   908 	bool bOverflow = !ReadString( str, sizeof( str ), false, &nChars );
   909 	if ( pOverflow )
   910 		*pOverflow = bOverflow;
   911 
   912 	// Now copy into the output and return it;
   913 	char *pRet = new char[ nChars + 1 ];
   914 	for ( int i=0; i <= nChars; i++ )
   915 		pRet[i] = str[i];
   916 
   917 	return pRet;
   918 }
   919 
   920 	
   921 bool bf_read::Seek(int iBit)
   922 {
   923 	if(iBit < 0)
   924 	{
   925 		SetOverflowFlag();
   926 		m_iCurBit = m_nDataBits;
   927 		return false;
   928 	}
   929 	else if(iBit > m_nDataBits)
   930 	{
   931 		SetOverflowFlag();
   932 		m_iCurBit = m_nDataBits;
   933 		return false;
   934 	}
   935 	else
   936 	{
   937 		m_iCurBit = iBit;
   938 		return true;
   939 	}
   940 }
   941 
   942 void bf_read::ExciseBits( int startbit, int bitstoremove )
   943 {
   944 	int endbit = startbit + bitstoremove;
   945 	int remaining_to_end = m_nDataBits - endbit;
   946 
   947 	bf_write temp;
   948 	temp.StartWriting( (void *)m_pData, m_nDataBits << 3, startbit );
   949 
   950 	Seek( endbit );
   951 
   952 	for ( int i = 0; i < remaining_to_end; i++ )
   953 	{
   954 		temp.WriteOneBit( ReadOneBit() );
   955 	}
   956 
   957 	Seek( startbit );
   958 	
   959 	m_nDataBits -= bitstoremove;
   960 	m_nDataBytes = m_nDataBits >> 3;
   961 }