2023-04-10 15:02:56 +02:00
|
|
|
|
//===== Copyright <20> 1996-2005, Valve Corporation, All rights reserved. ======//
|
2022-04-02 02:48:54 +02:00
|
|
|
|
//
|
2023-04-10 15:02:56 +02:00
|
|
|
|
// Purpose: buffer serialization/deserialization.
|
2022-04-02 02:48:54 +02:00
|
|
|
|
//
|
|
|
|
|
// $NoKeywords: $
|
|
|
|
|
//===========================================================================//
|
|
|
|
|
|
|
|
|
|
#include "core/stdafx.h"
|
|
|
|
|
#include "tier1/bitbuf.h"
|
2023-04-10 15:02:56 +02:00
|
|
|
|
#include "mathlib/swap.h"
|
|
|
|
|
#include "mathlib/bitvec.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Write masks
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2023-04-10 19:14:59 +02:00
|
|
|
|
class CBitWriteMasks
|
2023-04-10 15:02:56 +02:00
|
|
|
|
{
|
|
|
|
|
public:
|
2023-04-10 19:14:59 +02:00
|
|
|
|
CBitWriteMasks()
|
2023-04-10 15:02:56 +02:00
|
|
|
|
{
|
|
|
|
|
for (unsigned int startbit = 0; startbit < 32; startbit++)
|
|
|
|
|
{
|
|
|
|
|
for (unsigned int nBitsLeft = 0; nBitsLeft < 33; nBitsLeft++)
|
|
|
|
|
{
|
|
|
|
|
unsigned int endbit = startbit + nBitsLeft;
|
|
|
|
|
m_BitWriteMasks[startbit][nBitsLeft] = GetBitForBitnum(int(startbit)) - 1;
|
|
|
|
|
if (endbit < 32)
|
|
|
|
|
m_BitWriteMasks[startbit][nBitsLeft] |= ~(GetBitForBitnum(int(endbit)) - 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-02 02:48:54 +02:00
|
|
|
|
|
2023-04-10 15:02:56 +02:00
|
|
|
|
for (unsigned int maskBit = 0; maskBit < 32; maskBit++)
|
|
|
|
|
m_ExtraMasks[maskBit] = GetBitForBitnum(int(maskBit)) - 1;
|
|
|
|
|
m_ExtraMasks[32] = ~0ul;
|
|
|
|
|
|
|
|
|
|
for (unsigned int littleBit = 0; littleBit < 32; littleBit++)
|
|
|
|
|
StoreLittleDWord((unsigned int*)&m_LittleBits[littleBit], 0, 1u << littleBit);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Precalculated bit masks for WriteUBitLong. Using these tables instead of
|
|
|
|
|
// doing the calculations gives a 33% speedup in WriteUBitLong.
|
|
|
|
|
unsigned long m_BitWriteMasks[32][33];
|
|
|
|
|
unsigned long m_LittleBits[32];
|
2023-04-10 19:14:59 +02:00
|
|
|
|
unsigned long m_ExtraMasks[33]; // (1 << i) - 1
|
2022-04-30 03:00:24 +02:00
|
|
|
|
};
|
2023-04-10 19:14:59 +02:00
|
|
|
|
static CBitWriteMasks s_BitWriteMasks;
|
2023-04-10 15:02:56 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Error handler
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
static BitBufErrorHandler g_BitBufErrorHandler = 0;
|
|
|
|
|
void InternalBitBufErrorHandler(BitBufErrorType errorType, const char* pDebugName)
|
|
|
|
|
{
|
|
|
|
|
if (g_BitBufErrorHandler)
|
|
|
|
|
g_BitBufErrorHandler(errorType, pDebugName);
|
|
|
|
|
}
|
|
|
|
|
void SetBitBufErrorHandler(BitBufErrorHandler fn)
|
|
|
|
|
{
|
|
|
|
|
g_BitBufErrorHandler = fn;
|
|
|
|
|
}
|
2022-04-30 03:00:24 +02:00
|
|
|
|
|
2023-04-10 15:02:56 +02:00
|
|
|
|
// ---------------------------------------------------------------------------------------- //
|
|
|
|
|
// CBitBuffer
|
|
|
|
|
// ---------------------------------------------------------------------------------------- //
|
2022-04-30 03:00:24 +02:00
|
|
|
|
|
|
|
|
|
CBitBuffer::CBitBuffer(void)
|
|
|
|
|
{
|
|
|
|
|
m_bOverflow = false;
|
|
|
|
|
m_pDebugName = NULL;
|
|
|
|
|
m_nDataBits = -1;
|
|
|
|
|
m_nDataBytes = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CBitBuffer::SetDebugName(const char* pName)
|
|
|
|
|
{
|
|
|
|
|
m_pDebugName = pName;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-10 13:24:22 +02:00
|
|
|
|
const char* CBitBuffer::GetDebugName() const
|
2022-04-30 03:00:24 +02:00
|
|
|
|
{
|
|
|
|
|
return m_pDebugName;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-10 13:24:22 +02:00
|
|
|
|
bool CBitBuffer::IsOverflowed() const
|
2022-04-30 03:00:24 +02:00
|
|
|
|
{
|
|
|
|
|
return m_bOverflow;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CBitBuffer::SetOverflowFlag()
|
|
|
|
|
{
|
|
|
|
|
m_bOverflow = true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-10 15:02:56 +02:00
|
|
|
|
// ---------------------------------------------------------------------------------------- //
|
|
|
|
|
// bf_read
|
|
|
|
|
// ---------------------------------------------------------------------------------------- //
|
|
|
|
|
CBitRead::CBitRead(const void* pData, int nBytes, int nBits /*= -1*/)
|
|
|
|
|
{
|
|
|
|
|
StartReading(pData, nBytes, 0, nBits);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CBitRead::CBitRead(const char* pDebugName, const void* pData, int nBytes, int nBits /*= -1*/)
|
|
|
|
|
{
|
|
|
|
|
SetDebugName(pDebugName);
|
|
|
|
|
StartReading(pData, nBytes, 0, nBits);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose:
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
void CBitRead::StartReading(const void* pData, size_t nBytes, int64 iStartBit, int64 nBits)
|
|
|
|
|
{
|
|
|
|
|
// Make sure it's dword aligned and padded.
|
|
|
|
|
assert((int64(pData) & 3) == 0);
|
|
|
|
|
m_pData = (uint32*)pData;
|
|
|
|
|
m_pDataIn = m_pData;
|
|
|
|
|
m_nDataBytes = nBytes;
|
|
|
|
|
|
|
|
|
|
if (nBits == -1)
|
|
|
|
|
{
|
|
|
|
|
m_nDataBits = nBytes << 3;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
assert(nBits <= int64(nBytes * 8));
|
|
|
|
|
m_nDataBits = nBits;
|
|
|
|
|
}
|
|
|
|
|
m_bOverflow = false;
|
|
|
|
|
m_pBufferEnd = reinterpret_cast<uint32 const*> (reinterpret_cast<uint8 const*> (m_pData) + nBytes);
|
|
|
|
|
if (m_pData)
|
|
|
|
|
Seek(iStartBit);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: seeks to a specific position in the buffer
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
bool CBitRead::Seek(int64 nPosition)
|
|
|
|
|
{
|
|
|
|
|
bool bSucc = true;
|
|
|
|
|
if (nPosition < 0 || nPosition > m_nDataBits)
|
|
|
|
|
{
|
|
|
|
|
SetOverflowFlag();
|
|
|
|
|
bSucc = false;
|
|
|
|
|
nPosition = m_nDataBits;
|
|
|
|
|
}
|
|
|
|
|
size_t nHead = m_nDataBytes & 3; // non-multiple-of-4 bytes at head of buffer. We put the "round off"
|
|
|
|
|
// at the head to make reading and detecting the end efficient.
|
|
|
|
|
|
|
|
|
|
size_t nByteOfs = nPosition / 8;
|
|
|
|
|
if ((m_nDataBytes < 4) || (nHead && (nByteOfs < nHead)))
|
|
|
|
|
{
|
|
|
|
|
// partial first dword
|
|
|
|
|
uint8 const* pPartial = (uint8 const*)m_pData;
|
|
|
|
|
if (m_pData)
|
|
|
|
|
{
|
|
|
|
|
m_nInBufWord = *(pPartial++);
|
|
|
|
|
if (nHead > 1)
|
|
|
|
|
m_nInBufWord |= (*pPartial++) << 8;
|
|
|
|
|
if (nHead > 2)
|
|
|
|
|
m_nInBufWord |= (*pPartial++) << 16;
|
|
|
|
|
}
|
|
|
|
|
m_pDataIn = (uint32 const*)pPartial;
|
|
|
|
|
m_nInBufWord >>= (nPosition & 31);
|
|
|
|
|
m_nBitsAvail = int((nHead << 3) - (nPosition & 31));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ssize_t nAdjPosition = nPosition - (nHead << 3);
|
|
|
|
|
m_pDataIn = reinterpret_cast<uint32 const*> (
|
|
|
|
|
reinterpret_cast<uint8 const*>(m_pData) + ((nAdjPosition / 32) << 2) + nHead);
|
|
|
|
|
if (m_pData)
|
|
|
|
|
{
|
|
|
|
|
m_nBitsAvail = 32;
|
|
|
|
|
GrabNextDWord();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
m_nInBufWord = 0;
|
|
|
|
|
m_nBitsAvail = 1;
|
|
|
|
|
}
|
|
|
|
|
m_nInBufWord >>= (nAdjPosition & 31);
|
|
|
|
|
m_nBitsAvail = min(m_nBitsAvail, 32 - (nAdjPosition & 31)); // in case grabnextdword overflowed
|
|
|
|
|
}
|
|
|
|
|
return bSucc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose:
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-04-30 03:00:24 +02:00
|
|
|
|
void CBitRead::GrabNextDWord(bool bOverFlowImmediately)
|
|
|
|
|
{
|
|
|
|
|
if (m_pDataIn == m_pBufferEnd)
|
|
|
|
|
{
|
|
|
|
|
m_nBitsAvail = 1;
|
|
|
|
|
m_nInBufWord = 0;
|
|
|
|
|
|
|
|
|
|
m_pDataIn++;
|
|
|
|
|
|
|
|
|
|
if (bOverFlowImmediately)
|
|
|
|
|
SetOverflowFlag();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (m_pDataIn > m_pBufferEnd)
|
|
|
|
|
{
|
|
|
|
|
SetOverflowFlag();
|
|
|
|
|
m_nInBufWord = 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-04-02 17:23:53 +02:00
|
|
|
|
assert(reinterpret_cast<uintptr_t>(m_pDataIn) + 3 < reinterpret_cast<uintptr_t>(m_pBufferEnd));
|
2022-04-30 03:00:24 +02:00
|
|
|
|
m_nInBufWord = LittleDWord(*(m_pDataIn++));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-10 15:02:56 +02:00
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose:
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-04-30 03:00:24 +02:00
|
|
|
|
void CBitRead::FetchNext()
|
|
|
|
|
{
|
|
|
|
|
m_nBitsAvail = 32;
|
|
|
|
|
GrabNextDWord(false);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-10 15:02:56 +02:00
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: reads an unsigned integer from the buffer
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-04-30 03:00:24 +02:00
|
|
|
|
uint32 CBitRead::ReadUBitLong(int numbits)
|
|
|
|
|
{
|
|
|
|
|
if (m_nBitsAvail >= numbits)
|
|
|
|
|
{
|
2023-04-10 15:02:56 +02:00
|
|
|
|
unsigned int nRet = m_nInBufWord & s_BitWriteMasks.m_ExtraMasks[numbits];
|
2022-04-30 03:00:24 +02:00
|
|
|
|
m_nBitsAvail -= numbits;
|
|
|
|
|
if (m_nBitsAvail)
|
|
|
|
|
{
|
|
|
|
|
m_nInBufWord >>= numbits;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
FetchNext();
|
|
|
|
|
}
|
|
|
|
|
return nRet;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
uint32 nRet = m_nInBufWord;
|
|
|
|
|
numbits -= m_nBitsAvail;
|
|
|
|
|
GrabNextDWord(true);
|
|
|
|
|
|
|
|
|
|
if (IsOverflowed())
|
|
|
|
|
return 0;
|
|
|
|
|
|
2023-04-10 15:02:56 +02:00
|
|
|
|
nRet |= ((m_nInBufWord & s_BitWriteMasks.m_ExtraMasks[numbits]) << m_nBitsAvail);
|
2022-04-30 03:00:24 +02:00
|
|
|
|
m_nBitsAvail = 32 - numbits;
|
|
|
|
|
m_nInBufWord >>= numbits;
|
|
|
|
|
|
|
|
|
|
return nRet;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-10 15:02:56 +02:00
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: reads a signed integer from the buffer
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-04-30 03:00:24 +02:00
|
|
|
|
int CBitRead::ReadSBitLong(int numbits)
|
|
|
|
|
{
|
|
|
|
|
int nRet = ReadUBitLong(numbits);
|
|
|
|
|
return (nRet << (32 - numbits)) >> (32 - numbits);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-10 15:02:56 +02:00
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: reads byte from the buffer
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-04-30 03:00:24 +02:00
|
|
|
|
int CBitRead::ReadByte()
|
|
|
|
|
{
|
|
|
|
|
return ReadSBitLong(sizeof(unsigned char) << 3);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-10 15:02:56 +02:00
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: reads character from the buffer
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-04-30 03:00:24 +02:00
|
|
|
|
int CBitRead::ReadChar()
|
|
|
|
|
{
|
|
|
|
|
return ReadSBitLong(sizeof(char) << 3);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-10 15:02:56 +02:00
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: reads a string from the buffer
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-04-30 03:00:24 +02:00
|
|
|
|
bool CBitRead::ReadString(char* pStr, int maxLen, bool bLine, int* pOutNumChars)
|
|
|
|
|
{
|
|
|
|
|
assert(maxLen != 0);
|
|
|
|
|
|
|
|
|
|
bool bTooSmall = false;
|
|
|
|
|
int iChar = 0;
|
|
|
|
|
while (1)
|
|
|
|
|
{
|
2023-04-02 12:09:43 +02:00
|
|
|
|
char val = char(ReadChar());
|
2022-04-30 03:00:24 +02:00
|
|
|
|
if (val == 0)
|
|
|
|
|
break;
|
|
|
|
|
else if (bLine && val == '\n')
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
if (iChar < (maxLen - 1))
|
|
|
|
|
{
|
|
|
|
|
pStr[iChar] = val;
|
|
|
|
|
++iChar;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
bTooSmall = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Make sure it's null-terminated.
|
2023-04-09 23:35:58 +02:00
|
|
|
|
pStr[iChar] = '\0';
|
2022-04-30 03:00:24 +02:00
|
|
|
|
|
|
|
|
|
if (pOutNumChars)
|
|
|
|
|
*pOutNumChars = iChar;
|
|
|
|
|
|
|
|
|
|
return !IsOverflowed() && !bTooSmall;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-10 15:02:56 +02:00
|
|
|
|
// ---------------------------------------------------------------------------------------- //
|
|
|
|
|
// bf_write
|
|
|
|
|
// ---------------------------------------------------------------------------------------- //
|
|
|
|
|
bf_write::bf_write()
|
2022-04-30 03:00:24 +02:00
|
|
|
|
{
|
2023-04-10 15:02:56 +02:00
|
|
|
|
//DEBUG_LINK_CHECK;
|
|
|
|
|
m_pData = NULL;
|
|
|
|
|
m_nDataBytes = 0;
|
|
|
|
|
m_nDataBits = -1; // set to -1 so we generate overflow on any operation
|
|
|
|
|
m_iCurBit = 0;
|
|
|
|
|
m_bOverflow = false;
|
|
|
|
|
m_bAssertOnOverflow = true;
|
|
|
|
|
m_pDebugName = NULL;
|
|
|
|
|
}
|
|
|
|
|
bf_write::bf_write(const char* pDebugName, void* pData, int nBytes, int nBits)
|
|
|
|
|
{
|
|
|
|
|
//DEBUG_LINK_CHECK;
|
|
|
|
|
m_bAssertOnOverflow = true;
|
|
|
|
|
m_pDebugName = pDebugName;
|
|
|
|
|
StartWriting(pData, nBytes, 0, nBits);
|
|
|
|
|
}
|
|
|
|
|
bf_write::bf_write(void* pData, int nBytes, int nBits)
|
|
|
|
|
{
|
|
|
|
|
m_bAssertOnOverflow = true;
|
|
|
|
|
m_pDebugName = NULL;
|
|
|
|
|
StartWriting(pData, nBytes, 0, nBits);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: resets buffer writing
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
void bf_write::Reset()
|
|
|
|
|
{
|
|
|
|
|
m_iCurBit = 0;
|
|
|
|
|
m_bOverflow = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: seeks to a specific bit
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
void bf_write::SeekToBit(int bitPos)
|
|
|
|
|
{
|
|
|
|
|
m_iCurBit = bitPos;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose:
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
void bf_write::StartWriting(void* pData, int nBytes, int iStartBit, int nBits)
|
|
|
|
|
{
|
|
|
|
|
// Make sure it's dword aligned and padded.
|
|
|
|
|
//DEBUG_LINK_CHECK;
|
|
|
|
|
Assert((nBytes % 4) == 0);
|
|
|
|
|
Assert(((uintp)pData & 3) == 0);
|
|
|
|
|
|
|
|
|
|
// The writing code will overrun the end of the buffer if it isn't dword aligned, so truncate to force alignment
|
|
|
|
|
nBytes &= ~3;
|
|
|
|
|
|
|
|
|
|
m_pData = (unsigned char*)pData;
|
|
|
|
|
m_nDataBytes = nBytes;
|
|
|
|
|
|
|
|
|
|
if (nBits == -1)
|
2022-04-30 03:00:24 +02:00
|
|
|
|
{
|
2023-04-10 15:02:56 +02:00
|
|
|
|
m_nDataBits = nBytes << 3;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Assert(nBits <= nBytes * 8);
|
|
|
|
|
m_nDataBits = nBits;
|
2022-04-30 03:00:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-10 15:02:56 +02:00
|
|
|
|
m_iCurBit = iStartBit;
|
|
|
|
|
m_bOverflow = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: writes a bit to the buffer without checking for overflow
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
inline void bf_write::WriteOneBitNoCheck(int nValue)
|
|
|
|
|
{
|
|
|
|
|
if (nValue)
|
|
|
|
|
m_pData[m_iCurBit >> 3] |= (1 << (m_iCurBit & 7));
|
|
|
|
|
else
|
|
|
|
|
m_pData[m_iCurBit >> 3] &= ~(1 << (m_iCurBit & 7));
|
|
|
|
|
|
|
|
|
|
++m_iCurBit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: writes a bit to the buffer
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
inline void bf_write::WriteOneBit(int nValue)
|
|
|
|
|
{
|
|
|
|
|
if (!CheckForOverflow(1))
|
|
|
|
|
WriteOneBitNoCheck(nValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: writes a bit to the buffer at a specific bit index
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
inline void bf_write::WriteOneBitAt(int iBit, int nValue)
|
|
|
|
|
{
|
|
|
|
|
if (iBit + 1 > m_nDataBits)
|
2022-04-30 03:00:24 +02:00
|
|
|
|
{
|
2023-04-10 15:02:56 +02:00
|
|
|
|
SetOverflowFlag();
|
|
|
|
|
CallErrorHandler(BITBUFERROR_BUFFER_OVERRUN, GetDebugName());
|
|
|
|
|
return;
|
2022-04-30 03:00:24 +02:00
|
|
|
|
}
|
2023-04-10 15:02:56 +02:00
|
|
|
|
|
|
|
|
|
if (nValue)
|
|
|
|
|
m_pData[iBit >> 3] |= (1 << (iBit & 7));
|
2022-04-30 03:00:24 +02:00
|
|
|
|
else
|
2023-04-10 15:02:56 +02:00
|
|
|
|
m_pData[iBit >> 3] &= ~(1 << (iBit & 7));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: writes an unsigned integer to the buffer
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
/*BITBUF_INLINE*/ void bf_write::WriteUBitLong(unsigned int curData, int numbits, bool bCheckRange)
|
|
|
|
|
{
|
|
|
|
|
#ifdef _DEBUG
|
|
|
|
|
// Make sure it doesn't overflow.
|
|
|
|
|
if (bCheckRange && numbits < 32)
|
2022-04-30 03:00:24 +02:00
|
|
|
|
{
|
2023-04-10 15:02:56 +02:00
|
|
|
|
if (curData >= (uint32)(1 << numbits))
|
2022-04-30 03:00:24 +02:00
|
|
|
|
{
|
2023-04-10 15:02:56 +02:00
|
|
|
|
CallErrorHandler(BITBUFERROR_VALUE_OUT_OF_RANGE, GetDebugName());
|
2022-04-30 03:00:24 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-10 15:02:56 +02:00
|
|
|
|
Assert(numbits >= 0 && numbits <= 32);
|
2023-04-10 23:45:09 +02:00
|
|
|
|
#else
|
|
|
|
|
NOTE_UNUSED(bCheckRange);
|
2023-04-10 15:02:56 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// Bounds checking..
|
|
|
|
|
if ((m_iCurBit + numbits) > m_nDataBits)
|
|
|
|
|
{
|
|
|
|
|
m_iCurBit = m_nDataBits;
|
|
|
|
|
SetOverflowFlag();
|
|
|
|
|
CallErrorHandler(BITBUFERROR_BUFFER_OVERRUN, GetDebugName());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int nBitsLeft = numbits;
|
|
|
|
|
int iCurBit = m_iCurBit;
|
|
|
|
|
|
|
|
|
|
// Mask in a dword.
|
|
|
|
|
unsigned int iDWord = iCurBit >> 5;
|
|
|
|
|
Assert((iDWord * 4 + sizeof(int32)) <= (unsigned int)m_nDataBytes);
|
|
|
|
|
|
|
|
|
|
uint32 iCurBitMasked = iCurBit & 31;
|
|
|
|
|
|
|
|
|
|
uint32 dword = LoadLittleDWord((uint32*)m_pData, iDWord);
|
|
|
|
|
|
|
|
|
|
dword &= s_BitWriteMasks.m_BitWriteMasks[iCurBitMasked][nBitsLeft];
|
|
|
|
|
dword |= curData << iCurBitMasked;
|
|
|
|
|
|
|
|
|
|
// write to stream (lsb to msb) properly
|
|
|
|
|
StoreLittleDWord((uint32*)m_pData, iDWord, dword);
|
|
|
|
|
|
|
|
|
|
// Did it span a dword?
|
|
|
|
|
int nBitsWritten = 32 - iCurBitMasked;
|
|
|
|
|
if (nBitsWritten < nBitsLeft)
|
|
|
|
|
{
|
|
|
|
|
nBitsLeft -= nBitsWritten;
|
|
|
|
|
curData >>= nBitsWritten;
|
|
|
|
|
|
|
|
|
|
// read from stream (lsb to msb) properly
|
|
|
|
|
dword = LoadLittleDWord((uint32*)m_pData, iDWord + 1);
|
|
|
|
|
|
|
|
|
|
dword &= s_BitWriteMasks.m_BitWriteMasks[0][nBitsLeft];
|
|
|
|
|
dword |= curData;
|
|
|
|
|
|
|
|
|
|
// write to stream (lsb to msb) properly
|
|
|
|
|
StoreLittleDWord((uint32*)m_pData, iDWord + 1, dword);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_iCurBit += numbits;
|
2022-04-30 03:00:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-10 15:02:56 +02:00
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: writes a signed integer to the buffer
|
|
|
|
|
// (Sign bit comes first)
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
void bf_write::WriteSBitLong(int data, int numbits)
|
2022-04-30 03:00:24 +02:00
|
|
|
|
{
|
2023-04-10 15:02:56 +02:00
|
|
|
|
// Do we have a valid # of bits to encode with?
|
|
|
|
|
Assert(numbits >= 1);
|
2022-04-30 03:00:24 +02:00
|
|
|
|
|
2023-04-18 20:14:20 +02:00
|
|
|
|
// Note: it does this weirdness here so it's bit-compatible with regular integer data in the buffer.
|
2023-04-10 15:02:56 +02:00
|
|
|
|
// (Some old code writes direct integers right into the buffer).
|
|
|
|
|
if (data < 0)
|
2022-04-30 03:00:24 +02:00
|
|
|
|
{
|
2023-04-10 15:02:56 +02:00
|
|
|
|
#ifdef _DEBUG
|
|
|
|
|
if (numbits < 32)
|
|
|
|
|
{
|
|
|
|
|
// Make sure it doesn't overflow.
|
|
|
|
|
|
|
|
|
|
if (data < 0)
|
|
|
|
|
{
|
2023-04-10 22:31:42 +02:00
|
|
|
|
Assert(data >= -(GetBitForBitnum(numbits - 1)));
|
2023-04-10 15:02:56 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-04-10 22:31:42 +02:00
|
|
|
|
Assert(data < (GetBitForBitnum(numbits - 1)));
|
2023-04-10 15:02:56 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
WriteUBitLong((unsigned int)(0x80000000 + data), numbits - 1, false);
|
|
|
|
|
WriteOneBit(1);
|
2022-04-30 03:00:24 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-04-10 15:02:56 +02:00
|
|
|
|
WriteUBitLong((unsigned int)data, numbits - 1);
|
|
|
|
|
WriteOneBit(0);
|
2022-04-30 03:00:24 +02:00
|
|
|
|
}
|
2023-04-10 15:02:56 +02:00
|
|
|
|
}
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: writes a signed or unsigned integer to the buffer
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
void bf_write::WriteBitLong(unsigned int data, int numbits, bool bSigned)
|
|
|
|
|
{
|
|
|
|
|
if (bSigned)
|
|
|
|
|
WriteSBitLong((int)data, numbits);
|
|
|
|
|
else
|
|
|
|
|
WriteUBitLong(data, numbits);
|
2022-04-30 03:00:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-10 15:02:56 +02:00
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: writes a list of bits to the buffer
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
bool bf_write::WriteBits(const void* pInData, int nBits)
|
2022-04-02 02:48:54 +02:00
|
|
|
|
{
|
2023-04-10 15:02:56 +02:00
|
|
|
|
#if defined( BB_PROFILING )
|
|
|
|
|
VPROF("bf_write::WriteBits");
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
unsigned char* pIn = (unsigned char*)pInData;
|
|
|
|
|
int nBitsLeft = nBits;
|
|
|
|
|
|
|
|
|
|
// Bounds checking..
|
|
|
|
|
if ((m_iCurBit + nBits) > m_nDataBits)
|
|
|
|
|
{
|
|
|
|
|
SetOverflowFlag();
|
|
|
|
|
CallErrorHandler(BITBUFERROR_BUFFER_OVERRUN, GetDebugName());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Align input to dword boundary
|
|
|
|
|
while (((uintp)pIn & 3) != 0 && nBitsLeft >= 8)
|
|
|
|
|
{
|
|
|
|
|
WriteUBitLong(*pIn, 8, false);
|
|
|
|
|
++pIn;
|
|
|
|
|
nBitsLeft -= 8;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (nBitsLeft >= 32)
|
|
|
|
|
{
|
|
|
|
|
if ((m_iCurBit & 7) == 0)
|
|
|
|
|
{
|
|
|
|
|
// current bit is byte aligned, do block copy
|
|
|
|
|
int numbytes = nBitsLeft >> 3;
|
|
|
|
|
int numbits = numbytes << 3;
|
|
|
|
|
|
|
|
|
|
memcpy(m_pData + (m_iCurBit >> 3), pIn, numbytes);
|
|
|
|
|
pIn += numbytes;
|
|
|
|
|
nBitsLeft -= numbits;
|
|
|
|
|
m_iCurBit += numbits;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
const uint32 iBitsRight = (m_iCurBit & 31);
|
|
|
|
|
Assert(iBitsRight > 0); // should not be aligned, otherwise it would have been handled before
|
|
|
|
|
const uint32 iBitsLeft = 32 - iBitsRight;
|
|
|
|
|
const int iBitsChanging = 32 + iBitsLeft; // how many bits are changed during one step (not necessary written meaningful)
|
|
|
|
|
unsigned int iDWord = m_iCurBit >> 5;
|
|
|
|
|
|
|
|
|
|
uint32 outWord = LoadLittleDWord((uint32*)m_pData, iDWord);
|
|
|
|
|
outWord &= s_BitWriteMasks.m_BitWriteMasks[iBitsRight][32]; // clear rest of beginning DWORD
|
|
|
|
|
|
|
|
|
|
// copy in DWORD blocks
|
|
|
|
|
while (nBitsLeft >= iBitsChanging)
|
|
|
|
|
{
|
|
|
|
|
uint32 curData = LittleDWord(*(uint32*)pIn);
|
|
|
|
|
pIn += sizeof(uint32);
|
|
|
|
|
|
|
|
|
|
outWord |= curData << iBitsRight;
|
|
|
|
|
StoreLittleDWord((uint32*)m_pData, iDWord, outWord);
|
|
|
|
|
|
|
|
|
|
++iDWord;
|
|
|
|
|
outWord = curData >> iBitsLeft;
|
|
|
|
|
|
|
|
|
|
nBitsLeft -= 32;
|
|
|
|
|
m_iCurBit += 32;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// store last word
|
|
|
|
|
StoreLittleDWord((uint32*)m_pData, iDWord, outWord);
|
|
|
|
|
|
|
|
|
|
// write remaining DWORD
|
|
|
|
|
if (nBitsLeft >= 32)
|
|
|
|
|
{
|
|
|
|
|
WriteUBitLong(LittleDWord(*((uint32*)pIn)), 32, false);
|
|
|
|
|
pIn += sizeof(uint32);
|
|
|
|
|
nBitsLeft -= 32;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// write remaining bytes
|
|
|
|
|
while (nBitsLeft >= 8)
|
|
|
|
|
{
|
|
|
|
|
WriteUBitLong(*pIn, 8, false);
|
|
|
|
|
++pIn;
|
|
|
|
|
nBitsLeft -= 8;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// write remaining bits
|
|
|
|
|
if (nBitsLeft)
|
|
|
|
|
{
|
|
|
|
|
WriteUBitLong(*pIn, nBitsLeft, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return !IsOverflowed();
|
2022-04-02 02:48:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-10 15:02:56 +02:00
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: writes a bit into the buffer
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-04-02 02:48:54 +02:00
|
|
|
|
bool bf_write::IsOverflowed() const
|
|
|
|
|
{
|
|
|
|
|
return this->m_bOverflow;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-10 15:02:56 +02:00
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: returns the number of bytes written to the buffer
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-04-02 02:48:54 +02:00
|
|
|
|
int bf_write::GetNumBytesWritten() const
|
|
|
|
|
{
|
|
|
|
|
return BitByte(this->m_iCurBit);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-10 15:02:56 +02:00
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: returns the number of bits written to the buffer
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-04-02 02:48:54 +02:00
|
|
|
|
int bf_write::GetNumBitsWritten() const
|
|
|
|
|
{
|
|
|
|
|
return this->m_iCurBit;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-10 15:02:56 +02:00
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: returns the number of bits in the buffer
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-04-02 02:48:54 +02:00
|
|
|
|
int bf_write::GetMaxNumBits() const
|
|
|
|
|
{
|
|
|
|
|
return this->m_nDataBits;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-10 15:02:56 +02:00
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: returns the number of bits left in the buffer
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-04-02 02:48:54 +02:00
|
|
|
|
int bf_write::GetNumBitsLeft() const
|
|
|
|
|
{
|
|
|
|
|
return this->m_nDataBits - m_iCurBit;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-10 15:02:56 +02:00
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: returns the number of bytes left in the buffer
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-04-02 02:48:54 +02:00
|
|
|
|
int bf_write::GetNumBytesLeft() const
|
|
|
|
|
{
|
|
|
|
|
return this->GetNumBitsLeft() >> 3;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-10 15:02:56 +02:00
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: returns the data pointer
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-06-08 14:35:44 +02:00
|
|
|
|
unsigned char* bf_write::GetData() const
|
2022-04-02 02:48:54 +02:00
|
|
|
|
{
|
|
|
|
|
return this->m_pData;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-10 15:02:56 +02:00
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: returns the debug name
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-06-08 14:35:44 +02:00
|
|
|
|
const char* bf_write::GetDebugName() const
|
2022-04-02 02:48:54 +02:00
|
|
|
|
{
|
2022-06-08 14:35:44 +02:00
|
|
|
|
return this->m_pDebugName;
|
2022-04-02 02:48:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-10 15:02:56 +02:00
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: sets the debug name
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
void bf_write::SetDebugName(const char* pDebugName)
|
|
|
|
|
{
|
|
|
|
|
m_pDebugName = pDebugName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: checks if we have enough space for the requested number of bits
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-04-02 02:48:54 +02:00
|
|
|
|
bool bf_write::CheckForOverflow(int nBits)
|
|
|
|
|
{
|
|
|
|
|
if (this->m_iCurBit + nBits > this->m_nDataBits)
|
|
|
|
|
{
|
|
|
|
|
this->SetOverflowFlag();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this->m_bOverflow;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-10 15:02:56 +02:00
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: sets the overflow flag
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-04-02 02:48:54 +02:00
|
|
|
|
void bf_write::SetOverflowFlag()
|
|
|
|
|
{
|
|
|
|
|
if (this->m_bAssertOnOverflow)
|
|
|
|
|
{
|
2022-04-03 03:10:48 +02:00
|
|
|
|
assert(false);
|
2022-04-02 02:48:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this->m_bOverflow = true;
|
2023-04-10 15:02:56 +02:00
|
|
|
|
}
|