r5sdk/r5dev/tier1/bitbuf.h
Kawe Mazidjatari e6f45aaa9b CBitBuf improvements
* Use proper types reflecting assembly from the engine in 'CBitRead::Seek' (some values are 64bit, either changed by Respawn or the compiler used by Respawn).
* Confirmed changes made to NETMSG_TYPE_BITS (now 511 instead of 255) and NETMSG_LENGTH_BITS (now 12 which is 511 max instead of 255).
* Confirmed msg HUD types.
* Enforce NET_MIN_MESSAGE on 'SVC_UserMessage::Process'.
* Remove unnecessary padding and add proper symbols where padding was actually used.
2022-08-15 02:59:41 +02:00

89 lines
2.0 KiB
C++

#ifndef BITBUF_H
#define BITBUF_H
typedef enum
{
BITBUFERROR_VALUE_OUT_OF_RANGE = 0, // Tried to write a value with too few bits.
BITBUFERROR_BUFFER_OVERRUN, // Was about to overrun a buffer.
BITBUFERROR_NUM_ERRORS
} BitBufErrorType;
#define LittleDWord(val) (val)
//-----------------------------------------------------------------------------
// Used for serialization
//-----------------------------------------------------------------------------
class CBitBuffer
{
public:
CBitBuffer(void);
void SetDebugName(const char* pName);
const char* GetDebugName() const;
bool IsOverflowed() const;
void SetOverflowFlag();
////////////////////////////////////
const char* m_pDebugName;
uint8_t m_bOverflow;
size_t m_nDataBits;
size_t m_nDataBytes;
};
class CBitRead : public CBitBuffer
{
public:
void GrabNextDWord(bool bOverFlowImmediately = false);
void FetchNext();
int ReadSBitLong(int numbits);
uint32 ReadUBitLong(int numbits);
int ReadByte();
int ReadChar();
bool ReadString(char* pStr, int bufLen, bool bLine = false, int* pOutNumChars = nullptr);
void StartReading(const void* pData, size_t nBytes, size_t iStartBit = 0, size_t nBits = -1);
bool Seek(size_t nPosition);
////////////////////////////////////
uint32_t m_nInBufWord;
uint32_t m_nBitsAvail;
const uint32* m_pDataIn;
const uint32* m_pBufferEnd;
const uint32* m_pData;
};
class bf_read : public CBitRead
{
public:
};
struct bf_write
{
public:
// How many bytes are filled in?
int GetNumBytesWritten() const;
int GetNumBitsWritten() const;
int GetMaxNumBits() const;
int GetNumBitsLeft() const;
int GetNumBytesLeft() const;
unsigned char* GetData() const;
const char* GetDebugName() const;
// Has the buffer overflowed?
bool CheckForOverflow(int nBits);
bool IsOverflowed() const;
void SetOverflowFlag();
private:
uint8_t* m_pData;
int m_nDataBytes;
int m_nDataBits;
int m_iCurBit;
bool m_bOverflow;
bool m_bAssertOnOverflow;
const char* m_pDebugName;
};
#endif // BITBUF_H