Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

716 lines
15 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "bitbuf.h"
  9. #include "coordsize.h"
  10. #include "mathlib/vector.h"
  11. #include "mathlib/mathlib.h"
  12. #include "tier1/strtools.h"
  13. #include "bitvec.h"
  14. // FIXME: Can't use this until we get multithreaded allocations in tier0 working for tools
  15. // This is used by VVIS and fails to link
  16. // NOTE: This must be the last file included!!!
  17. //#include "tier0/memdbgon.h"
  18. #ifdef _X360
  19. // mandatory ... wary of above comment and isolating, tier0 is built as MT though
  20. #include "tier0/memdbgon.h"
  21. #endif
  22. #include "stdio.h"
  23. #if 0
  24. void CBitWrite::StartWriting( void *pData, int nBytes, int iStartBit, int nBits )
  25. {
  26. // Make sure it's dword aligned and padded.
  27. Assert( (nBytes % 4) == 0 );
  28. Assert(((unsigned long)pData & 3) == 0);
  29. Assert( iStartBit == 0 );
  30. m_pData = (uint32 *) pData;
  31. m_pDataOut = m_pData;
  32. m_nDataBytes = nBytes;
  33. if ( nBits == -1 )
  34. {
  35. m_nDataBits = nBytes << 3;
  36. }
  37. else
  38. {
  39. Assert( nBits <= nBytes*8 );
  40. m_nDataBits = nBits;
  41. }
  42. m_bOverflow = false;
  43. m_nOutBufWord = 0;
  44. m_nOutBitsAvail = 32;
  45. m_pBufferEnd = m_pDataOut + ( nBytes >> 2 );
  46. }
  47. const uint32 CBitBuffer::s_nMaskTable[33] = {
  48. 0,
  49. ( 1 << 1 ) - 1,
  50. ( 1 << 2 ) - 1,
  51. ( 1 << 3 ) - 1,
  52. ( 1 << 4 ) - 1,
  53. ( 1 << 5 ) - 1,
  54. ( 1 << 6 ) - 1,
  55. ( 1 << 7 ) - 1,
  56. ( 1 << 8 ) - 1,
  57. ( 1 << 9 ) - 1,
  58. ( 1 << 10 ) - 1,
  59. ( 1 << 11 ) - 1,
  60. ( 1 << 12 ) - 1,
  61. ( 1 << 13 ) - 1,
  62. ( 1 << 14 ) - 1,
  63. ( 1 << 15 ) - 1,
  64. ( 1 << 16 ) - 1,
  65. ( 1 << 17 ) - 1,
  66. ( 1 << 18 ) - 1,
  67. ( 1 << 19 ) - 1,
  68. ( 1 << 20 ) - 1,
  69. ( 1 << 21 ) - 1,
  70. ( 1 << 22 ) - 1,
  71. ( 1 << 23 ) - 1,
  72. ( 1 << 24 ) - 1,
  73. ( 1 << 25 ) - 1,
  74. ( 1 << 26 ) - 1,
  75. ( 1 << 27 ) - 1,
  76. ( 1 << 28 ) - 1,
  77. ( 1 << 29 ) - 1,
  78. ( 1 << 30 ) - 1,
  79. 0x7fffffff,
  80. 0xffffffff,
  81. };
  82. bool CBitWrite::WriteString( const char *pStr )
  83. {
  84. if(pStr)
  85. {
  86. while( *pStr )
  87. {
  88. WriteChar( * ( pStr++ ) );
  89. }
  90. }
  91. WriteChar( 0 );
  92. return !IsOverflowed();
  93. }
  94. void CBitWrite::WriteLongLong(int64 val)
  95. {
  96. uint *pLongs = (uint*)&val;
  97. // Insert the two DWORDS according to network endian
  98. const short endianIndex = 0x0100;
  99. byte *idx = (byte*)&endianIndex;
  100. WriteUBitLong(pLongs[*idx++], sizeof(long) << 3);
  101. WriteUBitLong(pLongs[*idx], sizeof(long) << 3);
  102. }
  103. bool CBitWrite::WriteBits(const void *pInData, int nBits)
  104. {
  105. unsigned char *pOut = (unsigned char*)pInData;
  106. int nBitsLeft = nBits;
  107. // Bounds checking..
  108. if ( ( GetNumBitsWritten() + nBits) > m_nDataBits )
  109. {
  110. SetOverflowFlag();
  111. CallErrorHandler( BITBUFERROR_BUFFER_OVERRUN, m_pDebugName );
  112. return false;
  113. }
  114. // !! speed!! need fast paths
  115. // write remaining bytes
  116. while ( nBitsLeft >= 8 )
  117. {
  118. WriteUBitLong( *pOut, 8, false );
  119. ++pOut;
  120. nBitsLeft -= 8;
  121. }
  122. // write remaining bits
  123. if ( nBitsLeft )
  124. {
  125. WriteUBitLong( *pOut, nBitsLeft, false );
  126. }
  127. return !IsOverflowed();
  128. }
  129. void CBitWrite::WriteBytes( const void *pBuf, int nBytes )
  130. {
  131. WriteBits(pBuf, nBytes << 3);
  132. }
  133. void CBitWrite::WriteBitCoord (const float f)
  134. {
  135. int signbit = (f <= -COORD_RESOLUTION);
  136. int intval = (int)abs(f);
  137. int fractval = abs((int)(f*COORD_DENOMINATOR)) & (COORD_DENOMINATOR-1);
  138. // Send the bit flags that indicate whether we have an integer part and/or a fraction part.
  139. WriteOneBit( intval );
  140. WriteOneBit( fractval );
  141. if ( intval || fractval )
  142. {
  143. // Send the sign bit
  144. WriteOneBit( signbit );
  145. // Send the integer if we have one.
  146. if ( intval )
  147. {
  148. // Adjust the integers from [1..MAX_COORD_VALUE] to [0..MAX_COORD_VALUE-1]
  149. intval--;
  150. WriteUBitLong( (unsigned int)intval, COORD_INTEGER_BITS );
  151. }
  152. // Send the fraction if we have one
  153. if ( fractval )
  154. {
  155. WriteUBitLong( (unsigned int)fractval, COORD_FRACTIONAL_BITS );
  156. }
  157. }
  158. }
  159. void CBitWrite::WriteBitCoordMP (const float f, bool bIntegral, bool bLowPrecision )
  160. {
  161. int signbit = (f <= -( bLowPrecision ? COORD_RESOLUTION_LOWPRECISION : COORD_RESOLUTION ));
  162. int intval = (int)abs(f);
  163. int fractval = bLowPrecision ?
  164. ( abs((int)(f*COORD_DENOMINATOR_LOWPRECISION)) & (COORD_DENOMINATOR_LOWPRECISION-1) ) :
  165. ( abs((int)(f*COORD_DENOMINATOR)) & (COORD_DENOMINATOR-1) );
  166. bool bInBounds = intval < (1 << COORD_INTEGER_BITS_MP );
  167. WriteOneBit( bInBounds );
  168. if ( bIntegral )
  169. {
  170. // Send the sign bit
  171. WriteOneBit( intval );
  172. if ( intval )
  173. {
  174. WriteOneBit( signbit );
  175. // Send the integer if we have one.
  176. // Adjust the integers from [1..MAX_COORD_VALUE] to [0..MAX_COORD_VALUE-1]
  177. intval--;
  178. if ( bInBounds )
  179. {
  180. WriteUBitLong( (unsigned int)intval, COORD_INTEGER_BITS_MP );
  181. }
  182. else
  183. {
  184. WriteUBitLong( (unsigned int)intval, COORD_INTEGER_BITS );
  185. }
  186. }
  187. }
  188. else
  189. {
  190. // Send the bit flags that indicate whether we have an integer part and/or a fraction part.
  191. WriteOneBit( intval );
  192. // Send the sign bit
  193. WriteOneBit( signbit );
  194. // Send the integer if we have one.
  195. if ( intval )
  196. {
  197. // Adjust the integers from [1..MAX_COORD_VALUE] to [0..MAX_COORD_VALUE-1]
  198. intval--;
  199. if ( bInBounds )
  200. {
  201. WriteUBitLong( (unsigned int)intval, COORD_INTEGER_BITS_MP );
  202. }
  203. else
  204. {
  205. WriteUBitLong( (unsigned int)intval, COORD_INTEGER_BITS );
  206. }
  207. }
  208. WriteUBitLong( (unsigned int)fractval, bLowPrecision ? COORD_FRACTIONAL_BITS_MP_LOWPRECISION : COORD_FRACTIONAL_BITS );
  209. }
  210. }
  211. void CBitWrite::SeekToBit( int nBit )
  212. {
  213. TempFlush();
  214. m_pDataOut = m_pData + ( nBit / 32 );
  215. m_nOutBufWord = *( m_pDataOut );
  216. m_nOutBitsAvail = 32 - ( nBit & 31 );
  217. }
  218. void CBitWrite::WriteBitVec3Coord( const Vector& fa )
  219. {
  220. int xflag, yflag, zflag;
  221. xflag = (fa[0] >= COORD_RESOLUTION) || (fa[0] <= -COORD_RESOLUTION);
  222. yflag = (fa[1] >= COORD_RESOLUTION) || (fa[1] <= -COORD_RESOLUTION);
  223. zflag = (fa[2] >= COORD_RESOLUTION) || (fa[2] <= -COORD_RESOLUTION);
  224. WriteOneBit( xflag );
  225. WriteOneBit( yflag );
  226. WriteOneBit( zflag );
  227. if ( xflag )
  228. WriteBitCoord( fa[0] );
  229. if ( yflag )
  230. WriteBitCoord( fa[1] );
  231. if ( zflag )
  232. WriteBitCoord( fa[2] );
  233. }
  234. void CBitWrite::WriteBitNormal( float f )
  235. {
  236. int signbit = (f <= -NORMAL_RESOLUTION);
  237. // NOTE: Since +/-1 are valid values for a normal, I'm going to encode that as all ones
  238. unsigned int fractval = abs( (int)(f*NORMAL_DENOMINATOR) );
  239. // clamp..
  240. if (fractval > NORMAL_DENOMINATOR)
  241. fractval = NORMAL_DENOMINATOR;
  242. // Send the sign bit
  243. WriteOneBit( signbit );
  244. // Send the fractional component
  245. WriteUBitLong( fractval, NORMAL_FRACTIONAL_BITS );
  246. }
  247. void CBitWrite::WriteBitVec3Normal( const Vector& fa )
  248. {
  249. int xflag, yflag;
  250. xflag = (fa[0] >= NORMAL_RESOLUTION) || (fa[0] <= -NORMAL_RESOLUTION);
  251. yflag = (fa[1] >= NORMAL_RESOLUTION) || (fa[1] <= -NORMAL_RESOLUTION);
  252. WriteOneBit( xflag );
  253. WriteOneBit( yflag );
  254. if ( xflag )
  255. WriteBitNormal( fa[0] );
  256. if ( yflag )
  257. WriteBitNormal( fa[1] );
  258. // Write z sign bit
  259. int signbit = (fa[2] <= -NORMAL_RESOLUTION);
  260. WriteOneBit( signbit );
  261. }
  262. void CBitWrite::WriteBitAngle( float fAngle, int numbits )
  263. {
  264. unsigned int shift = GetBitForBitnum(numbits);
  265. unsigned int mask = shift - 1;
  266. int d = (int)( (fAngle / 360.0) * shift );
  267. d &= mask;
  268. WriteUBitLong((unsigned int)d, numbits);
  269. }
  270. bool CBitWrite::WriteBitsFromBuffer( bf_read *pIn, int nBits )
  271. {
  272. // This could be optimized a little by
  273. while ( nBits > 32 )
  274. {
  275. WriteUBitLong( pIn->ReadUBitLong( 32 ), 32 );
  276. nBits -= 32;
  277. }
  278. WriteUBitLong( pIn->ReadUBitLong( nBits ), nBits );
  279. return !IsOverflowed() && !pIn->IsOverflowed();
  280. }
  281. void CBitWrite::WriteBitAngles( const QAngle& fa )
  282. {
  283. // FIXME:
  284. Vector tmp( fa.x, fa.y, fa.z );
  285. WriteBitVec3Coord( tmp );
  286. }
  287. bool CBitRead::Seek( int nPosition )
  288. {
  289. bool bSucc = true;
  290. if ( nPosition < 0 || nPosition > m_nDataBits)
  291. {
  292. SetOverflowFlag();
  293. bSucc = false;
  294. nPosition = m_nDataBits;
  295. }
  296. int nHead = m_nDataBytes & 3; // non-multiple-of-4 bytes at head of buffer. We put the "round off"
  297. // at the head to make reading and detecting the end efficient.
  298. int nByteOfs = nPosition / 8;
  299. if ( ( m_nDataBytes < 4 ) || ( nHead && ( nByteOfs < nHead ) ) )
  300. {
  301. // partial first dword
  302. uint8 const *pPartial = ( uint8 const *) m_pData;
  303. if ( m_pData )
  304. {
  305. m_nInBufWord = *( pPartial++ );
  306. if ( nHead > 1 )
  307. m_nInBufWord |= ( *pPartial++ ) << 8;
  308. if ( nHead > 2 )
  309. m_nInBufWord |= ( *pPartial++ ) << 16;
  310. }
  311. m_pDataIn = ( uint32 const * ) pPartial;
  312. m_nInBufWord >>= ( nPosition & 31 );
  313. m_nBitsAvail = ( nHead << 3 ) - ( nPosition & 31 );
  314. }
  315. else
  316. {
  317. int nAdjPosition = nPosition - ( nHead << 3 );
  318. m_pDataIn = reinterpret_cast<uint32 const *> (
  319. reinterpret_cast<uint8 const *>( m_pData ) + ( ( nAdjPosition / 32 ) << 2 ) + nHead );
  320. if ( m_pData )
  321. {
  322. m_nBitsAvail = 32;
  323. GrabNextDWord();
  324. }
  325. else
  326. {
  327. m_nInBufWord = 0;
  328. m_nBitsAvail = 1;
  329. }
  330. m_nInBufWord >>= ( nAdjPosition & 31 );
  331. m_nBitsAvail = min( m_nBitsAvail, 32 - ( nAdjPosition & 31 ) ); // in case grabnextdword overflowed
  332. }
  333. return bSucc;
  334. }
  335. void CBitRead::StartReading( const void *pData, int nBytes, int iStartBit, int nBits )
  336. {
  337. // Make sure it's dword aligned and padded.
  338. Assert(((unsigned long)pData & 3) == 0);
  339. m_pData = (uint32 *) pData;
  340. m_pDataIn = m_pData;
  341. m_nDataBytes = nBytes;
  342. if ( nBits == -1 )
  343. {
  344. m_nDataBits = nBytes << 3;
  345. }
  346. else
  347. {
  348. Assert( nBits <= nBytes*8 );
  349. m_nDataBits = nBits;
  350. }
  351. m_bOverflow = false;
  352. m_pBufferEnd = reinterpret_cast<uint32 const *> ( reinterpret_cast< uint8 const *> (m_pData) + nBytes );
  353. if ( m_pData )
  354. Seek( iStartBit );
  355. }
  356. bool CBitRead::ReadString( char *pStr, int maxLen, bool bLine, int *pOutNumChars )
  357. {
  358. Assert( maxLen != 0 );
  359. bool bTooSmall = false;
  360. int iChar = 0;
  361. while(1)
  362. {
  363. char val = ReadChar();
  364. if ( val == 0 )
  365. break;
  366. else if ( bLine && val == '\n' )
  367. break;
  368. if ( iChar < (maxLen-1) )
  369. {
  370. pStr[iChar] = val;
  371. ++iChar;
  372. }
  373. else
  374. {
  375. bTooSmall = true;
  376. }
  377. }
  378. // Make sure it's null-terminated.
  379. Assert( iChar < maxLen );
  380. pStr[iChar] = 0;
  381. if ( pOutNumChars )
  382. *pOutNumChars = iChar;
  383. return !IsOverflowed() && !bTooSmall;
  384. }
  385. char* CBitRead::ReadAndAllocateString( bool *pOverflow )
  386. {
  387. char str[2048];
  388. int nChars;
  389. bool bOverflow = !ReadString( str, sizeof( str ), false, &nChars );
  390. if ( pOverflow )
  391. *pOverflow = bOverflow;
  392. // Now copy into the output and return it;
  393. char *pRet = new char[ nChars + 1 ];
  394. for ( int i=0; i <= nChars; i++ )
  395. pRet[i] = str[i];
  396. return pRet;
  397. }
  398. int64 CBitRead::ReadLongLong( void )
  399. {
  400. int64 retval;
  401. uint *pLongs = (uint*)&retval;
  402. // Read the two DWORDs according to network endian
  403. const short endianIndex = 0x0100;
  404. byte *idx = (byte*)&endianIndex;
  405. pLongs[*idx++] = ReadUBitLong(sizeof(long) << 3);
  406. pLongs[*idx] = ReadUBitLong(sizeof(long) << 3);
  407. return retval;
  408. }
  409. void CBitRead::ReadBits(void *pOutData, int nBits)
  410. {
  411. unsigned char *pOut = (unsigned char*)pOutData;
  412. int nBitsLeft = nBits;
  413. // align output to dword boundary
  414. while( ((unsigned long)pOut & 3) != 0 && nBitsLeft >= 8 )
  415. {
  416. *pOut = (unsigned char)ReadUBitLong(8);
  417. ++pOut;
  418. nBitsLeft -= 8;
  419. }
  420. // X360TBD: Can't read dwords in ReadBits because they'll get swapped
  421. if ( IsPC() )
  422. {
  423. // read dwords
  424. while ( nBitsLeft >= 32 )
  425. {
  426. *((unsigned long*)pOut) = ReadUBitLong(32);
  427. pOut += sizeof(unsigned long);
  428. nBitsLeft -= 32;
  429. }
  430. }
  431. // read remaining bytes
  432. while ( nBitsLeft >= 8 )
  433. {
  434. *pOut = ReadUBitLong(8);
  435. ++pOut;
  436. nBitsLeft -= 8;
  437. }
  438. // read remaining bits
  439. if ( nBitsLeft )
  440. {
  441. *pOut = ReadUBitLong(nBitsLeft);
  442. }
  443. }
  444. bool CBitRead::ReadBytes(void *pOut, int nBytes)
  445. {
  446. ReadBits(pOut, nBytes << 3);
  447. return !IsOverflowed();
  448. }
  449. float CBitRead::ReadBitAngle( int numbits )
  450. {
  451. float shift = (float)( GetBitForBitnum(numbits) );
  452. int i = ReadUBitLong( numbits );
  453. float fReturn = (float)i * (360.0 / shift);
  454. return fReturn;
  455. }
  456. // Basic Coordinate Routines (these contain bit-field size AND fixed point scaling constants)
  457. float CBitRead::ReadBitCoord (void)
  458. {
  459. int intval=0,fractval=0,signbit=0;
  460. float value = 0.0;
  461. // Read the required integer and fraction flags
  462. intval = ReadOneBit();
  463. fractval = ReadOneBit();
  464. // If we got either parse them, otherwise it's a zero.
  465. if ( intval || fractval )
  466. {
  467. // Read the sign bit
  468. signbit = ReadOneBit();
  469. // If there's an integer, read it in
  470. if ( intval )
  471. {
  472. // Adjust the integers from [0..MAX_COORD_VALUE-1] to [1..MAX_COORD_VALUE]
  473. intval = ReadUBitLong( COORD_INTEGER_BITS ) + 1;
  474. }
  475. // If there's a fraction, read it in
  476. if ( fractval )
  477. {
  478. fractval = ReadUBitLong( COORD_FRACTIONAL_BITS );
  479. }
  480. // Calculate the correct floating point value
  481. value = intval + ((float)fractval * COORD_RESOLUTION);
  482. // Fixup the sign if negative.
  483. if ( signbit )
  484. value = -value;
  485. }
  486. return value;
  487. }
  488. float CBitRead::ReadBitCoordMP( bool bIntegral, bool bLowPrecision )
  489. {
  490. int intval=0,fractval=0,signbit=0;
  491. float value = 0.0;
  492. bool bInBounds = ReadOneBit() ? true : false;
  493. if ( bIntegral )
  494. {
  495. // Read the required integer and fraction flags
  496. intval = ReadOneBit();
  497. // If we got either parse them, otherwise it's a zero.
  498. if ( intval )
  499. {
  500. // Read the sign bit
  501. signbit = ReadOneBit();
  502. // If there's an integer, read it in
  503. // Adjust the integers from [0..MAX_COORD_VALUE-1] to [1..MAX_COORD_VALUE]
  504. if ( bInBounds )
  505. {
  506. value = ReadUBitLong( COORD_INTEGER_BITS_MP ) + 1;
  507. }
  508. else
  509. {
  510. value = ReadUBitLong( COORD_INTEGER_BITS ) + 1;
  511. }
  512. }
  513. }
  514. else
  515. {
  516. // Read the required integer and fraction flags
  517. intval = ReadOneBit();
  518. // Read the sign bit
  519. signbit = ReadOneBit();
  520. // If we got either parse them, otherwise it's a zero.
  521. if ( intval )
  522. {
  523. if ( bInBounds )
  524. {
  525. intval = ReadUBitLong( COORD_INTEGER_BITS_MP ) + 1;
  526. }
  527. else
  528. {
  529. intval = ReadUBitLong( COORD_INTEGER_BITS ) + 1;
  530. }
  531. }
  532. // If there's a fraction, read it in
  533. fractval = ReadUBitLong( bLowPrecision ? COORD_FRACTIONAL_BITS_MP_LOWPRECISION : COORD_FRACTIONAL_BITS );
  534. // Calculate the correct floating point value
  535. value = intval + ((float)fractval * ( bLowPrecision ? COORD_RESOLUTION_LOWPRECISION : COORD_RESOLUTION ) );
  536. }
  537. // Fixup the sign if negative.
  538. if ( signbit )
  539. value = -value;
  540. return value;
  541. }
  542. void CBitRead::ReadBitVec3Coord( Vector& fa )
  543. {
  544. int xflag, yflag, zflag;
  545. // This vector must be initialized! Otherwise, If any of the flags aren't set,
  546. // the corresponding component will not be read and will be stack garbage.
  547. fa.Init( 0, 0, 0 );
  548. xflag = ReadOneBit();
  549. yflag = ReadOneBit();
  550. zflag = ReadOneBit();
  551. if ( xflag )
  552. fa[0] = ReadBitCoord();
  553. if ( yflag )
  554. fa[1] = ReadBitCoord();
  555. if ( zflag )
  556. fa[2] = ReadBitCoord();
  557. }
  558. float CBitRead::ReadBitNormal (void)
  559. {
  560. // Read the sign bit
  561. int signbit = ReadOneBit();
  562. // Read the fractional part
  563. unsigned int fractval = ReadUBitLong( NORMAL_FRACTIONAL_BITS );
  564. // Calculate the correct floating point value
  565. float value = (float)fractval * NORMAL_RESOLUTION;
  566. // Fixup the sign if negative.
  567. if ( signbit )
  568. value = -value;
  569. return value;
  570. }
  571. void CBitRead::ReadBitVec3Normal( Vector& fa )
  572. {
  573. int xflag = ReadOneBit();
  574. int yflag = ReadOneBit();
  575. if (xflag)
  576. fa[0] = ReadBitNormal();
  577. else
  578. fa[0] = 0.0f;
  579. if (yflag)
  580. fa[1] = ReadBitNormal();
  581. else
  582. fa[1] = 0.0f;
  583. // The first two imply the third (but not its sign)
  584. int znegative = ReadOneBit();
  585. float fafafbfb = fa[0] * fa[0] + fa[1] * fa[1];
  586. if (fafafbfb < 1.0f)
  587. fa[2] = sqrt( 1.0f - fafafbfb );
  588. else
  589. fa[2] = 0.0f;
  590. if (znegative)
  591. fa[2] = -fa[2];
  592. }
  593. void CBitRead::ReadBitAngles( QAngle& fa )
  594. {
  595. Vector tmp;
  596. ReadBitVec3Coord( tmp );
  597. fa.Init( tmp.x, tmp.y, tmp.z );
  598. }
  599. #endif