Counter Strike : Global Offensive Source Code
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.

1626 lines
33 KiB

  1. //========= Copyright � 1996-2005, 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. #ifndef NDEBUG
  23. static volatile char const *pDebugString;
  24. #define DEBUG_LINK_CHECK pDebugString = "tier1.lib built debug!"
  25. #else
  26. #define DEBUG_LINK_CHECK
  27. #endif
  28. #if _WIN32
  29. #define FAST_BIT_SCAN 1
  30. #if defined( _X360 )
  31. #define CountLeadingZeros(x) _CountLeadingZeros(x)
  32. inline unsigned int CountTrailingZeros( unsigned int elem )
  33. {
  34. // this implements CountTrailingZeros() / BitScanForward()
  35. unsigned int mask = elem-1;
  36. unsigned int comp = ~elem;
  37. elem = mask & comp;
  38. return (32 - _CountLeadingZeros(elem));
  39. }
  40. #else
  41. #include <intrin.h>
  42. #pragma intrinsic(_BitScanReverse)
  43. #pragma intrinsic(_BitScanForward)
  44. inline unsigned int CountLeadingZeros(unsigned int x)
  45. {
  46. unsigned long firstBit;
  47. if ( _BitScanReverse(&firstBit,x) )
  48. return 31 - firstBit;
  49. return 32;
  50. }
  51. inline unsigned int CountTrailingZeros(unsigned int elem)
  52. {
  53. unsigned long out;
  54. if ( _BitScanForward(&out, elem) )
  55. return out;
  56. return 32;
  57. }
  58. #endif
  59. #else
  60. #define FAST_BIT_SCAN 0
  61. #endif
  62. static BitBufErrorHandler g_BitBufErrorHandler = 0;
  63. inline int BitForBitnum(int bitnum)
  64. {
  65. return GetBitForBitnum(bitnum);
  66. }
  67. void InternalBitBufErrorHandler( BitBufErrorType errorType, const char *pDebugName )
  68. {
  69. if ( g_BitBufErrorHandler )
  70. g_BitBufErrorHandler( errorType, pDebugName );
  71. }
  72. void SetBitBufErrorHandler( BitBufErrorHandler fn )
  73. {
  74. g_BitBufErrorHandler = fn;
  75. }
  76. // #define BB_PROFILING
  77. // Precalculated bit masks for WriteUBitLong. Using these tables instead of
  78. // doing the calculations gives a 33% speedup in WriteUBitLong.
  79. uint32 g_BitWriteMasks[32][33];
  80. // (1 << i) - 1
  81. uint32 g_ExtraMasks[32];
  82. class CBitWriteMasksInit
  83. {
  84. public:
  85. CBitWriteMasksInit()
  86. {
  87. for( unsigned int startbit=0; startbit < 32; startbit++ )
  88. {
  89. for( unsigned int nBitsLeft=0; nBitsLeft < 33; nBitsLeft++ )
  90. {
  91. unsigned int endbit = startbit + nBitsLeft;
  92. g_BitWriteMasks[startbit][nBitsLeft] = BitForBitnum(startbit) - 1;
  93. if(endbit < 32)
  94. g_BitWriteMasks[startbit][nBitsLeft] |= ~(BitForBitnum(endbit) - 1);
  95. }
  96. }
  97. for ( unsigned int maskBit=0; maskBit < 32; maskBit++ )
  98. g_ExtraMasks[maskBit] = BitForBitnum(maskBit) - 1;
  99. }
  100. };
  101. CBitWriteMasksInit g_BitWriteMasksInit;
  102. // ---------------------------------------------------------------------------------------- //
  103. // bf_write
  104. // ---------------------------------------------------------------------------------------- //
  105. bf_write::bf_write()
  106. {
  107. DEBUG_LINK_CHECK;
  108. m_pData = NULL;
  109. m_nDataBytes = 0;
  110. m_nDataBits = -1; // set to -1 so we generate overflow on any operation
  111. m_iCurBit = 0;
  112. m_bOverflow = false;
  113. m_bAssertOnOverflow = true;
  114. m_pDebugName = NULL;
  115. }
  116. bf_write::bf_write( const char *pDebugName, void *pData, int nBytes, int nBits )
  117. {
  118. DEBUG_LINK_CHECK;
  119. m_bAssertOnOverflow = true;
  120. m_pDebugName = pDebugName;
  121. StartWriting( pData, nBytes, 0, nBits );
  122. }
  123. bf_write::bf_write( void *pData, int nBytes, int nBits )
  124. {
  125. m_bAssertOnOverflow = true;
  126. m_pDebugName = NULL;
  127. StartWriting( pData, nBytes, 0, nBits );
  128. }
  129. void bf_write::StartWriting( void *pData, int nBytes, int iStartBit, int nBits )
  130. {
  131. // Make sure it's dword aligned and padded.
  132. DEBUG_LINK_CHECK;
  133. Assert( (nBytes % 4) == 0 );
  134. Assert(((uintp)pData & 3) == 0);
  135. // The writing code will overrun the end of the buffer if it isn't dword aligned, so truncate to force alignment
  136. nBytes &= ~3;
  137. m_pData = (unsigned char*)pData;
  138. m_nDataBytes = nBytes;
  139. if ( nBits == -1 )
  140. {
  141. m_nDataBits = nBytes << 3;
  142. }
  143. else
  144. {
  145. Assert( nBits <= nBytes*8 );
  146. m_nDataBits = nBits;
  147. }
  148. m_iCurBit = iStartBit;
  149. m_bOverflow = false;
  150. }
  151. void bf_write::Reset()
  152. {
  153. m_iCurBit = 0;
  154. m_bOverflow = false;
  155. }
  156. void bf_write::SetAssertOnOverflow( bool bAssert )
  157. {
  158. m_bAssertOnOverflow = bAssert;
  159. }
  160. const char* bf_write::GetDebugName()
  161. {
  162. return m_pDebugName;
  163. }
  164. void bf_write::SetDebugName( const char *pDebugName )
  165. {
  166. m_pDebugName = pDebugName;
  167. }
  168. void bf_write::SeekToBit( int bitPos )
  169. {
  170. m_iCurBit = bitPos;
  171. }
  172. // Sign bit comes first
  173. void bf_write::WriteSBitLong( int data, int numbits )
  174. {
  175. // Do we have a valid # of bits to encode with?
  176. Assert( numbits >= 1 );
  177. // Note: it does this wierdness here so it's bit-compatible with regular integer data in the buffer.
  178. // (Some old code writes direct integers right into the buffer).
  179. if(data < 0)
  180. {
  181. #ifdef _DEBUG
  182. if( numbits < 32 )
  183. {
  184. // Make sure it doesn't overflow.
  185. if( data < 0 )
  186. {
  187. Assert( data >= -(BitForBitnum(numbits-1)) );
  188. }
  189. else
  190. {
  191. Assert( data < (BitForBitnum(numbits-1)) );
  192. }
  193. }
  194. #endif
  195. WriteUBitLong( (unsigned int)(0x80000000 + data), numbits - 1, false );
  196. WriteOneBit( 1 );
  197. }
  198. else
  199. {
  200. WriteUBitLong((unsigned int)data, numbits - 1);
  201. WriteOneBit( 0 );
  202. }
  203. }
  204. #if _WIN32
  205. inline unsigned int BitCountNeededToEncode(unsigned int data)
  206. {
  207. #if defined(_X360)
  208. return (32 - CountLeadingZeros(data+1)) - 1;
  209. #else
  210. unsigned long firstBit;
  211. _BitScanReverse(&firstBit,data+1);
  212. return firstBit;
  213. #endif
  214. }
  215. #endif // _WIN32
  216. // writes an unsigned integer with variable bit length
  217. void bf_write::WriteUBitVar( unsigned int n )
  218. {
  219. if ( n < 16 )
  220. WriteUBitLong( n, 6 );
  221. else
  222. if ( n < 256 )
  223. WriteUBitLong( ( n & 15 ) | 16 | ( ( n & ( 128 | 64 | 32 | 16 ) ) << 2 ), 10 );
  224. else
  225. if ( n < 4096 )
  226. WriteUBitLong( ( n & 15 ) | 32 | ( ( n & ( 2048 | 1024 | 512 | 256 | 128 | 64 | 32 | 16 ) ) << 2 ), 14 );
  227. else
  228. {
  229. WriteUBitLong( ( n & 15 ) | 48, 6 );
  230. WriteUBitLong( ( n >> 4 ), 32 - 4 );
  231. }
  232. }
  233. void bf_write::WriteVarInt32( uint32 data )
  234. {
  235. // Check if align and we have room, slow path if not
  236. if ( (m_iCurBit & 7) == 0 && (m_iCurBit + bitbuf::kMaxVarint32Bytes * 8 ) <= m_nDataBits)
  237. {
  238. uint8 *target = ((uint8*)m_pData) + (m_iCurBit>>3);
  239. target[0] = static_cast<uint8>(data | 0x80);
  240. if ( data >= (1 << 7) )
  241. {
  242. target[1] = static_cast<uint8>((data >> 7) | 0x80);
  243. if ( data >= (1 << 14) )
  244. {
  245. target[2] = static_cast<uint8>((data >> 14) | 0x80);
  246. if ( data >= (1 << 21) )
  247. {
  248. target[3] = static_cast<uint8>((data >> 21) | 0x80);
  249. if ( data >= (1 << 28) )
  250. {
  251. target[4] = static_cast<uint8>(data >> 28);
  252. m_iCurBit += 5 * 8;
  253. return;
  254. }
  255. else
  256. {
  257. target[3] &= 0x7F;
  258. m_iCurBit += 4 * 8;
  259. return;
  260. }
  261. }
  262. else
  263. {
  264. target[2] &= 0x7F;
  265. m_iCurBit += 3 * 8;
  266. return;
  267. }
  268. }
  269. else
  270. {
  271. target[1] &= 0x7F;
  272. m_iCurBit += 2 * 8;
  273. return;
  274. }
  275. }
  276. else
  277. {
  278. target[0] &= 0x7F;
  279. m_iCurBit += 1 * 8;
  280. return;
  281. }
  282. }
  283. else // Slow path
  284. {
  285. while ( data > 0x7F )
  286. {
  287. WriteUBitLong( (data & 0x7F) | 0x80, 8 );
  288. data >>= 7;
  289. }
  290. WriteUBitLong( data & 0x7F, 8 );
  291. }
  292. }
  293. void bf_write::WriteVarInt64( uint64 data )
  294. {
  295. // Check if align and we have room, slow path if not
  296. if ( (m_iCurBit & 7) == 0 && (m_iCurBit + bitbuf::kMaxVarintBytes * 8 ) <= m_nDataBits )
  297. {
  298. uint8 *target = ((uint8*)m_pData) + (m_iCurBit>>3);
  299. // Splitting into 32-bit pieces gives better performance on 32-bit
  300. // processors.
  301. uint32 part0 = static_cast<uint32>(data );
  302. uint32 part1 = static_cast<uint32>(data >> 28);
  303. uint32 part2 = static_cast<uint32>(data >> 56);
  304. int size;
  305. // Here we can't really optimize for small numbers, since the data is
  306. // split into three parts. Cheking for numbers < 128, for instance,
  307. // would require three comparisons, since you'd have to make sure part1
  308. // and part2 are zero. However, if the caller is using 64-bit integers,
  309. // it is likely that they expect the numbers to often be very large, so
  310. // we probably don't want to optimize for small numbers anyway. Thus,
  311. // we end up with a hardcoded binary search tree...
  312. if ( part2 == 0 )
  313. {
  314. if ( part1 == 0 )
  315. {
  316. if ( part0 < (1 << 14) )
  317. {
  318. if ( part0 < (1 << 7) )
  319. {
  320. size = 1; goto size1;
  321. }
  322. else
  323. {
  324. size = 2; goto size2;
  325. }
  326. }
  327. else
  328. {
  329. if ( part0 < (1 << 21) )
  330. {
  331. size = 3; goto size3;
  332. }
  333. else
  334. {
  335. size = 4; goto size4;
  336. }
  337. }
  338. }
  339. else
  340. {
  341. if ( part1 < (1 << 14) )
  342. {
  343. if ( part1 < (1 << 7) )
  344. {
  345. size = 5; goto size5;
  346. }
  347. else
  348. {
  349. size = 6; goto size6;
  350. }
  351. }
  352. else
  353. {
  354. if ( part1 < (1 << 21) )
  355. {
  356. size = 7; goto size7;
  357. }
  358. else
  359. {
  360. size = 8; goto size8;
  361. }
  362. }
  363. }
  364. }
  365. else
  366. {
  367. if ( part2 < (1 << 7) )
  368. {
  369. size = 9; goto size9;
  370. }
  371. else
  372. {
  373. size = 10; goto size10;
  374. }
  375. }
  376. AssertFatalMsg( false, "Can't get here." );
  377. size10: target[9] = static_cast<uint8>((part2 >> 7) | 0x80);
  378. size9 : target[8] = static_cast<uint8>((part2 ) | 0x80);
  379. size8 : target[7] = static_cast<uint8>((part1 >> 21) | 0x80);
  380. size7 : target[6] = static_cast<uint8>((part1 >> 14) | 0x80);
  381. size6 : target[5] = static_cast<uint8>((part1 >> 7) | 0x80);
  382. size5 : target[4] = static_cast<uint8>((part1 ) | 0x80);
  383. size4 : target[3] = static_cast<uint8>((part0 >> 21) | 0x80);
  384. size3 : target[2] = static_cast<uint8>((part0 >> 14) | 0x80);
  385. size2 : target[1] = static_cast<uint8>((part0 >> 7) | 0x80);
  386. size1 : target[0] = static_cast<uint8>((part0 ) | 0x80);
  387. target[size-1] &= 0x7F;
  388. m_iCurBit += size * 8;
  389. }
  390. else // slow path
  391. {
  392. while ( data > 0x7F )
  393. {
  394. WriteUBitLong( (data & 0x7F) | 0x80, 8 );
  395. data >>= 7;
  396. }
  397. WriteUBitLong( data & 0x7F, 8 );
  398. }
  399. }
  400. void bf_write::WriteSignedVarInt32( int32 data )
  401. {
  402. WriteVarInt32( bitbuf::ZigZagEncode32( data ) );
  403. }
  404. void bf_write::WriteSignedVarInt64( int64 data )
  405. {
  406. WriteVarInt64( bitbuf::ZigZagEncode64( data ) );
  407. }
  408. int bf_write::ByteSizeVarInt32( uint32 data )
  409. {
  410. int size = 1;
  411. while ( data > 0x7F ) {
  412. size++;
  413. data >>= 7;
  414. }
  415. return size;
  416. }
  417. int bf_write::ByteSizeVarInt64( uint64 data )
  418. {
  419. int size = 1;
  420. while ( data > 0x7F ) {
  421. size++;
  422. data >>= 7;
  423. }
  424. return size;
  425. }
  426. int bf_write::ByteSizeSignedVarInt32( int32 data )
  427. {
  428. return ByteSizeVarInt32( bitbuf::ZigZagEncode32( data ) );
  429. }
  430. int bf_write::ByteSizeSignedVarInt64( int64 data )
  431. {
  432. return ByteSizeVarInt64( bitbuf::ZigZagEncode64( data ) );
  433. }
  434. void bf_write::WriteBitLong(unsigned int data, int numbits, bool bSigned)
  435. {
  436. if(bSigned)
  437. WriteSBitLong((int)data, numbits);
  438. else
  439. WriteUBitLong(data, numbits);
  440. }
  441. bool bf_write::WriteBits(const void *pInData, int nBits)
  442. {
  443. #if defined( BB_PROFILING )
  444. VPROF( "bf_write::WriteBits" );
  445. #endif
  446. unsigned char *pIn = (unsigned char*)pInData;
  447. int nBitsLeft = nBits;
  448. // Bounds checking..
  449. if ( (m_iCurBit+nBits) > m_nDataBits )
  450. {
  451. SetOverflowFlag();
  452. CallErrorHandler( BITBUFERROR_BUFFER_OVERRUN, GetDebugName() );
  453. return false;
  454. }
  455. // Align input to dword boundary
  456. while (((uintp)pIn & 3) != 0 && nBitsLeft >= 8)
  457. {
  458. WriteUBitLong( *pIn, 8, false );
  459. ++pIn;
  460. nBitsLeft -= 8;
  461. }
  462. if ( nBitsLeft >= 32 )
  463. {
  464. if ( (m_iCurBit & 7) == 0 )
  465. {
  466. // current bit is byte aligned, do block copy
  467. int numbytes = nBitsLeft >> 3;
  468. int numbits = numbytes << 3;
  469. Q_memcpy( m_pData+(m_iCurBit>>3), pIn, numbytes );
  470. pIn += numbytes;
  471. nBitsLeft -= numbits;
  472. m_iCurBit += numbits;
  473. }
  474. else
  475. {
  476. const uint32 iBitsRight = (m_iCurBit & 31);
  477. Assert( iBitsRight > 0 ); // should not be aligned, otherwise it would have been handled before
  478. const uint32 iBitsLeft = 32 - iBitsRight;
  479. const int iBitsChanging = 32 + iBitsLeft; // how many bits are changed during one step (not necessary written meaningful)
  480. unsigned int iDWord = m_iCurBit >> 5;
  481. uint32 outWord = LoadLittleDWord( (uint32*)m_pData, iDWord );
  482. outWord &= g_BitWriteMasks[iBitsRight][32]; // clear rest of beginning DWORD
  483. // copy in DWORD blocks
  484. while(nBitsLeft >= iBitsChanging )
  485. {
  486. uint32 curData = LittleDWord( *(uint32*)pIn );
  487. pIn += sizeof(uint32);
  488. outWord |= curData << iBitsRight;
  489. StoreLittleDWord( (uint32*)m_pData, iDWord, outWord );
  490. ++iDWord;
  491. outWord = curData >> iBitsLeft;
  492. nBitsLeft -= 32;
  493. m_iCurBit += 32;
  494. }
  495. // store last word
  496. StoreLittleDWord( (uint32*)m_pData, iDWord, outWord );
  497. // write remaining DWORD
  498. if( nBitsLeft >= 32 )
  499. {
  500. WriteUBitLong( LittleDWord(*((uint32*)pIn)), 32, false );
  501. pIn += sizeof(uint32);
  502. nBitsLeft -= 32;
  503. }
  504. }
  505. }
  506. // write remaining bytes
  507. while ( nBitsLeft >= 8 )
  508. {
  509. WriteUBitLong( *pIn, 8, false );
  510. ++pIn;
  511. nBitsLeft -= 8;
  512. }
  513. // write remaining bits
  514. if ( nBitsLeft )
  515. {
  516. WriteUBitLong( *pIn, nBitsLeft, false );
  517. }
  518. return !IsOverflowed();
  519. }
  520. bool bf_write::WriteBitsFromBuffer( bf_read *pIn, int nBits )
  521. {
  522. // This could be optimized a little by
  523. while ( nBits > 32 )
  524. {
  525. WriteUBitLong( pIn->ReadUBitLong( 32 ), 32 );
  526. nBits -= 32;
  527. }
  528. WriteUBitLong( pIn->ReadUBitLong( nBits ), nBits );
  529. return !IsOverflowed() && !pIn->IsOverflowed();
  530. }
  531. void bf_write::WriteBitAngle( float fAngle, int numbits )
  532. {
  533. int d;
  534. unsigned int mask;
  535. unsigned int shift;
  536. shift = BitForBitnum(numbits);
  537. mask = shift - 1;
  538. d = (int)( (fAngle / 360.0) * shift );
  539. d &= mask;
  540. WriteUBitLong((unsigned int)d, numbits);
  541. }
  542. void bf_write::WriteBitCoordMP( const float f, EBitCoordType coordType )
  543. {
  544. #if defined( BB_PROFILING )
  545. VPROF( "bf_write::WriteBitCoordMP" );
  546. #endif
  547. bool bIntegral = ( coordType == kCW_Integral );
  548. bool bLowPrecision = ( coordType == kCW_LowPrecision );
  549. int signbit = (f <= -( bLowPrecision ? COORD_RESOLUTION_LOWPRECISION : COORD_RESOLUTION ));
  550. int intval = (int)abs(f);
  551. int fractval = bLowPrecision ?
  552. ( abs((int)(f*COORD_DENOMINATOR_LOWPRECISION)) & (COORD_DENOMINATOR_LOWPRECISION-1) ) :
  553. ( abs((int)(f*COORD_DENOMINATOR)) & (COORD_DENOMINATOR-1) );
  554. bool bInBounds = intval < (1 << COORD_INTEGER_BITS_MP );
  555. WriteOneBit( bInBounds );
  556. if ( bIntegral )
  557. {
  558. // Send the sign bit
  559. WriteOneBit( intval );
  560. if ( intval )
  561. {
  562. WriteOneBit( signbit );
  563. // Send the integer if we have one.
  564. // Adjust the integers from [1..MAX_COORD_VALUE] to [0..MAX_COORD_VALUE-1]
  565. intval--;
  566. if ( bInBounds )
  567. {
  568. WriteUBitLong( (unsigned int)intval, COORD_INTEGER_BITS_MP );
  569. }
  570. else
  571. {
  572. WriteUBitLong( (unsigned int)intval, COORD_INTEGER_BITS );
  573. }
  574. }
  575. }
  576. else
  577. {
  578. // Send the bit flags that indicate whether we have an integer part and/or a fraction part.
  579. WriteOneBit( intval );
  580. // Send the sign bit
  581. WriteOneBit( signbit );
  582. if ( intval )
  583. {
  584. // Adjust the integers from [1..MAX_COORD_VALUE] to [0..MAX_COORD_VALUE-1]
  585. intval--;
  586. if ( bInBounds )
  587. {
  588. WriteUBitLong( (unsigned int)intval, COORD_INTEGER_BITS_MP );
  589. }
  590. else
  591. {
  592. WriteUBitLong( (unsigned int)intval, COORD_INTEGER_BITS );
  593. }
  594. }
  595. WriteUBitLong( (unsigned int)fractval, bLowPrecision ? COORD_FRACTIONAL_BITS_MP_LOWPRECISION : COORD_FRACTIONAL_BITS );
  596. }
  597. }
  598. void bf_write::WriteBitCellCoord( const float f, int bits, EBitCoordType coordType )
  599. {
  600. #if defined( BB_PROFILING )
  601. VPROF( "bf_write::WriteBitCellCoord" );
  602. #endif
  603. Assert( f >= 0.0f ); // cell coords can't be negative
  604. Assert( f < ( 1 << bits ) );
  605. bool bIntegral = ( coordType == kCW_Integral );
  606. bool bLowPrecision = ( coordType == kCW_LowPrecision );
  607. int intval = (int)abs(f);
  608. int fractval = bLowPrecision ?
  609. ( abs((int)(f*COORD_DENOMINATOR_LOWPRECISION)) & (COORD_DENOMINATOR_LOWPRECISION-1) ) :
  610. ( abs((int)(f*COORD_DENOMINATOR)) & (COORD_DENOMINATOR-1) );
  611. if ( bIntegral )
  612. {
  613. WriteUBitLong( (unsigned int)intval, bits );
  614. }
  615. else
  616. {
  617. WriteUBitLong( (unsigned int)intval, bits );
  618. WriteUBitLong( (unsigned int)fractval, bLowPrecision ? COORD_FRACTIONAL_BITS_MP_LOWPRECISION : COORD_FRACTIONAL_BITS );
  619. }
  620. }
  621. void bf_write::WriteBitCoord(const float f)
  622. {
  623. #if defined( BB_PROFILING )
  624. VPROF( "bf_write::WriteBitCoord" );
  625. #endif
  626. int signbit = (f <= -COORD_RESOLUTION);
  627. int intval = (int)abs(f);
  628. int fractval = abs((int)(f*COORD_DENOMINATOR)) & (COORD_DENOMINATOR-1);
  629. // Send the bit flags that indicate whether we have an integer part and/or a fraction part.
  630. WriteOneBit( intval );
  631. WriteOneBit( fractval );
  632. if ( intval || fractval )
  633. {
  634. // Send the sign bit
  635. WriteOneBit( signbit );
  636. // Send the integer if we have one.
  637. if ( intval )
  638. {
  639. // Adjust the integers from [1..MAX_COORD_VALUE] to [0..MAX_COORD_VALUE-1]
  640. intval--;
  641. WriteUBitLong( (unsigned int)intval, COORD_INTEGER_BITS );
  642. }
  643. // Send the fraction if we have one
  644. if ( fractval )
  645. {
  646. WriteUBitLong( (unsigned int)fractval, COORD_FRACTIONAL_BITS );
  647. }
  648. }
  649. }
  650. void bf_write::WriteBitFloat(float val)
  651. {
  652. int32 intVal;
  653. Assert(sizeof(int32) == sizeof(float));
  654. Assert(sizeof(float) == 4);
  655. intVal = *((int32*)&val);
  656. WriteUBitLong( intVal, 32 );
  657. }
  658. void bf_write::WriteBitVec3Coord( const Vector& fa )
  659. {
  660. int xflag, yflag, zflag;
  661. xflag = (fa[0] >= COORD_RESOLUTION) || (fa[0] <= -COORD_RESOLUTION);
  662. yflag = (fa[1] >= COORD_RESOLUTION) || (fa[1] <= -COORD_RESOLUTION);
  663. zflag = (fa[2] >= COORD_RESOLUTION) || (fa[2] <= -COORD_RESOLUTION);
  664. WriteOneBit( xflag );
  665. WriteOneBit( yflag );
  666. WriteOneBit( zflag );
  667. if ( xflag )
  668. WriteBitCoord( fa[0] );
  669. if ( yflag )
  670. WriteBitCoord( fa[1] );
  671. if ( zflag )
  672. WriteBitCoord( fa[2] );
  673. }
  674. void bf_write::WriteBitNormal( float f )
  675. {
  676. int signbit = (f <= -NORMAL_RESOLUTION);
  677. // NOTE: Since +/-1 are valid values for a normal, I'm going to encode that as all ones
  678. unsigned int fractval = abs( (int)(f*NORMAL_DENOMINATOR) );
  679. // clamp..
  680. if (fractval > NORMAL_DENOMINATOR)
  681. fractval = NORMAL_DENOMINATOR;
  682. // Send the sign bit
  683. WriteOneBit( signbit );
  684. // Send the fractional component
  685. WriteUBitLong( fractval, NORMAL_FRACTIONAL_BITS );
  686. }
  687. void bf_write::WriteBitVec3Normal( const Vector& fa )
  688. {
  689. int xflag, yflag;
  690. xflag = (fa[0] >= NORMAL_RESOLUTION) || (fa[0] <= -NORMAL_RESOLUTION);
  691. yflag = (fa[1] >= NORMAL_RESOLUTION) || (fa[1] <= -NORMAL_RESOLUTION);
  692. WriteOneBit( xflag );
  693. WriteOneBit( yflag );
  694. if ( xflag )
  695. WriteBitNormal( fa[0] );
  696. if ( yflag )
  697. WriteBitNormal( fa[1] );
  698. // Write z sign bit
  699. int signbit = (fa[2] <= -NORMAL_RESOLUTION);
  700. WriteOneBit( signbit );
  701. }
  702. void bf_write::WriteBitAngles( const QAngle& fa )
  703. {
  704. // FIXME:
  705. Vector tmp( fa.x, fa.y, fa.z );
  706. WriteBitVec3Coord( tmp );
  707. }
  708. void bf_write::WriteChar(int val)
  709. {
  710. WriteSBitLong(val, sizeof(char) << 3);
  711. }
  712. void bf_write::WriteByte( unsigned int val )
  713. {
  714. WriteUBitLong(val, sizeof(unsigned char) << 3);
  715. }
  716. void bf_write::WriteShort(int val)
  717. {
  718. WriteSBitLong(val, sizeof(short) << 3);
  719. }
  720. void bf_write::WriteWord( unsigned int val )
  721. {
  722. WriteUBitLong(val, sizeof(unsigned short) << 3);
  723. }
  724. void bf_write::WriteLong(int32 val)
  725. {
  726. WriteSBitLong(val, sizeof(int32) << 3);
  727. }
  728. void bf_write::WriteLongLong(int64 val)
  729. {
  730. uint *pLongs = (uint*)&val;
  731. // Insert the two DWORDS according to network endian
  732. const short endianIndex = 0x0100;
  733. byte *idx = (byte*)&endianIndex;
  734. WriteUBitLong(pLongs[*idx++], sizeof(int32) << 3);
  735. WriteUBitLong(pLongs[*idx], sizeof(int32) << 3);
  736. }
  737. void bf_write::WriteFloat(float val)
  738. {
  739. // Pre-swap the float, since WriteBits writes raw data
  740. LittleFloat( &val, &val );
  741. WriteBits(&val, sizeof(val) << 3);
  742. }
  743. bool bf_write::WriteBytes( const void *pBuf, int nBytes )
  744. {
  745. return WriteBits(pBuf, nBytes << 3);
  746. }
  747. bool bf_write::WriteString(const char *pStr)
  748. {
  749. if(pStr)
  750. {
  751. do
  752. {
  753. WriteChar( *pStr );
  754. ++pStr;
  755. } while( *(pStr-1) != 0 );
  756. }
  757. else
  758. {
  759. WriteChar( 0 );
  760. }
  761. return !IsOverflowed();
  762. }
  763. bool bf_write::WriteString(const wchar_t *pStr)
  764. {
  765. if(pStr)
  766. {
  767. do
  768. {
  769. WriteShort( *pStr );
  770. ++pStr;
  771. } while( *(pStr-1) != 0 );
  772. }
  773. else
  774. {
  775. WriteShort( 0 );
  776. }
  777. return !IsOverflowed();
  778. }
  779. // ---------------------------------------------------------------------------------------- //
  780. // old_bf_read
  781. // ---------------------------------------------------------------------------------------- //
  782. old_bf_read::old_bf_read()
  783. {
  784. DEBUG_LINK_CHECK;
  785. m_pData = NULL;
  786. m_nDataBytes = 0;
  787. m_nDataBits = -1; // set to -1 so we overflow on any operation
  788. m_iCurBit = 0;
  789. m_bOverflow = false;
  790. m_bAssertOnOverflow = true;
  791. m_pDebugName = NULL;
  792. }
  793. old_bf_read::old_bf_read( const void *pData, int nBytes, int nBits )
  794. {
  795. m_bAssertOnOverflow = true;
  796. StartReading( pData, nBytes, 0, nBits );
  797. }
  798. old_bf_read::old_bf_read( const char *pDebugName, const void *pData, int nBytes, int nBits )
  799. {
  800. m_bAssertOnOverflow = true;
  801. m_pDebugName = pDebugName;
  802. StartReading( pData, nBytes, 0, nBits );
  803. }
  804. void old_bf_read::StartReading( const void *pData, int nBytes, int iStartBit, int nBits )
  805. {
  806. // Make sure we're dword aligned.
  807. Assert(((uintp)pData & 3) == 0);
  808. m_pData = (unsigned char*)pData;
  809. m_nDataBytes = nBytes;
  810. if ( nBits == -1 )
  811. {
  812. m_nDataBits = m_nDataBytes << 3;
  813. }
  814. else
  815. {
  816. Assert( nBits <= nBytes*8 );
  817. m_nDataBits = nBits;
  818. }
  819. m_iCurBit = iStartBit;
  820. m_bOverflow = false;
  821. }
  822. void old_bf_read::Reset()
  823. {
  824. m_iCurBit = 0;
  825. m_bOverflow = false;
  826. }
  827. void old_bf_read::SetAssertOnOverflow( bool bAssert )
  828. {
  829. m_bAssertOnOverflow = bAssert;
  830. }
  831. const char* old_bf_read::GetDebugName()
  832. {
  833. return m_pDebugName;
  834. }
  835. void old_bf_read::SetDebugName( const char *pName )
  836. {
  837. m_pDebugName = pName;
  838. }
  839. unsigned int old_bf_read::CheckReadUBitLong(int numbits)
  840. {
  841. // Ok, just read bits out.
  842. int i, nBitValue;
  843. unsigned int r = 0;
  844. for(i=0; i < numbits; i++)
  845. {
  846. nBitValue = ReadOneBitNoCheck();
  847. r |= nBitValue << i;
  848. }
  849. m_iCurBit -= numbits;
  850. return r;
  851. }
  852. void old_bf_read::ReadBits(void *pOutData, int nBits)
  853. {
  854. #if defined( BB_PROFILING )
  855. VPROF( "bf_write::ReadBits" );
  856. #endif
  857. unsigned char *pOut = (unsigned char*)pOutData;
  858. int nBitsLeft = nBits;
  859. // align output to dword boundary
  860. while( ((uintp)pOut & 3) != 0 && nBitsLeft >= 8 )
  861. {
  862. *pOut = (unsigned char)ReadUBitLong(8);
  863. ++pOut;
  864. nBitsLeft -= 8;
  865. }
  866. // X360TBD: Can't read dwords in ReadBits because they'll get swapped
  867. if ( IsPC() )
  868. {
  869. // read dwords
  870. while ( nBitsLeft >= 32 )
  871. {
  872. *((uint32*)pOut) = ReadUBitLong(32);
  873. pOut += sizeof(uint32);
  874. nBitsLeft -= 32;
  875. }
  876. }
  877. // read remaining bytes
  878. while ( nBitsLeft >= 8 )
  879. {
  880. *pOut = ReadUBitLong(8);
  881. ++pOut;
  882. nBitsLeft -= 8;
  883. }
  884. // read remaining bits
  885. if ( nBitsLeft )
  886. {
  887. *pOut = ReadUBitLong(nBitsLeft);
  888. }
  889. }
  890. float old_bf_read::ReadBitAngle( int numbits )
  891. {
  892. float fReturn;
  893. int i;
  894. float shift;
  895. shift = (float)( BitForBitnum(numbits) );
  896. i = ReadUBitLong( numbits );
  897. fReturn = (float)i * (360.0 / shift);
  898. return fReturn;
  899. }
  900. unsigned int old_bf_read::PeekUBitLong( int numbits )
  901. {
  902. unsigned int r;
  903. int i, nBitValue;
  904. #ifdef BIT_VERBOSE
  905. int nShifts = numbits;
  906. #endif
  907. old_bf_read savebf;
  908. savebf = *this; // Save current state info
  909. r = 0;
  910. for(i=0; i < numbits; i++)
  911. {
  912. nBitValue = ReadOneBit();
  913. // Append to current stream
  914. if ( nBitValue )
  915. {
  916. r |= BitForBitnum(i);
  917. }
  918. }
  919. *this = savebf;
  920. #ifdef BIT_VERBOSE
  921. Con_Printf( "PeekBitLong: %i %i\n", nShifts, (unsigned int)r );
  922. #endif
  923. return r;
  924. }
  925. // Append numbits least significant bits from data to the current bit stream
  926. int old_bf_read::ReadSBitLong( int numbits )
  927. {
  928. int r, sign;
  929. r = ReadUBitLong(numbits - 1);
  930. // Note: it does this wierdness here so it's bit-compatible with regular integer data in the buffer.
  931. // (Some old code writes direct integers right into the buffer).
  932. sign = ReadOneBit();
  933. if(sign)
  934. r = -((BitForBitnum(numbits-1)) - r);
  935. return r;
  936. }
  937. const byte g_BitMask[8] = {0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80};
  938. const byte g_TrailingMask[8] = {0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80};
  939. inline int old_bf_read::CountRunOfZeros()
  940. {
  941. int bits = 0;
  942. if ( m_iCurBit + 32 < m_nDataBits )
  943. {
  944. #if !FAST_BIT_SCAN
  945. while (true)
  946. {
  947. int value = (m_pData[m_iCurBit >> 3] & g_BitMask[m_iCurBit & 7]);
  948. ++m_iCurBit;
  949. if ( value )
  950. return bits;
  951. ++bits;
  952. }
  953. #else
  954. while (true)
  955. {
  956. int value = (m_pData[m_iCurBit >> 3] & g_TrailingMask[m_iCurBit & 7]);
  957. if ( !value )
  958. {
  959. int zeros = (8-(m_iCurBit&7));
  960. bits += zeros;
  961. m_iCurBit += zeros;
  962. }
  963. else
  964. {
  965. int zeros = CountTrailingZeros(value) - (m_iCurBit & 7);
  966. m_iCurBit += zeros + 1;
  967. bits += zeros;
  968. return bits;
  969. }
  970. }
  971. #endif
  972. }
  973. else
  974. {
  975. while ( ReadOneBit() == 0 )
  976. bits++;
  977. }
  978. return bits;
  979. }
  980. unsigned int old_bf_read::ReadUBitVar()
  981. {
  982. unsigned int ret = ReadUBitLong( 6 );
  983. switch( ret & ( 16 | 32 ) )
  984. {
  985. case 16:
  986. ret = ( ret & 15 ) | ( ReadUBitLong( 4 ) << 4 );
  987. Assert( ret >= 16);
  988. break;
  989. case 32:
  990. ret = ( ret & 15 ) | ( ReadUBitLong( 8 ) << 4 );
  991. Assert( ret >= 256);
  992. break;
  993. case 48:
  994. ret = ( ret & 15 ) | ( ReadUBitLong( 32 - 4 ) << 4 );
  995. Assert( ret >= 4096 );
  996. break;
  997. }
  998. return ret;
  999. }
  1000. // Read 1-5 bytes in order to extract a 32-bit unsigned value from the
  1001. // stream. 7 data bits are extracted from each byte with the 8th bit used
  1002. // to indicate whether the loop should continue.
  1003. // This allows variable size numbers to be stored with tolerable
  1004. // efficiency. Numbers sizes that can be stored for various numbers of
  1005. // encoded bits are:
  1006. // 8-bits: 0-127
  1007. // 16-bits: 128-16383
  1008. // 24-bits: 16384-2097151
  1009. // 32-bits: 2097152-268435455
  1010. // 40-bits: 268435456-0xFFFFFFFF
  1011. uint32 old_bf_read::ReadVarInt32()
  1012. {
  1013. uint32 result = 0;
  1014. int count = 0;
  1015. uint32 b;
  1016. do
  1017. {
  1018. if ( count == bitbuf::kMaxVarint32Bytes )
  1019. {
  1020. // If we get here it means that the fifth bit had its
  1021. // high bit set, which implies corrupt data.
  1022. Assert( 0 );
  1023. return result;
  1024. }
  1025. b = ReadUBitLong( 8 );
  1026. result |= (b & 0x7F) << (7 * count);
  1027. ++count;
  1028. } while (b & 0x80);
  1029. return result;
  1030. }
  1031. uint64 old_bf_read::ReadVarInt64()
  1032. {
  1033. uint64 result = 0;
  1034. int count = 0;
  1035. uint64 b;
  1036. do
  1037. {
  1038. if ( count == bitbuf::kMaxVarintBytes )
  1039. {
  1040. return result;
  1041. }
  1042. b = ReadUBitLong( 8 );
  1043. result |= static_cast<uint64>(b & 0x7F) << (7 * count);
  1044. ++count;
  1045. } while (b & 0x80);
  1046. return result;
  1047. }
  1048. unsigned int old_bf_read::ReadBitLong(int numbits, bool bSigned)
  1049. {
  1050. if(bSigned)
  1051. return (unsigned int)ReadSBitLong(numbits);
  1052. else
  1053. return ReadUBitLong(numbits);
  1054. }
  1055. // Basic Coordinate Routines (these contain bit-field size AND fixed point scaling constants)
  1056. float old_bf_read::ReadBitCoord (void)
  1057. {
  1058. #if defined( BB_PROFILING )
  1059. VPROF( "bf_write::ReadBitCoord" );
  1060. #endif
  1061. int intval=0,fractval=0,signbit=0;
  1062. float value = 0.0;
  1063. // Read the required integer and fraction flags
  1064. intval = ReadOneBit();
  1065. fractval = ReadOneBit();
  1066. // If we got either parse them, otherwise it's a zero.
  1067. if ( intval || fractval )
  1068. {
  1069. // Read the sign bit
  1070. signbit = ReadOneBit();
  1071. // If there's an integer, read it in
  1072. if ( intval )
  1073. {
  1074. // Adjust the integers from [0..MAX_COORD_VALUE-1] to [1..MAX_COORD_VALUE]
  1075. intval = ReadUBitLong( COORD_INTEGER_BITS ) + 1;
  1076. }
  1077. // If there's a fraction, read it in
  1078. if ( fractval )
  1079. {
  1080. fractval = ReadUBitLong( COORD_FRACTIONAL_BITS );
  1081. }
  1082. // Calculate the correct floating point value
  1083. value = intval + ((float)fractval * COORD_RESOLUTION);
  1084. // Fixup the sign if negative.
  1085. if ( signbit )
  1086. value = -value;
  1087. }
  1088. return value;
  1089. }
  1090. float old_bf_read::ReadBitCoordMP( EBitCoordType coordType )
  1091. {
  1092. #if defined( BB_PROFILING )
  1093. VPROF( "bf_write::ReadBitCoordMP" );
  1094. #endif
  1095. bool bIntegral = ( coordType == kCW_Integral );
  1096. bool bLowPrecision = ( coordType == kCW_LowPrecision );
  1097. int intval=0,fractval=0,signbit=0;
  1098. float value = 0.0;
  1099. bool bInBounds = ReadOneBit() ? true : false;
  1100. if ( bIntegral )
  1101. {
  1102. // Read the required integer and fraction flags
  1103. intval = ReadOneBit();
  1104. // If we got either parse them, otherwise it's a zero.
  1105. if ( intval )
  1106. {
  1107. // Read the sign bit
  1108. signbit = ReadOneBit();
  1109. // If there's an integer, read it in
  1110. // Adjust the integers from [0..MAX_COORD_VALUE-1] to [1..MAX_COORD_VALUE]
  1111. if ( bInBounds )
  1112. {
  1113. value = ReadUBitLong( COORD_INTEGER_BITS_MP ) + 1;
  1114. }
  1115. else
  1116. {
  1117. value = ReadUBitLong( COORD_INTEGER_BITS ) + 1;
  1118. }
  1119. }
  1120. }
  1121. else
  1122. {
  1123. // Read the required integer and fraction flags
  1124. intval = ReadOneBit();
  1125. // Read the sign bit
  1126. signbit = ReadOneBit();
  1127. // If we got either parse them, otherwise it's a zero.
  1128. if ( intval )
  1129. {
  1130. if ( bInBounds )
  1131. {
  1132. intval = ReadUBitLong( COORD_INTEGER_BITS_MP ) + 1;
  1133. }
  1134. else
  1135. {
  1136. intval = ReadUBitLong( COORD_INTEGER_BITS ) + 1;
  1137. }
  1138. }
  1139. // If there's a fraction, read it in
  1140. fractval = ReadUBitLong( bLowPrecision ? COORD_FRACTIONAL_BITS_MP_LOWPRECISION : COORD_FRACTIONAL_BITS );
  1141. // Calculate the correct floating point value
  1142. value = intval + ((float)fractval * ( bLowPrecision ? COORD_RESOLUTION_LOWPRECISION : COORD_RESOLUTION ) );
  1143. }
  1144. // Fixup the sign if negative.
  1145. if ( signbit )
  1146. value = -value;
  1147. return value;
  1148. }
  1149. float old_bf_read::ReadBitCellCoord( int bits, EBitCoordType coordType )
  1150. {
  1151. #if defined( BB_PROFILING )
  1152. VPROF( "bf_write::ReadBitCoordMP" );
  1153. #endif
  1154. bool bIntegral = ( coordType == kCW_Integral );
  1155. bool bLowPrecision = ( coordType == kCW_LowPrecision );
  1156. int intval=0,fractval=0;
  1157. float value = 0.0;
  1158. if ( bIntegral )
  1159. {
  1160. value = ReadUBitLong( bits );
  1161. }
  1162. else
  1163. {
  1164. intval = ReadUBitLong( bits );
  1165. // If there's a fraction, read it in
  1166. fractval = ReadUBitLong( bLowPrecision ? COORD_FRACTIONAL_BITS_MP_LOWPRECISION : COORD_FRACTIONAL_BITS );
  1167. // Calculate the correct floating point value
  1168. value = intval + ((float)fractval * ( bLowPrecision ? COORD_RESOLUTION_LOWPRECISION : COORD_RESOLUTION ) );
  1169. }
  1170. return value;
  1171. }
  1172. void old_bf_read::ReadBitVec3Coord( Vector& fa )
  1173. {
  1174. int xflag, yflag, zflag;
  1175. // This vector must be initialized! Otherwise, If any of the flags aren't set,
  1176. // the corresponding component will not be read and will be stack garbage.
  1177. fa.Init( 0, 0, 0 );
  1178. xflag = ReadOneBit();
  1179. yflag = ReadOneBit();
  1180. zflag = ReadOneBit();
  1181. if ( xflag )
  1182. fa[0] = ReadBitCoord();
  1183. if ( yflag )
  1184. fa[1] = ReadBitCoord();
  1185. if ( zflag )
  1186. fa[2] = ReadBitCoord();
  1187. }
  1188. float old_bf_read::ReadBitNormal (void)
  1189. {
  1190. // Read the sign bit
  1191. int signbit = ReadOneBit();
  1192. // Read the fractional part
  1193. unsigned int fractval = ReadUBitLong( NORMAL_FRACTIONAL_BITS );
  1194. // Calculate the correct floating point value
  1195. float value = (float)fractval * NORMAL_RESOLUTION;
  1196. // Fixup the sign if negative.
  1197. if ( signbit )
  1198. value = -value;
  1199. return value;
  1200. }
  1201. void old_bf_read::ReadBitVec3Normal( Vector& fa )
  1202. {
  1203. int xflag = ReadOneBit();
  1204. int yflag = ReadOneBit();
  1205. if (xflag)
  1206. fa[0] = ReadBitNormal();
  1207. else
  1208. fa[0] = 0.0f;
  1209. if (yflag)
  1210. fa[1] = ReadBitNormal();
  1211. else
  1212. fa[1] = 0.0f;
  1213. // The first two imply the third (but not its sign)
  1214. int znegative = ReadOneBit();
  1215. float fafafbfb = fa[0] * fa[0] + fa[1] * fa[1];
  1216. if (fafafbfb < 1.0f)
  1217. fa[2] = sqrt( 1.0f - fafafbfb );
  1218. else
  1219. fa[2] = 0.0f;
  1220. if (znegative)
  1221. fa[2] = -fa[2];
  1222. }
  1223. void old_bf_read::ReadBitAngles( QAngle& fa )
  1224. {
  1225. Vector tmp;
  1226. ReadBitVec3Coord( tmp );
  1227. fa.Init( tmp.x, tmp.y, tmp.z );
  1228. }
  1229. int old_bf_read::ReadChar()
  1230. {
  1231. return ReadSBitLong(sizeof(char) << 3);
  1232. }
  1233. int old_bf_read::ReadByte()
  1234. {
  1235. return ReadUBitLong(sizeof(unsigned char) << 3);
  1236. }
  1237. int old_bf_read::ReadShort()
  1238. {
  1239. return ReadSBitLong(sizeof(short) << 3);
  1240. }
  1241. int old_bf_read::ReadWord()
  1242. {
  1243. return ReadUBitLong(sizeof(unsigned short) << 3);
  1244. }
  1245. int32 old_bf_read::ReadLong()
  1246. {
  1247. return ReadSBitLong(sizeof(int32) << 3);
  1248. }
  1249. int64 old_bf_read::ReadLongLong()
  1250. {
  1251. int64 retval;
  1252. uint *pLongs = (uint*)&retval;
  1253. // Read the two DWORDs according to network endian
  1254. const short endianIndex = 0x0100;
  1255. byte *idx = (byte*)&endianIndex;
  1256. pLongs[*idx++] = ReadUBitLong(sizeof(int32) << 3);
  1257. pLongs[*idx] = ReadUBitLong(sizeof(int32) << 3);
  1258. return retval;
  1259. }
  1260. float old_bf_read::ReadFloat()
  1261. {
  1262. float ret;
  1263. Assert( sizeof(ret) == 4 );
  1264. ReadBits(&ret, 32);
  1265. // Swap the float, since ReadBits reads raw data
  1266. LittleFloat( &ret, &ret );
  1267. return ret;
  1268. }
  1269. bool old_bf_read::ReadBytes(void *pOut, int nBytes)
  1270. {
  1271. ReadBits(pOut, nBytes << 3);
  1272. return !IsOverflowed();
  1273. }
  1274. bool old_bf_read::ReadString( char *pStr, int maxLen, bool bLine, int *pOutNumChars )
  1275. {
  1276. Assert( maxLen != 0 );
  1277. bool bTooSmall = false;
  1278. int iChar = 0;
  1279. while(1)
  1280. {
  1281. char val = ReadChar();
  1282. if ( val == 0 )
  1283. break;
  1284. else if ( bLine && val == '\n' )
  1285. break;
  1286. if ( iChar < (maxLen-1) )
  1287. {
  1288. pStr[iChar] = val;
  1289. ++iChar;
  1290. }
  1291. else
  1292. {
  1293. bTooSmall = true;
  1294. }
  1295. }
  1296. // Make sure it's null-terminated.
  1297. Assert( iChar < maxLen );
  1298. pStr[iChar] = 0;
  1299. if ( pOutNumChars )
  1300. *pOutNumChars = iChar;
  1301. return !IsOverflowed() && !bTooSmall;
  1302. }
  1303. bool old_bf_read::ReadWString( wchar_t *pStr, int maxLen, bool bLine, int *pOutNumChars )
  1304. {
  1305. Assert( maxLen != 0 );
  1306. bool bTooSmall = false;
  1307. int iChar = 0;
  1308. while(1)
  1309. {
  1310. wchar val = ReadShort();
  1311. if ( val == 0 )
  1312. break;
  1313. else if ( bLine && val == L'\n' )
  1314. break;
  1315. if ( iChar < (maxLen-1) )
  1316. {
  1317. pStr[iChar] = val;
  1318. ++iChar;
  1319. }
  1320. else
  1321. {
  1322. bTooSmall = true;
  1323. }
  1324. }
  1325. // Make sure it's null-terminated.
  1326. Assert( iChar < maxLen );
  1327. pStr[iChar] = 0;
  1328. if ( pOutNumChars )
  1329. *pOutNumChars = iChar;
  1330. return !IsOverflowed() && !bTooSmall;
  1331. }
  1332. char* old_bf_read::ReadAndAllocateString( bool *pOverflow )
  1333. {
  1334. char str[2048];
  1335. int nChars;
  1336. bool bOverflow = !ReadString( str, sizeof( str ), false, &nChars );
  1337. if ( pOverflow )
  1338. *pOverflow = bOverflow;
  1339. // Now copy into the output and return it;
  1340. char *pRet = new char[ nChars + 1 ];
  1341. for ( int i=0; i <= nChars; i++ )
  1342. pRet[i] = str[i];
  1343. return pRet;
  1344. }
  1345. void old_bf_read::ExciseBits( int startbit, int bitstoremove )
  1346. {
  1347. int endbit = startbit + bitstoremove;
  1348. int remaining_to_end = m_nDataBits - endbit;
  1349. bf_write temp;
  1350. temp.StartWriting( (void *)m_pData, m_nDataBits << 3, startbit );
  1351. Seek( endbit );
  1352. for ( int i = 0; i < remaining_to_end; i++ )
  1353. {
  1354. temp.WriteOneBit( ReadOneBit() );
  1355. }
  1356. Seek( startbit );
  1357. m_nDataBits -= bitstoremove;
  1358. m_nDataBytes = m_nDataBits >> 3;
  1359. }