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.

1591 lines
38 KiB

  1. //========= Copyright (c) 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //===========================================================================//
  8. // NOTE: old_bf_read is guaranteed to return zeros if it overflows.
  9. #ifndef BITBUF_H
  10. #define BITBUF_H
  11. #ifdef _WIN32
  12. #pragma once
  13. #endif
  14. #include "mathlib/mathlib.h"
  15. #include "mathlib/vector.h"
  16. #include "basetypes.h"
  17. #include "tier0/platform.h"
  18. #include "tier0/dbg.h"
  19. //-----------------------------------------------------------------------------
  20. // Forward declarations.
  21. //-----------------------------------------------------------------------------
  22. class Vector;
  23. class QAngle;
  24. //-----------------------------------------------------------------------------
  25. // You can define a handler function that will be called in case of
  26. // out-of-range values and overruns here.
  27. //
  28. // NOTE: the handler is only called in debug mode.
  29. //
  30. // Call SetBitBufErrorHandler to install a handler.
  31. //-----------------------------------------------------------------------------
  32. typedef enum
  33. {
  34. BITBUFERROR_VALUE_OUT_OF_RANGE=0, // Tried to write a value with too few bits.
  35. BITBUFERROR_BUFFER_OVERRUN, // Was about to overrun a buffer.
  36. BITBUFERROR_NUM_ERRORS
  37. } BitBufErrorType;
  38. typedef void (*BitBufErrorHandler)( BitBufErrorType errorType, const char *pDebugName );
  39. #if defined( _DEBUG )
  40. extern void InternalBitBufErrorHandler( BitBufErrorType errorType, const char *pDebugName );
  41. #define CallErrorHandler( errorType, pDebugName ) InternalBitBufErrorHandler( errorType, pDebugName );
  42. #else
  43. #define CallErrorHandler( errorType, pDebugName )
  44. #endif
  45. // Use this to install the error handler. Call with NULL to uninstall your error handler.
  46. void SetBitBufErrorHandler( BitBufErrorHandler fn );
  47. //-----------------------------------------------------------------------------
  48. // Helpers.
  49. //-----------------------------------------------------------------------------
  50. inline int BitByte( int bits )
  51. {
  52. // return PAD_NUMBER( bits, 8 ) >> 3;
  53. return (bits + 7) >> 3;
  54. }
  55. //-----------------------------------------------------------------------------
  56. enum EBitCoordType
  57. {
  58. kCW_None,
  59. kCW_LowPrecision,
  60. kCW_Integral
  61. };
  62. //-----------------------------------------------------------------------------
  63. // namespaced helpers
  64. //-----------------------------------------------------------------------------
  65. namespace bitbuf
  66. {
  67. // ZigZag Transform: Encodes signed integers so that they can be
  68. // effectively used with varint encoding.
  69. //
  70. // varint operates on unsigned integers, encoding smaller numbers into
  71. // fewer bytes. If you try to use it on a signed integer, it will treat
  72. // this number as a very large unsigned integer, which means that even
  73. // small signed numbers like -1 will take the maximum number of bytes
  74. // (10) to encode. ZigZagEncode() maps signed integers to unsigned
  75. // in such a way that those with a small absolute value will have smaller
  76. // encoded values, making them appropriate for encoding using varint.
  77. //
  78. // int32 -> uint32
  79. // -------------------------
  80. // 0 -> 0
  81. // -1 -> 1
  82. // 1 -> 2
  83. // -2 -> 3
  84. // ... -> ...
  85. // 2147483647 -> 4294967294
  86. // -2147483648 -> 4294967295
  87. //
  88. // >> encode >>
  89. // << decode <<
  90. inline uint32 ZigZagEncode32(int32 n)
  91. {
  92. // Note: the right-shift must be arithmetic
  93. return(n << 1) ^ (n >> 31);
  94. }
  95. inline int32 ZigZagDecode32(uint32 n)
  96. {
  97. return(n >> 1) ^ -static_cast<int32>(n & 1);
  98. }
  99. inline uint64 ZigZagEncode64(int64 n)
  100. {
  101. // Note: the right-shift must be arithmetic
  102. return(n << 1) ^ (n >> 63);
  103. }
  104. inline int64 ZigZagDecode64(uint64 n)
  105. {
  106. return(n >> 1) ^ -static_cast<int64>(n & 1);
  107. }
  108. const int kMaxVarintBytes = 10;
  109. const int kMaxVarint32Bytes = 5;
  110. }
  111. //-----------------------------------------------------------------------------
  112. // Used for serialization
  113. //-----------------------------------------------------------------------------
  114. class bf_write
  115. {
  116. public:
  117. bf_write();
  118. // nMaxBits can be used as the number of bits in the buffer.
  119. // It must be <= nBytes*8. If you leave it at -1, then it's set to nBytes * 8.
  120. bf_write( void *pData, int nBytes, int nMaxBits = -1 );
  121. bf_write( const char *pDebugName, void *pData, int nBytes, int nMaxBits = -1 );
  122. // Start writing to the specified buffer.
  123. // nMaxBits can be used as the number of bits in the buffer.
  124. // It must be <= nBytes*8. If you leave it at -1, then it's set to nBytes * 8.
  125. void StartWriting( void *pData, int nBytes, int iStartBit = 0, int nMaxBits = -1 );
  126. // Restart buffer writing.
  127. void Reset();
  128. // Get the base pointer.
  129. unsigned char* GetBasePointer() { return m_pData; }
  130. // Enable or disable assertion on overflow. 99% of the time, it's a bug that we need to catch,
  131. // but there may be the occasional buffer that is allowed to overflow gracefully.
  132. void SetAssertOnOverflow( bool bAssert );
  133. // This can be set to assign a name that gets output if the buffer overflows.
  134. const char* GetDebugName();
  135. void SetDebugName( const char *pDebugName );
  136. // Seek to a specific position.
  137. public:
  138. void SeekToBit( int bitPos );
  139. // Bit functions.
  140. public:
  141. void WriteOneBit(int nValue);
  142. void WriteOneBitNoCheck(int nValue);
  143. void WriteOneBitAt( int iBit, int nValue );
  144. // Write signed or unsigned. Range is only checked in debug.
  145. void WriteUBitLong( unsigned int data, int numbits, bool bCheckRange=true );
  146. void WriteSBitLong( int data, int numbits );
  147. // Tell it whether or not the data is unsigned. If it's signed,
  148. // cast to unsigned before passing in (it will cast back inside).
  149. void WriteBitLong(unsigned int data, int numbits, bool bSigned);
  150. // Write a list of bits in.
  151. bool WriteBits(const void *pIn, int nBits);
  152. // writes an unsigned integer with variable bit length
  153. void WriteUBitVar( unsigned int data );
  154. // writes a varint encoded integer
  155. void WriteVarInt32( uint32 data );
  156. void WriteVarInt64( uint64 data );
  157. void WriteSignedVarInt32( int32 data );
  158. void WriteSignedVarInt64( int64 data );
  159. int ByteSizeVarInt32( uint32 data );
  160. int ByteSizeVarInt64( uint64 data );
  161. int ByteSizeSignedVarInt32( int32 data );
  162. int ByteSizeSignedVarInt64( int64 data );
  163. // Copy the bits straight out of pIn. This seeks pIn forward by nBits.
  164. // Returns an error if this buffer or the read buffer overflows.
  165. bool WriteBitsFromBuffer( class bf_read *pIn, int nBits );
  166. void WriteBitAngle( float fAngle, int numbits );
  167. void WriteBitCoord (const float f);
  168. void WriteBitCoordMP( const float f, EBitCoordType coordType );
  169. void WriteBitCellCoord( const float f, int bits, EBitCoordType coordType );
  170. void WriteBitFloat(float val);
  171. void WriteBitVec3Coord( const Vector& fa );
  172. void WriteBitNormal( float f );
  173. void WriteBitVec3Normal( const Vector& fa );
  174. void WriteBitAngles( const QAngle& fa );
  175. // Byte functions.
  176. public:
  177. void WriteChar(int val);
  178. void WriteByte( unsigned int val );
  179. void WriteShort(int val);
  180. void WriteWord( unsigned int val );
  181. void WriteLong(int32 val);
  182. void WriteLongLong(int64 val);
  183. void WriteFloat(float val);
  184. bool WriteBytes( const void *pBuf, int nBytes );
  185. // Returns false if it overflows the buffer.
  186. bool WriteString(const char *pStr);
  187. bool WriteString(const wchar_t *pStr);
  188. // Status.
  189. public:
  190. // How many bytes are filled in?
  191. int GetNumBytesWritten() const;
  192. int GetNumBitsWritten() const;
  193. int GetMaxNumBits() const;
  194. int GetNumBitsLeft() const;
  195. int GetNumBytesLeft() const;
  196. unsigned char* GetData();
  197. const unsigned char* GetData() const;
  198. // Has the buffer overflowed?
  199. bool CheckForOverflow(int nBits);
  200. inline bool IsOverflowed() const {return m_bOverflow;}
  201. inline void SetOverflowFlag();
  202. public:
  203. // The current buffer.
  204. unsigned char* m_pData;
  205. int m_nDataBytes;
  206. int m_nDataBits;
  207. // Where we are in the buffer.
  208. int m_iCurBit;
  209. private:
  210. // Errors?
  211. bool m_bOverflow;
  212. bool m_bAssertOnOverflow;
  213. const char *m_pDebugName;
  214. };
  215. //-----------------------------------------------------------------------------
  216. // Inlined methods
  217. //-----------------------------------------------------------------------------
  218. // How many bytes are filled in?
  219. inline int bf_write::GetNumBytesWritten() const
  220. {
  221. return BitByte(m_iCurBit);
  222. }
  223. inline int bf_write::GetNumBitsWritten() const
  224. {
  225. return m_iCurBit;
  226. }
  227. inline int bf_write::GetMaxNumBits() const
  228. {
  229. return m_nDataBits;
  230. }
  231. inline int bf_write::GetNumBitsLeft() const
  232. {
  233. return m_nDataBits - m_iCurBit;
  234. }
  235. inline int bf_write::GetNumBytesLeft() const
  236. {
  237. return GetNumBitsLeft() >> 3;
  238. }
  239. inline unsigned char* bf_write::GetData()
  240. {
  241. return m_pData;
  242. }
  243. inline const unsigned char* bf_write::GetData() const
  244. {
  245. return m_pData;
  246. }
  247. inline bool bf_write::CheckForOverflow(int nBits)
  248. {
  249. if ( m_iCurBit + nBits > m_nDataBits )
  250. {
  251. SetOverflowFlag();
  252. CallErrorHandler( BITBUFERROR_BUFFER_OVERRUN, GetDebugName() );
  253. }
  254. return m_bOverflow;
  255. }
  256. inline void bf_write::SetOverflowFlag()
  257. {
  258. if ( m_bAssertOnOverflow )
  259. {
  260. Assert( false );
  261. }
  262. m_bOverflow = true;
  263. }
  264. inline void bf_write::WriteOneBitNoCheck(int nValue)
  265. {
  266. if(nValue)
  267. m_pData[m_iCurBit >> 3] |= (1 << (m_iCurBit & 7));
  268. else
  269. m_pData[m_iCurBit >> 3] &= ~(1 << (m_iCurBit & 7));
  270. ++m_iCurBit;
  271. }
  272. inline void bf_write::WriteOneBit(int nValue)
  273. {
  274. if( !CheckForOverflow(1) )
  275. WriteOneBitNoCheck( nValue );
  276. }
  277. inline void bf_write::WriteOneBitAt( int iBit, int nValue )
  278. {
  279. if( iBit+1 > m_nDataBits )
  280. {
  281. SetOverflowFlag();
  282. CallErrorHandler( BITBUFERROR_BUFFER_OVERRUN, GetDebugName() );
  283. return;
  284. }
  285. if( nValue )
  286. m_pData[iBit >> 3] |= (1 << (iBit & 7));
  287. else
  288. m_pData[iBit >> 3] &= ~(1 << (iBit & 7));
  289. }
  290. inline void bf_write::WriteUBitLong( unsigned int curData, int numbits, bool bCheckRange )
  291. {
  292. #ifdef _DEBUG
  293. // Make sure it doesn't overflow.
  294. if ( bCheckRange && numbits < 32 )
  295. {
  296. if ( curData >= (uint32)(1 << numbits) )
  297. {
  298. CallErrorHandler( BITBUFERROR_VALUE_OUT_OF_RANGE, GetDebugName() );
  299. }
  300. }
  301. Assert( numbits >= 0 && numbits <= 32 );
  302. #endif
  303. extern uint32 g_BitWriteMasks[32][33];
  304. // Bounds checking..
  305. if ((m_iCurBit+numbits) > m_nDataBits)
  306. {
  307. m_iCurBit = m_nDataBits;
  308. SetOverflowFlag();
  309. CallErrorHandler( BITBUFERROR_BUFFER_OVERRUN, GetDebugName() );
  310. return;
  311. }
  312. int nBitsLeft = numbits;
  313. int iCurBit = m_iCurBit;
  314. // Mask in a dword.
  315. unsigned int iDWord = iCurBit >> 5;
  316. Assert( (iDWord*4 + sizeof(int32)) <= (unsigned int)m_nDataBytes );
  317. uint32 iCurBitMasked = iCurBit & 31;
  318. uint32 dword = LoadLittleDWord( (uint32*)m_pData, iDWord );
  319. dword &= g_BitWriteMasks[iCurBitMasked][nBitsLeft];
  320. dword |= curData << iCurBitMasked;
  321. // write to stream (lsb to msb ) properly
  322. StoreLittleDWord( (uint32*)m_pData, iDWord, dword );
  323. // Did it span a dword?
  324. int nBitsWritten = 32 - iCurBitMasked;
  325. if ( nBitsWritten < nBitsLeft )
  326. {
  327. nBitsLeft -= nBitsWritten;
  328. curData >>= nBitsWritten;
  329. // read from stream (lsb to msb) properly
  330. dword = LoadLittleDWord( (uint32*)m_pData, iDWord+1 );
  331. dword &= g_BitWriteMasks[0][nBitsLeft];
  332. dword |= curData;
  333. // write to stream (lsb to msb) properly
  334. StoreLittleDWord( (uint32*)m_pData, iDWord+1, dword );
  335. }
  336. m_iCurBit += numbits;
  337. }
  338. //-----------------------------------------------------------------------------
  339. // This is useful if you just want a buffer to write into on the stack.
  340. //-----------------------------------------------------------------------------
  341. template<int SIZE>
  342. class old_bf_write_static : public bf_write
  343. {
  344. public:
  345. inline old_bf_write_static() : bf_write(m_StaticData, SIZE) {}
  346. char m_StaticData[SIZE];
  347. };
  348. //-----------------------------------------------------------------------------
  349. // Used for unserialization
  350. //-----------------------------------------------------------------------------
  351. class old_bf_read
  352. {
  353. public:
  354. old_bf_read();
  355. // nMaxBits can be used as the number of bits in the buffer.
  356. // It must be <= nBytes*8. If you leave it at -1, then it's set to nBytes * 8.
  357. old_bf_read( const void *pData, int nBytes, int nBits = -1 );
  358. old_bf_read( const char *pDebugName, const void *pData, int nBytes, int nBits = -1 );
  359. // Start reading from the specified buffer.
  360. // pData's start address must be dword-aligned.
  361. // nMaxBits can be used as the number of bits in the buffer.
  362. // It must be <= nBytes*8. If you leave it at -1, then it's set to nBytes * 8.
  363. void StartReading( const void *pData, int nBytes, int iStartBit = 0, int nBits = -1 );
  364. // Restart buffer reading.
  365. void Reset();
  366. // Enable or disable assertion on overflow. 99% of the time, it's a bug that we need to catch,
  367. // but there may be the occasional buffer that is allowed to overflow gracefully.
  368. void SetAssertOnOverflow( bool bAssert );
  369. // This can be set to assign a name that gets output if the buffer overflows.
  370. const char* GetDebugName();
  371. void SetDebugName( const char *pName );
  372. void ExciseBits( int startbit, int bitstoremove );
  373. // Bit functions.
  374. public:
  375. // Returns 0 or 1.
  376. int ReadOneBit();
  377. protected:
  378. unsigned int CheckReadUBitLong(int numbits); // For debugging.
  379. int ReadOneBitNoCheck(); // Faster version, doesn't check bounds and is inlined.
  380. bool CheckForOverflow(int nBits);
  381. public:
  382. // Get the base pointer.
  383. const unsigned char* GetBasePointer() { return m_pData; }
  384. FORCEINLINE int TotalBytesAvailable( void ) const
  385. {
  386. return m_nDataBytes;
  387. }
  388. // Read a list of bits in..
  389. void ReadBits(void *pOut, int nBits);
  390. float ReadBitAngle( int numbits );
  391. unsigned int ReadUBitLong( int numbits );
  392. unsigned int PeekUBitLong( int numbits );
  393. int ReadSBitLong( int numbits );
  394. // reads an unsigned integer with variable bit length
  395. unsigned int ReadUBitVar();
  396. // reads a varint encoded integer
  397. uint32 ReadVarInt32();
  398. uint64 ReadVarInt64();
  399. int32 ReadSignedVarInt32();
  400. int64 ReadSignedVarInt64();
  401. // You can read signed or unsigned data with this, just cast to
  402. // a signed int if necessary.
  403. unsigned int ReadBitLong(int numbits, bool bSigned);
  404. float ReadBitCoord();
  405. float ReadBitCoordMP( EBitCoordType coordType );
  406. float ReadBitCellCoord( int bits, EBitCoordType coordType );
  407. float ReadBitFloat();
  408. float ReadBitNormal();
  409. void ReadBitVec3Coord( Vector& fa );
  410. void ReadBitVec3Normal( Vector& fa );
  411. void ReadBitAngles( QAngle& fa );
  412. // Byte functions (these still read data in bit-by-bit).
  413. public:
  414. int ReadChar();
  415. int ReadByte();
  416. int ReadShort();
  417. int ReadWord();
  418. int32 ReadLong();
  419. int64 ReadLongLong();
  420. float ReadFloat();
  421. bool ReadBytes(void *pOut, int nBytes);
  422. // Returns false if bufLen isn't large enough to hold the
  423. // string in the buffer.
  424. //
  425. // Always reads to the end of the string (so you can read the
  426. // next piece of data waiting).
  427. //
  428. // If bLine is true, it stops when it reaches a '\n' or a null-terminator.
  429. //
  430. // pStr is always null-terminated (unless bufLen is 0).
  431. //
  432. // pOutNumChars is set to the number of characters left in pStr when the routine is
  433. // complete (this will never exceed bufLen-1).
  434. //
  435. bool ReadString( char *pStr, int bufLen, bool bLine=false, int *pOutNumChars=NULL );
  436. bool ReadWString( wchar_t *pStr, int bufLen, bool bLine=false, int *pOutNumChars=NULL );
  437. // Reads a string and allocates memory for it. If the string in the buffer
  438. // is > 2048 bytes, then pOverflow is set to true (if it's not NULL).
  439. char* ReadAndAllocateString( bool *pOverflow = 0 );
  440. // Status.
  441. public:
  442. int GetNumBytesLeft();
  443. int GetNumBytesRead();
  444. int GetNumBitsLeft();
  445. int GetNumBitsRead() const;
  446. // Has the buffer overflowed?
  447. inline bool IsOverflowed() const {return m_bOverflow;}
  448. inline bool Seek(int iBit); // Seek to a specific bit.
  449. inline bool SeekRelative(int iBitDelta); // Seek to an offset from the current position.
  450. // Called when the buffer is overflowed.
  451. inline void SetOverflowFlag();
  452. public:
  453. // The current buffer.
  454. const unsigned char* m_pData;
  455. int m_nDataBytes;
  456. int m_nDataBits;
  457. // Where we are in the buffer.
  458. int m_iCurBit;
  459. private:
  460. // used by varbit reads internally
  461. inline int CountRunOfZeros();
  462. // Errors?
  463. bool m_bOverflow;
  464. // For debugging..
  465. bool m_bAssertOnOverflow;
  466. const char *m_pDebugName;
  467. };
  468. //-----------------------------------------------------------------------------
  469. // Inlines.
  470. //-----------------------------------------------------------------------------
  471. inline int old_bf_read::GetNumBytesRead()
  472. {
  473. return BitByte(m_iCurBit);
  474. }
  475. inline int old_bf_read::GetNumBitsLeft()
  476. {
  477. return m_nDataBits - m_iCurBit;
  478. }
  479. inline int old_bf_read::GetNumBytesLeft()
  480. {
  481. return GetNumBitsLeft() >> 3;
  482. }
  483. inline int old_bf_read::GetNumBitsRead() const
  484. {
  485. return m_iCurBit;
  486. }
  487. inline void old_bf_read::SetOverflowFlag()
  488. {
  489. if ( m_bAssertOnOverflow )
  490. {
  491. Assert( false );
  492. }
  493. m_bOverflow = true;
  494. }
  495. inline bool old_bf_read::Seek(int iBit)
  496. {
  497. if(iBit < 0 || iBit > m_nDataBits)
  498. {
  499. SetOverflowFlag();
  500. m_iCurBit = m_nDataBits;
  501. return false;
  502. }
  503. else
  504. {
  505. m_iCurBit = iBit;
  506. return true;
  507. }
  508. }
  509. // Seek to an offset from the current position.
  510. inline bool old_bf_read::SeekRelative(int iBitDelta)
  511. {
  512. return Seek(m_iCurBit+iBitDelta);
  513. }
  514. inline bool old_bf_read::CheckForOverflow(int nBits)
  515. {
  516. if( m_iCurBit + nBits > m_nDataBits )
  517. {
  518. SetOverflowFlag();
  519. CallErrorHandler( BITBUFERROR_BUFFER_OVERRUN, GetDebugName() );
  520. }
  521. return m_bOverflow;
  522. }
  523. inline int old_bf_read::ReadOneBitNoCheck()
  524. {
  525. int value = m_pData[m_iCurBit >> 3] & (1 << (m_iCurBit & 7));
  526. ++m_iCurBit;
  527. return !!value;
  528. }
  529. inline int old_bf_read::ReadOneBit()
  530. {
  531. return (!CheckForOverflow(1)) ? ReadOneBitNoCheck() : 0;
  532. }
  533. inline float old_bf_read::ReadBitFloat()
  534. {
  535. int32 val;
  536. Assert(sizeof(float) == sizeof(int32));
  537. Assert(sizeof(float) == 4);
  538. if(CheckForOverflow(32))
  539. return 0.0f;
  540. int bit = m_iCurBit & 0x7;
  541. int byte = m_iCurBit >> 3;
  542. val = m_pData[byte] >> bit;
  543. val |= ((int)m_pData[byte + 1]) << (8 - bit);
  544. val |= ((int)m_pData[byte + 2]) << (16 - bit);
  545. val |= ((int)m_pData[byte + 3]) << (24 - bit);
  546. if (bit != 0)
  547. val |= ((int)m_pData[byte + 4]) << (32 - bit);
  548. m_iCurBit += 32;
  549. return *((float*)&val);
  550. }
  551. inline unsigned int old_bf_read::ReadUBitLong( int numbits )
  552. {
  553. extern uint32 g_ExtraMasks[32];
  554. if ( (m_iCurBit+numbits) > m_nDataBits )
  555. {
  556. m_iCurBit = m_nDataBits;
  557. SetOverflowFlag();
  558. return 0;
  559. }
  560. Assert( numbits > 0 && numbits <= 32 );
  561. // Read the current dword.
  562. int idword1 = m_iCurBit >> 5;
  563. unsigned int dword1 = LoadLittleDWord( (uint32*)m_pData, idword1 );
  564. dword1 >>= (m_iCurBit & 31); // Get the bits we're interested in.
  565. m_iCurBit += numbits;
  566. unsigned int ret = dword1;
  567. // Does it span this dword?
  568. if ( (m_iCurBit-1) >> 5 == idword1 )
  569. {
  570. if (numbits != 32)
  571. ret &= g_ExtraMasks[numbits];
  572. }
  573. else
  574. {
  575. int nExtraBits = m_iCurBit & 31;
  576. unsigned int dword2 = LoadLittleDWord( (uint32*)m_pData, idword1+1 );
  577. dword2 &= g_ExtraMasks[nExtraBits];
  578. // No need to mask since we hit the end of the dword.
  579. // Shift the second dword's part into the high bits.
  580. ret |= (dword2 << (numbits - nExtraBits));
  581. }
  582. return ret;
  583. }
  584. class CBitBuffer
  585. {
  586. public:
  587. char const * m_pDebugName;
  588. bool m_bOverflow;
  589. int m_nDataBits;
  590. size_t m_nDataBytes;
  591. void SetDebugName( char const *pName )
  592. {
  593. m_pDebugName = pName;
  594. }
  595. CBitBuffer( void )
  596. {
  597. m_bOverflow = false;
  598. m_pDebugName = NULL;
  599. m_nDataBits = -1;
  600. m_nDataBytes = 0;
  601. }
  602. FORCEINLINE void SetOverflowFlag( void )
  603. {
  604. m_bOverflow = true;
  605. }
  606. FORCEINLINE bool IsOverflowed( void ) const
  607. {
  608. return m_bOverflow;
  609. }
  610. static const uint32 s_nMaskTable[33]; // 0 1 3 7 15 ..
  611. };
  612. class CBitWrite : public CBitBuffer
  613. {
  614. uint32 m_nOutBufWord;
  615. int m_nOutBitsAvail;
  616. uint32 *m_pDataOut;
  617. uint32 *m_pBufferEnd;
  618. uint32 *m_pData;
  619. bool m_bFlushed;
  620. public:
  621. void StartWriting( void *pData, int nBytes, int iStartBit = 0, int nMaxBits = -1 );
  622. CBitWrite( void *pData, int nBytes, int nBits = -1 )
  623. {
  624. m_bFlushed = false;
  625. StartWriting( pData, nBytes, 0, nBits );
  626. }
  627. CBitWrite( const char *pDebugName, void *pData, int nBytes, int nBits = -1 )
  628. {
  629. m_bFlushed = false;
  630. SetDebugName( pDebugName );
  631. StartWriting( pData, nBytes, 0, nBits );
  632. }
  633. CBitWrite( void )
  634. {
  635. m_bFlushed = false;
  636. }
  637. ~CBitWrite( void )
  638. {
  639. TempFlush();
  640. Assert( (! m_pData ) || m_bFlushed );
  641. }
  642. FORCEINLINE int GetNumBitsLeft( void ) const
  643. {
  644. return m_nOutBitsAvail + ( 32 * ( m_pBufferEnd - m_pDataOut -1 ) );
  645. }
  646. FORCEINLINE void Reset( void )
  647. {
  648. m_bOverflow = false;
  649. m_nOutBitsAvail = 32;
  650. m_pDataOut = m_pData;
  651. m_nOutBufWord = 0;
  652. }
  653. FORCEINLINE void TempFlush( void )
  654. {
  655. // someone wants to know how much data we have written, or the pointer to it, so we'd better make
  656. // sure we write our data
  657. if ( m_nOutBitsAvail != 32 )
  658. {
  659. if ( m_pDataOut == m_pBufferEnd )
  660. {
  661. SetOverflowFlag();
  662. }
  663. else
  664. {
  665. StoreLittleDWord( m_pDataOut, 0, LoadLittleDWord(m_pDataOut,0) & ~s_nMaskTable[ 32 - m_nOutBitsAvail ] | m_nOutBufWord );
  666. }
  667. }
  668. m_bFlushed = true;
  669. }
  670. FORCEINLINE unsigned char *GetBasePointer()
  671. {
  672. TempFlush();
  673. return reinterpret_cast< unsigned char *>( m_pData );
  674. }
  675. FORCEINLINE unsigned char *GetData()
  676. {
  677. return GetBasePointer();
  678. }
  679. FORCEINLINE void Finish();
  680. FORCEINLINE void Flush();
  681. FORCEINLINE void FlushNoCheck();
  682. FORCEINLINE void WriteOneBit(int nValue);
  683. FORCEINLINE void WriteOneBitNoCheck(int nValue);
  684. FORCEINLINE void WriteUBitLong( unsigned int data, int numbits, bool bCheckRange=true );
  685. FORCEINLINE void WriteSBitLong( int data, int numbits );
  686. FORCEINLINE void WriteUBitVar( unsigned int data );
  687. FORCEINLINE void WriteBitFloat( float flValue );
  688. FORCEINLINE void WriteFloat( float flValue );
  689. bool WriteBits(const void *pInData, int nBits);
  690. void WriteBytes( const void *pBuf, int nBytes );
  691. void SeekToBit( int nSeekPos );
  692. FORCEINLINE int GetNumBitsWritten( void ) const
  693. {
  694. return ( 32 - m_nOutBitsAvail ) + ( 32 * ( m_pDataOut - m_pData ) );
  695. }
  696. FORCEINLINE int GetNumBytesWritten( void ) const
  697. {
  698. return ( GetNumBitsWritten() + 7 ) >> 3;
  699. }
  700. FORCEINLINE void WriteLong(int32 val)
  701. {
  702. WriteSBitLong( val, 32 );
  703. }
  704. FORCEINLINE void WriteChar( int val )
  705. {
  706. WriteSBitLong(val, sizeof(char) << 3 );
  707. }
  708. FORCEINLINE void WriteByte( int val )
  709. {
  710. WriteUBitLong(val, sizeof(unsigned char) << 3, false );
  711. }
  712. FORCEINLINE void WriteShort(int val)
  713. {
  714. WriteSBitLong(val, sizeof(short) << 3);
  715. }
  716. FORCEINLINE void WriteWord(int val)
  717. {
  718. WriteUBitLong(val, sizeof(unsigned short) << 3);
  719. }
  720. bool WriteString( const char *pStr );
  721. bool WriteString( const wchar_t *pStr );
  722. void WriteLongLong( int64 val );
  723. void WriteBitAngle( float fAngle, int numbits );
  724. void WriteBitCoord (const float f);
  725. void WriteBitCoordMP( const float f, EBitCoordType coordType );
  726. void WriteBitCellCoord( const float f, int bits, EBitCoordType coordType );
  727. void WriteBitVec3Coord( const Vector& fa );
  728. void WriteBitNormal( float f );
  729. void WriteBitVec3Normal( const Vector& fa );
  730. void WriteBitAngles( const QAngle& fa );
  731. // Copy the bits straight out of pIn. This seeks pIn forward by nBits.
  732. // Returns an error if this buffer or the read buffer overflows.
  733. bool WriteBitsFromBuffer( class bf_read *pIn, int nBits );
  734. };
  735. void CBitWrite::Finish( void )
  736. {
  737. if ( m_nOutBitsAvail != 32 )
  738. {
  739. if ( m_pDataOut == m_pBufferEnd )
  740. {
  741. SetOverflowFlag();
  742. }
  743. StoreLittleDWord( m_pDataOut, 0, m_nOutBufWord );
  744. }
  745. }
  746. void CBitWrite::FlushNoCheck( void )
  747. {
  748. StoreLittleDWord( m_pDataOut++, 0, m_nOutBufWord );
  749. m_nOutBitsAvail = 32;
  750. m_nOutBufWord = 0; // ugh - I need this because of 32 bit writes. a<<=32 is a nop
  751. }
  752. void CBitWrite::Flush( void )
  753. {
  754. if ( m_pDataOut == m_pBufferEnd )
  755. {
  756. SetOverflowFlag();
  757. }
  758. else
  759. {
  760. StoreLittleDWord( m_pDataOut++, 0, m_nOutBufWord );
  761. }
  762. m_nOutBufWord = 0; // ugh - I need this because of 32 bit writes. a<<=32 is a nop
  763. m_nOutBitsAvail = 32;
  764. }
  765. void CBitWrite::WriteOneBitNoCheck( int nValue )
  766. {
  767. m_nOutBufWord |= ( nValue & 1 ) << ( 32 - m_nOutBitsAvail );
  768. if ( --m_nOutBitsAvail == 0 )
  769. {
  770. FlushNoCheck();
  771. }
  772. }
  773. void CBitWrite::WriteOneBit( int nValue )
  774. {
  775. m_nOutBufWord |= ( nValue & 1 ) << ( 32 - m_nOutBitsAvail );
  776. if ( --m_nOutBitsAvail == 0 )
  777. {
  778. Flush();
  779. }
  780. }
  781. FORCEINLINE void CBitWrite::WriteUBitLong( unsigned int nData, int nNumBits, bool bCheckRange )
  782. {
  783. #ifdef _DEBUG
  784. // Make sure it doesn't overflow.
  785. if ( bCheckRange && nNumBits < 32 )
  786. {
  787. Assert( nData <= (uint32)(1 << nNumBits ) );
  788. }
  789. Assert( nNumBits >= 0 && nNumBits <= 32 );
  790. #endif
  791. if ( nNumBits <= m_nOutBitsAvail )
  792. {
  793. if ( bCheckRange )
  794. m_nOutBufWord |= ( nData ) << ( 32 - m_nOutBitsAvail );
  795. else
  796. m_nOutBufWord |= ( nData & s_nMaskTable[ nNumBits] ) << ( 32 - m_nOutBitsAvail );
  797. m_nOutBitsAvail -= nNumBits;
  798. if ( m_nOutBitsAvail == 0 )
  799. {
  800. Flush();
  801. }
  802. }
  803. else
  804. {
  805. // split dwords case
  806. int nOverflowBits = ( nNumBits - m_nOutBitsAvail );
  807. m_nOutBufWord |= ( nData & s_nMaskTable[m_nOutBitsAvail] ) << ( 32 - m_nOutBitsAvail );
  808. Flush();
  809. m_nOutBufWord = ( nData >> ( nNumBits - nOverflowBits ) );
  810. m_nOutBitsAvail = 32 - nOverflowBits;
  811. }
  812. }
  813. FORCEINLINE void CBitWrite::WriteSBitLong( int nData, int nNumBits )
  814. {
  815. WriteUBitLong( ( uint32 ) nData, nNumBits, false );
  816. }
  817. FORCEINLINE void CBitWrite::WriteUBitVar( unsigned int n )
  818. {
  819. if ( n < 16 )
  820. WriteUBitLong( n, 6 );
  821. else
  822. if ( n < 256 )
  823. WriteUBitLong( ( n & 15 ) | 16 | ( ( n & ( 128 | 64 | 32 | 16 ) ) << 2 ), 10 );
  824. else
  825. if ( n < 4096 )
  826. WriteUBitLong( ( n & 15 ) | 32 | ( ( n & ( 2048 | 1024 | 512 | 256 | 128 | 64 | 32 | 16 ) ) << 2 ), 14 );
  827. else
  828. {
  829. WriteUBitLong( ( n & 15 ) | 48, 6 );
  830. WriteUBitLong( ( n >> 4 ), 32 - 4 );
  831. }
  832. }
  833. FORCEINLINE void CBitWrite::WriteBitFloat( float flValue )
  834. {
  835. WriteUBitLong( *((uint32 *) &flValue ), 32 );
  836. }
  837. FORCEINLINE void CBitWrite::WriteFloat( float flValue )
  838. {
  839. // Pre-swap the float, since WriteBits writes raw data
  840. LittleFloat( &flValue, &flValue );
  841. WriteUBitLong( *((uint32 *) &flValue ), 32 );
  842. }
  843. class CBitRead : public CBitBuffer
  844. {
  845. uint32 m_nInBufWord;
  846. int m_nBitsAvail;
  847. uint32 const *m_pDataIn;
  848. uint32 const *m_pBufferEnd;
  849. uint32 const *m_pData;
  850. public:
  851. CBitRead( const void *pData, int nBytes, int nBits = -1 )
  852. {
  853. StartReading( pData, nBytes, 0, nBits );
  854. }
  855. CBitRead( const char *pDebugName, const void *pData, int nBytes, int nBits = -1 )
  856. {
  857. SetDebugName( pDebugName );
  858. StartReading( pData, nBytes, 0, nBits );
  859. }
  860. CBitRead( void ) : CBitBuffer()
  861. {
  862. }
  863. FORCEINLINE int Tell( void ) const
  864. {
  865. return GetNumBitsRead();
  866. }
  867. FORCEINLINE size_t TotalBytesAvailable( void ) const
  868. {
  869. return m_nDataBytes;
  870. }
  871. FORCEINLINE int GetNumBitsLeft() const
  872. {
  873. return m_nDataBits - Tell();
  874. }
  875. FORCEINLINE int GetNumBytesLeft() const
  876. {
  877. return GetNumBitsLeft() >> 3;
  878. }
  879. bool Seek( int nPosition );
  880. FORCEINLINE bool SeekRelative( int nOffset )
  881. {
  882. return Seek( GetNumBitsRead() + nOffset );
  883. }
  884. FORCEINLINE unsigned char const * GetBasePointer()
  885. {
  886. return reinterpret_cast< unsigned char const *>( m_pData );
  887. }
  888. void StartReading( const void *pData, int nBytes, int iStartBit = 0, int nBits = -1 );
  889. FORCEINLINE int GetNumBitsRead( void ) const;
  890. FORCEINLINE int GetNumBytesRead( void ) const;
  891. FORCEINLINE void GrabNextDWord( bool bOverFlowImmediately = false );
  892. FORCEINLINE void FetchNext( void );
  893. FORCEINLINE unsigned int ReadUBitLong( int numbits );
  894. FORCEINLINE int ReadSBitLong( int numbits );
  895. FORCEINLINE unsigned int ReadUBitVar( void );
  896. FORCEINLINE unsigned int PeekUBitLong( int numbits );
  897. FORCEINLINE float ReadBitFloat( void );
  898. float ReadBitCoord();
  899. float ReadBitCoordMP( EBitCoordType coordType );
  900. float ReadBitCellCoord( int bits, EBitCoordType coordType );
  901. float ReadBitNormal();
  902. void ReadBitVec3Coord( Vector& fa );
  903. void ReadBitVec3Normal( Vector& fa );
  904. void ReadBitAngles( QAngle& fa );
  905. bool ReadBytes(void *pOut, int nBytes);
  906. float ReadBitAngle( int numbits );
  907. // Returns 0 or 1.
  908. FORCEINLINE int ReadOneBit( void );
  909. FORCEINLINE int ReadLong( void );
  910. FORCEINLINE int ReadChar( void );
  911. FORCEINLINE int ReadByte( void );
  912. FORCEINLINE int ReadShort( void );
  913. FORCEINLINE int ReadWord( void );
  914. FORCEINLINE float ReadFloat( void );
  915. void ReadBits(void *pOut, int nBits);
  916. // Returns false if bufLen isn't large enough to hold the
  917. // string in the buffer.
  918. //
  919. // Always reads to the end of the string (so you can read the
  920. // next piece of data waiting).
  921. //
  922. // If bLine is true, it stops when it reaches a '\n' or a null-terminator.
  923. //
  924. // pStr is always null-terminated (unless bufLen is 0).
  925. //
  926. // pOutN<umChars is set to the number of characters left in pStr when the routine is
  927. // complete (this will never exceed bufLen-1).
  928. //
  929. bool ReadString( char *pStr, int bufLen, bool bLine=false, int *pOutNumChars=NULL );
  930. bool ReadWString( OUT_Z_CAP(maxLenInChars) wchar_t *pStr, int maxLenInChars, bool bLine=false, int *pOutNumChars=NULL );
  931. char* ReadAndAllocateString( bool *pOverflow = 0 );
  932. int64 ReadLongLong( void );
  933. // reads a varint encoded integer
  934. uint32 ReadVarInt32();
  935. uint64 ReadVarInt64();
  936. int32 ReadSignedVarInt32() { return bitbuf::ZigZagDecode32( ReadVarInt32() ); }
  937. int64 ReadSignedVarInt64() { return bitbuf::ZigZagDecode64( ReadVarInt64() ); }
  938. };
  939. FORCEINLINE int CBitRead::GetNumBitsRead( void ) const
  940. {
  941. if ( ! m_pData ) // pesky null ptr bitbufs. these happen.
  942. return 0;
  943. int nCurOfs = int(((intp(m_pDataIn) - intp(m_pData))/4)-1);
  944. nCurOfs *= 32;
  945. nCurOfs += ( 32 - m_nBitsAvail );
  946. int nAdjust = 8 * ( m_nDataBytes & 3 );
  947. return MIN( nCurOfs + nAdjust, m_nDataBits );
  948. }
  949. FORCEINLINE int CBitRead::GetNumBytesRead( void ) const
  950. {
  951. return ( (GetNumBitsRead()+7) >> 3 );
  952. }
  953. FORCEINLINE void CBitRead::GrabNextDWord( bool bOverFlowImmediately )
  954. {
  955. if ( m_pDataIn == m_pBufferEnd )
  956. {
  957. m_nBitsAvail = 1; // so that next read will run out of words
  958. m_nInBufWord = 0;
  959. m_pDataIn++; // so seek count increments like old
  960. if ( bOverFlowImmediately )
  961. SetOverflowFlag();
  962. }
  963. else
  964. if ( m_pDataIn > m_pBufferEnd )
  965. {
  966. SetOverflowFlag();
  967. m_nInBufWord = 0;
  968. }
  969. else
  970. {
  971. Assert( reinterpret_cast<intp>(m_pDataIn) + 3 < reinterpret_cast<intp>(m_pBufferEnd));
  972. m_nInBufWord = LittleDWord( *( m_pDataIn++ ) );
  973. }
  974. }
  975. FORCEINLINE void CBitRead::FetchNext( void )
  976. {
  977. m_nBitsAvail = 32;
  978. GrabNextDWord( false );
  979. }
  980. int CBitRead::ReadOneBit( void )
  981. {
  982. int nRet = m_nInBufWord & 1;
  983. if ( --m_nBitsAvail == 0 )
  984. {
  985. FetchNext();
  986. }
  987. else
  988. m_nInBufWord >>= 1;
  989. return nRet;
  990. }
  991. unsigned int CBitRead::ReadUBitLong( int numbits )
  992. {
  993. if ( m_nBitsAvail >= numbits )
  994. {
  995. unsigned int nRet = m_nInBufWord & s_nMaskTable[ numbits ];
  996. m_nBitsAvail -= numbits;
  997. if ( m_nBitsAvail )
  998. {
  999. m_nInBufWord >>= numbits;
  1000. }
  1001. else
  1002. {
  1003. FetchNext();
  1004. }
  1005. return nRet;
  1006. }
  1007. else
  1008. {
  1009. // need to merge words
  1010. unsigned int nRet = m_nInBufWord;
  1011. numbits -= m_nBitsAvail;
  1012. GrabNextDWord( true );
  1013. if ( m_bOverflow )
  1014. return 0;
  1015. nRet |= ( ( m_nInBufWord & s_nMaskTable[numbits] ) << m_nBitsAvail );
  1016. m_nBitsAvail = 32 - numbits;
  1017. m_nInBufWord >>= numbits;
  1018. return nRet;
  1019. }
  1020. }
  1021. FORCEINLINE unsigned int CBitRead::PeekUBitLong( int numbits )
  1022. {
  1023. int nSaveBA = m_nBitsAvail;
  1024. int nSaveW = m_nInBufWord;
  1025. uint32 const *pSaveP = m_pDataIn;
  1026. unsigned int nRet = ReadUBitLong( numbits );
  1027. m_nBitsAvail = nSaveBA;
  1028. m_nInBufWord = nSaveW;
  1029. m_pDataIn = pSaveP;
  1030. return nRet;
  1031. }
  1032. FORCEINLINE int CBitRead::ReadSBitLong( int numbits )
  1033. {
  1034. int nRet = ReadUBitLong( numbits );
  1035. // sign extend
  1036. return ( nRet << ( 32 - numbits ) ) >> ( 32 - numbits );
  1037. }
  1038. FORCEINLINE int CBitRead::ReadLong( void )
  1039. {
  1040. return ( int ) ReadUBitLong( sizeof(int32) << 3 );
  1041. }
  1042. FORCEINLINE float CBitRead::ReadFloat( void )
  1043. {
  1044. uint32 nUval = ReadUBitLong( sizeof(int32) << 3 );
  1045. return * ( ( float * ) &nUval );
  1046. }
  1047. #ifdef _WIN32
  1048. #pragma warning(push)
  1049. #pragma warning(disable : 4715) // disable warning on not all cases
  1050. // returning a value. throwing default:
  1051. // in measurably reduces perf in bit
  1052. // packing benchmark
  1053. #endif
  1054. FORCEINLINE unsigned int CBitRead::ReadUBitVar( void )
  1055. {
  1056. unsigned int ret = ReadUBitLong( 6 );
  1057. switch( ret & ( 16 | 32 ) )
  1058. {
  1059. case 16:
  1060. ret = ( ret & 15 ) | ( ReadUBitLong( 4 ) << 4 );
  1061. Assert( ret >= 16);
  1062. break;
  1063. case 32:
  1064. ret = ( ret & 15 ) | ( ReadUBitLong( 8 ) << 4 );
  1065. Assert( ret >= 256);
  1066. break;
  1067. case 48:
  1068. ret = ( ret & 15 ) | ( ReadUBitLong( 32 - 4 ) << 4 );
  1069. Assert( ret >= 4096 );
  1070. break;
  1071. }
  1072. return ret;
  1073. }
  1074. #ifdef _WIN32
  1075. #pragma warning(pop)
  1076. #endif
  1077. FORCEINLINE float CBitRead::ReadBitFloat( void )
  1078. {
  1079. uint32 nvalue = ReadUBitLong( 32 );
  1080. return *( ( float * ) &nvalue );
  1081. }
  1082. int CBitRead::ReadChar( void )
  1083. {
  1084. return ReadSBitLong(sizeof(char) << 3);
  1085. }
  1086. int CBitRead::ReadByte( void )
  1087. {
  1088. return ReadUBitLong(sizeof(unsigned char) << 3);
  1089. }
  1090. int CBitRead::ReadShort( void )
  1091. {
  1092. return ReadSBitLong(sizeof(short) << 3);
  1093. }
  1094. int CBitRead::ReadWord( void )
  1095. {
  1096. return ReadUBitLong(sizeof(unsigned short) << 3);
  1097. }
  1098. #define WRAP_READ( bc ) \
  1099. class bf_read : public bc \
  1100. { \
  1101. public: \
  1102. FORCEINLINE bf_read( void ) : bc( ) \
  1103. { \
  1104. } \
  1105. \
  1106. FORCEINLINE bf_read( const void *pData, int nBytes, int nBits = -1 ) : bc( pData, nBytes, nBits ) \
  1107. { \
  1108. } \
  1109. \
  1110. FORCEINLINE bf_read( const char *pDebugName, const void *pData, int nBytes, int nBits = -1 ) : bc( pDebugName, pData, nBytes, nBits ) \
  1111. { \
  1112. } \
  1113. };
  1114. #if 0
  1115. #define DELEGATE0( t, m ) t m() \
  1116. { \
  1117. Check(); \
  1118. t nOld = old1.m(); \
  1119. t nNew = new1.m(); \
  1120. Assert( nOld == nNew ); \
  1121. Check(); \
  1122. return nOld; \
  1123. }
  1124. #define DELEGATE1( t, m, t1 ) t m( t1 x) \
  1125. { \
  1126. Check(); \
  1127. t nOld = old1.m( x); \
  1128. t nNew = new1.m( x ); \
  1129. Assert( nOld == nNew ); \
  1130. Check(); \
  1131. return nOld; \
  1132. }
  1133. #define DELEGATE0I( m ) DELEGATE0( int, m )
  1134. #define DELEGATE0LL( m ) DELEGATE0( int64, m )
  1135. class bf_read
  1136. {
  1137. old_bf_read old1;
  1138. CBitRead new1;
  1139. void Check( void ) const
  1140. {
  1141. int n=new1.GetNumBitsRead();
  1142. int o=old1.GetNumBitsRead();
  1143. Assert( n == o );
  1144. Assert( old1.IsOverflowed() == new1.IsOverflowed() );
  1145. }
  1146. public:
  1147. FORCEINLINE bf_read( void ) : old1(), new1()
  1148. {
  1149. }
  1150. FORCEINLINE bf_read( const void *pData, int nBytes, int nBits = -1 ) : old1( pData, nBytes, nBits ),new1( pData, nBytes, nBits )
  1151. {
  1152. }
  1153. FORCEINLINE bf_read( const char *pDebugName, const void *pData, int nBytes, int nBits = -1 ) : old1( pDebugName, pData, nBytes, nBits ), new1( pDebugName, pData, nBytes, nBits )
  1154. {
  1155. }
  1156. FORCEINLINE bool IsOverflowed( void ) const
  1157. {
  1158. bool bOld = old1.IsOverflowed();
  1159. bool bNew = new1.IsOverflowed();
  1160. Assert( bOld == bNew );
  1161. Check();
  1162. return bOld;
  1163. }
  1164. void ReadBits(void *pOut, int nBits)
  1165. {
  1166. old1.ReadBits( pOut, nBits );
  1167. void *mem=stackalloc( 1+ ( nBits / 8 ) );
  1168. new1.ReadBits( mem, nBits );
  1169. Assert( memcmp( mem, pOut, nBits / 8 ) == 0 );
  1170. }
  1171. bool ReadBytes(void *pOut, int nBytes)
  1172. {
  1173. ReadBits(pOut, nBytes << 3);
  1174. return ! IsOverflowed();
  1175. }
  1176. unsigned int ReadUBitLong( int numbits )
  1177. {
  1178. unsigned int nOld = old1.ReadUBitLong( numbits );
  1179. unsigned int nNew = new1.ReadUBitLong( numbits );
  1180. Assert( nOld == nNew );
  1181. Check();
  1182. return nOld;
  1183. }
  1184. unsigned const char* GetBasePointer()
  1185. {
  1186. Assert( old1.GetBasePointer() == new1.GetBasePointer() );
  1187. Check();
  1188. return old1.GetBasePointer();
  1189. }
  1190. void SetDebugName( const char *pDebugName )
  1191. {
  1192. old1.SetDebugName( pDebugName );
  1193. new1.SetDebugName( pDebugName );
  1194. Check();
  1195. }
  1196. void StartReading( const void *pData, int nBytes, int iStartBit = 0, int nBits = -1 )
  1197. {
  1198. old1.StartReading( pData, nBytes, iStartBit, nBits );
  1199. new1.StartReading( pData, nBytes, iStartBit, nBits );
  1200. Check();
  1201. }
  1202. void SetAssertOnOverflow( bool bAssert )
  1203. {
  1204. old1.SetAssertOnOverflow( bAssert );
  1205. // new1.SetAssertOnOverflow( bAssert );
  1206. Check();
  1207. }
  1208. DELEGATE0I( ReadOneBit );
  1209. DELEGATE0I( ReadByte );
  1210. DELEGATE0I( ReadWord );
  1211. DELEGATE0I( ReadLong );
  1212. DELEGATE0I( GetNumBytesLeft );
  1213. DELEGATE0I( ReadShort );
  1214. DELEGATE1( int, PeekUBitLong, int );
  1215. DELEGATE0I( ReadChar );
  1216. DELEGATE0I( GetNumBitsRead );
  1217. DELEGATE0LL( ReadLongLong );
  1218. DELEGATE0( float, ReadFloat);
  1219. DELEGATE0( unsigned int, ReadUBitVar );
  1220. DELEGATE0( float, ReadBitCoord);
  1221. DELEGATE2( float, ReadBitCoordMP, bool, bool );
  1222. DELEGATE0( float, ReadBitFloat);
  1223. DELEGATE0( float, ReadBitNormal);
  1224. DELEGATE1( bool,Seek, int );
  1225. DELEGATE1( float, ReadBitAngle, int );
  1226. DELEGATE1( bool,SeekRelative,int);
  1227. DELEGATE0I( GetNumBitsLeft );
  1228. DELEGATE0I( TotalBytesAvailable );
  1229. void SetOverflowFlag()
  1230. {
  1231. old1.SetOverflowFlag();
  1232. new1.SetOverflowFlag();
  1233. Check();
  1234. }
  1235. bool ReadString( char *pStr, int bufLen, bool bLine=false, int *pOutNumChars=NULL )
  1236. {
  1237. Check();
  1238. int oldn, newn;
  1239. bool bOld = old1.ReadString( pStr, bufLen, bLine, &oldn );
  1240. bool bNew = new1.ReadString( pStr, bufLen, bLine, &newn );
  1241. Assert( bOld == bNew );
  1242. Assert( oldn == newn );
  1243. if ( pOutNumChars )
  1244. *pOutNumChars = oldn;
  1245. Check();
  1246. return bOld;
  1247. }
  1248. bool ReadWString( wchar_t *pStr, int bufLen, bool bLine=false, int *pOutNumChars=NULL )
  1249. {
  1250. Check();
  1251. int oldn, newn;
  1252. bool bOld = old1.ReadWString( pStr, bufLen, bLine, &oldn );
  1253. bool bNew = new1.ReadWString( pStr, bufLen, bLine, &newn );
  1254. Assert( bOld == bNew );
  1255. Assert( oldn == newn );
  1256. if ( pOutNumChars )
  1257. *pOutNumChars = oldn;
  1258. Check();
  1259. return bOld;
  1260. }
  1261. void ReadBitVec3Coord( Vector& fa )
  1262. {
  1263. Check();
  1264. old1.ReadBitVec3Coord( fa );
  1265. Vector test;
  1266. new1.ReadBitVec3Coord( test );
  1267. Assert( VectorsAreEqual( fa, test ));
  1268. Check();
  1269. }
  1270. void ReadBitVec3Normal( Vector& fa )
  1271. {
  1272. Check();
  1273. old1.ReadBitVec3Coord( fa );
  1274. Vector test;
  1275. new1.ReadBitVec3Coord( test );
  1276. Assert( VectorsAreEqual( fa, test ));
  1277. Check();
  1278. }
  1279. char* ReadAndAllocateString( bool *pOverflow = NULL )
  1280. {
  1281. Check();
  1282. bool bold, bnew;
  1283. char *pold = old1.ReadAndAllocateString( &bold );
  1284. char *pnew = new1.ReadAndAllocateString( &bnew );
  1285. Assert( bold == bnew );
  1286. Assert(strcmp( pold, pnew ) == 0 );
  1287. delete[] pnew;
  1288. Check();
  1289. if ( pOverflow )
  1290. *pOverflow = bold;
  1291. return pold;
  1292. }
  1293. DELEGATE1( int, ReadSBitLong, int );
  1294. };
  1295. #endif
  1296. WRAP_READ( CBitRead );
  1297. #endif