void CBitRead::GrabNextDWord(bool bOverFlowImmediately) { if (m_pDataIn == m_pBufferEnd) { m_nBitsAvail = 1; // so that next read will run out of words m_nInBufWord = 0; m_pDataIn++; // so seek count increments like old if (bOverFlowImmediately) { SetOverflowFlag(); } } else if (m_pDataIn > m_pBufferEnd) { SetOverflowFlag(); m_nInBufWord = 0; } else { assert(reinterpret_cast<size_t>(m_pDataIn) + 3 < reinterpret_cast<size_t>(m_pBufferEnd)); m_nInBufWord = *(m_pDataIn++); } }
bool bf_write::WriteBits(const void *pInData, int nBits) { #if defined( BB_PROFILING ) VPROF( "bf_write::WriteBits" ); #endif unsigned char *pOut = (unsigned char*)pInData; int nBitsLeft = nBits; if((m_iCurBit+nBits) > m_nDataBits) { SetOverflowFlag(); CallErrorHandler( BITBUFERROR_BUFFER_OVERRUN, GetDebugName() ); return false; } // Get output dword-aligned. while(((unsigned long)pOut & 3) != 0 && nBitsLeft >= 8) { WriteUBitLong( *pOut, 8, false ); ++pOut; nBitsLeft -= 8; } // check if we can use fast memcpy if m_iCurBit is byte aligned if ( (nBitsLeft >= 32) && (m_iCurBit & 7) == 0 ) { int numbytes = (nBitsLeft >> 3); int numbits = numbytes << 3; // Bounds checking.. // TODO: May not need this check anymore if((m_iCurBit+numbits) > m_nDataBits) { m_iCurBit = m_nDataBits; SetOverflowFlag(); CallErrorHandler( BITBUFERROR_BUFFER_OVERRUN, GetDebugName() ); return false; } Q_memcpy( m_pData+(m_iCurBit>>3), pOut, numbytes ); pOut += numbytes; nBitsLeft -= numbits; m_iCurBit += numbits; }
bool old_bf_write::WriteBits(const void *pInData, int nBits) { #if defined( BB_PROFILING ) VPROF( "old_bf_write::WriteBits" ); #endif unsigned char *pOut = (unsigned char*)pInData; int nBitsLeft = nBits; // Bounds checking.. if ( (m_iCurBit+nBits) > m_nDataBits ) { SetOverflowFlag(); CallErrorHandler( BITBUFERROR_BUFFER_OVERRUN, GetDebugName() ); return false; } // Align output to dword boundary while (((unsigned long)pOut & 3) != 0 && nBitsLeft >= 8) { WriteUBitLong( *pOut, 8, false ); ++pOut; nBitsLeft -= 8; } if ( IsPC() && (nBitsLeft >= 32) && (m_iCurBit & 7) == 0 ) { // current bit is byte aligned, do block copy int numbytes = nBitsLeft >> 3; int numbits = numbytes << 3; Q_memcpy( m_pData+(m_iCurBit>>3), pOut, numbytes ); pOut += numbytes; nBitsLeft -= numbits; m_iCurBit += numbits; }